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 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 and prior overflow indicator, and
43 forces the value to fit the type. It returns an overflow indicator. */
47 #include "coretypes.h"
58 #include "langhooks.h"
61 /* The following constants represent a bit based encoding of GCC's
62 comparison operators. This encoding simplifies transformations
63 on relational comparison operators, such as AND and OR. */
64 enum comparison_code {
83 static void encode (HOST_WIDE_INT *, unsigned HOST_WIDE_INT, HOST_WIDE_INT);
84 static void decode (HOST_WIDE_INT *, unsigned HOST_WIDE_INT *, HOST_WIDE_INT *);
85 static bool negate_mathfn_p (enum built_in_function);
86 static bool negate_expr_p (tree);
87 static tree negate_expr (tree);
88 static tree split_tree (tree, enum tree_code, tree *, tree *, tree *, int);
89 static tree associate_trees (tree, tree, enum tree_code, tree);
90 static tree const_binop (enum tree_code, tree, tree, int);
91 static hashval_t size_htab_hash (const void *);
92 static int size_htab_eq (const void *, const void *);
93 static tree fold_convert_const (enum tree_code, tree, tree);
94 static enum tree_code invert_tree_comparison (enum tree_code, bool);
95 static enum comparison_code comparison_to_compcode (enum tree_code);
96 static enum tree_code compcode_to_comparison (enum comparison_code);
97 static tree combine_comparisons (enum tree_code, enum tree_code,
98 enum tree_code, tree, tree, tree);
99 static int truth_value_p (enum tree_code);
100 static int operand_equal_for_comparison_p (tree, tree, tree);
101 static int twoval_comparison_p (tree, tree *, tree *, int *);
102 static tree eval_subst (tree, tree, tree, tree, tree);
103 static tree pedantic_omit_one_operand (tree, tree, tree);
104 static tree distribute_bit_expr (enum tree_code, tree, tree, tree);
105 static tree make_bit_field_ref (tree, tree, int, int, int);
106 static tree optimize_bit_field_compare (enum tree_code, tree, tree, tree);
107 static tree decode_field_reference (tree, HOST_WIDE_INT *, HOST_WIDE_INT *,
108 enum machine_mode *, int *, int *,
110 static int all_ones_mask_p (tree, int);
111 static tree sign_bit_p (tree, tree);
112 static int simple_operand_p (tree);
113 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
114 static tree make_range (tree, int *, tree *, tree *);
115 static tree build_range_check (tree, tree, int, tree, tree);
116 static int merge_ranges (int *, tree *, tree *, int, tree, tree, int, tree,
118 static tree fold_range_test (tree);
119 static tree unextend (tree, int, int, tree);
120 static tree fold_truthop (enum tree_code, tree, tree, tree);
121 static tree optimize_minmax_comparison (tree);
122 static tree extract_muldiv (tree, tree, enum tree_code, tree);
123 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree);
124 static int multiple_of_p (tree, tree, tree);
125 static tree constant_boolean_node (int, tree);
126 static tree fold_binary_op_with_conditional_arg (enum tree_code, tree, tree,
128 static bool fold_real_zero_addition_p (tree, tree, int);
129 static tree fold_mathfn_compare (enum built_in_function, enum tree_code,
131 static tree fold_inf_compare (enum tree_code, tree, tree, tree);
132 static tree fold_div_compare (enum tree_code, tree, tree, tree);
133 static bool reorder_operands_p (tree, tree);
134 static tree fold_negate_const (tree, tree);
135 static tree fold_not_const (tree, tree);
136 static tree fold_relational_const (enum tree_code, tree, tree, tree);
137 static tree fold_relational_hi_lo (enum tree_code *, const 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 /* Make the integer constant T valid for its type by setting to 0 or 1 all
187 the bits in the constant that don't belong in the type.
189 Return 1 if a signed overflow occurs, 0 otherwise. If OVERFLOW is
190 nonzero, a signed overflow has already occurred in calculating T, so
194 force_fit_type (tree t, int overflow)
196 unsigned HOST_WIDE_INT low;
200 if (TREE_CODE (t) == REAL_CST)
202 /* ??? Used to check for overflow here via CHECK_FLOAT_TYPE.
203 Consider doing it via real_convert now. */
207 else if (TREE_CODE (t) != INTEGER_CST)
210 low = TREE_INT_CST_LOW (t);
211 high = TREE_INT_CST_HIGH (t);
213 if (POINTER_TYPE_P (TREE_TYPE (t))
214 || TREE_CODE (TREE_TYPE (t)) == OFFSET_TYPE)
217 prec = TYPE_PRECISION (TREE_TYPE (t));
219 /* First clear all bits that are beyond the type's precision. */
221 if (prec == 2 * HOST_BITS_PER_WIDE_INT)
223 else if (prec > HOST_BITS_PER_WIDE_INT)
224 TREE_INT_CST_HIGH (t)
225 &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
228 TREE_INT_CST_HIGH (t) = 0;
229 if (prec < HOST_BITS_PER_WIDE_INT)
230 TREE_INT_CST_LOW (t) &= ~((unsigned HOST_WIDE_INT) (-1) << prec);
233 /* Unsigned types do not suffer sign extension or overflow unless they
235 if (TYPE_UNSIGNED (TREE_TYPE (t))
236 && ! (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
237 && TYPE_IS_SIZETYPE (TREE_TYPE (t))))
240 /* If the value's sign bit is set, extend the sign. */
241 if (prec != 2 * HOST_BITS_PER_WIDE_INT
242 && (prec > HOST_BITS_PER_WIDE_INT
243 ? 0 != (TREE_INT_CST_HIGH (t)
245 << (prec - HOST_BITS_PER_WIDE_INT - 1)))
246 : 0 != (TREE_INT_CST_LOW (t)
247 & ((unsigned HOST_WIDE_INT) 1 << (prec - 1)))))
249 /* Value is negative:
250 set to 1 all the bits that are outside this type's precision. */
251 if (prec > HOST_BITS_PER_WIDE_INT)
252 TREE_INT_CST_HIGH (t)
253 |= ((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
256 TREE_INT_CST_HIGH (t) = -1;
257 if (prec < HOST_BITS_PER_WIDE_INT)
258 TREE_INT_CST_LOW (t) |= ((unsigned HOST_WIDE_INT) (-1) << prec);
262 /* Return nonzero if signed overflow occurred. */
264 ((overflow | (low ^ TREE_INT_CST_LOW (t)) | (high ^ TREE_INT_CST_HIGH (t)))
268 /* Add two doubleword integers with doubleword result.
269 Each argument is given as two `HOST_WIDE_INT' pieces.
270 One argument is L1 and H1; the other, L2 and H2.
271 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
274 add_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
275 unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
276 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
278 unsigned HOST_WIDE_INT l;
282 h = h1 + h2 + (l < l1);
286 return OVERFLOW_SUM_SIGN (h1, h2, h);
289 /* Negate a doubleword integer with doubleword result.
290 Return nonzero if the operation overflows, assuming it's signed.
291 The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
292 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
295 neg_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
296 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
302 return (*hv & h1) < 0;
312 /* Multiply two doubleword integers with doubleword result.
313 Return nonzero if the operation overflows, assuming it's signed.
314 Each argument is given as two `HOST_WIDE_INT' pieces.
315 One argument is L1 and H1; the other, L2 and H2.
316 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
319 mul_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
320 unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
321 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
323 HOST_WIDE_INT arg1[4];
324 HOST_WIDE_INT arg2[4];
325 HOST_WIDE_INT prod[4 * 2];
326 unsigned HOST_WIDE_INT carry;
328 unsigned HOST_WIDE_INT toplow, neglow;
329 HOST_WIDE_INT tophigh, neghigh;
331 encode (arg1, l1, h1);
332 encode (arg2, l2, h2);
334 memset (prod, 0, sizeof prod);
336 for (i = 0; i < 4; i++)
339 for (j = 0; j < 4; j++)
342 /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000. */
343 carry += arg1[i] * arg2[j];
344 /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF. */
346 prod[k] = LOWPART (carry);
347 carry = HIGHPART (carry);
352 decode (prod, lv, hv); /* This ignores prod[4] through prod[4*2-1] */
354 /* Check for overflow by calculating the top half of the answer in full;
355 it should agree with the low half's sign bit. */
356 decode (prod + 4, &toplow, &tophigh);
359 neg_double (l2, h2, &neglow, &neghigh);
360 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
364 neg_double (l1, h1, &neglow, &neghigh);
365 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
367 return (*hv < 0 ? ~(toplow & tophigh) : toplow | tophigh) != 0;
370 /* Shift the doubleword integer in L1, H1 left by COUNT places
371 keeping only PREC bits of result.
372 Shift right if COUNT is negative.
373 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
374 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
377 lshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
378 HOST_WIDE_INT count, unsigned int prec,
379 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, int arith)
381 unsigned HOST_WIDE_INT signmask;
385 rshift_double (l1, h1, -count, prec, lv, hv, arith);
389 if (SHIFT_COUNT_TRUNCATED)
392 if (count >= 2 * HOST_BITS_PER_WIDE_INT)
394 /* Shifting by the host word size is undefined according to the
395 ANSI standard, so we must handle this as a special case. */
399 else if (count >= HOST_BITS_PER_WIDE_INT)
401 *hv = l1 << (count - HOST_BITS_PER_WIDE_INT);
406 *hv = (((unsigned HOST_WIDE_INT) h1 << count)
407 | (l1 >> (HOST_BITS_PER_WIDE_INT - count - 1) >> 1));
411 /* Sign extend all bits that are beyond the precision. */
413 signmask = -((prec > HOST_BITS_PER_WIDE_INT
414 ? ((unsigned HOST_WIDE_INT) *hv
415 >> (prec - HOST_BITS_PER_WIDE_INT - 1))
416 : (*lv >> (prec - 1))) & 1);
418 if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
420 else if (prec >= HOST_BITS_PER_WIDE_INT)
422 *hv &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
423 *hv |= signmask << (prec - HOST_BITS_PER_WIDE_INT);
428 *lv &= ~((unsigned HOST_WIDE_INT) (-1) << prec);
429 *lv |= signmask << prec;
433 /* Shift the doubleword integer in L1, H1 right by COUNT places
434 keeping only PREC bits of result. COUNT must be positive.
435 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
436 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
439 rshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
440 HOST_WIDE_INT count, unsigned int prec,
441 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
444 unsigned HOST_WIDE_INT signmask;
447 ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
450 if (SHIFT_COUNT_TRUNCATED)
453 if (count >= 2 * HOST_BITS_PER_WIDE_INT)
455 /* Shifting by the host word size is undefined according to the
456 ANSI standard, so we must handle this as a special case. */
460 else if (count >= HOST_BITS_PER_WIDE_INT)
463 *lv = (unsigned HOST_WIDE_INT) h1 >> (count - HOST_BITS_PER_WIDE_INT);
467 *hv = (unsigned HOST_WIDE_INT) h1 >> count;
469 | ((unsigned HOST_WIDE_INT) h1 << (HOST_BITS_PER_WIDE_INT - count - 1) << 1));
472 /* Zero / sign extend all bits that are beyond the precision. */
474 if (count >= (HOST_WIDE_INT)prec)
479 else if ((prec - count) >= 2 * HOST_BITS_PER_WIDE_INT)
481 else if ((prec - count) >= HOST_BITS_PER_WIDE_INT)
483 *hv &= ~((HOST_WIDE_INT) (-1) << (prec - count - HOST_BITS_PER_WIDE_INT));
484 *hv |= signmask << (prec - count - HOST_BITS_PER_WIDE_INT);
489 *lv &= ~((unsigned HOST_WIDE_INT) (-1) << (prec - count));
490 *lv |= signmask << (prec - count);
494 /* Rotate the doubleword integer in L1, H1 left by COUNT places
495 keeping only PREC bits of result.
496 Rotate right if COUNT is negative.
497 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
500 lrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
501 HOST_WIDE_INT count, unsigned int prec,
502 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
504 unsigned HOST_WIDE_INT s1l, s2l;
505 HOST_WIDE_INT s1h, s2h;
511 lshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
512 rshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
517 /* Rotate the doubleword integer in L1, H1 left by COUNT places
518 keeping only PREC bits of result. COUNT must be positive.
519 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
522 rrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
523 HOST_WIDE_INT count, unsigned int prec,
524 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
526 unsigned HOST_WIDE_INT s1l, s2l;
527 HOST_WIDE_INT s1h, s2h;
533 rshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
534 lshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
539 /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
540 for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
541 CODE is a tree code for a kind of division, one of
542 TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
544 It controls how the quotient is rounded to an integer.
545 Return nonzero if the operation overflows.
546 UNS nonzero says do unsigned division. */
549 div_and_round_double (enum tree_code code, int uns,
550 unsigned HOST_WIDE_INT lnum_orig, /* num == numerator == dividend */
551 HOST_WIDE_INT hnum_orig,
552 unsigned HOST_WIDE_INT lden_orig, /* den == denominator == divisor */
553 HOST_WIDE_INT hden_orig,
554 unsigned HOST_WIDE_INT *lquo,
555 HOST_WIDE_INT *hquo, unsigned HOST_WIDE_INT *lrem,
559 HOST_WIDE_INT num[4 + 1]; /* extra element for scaling. */
560 HOST_WIDE_INT den[4], quo[4];
562 unsigned HOST_WIDE_INT work;
563 unsigned HOST_WIDE_INT carry = 0;
564 unsigned HOST_WIDE_INT lnum = lnum_orig;
565 HOST_WIDE_INT hnum = hnum_orig;
566 unsigned HOST_WIDE_INT lden = lden_orig;
567 HOST_WIDE_INT hden = hden_orig;
570 if (hden == 0 && lden == 0)
571 overflow = 1, lden = 1;
573 /* Calculate quotient sign and convert operands to unsigned. */
579 /* (minimum integer) / (-1) is the only overflow case. */
580 if (neg_double (lnum, hnum, &lnum, &hnum)
581 && ((HOST_WIDE_INT) lden & hden) == -1)
587 neg_double (lden, hden, &lden, &hden);
591 if (hnum == 0 && hden == 0)
592 { /* single precision */
594 /* This unsigned division rounds toward zero. */
600 { /* trivial case: dividend < divisor */
601 /* hden != 0 already checked. */
608 memset (quo, 0, sizeof quo);
610 memset (num, 0, sizeof num); /* to zero 9th element */
611 memset (den, 0, sizeof den);
613 encode (num, lnum, hnum);
614 encode (den, lden, hden);
616 /* Special code for when the divisor < BASE. */
617 if (hden == 0 && lden < (unsigned HOST_WIDE_INT) BASE)
619 /* hnum != 0 already checked. */
620 for (i = 4 - 1; i >= 0; i--)
622 work = num[i] + carry * BASE;
623 quo[i] = work / lden;
629 /* Full double precision division,
630 with thanks to Don Knuth's "Seminumerical Algorithms". */
631 int num_hi_sig, den_hi_sig;
632 unsigned HOST_WIDE_INT quo_est, scale;
634 /* Find the highest nonzero divisor digit. */
635 for (i = 4 - 1;; i--)
642 /* Insure that the first digit of the divisor is at least BASE/2.
643 This is required by the quotient digit estimation algorithm. */
645 scale = BASE / (den[den_hi_sig] + 1);
647 { /* scale divisor and dividend */
649 for (i = 0; i <= 4 - 1; i++)
651 work = (num[i] * scale) + carry;
652 num[i] = LOWPART (work);
653 carry = HIGHPART (work);
658 for (i = 0; i <= 4 - 1; i++)
660 work = (den[i] * scale) + carry;
661 den[i] = LOWPART (work);
662 carry = HIGHPART (work);
663 if (den[i] != 0) den_hi_sig = i;
670 for (i = num_hi_sig - den_hi_sig - 1; i >= 0; i--)
672 /* Guess the next quotient digit, quo_est, by dividing the first
673 two remaining dividend digits by the high order quotient digit.
674 quo_est is never low and is at most 2 high. */
675 unsigned HOST_WIDE_INT tmp;
677 num_hi_sig = i + den_hi_sig + 1;
678 work = num[num_hi_sig] * BASE + num[num_hi_sig - 1];
679 if (num[num_hi_sig] != den[den_hi_sig])
680 quo_est = work / den[den_hi_sig];
684 /* Refine quo_est so it's usually correct, and at most one high. */
685 tmp = work - quo_est * den[den_hi_sig];
687 && (den[den_hi_sig - 1] * quo_est
688 > (tmp * BASE + num[num_hi_sig - 2])))
691 /* Try QUO_EST as the quotient digit, by multiplying the
692 divisor by QUO_EST and subtracting from the remaining dividend.
693 Keep in mind that QUO_EST is the I - 1st digit. */
696 for (j = 0; j <= den_hi_sig; j++)
698 work = quo_est * den[j] + carry;
699 carry = HIGHPART (work);
700 work = num[i + j] - LOWPART (work);
701 num[i + j] = LOWPART (work);
702 carry += HIGHPART (work) != 0;
705 /* If quo_est was high by one, then num[i] went negative and
706 we need to correct things. */
707 if (num[num_hi_sig] < (HOST_WIDE_INT) carry)
710 carry = 0; /* add divisor back in */
711 for (j = 0; j <= den_hi_sig; j++)
713 work = num[i + j] + den[j] + carry;
714 carry = HIGHPART (work);
715 num[i + j] = LOWPART (work);
718 num [num_hi_sig] += carry;
721 /* Store the quotient digit. */
726 decode (quo, lquo, hquo);
729 /* If result is negative, make it so. */
731 neg_double (*lquo, *hquo, lquo, hquo);
733 /* Compute trial remainder: rem = num - (quo * den) */
734 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
735 neg_double (*lrem, *hrem, lrem, hrem);
736 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
741 case TRUNC_MOD_EXPR: /* round toward zero */
742 case EXACT_DIV_EXPR: /* for this one, it shouldn't matter */
746 case FLOOR_MOD_EXPR: /* round toward negative infinity */
747 if (quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio < 0 && rem != 0 */
750 add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1,
758 case CEIL_MOD_EXPR: /* round toward positive infinity */
759 if (!quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio > 0 && rem != 0 */
761 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
769 case ROUND_MOD_EXPR: /* round to closest integer */
771 unsigned HOST_WIDE_INT labs_rem = *lrem;
772 HOST_WIDE_INT habs_rem = *hrem;
773 unsigned HOST_WIDE_INT labs_den = lden, ltwice;
774 HOST_WIDE_INT habs_den = hden, htwice;
776 /* Get absolute values. */
778 neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
780 neg_double (lden, hden, &labs_den, &habs_den);
782 /* If (2 * abs (lrem) >= abs (lden)) */
783 mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0,
784 labs_rem, habs_rem, <wice, &htwice);
786 if (((unsigned HOST_WIDE_INT) habs_den
787 < (unsigned HOST_WIDE_INT) htwice)
788 || (((unsigned HOST_WIDE_INT) habs_den
789 == (unsigned HOST_WIDE_INT) htwice)
790 && (labs_den < ltwice)))
794 add_double (*lquo, *hquo,
795 (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
798 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
810 /* Compute true remainder: rem = num - (quo * den) */
811 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
812 neg_double (*lrem, *hrem, lrem, hrem);
813 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
817 /* Return true if built-in mathematical function specified by CODE
818 preserves the sign of it argument, i.e. -f(x) == f(-x). */
821 negate_mathfn_p (enum built_in_function code)
845 /* Determine whether an expression T can be cheaply negated using
846 the function negate_expr. */
849 negate_expr_p (tree t)
851 unsigned HOST_WIDE_INT val;
858 type = TREE_TYPE (t);
861 switch (TREE_CODE (t))
864 if (TYPE_UNSIGNED (type) || ! flag_trapv)
867 /* Check that -CST will not overflow type. */
868 prec = TYPE_PRECISION (type);
869 if (prec > HOST_BITS_PER_WIDE_INT)
871 if (TREE_INT_CST_LOW (t) != 0)
873 prec -= HOST_BITS_PER_WIDE_INT;
874 val = TREE_INT_CST_HIGH (t);
877 val = TREE_INT_CST_LOW (t);
878 if (prec < HOST_BITS_PER_WIDE_INT)
879 val &= ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
880 return val != ((unsigned HOST_WIDE_INT) 1 << (prec - 1));
887 return negate_expr_p (TREE_REALPART (t))
888 && negate_expr_p (TREE_IMAGPART (t));
891 if (FLOAT_TYPE_P (type) && !flag_unsafe_math_optimizations)
893 /* -(A + B) -> (-B) - A. */
894 if (negate_expr_p (TREE_OPERAND (t, 1))
895 && reorder_operands_p (TREE_OPERAND (t, 0),
896 TREE_OPERAND (t, 1)))
898 /* -(A + B) -> (-A) - B. */
899 return negate_expr_p (TREE_OPERAND (t, 0));
902 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
903 return (! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
904 && reorder_operands_p (TREE_OPERAND (t, 0),
905 TREE_OPERAND (t, 1));
908 if (TYPE_UNSIGNED (TREE_TYPE (t)))
914 if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
915 return negate_expr_p (TREE_OPERAND (t, 1))
916 || negate_expr_p (TREE_OPERAND (t, 0));
920 /* Negate -((double)float) as (double)(-float). */
921 if (TREE_CODE (type) == REAL_TYPE)
923 tree tem = strip_float_extensions (t);
925 return negate_expr_p (tem);
930 /* Negate -f(x) as f(-x). */
931 if (negate_mathfn_p (builtin_mathfn_code (t)))
932 return negate_expr_p (TREE_VALUE (TREE_OPERAND (t, 1)));
936 /* Optimize -((int)x >> 31) into (unsigned)x >> 31. */
937 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
939 tree op1 = TREE_OPERAND (t, 1);
940 if (TREE_INT_CST_HIGH (op1) == 0
941 && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
942 == TREE_INT_CST_LOW (op1))
953 /* Given T, an expression, return the negation of T. Allow for T to be
954 null, in which case return null. */
965 type = TREE_TYPE (t);
968 switch (TREE_CODE (t))
971 tem = fold_negate_const (t, type);
972 if (! TREE_OVERFLOW (tem)
973 || TYPE_UNSIGNED (type)
979 tem = fold_negate_const (t, type);
980 /* Two's complement FP formats, such as c4x, may overflow. */
981 if (! TREE_OVERFLOW (tem) || ! flag_trapping_math)
982 return fold_convert (type, tem);
987 tree rpart = negate_expr (TREE_REALPART (t));
988 tree ipart = negate_expr (TREE_IMAGPART (t));
990 if ((TREE_CODE (rpart) == REAL_CST
991 && TREE_CODE (ipart) == REAL_CST)
992 || (TREE_CODE (rpart) == INTEGER_CST
993 && TREE_CODE (ipart) == INTEGER_CST))
994 return build_complex (type, rpart, ipart);
999 return fold_convert (type, TREE_OPERAND (t, 0));
1002 if (! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
1004 /* -(A + B) -> (-B) - A. */
1005 if (negate_expr_p (TREE_OPERAND (t, 1))
1006 && reorder_operands_p (TREE_OPERAND (t, 0),
1007 TREE_OPERAND (t, 1)))
1009 tem = negate_expr (TREE_OPERAND (t, 1));
1010 tem = fold (build2 (MINUS_EXPR, TREE_TYPE (t),
1011 tem, TREE_OPERAND (t, 0)));
1012 return fold_convert (type, tem);
1015 /* -(A + B) -> (-A) - B. */
1016 if (negate_expr_p (TREE_OPERAND (t, 0)))
1018 tem = negate_expr (TREE_OPERAND (t, 0));
1019 tem = fold (build2 (MINUS_EXPR, TREE_TYPE (t),
1020 tem, TREE_OPERAND (t, 1)));
1021 return fold_convert (type, tem);
1027 /* - (A - B) -> B - A */
1028 if ((! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
1029 && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
1030 return fold_convert (type,
1031 fold (build2 (MINUS_EXPR, TREE_TYPE (t),
1032 TREE_OPERAND (t, 1),
1033 TREE_OPERAND (t, 0))));
1037 if (TYPE_UNSIGNED (TREE_TYPE (t)))
1043 if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
1045 tem = TREE_OPERAND (t, 1);
1046 if (negate_expr_p (tem))
1047 return fold_convert (type,
1048 fold (build2 (TREE_CODE (t), TREE_TYPE (t),
1049 TREE_OPERAND (t, 0),
1050 negate_expr (tem))));
1051 tem = TREE_OPERAND (t, 0);
1052 if (negate_expr_p (tem))
1053 return fold_convert (type,
1054 fold (build2 (TREE_CODE (t), TREE_TYPE (t),
1056 TREE_OPERAND (t, 1))));
1061 /* Convert -((double)float) into (double)(-float). */
1062 if (TREE_CODE (type) == REAL_TYPE)
1064 tem = strip_float_extensions (t);
1065 if (tem != t && negate_expr_p (tem))
1066 return fold_convert (type, negate_expr (tem));
1071 /* Negate -f(x) as f(-x). */
1072 if (negate_mathfn_p (builtin_mathfn_code (t))
1073 && negate_expr_p (TREE_VALUE (TREE_OPERAND (t, 1))))
1075 tree fndecl, arg, arglist;
1077 fndecl = get_callee_fndecl (t);
1078 arg = negate_expr (TREE_VALUE (TREE_OPERAND (t, 1)));
1079 arglist = build_tree_list (NULL_TREE, arg);
1080 return build_function_call_expr (fndecl, arglist);
1085 /* Optimize -((int)x >> 31) into (unsigned)x >> 31. */
1086 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1088 tree op1 = TREE_OPERAND (t, 1);
1089 if (TREE_INT_CST_HIGH (op1) == 0
1090 && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1091 == TREE_INT_CST_LOW (op1))
1093 tree ntype = TYPE_UNSIGNED (type)
1094 ? lang_hooks.types.signed_type (type)
1095 : lang_hooks.types.unsigned_type (type);
1096 tree temp = fold_convert (ntype, TREE_OPERAND (t, 0));
1097 temp = fold (build2 (RSHIFT_EXPR, ntype, temp, op1));
1098 return fold_convert (type, temp);
1107 tem = fold (build1 (NEGATE_EXPR, TREE_TYPE (t), t));
1108 return fold_convert (type, tem);
1111 /* Split a tree IN into a constant, literal and variable parts that could be
1112 combined with CODE to make IN. "constant" means an expression with
1113 TREE_CONSTANT but that isn't an actual constant. CODE must be a
1114 commutative arithmetic operation. Store the constant part into *CONP,
1115 the literal in *LITP and return the variable part. If a part isn't
1116 present, set it to null. If the tree does not decompose in this way,
1117 return the entire tree as the variable part and the other parts as null.
1119 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
1120 case, we negate an operand that was subtracted. Except if it is a
1121 literal for which we use *MINUS_LITP instead.
1123 If NEGATE_P is true, we are negating all of IN, again except a literal
1124 for which we use *MINUS_LITP instead.
1126 If IN is itself a literal or constant, return it as appropriate.
1128 Note that we do not guarantee that any of the three values will be the
1129 same type as IN, but they will have the same signedness and mode. */
1132 split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
1133 tree *minus_litp, int negate_p)
1141 /* Strip any conversions that don't change the machine mode or signedness. */
1142 STRIP_SIGN_NOPS (in);
1144 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST)
1146 else if (TREE_CODE (in) == code
1147 || (! FLOAT_TYPE_P (TREE_TYPE (in))
1148 /* We can associate addition and subtraction together (even
1149 though the C standard doesn't say so) for integers because
1150 the value is not affected. For reals, the value might be
1151 affected, so we can't. */
1152 && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
1153 || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
1155 tree op0 = TREE_OPERAND (in, 0);
1156 tree op1 = TREE_OPERAND (in, 1);
1157 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
1158 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
1160 /* First see if either of the operands is a literal, then a constant. */
1161 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST)
1162 *litp = op0, op0 = 0;
1163 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST)
1164 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
1166 if (op0 != 0 && TREE_CONSTANT (op0))
1167 *conp = op0, op0 = 0;
1168 else if (op1 != 0 && TREE_CONSTANT (op1))
1169 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
1171 /* If we haven't dealt with either operand, this is not a case we can
1172 decompose. Otherwise, VAR is either of the ones remaining, if any. */
1173 if (op0 != 0 && op1 != 0)
1178 var = op1, neg_var_p = neg1_p;
1180 /* Now do any needed negations. */
1182 *minus_litp = *litp, *litp = 0;
1184 *conp = negate_expr (*conp);
1186 var = negate_expr (var);
1188 else if (TREE_CONSTANT (in))
1196 *minus_litp = *litp, *litp = 0;
1197 else if (*minus_litp)
1198 *litp = *minus_litp, *minus_litp = 0;
1199 *conp = negate_expr (*conp);
1200 var = negate_expr (var);
1206 /* Re-associate trees split by the above function. T1 and T2 are either
1207 expressions to associate or null. Return the new expression, if any. If
1208 we build an operation, do it in TYPE and with CODE. */
1211 associate_trees (tree t1, tree t2, enum tree_code code, tree type)
1218 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
1219 try to fold this since we will have infinite recursion. But do
1220 deal with any NEGATE_EXPRs. */
1221 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
1222 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
1224 if (code == PLUS_EXPR)
1226 if (TREE_CODE (t1) == NEGATE_EXPR)
1227 return build2 (MINUS_EXPR, type, fold_convert (type, t2),
1228 fold_convert (type, TREE_OPERAND (t1, 0)));
1229 else if (TREE_CODE (t2) == NEGATE_EXPR)
1230 return build2 (MINUS_EXPR, type, fold_convert (type, t1),
1231 fold_convert (type, TREE_OPERAND (t2, 0)));
1233 return build2 (code, type, fold_convert (type, t1),
1234 fold_convert (type, t2));
1237 return fold (build2 (code, type, fold_convert (type, t1),
1238 fold_convert (type, t2)));
1241 /* Combine two integer constants ARG1 and ARG2 under operation CODE
1242 to produce a new constant.
1244 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
1247 int_const_binop (enum tree_code code, tree arg1, tree arg2, int notrunc)
1249 unsigned HOST_WIDE_INT int1l, int2l;
1250 HOST_WIDE_INT int1h, int2h;
1251 unsigned HOST_WIDE_INT low;
1253 unsigned HOST_WIDE_INT garbagel;
1254 HOST_WIDE_INT garbageh;
1256 tree type = TREE_TYPE (arg1);
1257 int uns = TYPE_UNSIGNED (type);
1259 = (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type));
1261 int no_overflow = 0;
1263 int1l = TREE_INT_CST_LOW (arg1);
1264 int1h = TREE_INT_CST_HIGH (arg1);
1265 int2l = TREE_INT_CST_LOW (arg2);
1266 int2h = TREE_INT_CST_HIGH (arg2);
1271 low = int1l | int2l, hi = int1h | int2h;
1275 low = int1l ^ int2l, hi = int1h ^ int2h;
1279 low = int1l & int2l, hi = int1h & int2h;
1285 /* It's unclear from the C standard whether shifts can overflow.
1286 The following code ignores overflow; perhaps a C standard
1287 interpretation ruling is needed. */
1288 lshift_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1296 lrotate_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1301 overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
1305 neg_double (int2l, int2h, &low, &hi);
1306 add_double (int1l, int1h, low, hi, &low, &hi);
1307 overflow = OVERFLOW_SUM_SIGN (hi, int2h, int1h);
1311 overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
1314 case TRUNC_DIV_EXPR:
1315 case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1316 case EXACT_DIV_EXPR:
1317 /* This is a shortcut for a common special case. */
1318 if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1319 && ! TREE_CONSTANT_OVERFLOW (arg1)
1320 && ! TREE_CONSTANT_OVERFLOW (arg2)
1321 && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1323 if (code == CEIL_DIV_EXPR)
1326 low = int1l / int2l, hi = 0;
1330 /* ... fall through ... */
1332 case ROUND_DIV_EXPR:
1333 if (int2h == 0 && int2l == 1)
1335 low = int1l, hi = int1h;
1338 if (int1l == int2l && int1h == int2h
1339 && ! (int1l == 0 && int1h == 0))
1344 overflow = div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
1345 &low, &hi, &garbagel, &garbageh);
1348 case TRUNC_MOD_EXPR:
1349 case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1350 /* This is a shortcut for a common special case. */
1351 if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1352 && ! TREE_CONSTANT_OVERFLOW (arg1)
1353 && ! TREE_CONSTANT_OVERFLOW (arg2)
1354 && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1356 if (code == CEIL_MOD_EXPR)
1358 low = int1l % int2l, hi = 0;
1362 /* ... fall through ... */
1364 case ROUND_MOD_EXPR:
1365 overflow = div_and_round_double (code, uns,
1366 int1l, int1h, int2l, int2h,
1367 &garbagel, &garbageh, &low, &hi);
1373 low = (((unsigned HOST_WIDE_INT) int1h
1374 < (unsigned HOST_WIDE_INT) int2h)
1375 || (((unsigned HOST_WIDE_INT) int1h
1376 == (unsigned HOST_WIDE_INT) int2h)
1379 low = (int1h < int2h
1380 || (int1h == int2h && int1l < int2l));
1382 if (low == (code == MIN_EXPR))
1383 low = int1l, hi = int1h;
1385 low = int2l, hi = int2h;
1392 /* If this is for a sizetype, can be represented as one (signed)
1393 HOST_WIDE_INT word, and doesn't overflow, use size_int since it caches
1396 && ((hi == 0 && (HOST_WIDE_INT) low >= 0)
1397 || (hi == -1 && (HOST_WIDE_INT) low < 0))
1398 && overflow == 0 && ! TREE_OVERFLOW (arg1) && ! TREE_OVERFLOW (arg2))
1399 return size_int_type_wide (low, type);
1402 t = build_int_2 (low, hi);
1403 TREE_TYPE (t) = TREE_TYPE (arg1);
1408 ? (!uns || is_sizetype) && overflow
1409 : (force_fit_type (t, (!uns || is_sizetype) && overflow)
1411 | TREE_OVERFLOW (arg1)
1412 | TREE_OVERFLOW (arg2));
1414 /* If we're doing a size calculation, unsigned arithmetic does overflow.
1415 So check if force_fit_type truncated the value. */
1417 && ! TREE_OVERFLOW (t)
1418 && (TREE_INT_CST_HIGH (t) != hi
1419 || TREE_INT_CST_LOW (t) != low))
1420 TREE_OVERFLOW (t) = 1;
1422 TREE_CONSTANT_OVERFLOW (t) = (TREE_OVERFLOW (t)
1423 | TREE_CONSTANT_OVERFLOW (arg1)
1424 | TREE_CONSTANT_OVERFLOW (arg2));
1428 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1429 constant. We assume ARG1 and ARG2 have the same data type, or at least
1430 are the same kind of constant and the same machine mode.
1432 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
1435 const_binop (enum tree_code code, tree arg1, tree arg2, int notrunc)
1440 if (TREE_CODE (arg1) == INTEGER_CST)
1441 return int_const_binop (code, arg1, arg2, notrunc);
1443 if (TREE_CODE (arg1) == REAL_CST)
1445 enum machine_mode mode;
1448 REAL_VALUE_TYPE value;
1451 d1 = TREE_REAL_CST (arg1);
1452 d2 = TREE_REAL_CST (arg2);
1454 type = TREE_TYPE (arg1);
1455 mode = TYPE_MODE (type);
1457 /* Don't perform operation if we honor signaling NaNs and
1458 either operand is a NaN. */
1459 if (HONOR_SNANS (mode)
1460 && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1463 /* Don't perform operation if it would raise a division
1464 by zero exception. */
1465 if (code == RDIV_EXPR
1466 && REAL_VALUES_EQUAL (d2, dconst0)
1467 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1470 /* If either operand is a NaN, just return it. Otherwise, set up
1471 for floating-point trap; we return an overflow. */
1472 if (REAL_VALUE_ISNAN (d1))
1474 else if (REAL_VALUE_ISNAN (d2))
1477 REAL_ARITHMETIC (value, code, d1, d2);
1479 t = build_real (type, real_value_truncate (mode, value));
1482 = (force_fit_type (t, 0)
1483 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1484 TREE_CONSTANT_OVERFLOW (t)
1486 | TREE_CONSTANT_OVERFLOW (arg1)
1487 | TREE_CONSTANT_OVERFLOW (arg2);
1490 if (TREE_CODE (arg1) == COMPLEX_CST)
1492 tree type = TREE_TYPE (arg1);
1493 tree r1 = TREE_REALPART (arg1);
1494 tree i1 = TREE_IMAGPART (arg1);
1495 tree r2 = TREE_REALPART (arg2);
1496 tree i2 = TREE_IMAGPART (arg2);
1502 t = build_complex (type,
1503 const_binop (PLUS_EXPR, r1, r2, notrunc),
1504 const_binop (PLUS_EXPR, i1, i2, notrunc));
1508 t = build_complex (type,
1509 const_binop (MINUS_EXPR, r1, r2, notrunc),
1510 const_binop (MINUS_EXPR, i1, i2, notrunc));
1514 t = build_complex (type,
1515 const_binop (MINUS_EXPR,
1516 const_binop (MULT_EXPR,
1518 const_binop (MULT_EXPR,
1521 const_binop (PLUS_EXPR,
1522 const_binop (MULT_EXPR,
1524 const_binop (MULT_EXPR,
1532 = const_binop (PLUS_EXPR,
1533 const_binop (MULT_EXPR, r2, r2, notrunc),
1534 const_binop (MULT_EXPR, i2, i2, notrunc),
1537 t = build_complex (type,
1539 (INTEGRAL_TYPE_P (TREE_TYPE (r1))
1540 ? TRUNC_DIV_EXPR : RDIV_EXPR,
1541 const_binop (PLUS_EXPR,
1542 const_binop (MULT_EXPR, r1, r2,
1544 const_binop (MULT_EXPR, i1, i2,
1547 magsquared, notrunc),
1549 (INTEGRAL_TYPE_P (TREE_TYPE (r1))
1550 ? TRUNC_DIV_EXPR : RDIV_EXPR,
1551 const_binop (MINUS_EXPR,
1552 const_binop (MULT_EXPR, i1, r2,
1554 const_binop (MULT_EXPR, r1, i2,
1557 magsquared, notrunc));
1569 /* These are the hash table functions for the hash table of INTEGER_CST
1570 nodes of a sizetype. */
1572 /* Return the hash code code X, an INTEGER_CST. */
1575 size_htab_hash (const void *x)
1579 return (TREE_INT_CST_HIGH (t) ^ TREE_INT_CST_LOW (t)
1580 ^ htab_hash_pointer (TREE_TYPE (t))
1581 ^ (TREE_OVERFLOW (t) << 20));
1584 /* Return nonzero if the value represented by *X (an INTEGER_CST tree node)
1585 is the same as that given by *Y, which is the same. */
1588 size_htab_eq (const void *x, const void *y)
1593 return (TREE_INT_CST_HIGH (xt) == TREE_INT_CST_HIGH (yt)
1594 && TREE_INT_CST_LOW (xt) == TREE_INT_CST_LOW (yt)
1595 && TREE_TYPE (xt) == TREE_TYPE (yt)
1596 && TREE_OVERFLOW (xt) == TREE_OVERFLOW (yt));
1599 /* Return an INTEGER_CST with value whose low-order HOST_BITS_PER_WIDE_INT
1600 bits are given by NUMBER and of the sizetype represented by KIND. */
1603 size_int_wide (HOST_WIDE_INT number, enum size_type_kind kind)
1605 return size_int_type_wide (number, sizetype_tab[(int) kind]);
1608 /* Likewise, but the desired type is specified explicitly. */
1610 static GTY (()) tree new_const;
1611 static GTY ((if_marked ("ggc_marked_p"), param_is (union tree_node)))
1615 size_int_type_wide (HOST_WIDE_INT number, tree type)
1621 size_htab = htab_create_ggc (1024, size_htab_hash, size_htab_eq, NULL);
1622 new_const = make_node (INTEGER_CST);
1625 /* Adjust NEW_CONST to be the constant we want. If it's already in the
1626 hash table, we return the value from the hash table. Otherwise, we
1627 place that in the hash table and make a new node for the next time. */
1628 TREE_INT_CST_LOW (new_const) = number;
1629 TREE_INT_CST_HIGH (new_const) = number < 0 ? -1 : 0;
1630 TREE_TYPE (new_const) = type;
1631 TREE_OVERFLOW (new_const) = TREE_CONSTANT_OVERFLOW (new_const)
1632 = force_fit_type (new_const, 0);
1634 slot = htab_find_slot (size_htab, new_const, INSERT);
1640 new_const = make_node (INTEGER_CST);
1644 return (tree) *slot;
1647 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1648 is a tree code. The type of the result is taken from the operands.
1649 Both must be the same type integer type and it must be a size type.
1650 If the operands are constant, so is the result. */
1653 size_binop (enum tree_code code, tree arg0, tree arg1)
1655 tree type = TREE_TYPE (arg0);
1657 if (TREE_CODE (type) != INTEGER_TYPE || ! TYPE_IS_SIZETYPE (type)
1658 || type != TREE_TYPE (arg1))
1661 /* Handle the special case of two integer constants faster. */
1662 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1664 /* And some specific cases even faster than that. */
1665 if (code == PLUS_EXPR && integer_zerop (arg0))
1667 else if ((code == MINUS_EXPR || code == PLUS_EXPR)
1668 && integer_zerop (arg1))
1670 else if (code == MULT_EXPR && integer_onep (arg0))
1673 /* Handle general case of two integer constants. */
1674 return int_const_binop (code, arg0, arg1, 0);
1677 if (arg0 == error_mark_node || arg1 == error_mark_node)
1678 return error_mark_node;
1680 return fold (build2 (code, type, arg0, arg1));
1683 /* Given two values, either both of sizetype or both of bitsizetype,
1684 compute the difference between the two values. Return the value
1685 in signed type corresponding to the type of the operands. */
1688 size_diffop (tree arg0, tree arg1)
1690 tree type = TREE_TYPE (arg0);
1693 if (TREE_CODE (type) != INTEGER_TYPE || ! TYPE_IS_SIZETYPE (type)
1694 || type != TREE_TYPE (arg1))
1697 /* If the type is already signed, just do the simple thing. */
1698 if (!TYPE_UNSIGNED (type))
1699 return size_binop (MINUS_EXPR, arg0, arg1);
1701 ctype = (type == bitsizetype || type == ubitsizetype
1702 ? sbitsizetype : ssizetype);
1704 /* If either operand is not a constant, do the conversions to the signed
1705 type and subtract. The hardware will do the right thing with any
1706 overflow in the subtraction. */
1707 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1708 return size_binop (MINUS_EXPR, fold_convert (ctype, arg0),
1709 fold_convert (ctype, arg1));
1711 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1712 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1713 overflow) and negate (which can't either). Special-case a result
1714 of zero while we're here. */
1715 if (tree_int_cst_equal (arg0, arg1))
1716 return fold_convert (ctype, integer_zero_node);
1717 else if (tree_int_cst_lt (arg1, arg0))
1718 return fold_convert (ctype, size_binop (MINUS_EXPR, arg0, arg1));
1720 return size_binop (MINUS_EXPR, fold_convert (ctype, integer_zero_node),
1721 fold_convert (ctype, size_binop (MINUS_EXPR,
1726 /* Attempt to fold type conversion operation CODE of expression ARG1 to
1727 type TYPE. If no simplification can be done return NULL_TREE. */
1730 fold_convert_const (enum tree_code code, tree type, tree arg1)
1735 if (TREE_TYPE (arg1) == type)
1738 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
1740 if (TREE_CODE (arg1) == INTEGER_CST)
1742 /* If we would build a constant wider than GCC supports,
1743 leave the conversion unfolded. */
1744 if (TYPE_PRECISION (type) > 2 * HOST_BITS_PER_WIDE_INT)
1747 /* If we are trying to make a sizetype for a small integer, use
1748 size_int to pick up cached types to reduce duplicate nodes. */
1749 if (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type)
1750 && !TREE_CONSTANT_OVERFLOW (arg1)
1751 && compare_tree_int (arg1, 10000) < 0)
1752 return size_int_type_wide (TREE_INT_CST_LOW (arg1), type);
1754 /* Given an integer constant, make new constant with new type,
1755 appropriately sign-extended or truncated. */
1756 t = build_int_2 (TREE_INT_CST_LOW (arg1),
1757 TREE_INT_CST_HIGH (arg1));
1758 TREE_TYPE (t) = type;
1759 /* Indicate an overflow if (1) ARG1 already overflowed,
1760 or (2) force_fit_type indicates an overflow.
1761 Tell force_fit_type that an overflow has already occurred
1762 if ARG1 is a too-large unsigned value and T is signed.
1763 But don't indicate an overflow if converting a pointer. */
1765 = ((force_fit_type (t,
1766 (TREE_INT_CST_HIGH (arg1) < 0
1767 && (TYPE_UNSIGNED (type)
1768 < TYPE_UNSIGNED (TREE_TYPE (arg1)))))
1769 && ! POINTER_TYPE_P (TREE_TYPE (arg1)))
1770 || TREE_OVERFLOW (arg1));
1771 TREE_CONSTANT_OVERFLOW (t)
1772 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
1775 else if (TREE_CODE (arg1) == REAL_CST)
1777 /* The following code implements the floating point to integer
1778 conversion rules required by the Java Language Specification,
1779 that IEEE NaNs are mapped to zero and values that overflow
1780 the target precision saturate, i.e. values greater than
1781 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1782 are mapped to INT_MIN. These semantics are allowed by the
1783 C and C++ standards that simply state that the behavior of
1784 FP-to-integer conversion is unspecified upon overflow. */
1786 HOST_WIDE_INT high, low;
1789 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
1793 case FIX_TRUNC_EXPR:
1794 real_trunc (&r, VOIDmode, &x);
1798 real_ceil (&r, VOIDmode, &x);
1801 case FIX_FLOOR_EXPR:
1802 real_floor (&r, VOIDmode, &x);
1805 case FIX_ROUND_EXPR:
1806 real_round (&r, VOIDmode, &x);
1813 /* If R is NaN, return zero and show we have an overflow. */
1814 if (REAL_VALUE_ISNAN (r))
1821 /* See if R is less than the lower bound or greater than the
1826 tree lt = TYPE_MIN_VALUE (type);
1827 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
1828 if (REAL_VALUES_LESS (r, l))
1831 high = TREE_INT_CST_HIGH (lt);
1832 low = TREE_INT_CST_LOW (lt);
1838 tree ut = TYPE_MAX_VALUE (type);
1841 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
1842 if (REAL_VALUES_LESS (u, r))
1845 high = TREE_INT_CST_HIGH (ut);
1846 low = TREE_INT_CST_LOW (ut);
1852 REAL_VALUE_TO_INT (&low, &high, r);
1854 t = build_int_2 (low, high);
1855 TREE_TYPE (t) = type;
1857 = TREE_OVERFLOW (arg1) | force_fit_type (t, overflow);
1858 TREE_CONSTANT_OVERFLOW (t)
1859 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
1863 else if (TREE_CODE (type) == REAL_TYPE)
1865 if (TREE_CODE (arg1) == INTEGER_CST)
1866 return build_real_from_int_cst (type, arg1);
1867 if (TREE_CODE (arg1) == REAL_CST)
1869 if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1)))
1871 /* We make a copy of ARG1 so that we don't modify an
1872 existing constant tree. */
1873 t = copy_node (arg1);
1874 TREE_TYPE (t) = type;
1878 t = build_real (type,
1879 real_value_truncate (TYPE_MODE (type),
1880 TREE_REAL_CST (arg1)));
1883 = TREE_OVERFLOW (arg1) | force_fit_type (t, 0);
1884 TREE_CONSTANT_OVERFLOW (t)
1885 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
1892 /* Convert expression ARG to type TYPE. Used by the middle-end for
1893 simple conversions in preference to calling the front-end's convert. */
1896 fold_convert (tree type, tree arg)
1898 tree orig = TREE_TYPE (arg);
1904 if (TREE_CODE (arg) == ERROR_MARK
1905 || TREE_CODE (type) == ERROR_MARK
1906 || TREE_CODE (orig) == ERROR_MARK)
1907 return error_mark_node;
1909 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
1910 return fold (build1 (NOP_EXPR, type, arg));
1912 if (INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type)
1913 || TREE_CODE (type) == OFFSET_TYPE)
1915 if (TREE_CODE (arg) == INTEGER_CST)
1917 tem = fold_convert_const (NOP_EXPR, type, arg);
1918 if (tem != NULL_TREE)
1921 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
1922 || TREE_CODE (orig) == OFFSET_TYPE)
1923 return fold (build1 (NOP_EXPR, type, arg));
1924 if (TREE_CODE (orig) == COMPLEX_TYPE)
1926 tem = fold (build1 (REALPART_EXPR, TREE_TYPE (orig), arg));
1927 return fold_convert (type, tem);
1929 if (TREE_CODE (orig) == VECTOR_TYPE
1930 && GET_MODE_SIZE (TYPE_MODE (type))
1931 == GET_MODE_SIZE (TYPE_MODE (orig)))
1932 return fold (build1 (NOP_EXPR, type, arg));
1934 else if (TREE_CODE (type) == REAL_TYPE)
1936 if (TREE_CODE (arg) == INTEGER_CST)
1938 tem = fold_convert_const (FLOAT_EXPR, type, arg);
1939 if (tem != NULL_TREE)
1942 else if (TREE_CODE (arg) == REAL_CST)
1944 tem = fold_convert_const (NOP_EXPR, type, arg);
1945 if (tem != NULL_TREE)
1949 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig))
1950 return fold (build1 (FLOAT_EXPR, type, arg));
1951 if (TREE_CODE (orig) == REAL_TYPE)
1952 return fold (build1 (flag_float_store ? CONVERT_EXPR : NOP_EXPR,
1954 if (TREE_CODE (orig) == COMPLEX_TYPE)
1956 tem = fold (build1 (REALPART_EXPR, TREE_TYPE (orig), arg));
1957 return fold_convert (type, tem);
1960 else if (TREE_CODE (type) == COMPLEX_TYPE)
1962 if (INTEGRAL_TYPE_P (orig)
1963 || POINTER_TYPE_P (orig)
1964 || TREE_CODE (orig) == REAL_TYPE)
1965 return build2 (COMPLEX_EXPR, type,
1966 fold_convert (TREE_TYPE (type), arg),
1967 fold_convert (TREE_TYPE (type), integer_zero_node));
1968 if (TREE_CODE (orig) == COMPLEX_TYPE)
1972 if (TREE_CODE (arg) == COMPLEX_EXPR)
1974 rpart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 0));
1975 ipart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 1));
1976 return fold (build2 (COMPLEX_EXPR, type, rpart, ipart));
1979 arg = save_expr (arg);
1980 rpart = fold (build1 (REALPART_EXPR, TREE_TYPE (orig), arg));
1981 ipart = fold (build1 (IMAGPART_EXPR, TREE_TYPE (orig), arg));
1982 rpart = fold_convert (TREE_TYPE (type), rpart);
1983 ipart = fold_convert (TREE_TYPE (type), ipart);
1984 return fold (build2 (COMPLEX_EXPR, type, rpart, ipart));
1987 else if (TREE_CODE (type) == VECTOR_TYPE)
1989 if ((INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig))
1990 && GET_MODE_SIZE (TYPE_MODE (type))
1991 == GET_MODE_SIZE (TYPE_MODE (orig)))
1992 return fold (build1 (NOP_EXPR, type, arg));
1993 if (TREE_CODE (orig) == VECTOR_TYPE
1994 && GET_MODE_SIZE (TYPE_MODE (type))
1995 == GET_MODE_SIZE (TYPE_MODE (orig)))
1996 return fold (build1 (NOP_EXPR, type, arg));
1998 else if (VOID_TYPE_P (type))
1999 return fold (build1 (CONVERT_EXPR, type, arg));
2003 /* Return an expr equal to X but certainly not valid as an lvalue. */
2008 /* We only need to wrap lvalue tree codes. */
2009 switch (TREE_CODE (x))
2023 case ARRAY_RANGE_REF:
2028 case PREINCREMENT_EXPR:
2029 case PREDECREMENT_EXPR:
2032 case TRY_CATCH_EXPR:
2033 case WITH_CLEANUP_EXPR:
2045 /* Assume the worst for front-end tree codes. */
2046 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2050 return build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
2053 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2054 Zero means allow extended lvalues. */
2056 int pedantic_lvalues;
2058 /* When pedantic, return an expr equal to X but certainly not valid as a
2059 pedantic lvalue. Otherwise, return X. */
2062 pedantic_non_lvalue (tree x)
2064 if (pedantic_lvalues)
2065 return non_lvalue (x);
2070 /* Given a tree comparison code, return the code that is the logical inverse
2071 of the given code. It is not safe to do this for floating-point
2072 comparisons, except for NE_EXPR and EQ_EXPR, so we receive a machine mode
2073 as well: if reversing the comparison is unsafe, return ERROR_MARK. */
2075 static enum tree_code
2076 invert_tree_comparison (enum tree_code code, bool honor_nans)
2078 if (honor_nans && flag_trapping_math)
2088 return honor_nans ? UNLE_EXPR : LE_EXPR;
2090 return honor_nans ? UNLT_EXPR : LT_EXPR;
2092 return honor_nans ? UNGE_EXPR : GE_EXPR;
2094 return honor_nans ? UNGT_EXPR : GT_EXPR;
2108 return UNORDERED_EXPR;
2109 case UNORDERED_EXPR:
2110 return ORDERED_EXPR;
2116 /* Similar, but return the comparison that results if the operands are
2117 swapped. This is safe for floating-point. */
2120 swap_tree_comparison (enum tree_code code)
2141 /* Convert a comparison tree code from an enum tree_code representation
2142 into a compcode bit-based encoding. This function is the inverse of
2143 compcode_to_comparison. */
2145 static enum comparison_code
2146 comparison_to_compcode (enum tree_code code)
2163 return COMPCODE_ORD;
2164 case UNORDERED_EXPR:
2165 return COMPCODE_UNORD;
2167 return COMPCODE_UNLT;
2169 return COMPCODE_UNEQ;
2171 return COMPCODE_UNLE;
2173 return COMPCODE_UNGT;
2175 return COMPCODE_LTGT;
2177 return COMPCODE_UNGE;
2183 /* Convert a compcode bit-based encoding of a comparison operator back
2184 to GCC's enum tree_code representation. This function is the
2185 inverse of comparison_to_compcode. */
2187 static enum tree_code
2188 compcode_to_comparison (enum comparison_code code)
2205 return ORDERED_EXPR;
2206 case COMPCODE_UNORD:
2207 return UNORDERED_EXPR;
2225 /* Return a tree for the comparison which is the combination of
2226 doing the AND or OR (depending on CODE) of the two operations LCODE
2227 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2228 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2229 if this makes the transformation invalid. */
2232 combine_comparisons (enum tree_code code, enum tree_code lcode,
2233 enum tree_code rcode, tree truth_type,
2234 tree ll_arg, tree lr_arg)
2236 bool honor_nans = HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg)));
2237 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2238 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2239 enum comparison_code compcode;
2243 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2244 compcode = lcompcode & rcompcode;
2247 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2248 compcode = lcompcode | rcompcode;
2257 /* Eliminate unordered comparisons, as well as LTGT and ORD
2258 which are not used unless the mode has NaNs. */
2259 compcode &= ~COMPCODE_UNORD;
2260 if (compcode == COMPCODE_LTGT)
2261 compcode = COMPCODE_NE;
2262 else if (compcode == COMPCODE_ORD)
2263 compcode = COMPCODE_TRUE;
2265 else if (flag_trapping_math)
2267 /* Check that the original operation and the optimized ones will trap
2268 under the same condition. */
2269 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2270 && (lcompcode != COMPCODE_EQ)
2271 && (lcompcode != COMPCODE_ORD);
2272 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2273 && (rcompcode != COMPCODE_EQ)
2274 && (rcompcode != COMPCODE_ORD);
2275 bool trap = (compcode & COMPCODE_UNORD) == 0
2276 && (compcode != COMPCODE_EQ)
2277 && (compcode != COMPCODE_ORD);
2279 /* In a short-circuited boolean expression the LHS might be
2280 such that the RHS, if evaluated, will never trap. For
2281 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2282 if neither x nor y is NaN. (This is a mixed blessing: for
2283 example, the expression above will never trap, hence
2284 optimizing it to x < y would be invalid). */
2285 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2286 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2289 /* If the comparison was short-circuited, and only the RHS
2290 trapped, we may now generate a spurious trap. */
2292 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2295 /* If we changed the conditions that cause a trap, we lose. */
2296 if ((ltrap || rtrap) != trap)
2300 if (compcode == COMPCODE_TRUE)
2301 return constant_boolean_node (true, truth_type);
2302 else if (compcode == COMPCODE_FALSE)
2303 return constant_boolean_node (false, truth_type);
2305 return fold (build2 (compcode_to_comparison (compcode),
2306 truth_type, ll_arg, lr_arg));
2309 /* Return nonzero if CODE is a tree code that represents a truth value. */
2312 truth_value_p (enum tree_code code)
2314 return (TREE_CODE_CLASS (code) == '<'
2315 || code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
2316 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
2317 || code == TRUTH_XOR_EXPR || code == TRUTH_NOT_EXPR);
2320 /* Return nonzero if two operands (typically of the same tree node)
2321 are necessarily equal. If either argument has side-effects this
2322 function returns zero. FLAGS modifies behavior as follows:
2324 If OEP_ONLY_CONST is set, only return nonzero for constants.
2325 This function tests whether the operands are indistinguishable;
2326 it does not test whether they are equal using C's == operation.
2327 The distinction is important for IEEE floating point, because
2328 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2329 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2331 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2332 even though it may hold multiple values during a function.
2333 This is because a GCC tree node guarantees that nothing else is
2334 executed between the evaluation of its "operands" (which may often
2335 be evaluated in arbitrary order). Hence if the operands themselves
2336 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2337 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2338 unset means assuming isochronic (or instantaneous) tree equivalence.
2339 Unless comparing arbitrary expression trees, such as from different
2340 statements, this flag can usually be left unset.
2342 If OEP_PURE_SAME is set, then pure functions with identical arguments
2343 are considered the same. It is used when the caller has other ways
2344 to ensure that global memory is unchanged in between. */
2347 operand_equal_p (tree arg0, tree arg1, unsigned int flags)
2349 /* If either is ERROR_MARK, they aren't equal. */
2350 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK)
2353 /* If both types don't have the same signedness, then we can't consider
2354 them equal. We must check this before the STRIP_NOPS calls
2355 because they may change the signedness of the arguments. */
2356 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1)))
2362 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2363 /* This is needed for conversions and for COMPONENT_REF.
2364 Might as well play it safe and always test this. */
2365 || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2366 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2367 || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
2370 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2371 We don't care about side effects in that case because the SAVE_EXPR
2372 takes care of that for us. In all other cases, two expressions are
2373 equal if they have no side effects. If we have two identical
2374 expressions with side effects that should be treated the same due
2375 to the only side effects being identical SAVE_EXPR's, that will
2376 be detected in the recursive calls below. */
2377 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
2378 && (TREE_CODE (arg0) == SAVE_EXPR
2379 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2382 /* Next handle constant cases, those for which we can return 1 even
2383 if ONLY_CONST is set. */
2384 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2385 switch (TREE_CODE (arg0))
2388 return (! TREE_CONSTANT_OVERFLOW (arg0)
2389 && ! TREE_CONSTANT_OVERFLOW (arg1)
2390 && tree_int_cst_equal (arg0, arg1));
2393 return (! TREE_CONSTANT_OVERFLOW (arg0)
2394 && ! TREE_CONSTANT_OVERFLOW (arg1)
2395 && REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
2396 TREE_REAL_CST (arg1)));
2402 if (TREE_CONSTANT_OVERFLOW (arg0)
2403 || TREE_CONSTANT_OVERFLOW (arg1))
2406 v1 = TREE_VECTOR_CST_ELTS (arg0);
2407 v2 = TREE_VECTOR_CST_ELTS (arg1);
2410 if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2),
2413 v1 = TREE_CHAIN (v1);
2414 v2 = TREE_CHAIN (v2);
2421 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2423 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2427 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2428 && ! memcmp (TREE_STRING_POINTER (arg0),
2429 TREE_STRING_POINTER (arg1),
2430 TREE_STRING_LENGTH (arg0)));
2433 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2439 if (flags & OEP_ONLY_CONST)
2442 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2445 /* Two conversions are equal only if signedness and modes match. */
2446 if ((TREE_CODE (arg0) == NOP_EXPR || TREE_CODE (arg0) == CONVERT_EXPR)
2447 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
2448 != TYPE_UNSIGNED (TREE_TYPE (arg1))))
2451 return operand_equal_p (TREE_OPERAND (arg0, 0),
2452 TREE_OPERAND (arg1, 0), flags);
2456 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0)
2457 && operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1),
2461 /* For commutative ops, allow the other order. */
2462 return (commutative_tree_code (TREE_CODE (arg0))
2463 && operand_equal_p (TREE_OPERAND (arg0, 0),
2464 TREE_OPERAND (arg1, 1), flags)
2465 && operand_equal_p (TREE_OPERAND (arg0, 1),
2466 TREE_OPERAND (arg1, 0), flags));
2469 /* If either of the pointer (or reference) expressions we are
2470 dereferencing contain a side effect, these cannot be equal. */
2471 if (TREE_SIDE_EFFECTS (arg0)
2472 || TREE_SIDE_EFFECTS (arg1))
2475 switch (TREE_CODE (arg0))
2478 return operand_equal_p (TREE_OPERAND (arg0, 0),
2479 TREE_OPERAND (arg1, 0), flags);
2483 case ARRAY_RANGE_REF:
2484 return (operand_equal_p (TREE_OPERAND (arg0, 0),
2485 TREE_OPERAND (arg1, 0), flags)
2486 && operand_equal_p (TREE_OPERAND (arg0, 1),
2487 TREE_OPERAND (arg1, 1), flags));
2490 return (operand_equal_p (TREE_OPERAND (arg0, 0),
2491 TREE_OPERAND (arg1, 0), flags)
2492 && operand_equal_p (TREE_OPERAND (arg0, 1),
2493 TREE_OPERAND (arg1, 1), flags)
2494 && operand_equal_p (TREE_OPERAND (arg0, 2),
2495 TREE_OPERAND (arg1, 2), flags));
2501 switch (TREE_CODE (arg0))
2504 case TRUTH_NOT_EXPR:
2505 return operand_equal_p (TREE_OPERAND (arg0, 0),
2506 TREE_OPERAND (arg1, 0), flags);
2509 return rtx_equal_p (RTL_EXPR_RTL (arg0), RTL_EXPR_RTL (arg1));
2512 /* If the CALL_EXPRs call different functions, then they
2513 clearly can not be equal. */
2514 if (! operand_equal_p (TREE_OPERAND (arg0, 0),
2515 TREE_OPERAND (arg1, 0), flags))
2519 unsigned int cef = call_expr_flags (arg0);
2520 if (flags & OEP_PURE_SAME)
2521 cef &= ECF_CONST | ECF_PURE;
2528 /* Now see if all the arguments are the same. operand_equal_p
2529 does not handle TREE_LIST, so we walk the operands here
2530 feeding them to operand_equal_p. */
2531 arg0 = TREE_OPERAND (arg0, 1);
2532 arg1 = TREE_OPERAND (arg1, 1);
2533 while (arg0 && arg1)
2535 if (! operand_equal_p (TREE_VALUE (arg0), TREE_VALUE (arg1),
2539 arg0 = TREE_CHAIN (arg0);
2540 arg1 = TREE_CHAIN (arg1);
2543 /* If we get here and both argument lists are exhausted
2544 then the CALL_EXPRs are equal. */
2545 return ! (arg0 || arg1);
2552 /* Consider __builtin_sqrt equal to sqrt. */
2553 return (TREE_CODE (arg0) == FUNCTION_DECL
2554 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
2555 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
2556 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
2563 /* Similar to operand_equal_p, but see if ARG0 might have been made by
2564 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
2566 When in doubt, return 0. */
2569 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
2571 int unsignedp1, unsignedpo;
2572 tree primarg0, primarg1, primother;
2573 unsigned int correct_width;
2575 if (operand_equal_p (arg0, arg1, 0))
2578 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
2579 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
2582 /* Discard any conversions that don't change the modes of ARG0 and ARG1
2583 and see if the inner values are the same. This removes any
2584 signedness comparison, which doesn't matter here. */
2585 primarg0 = arg0, primarg1 = arg1;
2586 STRIP_NOPS (primarg0);
2587 STRIP_NOPS (primarg1);
2588 if (operand_equal_p (primarg0, primarg1, 0))
2591 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
2592 actual comparison operand, ARG0.
2594 First throw away any conversions to wider types
2595 already present in the operands. */
2597 primarg1 = get_narrower (arg1, &unsignedp1);
2598 primother = get_narrower (other, &unsignedpo);
2600 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
2601 if (unsignedp1 == unsignedpo
2602 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
2603 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
2605 tree type = TREE_TYPE (arg0);
2607 /* Make sure shorter operand is extended the right way
2608 to match the longer operand. */
2609 primarg1 = fold_convert (lang_hooks.types.signed_or_unsigned_type
2610 (unsignedp1, TREE_TYPE (primarg1)), primarg1);
2612 if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
2619 /* See if ARG is an expression that is either a comparison or is performing
2620 arithmetic on comparisons. The comparisons must only be comparing
2621 two different values, which will be stored in *CVAL1 and *CVAL2; if
2622 they are nonzero it means that some operands have already been found.
2623 No variables may be used anywhere else in the expression except in the
2624 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
2625 the expression and save_expr needs to be called with CVAL1 and CVAL2.
2627 If this is true, return 1. Otherwise, return zero. */
2630 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
2632 enum tree_code code = TREE_CODE (arg);
2633 char class = TREE_CODE_CLASS (code);
2635 /* We can handle some of the 'e' cases here. */
2636 if (class == 'e' && code == TRUTH_NOT_EXPR)
2638 else if (class == 'e'
2639 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
2640 || code == COMPOUND_EXPR))
2643 else if (class == 'e' && code == SAVE_EXPR && SAVE_EXPR_RTL (arg) == 0
2644 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
2646 /* If we've already found a CVAL1 or CVAL2, this expression is
2647 two complex to handle. */
2648 if (*cval1 || *cval2)
2658 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
2661 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
2662 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2663 cval1, cval2, save_p));
2669 if (code == COND_EXPR)
2670 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
2671 cval1, cval2, save_p)
2672 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2673 cval1, cval2, save_p)
2674 && twoval_comparison_p (TREE_OPERAND (arg, 2),
2675 cval1, cval2, save_p));
2679 /* First see if we can handle the first operand, then the second. For
2680 the second operand, we know *CVAL1 can't be zero. It must be that
2681 one side of the comparison is each of the values; test for the
2682 case where this isn't true by failing if the two operands
2685 if (operand_equal_p (TREE_OPERAND (arg, 0),
2686 TREE_OPERAND (arg, 1), 0))
2690 *cval1 = TREE_OPERAND (arg, 0);
2691 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
2693 else if (*cval2 == 0)
2694 *cval2 = TREE_OPERAND (arg, 0);
2695 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
2700 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
2702 else if (*cval2 == 0)
2703 *cval2 = TREE_OPERAND (arg, 1);
2704 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
2716 /* ARG is a tree that is known to contain just arithmetic operations and
2717 comparisons. Evaluate the operations in the tree substituting NEW0 for
2718 any occurrence of OLD0 as an operand of a comparison and likewise for
2722 eval_subst (tree arg, tree old0, tree new0, tree old1, tree new1)
2724 tree type = TREE_TYPE (arg);
2725 enum tree_code code = TREE_CODE (arg);
2726 char class = TREE_CODE_CLASS (code);
2728 /* We can handle some of the 'e' cases here. */
2729 if (class == 'e' && code == TRUTH_NOT_EXPR)
2731 else if (class == 'e'
2732 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2738 return fold (build1 (code, type,
2739 eval_subst (TREE_OPERAND (arg, 0),
2740 old0, new0, old1, new1)));
2743 return fold (build2 (code, type,
2744 eval_subst (TREE_OPERAND (arg, 0),
2745 old0, new0, old1, new1),
2746 eval_subst (TREE_OPERAND (arg, 1),
2747 old0, new0, old1, new1)));
2753 return eval_subst (TREE_OPERAND (arg, 0), old0, new0, old1, new1);
2756 return eval_subst (TREE_OPERAND (arg, 1), old0, new0, old1, new1);
2759 return fold (build3 (code, type,
2760 eval_subst (TREE_OPERAND (arg, 0),
2761 old0, new0, old1, new1),
2762 eval_subst (TREE_OPERAND (arg, 1),
2763 old0, new0, old1, new1),
2764 eval_subst (TREE_OPERAND (arg, 2),
2765 old0, new0, old1, new1)));
2769 /* Fall through - ??? */
2773 tree arg0 = TREE_OPERAND (arg, 0);
2774 tree arg1 = TREE_OPERAND (arg, 1);
2776 /* We need to check both for exact equality and tree equality. The
2777 former will be true if the operand has a side-effect. In that
2778 case, we know the operand occurred exactly once. */
2780 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
2782 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
2785 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
2787 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
2790 return fold (build2 (code, type, arg0, arg1));
2798 /* Return a tree for the case when the result of an expression is RESULT
2799 converted to TYPE and OMITTED was previously an operand of the expression
2800 but is now not needed (e.g., we folded OMITTED * 0).
2802 If OMITTED has side effects, we must evaluate it. Otherwise, just do
2803 the conversion of RESULT to TYPE. */
2806 omit_one_operand (tree type, tree result, tree omitted)
2808 tree t = fold_convert (type, result);
2810 if (TREE_SIDE_EFFECTS (omitted))
2811 return build2 (COMPOUND_EXPR, type, omitted, t);
2813 return non_lvalue (t);
2816 /* Similar, but call pedantic_non_lvalue instead of non_lvalue. */
2819 pedantic_omit_one_operand (tree type, tree result, tree omitted)
2821 tree t = fold_convert (type, result);
2823 if (TREE_SIDE_EFFECTS (omitted))
2824 return build2 (COMPOUND_EXPR, type, omitted, t);
2826 return pedantic_non_lvalue (t);
2829 /* Return a tree for the case when the result of an expression is RESULT
2830 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
2831 of the expression but are now not needed.
2833 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
2834 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
2835 evaluated before OMITTED2. Otherwise, if neither has side effects,
2836 just do the conversion of RESULT to TYPE. */
2839 omit_two_operands (tree type, tree result, tree omitted1, tree omitted2)
2841 tree t = fold_convert (type, result);
2843 if (TREE_SIDE_EFFECTS (omitted2))
2844 t = build2 (COMPOUND_EXPR, type, omitted2, t);
2845 if (TREE_SIDE_EFFECTS (omitted1))
2846 t = build2 (COMPOUND_EXPR, type, omitted1, t);
2848 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue (t) : t;
2852 /* Return a simplified tree node for the truth-negation of ARG. This
2853 never alters ARG itself. We assume that ARG is an operation that
2854 returns a truth value (0 or 1).
2856 FIXME: one would think we would fold the result, but it causes
2857 problems with the dominator optimizer. */
2859 invert_truthvalue (tree arg)
2861 tree type = TREE_TYPE (arg);
2862 enum tree_code code = TREE_CODE (arg);
2864 if (code == ERROR_MARK)
2867 /* If this is a comparison, we can simply invert it, except for
2868 floating-point non-equality comparisons, in which case we just
2869 enclose a TRUTH_NOT_EXPR around what we have. */
2871 if (TREE_CODE_CLASS (code) == '<')
2873 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
2874 if (FLOAT_TYPE_P (op_type)
2875 && flag_trapping_math
2876 && code != ORDERED_EXPR && code != UNORDERED_EXPR
2877 && code != NE_EXPR && code != EQ_EXPR)
2878 return build1 (TRUTH_NOT_EXPR, type, arg);
2881 code = invert_tree_comparison (code,
2882 HONOR_NANS (TYPE_MODE (op_type)));
2883 if (code == ERROR_MARK)
2884 return build1 (TRUTH_NOT_EXPR, type, arg);
2886 return build2 (code, type,
2887 TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
2894 return fold_convert (type, build_int_2 (integer_zerop (arg), 0));
2896 case TRUTH_AND_EXPR:
2897 return build2 (TRUTH_OR_EXPR, type,
2898 invert_truthvalue (TREE_OPERAND (arg, 0)),
2899 invert_truthvalue (TREE_OPERAND (arg, 1)));
2902 return build2 (TRUTH_AND_EXPR, type,
2903 invert_truthvalue (TREE_OPERAND (arg, 0)),
2904 invert_truthvalue (TREE_OPERAND (arg, 1)));
2906 case TRUTH_XOR_EXPR:
2907 /* Here we can invert either operand. We invert the first operand
2908 unless the second operand is a TRUTH_NOT_EXPR in which case our
2909 result is the XOR of the first operand with the inside of the
2910 negation of the second operand. */
2912 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
2913 return build2 (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
2914 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
2916 return build2 (TRUTH_XOR_EXPR, type,
2917 invert_truthvalue (TREE_OPERAND (arg, 0)),
2918 TREE_OPERAND (arg, 1));
2920 case TRUTH_ANDIF_EXPR:
2921 return build2 (TRUTH_ORIF_EXPR, type,
2922 invert_truthvalue (TREE_OPERAND (arg, 0)),
2923 invert_truthvalue (TREE_OPERAND (arg, 1)));
2925 case TRUTH_ORIF_EXPR:
2926 return build2 (TRUTH_ANDIF_EXPR, type,
2927 invert_truthvalue (TREE_OPERAND (arg, 0)),
2928 invert_truthvalue (TREE_OPERAND (arg, 1)));
2930 case TRUTH_NOT_EXPR:
2931 return TREE_OPERAND (arg, 0);
2934 return build3 (COND_EXPR, type, TREE_OPERAND (arg, 0),
2935 invert_truthvalue (TREE_OPERAND (arg, 1)),
2936 invert_truthvalue (TREE_OPERAND (arg, 2)));
2939 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0),
2940 invert_truthvalue (TREE_OPERAND (arg, 1)));
2942 case NON_LVALUE_EXPR:
2943 return invert_truthvalue (TREE_OPERAND (arg, 0));
2946 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2951 return build1 (TREE_CODE (arg), type,
2952 invert_truthvalue (TREE_OPERAND (arg, 0)));
2955 if (!integer_onep (TREE_OPERAND (arg, 1)))
2957 return build2 (EQ_EXPR, type, arg,
2958 fold_convert (type, integer_zero_node));
2961 return build1 (TRUTH_NOT_EXPR, type, arg);
2963 case CLEANUP_POINT_EXPR:
2964 return build1 (CLEANUP_POINT_EXPR, type,
2965 invert_truthvalue (TREE_OPERAND (arg, 0)));
2970 if (TREE_CODE (TREE_TYPE (arg)) != BOOLEAN_TYPE)
2972 return build1 (TRUTH_NOT_EXPR, type, arg);
2975 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
2976 operands are another bit-wise operation with a common input. If so,
2977 distribute the bit operations to save an operation and possibly two if
2978 constants are involved. For example, convert
2979 (A | B) & (A | C) into A | (B & C)
2980 Further simplification will occur if B and C are constants.
2982 If this optimization cannot be done, 0 will be returned. */
2985 distribute_bit_expr (enum tree_code code, tree type, tree arg0, tree arg1)
2990 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2991 || TREE_CODE (arg0) == code
2992 || (TREE_CODE (arg0) != BIT_AND_EXPR
2993 && TREE_CODE (arg0) != BIT_IOR_EXPR))
2996 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
2998 common = TREE_OPERAND (arg0, 0);
2999 left = TREE_OPERAND (arg0, 1);
3000 right = TREE_OPERAND (arg1, 1);
3002 else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
3004 common = TREE_OPERAND (arg0, 0);
3005 left = TREE_OPERAND (arg0, 1);
3006 right = TREE_OPERAND (arg1, 0);
3008 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
3010 common = TREE_OPERAND (arg0, 1);
3011 left = TREE_OPERAND (arg0, 0);
3012 right = TREE_OPERAND (arg1, 1);
3014 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
3016 common = TREE_OPERAND (arg0, 1);
3017 left = TREE_OPERAND (arg0, 0);
3018 right = TREE_OPERAND (arg1, 0);
3023 return fold (build2 (TREE_CODE (arg0), type, common,
3024 fold (build2 (code, type, left, right))));
3027 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3028 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero. */
3031 make_bit_field_ref (tree inner, tree type, int bitsize, int bitpos,
3034 tree result = build3 (BIT_FIELD_REF, type, inner,
3035 size_int (bitsize), bitsize_int (bitpos));
3037 BIT_FIELD_REF_UNSIGNED (result) = unsignedp;
3042 /* Optimize a bit-field compare.
3044 There are two cases: First is a compare against a constant and the
3045 second is a comparison of two items where the fields are at the same
3046 bit position relative to the start of a chunk (byte, halfword, word)
3047 large enough to contain it. In these cases we can avoid the shift
3048 implicit in bitfield extractions.
3050 For constants, we emit a compare of the shifted constant with the
3051 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3052 compared. For two fields at the same position, we do the ANDs with the
3053 similar mask and compare the result of the ANDs.
3055 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3056 COMPARE_TYPE is the type of the comparison, and LHS and RHS
3057 are the left and right operands of the comparison, respectively.
3059 If the optimization described above can be done, we return the resulting
3060 tree. Otherwise we return zero. */
3063 optimize_bit_field_compare (enum tree_code code, tree compare_type,
3066 HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3067 tree type = TREE_TYPE (lhs);
3068 tree signed_type, unsigned_type;
3069 int const_p = TREE_CODE (rhs) == INTEGER_CST;
3070 enum machine_mode lmode, rmode, nmode;
3071 int lunsignedp, runsignedp;
3072 int lvolatilep = 0, rvolatilep = 0;
3073 tree linner, rinner = NULL_TREE;
3077 /* Get all the information about the extractions being done. If the bit size
3078 if the same as the size of the underlying object, we aren't doing an
3079 extraction at all and so can do nothing. We also don't want to
3080 do anything if the inner expression is a PLACEHOLDER_EXPR since we
3081 then will no longer be able to replace it. */
3082 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3083 &lunsignedp, &lvolatilep);
3084 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3085 || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
3090 /* If this is not a constant, we can only do something if bit positions,
3091 sizes, and signedness are the same. */
3092 rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3093 &runsignedp, &rvolatilep);
3095 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3096 || lunsignedp != runsignedp || offset != 0
3097 || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
3101 /* See if we can find a mode to refer to this field. We should be able to,
3102 but fail if we can't. */
3103 nmode = get_best_mode (lbitsize, lbitpos,
3104 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3105 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3106 TYPE_ALIGN (TREE_TYPE (rinner))),
3107 word_mode, lvolatilep || rvolatilep);
3108 if (nmode == VOIDmode)
3111 /* Set signed and unsigned types of the precision of this mode for the
3113 signed_type = lang_hooks.types.type_for_mode (nmode, 0);
3114 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3116 /* Compute the bit position and size for the new reference and our offset
3117 within it. If the new reference is the same size as the original, we
3118 won't optimize anything, so return zero. */
3119 nbitsize = GET_MODE_BITSIZE (nmode);
3120 nbitpos = lbitpos & ~ (nbitsize - 1);
3122 if (nbitsize == lbitsize)
3125 if (BYTES_BIG_ENDIAN)
3126 lbitpos = nbitsize - lbitsize - lbitpos;
3128 /* Make the mask to be used against the extracted field. */
3129 mask = build_int_2 (~0, ~0);
3130 TREE_TYPE (mask) = unsigned_type;
3131 force_fit_type (mask, 0);
3132 mask = fold_convert (unsigned_type, mask);
3133 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize), 0);
3134 mask = const_binop (RSHIFT_EXPR, mask,
3135 size_int (nbitsize - lbitsize - lbitpos), 0);
3138 /* If not comparing with constant, just rework the comparison
3140 return build2 (code, compare_type,
3141 build2 (BIT_AND_EXPR, unsigned_type,
3142 make_bit_field_ref (linner, unsigned_type,
3143 nbitsize, nbitpos, 1),
3145 build2 (BIT_AND_EXPR, unsigned_type,
3146 make_bit_field_ref (rinner, unsigned_type,
3147 nbitsize, nbitpos, 1),
3150 /* Otherwise, we are handling the constant case. See if the constant is too
3151 big for the field. Warn and return a tree of for 0 (false) if so. We do
3152 this not only for its own sake, but to avoid having to test for this
3153 error case below. If we didn't, we might generate wrong code.
3155 For unsigned fields, the constant shifted right by the field length should
3156 be all zero. For signed fields, the high-order bits should agree with
3161 if (! integer_zerop (const_binop (RSHIFT_EXPR,
3162 fold_convert (unsigned_type, rhs),
3163 size_int (lbitsize), 0)))
3165 warning ("comparison is always %d due to width of bit-field",
3167 return constant_boolean_node (code == NE_EXPR, compare_type);
3172 tree tem = const_binop (RSHIFT_EXPR, fold_convert (signed_type, rhs),
3173 size_int (lbitsize - 1), 0);
3174 if (! integer_zerop (tem) && ! integer_all_onesp (tem))
3176 warning ("comparison is always %d due to width of bit-field",
3178 return constant_boolean_node (code == NE_EXPR, compare_type);
3182 /* Single-bit compares should always be against zero. */
3183 if (lbitsize == 1 && ! integer_zerop (rhs))
3185 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
3186 rhs = fold_convert (type, integer_zero_node);
3189 /* Make a new bitfield reference, shift the constant over the
3190 appropriate number of bits and mask it with the computed mask
3191 (in case this was a signed field). If we changed it, make a new one. */
3192 lhs = make_bit_field_ref (linner, unsigned_type, nbitsize, nbitpos, 1);
3195 TREE_SIDE_EFFECTS (lhs) = 1;
3196 TREE_THIS_VOLATILE (lhs) = 1;
3199 rhs = fold (const_binop (BIT_AND_EXPR,
3200 const_binop (LSHIFT_EXPR,
3201 fold_convert (unsigned_type, rhs),
3202 size_int (lbitpos), 0),
3205 return build2 (code, compare_type,
3206 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask),
3210 /* Subroutine for fold_truthop: decode a field reference.
3212 If EXP is a comparison reference, we return the innermost reference.
3214 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3215 set to the starting bit number.
3217 If the innermost field can be completely contained in a mode-sized
3218 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
3220 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3221 otherwise it is not changed.
3223 *PUNSIGNEDP is set to the signedness of the field.
3225 *PMASK is set to the mask used. This is either contained in a
3226 BIT_AND_EXPR or derived from the width of the field.
3228 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
3230 Return 0 if this is not a component reference or is one that we can't
3231 do anything with. */
3234 decode_field_reference (tree exp, HOST_WIDE_INT *pbitsize,
3235 HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
3236 int *punsignedp, int *pvolatilep,
3237 tree *pmask, tree *pand_mask)
3239 tree outer_type = 0;
3241 tree mask, inner, offset;
3243 unsigned int precision;
3245 /* All the optimizations using this function assume integer fields.
3246 There are problems with FP fields since the type_for_size call
3247 below can fail for, e.g., XFmode. */
3248 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
3251 /* We are interested in the bare arrangement of bits, so strip everything
3252 that doesn't affect the machine mode. However, record the type of the
3253 outermost expression if it may matter below. */
3254 if (TREE_CODE (exp) == NOP_EXPR
3255 || TREE_CODE (exp) == CONVERT_EXPR
3256 || TREE_CODE (exp) == NON_LVALUE_EXPR)
3257 outer_type = TREE_TYPE (exp);
3260 if (TREE_CODE (exp) == BIT_AND_EXPR)
3262 and_mask = TREE_OPERAND (exp, 1);
3263 exp = TREE_OPERAND (exp, 0);
3264 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
3265 if (TREE_CODE (and_mask) != INTEGER_CST)
3269 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
3270 punsignedp, pvolatilep);
3271 if ((inner == exp && and_mask == 0)
3272 || *pbitsize < 0 || offset != 0
3273 || TREE_CODE (inner) == PLACEHOLDER_EXPR)
3276 /* If the number of bits in the reference is the same as the bitsize of
3277 the outer type, then the outer type gives the signedness. Otherwise
3278 (in case of a small bitfield) the signedness is unchanged. */
3279 if (outer_type && *pbitsize == tree_low_cst (TYPE_SIZE (outer_type), 1))
3280 *punsignedp = TYPE_UNSIGNED (outer_type);
3282 /* Compute the mask to access the bitfield. */
3283 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
3284 precision = TYPE_PRECISION (unsigned_type);
3286 mask = build_int_2 (~0, ~0);
3287 TREE_TYPE (mask) = unsigned_type;
3288 force_fit_type (mask, 0);
3289 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3290 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3292 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
3294 mask = fold (build2 (BIT_AND_EXPR, unsigned_type,
3295 fold_convert (unsigned_type, and_mask), mask));
3298 *pand_mask = and_mask;
3302 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
3306 all_ones_mask_p (tree mask, int size)
3308 tree type = TREE_TYPE (mask);
3309 unsigned int precision = TYPE_PRECISION (type);
3312 tmask = build_int_2 (~0, ~0);
3313 TREE_TYPE (tmask) = lang_hooks.types.signed_type (type);
3314 force_fit_type (tmask, 0);
3316 tree_int_cst_equal (mask,
3317 const_binop (RSHIFT_EXPR,
3318 const_binop (LSHIFT_EXPR, tmask,
3319 size_int (precision - size),
3321 size_int (precision - size), 0));
3324 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
3325 represents the sign bit of EXP's type. If EXP represents a sign
3326 or zero extension, also test VAL against the unextended type.
3327 The return value is the (sub)expression whose sign bit is VAL,
3328 or NULL_TREE otherwise. */
3331 sign_bit_p (tree exp, tree val)
3333 unsigned HOST_WIDE_INT mask_lo, lo;
3334 HOST_WIDE_INT mask_hi, hi;
3338 /* Tree EXP must have an integral type. */
3339 t = TREE_TYPE (exp);
3340 if (! INTEGRAL_TYPE_P (t))
3343 /* Tree VAL must be an integer constant. */
3344 if (TREE_CODE (val) != INTEGER_CST
3345 || TREE_CONSTANT_OVERFLOW (val))
3348 width = TYPE_PRECISION (t);
3349 if (width > HOST_BITS_PER_WIDE_INT)
3351 hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
3354 mask_hi = ((unsigned HOST_WIDE_INT) -1
3355 >> (2 * HOST_BITS_PER_WIDE_INT - width));
3361 lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
3364 mask_lo = ((unsigned HOST_WIDE_INT) -1
3365 >> (HOST_BITS_PER_WIDE_INT - width));
3368 /* We mask off those bits beyond TREE_TYPE (exp) so that we can
3369 treat VAL as if it were unsigned. */
3370 if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi
3371 && (TREE_INT_CST_LOW (val) & mask_lo) == lo)
3374 /* Handle extension from a narrower type. */
3375 if (TREE_CODE (exp) == NOP_EXPR
3376 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
3377 return sign_bit_p (TREE_OPERAND (exp, 0), val);
3382 /* Subroutine for fold_truthop: determine if an operand is simple enough
3383 to be evaluated unconditionally. */
3386 simple_operand_p (tree exp)
3388 /* Strip any conversions that don't change the machine mode. */
3389 while ((TREE_CODE (exp) == NOP_EXPR
3390 || TREE_CODE (exp) == CONVERT_EXPR)
3391 && (TYPE_MODE (TREE_TYPE (exp))
3392 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)))))
3393 exp = TREE_OPERAND (exp, 0);
3395 return (TREE_CODE_CLASS (TREE_CODE (exp)) == 'c'
3397 && ! TREE_ADDRESSABLE (exp)
3398 && ! TREE_THIS_VOLATILE (exp)
3399 && ! DECL_NONLOCAL (exp)
3400 /* Don't regard global variables as simple. They may be
3401 allocated in ways unknown to the compiler (shared memory,
3402 #pragma weak, etc). */
3403 && ! TREE_PUBLIC (exp)
3404 && ! DECL_EXTERNAL (exp)
3405 /* Loading a static variable is unduly expensive, but global
3406 registers aren't expensive. */
3407 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
3410 /* The following functions are subroutines to fold_range_test and allow it to
3411 try to change a logical combination of comparisons into a range test.
3414 X == 2 || X == 3 || X == 4 || X == 5
3418 (unsigned) (X - 2) <= 3
3420 We describe each set of comparisons as being either inside or outside
3421 a range, using a variable named like IN_P, and then describe the
3422 range with a lower and upper bound. If one of the bounds is omitted,
3423 it represents either the highest or lowest value of the type.
3425 In the comments below, we represent a range by two numbers in brackets
3426 preceded by a "+" to designate being inside that range, or a "-" to
3427 designate being outside that range, so the condition can be inverted by
3428 flipping the prefix. An omitted bound is represented by a "-". For
3429 example, "- [-, 10]" means being outside the range starting at the lowest
3430 possible value and ending at 10, in other words, being greater than 10.
3431 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
3434 We set up things so that the missing bounds are handled in a consistent
3435 manner so neither a missing bound nor "true" and "false" need to be
3436 handled using a special case. */
3438 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
3439 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
3440 and UPPER1_P are nonzero if the respective argument is an upper bound
3441 and zero for a lower. TYPE, if nonzero, is the type of the result; it
3442 must be specified for a comparison. ARG1 will be converted to ARG0's
3443 type if both are specified. */
3446 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
3447 tree arg1, int upper1_p)
3453 /* If neither arg represents infinity, do the normal operation.
3454 Else, if not a comparison, return infinity. Else handle the special
3455 comparison rules. Note that most of the cases below won't occur, but
3456 are handled for consistency. */
3458 if (arg0 != 0 && arg1 != 0)
3460 tem = fold (build2 (code, type != 0 ? type : TREE_TYPE (arg0),
3461 arg0, fold_convert (TREE_TYPE (arg0), arg1)));
3463 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
3466 if (TREE_CODE_CLASS (code) != '<')
3469 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
3470 for neither. In real maths, we cannot assume open ended ranges are
3471 the same. But, this is computer arithmetic, where numbers are finite.
3472 We can therefore make the transformation of any unbounded range with
3473 the value Z, Z being greater than any representable number. This permits
3474 us to treat unbounded ranges as equal. */
3475 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
3476 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
3480 result = sgn0 == sgn1;
3483 result = sgn0 != sgn1;
3486 result = sgn0 < sgn1;
3489 result = sgn0 <= sgn1;
3492 result = sgn0 > sgn1;
3495 result = sgn0 >= sgn1;
3501 return constant_boolean_node (result, type);
3504 /* Given EXP, a logical expression, set the range it is testing into
3505 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
3506 actually being tested. *PLOW and *PHIGH will be made of the same type
3507 as the returned expression. If EXP is not a comparison, we will most
3508 likely not be returning a useful value and range. */
3511 make_range (tree exp, int *pin_p, tree *plow, tree *phigh)
3513 enum tree_code code;
3514 tree arg0 = NULL_TREE, arg1 = NULL_TREE, type = NULL_TREE;
3515 tree orig_type = NULL_TREE;
3517 tree low, high, n_low, n_high;
3519 /* Start with simply saying "EXP != 0" and then look at the code of EXP
3520 and see if we can refine the range. Some of the cases below may not
3521 happen, but it doesn't seem worth worrying about this. We "continue"
3522 the outer loop when we've changed something; otherwise we "break"
3523 the switch, which will "break" the while. */
3526 low = high = fold_convert (TREE_TYPE (exp), integer_zero_node);
3530 code = TREE_CODE (exp);
3532 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
3534 if (first_rtl_op (code) > 0)
3535 arg0 = TREE_OPERAND (exp, 0);
3536 if (TREE_CODE_CLASS (code) == '<'
3537 || TREE_CODE_CLASS (code) == '1'
3538 || TREE_CODE_CLASS (code) == '2')
3539 type = TREE_TYPE (arg0);
3540 if (TREE_CODE_CLASS (code) == '2'
3541 || TREE_CODE_CLASS (code) == '<'
3542 || (TREE_CODE_CLASS (code) == 'e'
3543 && TREE_CODE_LENGTH (code) > 1))
3544 arg1 = TREE_OPERAND (exp, 1);
3547 /* Set ORIG_TYPE as soon as TYPE is non-null so that we do not
3548 lose a cast by accident. */
3549 if (type != NULL_TREE && orig_type == NULL_TREE)
3554 case TRUTH_NOT_EXPR:
3555 in_p = ! in_p, exp = arg0;
3558 case EQ_EXPR: case NE_EXPR:
3559 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
3560 /* We can only do something if the range is testing for zero
3561 and if the second operand is an integer constant. Note that
3562 saying something is "in" the range we make is done by
3563 complementing IN_P since it will set in the initial case of
3564 being not equal to zero; "out" is leaving it alone. */
3565 if (low == 0 || high == 0
3566 || ! integer_zerop (low) || ! integer_zerop (high)
3567 || TREE_CODE (arg1) != INTEGER_CST)
3572 case NE_EXPR: /* - [c, c] */
3575 case EQ_EXPR: /* + [c, c] */
3576 in_p = ! in_p, low = high = arg1;
3578 case GT_EXPR: /* - [-, c] */
3579 low = 0, high = arg1;
3581 case GE_EXPR: /* + [c, -] */
3582 in_p = ! in_p, low = arg1, high = 0;
3584 case LT_EXPR: /* - [c, -] */
3585 low = arg1, high = 0;
3587 case LE_EXPR: /* + [-, c] */
3588 in_p = ! in_p, low = 0, high = arg1;
3596 /* If this is an unsigned comparison, we also know that EXP is
3597 greater than or equal to zero. We base the range tests we make
3598 on that fact, so we record it here so we can parse existing
3600 if (TYPE_UNSIGNED (type) && (low == 0 || high == 0))
3602 if (! merge_ranges (&n_in_p, &n_low, &n_high, in_p, low, high,
3603 1, fold_convert (type, integer_zero_node),
3607 in_p = n_in_p, low = n_low, high = n_high;
3609 /* If the high bound is missing, but we have a nonzero low
3610 bound, reverse the range so it goes from zero to the low bound
3612 if (high == 0 && low && ! integer_zerop (low))
3615 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
3616 integer_one_node, 0);
3617 low = fold_convert (type, integer_zero_node);
3623 /* (-x) IN [a,b] -> x in [-b, -a] */
3624 n_low = range_binop (MINUS_EXPR, type,
3625 fold_convert (type, integer_zero_node),
3627 n_high = range_binop (MINUS_EXPR, type,
3628 fold_convert (type, integer_zero_node),
3630 low = n_low, high = n_high;
3636 exp = build2 (MINUS_EXPR, type, negate_expr (arg0),
3637 fold_convert (type, integer_one_node));
3640 case PLUS_EXPR: case MINUS_EXPR:
3641 if (TREE_CODE (arg1) != INTEGER_CST)
3644 /* If EXP is signed, any overflow in the computation is undefined,
3645 so we don't worry about it so long as our computations on
3646 the bounds don't overflow. For unsigned, overflow is defined
3647 and this is exactly the right thing. */
3648 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3649 type, low, 0, arg1, 0);
3650 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3651 type, high, 1, arg1, 0);
3652 if ((n_low != 0 && TREE_OVERFLOW (n_low))
3653 || (n_high != 0 && TREE_OVERFLOW (n_high)))
3656 /* Check for an unsigned range which has wrapped around the maximum
3657 value thus making n_high < n_low, and normalize it. */
3658 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
3660 low = range_binop (PLUS_EXPR, type, n_high, 0,
3661 integer_one_node, 0);
3662 high = range_binop (MINUS_EXPR, type, n_low, 0,
3663 integer_one_node, 0);
3665 /* If the range is of the form +/- [ x+1, x ], we won't
3666 be able to normalize it. But then, it represents the
3667 whole range or the empty set, so make it
3669 if (tree_int_cst_equal (n_low, low)
3670 && tree_int_cst_equal (n_high, high))
3676 low = n_low, high = n_high;
3681 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
3682 if (TYPE_PRECISION (type) > TYPE_PRECISION (orig_type))
3685 if (! INTEGRAL_TYPE_P (type)
3686 || (low != 0 && ! int_fits_type_p (low, type))
3687 || (high != 0 && ! int_fits_type_p (high, type)))
3690 n_low = low, n_high = high;
3693 n_low = fold_convert (type, n_low);
3696 n_high = fold_convert (type, n_high);
3698 /* If we're converting from an unsigned to a signed type,
3699 we will be doing the comparison as unsigned. The tests above
3700 have already verified that LOW and HIGH are both positive.
3702 So we have to make sure that the original unsigned value will
3703 be interpreted as positive. */
3704 if (TYPE_UNSIGNED (type) && ! TYPE_UNSIGNED (TREE_TYPE (exp)))
3706 tree equiv_type = lang_hooks.types.type_for_mode
3707 (TYPE_MODE (type), 1);
3710 /* A range without an upper bound is, naturally, unbounded.
3711 Since convert would have cropped a very large value, use
3712 the max value for the destination type. */
3714 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
3715 : TYPE_MAX_VALUE (type);
3717 if (TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (exp)))
3718 high_positive = fold (build2 (RSHIFT_EXPR, type,
3722 integer_one_node)));
3724 /* If the low bound is specified, "and" the range with the
3725 range for which the original unsigned value will be
3729 if (! merge_ranges (&n_in_p, &n_low, &n_high,
3730 1, n_low, n_high, 1,
3731 fold_convert (type, integer_zero_node),
3735 in_p = (n_in_p == in_p);
3739 /* Otherwise, "or" the range with the range of the input
3740 that will be interpreted as negative. */
3741 if (! merge_ranges (&n_in_p, &n_low, &n_high,
3742 0, n_low, n_high, 1,
3743 fold_convert (type, integer_zero_node),
3747 in_p = (in_p != n_in_p);
3752 low = n_low, high = n_high;
3762 /* If EXP is a constant, we can evaluate whether this is true or false. */
3763 if (TREE_CODE (exp) == INTEGER_CST)
3765 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
3767 && integer_onep (range_binop (LE_EXPR, integer_type_node,
3773 *pin_p = in_p, *plow = low, *phigh = high;
3777 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
3778 type, TYPE, return an expression to test if EXP is in (or out of, depending
3779 on IN_P) the range. */
3782 build_range_check (tree type, tree exp, int in_p, tree low, tree high)
3784 tree etype = TREE_TYPE (exp);
3788 && (0 != (value = build_range_check (type, exp, 1, low, high))))
3789 return invert_truthvalue (value);
3791 if (low == 0 && high == 0)
3792 return fold_convert (type, integer_one_node);
3795 return fold (build2 (LE_EXPR, type, exp, high));
3798 return fold (build2 (GE_EXPR, type, exp, low));
3800 if (operand_equal_p (low, high, 0))
3801 return fold (build2 (EQ_EXPR, type, exp, low));
3803 if (integer_zerop (low))
3805 if (! TYPE_UNSIGNED (etype))
3807 etype = lang_hooks.types.unsigned_type (etype);
3808 high = fold_convert (etype, high);
3809 exp = fold_convert (etype, exp);
3811 return build_range_check (type, exp, 1, 0, high);
3814 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
3815 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
3817 unsigned HOST_WIDE_INT lo;
3821 prec = TYPE_PRECISION (etype);
3822 if (prec <= HOST_BITS_PER_WIDE_INT)
3825 lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
3829 hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
3830 lo = (unsigned HOST_WIDE_INT) -1;
3833 if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
3835 if (TYPE_UNSIGNED (etype))
3837 etype = lang_hooks.types.signed_type (etype);
3838 exp = fold_convert (etype, exp);
3840 return fold (build2 (GT_EXPR, type, exp,
3841 fold_convert (etype, integer_zero_node)));
3845 if (0 != (value = const_binop (MINUS_EXPR, high, low, 0))
3846 && ! TREE_OVERFLOW (value))
3847 return build_range_check (type,
3848 fold (build2 (MINUS_EXPR, etype, exp, low)),
3849 1, fold_convert (etype, integer_zero_node),
3855 /* Given two ranges, see if we can merge them into one. Return 1 if we
3856 can, 0 if we can't. Set the output range into the specified parameters. */
3859 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
3860 tree high0, int in1_p, tree low1, tree high1)
3868 int lowequal = ((low0 == 0 && low1 == 0)
3869 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
3870 low0, 0, low1, 0)));
3871 int highequal = ((high0 == 0 && high1 == 0)
3872 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
3873 high0, 1, high1, 1)));
3875 /* Make range 0 be the range that starts first, or ends last if they
3876 start at the same value. Swap them if it isn't. */
3877 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
3880 && integer_onep (range_binop (GT_EXPR, integer_type_node,
3881 high1, 1, high0, 1))))
3883 temp = in0_p, in0_p = in1_p, in1_p = temp;
3884 tem = low0, low0 = low1, low1 = tem;
3885 tem = high0, high0 = high1, high1 = tem;
3888 /* Now flag two cases, whether the ranges are disjoint or whether the
3889 second range is totally subsumed in the first. Note that the tests
3890 below are simplified by the ones above. */
3891 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
3892 high0, 1, low1, 0));
3893 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
3894 high1, 1, high0, 1));
3896 /* We now have four cases, depending on whether we are including or
3897 excluding the two ranges. */
3900 /* If they don't overlap, the result is false. If the second range
3901 is a subset it is the result. Otherwise, the range is from the start
3902 of the second to the end of the first. */
3904 in_p = 0, low = high = 0;
3906 in_p = 1, low = low1, high = high1;
3908 in_p = 1, low = low1, high = high0;
3911 else if (in0_p && ! in1_p)
3913 /* If they don't overlap, the result is the first range. If they are
3914 equal, the result is false. If the second range is a subset of the
3915 first, and the ranges begin at the same place, we go from just after
3916 the end of the first range to the end of the second. If the second
3917 range is not a subset of the first, or if it is a subset and both
3918 ranges end at the same place, the range starts at the start of the
3919 first range and ends just before the second range.
3920 Otherwise, we can't describe this as a single range. */
3922 in_p = 1, low = low0, high = high0;
3923 else if (lowequal && highequal)
3924 in_p = 0, low = high = 0;
3925 else if (subset && lowequal)
3927 in_p = 1, high = high0;
3928 low = range_binop (PLUS_EXPR, NULL_TREE, high1, 0,
3929 integer_one_node, 0);
3931 else if (! subset || highequal)
3933 in_p = 1, low = low0;
3934 high = range_binop (MINUS_EXPR, NULL_TREE, low1, 0,
3935 integer_one_node, 0);
3941 else if (! in0_p && in1_p)
3943 /* If they don't overlap, the result is the second range. If the second
3944 is a subset of the first, the result is false. Otherwise,
3945 the range starts just after the first range and ends at the
3946 end of the second. */
3948 in_p = 1, low = low1, high = high1;
3949 else if (subset || highequal)
3950 in_p = 0, low = high = 0;
3953 in_p = 1, high = high1;
3954 low = range_binop (PLUS_EXPR, NULL_TREE, high0, 1,
3955 integer_one_node, 0);
3961 /* The case where we are excluding both ranges. Here the complex case
3962 is if they don't overlap. In that case, the only time we have a
3963 range is if they are adjacent. If the second is a subset of the
3964 first, the result is the first. Otherwise, the range to exclude
3965 starts at the beginning of the first range and ends at the end of the
3969 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
3970 range_binop (PLUS_EXPR, NULL_TREE,
3972 integer_one_node, 1),
3974 in_p = 0, low = low0, high = high1;
3979 in_p = 0, low = low0, high = high0;
3981 in_p = 0, low = low0, high = high1;
3984 *pin_p = in_p, *plow = low, *phigh = high;
3988 #ifndef RANGE_TEST_NON_SHORT_CIRCUIT
3989 #define RANGE_TEST_NON_SHORT_CIRCUIT (BRANCH_COST >= 2)
3992 /* EXP is some logical combination of boolean tests. See if we can
3993 merge it into some range test. Return the new tree if so. */
3996 fold_range_test (tree exp)
3998 int or_op = (TREE_CODE (exp) == TRUTH_ORIF_EXPR
3999 || TREE_CODE (exp) == TRUTH_OR_EXPR);
4000 int in0_p, in1_p, in_p;
4001 tree low0, low1, low, high0, high1, high;
4002 tree lhs = make_range (TREE_OPERAND (exp, 0), &in0_p, &low0, &high0);
4003 tree rhs = make_range (TREE_OPERAND (exp, 1), &in1_p, &low1, &high1);
4006 /* If this is an OR operation, invert both sides; we will invert
4007 again at the end. */
4009 in0_p = ! in0_p, in1_p = ! in1_p;
4011 /* If both expressions are the same, if we can merge the ranges, and we
4012 can build the range test, return it or it inverted. If one of the
4013 ranges is always true or always false, consider it to be the same
4014 expression as the other. */
4015 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
4016 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
4018 && 0 != (tem = (build_range_check (TREE_TYPE (exp),
4020 : rhs != 0 ? rhs : integer_zero_node,
4022 return or_op ? invert_truthvalue (tem) : tem;
4024 /* On machines where the branch cost is expensive, if this is a
4025 short-circuited branch and the underlying object on both sides
4026 is the same, make a non-short-circuit operation. */
4027 else if (RANGE_TEST_NON_SHORT_CIRCUIT
4028 && lhs != 0 && rhs != 0
4029 && (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
4030 || TREE_CODE (exp) == TRUTH_ORIF_EXPR)
4031 && operand_equal_p (lhs, rhs, 0))
4033 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
4034 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
4035 which cases we can't do this. */
4036 if (simple_operand_p (lhs))
4037 return build2 (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
4038 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
4039 TREE_TYPE (exp), TREE_OPERAND (exp, 0),
4040 TREE_OPERAND (exp, 1));
4042 else if (lang_hooks.decls.global_bindings_p () == 0
4043 && ! CONTAINS_PLACEHOLDER_P (lhs))
4045 tree common = save_expr (lhs);
4047 if (0 != (lhs = build_range_check (TREE_TYPE (exp), common,
4048 or_op ? ! in0_p : in0_p,
4050 && (0 != (rhs = build_range_check (TREE_TYPE (exp), common,
4051 or_op ? ! in1_p : in1_p,
4053 return build2 (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
4054 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
4055 TREE_TYPE (exp), lhs, rhs);
4062 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
4063 bit value. Arrange things so the extra bits will be set to zero if and
4064 only if C is signed-extended to its full width. If MASK is nonzero,
4065 it is an INTEGER_CST that should be AND'ed with the extra bits. */
4068 unextend (tree c, int p, int unsignedp, tree mask)
4070 tree type = TREE_TYPE (c);
4071 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
4074 if (p == modesize || unsignedp)
4077 /* We work by getting just the sign bit into the low-order bit, then
4078 into the high-order bit, then sign-extend. We then XOR that value
4080 temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
4081 temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
4083 /* We must use a signed type in order to get an arithmetic right shift.
4084 However, we must also avoid introducing accidental overflows, so that
4085 a subsequent call to integer_zerop will work. Hence we must
4086 do the type conversion here. At this point, the constant is either
4087 zero or one, and the conversion to a signed type can never overflow.
4088 We could get an overflow if this conversion is done anywhere else. */
4089 if (TYPE_UNSIGNED (type))
4090 temp = fold_convert (lang_hooks.types.signed_type (type), temp);
4092 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
4093 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
4095 temp = const_binop (BIT_AND_EXPR, temp,
4096 fold_convert (TREE_TYPE (c), mask), 0);
4097 /* If necessary, convert the type back to match the type of C. */
4098 if (TYPE_UNSIGNED (type))
4099 temp = fold_convert (type, temp);
4101 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp, 0));
4104 /* Find ways of folding logical expressions of LHS and RHS:
4105 Try to merge two comparisons to the same innermost item.
4106 Look for range tests like "ch >= '0' && ch <= '9'".
4107 Look for combinations of simple terms on machines with expensive branches
4108 and evaluate the RHS unconditionally.
4110 For example, if we have p->a == 2 && p->b == 4 and we can make an
4111 object large enough to span both A and B, we can do this with a comparison
4112 against the object ANDed with the a mask.
4114 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
4115 operations to do this with one comparison.
4117 We check for both normal comparisons and the BIT_AND_EXPRs made this by
4118 function and the one above.
4120 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
4121 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
4123 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
4126 We return the simplified tree or 0 if no optimization is possible. */
4129 fold_truthop (enum tree_code code, tree truth_type, tree lhs, tree rhs)
4131 /* If this is the "or" of two comparisons, we can do something if
4132 the comparisons are NE_EXPR. If this is the "and", we can do something
4133 if the comparisons are EQ_EXPR. I.e.,
4134 (a->b == 2 && a->c == 4) can become (a->new == NEW).
4136 WANTED_CODE is this operation code. For single bit fields, we can
4137 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
4138 comparison for one-bit fields. */
4140 enum tree_code wanted_code;
4141 enum tree_code lcode, rcode;
4142 tree ll_arg, lr_arg, rl_arg, rr_arg;
4143 tree ll_inner, lr_inner, rl_inner, rr_inner;
4144 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
4145 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
4146 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
4147 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
4148 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
4149 enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
4150 enum machine_mode lnmode, rnmode;
4151 tree ll_mask, lr_mask, rl_mask, rr_mask;
4152 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
4153 tree l_const, r_const;
4154 tree lntype, rntype, result;
4155 int first_bit, end_bit;
4158 /* Start by getting the comparison codes. Fail if anything is volatile.
4159 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
4160 it were surrounded with a NE_EXPR. */
4162 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
4165 lcode = TREE_CODE (lhs);
4166 rcode = TREE_CODE (rhs);
4168 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
4170 lhs = build2 (NE_EXPR, truth_type, lhs, integer_zero_node);
4174 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
4176 rhs = build2 (NE_EXPR, truth_type, rhs, integer_zero_node);
4180 if (TREE_CODE_CLASS (lcode) != '<' || TREE_CODE_CLASS (rcode) != '<')
4183 ll_arg = TREE_OPERAND (lhs, 0);
4184 lr_arg = TREE_OPERAND (lhs, 1);
4185 rl_arg = TREE_OPERAND (rhs, 0);
4186 rr_arg = TREE_OPERAND (rhs, 1);
4188 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
4189 if (simple_operand_p (ll_arg)
4190 && simple_operand_p (lr_arg))
4193 if (operand_equal_p (ll_arg, rl_arg, 0)
4194 && operand_equal_p (lr_arg, rr_arg, 0))
4196 result = combine_comparisons (code, lcode, rcode,
4197 truth_type, ll_arg, lr_arg);
4201 else if (operand_equal_p (ll_arg, rr_arg, 0)
4202 && operand_equal_p (lr_arg, rl_arg, 0))
4204 result = combine_comparisons (code, lcode,
4205 swap_tree_comparison (rcode),
4206 truth_type, ll_arg, lr_arg);
4212 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
4213 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
4215 /* If the RHS can be evaluated unconditionally and its operands are
4216 simple, it wins to evaluate the RHS unconditionally on machines
4217 with expensive branches. In this case, this isn't a comparison
4218 that can be merged. Avoid doing this if the RHS is a floating-point
4219 comparison since those can trap. */
4221 if (BRANCH_COST >= 2
4222 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
4223 && simple_operand_p (rl_arg)
4224 && simple_operand_p (rr_arg))
4226 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
4227 if (code == TRUTH_OR_EXPR
4228 && lcode == NE_EXPR && integer_zerop (lr_arg)
4229 && rcode == NE_EXPR && integer_zerop (rr_arg)
4230 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg))
4231 return build2 (NE_EXPR, truth_type,
4232 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
4234 fold_convert (TREE_TYPE (ll_arg), integer_zero_node));
4236 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
4237 if (code == TRUTH_AND_EXPR
4238 && lcode == EQ_EXPR && integer_zerop (lr_arg)
4239 && rcode == EQ_EXPR && integer_zerop (rr_arg)
4240 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg))
4241 return build2 (EQ_EXPR, truth_type,
4242 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
4244 fold_convert (TREE_TYPE (ll_arg), integer_zero_node));
4246 return build2 (code, truth_type, lhs, rhs);
4249 /* See if the comparisons can be merged. Then get all the parameters for
4252 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
4253 || (rcode != EQ_EXPR && rcode != NE_EXPR))
4257 ll_inner = decode_field_reference (ll_arg,
4258 &ll_bitsize, &ll_bitpos, &ll_mode,
4259 &ll_unsignedp, &volatilep, &ll_mask,
4261 lr_inner = decode_field_reference (lr_arg,
4262 &lr_bitsize, &lr_bitpos, &lr_mode,
4263 &lr_unsignedp, &volatilep, &lr_mask,
4265 rl_inner = decode_field_reference (rl_arg,
4266 &rl_bitsize, &rl_bitpos, &rl_mode,
4267 &rl_unsignedp, &volatilep, &rl_mask,
4269 rr_inner = decode_field_reference (rr_arg,
4270 &rr_bitsize, &rr_bitpos, &rr_mode,
4271 &rr_unsignedp, &volatilep, &rr_mask,
4274 /* It must be true that the inner operation on the lhs of each
4275 comparison must be the same if we are to be able to do anything.
4276 Then see if we have constants. If not, the same must be true for
4278 if (volatilep || ll_inner == 0 || rl_inner == 0
4279 || ! operand_equal_p (ll_inner, rl_inner, 0))
4282 if (TREE_CODE (lr_arg) == INTEGER_CST
4283 && TREE_CODE (rr_arg) == INTEGER_CST)
4284 l_const = lr_arg, r_const = rr_arg;
4285 else if (lr_inner == 0 || rr_inner == 0
4286 || ! operand_equal_p (lr_inner, rr_inner, 0))
4289 l_const = r_const = 0;
4291 /* If either comparison code is not correct for our logical operation,
4292 fail. However, we can convert a one-bit comparison against zero into
4293 the opposite comparison against that bit being set in the field. */
4295 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
4296 if (lcode != wanted_code)
4298 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
4300 /* Make the left operand unsigned, since we are only interested
4301 in the value of one bit. Otherwise we are doing the wrong
4310 /* This is analogous to the code for l_const above. */
4311 if (rcode != wanted_code)
4313 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
4322 /* After this point all optimizations will generate bit-field
4323 references, which we might not want. */
4324 if (! lang_hooks.can_use_bit_fields_p ())
4327 /* See if we can find a mode that contains both fields being compared on
4328 the left. If we can't, fail. Otherwise, update all constants and masks
4329 to be relative to a field of that size. */
4330 first_bit = MIN (ll_bitpos, rl_bitpos);
4331 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
4332 lnmode = get_best_mode (end_bit - first_bit, first_bit,
4333 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
4335 if (lnmode == VOIDmode)
4338 lnbitsize = GET_MODE_BITSIZE (lnmode);
4339 lnbitpos = first_bit & ~ (lnbitsize - 1);
4340 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
4341 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
4343 if (BYTES_BIG_ENDIAN)
4345 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
4346 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
4349 ll_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, ll_mask),
4350 size_int (xll_bitpos), 0);
4351 rl_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, rl_mask),
4352 size_int (xrl_bitpos), 0);
4356 l_const = fold_convert (lntype, l_const);
4357 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
4358 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
4359 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
4360 fold (build1 (BIT_NOT_EXPR,
4364 warning ("comparison is always %d", wanted_code == NE_EXPR);
4366 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
4371 r_const = fold_convert (lntype, r_const);
4372 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
4373 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
4374 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
4375 fold (build1 (BIT_NOT_EXPR,
4379 warning ("comparison is always %d", wanted_code == NE_EXPR);
4381 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
4385 /* If the right sides are not constant, do the same for it. Also,
4386 disallow this optimization if a size or signedness mismatch occurs
4387 between the left and right sides. */
4390 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
4391 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
4392 /* Make sure the two fields on the right
4393 correspond to the left without being swapped. */
4394 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
4397 first_bit = MIN (lr_bitpos, rr_bitpos);
4398 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
4399 rnmode = get_best_mode (end_bit - first_bit, first_bit,
4400 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
4402 if (rnmode == VOIDmode)
4405 rnbitsize = GET_MODE_BITSIZE (rnmode);
4406 rnbitpos = first_bit & ~ (rnbitsize - 1);
4407 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
4408 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
4410 if (BYTES_BIG_ENDIAN)
4412 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
4413 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
4416 lr_mask = const_binop (LSHIFT_EXPR, fold_convert (rntype, lr_mask),
4417 size_int (xlr_bitpos), 0);
4418 rr_mask = const_binop (LSHIFT_EXPR, fold_convert (rntype, rr_mask),
4419 size_int (xrr_bitpos), 0);
4421 /* Make a mask that corresponds to both fields being compared.
4422 Do this for both items being compared. If the operands are the
4423 same size and the bits being compared are in the same position
4424 then we can do this by masking both and comparing the masked
4426 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
4427 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0);
4428 if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
4430 lhs = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
4431 ll_unsignedp || rl_unsignedp);
4432 if (! all_ones_mask_p (ll_mask, lnbitsize))
4433 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
4435 rhs = make_bit_field_ref (lr_inner, rntype, rnbitsize, rnbitpos,
4436 lr_unsignedp || rr_unsignedp);
4437 if (! all_ones_mask_p (lr_mask, rnbitsize))
4438 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
4440 return build2 (wanted_code, truth_type, lhs, rhs);
4443 /* There is still another way we can do something: If both pairs of
4444 fields being compared are adjacent, we may be able to make a wider
4445 field containing them both.
4447 Note that we still must mask the lhs/rhs expressions. Furthermore,
4448 the mask must be shifted to account for the shift done by
4449 make_bit_field_ref. */
4450 if ((ll_bitsize + ll_bitpos == rl_bitpos
4451 && lr_bitsize + lr_bitpos == rr_bitpos)
4452 || (ll_bitpos == rl_bitpos + rl_bitsize
4453 && lr_bitpos == rr_bitpos + rr_bitsize))
4457 lhs = make_bit_field_ref (ll_inner, lntype, ll_bitsize + rl_bitsize,
4458 MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
4459 rhs = make_bit_field_ref (lr_inner, rntype, lr_bitsize + rr_bitsize,
4460 MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
4462 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
4463 size_int (MIN (xll_bitpos, xrl_bitpos)), 0);
4464 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
4465 size_int (MIN (xlr_bitpos, xrr_bitpos)), 0);
4467 /* Convert to the smaller type before masking out unwanted bits. */
4469 if (lntype != rntype)
4471 if (lnbitsize > rnbitsize)
4473 lhs = fold_convert (rntype, lhs);
4474 ll_mask = fold_convert (rntype, ll_mask);
4477 else if (lnbitsize < rnbitsize)
4479 rhs = fold_convert (lntype, rhs);
4480 lr_mask = fold_convert (lntype, lr_mask);
4485 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
4486 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
4488 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
4489 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
4491 return build2 (wanted_code, truth_type, lhs, rhs);
4497 /* Handle the case of comparisons with constants. If there is something in
4498 common between the masks, those bits of the constants must be the same.
4499 If not, the condition is always false. Test for this to avoid generating
4500 incorrect code below. */
4501 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
4502 if (! integer_zerop (result)
4503 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
4504 const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
4506 if (wanted_code == NE_EXPR)
4508 warning ("`or' of unmatched not-equal tests is always 1");
4509 return constant_boolean_node (true, truth_type);
4513 warning ("`and' of mutually exclusive equal-tests is always 0");
4514 return constant_boolean_node (false, truth_type);
4518 /* Construct the expression we will return. First get the component
4519 reference we will make. Unless the mask is all ones the width of
4520 that field, perform the mask operation. Then compare with the
4522 result = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
4523 ll_unsignedp || rl_unsignedp);
4525 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
4526 if (! all_ones_mask_p (ll_mask, lnbitsize))
4527 result = build2 (BIT_AND_EXPR, lntype, result, ll_mask);
4529 return build2 (wanted_code, truth_type, result,
4530 const_binop (BIT_IOR_EXPR, l_const, r_const, 0));
4533 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
4537 optimize_minmax_comparison (tree t)
4539 tree type = TREE_TYPE (t);
4540 tree arg0 = TREE_OPERAND (t, 0);
4541 enum tree_code op_code;
4542 tree comp_const = TREE_OPERAND (t, 1);
4544 int consts_equal, consts_lt;
4547 STRIP_SIGN_NOPS (arg0);
4549 op_code = TREE_CODE (arg0);
4550 minmax_const = TREE_OPERAND (arg0, 1);
4551 consts_equal = tree_int_cst_equal (minmax_const, comp_const);
4552 consts_lt = tree_int_cst_lt (minmax_const, comp_const);
4553 inner = TREE_OPERAND (arg0, 0);
4555 /* If something does not permit us to optimize, return the original tree. */
4556 if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
4557 || TREE_CODE (comp_const) != INTEGER_CST
4558 || TREE_CONSTANT_OVERFLOW (comp_const)
4559 || TREE_CODE (minmax_const) != INTEGER_CST
4560 || TREE_CONSTANT_OVERFLOW (minmax_const))
4563 /* Now handle all the various comparison codes. We only handle EQ_EXPR
4564 and GT_EXPR, doing the rest with recursive calls using logical
4566 switch (TREE_CODE (t))
4568 case NE_EXPR: case LT_EXPR: case LE_EXPR:
4570 invert_truthvalue (optimize_minmax_comparison (invert_truthvalue (t)));
4574 fold (build2 (TRUTH_ORIF_EXPR, type,
4575 optimize_minmax_comparison
4576 (build2 (EQ_EXPR, type, arg0, comp_const)),
4577 optimize_minmax_comparison
4578 (build2 (GT_EXPR, type, arg0, comp_const))));
4581 if (op_code == MAX_EXPR && consts_equal)
4582 /* MAX (X, 0) == 0 -> X <= 0 */
4583 return fold (build2 (LE_EXPR, type, inner, comp_const));
4585 else if (op_code == MAX_EXPR && consts_lt)
4586 /* MAX (X, 0) == 5 -> X == 5 */
4587 return fold (build2 (EQ_EXPR, type, inner, comp_const));
4589 else if (op_code == MAX_EXPR)
4590 /* MAX (X, 0) == -1 -> false */
4591 return omit_one_operand (type, integer_zero_node, inner);
4593 else if (consts_equal)
4594 /* MIN (X, 0) == 0 -> X >= 0 */
4595 return fold (build2 (GE_EXPR, type, inner, comp_const));
4598 /* MIN (X, 0) == 5 -> false */
4599 return omit_one_operand (type, integer_zero_node, inner);
4602 /* MIN (X, 0) == -1 -> X == -1 */
4603 return fold (build2 (EQ_EXPR, type, inner, comp_const));
4606 if (op_code == MAX_EXPR && (consts_equal || consts_lt))
4607 /* MAX (X, 0) > 0 -> X > 0
4608 MAX (X, 0) > 5 -> X > 5 */
4609 return fold (build2 (GT_EXPR, type, inner, comp_const));
4611 else if (op_code == MAX_EXPR)
4612 /* MAX (X, 0) > -1 -> true */
4613 return omit_one_operand (type, integer_one_node, inner);
4615 else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
4616 /* MIN (X, 0) > 0 -> false
4617 MIN (X, 0) > 5 -> false */
4618 return omit_one_operand (type, integer_zero_node, inner);
4621 /* MIN (X, 0) > -1 -> X > -1 */
4622 return fold (build2 (GT_EXPR, type, inner, comp_const));
4629 /* T is an integer expression that is being multiplied, divided, or taken a
4630 modulus (CODE says which and what kind of divide or modulus) by a
4631 constant C. See if we can eliminate that operation by folding it with
4632 other operations already in T. WIDE_TYPE, if non-null, is a type that
4633 should be used for the computation if wider than our type.
4635 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
4636 (X * 2) + (Y * 4). We must, however, be assured that either the original
4637 expression would not overflow or that overflow is undefined for the type
4638 in the language in question.
4640 We also canonicalize (X + 7) * 4 into X * 4 + 28 in the hope that either
4641 the machine has a multiply-accumulate insn or that this is part of an
4642 addressing calculation.
4644 If we return a non-null expression, it is an equivalent form of the
4645 original computation, but need not be in the original type. */
4648 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type)
4650 /* To avoid exponential search depth, refuse to allow recursion past
4651 three levels. Beyond that (1) it's highly unlikely that we'll find
4652 something interesting and (2) we've probably processed it before
4653 when we built the inner expression. */
4662 ret = extract_muldiv_1 (t, c, code, wide_type);
4669 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type)
4671 tree type = TREE_TYPE (t);
4672 enum tree_code tcode = TREE_CODE (t);
4673 tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
4674 > GET_MODE_SIZE (TYPE_MODE (type)))
4675 ? wide_type : type);
4677 int same_p = tcode == code;
4678 tree op0 = NULL_TREE, op1 = NULL_TREE;
4680 /* Don't deal with constants of zero here; they confuse the code below. */
4681 if (integer_zerop (c))
4684 if (TREE_CODE_CLASS (tcode) == '1')
4685 op0 = TREE_OPERAND (t, 0);
4687 if (TREE_CODE_CLASS (tcode) == '2')
4688 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
4690 /* Note that we need not handle conditional operations here since fold
4691 already handles those cases. So just do arithmetic here. */
4695 /* For a constant, we can always simplify if we are a multiply
4696 or (for divide and modulus) if it is a multiple of our constant. */
4697 if (code == MULT_EXPR
4698 || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c, 0)))
4699 return const_binop (code, fold_convert (ctype, t),
4700 fold_convert (ctype, c), 0);
4703 case CONVERT_EXPR: case NON_LVALUE_EXPR: case NOP_EXPR:
4704 /* If op0 is an expression ... */
4705 if ((TREE_CODE_CLASS (TREE_CODE (op0)) == '<'
4706 || TREE_CODE_CLASS (TREE_CODE (op0)) == '1'
4707 || TREE_CODE_CLASS (TREE_CODE (op0)) == '2'
4708 || TREE_CODE_CLASS (TREE_CODE (op0)) == 'e')
4709 /* ... and is unsigned, and its type is smaller than ctype,
4710 then we cannot pass through as widening. */
4711 && ((TYPE_UNSIGNED (TREE_TYPE (op0))
4712 && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
4713 && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
4714 && (GET_MODE_SIZE (TYPE_MODE (ctype))
4715 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))))
4716 /* ... or its type is larger than ctype,
4717 then we cannot pass through this truncation. */
4718 || (GET_MODE_SIZE (TYPE_MODE (ctype))
4719 < GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0))))
4720 /* ... or signedness changes for division or modulus,
4721 then we cannot pass through this conversion. */
4722 || (code != MULT_EXPR
4723 && (TYPE_UNSIGNED (ctype)
4724 != TYPE_UNSIGNED (TREE_TYPE (op0))))))
4727 /* Pass the constant down and see if we can make a simplification. If
4728 we can, replace this expression with the inner simplification for
4729 possible later conversion to our or some other type. */
4730 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
4731 && TREE_CODE (t2) == INTEGER_CST
4732 && ! TREE_CONSTANT_OVERFLOW (t2)
4733 && (0 != (t1 = extract_muldiv (op0, t2, code,
4735 ? ctype : NULL_TREE))))
4739 case NEGATE_EXPR: case ABS_EXPR:
4740 if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
4741 return fold (build1 (tcode, ctype, fold_convert (ctype, t1)));
4744 case MIN_EXPR: case MAX_EXPR:
4745 /* If widening the type changes the signedness, then we can't perform
4746 this optimization as that changes the result. */
4747 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
4750 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
4751 if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0
4752 && (t2 = extract_muldiv (op1, c, code, wide_type)) != 0)
4754 if (tree_int_cst_sgn (c) < 0)
4755 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
4757 return fold (build2 (tcode, ctype, fold_convert (ctype, t1),
4758 fold_convert (ctype, t2)));
4762 case LSHIFT_EXPR: case RSHIFT_EXPR:
4763 /* If the second operand is constant, this is a multiplication
4764 or floor division, by a power of two, so we can treat it that
4765 way unless the multiplier or divisor overflows. */
4766 if (TREE_CODE (op1) == INTEGER_CST
4767 /* const_binop may not detect overflow correctly,
4768 so check for it explicitly here. */
4769 && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
4770 && TREE_INT_CST_HIGH (op1) == 0
4771 && 0 != (t1 = fold_convert (ctype,
4772 const_binop (LSHIFT_EXPR,
4775 && ! TREE_OVERFLOW (t1))
4776 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
4777 ? MULT_EXPR : FLOOR_DIV_EXPR,
4778 ctype, fold_convert (ctype, op0), t1),
4779 c, code, wide_type);
4782 case PLUS_EXPR: case MINUS_EXPR:
4783 /* See if we can eliminate the operation on both sides. If we can, we
4784 can return a new PLUS or MINUS. If we can't, the only remaining
4785 cases where we can do anything are if the second operand is a
4787 t1 = extract_muldiv (op0, c, code, wide_type);
4788 t2 = extract_muldiv (op1, c, code, wide_type);
4789 if (t1 != 0 && t2 != 0
4790 && (code == MULT_EXPR
4791 /* If not multiplication, we can only do this if both operands
4792 are divisible by c. */
4793 || (multiple_of_p (ctype, op0, c)
4794 && multiple_of_p (ctype, op1, c))))
4795 return fold (build2 (tcode, ctype, fold_convert (ctype, t1),
4796 fold_convert (ctype, t2)));
4798 /* If this was a subtraction, negate OP1 and set it to be an addition.
4799 This simplifies the logic below. */
4800 if (tcode == MINUS_EXPR)
4801 tcode = PLUS_EXPR, op1 = negate_expr (op1);
4803 if (TREE_CODE (op1) != INTEGER_CST)
4806 /* If either OP1 or C are negative, this optimization is not safe for
4807 some of the division and remainder types while for others we need
4808 to change the code. */
4809 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
4811 if (code == CEIL_DIV_EXPR)
4812 code = FLOOR_DIV_EXPR;
4813 else if (code == FLOOR_DIV_EXPR)
4814 code = CEIL_DIV_EXPR;
4815 else if (code != MULT_EXPR
4816 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
4820 /* If it's a multiply or a division/modulus operation of a multiple
4821 of our constant, do the operation and verify it doesn't overflow. */
4822 if (code == MULT_EXPR
4823 || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
4825 op1 = const_binop (code, fold_convert (ctype, op1),
4826 fold_convert (ctype, c), 0);
4827 /* We allow the constant to overflow with wrapping semantics. */
4829 || (TREE_OVERFLOW (op1) && ! flag_wrapv))
4835 /* If we have an unsigned type is not a sizetype, we cannot widen
4836 the operation since it will change the result if the original
4837 computation overflowed. */
4838 if (TYPE_UNSIGNED (ctype)
4839 && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
4843 /* If we were able to eliminate our operation from the first side,
4844 apply our operation to the second side and reform the PLUS. */
4845 if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
4846 return fold (build2 (tcode, ctype, fold_convert (ctype, t1), op1));
4848 /* The last case is if we are a multiply. In that case, we can
4849 apply the distributive law to commute the multiply and addition
4850 if the multiplication of the constants doesn't overflow. */
4851 if (code == MULT_EXPR)
4852 return fold (build2 (tcode, ctype,
4853 fold (build2 (code, ctype,
4854 fold_convert (ctype, op0),
4855 fold_convert (ctype, c))),
4861 /* We have a special case here if we are doing something like
4862 (C * 8) % 4 since we know that's zero. */
4863 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
4864 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
4865 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
4866 && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
4867 return omit_one_operand (type, integer_zero_node, op0);
4869 /* ... fall through ... */
4871 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
4872 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
4873 /* If we can extract our operation from the LHS, do so and return a
4874 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
4875 do something only if the second operand is a constant. */
4877 && (t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
4878 return fold (build2 (tcode, ctype, fold_convert (ctype, t1),
4879 fold_convert (ctype, op1)));
4880 else if (tcode == MULT_EXPR && code == MULT_EXPR
4881 && (t1 = extract_muldiv (op1, c, code, wide_type)) != 0)
4882 return fold (build2 (tcode, ctype, fold_convert (ctype, op0),
4883 fold_convert (ctype, t1)));
4884 else if (TREE_CODE (op1) != INTEGER_CST)
4887 /* If these are the same operation types, we can associate them
4888 assuming no overflow. */
4890 && 0 != (t1 = const_binop (MULT_EXPR, fold_convert (ctype, op1),
4891 fold_convert (ctype, c), 0))
4892 && ! TREE_OVERFLOW (t1))
4893 return fold (build2 (tcode, ctype, fold_convert (ctype, op0), t1));
4895 /* If these operations "cancel" each other, we have the main
4896 optimizations of this pass, which occur when either constant is a
4897 multiple of the other, in which case we replace this with either an
4898 operation or CODE or TCODE.
4900 If we have an unsigned type that is not a sizetype, we cannot do
4901 this since it will change the result if the original computation
4903 if ((! TYPE_UNSIGNED (ctype)
4904 || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
4906 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
4907 || (tcode == MULT_EXPR
4908 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
4909 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR)))
4911 if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
4912 return fold (build2 (tcode, ctype, fold_convert (ctype, op0),
4913 fold_convert (ctype,
4914 const_binop (TRUNC_DIV_EXPR,
4916 else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1, 0)))
4917 return fold (build2 (code, ctype, fold_convert (ctype, op0),
4918 fold_convert (ctype,
4919 const_binop (TRUNC_DIV_EXPR,
4931 /* Return a node which has the indicated constant VALUE (either 0 or
4932 1), and is of the indicated TYPE. */
4935 constant_boolean_node (int value, tree type)
4937 if (type == integer_type_node)
4938 return value ? integer_one_node : integer_zero_node;
4939 else if (type == boolean_type_node)
4940 return value ? boolean_true_node : boolean_false_node;
4941 else if (TREE_CODE (type) == BOOLEAN_TYPE)
4942 return lang_hooks.truthvalue_conversion (value ? integer_one_node
4943 : integer_zero_node);
4946 tree t = build_int_2 (value, 0);
4948 TREE_TYPE (t) = type;
4953 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
4954 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
4955 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
4956 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
4957 COND is the first argument to CODE; otherwise (as in the example
4958 given here), it is the second argument. TYPE is the type of the
4959 original expression. Return NULL_TREE if no simplification is
4963 fold_binary_op_with_conditional_arg (enum tree_code code, tree type,
4964 tree cond, tree arg, int cond_first_p)
4966 tree test, true_value, false_value;
4967 tree lhs = NULL_TREE;
4968 tree rhs = NULL_TREE;
4970 /* This transformation is only worthwhile if we don't have to wrap
4971 arg in a SAVE_EXPR, and the operation can be simplified on atleast
4972 one of the branches once its pushed inside the COND_EXPR. */
4973 if (!TREE_CONSTANT (arg))
4976 if (TREE_CODE (cond) == COND_EXPR)
4978 test = TREE_OPERAND (cond, 0);
4979 true_value = TREE_OPERAND (cond, 1);
4980 false_value = TREE_OPERAND (cond, 2);
4981 /* If this operand throws an expression, then it does not make
4982 sense to try to perform a logical or arithmetic operation
4984 if (VOID_TYPE_P (TREE_TYPE (true_value)))
4986 if (VOID_TYPE_P (TREE_TYPE (false_value)))
4991 tree testtype = TREE_TYPE (cond);
4993 true_value = constant_boolean_node (true, testtype);
4994 false_value = constant_boolean_node (false, testtype);
4998 lhs = fold (cond_first_p ? build2 (code, type, true_value, arg)
4999 : build2 (code, type, arg, true_value));
5001 rhs = fold (cond_first_p ? build2 (code, type, false_value, arg)
5002 : build2 (code, type, arg, false_value));
5004 test = fold (build3 (COND_EXPR, type, test, lhs, rhs));
5005 return fold_convert (type, test);
5009 /* Subroutine of fold() that checks for the addition of +/- 0.0.
5011 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
5012 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
5013 ADDEND is the same as X.
5015 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
5016 and finite. The problematic cases are when X is zero, and its mode
5017 has signed zeros. In the case of rounding towards -infinity,
5018 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
5019 modes, X + 0 is not the same as X because -0 + 0 is 0. */
5022 fold_real_zero_addition_p (tree type, tree addend, int negate)
5024 if (!real_zerop (addend))
5027 /* Don't allow the fold with -fsignaling-nans. */
5028 if (HONOR_SNANS (TYPE_MODE (type)))
5031 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
5032 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
5035 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
5036 if (TREE_CODE (addend) == REAL_CST
5037 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
5040 /* The mode has signed zeros, and we have to honor their sign.
5041 In this situation, there is only one case we can return true for.
5042 X - 0 is the same as X unless rounding towards -infinity is
5044 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
5047 /* Subroutine of fold() that checks comparisons of built-in math
5048 functions against real constants.
5050 FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
5051 operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR. TYPE
5052 is the type of the result and ARG0 and ARG1 are the operands of the
5053 comparison. ARG1 must be a TREE_REAL_CST.
5055 The function returns the constant folded tree if a simplification
5056 can be made, and NULL_TREE otherwise. */
5059 fold_mathfn_compare (enum built_in_function fcode, enum tree_code code,
5060 tree type, tree arg0, tree arg1)
5064 if (BUILTIN_SQRT_P (fcode))
5066 tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1));
5067 enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
5069 c = TREE_REAL_CST (arg1);
5070 if (REAL_VALUE_NEGATIVE (c))
5072 /* sqrt(x) < y is always false, if y is negative. */
5073 if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
5074 return omit_one_operand (type, integer_zero_node, arg);
5076 /* sqrt(x) > y is always true, if y is negative and we
5077 don't care about NaNs, i.e. negative values of x. */
5078 if (code == NE_EXPR || !HONOR_NANS (mode))
5079 return omit_one_operand (type, integer_one_node, arg);
5081 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
5082 return fold (build2 (GE_EXPR, type, arg,
5083 build_real (TREE_TYPE (arg), dconst0)));
5085 else if (code == GT_EXPR || code == GE_EXPR)
5089 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
5090 real_convert (&c2, mode, &c2);
5092 if (REAL_VALUE_ISINF (c2))
5094 /* sqrt(x) > y is x == +Inf, when y is very large. */
5095 if (HONOR_INFINITIES (mode))
5096 return fold (build2 (EQ_EXPR, type, arg,
5097 build_real (TREE_TYPE (arg), c2)));
5099 /* sqrt(x) > y is always false, when y is very large
5100 and we don't care about infinities. */
5101 return omit_one_operand (type, integer_zero_node, arg);
5104 /* sqrt(x) > c is the same as x > c*c. */
5105 return fold (build2 (code, type, arg,
5106 build_real (TREE_TYPE (arg), c2)));
5108 else if (code == LT_EXPR || code == LE_EXPR)
5112 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
5113 real_convert (&c2, mode, &c2);
5115 if (REAL_VALUE_ISINF (c2))
5117 /* sqrt(x) < y is always true, when y is a very large
5118 value and we don't care about NaNs or Infinities. */
5119 if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
5120 return omit_one_operand (type, integer_one_node, arg);
5122 /* sqrt(x) < y is x != +Inf when y is very large and we
5123 don't care about NaNs. */
5124 if (! HONOR_NANS (mode))
5125 return fold (build2 (NE_EXPR, type, arg,
5126 build_real (TREE_TYPE (arg), c2)));
5128 /* sqrt(x) < y is x >= 0 when y is very large and we
5129 don't care about Infinities. */
5130 if (! HONOR_INFINITIES (mode))
5131 return fold (build2 (GE_EXPR, type, arg,
5132 build_real (TREE_TYPE (arg), dconst0)));
5134 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
5135 if (lang_hooks.decls.global_bindings_p () != 0
5136 || CONTAINS_PLACEHOLDER_P (arg))
5139 arg = save_expr (arg);
5140 return fold (build2 (TRUTH_ANDIF_EXPR, type,
5141 fold (build2 (GE_EXPR, type, arg,
5142 build_real (TREE_TYPE (arg),
5144 fold (build2 (NE_EXPR, type, arg,
5145 build_real (TREE_TYPE (arg),
5149 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
5150 if (! HONOR_NANS (mode))
5151 return fold (build2 (code, type, arg,
5152 build_real (TREE_TYPE (arg), c2)));
5154 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
5155 if (lang_hooks.decls.global_bindings_p () == 0
5156 && ! CONTAINS_PLACEHOLDER_P (arg))
5158 arg = save_expr (arg);
5159 return fold (build2 (TRUTH_ANDIF_EXPR, type,
5160 fold (build2 (GE_EXPR, type, arg,
5161 build_real (TREE_TYPE (arg),
5163 fold (build2 (code, type, arg,
5164 build_real (TREE_TYPE (arg),
5173 /* Subroutine of fold() that optimizes comparisons against Infinities,
5174 either +Inf or -Inf.
5176 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
5177 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
5178 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
5180 The function returns the constant folded tree if a simplification
5181 can be made, and NULL_TREE otherwise. */
5184 fold_inf_compare (enum tree_code code, tree type, tree arg0, tree arg1)
5186 enum machine_mode mode;
5187 REAL_VALUE_TYPE max;
5191 mode = TYPE_MODE (TREE_TYPE (arg0));
5193 /* For negative infinity swap the sense of the comparison. */
5194 neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
5196 code = swap_tree_comparison (code);
5201 /* x > +Inf is always false, if with ignore sNANs. */
5202 if (HONOR_SNANS (mode))
5204 return omit_one_operand (type, integer_zero_node, arg0);
5207 /* x <= +Inf is always true, if we don't case about NaNs. */
5208 if (! HONOR_NANS (mode))
5209 return omit_one_operand (type, integer_one_node, arg0);
5211 /* x <= +Inf is the same as x == x, i.e. isfinite(x). */
5212 if (lang_hooks.decls.global_bindings_p () == 0
5213 && ! CONTAINS_PLACEHOLDER_P (arg0))
5215 arg0 = save_expr (arg0);
5216 return fold (build2 (EQ_EXPR, type, arg0, arg0));
5222 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
5223 real_maxval (&max, neg, mode);
5224 return fold (build2 (neg ? LT_EXPR : GT_EXPR, type,
5225 arg0, build_real (TREE_TYPE (arg0), max)));
5228 /* x < +Inf is always equal to x <= DBL_MAX. */
5229 real_maxval (&max, neg, mode);
5230 return fold (build2 (neg ? GE_EXPR : LE_EXPR, type,
5231 arg0, build_real (TREE_TYPE (arg0), max)));
5234 /* x != +Inf is always equal to !(x > DBL_MAX). */
5235 real_maxval (&max, neg, mode);
5236 if (! HONOR_NANS (mode))
5237 return fold (build2 (neg ? GE_EXPR : LE_EXPR, type,
5238 arg0, build_real (TREE_TYPE (arg0), max)));
5240 /* The transformation below creates non-gimple code and thus is
5241 not appropriate if we are in gimple form. */
5245 temp = fold (build2 (neg ? LT_EXPR : GT_EXPR, type,
5246 arg0, build_real (TREE_TYPE (arg0), max)));
5247 return fold (build1 (TRUTH_NOT_EXPR, type, temp));
5256 /* Subroutine of fold() that optimizes comparisons of a division by
5257 a nonzero integer constant against an integer constant, i.e.
5260 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
5261 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
5262 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
5264 The function returns the constant folded tree if a simplification
5265 can be made, and NULL_TREE otherwise. */
5268 fold_div_compare (enum tree_code code, tree type, tree arg0, tree arg1)
5270 tree prod, tmp, hi, lo;
5271 tree arg00 = TREE_OPERAND (arg0, 0);
5272 tree arg01 = TREE_OPERAND (arg0, 1);
5273 unsigned HOST_WIDE_INT lpart;
5274 HOST_WIDE_INT hpart;
5277 /* We have to do this the hard way to detect unsigned overflow.
5278 prod = int_const_binop (MULT_EXPR, arg01, arg1, 0); */
5279 overflow = mul_double (TREE_INT_CST_LOW (arg01),
5280 TREE_INT_CST_HIGH (arg01),
5281 TREE_INT_CST_LOW (arg1),
5282 TREE_INT_CST_HIGH (arg1), &lpart, &hpart);
5283 prod = build_int_2 (lpart, hpart);
5284 TREE_TYPE (prod) = TREE_TYPE (arg00);
5285 TREE_OVERFLOW (prod) = force_fit_type (prod, overflow)
5286 || TREE_INT_CST_HIGH (prod) != hpart
5287 || TREE_INT_CST_LOW (prod) != lpart;
5288 TREE_CONSTANT_OVERFLOW (prod) = TREE_OVERFLOW (prod);
5290 if (TYPE_UNSIGNED (TREE_TYPE (arg0)))
5292 tmp = int_const_binop (MINUS_EXPR, arg01, integer_one_node, 0);
5295 /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp, 0). */
5296 overflow = add_double (TREE_INT_CST_LOW (prod),
5297 TREE_INT_CST_HIGH (prod),
5298 TREE_INT_CST_LOW (tmp),
5299 TREE_INT_CST_HIGH (tmp),
5301 hi = build_int_2 (lpart, hpart);
5302 TREE_TYPE (hi) = TREE_TYPE (arg00);
5303 TREE_OVERFLOW (hi) = force_fit_type (hi, overflow)
5304 || TREE_INT_CST_HIGH (hi) != hpart
5305 || TREE_INT_CST_LOW (hi) != lpart
5306 || TREE_OVERFLOW (prod);
5307 TREE_CONSTANT_OVERFLOW (hi) = TREE_OVERFLOW (hi);
5309 else if (tree_int_cst_sgn (arg01) >= 0)
5311 tmp = int_const_binop (MINUS_EXPR, arg01, integer_one_node, 0);
5312 switch (tree_int_cst_sgn (arg1))
5315 lo = int_const_binop (MINUS_EXPR, prod, tmp, 0);
5320 lo = fold_negate_const (tmp, TREE_TYPE (arg0));
5325 hi = int_const_binop (PLUS_EXPR, prod, tmp, 0);
5335 tmp = int_const_binop (PLUS_EXPR, arg01, integer_one_node, 0);
5336 switch (tree_int_cst_sgn (arg1))
5339 hi = int_const_binop (MINUS_EXPR, prod, tmp, 0);
5344 hi = fold_negate_const (tmp, TREE_TYPE (arg0));
5349 lo = int_const_binop (PLUS_EXPR, prod, tmp, 0);
5361 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
5362 return omit_one_operand (type, integer_zero_node, arg00);
5363 if (TREE_OVERFLOW (hi))
5364 return fold (build2 (GE_EXPR, type, arg00, lo));
5365 if (TREE_OVERFLOW (lo))
5366 return fold (build2 (LE_EXPR, type, arg00, hi));
5367 return build_range_check (type, arg00, 1, lo, hi);
5370 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
5371 return omit_one_operand (type, integer_one_node, arg00);
5372 if (TREE_OVERFLOW (hi))
5373 return fold (build2 (LT_EXPR, type, arg00, lo));
5374 if (TREE_OVERFLOW (lo))
5375 return fold (build2 (GT_EXPR, type, arg00, hi));
5376 return build_range_check (type, arg00, 0, lo, hi);
5379 if (TREE_OVERFLOW (lo))
5380 return omit_one_operand (type, integer_zero_node, arg00);
5381 return fold (build2 (LT_EXPR, type, arg00, lo));
5384 if (TREE_OVERFLOW (hi))
5385 return omit_one_operand (type, integer_one_node, arg00);
5386 return fold (build2 (LE_EXPR, type, arg00, hi));
5389 if (TREE_OVERFLOW (hi))
5390 return omit_one_operand (type, integer_zero_node, arg00);
5391 return fold (build2 (GT_EXPR, type, arg00, hi));
5394 if (TREE_OVERFLOW (lo))
5395 return omit_one_operand (type, integer_one_node, arg00);
5396 return fold (build2 (GE_EXPR, type, arg00, lo));
5406 /* If CODE with arguments ARG0 and ARG1 represents a single bit
5407 equality/inequality test, then return a simplified form of
5408 the test using shifts and logical operations. Otherwise return
5409 NULL. TYPE is the desired result type. */
5412 fold_single_bit_test (enum tree_code code, tree arg0, tree arg1,
5415 /* If this is a TRUTH_NOT_EXPR, it may have a single bit test inside
5417 if (code == TRUTH_NOT_EXPR)
5419 code = TREE_CODE (arg0);
5420 if (code != NE_EXPR && code != EQ_EXPR)
5423 /* Extract the arguments of the EQ/NE. */
5424 arg1 = TREE_OPERAND (arg0, 1);
5425 arg0 = TREE_OPERAND (arg0, 0);
5427 /* This requires us to invert the code. */
5428 code = (code == EQ_EXPR ? NE_EXPR : EQ_EXPR);
5431 /* If this is testing a single bit, we can optimize the test. */
5432 if ((code == NE_EXPR || code == EQ_EXPR)
5433 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
5434 && integer_pow2p (TREE_OPERAND (arg0, 1)))
5436 tree inner = TREE_OPERAND (arg0, 0);
5437 tree type = TREE_TYPE (arg0);
5438 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
5439 enum machine_mode operand_mode = TYPE_MODE (type);
5441 tree signed_type, unsigned_type, intermediate_type;
5444 /* If we have (A & C) != 0 where C is the sign bit of A, convert
5445 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
5446 arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
5447 if (arg00 != NULL_TREE
5448 /* This is only a win if casting to a signed type is cheap,
5449 i.e. when arg00's type is not a partial mode. */
5450 && TYPE_PRECISION (TREE_TYPE (arg00))
5451 == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg00))))
5453 tree stype = lang_hooks.types.signed_type (TREE_TYPE (arg00));
5454 return fold (build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
5455 result_type, fold_convert (stype, arg00),
5456 fold_convert (stype, integer_zero_node)));
5459 /* Otherwise we have (A & C) != 0 where C is a single bit,
5460 convert that into ((A >> C2) & 1). Where C2 = log2(C).
5461 Similarly for (A & C) == 0. */
5463 /* If INNER is a right shift of a constant and it plus BITNUM does
5464 not overflow, adjust BITNUM and INNER. */
5465 if (TREE_CODE (inner) == RSHIFT_EXPR
5466 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
5467 && TREE_INT_CST_HIGH (TREE_OPERAND (inner, 1)) == 0
5468 && bitnum < TYPE_PRECISION (type)
5469 && 0 > compare_tree_int (TREE_OPERAND (inner, 1),
5470 bitnum - TYPE_PRECISION (type)))
5472 bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1));
5473 inner = TREE_OPERAND (inner, 0);
5476 /* If we are going to be able to omit the AND below, we must do our
5477 operations as unsigned. If we must use the AND, we have a choice.
5478 Normally unsigned is faster, but for some machines signed is. */
5479 #ifdef LOAD_EXTEND_OP
5480 ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND ? 0 : 1);
5485 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
5486 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
5487 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
5488 inner = fold_convert (intermediate_type, inner);
5491 inner = build2 (RSHIFT_EXPR, intermediate_type,
5492 inner, size_int (bitnum));
5494 if (code == EQ_EXPR)
5495 inner = build2 (BIT_XOR_EXPR, intermediate_type,
5496 inner, integer_one_node);
5498 /* Put the AND last so it can combine with more things. */
5499 inner = build2 (BIT_AND_EXPR, intermediate_type,
5500 inner, integer_one_node);
5502 /* Make sure to return the proper type. */
5503 inner = fold_convert (result_type, inner);
5510 /* Check whether we are allowed to reorder operands arg0 and arg1,
5511 such that the evaluation of arg1 occurs before arg0. */
5514 reorder_operands_p (tree arg0, tree arg1)
5516 if (! flag_evaluation_order)
5518 if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
5520 return ! TREE_SIDE_EFFECTS (arg0)
5521 && ! TREE_SIDE_EFFECTS (arg1);
5524 /* Test whether it is preferable two swap two operands, ARG0 and
5525 ARG1, for example because ARG0 is an integer constant and ARG1
5526 isn't. If REORDER is true, only recommend swapping if we can
5527 evaluate the operands in reverse order. */
5530 tree_swap_operands_p (tree arg0, tree arg1, bool reorder)
5532 STRIP_SIGN_NOPS (arg0);
5533 STRIP_SIGN_NOPS (arg1);
5535 if (TREE_CODE (arg1) == INTEGER_CST)
5537 if (TREE_CODE (arg0) == INTEGER_CST)
5540 if (TREE_CODE (arg1) == REAL_CST)
5542 if (TREE_CODE (arg0) == REAL_CST)
5545 if (TREE_CODE (arg1) == COMPLEX_CST)
5547 if (TREE_CODE (arg0) == COMPLEX_CST)
5550 if (TREE_CONSTANT (arg1))
5552 if (TREE_CONSTANT (arg0))
5558 if (reorder && flag_evaluation_order
5559 && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
5567 if (reorder && flag_evaluation_order
5568 && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
5576 /* It is preferable to swap two SSA_NAME to ensure a canonical form
5577 for commutative and comparison operators. Ensuring a canonical
5578 form allows the optimizers to find additional redundancies without
5579 having to explicitly check for both orderings. */
5580 if (TREE_CODE (arg0) == SSA_NAME
5581 && TREE_CODE (arg1) == SSA_NAME
5582 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
5588 /* Perform constant folding and related simplification of EXPR.
5589 The related simplifications include x*1 => x, x*0 => 0, etc.,
5590 and application of the associative law.
5591 NOP_EXPR conversions may be removed freely (as long as we
5592 are careful not to change the type of the overall expression).
5593 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
5594 but we can constant-fold them if they have constant operands. */
5596 #ifdef ENABLE_FOLD_CHECKING
5597 # define fold(x) fold_1 (x)
5598 static tree fold_1 (tree);
5604 const tree t = expr;
5605 const tree type = TREE_TYPE (expr);
5606 tree t1 = NULL_TREE;
5608 tree arg0 = NULL_TREE, arg1 = NULL_TREE;
5609 enum tree_code code = TREE_CODE (t);
5610 int kind = TREE_CODE_CLASS (code);
5612 /* WINS will be nonzero when the switch is done
5613 if all operands are constant. */
5616 /* Don't try to process an RTL_EXPR since its operands aren't trees.
5617 Likewise for a SAVE_EXPR that's already been evaluated. */
5618 if (code == RTL_EXPR || (code == SAVE_EXPR && SAVE_EXPR_RTL (t) != 0))
5621 /* Return right away if a constant. */
5625 if (code == NOP_EXPR || code == FLOAT_EXPR || code == CONVERT_EXPR)
5629 /* Special case for conversion ops that can have fixed point args. */
5630 arg0 = TREE_OPERAND (t, 0);
5632 /* Don't use STRIP_NOPS, because signedness of argument type matters. */
5634 STRIP_SIGN_NOPS (arg0);
5636 if (arg0 != 0 && TREE_CODE (arg0) == COMPLEX_CST)
5637 subop = TREE_REALPART (arg0);
5641 if (subop != 0 && TREE_CODE (subop) != INTEGER_CST
5642 && TREE_CODE (subop) != REAL_CST)
5643 /* Note that TREE_CONSTANT isn't enough:
5644 static var addresses are constant but we can't
5645 do arithmetic on them. */
5648 else if (IS_EXPR_CODE_CLASS (kind))
5650 int len = first_rtl_op (code);
5652 for (i = 0; i < len; i++)
5654 tree op = TREE_OPERAND (t, i);
5658 continue; /* Valid for CALL_EXPR, at least. */
5660 /* Strip any conversions that don't change the mode. This is
5661 safe for every expression, except for a comparison expression
5662 because its signedness is derived from its operands. So, in
5663 the latter case, only strip conversions that don't change the
5666 Note that this is done as an internal manipulation within the
5667 constant folder, in order to find the simplest representation
5668 of the arguments so that their form can be studied. In any
5669 cases, the appropriate type conversions should be put back in
5670 the tree that will get out of the constant folder. */
5672 STRIP_SIGN_NOPS (op);
5676 if (TREE_CODE (op) == COMPLEX_CST)
5677 subop = TREE_REALPART (op);
5681 if (TREE_CODE (subop) != INTEGER_CST
5682 && TREE_CODE (subop) != REAL_CST)
5683 /* Note that TREE_CONSTANT isn't enough:
5684 static var addresses are constant but we can't
5685 do arithmetic on them. */
5695 /* If this is a commutative operation, and ARG0 is a constant, move it
5696 to ARG1 to reduce the number of tests below. */
5697 if (commutative_tree_code (code)
5698 && tree_swap_operands_p (arg0, arg1, true))
5699 return fold (build2 (code, type, TREE_OPERAND (t, 1),
5700 TREE_OPERAND (t, 0)));
5702 /* Now WINS is set as described above,
5703 ARG0 is the first operand of EXPR,
5704 and ARG1 is the second operand (if it has more than one operand).
5706 First check for cases where an arithmetic operation is applied to a
5707 compound, conditional, or comparison operation. Push the arithmetic
5708 operation inside the compound or conditional to see if any folding
5709 can then be done. Convert comparison to conditional for this purpose.
5710 The also optimizes non-constant cases that used to be done in
5713 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
5714 one of the operands is a comparison and the other is a comparison, a
5715 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
5716 code below would make the expression more complex. Change it to a
5717 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
5718 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
5720 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
5721 || code == EQ_EXPR || code == NE_EXPR)
5722 && ((truth_value_p (TREE_CODE (arg0))
5723 && (truth_value_p (TREE_CODE (arg1))
5724 || (TREE_CODE (arg1) == BIT_AND_EXPR
5725 && integer_onep (TREE_OPERAND (arg1, 1)))))
5726 || (truth_value_p (TREE_CODE (arg1))
5727 && (truth_value_p (TREE_CODE (arg0))
5728 || (TREE_CODE (arg0) == BIT_AND_EXPR
5729 && integer_onep (TREE_OPERAND (arg0, 1)))))))
5731 tem = fold (build2 (code == BIT_AND_EXPR ? TRUTH_AND_EXPR
5732 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
5734 type, fold_convert (boolean_type_node, arg0),
5735 fold_convert (boolean_type_node, arg1)));
5737 if (code == EQ_EXPR)
5738 tem = invert_truthvalue (tem);
5743 if (TREE_CODE_CLASS (code) == '1')
5745 if (TREE_CODE (arg0) == COMPOUND_EXPR)
5746 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
5747 fold (build1 (code, type, TREE_OPERAND (arg0, 1))));
5748 else if (TREE_CODE (arg0) == COND_EXPR)
5750 tree arg01 = TREE_OPERAND (arg0, 1);
5751 tree arg02 = TREE_OPERAND (arg0, 2);
5752 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
5753 arg01 = fold (build1 (code, type, arg01));
5754 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
5755 arg02 = fold (build1 (code, type, arg02));
5756 tem = fold (build3 (COND_EXPR, type, TREE_OPERAND (arg0, 0),
5759 /* If this was a conversion, and all we did was to move into
5760 inside the COND_EXPR, bring it back out. But leave it if
5761 it is a conversion from integer to integer and the
5762 result precision is no wider than a word since such a
5763 conversion is cheap and may be optimized away by combine,
5764 while it couldn't if it were outside the COND_EXPR. Then return
5765 so we don't get into an infinite recursion loop taking the
5766 conversion out and then back in. */
5768 if ((code == NOP_EXPR || code == CONVERT_EXPR
5769 || code == NON_LVALUE_EXPR)
5770 && TREE_CODE (tem) == COND_EXPR
5771 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
5772 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
5773 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
5774 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
5775 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
5776 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
5777 && ! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
5779 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
5780 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD))
5781 tem = build1 (code, type,
5783 TREE_TYPE (TREE_OPERAND
5784 (TREE_OPERAND (tem, 1), 0)),
5785 TREE_OPERAND (tem, 0),
5786 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
5787 TREE_OPERAND (TREE_OPERAND (tem, 2), 0)));
5790 else if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<')
5792 if (TREE_CODE (type) == BOOLEAN_TYPE)
5794 arg0 = copy_node (arg0);
5795 TREE_TYPE (arg0) = type;
5798 else if (TREE_CODE (type) != INTEGER_TYPE)
5799 return fold (build3 (COND_EXPR, type, arg0,
5800 fold (build1 (code, type,
5802 fold (build1 (code, type,
5803 integer_zero_node))));
5806 else if (TREE_CODE_CLASS (code) == '<'
5807 && TREE_CODE (arg0) == COMPOUND_EXPR)
5808 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
5809 fold (build2 (code, type, TREE_OPERAND (arg0, 1), arg1)));
5810 else if (TREE_CODE_CLASS (code) == '<'
5811 && TREE_CODE (arg1) == COMPOUND_EXPR)
5812 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
5813 fold (build2 (code, type, arg0, TREE_OPERAND (arg1, 1))));
5814 else if (TREE_CODE_CLASS (code) == '2'
5815 || TREE_CODE_CLASS (code) == '<')
5817 if (TREE_CODE (arg0) == COMPOUND_EXPR)
5818 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
5819 fold (build2 (code, type, TREE_OPERAND (arg0, 1),
5821 if (TREE_CODE (arg1) == COMPOUND_EXPR
5822 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
5823 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
5824 fold (build2 (code, type,
5825 arg0, TREE_OPERAND (arg1, 1))));
5827 if (TREE_CODE (arg0) == COND_EXPR
5828 || TREE_CODE_CLASS (TREE_CODE (arg0)) == '<')
5830 tem = fold_binary_op_with_conditional_arg (code, type, arg0, arg1,
5831 /*cond_first_p=*/1);
5832 if (tem != NULL_TREE)
5836 if (TREE_CODE (arg1) == COND_EXPR
5837 || TREE_CODE_CLASS (TREE_CODE (arg1)) == '<')
5839 tem = fold_binary_op_with_conditional_arg (code, type, arg1, arg0,
5840 /*cond_first_p=*/0);
5841 if (tem != NULL_TREE)
5849 return fold (DECL_INITIAL (t));
5854 case FIX_TRUNC_EXPR:
5856 case FIX_FLOOR_EXPR:
5857 case FIX_ROUND_EXPR:
5858 if (TREE_TYPE (TREE_OPERAND (t, 0)) == type)
5859 return TREE_OPERAND (t, 0);
5861 /* Handle cases of two conversions in a row. */
5862 if (TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
5863 || TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR)
5865 tree inside_type = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5866 tree inter_type = TREE_TYPE (TREE_OPERAND (t, 0));
5867 int inside_int = INTEGRAL_TYPE_P (inside_type);
5868 int inside_ptr = POINTER_TYPE_P (inside_type);
5869 int inside_float = FLOAT_TYPE_P (inside_type);
5870 unsigned int inside_prec = TYPE_PRECISION (inside_type);
5871 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
5872 int inter_int = INTEGRAL_TYPE_P (inter_type);
5873 int inter_ptr = POINTER_TYPE_P (inter_type);
5874 int inter_float = FLOAT_TYPE_P (inter_type);
5875 unsigned int inter_prec = TYPE_PRECISION (inter_type);
5876 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
5877 int final_int = INTEGRAL_TYPE_P (type);
5878 int final_ptr = POINTER_TYPE_P (type);
5879 int final_float = FLOAT_TYPE_P (type);
5880 unsigned int final_prec = TYPE_PRECISION (type);
5881 int final_unsignedp = TYPE_UNSIGNED (type);
5883 /* In addition to the cases of two conversions in a row
5884 handled below, if we are converting something to its own
5885 type via an object of identical or wider precision, neither
5886 conversion is needed. */
5887 if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
5888 && ((inter_int && final_int) || (inter_float && final_float))
5889 && inter_prec >= final_prec)
5890 return fold (build1 (code, type,
5891 TREE_OPERAND (TREE_OPERAND (t, 0), 0)));
5893 /* Likewise, if the intermediate and final types are either both
5894 float or both integer, we don't need the middle conversion if
5895 it is wider than the final type and doesn't change the signedness
5896 (for integers). Avoid this if the final type is a pointer
5897 since then we sometimes need the inner conversion. Likewise if
5898 the outer has a precision not equal to the size of its mode. */
5899 if ((((inter_int || inter_ptr) && (inside_int || inside_ptr))
5900 || (inter_float && inside_float))
5901 && inter_prec >= inside_prec
5902 && (inter_float || inter_unsignedp == inside_unsignedp)
5903 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
5904 && TYPE_MODE (type) == TYPE_MODE (inter_type))
5906 return fold (build1 (code, type,
5907 TREE_OPERAND (TREE_OPERAND (t, 0), 0)));
5909 /* If we have a sign-extension of a zero-extended value, we can
5910 replace that by a single zero-extension. */
5911 if (inside_int && inter_int && final_int
5912 && inside_prec < inter_prec && inter_prec < final_prec
5913 && inside_unsignedp && !inter_unsignedp)
5914 return fold (build1 (code, type,
5915 TREE_OPERAND (TREE_OPERAND (t, 0), 0)));
5917 /* Two conversions in a row are not needed unless:
5918 - some conversion is floating-point (overstrict for now), or
5919 - the intermediate type is narrower than both initial and
5921 - the intermediate type and innermost type differ in signedness,
5922 and the outermost type is wider than the intermediate, or
5923 - the initial type is a pointer type and the precisions of the
5924 intermediate and final types differ, or
5925 - the final type is a pointer type and the precisions of the
5926 initial and intermediate types differ. */
5927 if (! inside_float && ! inter_float && ! final_float
5928 && (inter_prec > inside_prec || inter_prec > final_prec)
5929 && ! (inside_int && inter_int
5930 && inter_unsignedp != inside_unsignedp
5931 && inter_prec < final_prec)
5932 && ((inter_unsignedp && inter_prec > inside_prec)
5933 == (final_unsignedp && final_prec > inter_prec))
5934 && ! (inside_ptr && inter_prec != final_prec)
5935 && ! (final_ptr && inside_prec != inter_prec)
5936 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
5937 && TYPE_MODE (type) == TYPE_MODE (inter_type))
5939 return fold (build1 (code, type,
5940 TREE_OPERAND (TREE_OPERAND (t, 0), 0)));
5943 if (TREE_CODE (TREE_OPERAND (t, 0)) == MODIFY_EXPR
5944 && TREE_CONSTANT (TREE_OPERAND (TREE_OPERAND (t, 0), 1))
5945 /* Detect assigning a bitfield. */
5946 && !(TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) == COMPONENT_REF
5947 && DECL_BIT_FIELD (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 1))))
5949 /* Don't leave an assignment inside a conversion
5950 unless assigning a bitfield. */
5951 tree prev = TREE_OPERAND (t, 0);
5952 tem = copy_node (t);
5953 TREE_OPERAND (tem, 0) = TREE_OPERAND (prev, 1);
5954 /* First do the assignment, then return converted constant. */
5955 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem), prev, fold (tem));
5956 TREE_NO_WARNING (tem) = 1;
5957 TREE_USED (tem) = 1;
5961 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
5962 constants (if x has signed type, the sign bit cannot be set
5963 in c). This folds extension into the BIT_AND_EXPR. */
5964 if (INTEGRAL_TYPE_P (type)
5965 && TREE_CODE (type) != BOOLEAN_TYPE
5966 && TREE_CODE (TREE_OPERAND (t, 0)) == BIT_AND_EXPR
5967 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 1)) == INTEGER_CST)
5969 tree and = TREE_OPERAND (t, 0);
5970 tree and0 = TREE_OPERAND (and, 0), and1 = TREE_OPERAND (and, 1);
5973 if (TYPE_UNSIGNED (TREE_TYPE (and))
5974 || (TYPE_PRECISION (type)
5975 <= TYPE_PRECISION (TREE_TYPE (and))))
5977 else if (TYPE_PRECISION (TREE_TYPE (and1))
5978 <= HOST_BITS_PER_WIDE_INT
5979 && host_integerp (and1, 1))
5981 unsigned HOST_WIDE_INT cst;
5983 cst = tree_low_cst (and1, 1);
5984 cst &= (HOST_WIDE_INT) -1
5985 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
5986 change = (cst == 0);
5987 #ifdef LOAD_EXTEND_OP
5989 && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
5992 tree uns = lang_hooks.types.unsigned_type (TREE_TYPE (and0));
5993 and0 = fold_convert (uns, and0);
5994 and1 = fold_convert (uns, and1);
5999 return fold (build2 (BIT_AND_EXPR, type,
6000 fold_convert (type, and0),
6001 fold_convert (type, and1)));
6004 /* Convert (T1)((T2)X op Y) into (T1)X op Y, for pointer types T1 and
6005 T2 being pointers to types of the same size. */
6006 if (POINTER_TYPE_P (TREE_TYPE (t))
6007 && TREE_CODE_CLASS (TREE_CODE (arg0)) == '2'
6008 && TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
6009 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 0))))
6011 tree arg00 = TREE_OPERAND (arg0, 0);
6012 tree t0 = TREE_TYPE (t);
6013 tree t1 = TREE_TYPE (arg00);
6014 tree tt0 = TREE_TYPE (t0);
6015 tree tt1 = TREE_TYPE (t1);
6016 tree s0 = TYPE_SIZE (tt0);
6017 tree s1 = TYPE_SIZE (tt1);
6019 if (s0 && s1 && operand_equal_p (s0, s1, OEP_ONLY_CONST))
6020 return build2 (TREE_CODE (arg0), t0, fold_convert (t0, arg00),
6021 TREE_OPERAND (arg0, 1));
6024 tem = fold_convert_const (code, type, arg0);
6025 return tem ? tem : t;
6027 case VIEW_CONVERT_EXPR:
6028 if (TREE_CODE (TREE_OPERAND (t, 0)) == VIEW_CONVERT_EXPR)
6029 return build1 (VIEW_CONVERT_EXPR, type,
6030 TREE_OPERAND (TREE_OPERAND (t, 0), 0));
6034 if (TREE_CODE (arg0) == CONSTRUCTOR
6035 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
6037 tree m = purpose_member (arg1, CONSTRUCTOR_ELTS (arg0));
6039 return TREE_VALUE (m);
6044 if (TREE_CONSTANT (t) != wins)
6046 tem = copy_node (t);
6047 TREE_CONSTANT (tem) = wins;
6048 TREE_INVARIANT (tem) = wins;
6054 if (negate_expr_p (arg0))
6055 return fold_convert (type, negate_expr (arg0));
6059 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
6060 return fold_abs_const (arg0, type);
6061 else if (TREE_CODE (arg0) == NEGATE_EXPR)
6062 return fold (build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0)));
6063 /* Convert fabs((double)float) into (double)fabsf(float). */
6064 else if (TREE_CODE (arg0) == NOP_EXPR
6065 && TREE_CODE (type) == REAL_TYPE)
6067 tree targ0 = strip_float_extensions (arg0);
6069 return fold_convert (type, fold (build1 (ABS_EXPR,
6073 else if (tree_expr_nonnegative_p (arg0))
6078 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
6079 return fold_convert (type, arg0);
6080 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
6081 return build2 (COMPLEX_EXPR, type,
6082 TREE_OPERAND (arg0, 0),
6083 negate_expr (TREE_OPERAND (arg0, 1)));
6084 else if (TREE_CODE (arg0) == COMPLEX_CST)
6085 return build_complex (type, TREE_REALPART (arg0),
6086 negate_expr (TREE_IMAGPART (arg0)));
6087 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
6088 return fold (build2 (TREE_CODE (arg0), type,
6089 fold (build1 (CONJ_EXPR, type,
6090 TREE_OPERAND (arg0, 0))),
6091 fold (build1 (CONJ_EXPR, type,
6092 TREE_OPERAND (arg0, 1)))));
6093 else if (TREE_CODE (arg0) == CONJ_EXPR)
6094 return TREE_OPERAND (arg0, 0);
6098 if (TREE_CODE (arg0) == INTEGER_CST)
6099 return fold_not_const (arg0, type);
6100 else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
6101 return TREE_OPERAND (arg0, 0);
6105 /* A + (-B) -> A - B */
6106 if (TREE_CODE (arg1) == NEGATE_EXPR)
6107 return fold (build2 (MINUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
6108 /* (-A) + B -> B - A */
6109 if (TREE_CODE (arg0) == NEGATE_EXPR
6110 && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
6111 return fold (build2 (MINUS_EXPR, type, arg1, TREE_OPERAND (arg0, 0)));
6112 if (! FLOAT_TYPE_P (type))
6114 if (integer_zerop (arg1))
6115 return non_lvalue (fold_convert (type, arg0));
6117 /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
6118 with a constant, and the two constants have no bits in common,
6119 we should treat this as a BIT_IOR_EXPR since this may produce more
6121 if (TREE_CODE (arg0) == BIT_AND_EXPR
6122 && TREE_CODE (arg1) == BIT_AND_EXPR
6123 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
6124 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
6125 && integer_zerop (const_binop (BIT_AND_EXPR,
6126 TREE_OPERAND (arg0, 1),
6127 TREE_OPERAND (arg1, 1), 0)))
6129 code = BIT_IOR_EXPR;
6133 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
6134 (plus (plus (mult) (mult)) (foo)) so that we can
6135 take advantage of the factoring cases below. */
6136 if ((TREE_CODE (arg0) == PLUS_EXPR
6137 && TREE_CODE (arg1) == MULT_EXPR)
6138 || (TREE_CODE (arg1) == PLUS_EXPR
6139 && TREE_CODE (arg0) == MULT_EXPR))
6141 tree parg0, parg1, parg, marg;
6143 if (TREE_CODE (arg0) == PLUS_EXPR)
6144 parg = arg0, marg = arg1;
6146 parg = arg1, marg = arg0;
6147 parg0 = TREE_OPERAND (parg, 0);
6148 parg1 = TREE_OPERAND (parg, 1);
6152 if (TREE_CODE (parg0) == MULT_EXPR
6153 && TREE_CODE (parg1) != MULT_EXPR)
6154 return fold (build2 (PLUS_EXPR, type,
6155 fold (build2 (PLUS_EXPR, type,
6156 fold_convert (type, parg0),
6157 fold_convert (type, marg))),
6158 fold_convert (type, parg1)));
6159 if (TREE_CODE (parg0) != MULT_EXPR
6160 && TREE_CODE (parg1) == MULT_EXPR)
6161 return fold (build2 (PLUS_EXPR, type,
6162 fold (build2 (PLUS_EXPR, type,
6163 fold_convert (type, parg1),
6164 fold_convert (type, marg))),
6165 fold_convert (type, parg0)));
6168 if (TREE_CODE (arg0) == MULT_EXPR && TREE_CODE (arg1) == MULT_EXPR)
6170 tree arg00, arg01, arg10, arg11;
6171 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
6173 /* (A * C) + (B * C) -> (A+B) * C.
6174 We are most concerned about the case where C is a constant,
6175 but other combinations show up during loop reduction. Since
6176 it is not difficult, try all four possibilities. */
6178 arg00 = TREE_OPERAND (arg0, 0);
6179 arg01 = TREE_OPERAND (arg0, 1);
6180 arg10 = TREE_OPERAND (arg1, 0);
6181 arg11 = TREE_OPERAND (arg1, 1);
6184 if (operand_equal_p (arg01, arg11, 0))
6185 same = arg01, alt0 = arg00, alt1 = arg10;
6186 else if (operand_equal_p (arg00, arg10, 0))
6187 same = arg00, alt0 = arg01, alt1 = arg11;
6188 else if (operand_equal_p (arg00, arg11, 0))
6189 same = arg00, alt0 = arg01, alt1 = arg10;
6190 else if (operand_equal_p (arg01, arg10, 0))
6191 same = arg01, alt0 = arg00, alt1 = arg11;
6193 /* No identical multiplicands; see if we can find a common
6194 power-of-two factor in non-power-of-two multiplies. This
6195 can help in multi-dimensional array access. */
6196 else if (TREE_CODE (arg01) == INTEGER_CST
6197 && TREE_CODE (arg11) == INTEGER_CST
6198 && TREE_INT_CST_HIGH (arg01) == 0
6199 && TREE_INT_CST_HIGH (arg11) == 0)
6201 HOST_WIDE_INT int01, int11, tmp;
6202 int01 = TREE_INT_CST_LOW (arg01);
6203 int11 = TREE_INT_CST_LOW (arg11);
6205 /* Move min of absolute values to int11. */
6206 if ((int01 >= 0 ? int01 : -int01)
6207 < (int11 >= 0 ? int11 : -int11))
6209 tmp = int01, int01 = int11, int11 = tmp;
6210 alt0 = arg00, arg00 = arg10, arg10 = alt0;
6211 alt0 = arg01, arg01 = arg11, arg11 = alt0;
6214 if (exact_log2 (int11) > 0 && int01 % int11 == 0)
6216 alt0 = fold (build2 (MULT_EXPR, type, arg00,
6217 build_int_2 (int01 / int11, 0)));
6224 return fold (build2 (MULT_EXPR, type,
6225 fold (build2 (PLUS_EXPR, type,
6232 /* See if ARG1 is zero and X + ARG1 reduces to X. */
6233 if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
6234 return non_lvalue (fold_convert (type, arg0));
6236 /* Likewise if the operands are reversed. */
6237 if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
6238 return non_lvalue (fold_convert (type, arg1));
6240 /* Convert x+x into x*2.0. */
6241 if (operand_equal_p (arg0, arg1, 0)
6242 && SCALAR_FLOAT_TYPE_P (type))
6243 return fold (build2 (MULT_EXPR, type, arg0,
6244 build_real (type, dconst2)));
6246 /* Convert x*c+x into x*(c+1). */
6247 if (flag_unsafe_math_optimizations
6248 && TREE_CODE (arg0) == MULT_EXPR
6249 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
6250 && ! TREE_CONSTANT_OVERFLOW (TREE_OPERAND (arg0, 1))
6251 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
6255 c = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
6256 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
6257 return fold (build2 (MULT_EXPR, type, arg1,
6258 build_real (type, c)));
6261 /* Convert x+x*c into x*(c+1). */
6262 if (flag_unsafe_math_optimizations
6263 && TREE_CODE (arg1) == MULT_EXPR
6264 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST
6265 && ! TREE_CONSTANT_OVERFLOW (TREE_OPERAND (arg1, 1))
6266 && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
6270 c = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
6271 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
6272 return fold (build2 (MULT_EXPR, type, arg0,
6273 build_real (type, c)));
6276 /* Convert x*c1+x*c2 into x*(c1+c2). */
6277 if (flag_unsafe_math_optimizations
6278 && TREE_CODE (arg0) == MULT_EXPR
6279 && TREE_CODE (arg1) == MULT_EXPR
6280 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
6281 && ! TREE_CONSTANT_OVERFLOW (TREE_OPERAND (arg0, 1))
6282 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST
6283 && ! TREE_CONSTANT_OVERFLOW (TREE_OPERAND (arg1, 1))
6284 && operand_equal_p (TREE_OPERAND (arg0, 0),
6285 TREE_OPERAND (arg1, 0), 0))
6287 REAL_VALUE_TYPE c1, c2;
6289 c1 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
6290 c2 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
6291 real_arithmetic (&c1, PLUS_EXPR, &c1, &c2);
6292 return fold (build2 (MULT_EXPR, type,
6293 TREE_OPERAND (arg0, 0),
6294 build_real (type, c1)));
6296 /* Convert a + (b*c + d*e) into (a + b*c) + d*e */
6297 if (flag_unsafe_math_optimizations
6298 && TREE_CODE (arg1) == PLUS_EXPR
6299 && TREE_CODE (arg0) != MULT_EXPR)
6301 tree tree10 = TREE_OPERAND (arg1, 0);
6302 tree tree11 = TREE_OPERAND (arg1, 1);
6303 if (TREE_CODE (tree11) == MULT_EXPR
6304 && TREE_CODE (tree10) == MULT_EXPR)
6307 tree0 = fold (build2 (PLUS_EXPR, type, arg0, tree10));
6308 return fold (build2 (PLUS_EXPR, type, tree0, tree11));
6311 /* Convert (b*c + d*e) + a into b*c + (d*e +a) */
6312 if (flag_unsafe_math_optimizations
6313 && TREE_CODE (arg0) == PLUS_EXPR
6314 && TREE_CODE (arg1) != MULT_EXPR)
6316 tree tree00 = TREE_OPERAND (arg0, 0);
6317 tree tree01 = TREE_OPERAND (arg0, 1);
6318 if (TREE_CODE (tree01) == MULT_EXPR
6319 && TREE_CODE (tree00) == MULT_EXPR)
6322 tree0 = fold (build2 (PLUS_EXPR, type, tree01, arg1));
6323 return fold (build2 (PLUS_EXPR, type, tree00, tree0));
6329 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
6330 is a rotate of A by C1 bits. */
6331 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
6332 is a rotate of A by B bits. */
6334 enum tree_code code0, code1;
6335 code0 = TREE_CODE (arg0);
6336 code1 = TREE_CODE (arg1);
6337 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
6338 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
6339 && operand_equal_p (TREE_OPERAND (arg0, 0),
6340 TREE_OPERAND (arg1, 0), 0)
6341 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
6343 tree tree01, tree11;
6344 enum tree_code code01, code11;
6346 tree01 = TREE_OPERAND (arg0, 1);
6347 tree11 = TREE_OPERAND (arg1, 1);
6348 STRIP_NOPS (tree01);
6349 STRIP_NOPS (tree11);
6350 code01 = TREE_CODE (tree01);
6351 code11 = TREE_CODE (tree11);
6352 if (code01 == INTEGER_CST
6353 && code11 == INTEGER_CST
6354 && TREE_INT_CST_HIGH (tree01) == 0
6355 && TREE_INT_CST_HIGH (tree11) == 0
6356 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
6357 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
6358 return build2 (LROTATE_EXPR, type, TREE_OPERAND (arg0, 0),
6359 code0 == LSHIFT_EXPR ? tree01 : tree11);
6360 else if (code11 == MINUS_EXPR)
6362 tree tree110, tree111;
6363 tree110 = TREE_OPERAND (tree11, 0);
6364 tree111 = TREE_OPERAND (tree11, 1);
6365 STRIP_NOPS (tree110);
6366 STRIP_NOPS (tree111);
6367 if (TREE_CODE (tree110) == INTEGER_CST
6368 && 0 == compare_tree_int (tree110,
6370 (TREE_TYPE (TREE_OPERAND
6372 && operand_equal_p (tree01, tree111, 0))
6373 return build2 ((code0 == LSHIFT_EXPR
6376 type, TREE_OPERAND (arg0, 0), tree01);
6378 else if (code01 == MINUS_EXPR)
6380 tree tree010, tree011;
6381 tree010 = TREE_OPERAND (tree01, 0);
6382 tree011 = TREE_OPERAND (tree01, 1);
6383 STRIP_NOPS (tree010);
6384 STRIP_NOPS (tree011);
6385 if (TREE_CODE (tree010) == INTEGER_CST
6386 && 0 == compare_tree_int (tree010,
6388 (TREE_TYPE (TREE_OPERAND
6390 && operand_equal_p (tree11, tree011, 0))
6391 return build2 ((code0 != LSHIFT_EXPR
6394 type, TREE_OPERAND (arg0, 0), tree11);
6400 /* In most languages, can't associate operations on floats through
6401 parentheses. Rather than remember where the parentheses were, we
6402 don't associate floats at all, unless the user has specified
6403 -funsafe-math-optimizations. */
6406 && (! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations))
6408 tree var0, con0, lit0, minus_lit0;
6409 tree var1, con1, lit1, minus_lit1;
6411 /* Split both trees into variables, constants, and literals. Then
6412 associate each group together, the constants with literals,
6413 then the result with variables. This increases the chances of
6414 literals being recombined later and of generating relocatable
6415 expressions for the sum of a constant and literal. */
6416 var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
6417 var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
6418 code == MINUS_EXPR);
6420 /* Only do something if we found more than two objects. Otherwise,
6421 nothing has changed and we risk infinite recursion. */
6422 if (2 < ((var0 != 0) + (var1 != 0)
6423 + (con0 != 0) + (con1 != 0)
6424 + (lit0 != 0) + (lit1 != 0)
6425 + (minus_lit0 != 0) + (minus_lit1 != 0)))
6427 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
6428 if (code == MINUS_EXPR)
6431 var0 = associate_trees (var0, var1, code, type);
6432 con0 = associate_trees (con0, con1, code, type);
6433 lit0 = associate_trees (lit0, lit1, code, type);
6434 minus_lit0 = associate_trees (minus_lit0, minus_lit1, code, type);
6436 /* Preserve the MINUS_EXPR if the negative part of the literal is
6437 greater than the positive part. Otherwise, the multiplicative
6438 folding code (i.e extract_muldiv) may be fooled in case
6439 unsigned constants are subtracted, like in the following
6440 example: ((X*2 + 4) - 8U)/2. */
6441 if (minus_lit0 && lit0)
6443 if (TREE_CODE (lit0) == INTEGER_CST
6444 && TREE_CODE (minus_lit0) == INTEGER_CST
6445 && tree_int_cst_lt (lit0, minus_lit0))
6447 minus_lit0 = associate_trees (minus_lit0, lit0,
6453 lit0 = associate_trees (lit0, minus_lit0,
6461 return fold_convert (type,
6462 associate_trees (var0, minus_lit0,
6466 con0 = associate_trees (con0, minus_lit0,
6468 return fold_convert (type,
6469 associate_trees (var0, con0,
6474 con0 = associate_trees (con0, lit0, code, type);
6475 return fold_convert (type, associate_trees (var0, con0,
6482 t1 = const_binop (code, arg0, arg1, 0);
6483 if (t1 != NULL_TREE)
6485 /* The return value should always have
6486 the same type as the original expression. */
6487 if (TREE_TYPE (t1) != type)
6488 t1 = fold_convert (type, t1);
6495 /* A - (-B) -> A + B */
6496 if (TREE_CODE (arg1) == NEGATE_EXPR)
6497 return fold (build2 (PLUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
6498 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
6499 if (TREE_CODE (arg0) == NEGATE_EXPR
6500 && (FLOAT_TYPE_P (type)
6501 || (INTEGRAL_TYPE_P (type) && flag_wrapv && !flag_trapv))
6502 && negate_expr_p (arg1)
6503 && reorder_operands_p (arg0, arg1))
6504 return fold (build2 (MINUS_EXPR, type, negate_expr (arg1),
6505 TREE_OPERAND (arg0, 0)));
6507 if (! FLOAT_TYPE_P (type))
6509 if (! wins && integer_zerop (arg0))
6510 return negate_expr (fold_convert (type, arg1));
6511 if (integer_zerop (arg1))
6512 return non_lvalue (fold_convert (type, arg0));
6514 /* Fold A - (A & B) into ~B & A. */
6515 if (!TREE_SIDE_EFFECTS (arg0)
6516 && TREE_CODE (arg1) == BIT_AND_EXPR)
6518 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
6519 return fold (build2 (BIT_AND_EXPR, type,
6520 fold (build1 (BIT_NOT_EXPR, type,
6521 TREE_OPERAND (arg1, 0))),
6523 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
6524 return fold (build2 (BIT_AND_EXPR, type,
6525 fold (build1 (BIT_NOT_EXPR, type,
6526 TREE_OPERAND (arg1, 1))),
6530 /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
6531 any power of 2 minus 1. */
6532 if (TREE_CODE (arg0) == BIT_AND_EXPR
6533 && TREE_CODE (arg1) == BIT_AND_EXPR
6534 && operand_equal_p (TREE_OPERAND (arg0, 0),
6535 TREE_OPERAND (arg1, 0), 0))
6537 tree mask0 = TREE_OPERAND (arg0, 1);
6538 tree mask1 = TREE_OPERAND (arg1, 1);
6539 tree tem = fold (build1 (BIT_NOT_EXPR, type, mask0));
6541 if (operand_equal_p (tem, mask1, 0))
6543 tem = fold (build2 (BIT_XOR_EXPR, type,
6544 TREE_OPERAND (arg0, 0), mask1));
6545 return fold (build2 (MINUS_EXPR, type, tem, mask1));
6550 /* See if ARG1 is zero and X - ARG1 reduces to X. */
6551 else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
6552 return non_lvalue (fold_convert (type, arg0));
6554 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
6555 ARG0 is zero and X + ARG0 reduces to X, since that would mean
6556 (-ARG1 + ARG0) reduces to -ARG1. */
6557 else if (!wins && fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
6558 return negate_expr (fold_convert (type, arg1));
6560 /* Fold &x - &x. This can happen from &x.foo - &x.
6561 This is unsafe for certain floats even in non-IEEE formats.
6562 In IEEE, it is unsafe because it does wrong for NaNs.
6563 Also note that operand_equal_p is always false if an operand
6566 if ((! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
6567 && operand_equal_p (arg0, arg1, 0))
6568 return fold_convert (type, integer_zero_node);
6570 /* A - B -> A + (-B) if B is easily negatable. */
6571 if (!wins && negate_expr_p (arg1)
6572 && (FLOAT_TYPE_P (type)
6573 || (INTEGRAL_TYPE_P (type) && flag_wrapv && !flag_trapv)))
6574 return fold (build2 (PLUS_EXPR, type, arg0, negate_expr (arg1)));
6576 if (TREE_CODE (arg0) == MULT_EXPR
6577 && TREE_CODE (arg1) == MULT_EXPR
6578 && (INTEGRAL_TYPE_P (type) || flag_unsafe_math_optimizations))
6580 /* (A * C) - (B * C) -> (A-B) * C. */
6581 if (operand_equal_p (TREE_OPERAND (arg0, 1),
6582 TREE_OPERAND (arg1, 1), 0))
6583 return fold (build2 (MULT_EXPR, type,
6584 fold (build2 (MINUS_EXPR, type,
6585 TREE_OPERAND (arg0, 0),
6586 TREE_OPERAND (arg1, 0))),
6587 TREE_OPERAND (arg0, 1)));
6588 /* (A * C1) - (A * C2) -> A * (C1-C2). */
6589 if (operand_equal_p (TREE_OPERAND (arg0, 0),
6590 TREE_OPERAND (arg1, 0), 0))
6591 return fold (build2 (MULT_EXPR, type,
6592 TREE_OPERAND (arg0, 0),
6593 fold (build2 (MINUS_EXPR, type,
6594 TREE_OPERAND (arg0, 1),
6595 TREE_OPERAND (arg1, 1)))));
6601 /* (-A) * (-B) -> A * B */
6602 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
6603 return fold (build2 (MULT_EXPR, type,
6604 TREE_OPERAND (arg0, 0),
6605 negate_expr (arg1)));
6606 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
6607 return fold (build2 (MULT_EXPR, type,
6609 TREE_OPERAND (arg1, 0)));
6611 if (! FLOAT_TYPE_P (type))
6613 if (integer_zerop (arg1))
6614 return omit_one_operand (type, arg1, arg0);
6615 if (integer_onep (arg1))
6616 return non_lvalue (fold_convert (type, arg0));
6618 /* (a * (1 << b)) is (a << b) */
6619 if (TREE_CODE (arg1) == LSHIFT_EXPR
6620 && integer_onep (TREE_OPERAND (arg1, 0)))
6621 return fold (build2 (LSHIFT_EXPR, type, arg0,
6622 TREE_OPERAND (arg1, 1)));
6623 if (TREE_CODE (arg0) == LSHIFT_EXPR
6624 && integer_onep (TREE_OPERAND (arg0, 0)))
6625 return fold (build2 (LSHIFT_EXPR, type, arg1,
6626 TREE_OPERAND (arg0, 1)));
6628 if (TREE_CODE (arg1) == INTEGER_CST
6629 && 0 != (tem = extract_muldiv (TREE_OPERAND (t, 0),
6630 fold_convert (type, arg1),
6632 return fold_convert (type, tem);
6637 /* Maybe fold x * 0 to 0. The expressions aren't the same
6638 when x is NaN, since x * 0 is also NaN. Nor are they the
6639 same in modes with signed zeros, since multiplying a
6640 negative value by 0 gives -0, not +0. */
6641 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
6642 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
6643 && real_zerop (arg1))
6644 return omit_one_operand (type, arg1, arg0);
6645 /* In IEEE floating point, x*1 is not equivalent to x for snans. */
6646 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
6647 && real_onep (arg1))
6648 return non_lvalue (fold_convert (type, arg0));
6650 /* Transform x * -1.0 into -x. */
6651 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
6652 && real_minus_onep (arg1))
6653 return fold_convert (type, negate_expr (arg0));
6655 /* Convert (C1/X)*C2 into (C1*C2)/X. */
6656 if (flag_unsafe_math_optimizations
6657 && TREE_CODE (arg0) == RDIV_EXPR
6658 && TREE_CODE (arg1) == REAL_CST
6659 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
6661 tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
6664 return fold (build2 (RDIV_EXPR, type, tem,
6665 TREE_OPERAND (arg0, 1)));
6668 if (flag_unsafe_math_optimizations)
6670 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
6671 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
6673 /* Optimizations of root(...)*root(...). */
6674 if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
6676 tree rootfn, arg, arglist;
6677 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
6678 tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
6680 /* Optimize sqrt(x)*sqrt(x) as x. */
6681 if (BUILTIN_SQRT_P (fcode0)
6682 && operand_equal_p (arg00, arg10, 0)
6683 && ! HONOR_SNANS (TYPE_MODE (type)))
6686 /* Optimize root(x)*root(y) as root(x*y). */
6687 rootfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
6688 arg = fold (build2 (MULT_EXPR, type, arg00, arg10));
6689 arglist = build_tree_list (NULL_TREE, arg);
6690 return build_function_call_expr (rootfn, arglist);
6693 /* Optimize expN(x)*expN(y) as expN(x+y). */
6694 if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
6696 tree expfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
6697 tree arg = build2 (PLUS_EXPR, type,
6698 TREE_VALUE (TREE_OPERAND (arg0, 1)),
6699 TREE_VALUE (TREE_OPERAND (arg1, 1)));
6700 tree arglist = build_tree_list (NULL_TREE, fold (arg));
6701 return build_function_call_expr (expfn, arglist);
6704 /* Optimizations of pow(...)*pow(...). */
6705 if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
6706 || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
6707 || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
6709 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
6710 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0,
6712 tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
6713 tree arg11 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg1,
6716 /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y). */
6717 if (operand_equal_p (arg01, arg11, 0))
6719 tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
6720 tree arg = build2 (MULT_EXPR, type, arg00, arg10);
6721 tree arglist = tree_cons (NULL_TREE, fold (arg),
6722 build_tree_list (NULL_TREE,
6724 return build_function_call_expr (powfn, arglist);
6727 /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z). */
6728 if (operand_equal_p (arg00, arg10, 0))
6730 tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
6731 tree arg = fold (build2 (PLUS_EXPR, type, arg01, arg11));
6732 tree arglist = tree_cons (NULL_TREE, arg00,
6733 build_tree_list (NULL_TREE,
6735 return build_function_call_expr (powfn, arglist);
6739 /* Optimize tan(x)*cos(x) as sin(x). */
6740 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
6741 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
6742 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
6743 || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
6744 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
6745 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
6746 && operand_equal_p (TREE_VALUE (TREE_OPERAND (arg0, 1)),
6747 TREE_VALUE (TREE_OPERAND (arg1, 1)), 0))
6749 tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
6751 if (sinfn != NULL_TREE)
6752 return build_function_call_expr (sinfn,
6753 TREE_OPERAND (arg0, 1));
6756 /* Optimize x*pow(x,c) as pow(x,c+1). */
6757 if (fcode1 == BUILT_IN_POW
6758 || fcode1 == BUILT_IN_POWF
6759 || fcode1 == BUILT_IN_POWL)
6761 tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
6762 tree arg11 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg1,
6764 if (TREE_CODE (arg11) == REAL_CST
6765 && ! TREE_CONSTANT_OVERFLOW (arg11)
6766 && operand_equal_p (arg0, arg10, 0))
6768 tree powfn = TREE_OPERAND (TREE_OPERAND (arg1, 0), 0);
6772 c = TREE_REAL_CST (arg11);
6773 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
6774 arg = build_real (type, c);
6775 arglist = build_tree_list (NULL_TREE, arg);
6776 arglist = tree_cons (NULL_TREE, arg0, arglist);
6777 return build_function_call_expr (powfn, arglist);
6781 /* Optimize pow(x,c)*x as pow(x,c+1). */
6782 if (fcode0 == BUILT_IN_POW
6783 || fcode0 == BUILT_IN_POWF
6784 || fcode0 == BUILT_IN_POWL)
6786 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
6787 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0,
6789 if (TREE_CODE (arg01) == REAL_CST
6790 && ! TREE_CONSTANT_OVERFLOW (arg01)
6791 && operand_equal_p (arg1, arg00, 0))
6793 tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
6797 c = TREE_REAL_CST (arg01);
6798 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
6799 arg = build_real (type, c);
6800 arglist = build_tree_list (NULL_TREE, arg);
6801 arglist = tree_cons (NULL_TREE, arg1, arglist);
6802 return build_function_call_expr (powfn, arglist);
6806 /* Optimize x*x as pow(x,2.0), which is expanded as x*x. */
6808 && operand_equal_p (arg0, arg1, 0))
6810 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
6814 tree arg = build_real (type, dconst2);
6815 tree arglist = build_tree_list (NULL_TREE, arg);
6816 arglist = tree_cons (NULL_TREE, arg0, arglist);
6817 return build_function_call_expr (powfn, arglist);
6826 if (integer_all_onesp (arg1))
6827 return omit_one_operand (type, arg1, arg0);
6828 if (integer_zerop (arg1))
6829 return non_lvalue (fold_convert (type, arg0));
6830 if (operand_equal_p (arg0, arg1, 0))
6831 return non_lvalue (fold_convert (type, arg0));
6832 t1 = distribute_bit_expr (code, type, arg0, arg1);
6833 if (t1 != NULL_TREE)
6836 /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
6838 This results in more efficient code for machines without a NAND
6839 instruction. Combine will canonicalize to the first form
6840 which will allow use of NAND instructions provided by the
6841 backend if they exist. */
6842 if (TREE_CODE (arg0) == BIT_NOT_EXPR
6843 && TREE_CODE (arg1) == BIT_NOT_EXPR)
6845 return fold (build1 (BIT_NOT_EXPR, type,
6846 build2 (BIT_AND_EXPR, type,
6847 TREE_OPERAND (arg0, 0),
6848 TREE_OPERAND (arg1, 0))));
6851 /* See if this can be simplified into a rotate first. If that
6852 is unsuccessful continue in the association code. */
6856 if (integer_zerop (arg1))
6857 return non_lvalue (fold_convert (type, arg0));
6858 if (integer_all_onesp (arg1))
6859 return fold (build1 (BIT_NOT_EXPR, type, arg0));
6860 if (operand_equal_p (arg0, arg1, 0))
6861 return omit_one_operand (type, integer_zero_node, arg0);
6863 /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
6864 with a constant, and the two constants have no bits in common,
6865 we should treat this as a BIT_IOR_EXPR since this may produce more
6867 if (TREE_CODE (arg0) == BIT_AND_EXPR
6868 && TREE_CODE (arg1) == BIT_AND_EXPR
6869 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
6870 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
6871 && integer_zerop (const_binop (BIT_AND_EXPR,
6872 TREE_OPERAND (arg0, 1),
6873 TREE_OPERAND (arg1, 1), 0)))
6875 code = BIT_IOR_EXPR;
6879 /* See if this can be simplified into a rotate first. If that
6880 is unsuccessful continue in the association code. */
6884 if (integer_all_onesp (arg1))
6885 return non_lvalue (fold_convert (type, arg0));
6886 if (integer_zerop (arg1))
6887 return omit_one_operand (type, arg1, arg0);
6888 if (operand_equal_p (arg0, arg1, 0))
6889 return non_lvalue (fold_convert (type, arg0));
6890 t1 = distribute_bit_expr (code, type, arg0, arg1);
6891 if (t1 != NULL_TREE)
6893 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
6894 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
6895 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
6898 = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
6900 if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
6901 && (~TREE_INT_CST_LOW (arg1)
6902 & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
6903 return fold_convert (type, TREE_OPERAND (arg0, 0));
6906 /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
6908 This results in more efficient code for machines without a NOR
6909 instruction. Combine will canonicalize to the first form
6910 which will allow use of NOR instructions provided by the
6911 backend if they exist. */
6912 if (TREE_CODE (arg0) == BIT_NOT_EXPR
6913 && TREE_CODE (arg1) == BIT_NOT_EXPR)
6915 return fold (build1 (BIT_NOT_EXPR, type,
6916 build2 (BIT_IOR_EXPR, type,
6917 TREE_OPERAND (arg0, 0),
6918 TREE_OPERAND (arg1, 0))));
6924 /* Don't touch a floating-point divide by zero unless the mode
6925 of the constant can represent infinity. */
6926 if (TREE_CODE (arg1) == REAL_CST
6927 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
6928 && real_zerop (arg1))
6931 /* (-A) / (-B) -> A / B */
6932 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
6933 return fold (build2 (RDIV_EXPR, type,
6934 TREE_OPERAND (arg0, 0),
6935 negate_expr (arg1)));
6936 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
6937 return fold (build2 (RDIV_EXPR, type,
6939 TREE_OPERAND (arg1, 0)));
6941 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
6942 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
6943 && real_onep (arg1))
6944 return non_lvalue (fold_convert (type, arg0));
6946 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
6947 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
6948 && real_minus_onep (arg1))
6949 return non_lvalue (fold_convert (type, negate_expr (arg0)));
6951 /* If ARG1 is a constant, we can convert this to a multiply by the
6952 reciprocal. This does not have the same rounding properties,
6953 so only do this if -funsafe-math-optimizations. We can actually
6954 always safely do it if ARG1 is a power of two, but it's hard to
6955 tell if it is or not in a portable manner. */
6956 if (TREE_CODE (arg1) == REAL_CST)
6958 if (flag_unsafe_math_optimizations
6959 && 0 != (tem = const_binop (code, build_real (type, dconst1),
6961 return fold (build2 (MULT_EXPR, type, arg0, tem));
6962 /* Find the reciprocal if optimizing and the result is exact. */
6966 r = TREE_REAL_CST (arg1);
6967 if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
6969 tem = build_real (type, r);
6970 return fold (build2 (MULT_EXPR, type, arg0, tem));
6974 /* Convert A/B/C to A/(B*C). */
6975 if (flag_unsafe_math_optimizations
6976 && TREE_CODE (arg0) == RDIV_EXPR)
6977 return fold (build2 (RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
6978 fold (build2 (MULT_EXPR, type,
6979 TREE_OPERAND (arg0, 1), arg1))));
6981 /* Convert A/(B/C) to (A/B)*C. */
6982 if (flag_unsafe_math_optimizations
6983 && TREE_CODE (arg1) == RDIV_EXPR)
6984 return fold (build2 (MULT_EXPR, type,
6985 fold (build2 (RDIV_EXPR, type, arg0,
6986 TREE_OPERAND (arg1, 0))),
6987 TREE_OPERAND (arg1, 1)));
6989 /* Convert C1/(X*C2) into (C1/C2)/X. */
6990 if (flag_unsafe_math_optimizations
6991 && TREE_CODE (arg1) == MULT_EXPR
6992 && TREE_CODE (arg0) == REAL_CST
6993 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
6995 tree tem = const_binop (RDIV_EXPR, arg0,
6996 TREE_OPERAND (arg1, 1), 0);
6998 return fold (build2 (RDIV_EXPR, type, tem,
6999 TREE_OPERAND (arg1, 0)));
7002 if (flag_unsafe_math_optimizations)
7004 enum built_in_function fcode = builtin_mathfn_code (arg1);
7005 /* Optimize x/expN(y) into x*expN(-y). */
7006 if (BUILTIN_EXPONENT_P (fcode))
7008 tree expfn = TREE_OPERAND (TREE_OPERAND (arg1, 0), 0);
7009 tree arg = negate_expr (TREE_VALUE (TREE_OPERAND (arg1, 1)));
7010 tree arglist = build_tree_list (NULL_TREE,
7011 fold_convert (type, arg));
7012 arg1 = build_function_call_expr (expfn, arglist);
7013 return fold (build2 (MULT_EXPR, type, arg0, arg1));
7016 /* Optimize x/pow(y,z) into x*pow(y,-z). */
7017 if (fcode == BUILT_IN_POW
7018 || fcode == BUILT_IN_POWF
7019 || fcode == BUILT_IN_POWL)
7021 tree powfn = TREE_OPERAND (TREE_OPERAND (arg1, 0), 0);
7022 tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
7023 tree arg11 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg1, 1)));
7024 tree neg11 = fold_convert (type, negate_expr (arg11));
7025 tree arglist = tree_cons(NULL_TREE, arg10,
7026 build_tree_list (NULL_TREE, neg11));
7027 arg1 = build_function_call_expr (powfn, arglist);
7028 return fold (build2 (MULT_EXPR, type, arg0, arg1));
7032 if (flag_unsafe_math_optimizations)
7034 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
7035 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
7037 /* Optimize sin(x)/cos(x) as tan(x). */
7038 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
7039 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
7040 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
7041 && operand_equal_p (TREE_VALUE (TREE_OPERAND (arg0, 1)),
7042 TREE_VALUE (TREE_OPERAND (arg1, 1)), 0))
7044 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
7046 if (tanfn != NULL_TREE)
7047 return build_function_call_expr (tanfn,
7048 TREE_OPERAND (arg0, 1));
7051 /* Optimize cos(x)/sin(x) as 1.0/tan(x). */
7052 if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
7053 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
7054 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
7055 && operand_equal_p (TREE_VALUE (TREE_OPERAND (arg0, 1)),
7056 TREE_VALUE (TREE_OPERAND (arg1, 1)), 0))
7058 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
7060 if (tanfn != NULL_TREE)
7062 tree tmp = TREE_OPERAND (arg0, 1);
7063 tmp = build_function_call_expr (tanfn, tmp);
7064 return fold (build2 (RDIV_EXPR, type,
7065 build_real (type, dconst1), tmp));
7069 /* Optimize pow(x,c)/x as pow(x,c-1). */
7070 if (fcode0 == BUILT_IN_POW
7071 || fcode0 == BUILT_IN_POWF
7072 || fcode0 == BUILT_IN_POWL)
7074 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
7075 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0, 1)));
7076 if (TREE_CODE (arg01) == REAL_CST
7077 && ! TREE_CONSTANT_OVERFLOW (arg01)
7078 && operand_equal_p (arg1, arg00, 0))
7080 tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
7084 c = TREE_REAL_CST (arg01);
7085 real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
7086 arg = build_real (type, c);
7087 arglist = build_tree_list (NULL_TREE, arg);
7088 arglist = tree_cons (NULL_TREE, arg1, arglist);
7089 return build_function_call_expr (powfn, arglist);
7095 case TRUNC_DIV_EXPR:
7096 case ROUND_DIV_EXPR:
7097 case FLOOR_DIV_EXPR:
7099 case EXACT_DIV_EXPR:
7100 if (integer_onep (arg1))
7101 return non_lvalue (fold_convert (type, arg0));
7102 if (integer_zerop (arg1))
7105 if (!TYPE_UNSIGNED (type)
7106 && TREE_CODE (arg1) == INTEGER_CST
7107 && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
7108 && TREE_INT_CST_HIGH (arg1) == -1)
7109 return fold_convert (type, negate_expr (arg0));
7111 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
7112 operation, EXACT_DIV_EXPR.
7114 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
7115 At one time others generated faster code, it's not clear if they do
7116 after the last round to changes to the DIV code in expmed.c. */
7117 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
7118 && multiple_of_p (type, arg0, arg1))
7119 return fold (build2 (EXACT_DIV_EXPR, type, arg0, arg1));
7121 if (TREE_CODE (arg1) == INTEGER_CST
7122 && 0 != (tem = extract_muldiv (TREE_OPERAND (t, 0), arg1,
7124 return fold_convert (type, tem);
7129 case FLOOR_MOD_EXPR:
7130 case ROUND_MOD_EXPR:
7131 case TRUNC_MOD_EXPR:
7132 if (integer_onep (arg1))
7133 return omit_one_operand (type, integer_zero_node, arg0);
7134 if (integer_zerop (arg1))
7136 /* X % -1 is zero. */
7137 if (!TYPE_UNSIGNED (type)
7138 && TREE_CODE (arg1) == INTEGER_CST
7139 && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
7140 && TREE_INT_CST_HIGH (arg1) == -1)
7141 return omit_one_operand (type, integer_zero_node, arg0);
7143 if (TREE_CODE (arg1) == INTEGER_CST
7144 && 0 != (tem = extract_muldiv (TREE_OPERAND (t, 0), arg1,
7146 return fold_convert (type, tem);
7152 if (integer_all_onesp (arg0))
7153 return omit_one_operand (type, arg0, arg1);
7157 /* Optimize -1 >> x for arithmetic right shifts. */
7158 if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type))
7159 return omit_one_operand (type, arg0, arg1);
7160 /* ... fall through ... */
7164 if (integer_zerop (arg1))
7165 return non_lvalue (fold_convert (type, arg0));
7166 if (integer_zerop (arg0))
7167 return omit_one_operand (type, arg0, arg1);
7169 /* Since negative shift count is not well-defined,
7170 don't try to compute it in the compiler. */
7171 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
7173 /* Rewrite an LROTATE_EXPR by a constant into an
7174 RROTATE_EXPR by a new constant. */
7175 if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
7177 tree tem = build_int_2 (GET_MODE_BITSIZE (TYPE_MODE (type)), 0);
7178 tem = fold_convert (TREE_TYPE (arg1), tem);
7179 tem = const_binop (MINUS_EXPR, tem, arg1, 0);
7180 return fold (build2 (RROTATE_EXPR, type, arg0, tem));
7183 /* If we have a rotate of a bit operation with the rotate count and
7184 the second operand of the bit operation both constant,
7185 permute the two operations. */
7186 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
7187 && (TREE_CODE (arg0) == BIT_AND_EXPR
7188 || TREE_CODE (arg0) == BIT_IOR_EXPR
7189 || TREE_CODE (arg0) == BIT_XOR_EXPR)
7190 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
7191 return fold (build2 (TREE_CODE (arg0), type,
7192 fold (build2 (code, type,
7193 TREE_OPERAND (arg0, 0), arg1)),
7194 fold (build2 (code, type,
7195 TREE_OPERAND (arg0, 1), arg1))));
7197 /* Two consecutive rotates adding up to the width of the mode can
7199 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
7200 && TREE_CODE (arg0) == RROTATE_EXPR
7201 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7202 && TREE_INT_CST_HIGH (arg1) == 0
7203 && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
7204 && ((TREE_INT_CST_LOW (arg1)
7205 + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
7206 == (unsigned int) GET_MODE_BITSIZE (TYPE_MODE (type))))
7207 return TREE_OPERAND (arg0, 0);
7212 if (operand_equal_p (arg0, arg1, 0))
7213 return omit_one_operand (type, arg0, arg1);
7214 if (INTEGRAL_TYPE_P (type)
7215 && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
7216 return omit_one_operand (type, arg1, arg0);
7220 if (operand_equal_p (arg0, arg1, 0))
7221 return omit_one_operand (type, arg0, arg1);
7222 if (INTEGRAL_TYPE_P (type)
7223 && TYPE_MAX_VALUE (type)
7224 && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
7225 return omit_one_operand (type, arg1, arg0);
7228 case TRUTH_NOT_EXPR:
7229 /* The argument to invert_truthvalue must have Boolean type. */
7230 if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
7231 arg0 = fold_convert (boolean_type_node, arg0);
7233 /* Note that the operand of this must be an int
7234 and its values must be 0 or 1.
7235 ("true" is a fixed value perhaps depending on the language,
7236 but we don't handle values other than 1 correctly yet.) */
7237 tem = invert_truthvalue (arg0);
7238 /* Avoid infinite recursion. */
7239 if (TREE_CODE (tem) == TRUTH_NOT_EXPR)
7241 tem = fold_single_bit_test (code, arg0, arg1, type);
7246 return fold_convert (type, tem);
7248 case TRUTH_ANDIF_EXPR:
7249 /* Note that the operands of this must be ints
7250 and their values must be 0 or 1.
7251 ("true" is a fixed value perhaps depending on the language.) */
7252 /* If first arg is constant zero, return it. */
7253 if (integer_zerop (arg0))
7254 return fold_convert (type, arg0);
7255 case TRUTH_AND_EXPR:
7256 /* If either arg is constant true, drop it. */
7257 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
7258 return non_lvalue (fold_convert (type, arg1));
7259 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
7260 /* Preserve sequence points. */
7261 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
7262 return non_lvalue (fold_convert (type, arg0));
7263 /* If second arg is constant zero, result is zero, but first arg
7264 must be evaluated. */
7265 if (integer_zerop (arg1))
7266 return omit_one_operand (type, arg1, arg0);
7267 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
7268 case will be handled here. */
7269 if (integer_zerop (arg0))
7270 return omit_one_operand (type, arg0, arg1);
7273 /* We only do these simplifications if we are optimizing. */
7277 /* Check for things like (A || B) && (A || C). We can convert this
7278 to A || (B && C). Note that either operator can be any of the four
7279 truth and/or operations and the transformation will still be
7280 valid. Also note that we only care about order for the
7281 ANDIF and ORIF operators. If B contains side effects, this
7282 might change the truth-value of A. */
7283 if (TREE_CODE (arg0) == TREE_CODE (arg1)
7284 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
7285 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
7286 || TREE_CODE (arg0) == TRUTH_AND_EXPR
7287 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
7288 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
7290 tree a00 = TREE_OPERAND (arg0, 0);
7291 tree a01 = TREE_OPERAND (arg0, 1);
7292 tree a10 = TREE_OPERAND (arg1, 0);
7293 tree a11 = TREE_OPERAND (arg1, 1);
7294 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
7295 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
7296 && (code == TRUTH_AND_EXPR
7297 || code == TRUTH_OR_EXPR));
7299 if (operand_equal_p (a00, a10, 0))
7300 return fold (build2 (TREE_CODE (arg0), type, a00,
7301 fold (build2 (code, type, a01, a11))));
7302 else if (commutative && operand_equal_p (a00, a11, 0))
7303 return fold (build2 (TREE_CODE (arg0), type, a00,
7304 fold (build2 (code, type, a01, a10))));
7305 else if (commutative && operand_equal_p (a01, a10, 0))
7306 return fold (build2 (TREE_CODE (arg0), type, a01,
7307 fold (build2 (code, type, a00, a11))));
7309 /* This case if tricky because we must either have commutative
7310 operators or else A10 must not have side-effects. */
7312 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
7313 && operand_equal_p (a01, a11, 0))
7314 return fold (build2 (TREE_CODE (arg0), type,
7315 fold (build2 (code, type, a00, a10)),
7319 /* See if we can build a range comparison. */
7320 if (0 != (tem = fold_range_test (t)))
7323 /* Check for the possibility of merging component references. If our
7324 lhs is another similar operation, try to merge its rhs with our
7325 rhs. Then try to merge our lhs and rhs. */
7326 if (TREE_CODE (arg0) == code
7327 && 0 != (tem = fold_truthop (code, type,
7328 TREE_OPERAND (arg0, 1), arg1)))
7329 return fold (build2 (code, type, TREE_OPERAND (arg0, 0), tem));
7331 if ((tem = fold_truthop (code, type, arg0, arg1)) != 0)
7336 case TRUTH_ORIF_EXPR:
7337 /* Note that the operands of this must be ints
7338 and their values must be 0 or true.
7339 ("true" is a fixed value perhaps depending on the language.) */
7340 /* If first arg is constant true, return it. */
7341 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
7342 return fold_convert (type, arg0);
7344 /* If either arg is constant zero, drop it. */
7345 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
7346 return non_lvalue (fold_convert (type, arg1));
7347 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
7348 /* Preserve sequence points. */
7349 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
7350 return non_lvalue (fold_convert (type, arg0));
7351 /* If second arg is constant true, result is true, but we must
7352 evaluate first arg. */
7353 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
7354 return omit_one_operand (type, arg1, arg0);
7355 /* Likewise for first arg, but note this only occurs here for
7357 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
7358 return omit_one_operand (type, arg0, arg1);
7361 case TRUTH_XOR_EXPR:
7362 /* If either arg is constant zero, drop it. */
7363 if (integer_zerop (arg0))
7364 return non_lvalue (fold_convert (type, arg1));
7365 if (integer_zerop (arg1))
7366 return non_lvalue (fold_convert (type, arg0));
7367 /* If either arg is constant true, this is a logical inversion. */
7368 if (integer_onep (arg0))
7369 return non_lvalue (fold_convert (type, invert_truthvalue (arg1)));
7370 if (integer_onep (arg1))
7371 return non_lvalue (fold_convert (type, invert_truthvalue (arg0)));
7372 /* Identical arguments cancel to zero. */
7373 if (operand_equal_p (arg0, arg1, 0))
7374 return omit_one_operand (type, integer_zero_node, arg0);
7383 /* If one arg is a real or integer constant, put it last. */
7384 if (tree_swap_operands_p (arg0, arg1, true))
7385 return fold (build2 (swap_tree_comparison (code), type, arg1, arg0));
7387 /* If this is an equality comparison of the address of a non-weak
7388 object against zero, then we know the result. */
7389 if ((code == EQ_EXPR || code == NE_EXPR)
7390 && TREE_CODE (arg0) == ADDR_EXPR
7391 && DECL_P (TREE_OPERAND (arg0, 0))
7392 && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
7393 && integer_zerop (arg1))
7394 return constant_boolean_node (code != EQ_EXPR, type);
7396 /* If this is an equality comparison of the address of two non-weak,
7397 unaliased symbols neither of which are extern (since we do not
7398 have access to attributes for externs), then we know the result. */
7399 if ((code == EQ_EXPR || code == NE_EXPR)
7400 && TREE_CODE (arg0) == ADDR_EXPR
7401 && DECL_P (TREE_OPERAND (arg0, 0))
7402 && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
7403 && ! lookup_attribute ("alias",
7404 DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
7405 && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
7406 && TREE_CODE (arg1) == ADDR_EXPR
7407 && DECL_P (TREE_OPERAND (arg1, 0))
7408 && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
7409 && ! lookup_attribute ("alias",
7410 DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
7411 && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
7412 return constant_boolean_node (operand_equal_p (arg0, arg1, 0)
7413 ? code == EQ_EXPR : code != EQ_EXPR,
7416 if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
7418 tree targ0 = strip_float_extensions (arg0);
7419 tree targ1 = strip_float_extensions (arg1);
7420 tree newtype = TREE_TYPE (targ0);
7422 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
7423 newtype = TREE_TYPE (targ1);
7425 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
7426 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
7427 return fold (build2 (code, type, fold_convert (newtype, targ0),
7428 fold_convert (newtype, targ1)));
7430 /* (-a) CMP (-b) -> b CMP a */
7431 if (TREE_CODE (arg0) == NEGATE_EXPR
7432 && TREE_CODE (arg1) == NEGATE_EXPR)
7433 return fold (build2 (code, type, TREE_OPERAND (arg1, 0),
7434 TREE_OPERAND (arg0, 0)));
7436 if (TREE_CODE (arg1) == REAL_CST)
7438 REAL_VALUE_TYPE cst;
7439 cst = TREE_REAL_CST (arg1);
7441 /* (-a) CMP CST -> a swap(CMP) (-CST) */
7442 if (TREE_CODE (arg0) == NEGATE_EXPR)
7444 fold (build2 (swap_tree_comparison (code), type,
7445 TREE_OPERAND (arg0, 0),
7446 build_real (TREE_TYPE (arg1),
7447 REAL_VALUE_NEGATE (cst))));
7449 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
7450 /* a CMP (-0) -> a CMP 0 */
7451 if (REAL_VALUE_MINUS_ZERO (cst))
7452 return fold (build2 (code, type, arg0,
7453 build_real (TREE_TYPE (arg1), dconst0)));
7455 /* x != NaN is always true, other ops are always false. */
7456 if (REAL_VALUE_ISNAN (cst)
7457 && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
7459 tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
7460 return omit_one_operand (type, tem, arg0);
7463 /* Fold comparisons against infinity. */
7464 if (REAL_VALUE_ISINF (cst))
7466 tem = fold_inf_compare (code, type, arg0, arg1);
7467 if (tem != NULL_TREE)
7472 /* If this is a comparison of a real constant with a PLUS_EXPR
7473 or a MINUS_EXPR of a real constant, we can convert it into a
7474 comparison with a revised real constant as long as no overflow
7475 occurs when unsafe_math_optimizations are enabled. */
7476 if (flag_unsafe_math_optimizations
7477 && TREE_CODE (arg1) == REAL_CST
7478 && (TREE_CODE (arg0) == PLUS_EXPR
7479 || TREE_CODE (arg0) == MINUS_EXPR)
7480 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
7481 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
7482 ? MINUS_EXPR : PLUS_EXPR,
7483 arg1, TREE_OPERAND (arg0, 1), 0))
7484 && ! TREE_CONSTANT_OVERFLOW (tem))
7485 return fold (build2 (code, type, TREE_OPERAND (arg0, 0), tem));
7487 /* Likewise, we can simplify a comparison of a real constant with
7488 a MINUS_EXPR whose first operand is also a real constant, i.e.
7489 (c1 - x) < c2 becomes x > c1-c2. */
7490 if (flag_unsafe_math_optimizations
7491 && TREE_CODE (arg1) == REAL_CST
7492 && TREE_CODE (arg0) == MINUS_EXPR
7493 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
7494 && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
7496 && ! TREE_CONSTANT_OVERFLOW (tem))
7497 return fold (build2 (swap_tree_comparison (code), type,
7498 TREE_OPERAND (arg0, 1), tem));
7500 /* Fold comparisons against built-in math functions. */
7501 if (TREE_CODE (arg1) == REAL_CST
7502 && flag_unsafe_math_optimizations
7503 && ! flag_errno_math)
7505 enum built_in_function fcode = builtin_mathfn_code (arg0);
7507 if (fcode != END_BUILTINS)
7509 tem = fold_mathfn_compare (fcode, code, type, arg0, arg1);
7510 if (tem != NULL_TREE)
7516 /* Convert foo++ == CONST into ++foo == CONST + INCR. */
7517 if (TREE_CONSTANT (arg1)
7518 && (TREE_CODE (arg0) == POSTINCREMENT_EXPR
7519 || TREE_CODE (arg0) == POSTDECREMENT_EXPR)
7520 /* This optimization is invalid for ordered comparisons
7521 if CONST+INCR overflows or if foo+incr might overflow.
7522 This optimization is invalid for floating point due to rounding.
7523 For pointer types we assume overflow doesn't happen. */
7524 && (POINTER_TYPE_P (TREE_TYPE (arg0))
7525 || (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
7526 && (code == EQ_EXPR || code == NE_EXPR))))
7528 tree varop, newconst;
7530 if (TREE_CODE (arg0) == POSTINCREMENT_EXPR)
7532 newconst = fold (build2 (PLUS_EXPR, TREE_TYPE (arg0),
7533 arg1, TREE_OPERAND (arg0, 1)));
7534 varop = build2 (PREINCREMENT_EXPR, TREE_TYPE (arg0),
7535 TREE_OPERAND (arg0, 0),
7536 TREE_OPERAND (arg0, 1));
7540 newconst = fold (build2 (MINUS_EXPR, TREE_TYPE (arg0),
7541 arg1, TREE_OPERAND (arg0, 1)));
7542 varop = build2 (PREDECREMENT_EXPR, TREE_TYPE (arg0),
7543 TREE_OPERAND (arg0, 0),
7544 TREE_OPERAND (arg0, 1));
7548 /* If VAROP is a reference to a bitfield, we must mask
7549 the constant by the width of the field. */
7550 if (TREE_CODE (TREE_OPERAND (varop, 0)) == COMPONENT_REF
7551 && DECL_BIT_FIELD (TREE_OPERAND (TREE_OPERAND (varop, 0), 1)))
7553 tree fielddecl = TREE_OPERAND (TREE_OPERAND (varop, 0), 1);
7554 int size = TREE_INT_CST_LOW (DECL_SIZE (fielddecl));
7555 tree folded_compare, shift;
7557 /* First check whether the comparison would come out
7558 always the same. If we don't do that we would
7559 change the meaning with the masking. */
7560 folded_compare = fold (build2 (code, type,
7561 TREE_OPERAND (varop, 0),
7563 if (integer_zerop (folded_compare)
7564 || integer_onep (folded_compare))
7565 return omit_one_operand (type, folded_compare, varop);
7567 shift = build_int_2 (TYPE_PRECISION (TREE_TYPE (varop)) - size,
7569 newconst = fold (build2 (LSHIFT_EXPR, TREE_TYPE (varop),
7571 newconst = fold (build2 (RSHIFT_EXPR, TREE_TYPE (varop),
7575 return fold (build2 (code, type, varop, newconst));
7578 /* Change X >= C to X > (C - 1) and X < C to X <= (C - 1) if C > 0.
7579 This transformation affects the cases which are handled in later
7580 optimizations involving comparisons with non-negative constants. */
7581 if (TREE_CODE (arg1) == INTEGER_CST
7582 && TREE_CODE (arg0) != INTEGER_CST
7583 && tree_int_cst_sgn (arg1) > 0)
7588 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
7589 return fold (build2 (GT_EXPR, type, arg0, arg1));
7592 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
7593 return fold (build2 (LE_EXPR, type, arg0, arg1));
7600 /* Comparisons with the highest or lowest possible integer of
7601 the specified size will have known values.
7603 This is quite similar to fold_relational_hi_lo; however, my
7604 attempts to share the code have been nothing but trouble.
7605 I give up for now. */
7607 int width = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg1)));
7609 if (TREE_CODE (arg1) == INTEGER_CST
7610 && ! TREE_CONSTANT_OVERFLOW (arg1)
7611 && width <= HOST_BITS_PER_WIDE_INT
7612 && (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
7613 || POINTER_TYPE_P (TREE_TYPE (arg1))))
7615 unsigned HOST_WIDE_INT signed_max;
7616 unsigned HOST_WIDE_INT max, min;
7618 signed_max = ((unsigned HOST_WIDE_INT) 1 << (width - 1)) - 1;
7620 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
7622 max = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
7628 min = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
7631 if (TREE_INT_CST_HIGH (arg1) == 0
7632 && TREE_INT_CST_LOW (arg1) == max)
7636 return omit_one_operand (type, integer_zero_node, arg0);
7639 return fold (build2 (EQ_EXPR, type, arg0, arg1));
7642 return omit_one_operand (type, integer_one_node, arg0);
7645 return fold (build2 (NE_EXPR, type, arg0, arg1));
7647 /* The GE_EXPR and LT_EXPR cases above are not normally
7648 reached because of previous transformations. */
7653 else if (TREE_INT_CST_HIGH (arg1) == 0
7654 && TREE_INT_CST_LOW (arg1) == max - 1)
7658 arg1 = const_binop (PLUS_EXPR, arg1, integer_one_node, 0);
7659 return fold (build2 (EQ_EXPR, type, arg0, arg1));
7661 arg1 = const_binop (PLUS_EXPR, arg1, integer_one_node, 0);
7662 return fold (build2 (NE_EXPR, type, arg0, arg1));
7666 else if (TREE_INT_CST_HIGH (arg1) == (min ? -1 : 0)
7667 && TREE_INT_CST_LOW (arg1) == min)
7671 return omit_one_operand (type, integer_zero_node, arg0);
7674 return fold (build2 (EQ_EXPR, type, arg0, arg1));
7677 return omit_one_operand (type, integer_one_node, arg0);
7680 return fold (build2 (NE_EXPR, type, arg0, arg1));
7685 else if (TREE_INT_CST_HIGH (arg1) == (min ? -1 : 0)
7686 && TREE_INT_CST_LOW (arg1) == min + 1)
7690 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
7691 return fold (build2 (NE_EXPR, type, arg0, arg1));
7693 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
7694 return fold (build2 (EQ_EXPR, type, arg0, arg1));
7699 else if (!in_gimple_form
7700 && TREE_INT_CST_HIGH (arg1) == 0
7701 && TREE_INT_CST_LOW (arg1) == signed_max
7702 && TYPE_UNSIGNED (TREE_TYPE (arg1))
7703 /* signed_type does not work on pointer types. */
7704 && INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
7706 /* The following case also applies to X < signed_max+1
7707 and X >= signed_max+1 because previous transformations. */
7708 if (code == LE_EXPR || code == GT_EXPR)
7711 st0 = lang_hooks.types.signed_type (TREE_TYPE (arg0));
7712 st1 = lang_hooks.types.signed_type (TREE_TYPE (arg1));
7714 (build2 (code == LE_EXPR ? GE_EXPR: LT_EXPR,
7715 type, fold_convert (st0, arg0),
7716 fold_convert (st1, integer_zero_node)));
7722 /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
7723 a MINUS_EXPR of a constant, we can convert it into a comparison with
7724 a revised constant as long as no overflow occurs. */
7725 if ((code == EQ_EXPR || code == NE_EXPR)
7726 && TREE_CODE (arg1) == INTEGER_CST
7727 && (TREE_CODE (arg0) == PLUS_EXPR
7728 || TREE_CODE (arg0) == MINUS_EXPR)
7729 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7730 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
7731 ? MINUS_EXPR : PLUS_EXPR,
7732 arg1, TREE_OPERAND (arg0, 1), 0))
7733 && ! TREE_CONSTANT_OVERFLOW (tem))
7734 return fold (build2 (code, type, TREE_OPERAND (arg0, 0), tem));
7736 /* Similarly for a NEGATE_EXPR. */
7737 else if ((code == EQ_EXPR || code == NE_EXPR)
7738 && TREE_CODE (arg0) == NEGATE_EXPR
7739 && TREE_CODE (arg1) == INTEGER_CST
7740 && 0 != (tem = negate_expr (arg1))
7741 && TREE_CODE (tem) == INTEGER_CST
7742 && ! TREE_CONSTANT_OVERFLOW (tem))
7743 return fold (build2 (code, type, TREE_OPERAND (arg0, 0), tem));
7745 /* If we have X - Y == 0, we can convert that to X == Y and similarly
7746 for !=. Don't do this for ordered comparisons due to overflow. */
7747 else if ((code == NE_EXPR || code == EQ_EXPR)
7748 && integer_zerop (arg1) && TREE_CODE (arg0) == MINUS_EXPR)
7749 return fold (build2 (code, type,
7750 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1)));
7752 /* If we are widening one operand of an integer comparison,
7753 see if the other operand is similarly being widened. Perhaps we
7754 can do the comparison in the narrower type. */
7755 else if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
7756 && TREE_CODE (arg0) == NOP_EXPR
7757 && (tem = get_unwidened (arg0, NULL_TREE)) != arg0
7758 && (code == EQ_EXPR || code == NE_EXPR
7759 || TYPE_UNSIGNED (TREE_TYPE (arg0))
7760 == TYPE_UNSIGNED (TREE_TYPE (tem)))
7761 && (t1 = get_unwidened (arg1, TREE_TYPE (tem))) != 0
7762 && (TREE_TYPE (t1) == TREE_TYPE (tem)
7763 || (TREE_CODE (t1) == INTEGER_CST
7764 && int_fits_type_p (t1, TREE_TYPE (tem)))))
7765 return fold (build2 (code, type, tem,
7766 fold_convert (TREE_TYPE (tem), t1)));
7768 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
7769 constant, we can simplify it. */
7770 else if (TREE_CODE (arg1) == INTEGER_CST
7771 && (TREE_CODE (arg0) == MIN_EXPR
7772 || TREE_CODE (arg0) == MAX_EXPR)
7773 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
7774 return optimize_minmax_comparison (t);
7776 /* If we are comparing an ABS_EXPR with a constant, we can
7777 convert all the cases into explicit comparisons, but they may
7778 well not be faster than doing the ABS and one comparison.
7779 But ABS (X) <= C is a range comparison, which becomes a subtraction
7780 and a comparison, and is probably faster. */
7781 else if (code == LE_EXPR && TREE_CODE (arg1) == INTEGER_CST
7782 && TREE_CODE (arg0) == ABS_EXPR
7783 && ! TREE_SIDE_EFFECTS (arg0)
7784 && (0 != (tem = negate_expr (arg1)))
7785 && TREE_CODE (tem) == INTEGER_CST
7786 && ! TREE_CONSTANT_OVERFLOW (tem))
7787 return fold (build2 (TRUTH_ANDIF_EXPR, type,
7788 build2 (GE_EXPR, type,
7789 TREE_OPERAND (arg0, 0), tem),
7790 build2 (LE_EXPR, type,
7791 TREE_OPERAND (arg0, 0), arg1)));
7793 /* If this is an EQ or NE comparison with zero and ARG0 is
7794 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
7795 two operations, but the latter can be done in one less insn
7796 on machines that have only two-operand insns or on which a
7797 constant cannot be the first operand. */
7798 if (integer_zerop (arg1) && (code == EQ_EXPR || code == NE_EXPR)
7799 && TREE_CODE (arg0) == BIT_AND_EXPR)
7801 tree arg00 = TREE_OPERAND (arg0, 0);
7802 tree arg01 = TREE_OPERAND (arg0, 1);
7803 if (TREE_CODE (arg00) == LSHIFT_EXPR
7804 && integer_onep (TREE_OPERAND (arg00, 0)))
7806 fold (build2 (code, type,
7807 build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
7808 build2 (RSHIFT_EXPR, TREE_TYPE (arg00),
7809 arg01, TREE_OPERAND (arg00, 1)),
7810 fold_convert (TREE_TYPE (arg0),
7813 else if (TREE_CODE (TREE_OPERAND (arg0, 1)) == LSHIFT_EXPR
7814 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 1), 0)))
7816 fold (build2 (code, type,
7817 build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
7818 build2 (RSHIFT_EXPR, TREE_TYPE (arg01),
7819 arg00, TREE_OPERAND (arg01, 1)),
7820 fold_convert (TREE_TYPE (arg0),
7825 /* If this is an NE or EQ comparison of zero against the result of a
7826 signed MOD operation whose second operand is a power of 2, make
7827 the MOD operation unsigned since it is simpler and equivalent. */
7828 if ((code == NE_EXPR || code == EQ_EXPR)
7829 && integer_zerop (arg1)
7830 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
7831 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
7832 || TREE_CODE (arg0) == CEIL_MOD_EXPR
7833 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
7834 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
7835 && integer_pow2p (TREE_OPERAND (arg0, 1)))
7837 tree newtype = lang_hooks.types.unsigned_type (TREE_TYPE (arg0));
7838 tree newmod = build2 (TREE_CODE (arg0), newtype,
7839 fold_convert (newtype,
7840 TREE_OPERAND (arg0, 0)),
7841 fold_convert (newtype,
7842 TREE_OPERAND (arg0, 1)));
7844 return build2 (code, type, newmod, fold_convert (newtype, arg1));
7847 /* If this is an NE comparison of zero with an AND of one, remove the
7848 comparison since the AND will give the correct value. */
7849 if (code == NE_EXPR && integer_zerop (arg1)
7850 && TREE_CODE (arg0) == BIT_AND_EXPR
7851 && integer_onep (TREE_OPERAND (arg0, 1)))
7852 return fold_convert (type, arg0);
7854 /* If we have (A & C) == C where C is a power of 2, convert this into
7855 (A & C) != 0. Similarly for NE_EXPR. */
7856 if ((code == EQ_EXPR || code == NE_EXPR)
7857 && TREE_CODE (arg0) == BIT_AND_EXPR
7858 && integer_pow2p (TREE_OPERAND (arg0, 1))
7859 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
7860 return fold (build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
7861 arg0, integer_zero_node));
7863 /* If we have (A & C) != 0 or (A & C) == 0 and C is a power of
7864 2, then fold the expression into shifts and logical operations. */
7865 tem = fold_single_bit_test (code, arg0, arg1, type);
7869 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
7870 Similarly for NE_EXPR. */
7871 if ((code == EQ_EXPR || code == NE_EXPR)
7872 && TREE_CODE (arg0) == BIT_AND_EXPR
7873 && TREE_CODE (arg1) == INTEGER_CST
7874 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
7877 = fold (build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
7878 arg1, build1 (BIT_NOT_EXPR,
7879 TREE_TYPE (TREE_OPERAND (arg0, 1)),
7880 TREE_OPERAND (arg0, 1))));
7881 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
7882 if (integer_nonzerop (dandnotc))
7883 return omit_one_operand (type, rslt, arg0);
7886 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
7887 Similarly for NE_EXPR. */
7888 if ((code == EQ_EXPR || code == NE_EXPR)
7889 && TREE_CODE (arg0) == BIT_IOR_EXPR
7890 && TREE_CODE (arg1) == INTEGER_CST
7891 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
7894 = fold (build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
7895 TREE_OPERAND (arg0, 1),
7896 build1 (BIT_NOT_EXPR, TREE_TYPE (arg1), arg1)));
7897 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
7898 if (integer_nonzerop (candnotd))
7899 return omit_one_operand (type, rslt, arg0);
7902 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
7903 and similarly for >= into !=. */
7904 if ((code == LT_EXPR || code == GE_EXPR)
7905 && TYPE_UNSIGNED (TREE_TYPE (arg0))
7906 && TREE_CODE (arg1) == LSHIFT_EXPR
7907 && integer_onep (TREE_OPERAND (arg1, 0)))
7908 return build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
7909 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
7910 TREE_OPERAND (arg1, 1)),
7911 fold_convert (TREE_TYPE (arg0), integer_zero_node));
7913 else if ((code == LT_EXPR || code == GE_EXPR)
7914 && TYPE_UNSIGNED (TREE_TYPE (arg0))
7915 && (TREE_CODE (arg1) == NOP_EXPR
7916 || TREE_CODE (arg1) == CONVERT_EXPR)
7917 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
7918 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
7920 build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
7921 fold_convert (TREE_TYPE (arg0),
7922 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
7923 TREE_OPERAND (TREE_OPERAND (arg1, 0),
7925 fold_convert (TREE_TYPE (arg0), integer_zero_node));
7927 /* Simplify comparison of something with itself. (For IEEE
7928 floating-point, we can only do some of these simplifications.) */
7929 if (operand_equal_p (arg0, arg1, 0))
7934 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
7935 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
7936 return constant_boolean_node (1, type);
7941 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
7942 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
7943 return constant_boolean_node (1, type);
7944 return fold (build2 (EQ_EXPR, type, arg0, arg1));
7947 /* For NE, we can only do this simplification if integer
7948 or we don't honor IEEE floating point NaNs. */
7949 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
7950 && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
7952 /* ... fall through ... */
7955 return constant_boolean_node (0, type);
7961 /* If we are comparing an expression that just has comparisons
7962 of two integer values, arithmetic expressions of those comparisons,
7963 and constants, we can simplify it. There are only three cases
7964 to check: the two values can either be equal, the first can be
7965 greater, or the second can be greater. Fold the expression for
7966 those three values. Since each value must be 0 or 1, we have
7967 eight possibilities, each of which corresponds to the constant 0
7968 or 1 or one of the six possible comparisons.
7970 This handles common cases like (a > b) == 0 but also handles
7971 expressions like ((x > y) - (y > x)) > 0, which supposedly
7972 occur in macroized code. */
7974 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
7976 tree cval1 = 0, cval2 = 0;
7979 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
7980 /* Don't handle degenerate cases here; they should already
7981 have been handled anyway. */
7982 && cval1 != 0 && cval2 != 0
7983 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
7984 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
7985 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
7986 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
7987 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
7988 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
7989 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
7991 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
7992 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
7994 /* We can't just pass T to eval_subst in case cval1 or cval2
7995 was the same as ARG1. */
7998 = fold (build2 (code, type,
7999 eval_subst (arg0, cval1, maxval,
8003 = fold (build2 (code, type,
8004 eval_subst (arg0, cval1, maxval,
8008 = fold (build2 (code, type,
8009 eval_subst (arg0, cval1, minval,
8013 /* All three of these results should be 0 or 1. Confirm they
8014 are. Then use those values to select the proper code
8017 if ((integer_zerop (high_result)
8018 || integer_onep (high_result))
8019 && (integer_zerop (equal_result)
8020 || integer_onep (equal_result))
8021 && (integer_zerop (low_result)
8022 || integer_onep (low_result)))
8024 /* Make a 3-bit mask with the high-order bit being the
8025 value for `>', the next for '=', and the low for '<'. */
8026 switch ((integer_onep (high_result) * 4)
8027 + (integer_onep (equal_result) * 2)
8028 + integer_onep (low_result))
8032 return omit_one_operand (type, integer_zero_node, arg0);
8053 return omit_one_operand (type, integer_one_node, arg0);
8056 tem = build2 (code, type, cval1, cval2);
8058 return save_expr (tem);
8065 /* If this is a comparison of a field, we may be able to simplify it. */
8066 if (((TREE_CODE (arg0) == COMPONENT_REF
8067 && lang_hooks.can_use_bit_fields_p ())
8068 || TREE_CODE (arg0) == BIT_FIELD_REF)
8069 && (code == EQ_EXPR || code == NE_EXPR)
8070 /* Handle the constant case even without -O
8071 to make sure the warnings are given. */
8072 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
8074 t1 = optimize_bit_field_compare (code, type, arg0, arg1);
8079 /* If this is a comparison of complex values and either or both sides
8080 are a COMPLEX_EXPR or COMPLEX_CST, it is best to split up the
8081 comparisons and join them with a TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR.
8082 This may prevent needless evaluations. */
8083 if ((code == EQ_EXPR || code == NE_EXPR)
8084 && TREE_CODE (TREE_TYPE (arg0)) == COMPLEX_TYPE
8085 && (TREE_CODE (arg0) == COMPLEX_EXPR
8086 || TREE_CODE (arg1) == COMPLEX_EXPR
8087 || TREE_CODE (arg0) == COMPLEX_CST
8088 || TREE_CODE (arg1) == COMPLEX_CST))
8090 tree subtype = TREE_TYPE (TREE_TYPE (arg0));
8091 tree real0, imag0, real1, imag1;
8093 arg0 = save_expr (arg0);
8094 arg1 = save_expr (arg1);
8095 real0 = fold (build1 (REALPART_EXPR, subtype, arg0));
8096 imag0 = fold (build1 (IMAGPART_EXPR, subtype, arg0));
8097 real1 = fold (build1 (REALPART_EXPR, subtype, arg1));
8098 imag1 = fold (build1 (IMAGPART_EXPR, subtype, arg1));
8100 return fold (build2 ((code == EQ_EXPR ? TRUTH_ANDIF_EXPR
8103 fold (build2 (code, type, real0, real1)),
8104 fold (build2 (code, type, imag0, imag1))));
8107 /* Optimize comparisons of strlen vs zero to a compare of the
8108 first character of the string vs zero. To wit,
8109 strlen(ptr) == 0 => *ptr == 0
8110 strlen(ptr) != 0 => *ptr != 0
8111 Other cases should reduce to one of these two (or a constant)
8112 due to the return value of strlen being unsigned. */
8113 if ((code == EQ_EXPR || code == NE_EXPR)
8114 && integer_zerop (arg1)
8115 && TREE_CODE (arg0) == CALL_EXPR)
8117 tree fndecl = get_callee_fndecl (arg0);
8121 && DECL_BUILT_IN (fndecl)
8122 && DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_MD
8123 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
8124 && (arglist = TREE_OPERAND (arg0, 1))
8125 && TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) == POINTER_TYPE
8126 && ! TREE_CHAIN (arglist))
8127 return fold (build2 (code, type,
8128 build1 (INDIRECT_REF, char_type_node,
8129 TREE_VALUE(arglist)),
8130 integer_zero_node));
8133 /* We can fold X/C1 op C2 where C1 and C2 are integer constants
8134 into a single range test. */
8135 if (TREE_CODE (arg0) == TRUNC_DIV_EXPR
8136 && TREE_CODE (arg1) == INTEGER_CST
8137 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8138 && !integer_zerop (TREE_OPERAND (arg0, 1))
8139 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8140 && !TREE_OVERFLOW (arg1))
8142 t1 = fold_div_compare (code, type, arg0, arg1);
8143 if (t1 != NULL_TREE)
8147 /* Both ARG0 and ARG1 are known to be constants at this point. */
8148 t1 = fold_relational_const (code, type, arg0, arg1);
8149 return (t1 == NULL_TREE ? t : t1);
8152 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
8153 so all simple results must be passed through pedantic_non_lvalue. */
8154 if (TREE_CODE (arg0) == INTEGER_CST)
8156 tem = TREE_OPERAND (t, (integer_zerop (arg0) ? 2 : 1));
8157 /* Only optimize constant conditions when the selected branch
8158 has the same type as the COND_EXPR. This avoids optimizing
8159 away "c ? x : throw", where the throw has a void type. */
8160 if (! VOID_TYPE_P (TREE_TYPE (tem))
8161 || VOID_TYPE_P (type))
8162 return pedantic_non_lvalue (tem);
8165 if (operand_equal_p (arg1, TREE_OPERAND (t, 2), 0))
8166 return pedantic_omit_one_operand (type, arg1, arg0);
8168 /* If we have A op B ? A : C, we may be able to convert this to a
8169 simpler expression, depending on the operation and the values
8170 of B and C. Signed zeros prevent all of these transformations,
8171 for reasons given above each one. */
8173 if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
8174 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
8175 arg1, TREE_OPERAND (arg0, 1))
8176 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
8178 tree arg2 = TREE_OPERAND (t, 2);
8179 enum tree_code comp_code = TREE_CODE (arg0);
8183 /* If we have A op 0 ? A : -A, consider applying the following
8186 A == 0? A : -A same as -A
8187 A != 0? A : -A same as A
8188 A >= 0? A : -A same as abs (A)
8189 A > 0? A : -A same as abs (A)
8190 A <= 0? A : -A same as -abs (A)
8191 A < 0? A : -A same as -abs (A)
8193 None of these transformations work for modes with signed
8194 zeros. If A is +/-0, the first two transformations will
8195 change the sign of the result (from +0 to -0, or vice
8196 versa). The last four will fix the sign of the result,
8197 even though the original expressions could be positive or
8198 negative, depending on the sign of A.
8200 Note that all these transformations are correct if A is
8201 NaN, since the two alternatives (A and -A) are also NaNs. */
8202 if ((FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 1)))
8203 ? real_zerop (TREE_OPERAND (arg0, 1))
8204 : integer_zerop (TREE_OPERAND (arg0, 1)))
8205 && TREE_CODE (arg2) == NEGATE_EXPR
8206 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
8210 tem = fold_convert (TREE_TYPE (TREE_OPERAND (t, 1)), arg1);
8211 tem = fold_convert (type, negate_expr (tem));
8212 return pedantic_non_lvalue (tem);
8214 return pedantic_non_lvalue (fold_convert (type, arg1));
8217 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
8218 arg1 = fold_convert (lang_hooks.types.signed_type
8219 (TREE_TYPE (arg1)), arg1);
8220 arg1 = fold (build1 (ABS_EXPR, TREE_TYPE (arg1), arg1));
8221 return pedantic_non_lvalue (fold_convert (type, arg1));
8224 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
8225 arg1 = fold_convert (lang_hooks.types.signed_type
8226 (TREE_TYPE (arg1)), arg1);
8227 arg1 = fold (build1 (ABS_EXPR, TREE_TYPE (arg1), arg1));
8228 arg1 = negate_expr (fold_convert (type, arg1));
8229 return pedantic_non_lvalue (arg1);
8234 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
8235 A == 0 ? A : 0 is always 0 unless A is -0. Note that
8236 both transformations are correct when A is NaN: A != 0
8237 is then true, and A == 0 is false. */
8239 if (integer_zerop (TREE_OPERAND (arg0, 1)) && integer_zerop (arg2))
8241 if (comp_code == NE_EXPR)
8242 return pedantic_non_lvalue (fold_convert (type, arg1));
8243 else if (comp_code == EQ_EXPR)
8244 return pedantic_non_lvalue (fold_convert (type, integer_zero_node));
8247 /* Try some transformations of A op B ? A : B.
8249 A == B? A : B same as B
8250 A != B? A : B same as A
8251 A >= B? A : B same as max (A, B)
8252 A > B? A : B same as max (B, A)
8253 A <= B? A : B same as min (A, B)
8254 A < B? A : B same as min (B, A)
8256 As above, these transformations don't work in the presence
8257 of signed zeros. For example, if A and B are zeros of
8258 opposite sign, the first two transformations will change
8259 the sign of the result. In the last four, the original
8260 expressions give different results for (A=+0, B=-0) and
8261 (A=-0, B=+0), but the transformed expressions do not.
8263 The first two transformations are correct if either A or B
8264 is a NaN. In the first transformation, the condition will
8265 be false, and B will indeed be chosen. In the case of the
8266 second transformation, the condition A != B will be true,
8267 and A will be chosen.
8269 The conversions to max() and min() are not correct if B is
8270 a number and A is not. The conditions in the original
8271 expressions will be false, so all four give B. The min()
8272 and max() versions would give a NaN instead. */
8273 if (operand_equal_for_comparison_p (TREE_OPERAND (arg0, 1),
8274 arg2, TREE_OPERAND (arg0, 0)))
8276 tree comp_op0 = TREE_OPERAND (arg0, 0);
8277 tree comp_op1 = TREE_OPERAND (arg0, 1);
8278 tree comp_type = TREE_TYPE (comp_op0);
8280 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
8281 if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
8291 return pedantic_non_lvalue (fold_convert (type, arg2));
8293 return pedantic_non_lvalue (fold_convert (type, arg1));
8296 /* In C++ a ?: expression can be an lvalue, so put the
8297 operand which will be used if they are equal first
8298 so that we can convert this back to the
8299 corresponding COND_EXPR. */
8300 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
8301 return pedantic_non_lvalue (fold_convert
8302 (type, fold (build2 (MIN_EXPR, comp_type,
8303 (comp_code == LE_EXPR
8304 ? comp_op0 : comp_op1),
8305 (comp_code == LE_EXPR
8306 ? comp_op1 : comp_op0)))));
8310 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
8311 return pedantic_non_lvalue (fold_convert
8312 (type, fold (build2 (MAX_EXPR, comp_type,
8313 (comp_code == GE_EXPR
8314 ? comp_op0 : comp_op1),
8315 (comp_code == GE_EXPR
8316 ? comp_op1 : comp_op0)))));
8323 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
8324 we might still be able to simplify this. For example,
8325 if C1 is one less or one more than C2, this might have started
8326 out as a MIN or MAX and been transformed by this function.
8327 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
8329 if (INTEGRAL_TYPE_P (type)
8330 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8331 && TREE_CODE (arg2) == INTEGER_CST)
8335 /* We can replace A with C1 in this case. */
8336 arg1 = fold_convert (type, TREE_OPERAND (arg0, 1));
8337 return fold (build3 (code, type, TREE_OPERAND (t, 0), arg1,
8338 TREE_OPERAND (t, 2)));
8341 /* If C1 is C2 + 1, this is min(A, C2). */
8342 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
8344 && operand_equal_p (TREE_OPERAND (arg0, 1),
8345 const_binop (PLUS_EXPR, arg2,
8346 integer_one_node, 0),
8348 return pedantic_non_lvalue
8349 (fold (build2 (MIN_EXPR, type, arg1, arg2)));
8353 /* If C1 is C2 - 1, this is min(A, C2). */
8354 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
8356 && operand_equal_p (TREE_OPERAND (arg0, 1),
8357 const_binop (MINUS_EXPR, arg2,
8358 integer_one_node, 0),
8360 return pedantic_non_lvalue
8361 (fold (build2 (MIN_EXPR, type, arg1, arg2)));
8365 /* If C1 is C2 - 1, this is max(A, C2). */
8366 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
8368 && operand_equal_p (TREE_OPERAND (arg0, 1),
8369 const_binop (MINUS_EXPR, arg2,
8370 integer_one_node, 0),
8372 return pedantic_non_lvalue
8373 (fold (build2 (MAX_EXPR, type, arg1, arg2)));
8377 /* If C1 is C2 + 1, this is max(A, C2). */
8378 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
8380 && operand_equal_p (TREE_OPERAND (arg0, 1),
8381 const_binop (PLUS_EXPR, arg2,
8382 integer_one_node, 0),
8384 return pedantic_non_lvalue
8385 (fold (build2 (MAX_EXPR, type, arg1, arg2)));
8394 /* If the second operand is simpler than the third, swap them
8395 since that produces better jump optimization results. */
8396 if (tree_swap_operands_p (TREE_OPERAND (t, 1),
8397 TREE_OPERAND (t, 2), false))
8399 /* See if this can be inverted. If it can't, possibly because
8400 it was a floating-point inequality comparison, don't do
8402 tem = invert_truthvalue (arg0);
8404 if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
8405 return fold (build3 (code, type, tem,
8406 TREE_OPERAND (t, 2), TREE_OPERAND (t, 1)));
8409 /* Convert A ? 1 : 0 to simply A. */
8410 if (integer_onep (TREE_OPERAND (t, 1))
8411 && integer_zerop (TREE_OPERAND (t, 2))
8412 /* If we try to convert TREE_OPERAND (t, 0) to our type, the
8413 call to fold will try to move the conversion inside
8414 a COND, which will recurse. In that case, the COND_EXPR
8415 is probably the best choice, so leave it alone. */
8416 && type == TREE_TYPE (arg0))
8417 return pedantic_non_lvalue (arg0);
8419 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
8420 over COND_EXPR in cases such as floating point comparisons. */
8421 if (integer_zerop (TREE_OPERAND (t, 1))
8422 && integer_onep (TREE_OPERAND (t, 2))
8423 && truth_value_p (TREE_CODE (arg0)))
8424 return pedantic_non_lvalue (fold_convert (type,
8425 invert_truthvalue (arg0)));
8427 /* Look for expressions of the form A & 2 ? 2 : 0. The result of this
8428 operation is simply A & 2. */
8430 if (integer_zerop (TREE_OPERAND (t, 2))
8431 && TREE_CODE (arg0) == NE_EXPR
8432 && integer_zerop (TREE_OPERAND (arg0, 1))
8433 && integer_pow2p (arg1)
8434 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
8435 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
8436 arg1, OEP_ONLY_CONST))
8437 return pedantic_non_lvalue (fold_convert (type,
8438 TREE_OPERAND (arg0, 0)));
8440 /* Convert A ? B : 0 into A && B if A and B are truth values. */
8441 if (integer_zerop (TREE_OPERAND (t, 2))
8442 && truth_value_p (TREE_CODE (arg0))
8443 && truth_value_p (TREE_CODE (arg1)))
8444 return pedantic_non_lvalue (fold (build2 (TRUTH_ANDIF_EXPR, type,
8447 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
8448 if (integer_onep (TREE_OPERAND (t, 2))
8449 && truth_value_p (TREE_CODE (arg0))
8450 && truth_value_p (TREE_CODE (arg1)))
8452 /* Only perform transformation if ARG0 is easily inverted. */
8453 tem = invert_truthvalue (arg0);
8454 if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
8455 return pedantic_non_lvalue (fold (build2 (TRUTH_ORIF_EXPR, type,
8462 /* When pedantic, a compound expression can be neither an lvalue
8463 nor an integer constant expression. */
8464 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
8466 /* Don't let (0, 0) be null pointer constant. */
8467 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
8468 : fold_convert (type, arg1);
8469 return pedantic_non_lvalue (tem);
8473 return build_complex (type, arg0, arg1);
8477 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8479 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
8480 return omit_one_operand (type, TREE_OPERAND (arg0, 0),
8481 TREE_OPERAND (arg0, 1));
8482 else if (TREE_CODE (arg0) == COMPLEX_CST)
8483 return TREE_REALPART (arg0);
8484 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8485 return fold (build2 (TREE_CODE (arg0), type,
8486 fold (build1 (REALPART_EXPR, type,
8487 TREE_OPERAND (arg0, 0))),
8488 fold (build1 (REALPART_EXPR, type,
8489 TREE_OPERAND (arg0, 1)))));
8493 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8494 return fold_convert (type, integer_zero_node);
8495 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
8496 return omit_one_operand (type, TREE_OPERAND (arg0, 1),
8497 TREE_OPERAND (arg0, 0));
8498 else if (TREE_CODE (arg0) == COMPLEX_CST)
8499 return TREE_IMAGPART (arg0);
8500 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8501 return fold (build2 (TREE_CODE (arg0), type,
8502 fold (build1 (IMAGPART_EXPR, type,
8503 TREE_OPERAND (arg0, 0))),
8504 fold (build1 (IMAGPART_EXPR, type,
8505 TREE_OPERAND (arg0, 1)))));
8508 /* Pull arithmetic ops out of the CLEANUP_POINT_EXPR where
8510 case CLEANUP_POINT_EXPR:
8511 if (! has_cleanups (arg0))
8512 return TREE_OPERAND (t, 0);
8515 enum tree_code code0 = TREE_CODE (arg0);
8516 int kind0 = TREE_CODE_CLASS (code0);
8517 tree arg00 = TREE_OPERAND (arg0, 0);
8520 if (kind0 == '1' || code0 == TRUTH_NOT_EXPR)
8521 return fold (build1 (code0, type,
8522 fold (build1 (CLEANUP_POINT_EXPR,
8523 TREE_TYPE (arg00), arg00))));
8525 if (kind0 == '<' || kind0 == '2'
8526 || code0 == TRUTH_ANDIF_EXPR || code0 == TRUTH_ORIF_EXPR
8527 || code0 == TRUTH_AND_EXPR || code0 == TRUTH_OR_EXPR
8528 || code0 == TRUTH_XOR_EXPR)
8530 arg01 = TREE_OPERAND (arg0, 1);
8532 if (TREE_CONSTANT (arg00)
8533 || ((code0 == TRUTH_ANDIF_EXPR || code0 == TRUTH_ORIF_EXPR)
8534 && ! has_cleanups (arg00)))
8535 return fold (build2 (code0, type, arg00,
8536 fold (build1 (CLEANUP_POINT_EXPR,
8537 TREE_TYPE (arg01), arg01))));
8539 if (TREE_CONSTANT (arg01))
8540 return fold (build2 (code0, type,
8541 fold (build1 (CLEANUP_POINT_EXPR,
8542 TREE_TYPE (arg00), arg00)),
8550 /* Check for a built-in function. */
8551 if (TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
8552 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0))
8554 && DECL_BUILT_IN (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
8556 tree tmp = fold_builtin (t);
8564 } /* switch (code) */
8567 #ifdef ENABLE_FOLD_CHECKING
8570 static void fold_checksum_tree (tree, struct md5_ctx *, htab_t);
8571 static void fold_check_failed (tree, tree);
8572 void print_fold_checksum (tree);
8574 /* When --enable-checking=fold, compute a digest of expr before
8575 and after actual fold call to see if fold did not accidentally
8576 change original expr. */
8583 unsigned char checksum_before[16], checksum_after[16];
8586 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
8587 md5_init_ctx (&ctx);
8588 fold_checksum_tree (expr, &ctx, ht);
8589 md5_finish_ctx (&ctx, checksum_before);
8592 ret = fold_1 (expr);
8594 md5_init_ctx (&ctx);
8595 fold_checksum_tree (expr, &ctx, ht);
8596 md5_finish_ctx (&ctx, checksum_after);
8599 if (memcmp (checksum_before, checksum_after, 16))
8600 fold_check_failed (expr, ret);
8606 print_fold_checksum (tree expr)
8609 unsigned char checksum[16], cnt;
8612 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
8613 md5_init_ctx (&ctx);
8614 fold_checksum_tree (expr, &ctx, ht);
8615 md5_finish_ctx (&ctx, checksum);
8617 for (cnt = 0; cnt < 16; ++cnt)
8618 fprintf (stderr, "%02x", checksum[cnt]);
8619 putc ('\n', stderr);
8623 fold_check_failed (tree expr ATTRIBUTE_UNUSED, tree ret ATTRIBUTE_UNUSED)
8625 internal_error ("fold check: original tree changed by fold");
8629 fold_checksum_tree (tree expr, struct md5_ctx *ctx, htab_t ht)
8632 enum tree_code code;
8633 char buf[sizeof (struct tree_decl)];
8636 if (sizeof (struct tree_exp) + 5 * sizeof (tree)
8637 > sizeof (struct tree_decl)
8638 || sizeof (struct tree_type) > sizeof (struct tree_decl))
8642 slot = htab_find_slot (ht, expr, INSERT);
8646 code = TREE_CODE (expr);
8647 if (code == SAVE_EXPR && SAVE_EXPR_NOPLACEHOLDER (expr))
8649 /* Allow SAVE_EXPR_NOPLACEHOLDER flag to be modified. */
8650 memcpy (buf, expr, tree_size (expr));
8652 SAVE_EXPR_NOPLACEHOLDER (expr) = 0;
8654 else if (TREE_CODE_CLASS (code) == 'd' && DECL_ASSEMBLER_NAME_SET_P (expr))
8656 /* Allow DECL_ASSEMBLER_NAME to be modified. */
8657 memcpy (buf, expr, tree_size (expr));
8659 SET_DECL_ASSEMBLER_NAME (expr, NULL);
8661 else if (TREE_CODE_CLASS (code) == 't'
8662 && (TYPE_POINTER_TO (expr) || TYPE_REFERENCE_TO (expr)))
8664 /* Allow TYPE_POINTER_TO and TYPE_REFERENCE_TO to be modified. */
8665 memcpy (buf, expr, tree_size (expr));
8667 TYPE_POINTER_TO (expr) = NULL;
8668 TYPE_REFERENCE_TO (expr) = NULL;
8670 md5_process_bytes (expr, tree_size (expr), ctx);
8671 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
8672 if (TREE_CODE_CLASS (code) != 't' && TREE_CODE_CLASS (code) != 'd')
8673 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
8674 len = TREE_CODE_LENGTH (code);
8675 switch (TREE_CODE_CLASS (code))
8681 md5_process_bytes (TREE_STRING_POINTER (expr),
8682 TREE_STRING_LENGTH (expr), ctx);
8685 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
8686 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
8689 fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
8699 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
8700 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
8703 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
8704 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
8713 case SAVE_EXPR: len = 2; break;
8714 case GOTO_SUBROUTINE_EXPR: len = 0; break;
8715 case RTL_EXPR: len = 0; break;
8716 case WITH_CLEANUP_EXPR: len = 2; break;
8725 for (i = 0; i < len; ++i)
8726 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
8729 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
8730 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
8731 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
8732 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
8733 fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
8734 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
8735 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
8736 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
8737 fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
8738 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
8739 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
8742 if (TREE_CODE (expr) == ENUMERAL_TYPE)
8743 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
8744 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
8745 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
8746 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
8747 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
8748 if (INTEGRAL_TYPE_P (expr)
8749 || SCALAR_FLOAT_TYPE_P (expr))
8751 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
8752 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
8754 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
8755 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
8756 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
8765 /* Perform constant folding and related simplification of initializer
8766 expression EXPR. This behaves identically to "fold" but ignores
8767 potential run-time traps and exceptions that fold must preserve. */
8770 fold_initializer (tree expr)
8772 int saved_signaling_nans = flag_signaling_nans;
8773 int saved_trapping_math = flag_trapping_math;
8774 int saved_trapv = flag_trapv;
8777 flag_signaling_nans = 0;
8778 flag_trapping_math = 0;
8781 result = fold (expr);
8783 flag_signaling_nans = saved_signaling_nans;
8784 flag_trapping_math = saved_trapping_math;
8785 flag_trapv = saved_trapv;
8790 /* Determine if first argument is a multiple of second argument. Return 0 if
8791 it is not, or we cannot easily determined it to be.
8793 An example of the sort of thing we care about (at this point; this routine
8794 could surely be made more general, and expanded to do what the *_DIV_EXPR's
8795 fold cases do now) is discovering that
8797 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
8803 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
8805 This code also handles discovering that
8807 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
8809 is a multiple of 8 so we don't have to worry about dealing with a
8812 Note that we *look* inside a SAVE_EXPR only to determine how it was
8813 calculated; it is not safe for fold to do much of anything else with the
8814 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
8815 at run time. For example, the latter example above *cannot* be implemented
8816 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
8817 evaluation time of the original SAVE_EXPR is not necessarily the same at
8818 the time the new expression is evaluated. The only optimization of this
8819 sort that would be valid is changing
8821 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
8825 SAVE_EXPR (I) * SAVE_EXPR (J)
8827 (where the same SAVE_EXPR (J) is used in the original and the
8828 transformed version). */
8831 multiple_of_p (tree type, tree top, tree bottom)
8833 if (operand_equal_p (top, bottom, 0))
8836 if (TREE_CODE (type) != INTEGER_TYPE)
8839 switch (TREE_CODE (top))
8842 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
8843 || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
8847 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
8848 && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
8851 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
8855 op1 = TREE_OPERAND (top, 1);
8856 /* const_binop may not detect overflow correctly,
8857 so check for it explicitly here. */
8858 if (TYPE_PRECISION (TREE_TYPE (size_one_node))
8859 > TREE_INT_CST_LOW (op1)
8860 && TREE_INT_CST_HIGH (op1) == 0
8861 && 0 != (t1 = fold_convert (type,
8862 const_binop (LSHIFT_EXPR,
8865 && ! TREE_OVERFLOW (t1))
8866 return multiple_of_p (type, t1, bottom);
8871 /* Can't handle conversions from non-integral or wider integral type. */
8872 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
8873 || (TYPE_PRECISION (type)
8874 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
8877 /* .. fall through ... */
8880 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
8883 if (TREE_CODE (bottom) != INTEGER_CST
8884 || (TYPE_UNSIGNED (type)
8885 && (tree_int_cst_sgn (top) < 0
8886 || tree_int_cst_sgn (bottom) < 0)))
8888 return integer_zerop (const_binop (TRUNC_MOD_EXPR,
8896 /* Return true if `t' is known to be non-negative. */
8899 tree_expr_nonnegative_p (tree t)
8901 switch (TREE_CODE (t))
8907 return tree_int_cst_sgn (t) >= 0;
8910 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
8913 if (FLOAT_TYPE_P (TREE_TYPE (t)))
8914 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
8915 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
8917 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
8918 both unsigned and at least 2 bits shorter than the result. */
8919 if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
8920 && TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
8921 && TREE_CODE (TREE_OPERAND (t, 1)) == NOP_EXPR)
8923 tree inner1 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
8924 tree inner2 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0));
8925 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
8926 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
8928 unsigned int prec = MAX (TYPE_PRECISION (inner1),
8929 TYPE_PRECISION (inner2)) + 1;
8930 return prec < TYPE_PRECISION (TREE_TYPE (t));
8936 if (FLOAT_TYPE_P (TREE_TYPE (t)))
8938 /* x * x for floating point x is always non-negative. */
8939 if (operand_equal_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1), 0))
8941 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
8942 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
8945 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
8946 both unsigned and their total bits is shorter than the result. */
8947 if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
8948 && TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
8949 && TREE_CODE (TREE_OPERAND (t, 1)) == NOP_EXPR)
8951 tree inner1 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
8952 tree inner2 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0));
8953 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
8954 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
8955 return TYPE_PRECISION (inner1) + TYPE_PRECISION (inner2)
8956 < TYPE_PRECISION (TREE_TYPE (t));
8960 case TRUNC_DIV_EXPR:
8962 case FLOOR_DIV_EXPR:
8963 case ROUND_DIV_EXPR:
8964 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
8965 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
8967 case TRUNC_MOD_EXPR:
8969 case FLOOR_MOD_EXPR:
8970 case ROUND_MOD_EXPR:
8971 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
8974 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
8975 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
8978 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1))
8979 || tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
8982 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
8983 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
8987 tree inner_type = TREE_TYPE (TREE_OPERAND (t, 0));
8988 tree outer_type = TREE_TYPE (t);
8990 if (TREE_CODE (outer_type) == REAL_TYPE)
8992 if (TREE_CODE (inner_type) == REAL_TYPE)
8993 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
8994 if (TREE_CODE (inner_type) == INTEGER_TYPE)
8996 if (TYPE_UNSIGNED (inner_type))
8998 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
9001 else if (TREE_CODE (outer_type) == INTEGER_TYPE)
9003 if (TREE_CODE (inner_type) == REAL_TYPE)
9004 return tree_expr_nonnegative_p (TREE_OPERAND (t,0));
9005 if (TREE_CODE (inner_type) == INTEGER_TYPE)
9006 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
9007 && TYPE_UNSIGNED (inner_type);
9013 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1))
9014 && tree_expr_nonnegative_p (TREE_OPERAND (t, 2));
9016 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
9018 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
9019 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
9021 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
9022 || tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
9024 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
9026 return tree_expr_nonnegative_p (expr_last (TREE_OPERAND (t, 1)));
9028 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
9029 case NON_LVALUE_EXPR:
9030 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
9032 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
9034 return rtl_expr_nonnegative_p (RTL_EXPR_RTL (t));
9038 tree fndecl = get_callee_fndecl (t);
9039 tree arglist = TREE_OPERAND (t, 1);
9041 && DECL_BUILT_IN (fndecl)
9042 && DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_MD)
9043 switch (DECL_FUNCTION_CODE (fndecl))
9045 #define CASE_BUILTIN_F(BUILT_IN_FN) \
9046 case BUILT_IN_FN: case BUILT_IN_FN##F: case BUILT_IN_FN##L:
9047 #define CASE_BUILTIN_I(BUILT_IN_FN) \
9048 case BUILT_IN_FN: case BUILT_IN_FN##L: case BUILT_IN_FN##LL:
9050 CASE_BUILTIN_F (BUILT_IN_ACOS)
9051 CASE_BUILTIN_F (BUILT_IN_ACOSH)
9052 CASE_BUILTIN_F (BUILT_IN_CABS)
9053 CASE_BUILTIN_F (BUILT_IN_COSH)
9054 CASE_BUILTIN_F (BUILT_IN_ERFC)
9055 CASE_BUILTIN_F (BUILT_IN_EXP)
9056 CASE_BUILTIN_F (BUILT_IN_EXP10)
9057 CASE_BUILTIN_F (BUILT_IN_EXP2)
9058 CASE_BUILTIN_F (BUILT_IN_FABS)
9059 CASE_BUILTIN_F (BUILT_IN_FDIM)
9060 CASE_BUILTIN_F (BUILT_IN_FREXP)
9061 CASE_BUILTIN_F (BUILT_IN_HYPOT)
9062 CASE_BUILTIN_F (BUILT_IN_POW10)
9063 CASE_BUILTIN_I (BUILT_IN_FFS)
9064 CASE_BUILTIN_I (BUILT_IN_PARITY)
9065 CASE_BUILTIN_I (BUILT_IN_POPCOUNT)
9069 CASE_BUILTIN_F (BUILT_IN_SQRT)
9070 /* sqrt(-0.0) is -0.0. */
9071 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (t))))
9073 return tree_expr_nonnegative_p (TREE_VALUE (arglist));
9075 CASE_BUILTIN_F (BUILT_IN_ASINH)
9076 CASE_BUILTIN_F (BUILT_IN_ATAN)
9077 CASE_BUILTIN_F (BUILT_IN_ATANH)
9078 CASE_BUILTIN_F (BUILT_IN_CBRT)
9079 CASE_BUILTIN_F (BUILT_IN_CEIL)
9080 CASE_BUILTIN_F (BUILT_IN_ERF)
9081 CASE_BUILTIN_F (BUILT_IN_EXPM1)
9082 CASE_BUILTIN_F (BUILT_IN_FLOOR)
9083 CASE_BUILTIN_F (BUILT_IN_FMOD)
9084 CASE_BUILTIN_F (BUILT_IN_LDEXP)
9085 CASE_BUILTIN_F (BUILT_IN_LLRINT)
9086 CASE_BUILTIN_F (BUILT_IN_LLROUND)
9087 CASE_BUILTIN_F (BUILT_IN_LRINT)
9088 CASE_BUILTIN_F (BUILT_IN_LROUND)
9089 CASE_BUILTIN_F (BUILT_IN_MODF)
9090 CASE_BUILTIN_F (BUILT_IN_NEARBYINT)
9091 CASE_BUILTIN_F (BUILT_IN_POW)
9092 CASE_BUILTIN_F (BUILT_IN_RINT)
9093 CASE_BUILTIN_F (BUILT_IN_ROUND)
9094 CASE_BUILTIN_F (BUILT_IN_SIGNBIT)
9095 CASE_BUILTIN_F (BUILT_IN_SINH)
9096 CASE_BUILTIN_F (BUILT_IN_TANH)
9097 CASE_BUILTIN_F (BUILT_IN_TRUNC)
9098 /* True if the 1st argument is nonnegative. */
9099 return tree_expr_nonnegative_p (TREE_VALUE (arglist));
9101 CASE_BUILTIN_F (BUILT_IN_FMAX)
9102 /* True if the 1st OR 2nd arguments are nonnegative. */
9103 return tree_expr_nonnegative_p (TREE_VALUE (arglist))
9104 || tree_expr_nonnegative_p (TREE_VALUE (TREE_CHAIN (arglist)));
9106 CASE_BUILTIN_F (BUILT_IN_FMIN)
9107 /* True if the 1st AND 2nd arguments are nonnegative. */
9108 return tree_expr_nonnegative_p (TREE_VALUE (arglist))
9109 && tree_expr_nonnegative_p (TREE_VALUE (TREE_CHAIN (arglist)));
9111 CASE_BUILTIN_F (BUILT_IN_COPYSIGN)
9112 /* True if the 2nd argument is nonnegative. */
9113 return tree_expr_nonnegative_p (TREE_VALUE (TREE_CHAIN (arglist)));
9117 #undef CASE_BUILTIN_F
9118 #undef CASE_BUILTIN_I
9122 /* ... fall through ... */
9125 if (truth_value_p (TREE_CODE (t)))
9126 /* Truth values evaluate to 0 or 1, which is nonnegative. */
9130 /* We don't know sign of `t', so be conservative and return false. */
9134 /* Return true when T is an address and is known to be nonzero.
9135 For floating point we further ensure that T is not denormal.
9136 Similar logic is present in nonzero_address in rtlanal.h */
9139 tree_expr_nonzero_p (tree t)
9141 tree type = TREE_TYPE (t);
9143 /* Doing something useful for floating point would need more work. */
9144 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
9147 switch (TREE_CODE (t))
9150 if (!TYPE_UNSIGNED (type) && !flag_wrapv)
9151 return tree_expr_nonzero_p (TREE_OPERAND (t, 0));
9154 return !integer_zerop (t);
9157 if (!TYPE_UNSIGNED (type) && !flag_wrapv)
9159 /* With the presence of negative values it is hard
9160 to say something. */
9161 if (!tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
9162 || !tree_expr_nonnegative_p (TREE_OPERAND (t, 1)))
9164 /* One of operands must be positive and the other non-negative. */
9165 return (tree_expr_nonzero_p (TREE_OPERAND (t, 0))
9166 || tree_expr_nonzero_p (TREE_OPERAND (t, 1)));
9171 if (!TYPE_UNSIGNED (type) && !flag_wrapv)
9173 return (tree_expr_nonzero_p (TREE_OPERAND (t, 0))
9174 && tree_expr_nonzero_p (TREE_OPERAND (t, 1)));
9180 tree inner_type = TREE_TYPE (TREE_OPERAND (t, 0));
9181 tree outer_type = TREE_TYPE (t);
9183 return (TYPE_PRECISION (inner_type) >= TYPE_PRECISION (outer_type)
9184 && tree_expr_nonzero_p (TREE_OPERAND (t, 0)));
9189 /* Weak declarations may link to NULL. */
9190 if (DECL_P (TREE_OPERAND (t, 0)))
9191 return !DECL_WEAK (TREE_OPERAND (t, 0));
9192 /* Constants and all other cases are never weak. */
9196 return (tree_expr_nonzero_p (TREE_OPERAND (t, 1))
9197 && tree_expr_nonzero_p (TREE_OPERAND (t, 2)));
9200 return (tree_expr_nonzero_p (TREE_OPERAND (t, 0))
9201 && tree_expr_nonzero_p (TREE_OPERAND (t, 1)));
9204 if (tree_expr_nonzero_p (TREE_OPERAND (t, 0)))
9206 /* When both operands are nonzero, then MAX must be too. */
9207 if (tree_expr_nonzero_p (TREE_OPERAND (t, 1)))
9210 /* MAX where operand 0 is positive is positive. */
9211 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
9213 /* MAX where operand 1 is positive is positive. */
9214 else if (tree_expr_nonzero_p (TREE_OPERAND (t, 1))
9215 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1)))
9222 return tree_expr_nonzero_p (TREE_OPERAND (t, 1));
9225 case NON_LVALUE_EXPR:
9226 return tree_expr_nonzero_p (TREE_OPERAND (t, 0));
9229 return tree_expr_nonzero_p (TREE_OPERAND (t, 1))
9230 || tree_expr_nonzero_p (TREE_OPERAND (t, 0));
9238 /* Return true if `r' is known to be non-negative.
9239 Only handles constants at the moment. */
9242 rtl_expr_nonnegative_p (rtx r)
9244 switch (GET_CODE (r))
9247 return INTVAL (r) >= 0;
9250 if (GET_MODE (r) == VOIDmode)
9251 return CONST_DOUBLE_HIGH (r) >= 0;
9259 units = CONST_VECTOR_NUNITS (r);
9261 for (i = 0; i < units; ++i)
9263 elt = CONST_VECTOR_ELT (r, i);
9264 if (!rtl_expr_nonnegative_p (elt))
9273 /* These are always nonnegative. */
9282 /* See if we are applying CODE, a relational to the highest or lowest
9283 possible integer of TYPE. If so, then the result is a compile
9287 fold_relational_hi_lo (enum tree_code *code_p, const tree type, tree *op0_p,
9292 enum tree_code code = *code_p;
9293 int width = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (op1)));
9295 if (TREE_CODE (op1) == INTEGER_CST
9296 && ! TREE_CONSTANT_OVERFLOW (op1)
9297 && width <= HOST_BITS_PER_WIDE_INT
9298 && (INTEGRAL_TYPE_P (TREE_TYPE (op1))
9299 || POINTER_TYPE_P (TREE_TYPE (op1))))
9301 unsigned HOST_WIDE_INT signed_max;
9302 unsigned HOST_WIDE_INT max, min;
9304 signed_max = ((unsigned HOST_WIDE_INT) 1 << (width - 1)) - 1;
9306 if (TYPE_UNSIGNED (TREE_TYPE (op1)))
9308 max = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
9314 min = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
9317 if (TREE_INT_CST_HIGH (op1) == 0
9318 && TREE_INT_CST_LOW (op1) == max)
9322 return omit_one_operand (type, integer_zero_node, op0);
9328 return omit_one_operand (type, integer_one_node, op0);
9334 /* The GE_EXPR and LT_EXPR cases above are not normally
9335 reached because of previous transformations. */
9340 else if (TREE_INT_CST_HIGH (op1) == 0
9341 && TREE_INT_CST_LOW (op1) == max - 1)
9346 *op1_p = const_binop (PLUS_EXPR, op1, integer_one_node, 0);
9350 *op1_p = const_binop (PLUS_EXPR, op1, integer_one_node, 0);
9355 else if (TREE_INT_CST_HIGH (op1) == (min ? -1 : 0)
9356 && TREE_INT_CST_LOW (op1) == min)
9360 return omit_one_operand (type, integer_zero_node, op0);
9367 return omit_one_operand (type, integer_one_node, op0);
9376 else if (TREE_INT_CST_HIGH (op1) == (min ? -1 : 0)
9377 && TREE_INT_CST_LOW (op1) == min + 1)
9382 *op1_p = const_binop (MINUS_EXPR, op1, integer_one_node, 0);
9386 *op1_p = const_binop (MINUS_EXPR, op1, integer_one_node, 0);
9392 else if (TREE_INT_CST_HIGH (op1) == 0
9393 && TREE_INT_CST_LOW (op1) == signed_max
9394 && TYPE_UNSIGNED (TREE_TYPE (op1))
9395 /* signed_type does not work on pointer types. */
9396 && INTEGRAL_TYPE_P (TREE_TYPE (op1)))
9398 /* The following case also applies to X < signed_max+1
9399 and X >= signed_max+1 because previous transformations. */
9400 if (code == LE_EXPR || code == GT_EXPR)
9402 tree st0, st1, exp, retval;
9403 st0 = lang_hooks.types.signed_type (TREE_TYPE (op0));
9404 st1 = lang_hooks.types.signed_type (TREE_TYPE (op1));
9406 exp = build2 (code == LE_EXPR ? GE_EXPR: LT_EXPR,
9408 fold_convert (st0, op0),
9409 fold_convert (st1, integer_zero_node));
9412 = nondestructive_fold_binary_to_constant (TREE_CODE (exp),
9414 TREE_OPERAND (exp, 0),
9415 TREE_OPERAND (exp, 1));
9417 /* If we are in gimple form, then returning EXP would create
9418 non-gimple expressions. Clearing it is safe and insures
9419 we do not allow a non-gimple expression to escape. */
9423 return (retval ? retval : exp);
9432 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
9433 attempt to fold the expression to a constant without modifying TYPE,
9436 If the expression could be simplified to a constant, then return
9437 the constant. If the expression would not be simplified to a
9438 constant, then return NULL_TREE.
9440 Note this is primarily designed to be called after gimplification
9441 of the tree structures and when at least one operand is a constant.
9442 As a result of those simplifying assumptions this routine is far
9443 simpler than the generic fold routine. */
9446 nondestructive_fold_binary_to_constant (enum tree_code code, tree type,
9454 /* If this is a commutative operation, and ARG0 is a constant, move it
9455 to ARG1 to reduce the number of tests below. */
9456 if (commutative_tree_code (code)
9457 && (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST))
9464 /* If either operand is a complex type, extract its real component. */
9465 if (TREE_CODE (op0) == COMPLEX_CST)
9466 subop0 = TREE_REALPART (op0);
9470 if (TREE_CODE (op1) == COMPLEX_CST)
9471 subop1 = TREE_REALPART (op1);
9475 /* Note if either argument is not a real or integer constant.
9476 With a few exceptions, simplification is limited to cases
9477 where both arguments are constants. */
9478 if ((TREE_CODE (subop0) != INTEGER_CST
9479 && TREE_CODE (subop0) != REAL_CST)
9480 || (TREE_CODE (subop1) != INTEGER_CST
9481 && TREE_CODE (subop1) != REAL_CST))
9487 /* (plus (address) (const_int)) is a constant. */
9488 if (TREE_CODE (op0) == PLUS_EXPR
9489 && TREE_CODE (op1) == INTEGER_CST
9490 && (TREE_CODE (TREE_OPERAND (op0, 0)) == ADDR_EXPR
9491 || (TREE_CODE (TREE_OPERAND (op0, 0)) == NOP_EXPR
9492 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (op0, 0), 0))
9494 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
9496 return build2 (PLUS_EXPR, type, TREE_OPERAND (op0, 0),
9497 const_binop (PLUS_EXPR, op1,
9498 TREE_OPERAND (op0, 1), 0));
9506 /* Both arguments are constants. Simplify. */
9507 tem = const_binop (code, op0, op1, 0);
9508 if (tem != NULL_TREE)
9510 /* The return value should always have the same type as
9511 the original expression. */
9512 if (TREE_TYPE (tem) != type)
9513 tem = fold_convert (type, tem);
9520 /* Fold &x - &x. This can happen from &x.foo - &x.
9521 This is unsafe for certain floats even in non-IEEE formats.
9522 In IEEE, it is unsafe because it does wrong for NaNs.
9523 Also note that operand_equal_p is always false if an
9524 operand is volatile. */
9525 if (! FLOAT_TYPE_P (type) && operand_equal_p (op0, op1, 0))
9526 return fold_convert (type, integer_zero_node);
9532 /* Special case multiplication or bitwise AND where one argument
9534 if (! FLOAT_TYPE_P (type) && integer_zerop (op1))
9535 return omit_one_operand (type, op1, op0);
9537 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (op0)))
9538 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op0)))
9539 && real_zerop (op1))
9540 return omit_one_operand (type, op1, op0);
9545 /* Special case when we know the result will be all ones. */
9546 if (integer_all_onesp (op1))
9547 return omit_one_operand (type, op1, op0);
9551 case TRUNC_DIV_EXPR:
9552 case ROUND_DIV_EXPR:
9553 case FLOOR_DIV_EXPR:
9555 case EXACT_DIV_EXPR:
9556 case TRUNC_MOD_EXPR:
9557 case ROUND_MOD_EXPR:
9558 case FLOOR_MOD_EXPR:
9561 /* Division by zero is undefined. */
9562 if (integer_zerop (op1))
9565 if (TREE_CODE (op1) == REAL_CST
9566 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (op1)))
9567 && real_zerop (op1))
9573 if (INTEGRAL_TYPE_P (type)
9574 && operand_equal_p (op1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
9575 return omit_one_operand (type, op1, op0);
9580 if (INTEGRAL_TYPE_P (type)
9581 && TYPE_MAX_VALUE (type)
9582 && operand_equal_p (op1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
9583 return omit_one_operand (type, op1, op0);
9588 /* Optimize -1 >> x for arithmetic right shifts. */
9589 if (integer_all_onesp (op0) && ! TYPE_UNSIGNED (type))
9590 return omit_one_operand (type, op0, op1);
9591 /* ... fall through ... */
9594 if (integer_zerop (op0))
9595 return omit_one_operand (type, op0, op1);
9597 /* Since negative shift count is not well-defined, don't
9598 try to compute it in the compiler. */
9599 if (TREE_CODE (op1) == INTEGER_CST && tree_int_cst_sgn (op1) < 0)
9606 /* -1 rotated either direction by any amount is still -1. */
9607 if (integer_all_onesp (op0))
9608 return omit_one_operand (type, op0, op1);
9610 /* 0 rotated either direction by any amount is still zero. */
9611 if (integer_zerop (op0))
9612 return omit_one_operand (type, op0, op1);
9618 return build_complex (type, op0, op1);
9627 /* If one arg is a real or integer constant, put it last. */
9628 if ((TREE_CODE (op0) == INTEGER_CST
9629 && TREE_CODE (op1) != INTEGER_CST)
9630 || (TREE_CODE (op0) == REAL_CST
9631 && TREE_CODE (op0) != REAL_CST))
9638 code = swap_tree_comparison (code);
9641 /* Change X >= C to X > (C - 1) and X < C to X <= (C - 1) if C > 0.
9642 This transformation affects the cases which are handled in later
9643 optimizations involving comparisons with non-negative constants. */
9644 if (TREE_CODE (op1) == INTEGER_CST
9645 && TREE_CODE (op0) != INTEGER_CST
9646 && tree_int_cst_sgn (op1) > 0)
9652 op1 = const_binop (MINUS_EXPR, op1, integer_one_node, 0);
9657 op1 = const_binop (MINUS_EXPR, op1, integer_one_node, 0);
9665 tem = fold_relational_hi_lo (&code, type, &op0, &op1);
9672 return fold_relational_const (code, type, op0, op1);
9675 /* This could probably be handled. */
9678 case TRUTH_AND_EXPR:
9679 /* If second arg is constant zero, result is zero, but first arg
9680 must be evaluated. */
9681 if (integer_zerop (op1))
9682 return omit_one_operand (type, op1, op0);
9683 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
9684 case will be handled here. */
9685 if (integer_zerop (op0))
9686 return omit_one_operand (type, op0, op1);
9687 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
9688 return constant_boolean_node (true, type);
9692 /* If second arg is constant true, result is true, but we must
9693 evaluate first arg. */
9694 if (TREE_CODE (op1) == INTEGER_CST && ! integer_zerop (op1))
9695 return omit_one_operand (type, op1, op0);
9696 /* Likewise for first arg, but note this only occurs here for
9698 if (TREE_CODE (op0) == INTEGER_CST && ! integer_zerop (op0))
9699 return omit_one_operand (type, op0, op1);
9700 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
9701 return constant_boolean_node (false, type);
9704 case TRUTH_XOR_EXPR:
9705 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
9707 int x = ! integer_zerop (op0) ^ ! integer_zerop (op1);
9708 return constant_boolean_node (x, type);
9717 /* Given the components of a unary expression CODE, TYPE and OP0,
9718 attempt to fold the expression to a constant without modifying
9721 If the expression could be simplified to a constant, then return
9722 the constant. If the expression would not be simplified to a
9723 constant, then return NULL_TREE.
9725 Note this is primarily designed to be called after gimplification
9726 of the tree structures and when op0 is a constant. As a result
9727 of those simplifying assumptions this routine is far simpler than
9728 the generic fold routine. */
9731 nondestructive_fold_unary_to_constant (enum tree_code code, tree type,
9734 /* Make sure we have a suitable constant argument. */
9735 if (code == NOP_EXPR || code == FLOAT_EXPR || code == CONVERT_EXPR)
9739 if (TREE_CODE (op0) == COMPLEX_CST)
9740 subop = TREE_REALPART (op0);
9744 if (TREE_CODE (subop) != INTEGER_CST && TREE_CODE (subop) != REAL_CST)
9753 case FIX_TRUNC_EXPR:
9754 case FIX_FLOOR_EXPR:
9756 return fold_convert_const (code, type, op0);
9759 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST)
9760 return fold_negate_const (op0, type);
9765 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST)
9766 return fold_abs_const (op0, type);
9771 if (TREE_CODE (op0) == INTEGER_CST)
9772 return fold_not_const (op0, type);
9777 if (TREE_CODE (op0) == COMPLEX_CST)
9778 return TREE_REALPART (op0);
9783 if (TREE_CODE (op0) == COMPLEX_CST)
9784 return TREE_IMAGPART (op0);
9789 if (TREE_CODE (op0) == COMPLEX_CST
9790 && TREE_CODE (TREE_TYPE (op0)) == COMPLEX_TYPE)
9791 return build_complex (type, TREE_REALPART (op0),
9792 negate_expr (TREE_IMAGPART (op0)));
9800 /* If EXP represents referencing an element in a constant string
9801 (either via pointer arithmetic or array indexing), return the
9802 tree representing the value accessed, otherwise return NULL. */
9805 fold_read_from_constant_string (tree exp)
9807 if (TREE_CODE (exp) == INDIRECT_REF || TREE_CODE (exp) == ARRAY_REF)
9809 tree exp1 = TREE_OPERAND (exp, 0);
9813 if (TREE_CODE (exp) == INDIRECT_REF)
9815 string = string_constant (exp1, &index);
9819 tree domain = TYPE_DOMAIN (TREE_TYPE (exp1));
9820 tree low_bound = domain ? TYPE_MIN_VALUE (domain) : integer_zero_node;
9821 index = fold_convert (sizetype, TREE_OPERAND (exp, 1));
9823 /* Optimize the special-case of a zero lower bound.
9825 We convert the low_bound to sizetype to avoid some problems
9826 with constant folding. (E.g. suppose the lower bound is 1,
9827 and its mode is QI. Without the conversion,l (ARRAY
9828 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
9829 +INDEX), which becomes (ARRAY+255+INDEX). Opps!) */
9830 if (! integer_zerop (low_bound))
9831 index = size_diffop (index, fold_convert (sizetype, low_bound));
9837 && TREE_CODE (string) == STRING_CST
9838 && TREE_CODE (index) == INTEGER_CST
9839 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
9840 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
9842 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
9843 return fold_convert (TREE_TYPE (exp),
9844 build_int_2 ((TREE_STRING_POINTER (string)
9845 [TREE_INT_CST_LOW (index)]), 0));
9850 /* Return the tree for neg (ARG0) when ARG0 is known to be either
9851 an integer constant or real constant.
9853 TYPE is the type of the result. */
9856 fold_negate_const (tree arg0, tree type)
9860 if (TREE_CODE (arg0) == INTEGER_CST)
9862 unsigned HOST_WIDE_INT low;
9864 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
9865 TREE_INT_CST_HIGH (arg0),
9867 t = build_int_2 (low, high);
9868 TREE_TYPE (t) = type;
9870 = (TREE_OVERFLOW (arg0)
9871 | force_fit_type (t, overflow && !TYPE_UNSIGNED (type)));
9872 TREE_CONSTANT_OVERFLOW (t)
9873 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg0);
9875 else if (TREE_CODE (arg0) == REAL_CST)
9876 t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
9877 #ifdef ENABLE_CHECKING
9885 /* Return the tree for abs (ARG0) when ARG0 is known to be either
9886 an integer constant or real constant.
9888 TYPE is the type of the result. */
9891 fold_abs_const (tree arg0, tree type)
9895 if (TREE_CODE (arg0) == INTEGER_CST)
9897 /* If the value is unsigned, then the absolute value is
9898 the same as the ordinary value. */
9899 if (TYPE_UNSIGNED (type))
9901 /* Similarly, if the value is non-negative. */
9902 else if (INT_CST_LT (integer_minus_one_node, arg0))
9904 /* If the value is negative, then the absolute value is
9908 unsigned HOST_WIDE_INT low;
9910 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
9911 TREE_INT_CST_HIGH (arg0),
9913 t = build_int_2 (low, high);
9914 TREE_TYPE (t) = type;
9916 = (TREE_OVERFLOW (arg0)
9917 | force_fit_type (t, overflow));
9918 TREE_CONSTANT_OVERFLOW (t)
9919 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg0);
9923 else if (TREE_CODE (arg0) == REAL_CST)
9925 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
9926 return build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
9930 #ifdef ENABLE_CHECKING
9938 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
9939 constant. TYPE is the type of the result. */
9942 fold_not_const (tree arg0, tree type)
9946 if (TREE_CODE (arg0) == INTEGER_CST)
9948 t = build_int_2 (~ TREE_INT_CST_LOW (arg0),
9949 ~ TREE_INT_CST_HIGH (arg0));
9950 TREE_TYPE (t) = type;
9951 force_fit_type (t, 0);
9952 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg0);
9953 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (arg0);
9955 #ifdef ENABLE_CHECKING
9963 /* Given CODE, a relational operator, the target type, TYPE and two
9964 constant operands OP0 and OP1, return the result of the
9965 relational operation. If the result is not a compile time
9966 constant, then return NULL_TREE. */
9969 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
9973 /* From here on, the only cases we handle are when the result is
9974 known to be a constant.
9976 To compute GT, swap the arguments and do LT.
9977 To compute GE, do LT and invert the result.
9978 To compute LE, swap the arguments, do LT and invert the result.
9979 To compute NE, do EQ and invert the result.
9981 Therefore, the code below must handle only EQ and LT. */
9983 if (code == LE_EXPR || code == GT_EXPR)
9988 code = swap_tree_comparison (code);
9991 /* Note that it is safe to invert for real values here because we
9992 will check below in the one case that it matters. */
9995 if (code == NE_EXPR || code == GE_EXPR)
9998 code = invert_tree_comparison (code, false);
10001 /* Compute a result for LT or EQ if args permit;
10002 Otherwise return T. */
10003 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
10005 if (code == EQ_EXPR)
10006 result = tree_int_cst_equal (op0, op1);
10007 else if (TYPE_UNSIGNED (TREE_TYPE (op0)))
10008 result = INT_CST_LT_UNSIGNED (op0, op1);
10010 result = INT_CST_LT (op0, op1);
10013 else if (code == EQ_EXPR && !TREE_SIDE_EFFECTS (op0)
10014 && integer_zerop (op1) && tree_expr_nonzero_p (op0))
10017 /* Two real constants can be compared explicitly. */
10018 else if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
10020 /* If either operand is a NaN, the result is false with two
10021 exceptions: First, an NE_EXPR is true on NaNs, but that case
10022 is already handled correctly since we will be inverting the
10023 result for NE_EXPR. Second, if we had inverted a LE_EXPR
10024 or a GE_EXPR into a LT_EXPR, we must return true so that it
10025 will be inverted into false. */
10027 if (REAL_VALUE_ISNAN (TREE_REAL_CST (op0))
10028 || REAL_VALUE_ISNAN (TREE_REAL_CST (op1)))
10029 result = invert && code == LT_EXPR;
10031 else if (code == EQ_EXPR)
10032 result = REAL_VALUES_EQUAL (TREE_REAL_CST (op0),
10033 TREE_REAL_CST (op1));
10035 result = REAL_VALUES_LESS (TREE_REAL_CST (op0),
10036 TREE_REAL_CST (op1));
10043 return constant_boolean_node (result, type);
10046 /* Build an expression for the address of T. Folds away INDIRECT_REF to
10047 avoid confusing the gimplify process. */
10050 build_fold_addr_expr_with_type (tree t, tree ptrtype)
10052 if (TREE_CODE (t) == INDIRECT_REF)
10054 t = TREE_OPERAND (t, 0);
10055 if (TREE_TYPE (t) != ptrtype)
10056 t = build1 (NOP_EXPR, ptrtype, t);
10061 while (TREE_CODE (base) == COMPONENT_REF
10062 || TREE_CODE (base) == ARRAY_REF)
10063 base = TREE_OPERAND (base, 0);
10065 TREE_ADDRESSABLE (base) = 1;
10067 t = build1 (ADDR_EXPR, ptrtype, t);
10074 build_fold_addr_expr (tree t)
10076 return build_fold_addr_expr_with_type (t, build_pointer_type (TREE_TYPE (t)));
10079 /* Builds an expression for an indirection through T, simplifying some
10083 build_fold_indirect_ref (tree t)
10085 tree type = TREE_TYPE (TREE_TYPE (t));
10090 if (TREE_CODE (sub) == ADDR_EXPR)
10092 tree op = TREE_OPERAND (sub, 0);
10093 tree optype = TREE_TYPE (op);
10095 if (lang_hooks.types_compatible_p (type, optype))
10097 /* *(foo *)&fooarray => fooarray[0] */
10098 else if (TREE_CODE (optype) == ARRAY_TYPE
10099 && lang_hooks.types_compatible_p (type, TREE_TYPE (optype)))
10100 return build2 (ARRAY_REF, type, op, size_zero_node);
10103 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
10104 subtype = TREE_TYPE (sub);
10105 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
10106 && lang_hooks.types_compatible_p (type, TREE_TYPE (TREE_TYPE (subtype))))
10108 sub = build_fold_indirect_ref (sub);
10109 return build2 (ARRAY_REF, type, sub, size_zero_node);
10112 return build1 (INDIRECT_REF, type, t);
10115 #include "gt-fold-const.h"