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,
3 1999, 2000, 2001 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
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. */
56 static void encode PARAMS ((HOST_WIDE_INT *,
57 unsigned HOST_WIDE_INT,
59 static void decode PARAMS ((HOST_WIDE_INT *,
60 unsigned HOST_WIDE_INT *,
62 static tree negate_expr PARAMS ((tree));
63 static tree split_tree PARAMS ((tree, enum tree_code, tree *, tree *,
65 static tree associate_trees PARAMS ((tree, tree, enum tree_code, tree));
66 static tree int_const_binop PARAMS ((enum tree_code, tree, tree, int, int));
67 static void const_binop_1 PARAMS ((PTR));
68 static tree const_binop PARAMS ((enum tree_code, tree, tree, int));
69 static void fold_convert_1 PARAMS ((PTR));
70 static tree fold_convert PARAMS ((tree, tree));
71 static enum tree_code invert_tree_comparison PARAMS ((enum tree_code));
72 static enum tree_code swap_tree_comparison PARAMS ((enum tree_code));
73 static int truth_value_p PARAMS ((enum tree_code));
74 static int operand_equal_for_comparison_p PARAMS ((tree, tree, tree));
75 static int twoval_comparison_p PARAMS ((tree, tree *, tree *, int *));
76 static tree eval_subst PARAMS ((tree, tree, tree, tree, tree));
77 static tree omit_one_operand PARAMS ((tree, tree, tree));
78 static tree pedantic_omit_one_operand PARAMS ((tree, tree, tree));
79 static tree distribute_bit_expr PARAMS ((enum tree_code, tree, tree, tree));
80 static tree make_bit_field_ref PARAMS ((tree, tree, int, int, int));
81 static tree optimize_bit_field_compare PARAMS ((enum tree_code, tree,
83 static tree decode_field_reference PARAMS ((tree, HOST_WIDE_INT *,
85 enum machine_mode *, int *,
86 int *, tree *, tree *));
87 static int all_ones_mask_p PARAMS ((tree, int));
88 static int simple_operand_p PARAMS ((tree));
89 static tree range_binop PARAMS ((enum tree_code, tree, tree, int,
91 static tree make_range PARAMS ((tree, int *, tree *, tree *));
92 static tree build_range_check PARAMS ((tree, tree, int, tree, tree));
93 static int merge_ranges PARAMS ((int *, tree *, tree *, int, tree, tree,
95 static tree fold_range_test PARAMS ((tree));
96 static tree unextend PARAMS ((tree, int, int, tree));
97 static tree fold_truthop PARAMS ((enum tree_code, tree, tree, tree));
98 static tree optimize_minmax_comparison PARAMS ((tree));
99 static tree extract_muldiv PARAMS ((tree, tree, enum tree_code, tree));
100 static tree strip_compound_expr PARAMS ((tree, tree));
101 static int multiple_of_p PARAMS ((tree, tree, tree));
102 static tree constant_boolean_node PARAMS ((int, tree));
103 static int count_cond PARAMS ((tree, int));
104 static tree fold_binary_op_with_conditional_arg
105 PARAMS ((enum tree_code, tree, tree, tree, int));
108 #define BRANCH_COST 1
111 #if defined(HOST_EBCDIC)
112 /* bit 8 is significant in EBCDIC */
113 #define CHARMASK 0xff
115 #define CHARMASK 0x7f
118 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
119 overflow. Suppose A, B and SUM have the same respective signs as A1, B1,
120 and SUM1. Then this yields nonzero if overflow occurred during the
123 Overflow occurs if A and B have the same sign, but A and SUM differ in
124 sign. Use `^' to test whether signs differ, and `< 0' to isolate the
126 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
128 /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
129 We do that by representing the two-word integer in 4 words, with only
130 HOST_BITS_PER_WIDE_INT / 2 bits stored in each word, as a positive
131 number. The value of the word is LOWPART + HIGHPART * BASE. */
134 ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)) - 1))
135 #define HIGHPART(x) \
136 ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT / 2)
137 #define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT / 2)
139 /* Unpack a two-word integer into 4 words.
140 LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
141 WORDS points to the array of HOST_WIDE_INTs. */
144 encode (words, low, hi)
145 HOST_WIDE_INT *words;
146 unsigned HOST_WIDE_INT low;
149 words[0] = LOWPART (low);
150 words[1] = HIGHPART (low);
151 words[2] = LOWPART (hi);
152 words[3] = HIGHPART (hi);
155 /* Pack an array of 4 words into a two-word integer.
156 WORDS points to the array of words.
157 The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces. */
160 decode (words, low, hi)
161 HOST_WIDE_INT *words;
162 unsigned HOST_WIDE_INT *low;
165 *low = words[0] + words[1] * BASE;
166 *hi = words[2] + words[3] * BASE;
169 /* Make the integer constant T valid for its type by setting to 0 or 1 all
170 the bits in the constant that don't belong in the type.
172 Return 1 if a signed overflow occurs, 0 otherwise. If OVERFLOW is
173 nonzero, a signed overflow has already occurred in calculating T, so
176 Make the real constant T valid for its type by calling CHECK_FLOAT_VALUE,
180 force_fit_type (t, overflow)
184 unsigned HOST_WIDE_INT low;
188 if (TREE_CODE (t) == REAL_CST)
190 #ifdef CHECK_FLOAT_VALUE
191 CHECK_FLOAT_VALUE (TYPE_MODE (TREE_TYPE (t)), TREE_REAL_CST (t),
197 else if (TREE_CODE (t) != INTEGER_CST)
200 low = TREE_INT_CST_LOW (t);
201 high = TREE_INT_CST_HIGH (t);
203 if (POINTER_TYPE_P (TREE_TYPE (t)))
206 prec = TYPE_PRECISION (TREE_TYPE (t));
208 /* First clear all bits that are beyond the type's precision. */
210 if (prec == 2 * HOST_BITS_PER_WIDE_INT)
212 else if (prec > HOST_BITS_PER_WIDE_INT)
213 TREE_INT_CST_HIGH (t)
214 &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
217 TREE_INT_CST_HIGH (t) = 0;
218 if (prec < HOST_BITS_PER_WIDE_INT)
219 TREE_INT_CST_LOW (t) &= ~((unsigned HOST_WIDE_INT) (-1) << prec);
222 /* Unsigned types do not suffer sign extension or overflow unless they
224 if (TREE_UNSIGNED (TREE_TYPE (t))
225 && ! (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
226 && TYPE_IS_SIZETYPE (TREE_TYPE (t))))
229 /* If the value's sign bit is set, extend the sign. */
230 if (prec != 2 * HOST_BITS_PER_WIDE_INT
231 && (prec > HOST_BITS_PER_WIDE_INT
232 ? 0 != (TREE_INT_CST_HIGH (t)
234 << (prec - HOST_BITS_PER_WIDE_INT - 1)))
235 : 0 != (TREE_INT_CST_LOW (t)
236 & ((unsigned HOST_WIDE_INT) 1 << (prec - 1)))))
238 /* Value is negative:
239 set to 1 all the bits that are outside this type's precision. */
240 if (prec > HOST_BITS_PER_WIDE_INT)
241 TREE_INT_CST_HIGH (t)
242 |= ((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
245 TREE_INT_CST_HIGH (t) = -1;
246 if (prec < HOST_BITS_PER_WIDE_INT)
247 TREE_INT_CST_LOW (t) |= ((unsigned HOST_WIDE_INT) (-1) << prec);
251 /* Return nonzero if signed overflow occurred. */
253 ((overflow | (low ^ TREE_INT_CST_LOW (t)) | (high ^ TREE_INT_CST_HIGH (t)))
257 /* Add two doubleword integers with doubleword result.
258 Each argument is given as two `HOST_WIDE_INT' pieces.
259 One argument is L1 and H1; the other, L2 and H2.
260 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
263 add_double (l1, h1, l2, h2, lv, hv)
264 unsigned HOST_WIDE_INT l1, l2;
265 HOST_WIDE_INT h1, h2;
266 unsigned HOST_WIDE_INT *lv;
269 unsigned HOST_WIDE_INT l;
273 h = h1 + h2 + (l < l1);
277 return OVERFLOW_SUM_SIGN (h1, h2, h);
280 /* Negate a doubleword integer with doubleword result.
281 Return nonzero if the operation overflows, assuming it's signed.
282 The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
283 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
286 neg_double (l1, h1, lv, hv)
287 unsigned HOST_WIDE_INT l1;
289 unsigned HOST_WIDE_INT *lv;
296 return (*hv & h1) < 0;
306 /* Multiply two doubleword integers with doubleword result.
307 Return nonzero if the operation overflows, assuming it's signed.
308 Each argument is given as two `HOST_WIDE_INT' pieces.
309 One argument is L1 and H1; the other, L2 and H2.
310 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
313 mul_double (l1, h1, l2, h2, lv, hv)
314 unsigned HOST_WIDE_INT l1, l2;
315 HOST_WIDE_INT h1, h2;
316 unsigned HOST_WIDE_INT *lv;
319 HOST_WIDE_INT arg1[4];
320 HOST_WIDE_INT arg2[4];
321 HOST_WIDE_INT prod[4 * 2];
322 register unsigned HOST_WIDE_INT carry;
323 register int i, j, k;
324 unsigned HOST_WIDE_INT toplow, neglow;
325 HOST_WIDE_INT tophigh, neghigh;
327 encode (arg1, l1, h1);
328 encode (arg2, l2, h2);
330 memset ((char *) prod, 0, sizeof prod);
332 for (i = 0; i < 4; i++)
335 for (j = 0; j < 4; j++)
338 /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000. */
339 carry += arg1[i] * arg2[j];
340 /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF. */
342 prod[k] = LOWPART (carry);
343 carry = HIGHPART (carry);
348 decode (prod, lv, hv); /* This ignores prod[4] through prod[4*2-1] */
350 /* Check for overflow by calculating the top half of the answer in full;
351 it should agree with the low half's sign bit. */
352 decode (prod + 4, &toplow, &tophigh);
355 neg_double (l2, h2, &neglow, &neghigh);
356 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
360 neg_double (l1, h1, &neglow, &neghigh);
361 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
363 return (*hv < 0 ? ~(toplow & tophigh) : toplow | tophigh) != 0;
366 /* Shift the doubleword integer in L1, H1 left by COUNT places
367 keeping only PREC bits of result.
368 Shift right if COUNT is negative.
369 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
370 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
373 lshift_double (l1, h1, count, prec, lv, hv, arith)
374 unsigned HOST_WIDE_INT l1;
375 HOST_WIDE_INT h1, count;
377 unsigned HOST_WIDE_INT *lv;
383 rshift_double (l1, h1, -count, prec, lv, hv, arith);
387 #ifdef SHIFT_COUNT_TRUNCATED
388 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));
412 /* Shift the doubleword integer in L1, H1 right by COUNT places
413 keeping only PREC bits of result. COUNT must be positive.
414 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
415 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
418 rshift_double (l1, h1, count, prec, lv, hv, arith)
419 unsigned HOST_WIDE_INT l1;
420 HOST_WIDE_INT h1, count;
421 unsigned int prec ATTRIBUTE_UNUSED;
422 unsigned HOST_WIDE_INT *lv;
426 unsigned HOST_WIDE_INT signmask;
429 ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
432 #ifdef SHIFT_COUNT_TRUNCATED
433 if (SHIFT_COUNT_TRUNCATED)
437 if (count >= 2 * HOST_BITS_PER_WIDE_INT)
439 /* Shifting by the host word size is undefined according to the
440 ANSI standard, so we must handle this as a special case. */
444 else if (count >= HOST_BITS_PER_WIDE_INT)
447 *lv = ((signmask << (2 * HOST_BITS_PER_WIDE_INT - count - 1) << 1)
448 | ((unsigned HOST_WIDE_INT) h1 >> (count - HOST_BITS_PER_WIDE_INT)));
453 | ((unsigned HOST_WIDE_INT) h1 << (HOST_BITS_PER_WIDE_INT - count - 1) << 1));
454 *hv = ((signmask << (HOST_BITS_PER_WIDE_INT - count))
455 | ((unsigned HOST_WIDE_INT) h1 >> count));
459 /* Rotate the doubleword integer in L1, H1 left by COUNT places
460 keeping only PREC bits of result.
461 Rotate right if COUNT is negative.
462 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
465 lrotate_double (l1, h1, count, prec, lv, hv)
466 unsigned HOST_WIDE_INT l1;
467 HOST_WIDE_INT h1, count;
469 unsigned HOST_WIDE_INT *lv;
472 unsigned HOST_WIDE_INT s1l, s2l;
473 HOST_WIDE_INT s1h, s2h;
479 lshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
480 rshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
485 /* Rotate the doubleword integer in L1, H1 left by COUNT places
486 keeping only PREC bits of result. COUNT must be positive.
487 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
490 rrotate_double (l1, h1, count, prec, lv, hv)
491 unsigned HOST_WIDE_INT l1;
492 HOST_WIDE_INT h1, count;
494 unsigned HOST_WIDE_INT *lv;
497 unsigned HOST_WIDE_INT s1l, s2l;
498 HOST_WIDE_INT s1h, s2h;
504 rshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
505 lshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
510 /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
511 for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
512 CODE is a tree code for a kind of division, one of
513 TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
515 It controls how the quotient is rounded to a integer.
516 Return nonzero if the operation overflows.
517 UNS nonzero says do unsigned division. */
520 div_and_round_double (code, uns,
521 lnum_orig, hnum_orig, lden_orig, hden_orig,
522 lquo, hquo, lrem, hrem)
525 unsigned HOST_WIDE_INT lnum_orig; /* num == numerator == dividend */
526 HOST_WIDE_INT hnum_orig;
527 unsigned HOST_WIDE_INT lden_orig; /* den == denominator == divisor */
528 HOST_WIDE_INT hden_orig;
529 unsigned HOST_WIDE_INT *lquo, *lrem;
530 HOST_WIDE_INT *hquo, *hrem;
533 HOST_WIDE_INT num[4 + 1]; /* extra element for scaling. */
534 HOST_WIDE_INT den[4], quo[4];
536 unsigned HOST_WIDE_INT work;
537 unsigned HOST_WIDE_INT carry = 0;
538 unsigned HOST_WIDE_INT lnum = lnum_orig;
539 HOST_WIDE_INT hnum = hnum_orig;
540 unsigned HOST_WIDE_INT lden = lden_orig;
541 HOST_WIDE_INT hden = hden_orig;
544 if (hden == 0 && lden == 0)
545 overflow = 1, lden = 1;
547 /* calculate quotient sign and convert operands to unsigned. */
553 /* (minimum integer) / (-1) is the only overflow case. */
554 if (neg_double (lnum, hnum, &lnum, &hnum)
555 && ((HOST_WIDE_INT) lden & hden) == -1)
561 neg_double (lden, hden, &lden, &hden);
565 if (hnum == 0 && hden == 0)
566 { /* single precision */
568 /* This unsigned division rounds toward zero. */
574 { /* trivial case: dividend < divisor */
575 /* hden != 0 already checked. */
582 memset ((char *) quo, 0, sizeof quo);
584 memset ((char *) num, 0, sizeof num); /* to zero 9th element */
585 memset ((char *) den, 0, sizeof den);
587 encode (num, lnum, hnum);
588 encode (den, lden, hden);
590 /* Special code for when the divisor < BASE. */
591 if (hden == 0 && lden < (unsigned HOST_WIDE_INT) BASE)
593 /* hnum != 0 already checked. */
594 for (i = 4 - 1; i >= 0; i--)
596 work = num[i] + carry * BASE;
597 quo[i] = work / lden;
603 /* Full double precision division,
604 with thanks to Don Knuth's "Seminumerical Algorithms". */
605 int num_hi_sig, den_hi_sig;
606 unsigned HOST_WIDE_INT quo_est, scale;
608 /* Find the highest non-zero divisor digit. */
609 for (i = 4 - 1;; i--)
616 /* Insure that the first digit of the divisor is at least BASE/2.
617 This is required by the quotient digit estimation algorithm. */
619 scale = BASE / (den[den_hi_sig] + 1);
621 { /* scale divisor and dividend */
623 for (i = 0; i <= 4 - 1; i++)
625 work = (num[i] * scale) + carry;
626 num[i] = LOWPART (work);
627 carry = HIGHPART (work);
632 for (i = 0; i <= 4 - 1; i++)
634 work = (den[i] * scale) + carry;
635 den[i] = LOWPART (work);
636 carry = HIGHPART (work);
637 if (den[i] != 0) den_hi_sig = i;
644 for (i = num_hi_sig - den_hi_sig - 1; i >= 0; i--)
646 /* Guess the next quotient digit, quo_est, by dividing the first
647 two remaining dividend digits by the high order quotient digit.
648 quo_est is never low and is at most 2 high. */
649 unsigned HOST_WIDE_INT tmp;
651 num_hi_sig = i + den_hi_sig + 1;
652 work = num[num_hi_sig] * BASE + num[num_hi_sig - 1];
653 if (num[num_hi_sig] != den[den_hi_sig])
654 quo_est = work / den[den_hi_sig];
658 /* Refine quo_est so it's usually correct, and at most one high. */
659 tmp = work - quo_est * den[den_hi_sig];
661 && (den[den_hi_sig - 1] * quo_est
662 > (tmp * BASE + num[num_hi_sig - 2])))
665 /* Try QUO_EST as the quotient digit, by multiplying the
666 divisor by QUO_EST and subtracting from the remaining dividend.
667 Keep in mind that QUO_EST is the I - 1st digit. */
670 for (j = 0; j <= den_hi_sig; j++)
672 work = quo_est * den[j] + carry;
673 carry = HIGHPART (work);
674 work = num[i + j] - LOWPART (work);
675 num[i + j] = LOWPART (work);
676 carry += HIGHPART (work) != 0;
679 /* If quo_est was high by one, then num[i] went negative and
680 we need to correct things. */
681 if (num[num_hi_sig] < carry)
684 carry = 0; /* add divisor back in */
685 for (j = 0; j <= den_hi_sig; j++)
687 work = num[i + j] + den[j] + carry;
688 carry = HIGHPART (work);
689 num[i + j] = LOWPART (work);
692 num [num_hi_sig] += carry;
695 /* Store the quotient digit. */
700 decode (quo, lquo, hquo);
703 /* if result is negative, make it so. */
705 neg_double (*lquo, *hquo, lquo, hquo);
707 /* compute trial remainder: rem = num - (quo * den) */
708 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
709 neg_double (*lrem, *hrem, lrem, hrem);
710 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
715 case TRUNC_MOD_EXPR: /* round toward zero */
716 case EXACT_DIV_EXPR: /* for this one, it shouldn't matter */
720 case FLOOR_MOD_EXPR: /* round toward negative infinity */
721 if (quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio < 0 && rem != 0 */
724 add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1,
732 case CEIL_MOD_EXPR: /* round toward positive infinity */
733 if (!quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio > 0 && rem != 0 */
735 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
743 case ROUND_MOD_EXPR: /* round to closest integer */
745 unsigned HOST_WIDE_INT labs_rem = *lrem;
746 HOST_WIDE_INT habs_rem = *hrem;
747 unsigned HOST_WIDE_INT labs_den = lden, ltwice;
748 HOST_WIDE_INT habs_den = hden, htwice;
750 /* Get absolute values */
752 neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
754 neg_double (lden, hden, &labs_den, &habs_den);
756 /* If (2 * abs (lrem) >= abs (lden)) */
757 mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0,
758 labs_rem, habs_rem, <wice, &htwice);
760 if (((unsigned HOST_WIDE_INT) habs_den
761 < (unsigned HOST_WIDE_INT) htwice)
762 || (((unsigned HOST_WIDE_INT) habs_den
763 == (unsigned HOST_WIDE_INT) htwice)
764 && (labs_den < ltwice)))
768 add_double (*lquo, *hquo,
769 (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
772 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
784 /* compute true remainder: rem = num - (quo * den) */
785 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
786 neg_double (*lrem, *hrem, lrem, hrem);
787 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
791 #ifndef REAL_ARITHMETIC
792 /* Effectively truncate a real value to represent the nearest possible value
793 in a narrower mode. The result is actually represented in the same data
794 type as the argument, but its value is usually different.
796 A trap may occur during the FP operations and it is the responsibility
797 of the calling function to have a handler established. */
800 real_value_truncate (mode, arg)
801 enum machine_mode mode;
804 return REAL_VALUE_TRUNCATE (mode, arg);
807 #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
809 /* Check for infinity in an IEEE double precision number. */
815 /* The IEEE 64-bit double format. */
820 unsigned exponent : 11;
821 unsigned mantissa1 : 20;
826 unsigned mantissa1 : 20;
827 unsigned exponent : 11;
833 if (u.big_endian.sign == 1)
836 return (u.big_endian.exponent == 2047
837 && u.big_endian.mantissa1 == 0
838 && u.big_endian.mantissa2 == 0);
843 return (u.little_endian.exponent == 2047
844 && u.little_endian.mantissa1 == 0
845 && u.little_endian.mantissa2 == 0);
849 /* Check whether an IEEE double precision number is a NaN. */
855 /* The IEEE 64-bit double format. */
860 unsigned exponent : 11;
861 unsigned mantissa1 : 20;
866 unsigned mantissa1 : 20;
867 unsigned exponent : 11;
873 if (u.big_endian.sign == 1)
876 return (u.big_endian.exponent == 2047
877 && (u.big_endian.mantissa1 != 0
878 || u.big_endian.mantissa2 != 0));
883 return (u.little_endian.exponent == 2047
884 && (u.little_endian.mantissa1 != 0
885 || u.little_endian.mantissa2 != 0));
889 /* Check for a negative IEEE double precision number. */
895 /* The IEEE 64-bit double format. */
900 unsigned exponent : 11;
901 unsigned mantissa1 : 20;
906 unsigned mantissa1 : 20;
907 unsigned exponent : 11;
913 if (u.big_endian.sign == 1)
916 return u.big_endian.sign;
921 return u.little_endian.sign;
924 #else /* Target not IEEE */
926 /* Let's assume other float formats don't have infinity.
927 (This can be overridden by redefining REAL_VALUE_ISINF.) */
931 REAL_VALUE_TYPE x ATTRIBUTE_UNUSED;
936 /* Let's assume other float formats don't have NaNs.
937 (This can be overridden by redefining REAL_VALUE_ISNAN.) */
941 REAL_VALUE_TYPE x ATTRIBUTE_UNUSED;
946 /* Let's assume other float formats don't have minus zero.
947 (This can be overridden by redefining REAL_VALUE_NEGATIVE.) */
955 #endif /* Target not IEEE */
957 /* Try to change R into its exact multiplicative inverse in machine mode
958 MODE. Return nonzero function value if successful. */
961 exact_real_inverse (mode, r)
962 enum machine_mode mode;
971 #ifdef CHECK_FLOAT_VALUE
975 /* Usually disable if bounds checks are not reliable. */
976 if ((HOST_FLOAT_FORMAT != TARGET_FLOAT_FORMAT) && !flag_pretend_float)
979 /* Set array index to the less significant bits in the unions, depending
980 on the endian-ness of the host doubles.
981 Disable if insufficient information on the data structure. */
982 #if HOST_FLOAT_FORMAT == UNKNOWN_FLOAT_FORMAT
985 #if HOST_FLOAT_FORMAT == VAX_FLOAT_FORMAT
988 #if HOST_FLOAT_FORMAT == IBM_FLOAT_FORMAT
991 #define K (2 * HOST_FLOAT_WORDS_BIG_ENDIAN)
996 if (setjmp (float_error))
998 /* Don't do the optimization if there was an arithmetic error. */
1000 set_float_handler (NULL_PTR);
1003 set_float_handler (float_error);
1005 /* Domain check the argument. */
1010 #ifdef REAL_INFINITY
1011 if (REAL_VALUE_ISINF (x.d) || REAL_VALUE_ISNAN (x.d))
1015 /* Compute the reciprocal and check for numerical exactness.
1016 It is unnecessary to check all the significand bits to determine
1017 whether X is a power of 2. If X is not, then it is impossible for
1018 the bottom half significand of both X and 1/X to be all zero bits.
1019 Hence we ignore the data structure of the top half and examine only
1020 the low order bits of the two significands. */
1022 if (x.i[K] != 0 || x.i[K + 1] != 0 || t.i[K] != 0 || t.i[K + 1] != 0)
1025 /* Truncate to the required mode and range-check the result. */
1026 y.d = REAL_VALUE_TRUNCATE (mode, t.d);
1027 #ifdef CHECK_FLOAT_VALUE
1029 if (CHECK_FLOAT_VALUE (mode, y.d, i))
1033 /* Fail if truncation changed the value. */
1034 if (y.d != t.d || y.d == 0.0)
1037 #ifdef REAL_INFINITY
1038 if (REAL_VALUE_ISINF (y.d) || REAL_VALUE_ISNAN (y.d))
1042 /* Output the reciprocal and return success flag. */
1043 set_float_handler (NULL_PTR);
1048 /* Convert C99 hexadecimal floating point string constant S. Return
1049 real value type in mode MODE. This function uses the host computer's
1050 floating point arithmetic when there is no REAL_ARITHMETIC. */
1053 real_hex_to_f (s, mode)
1055 enum machine_mode mode;
1059 unsigned HOST_WIDE_INT low, high;
1060 int shcount, nrmcount, k;
1061 int sign, expsign, isfloat;
1062 int lost = 0;/* Nonzero low order bits shifted out and discarded. */
1063 int frexpon = 0; /* Bits after the decimal point. */
1064 int expon = 0; /* Value of exponent. */
1065 int decpt = 0; /* How many decimal points. */
1066 int gotp = 0; /* How many P's. */
1073 while (*p == ' ' || *p == '\t')
1076 /* Sign, if any, comes first. */
1084 /* The string is supposed to start with 0x or 0X . */
1088 if (*p == 'x' || *p == 'X')
1102 while ((c = *p) != '\0')
1104 if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F')
1105 || (c >= 'a' && c <= 'f'))
1108 if (k >= 'a' && k <= 'f')
1115 if ((high & 0xf0000000) == 0)
1117 high = (high << 4) + ((low >> 28) & 15);
1118 low = (low << 4) + k;
1125 /* Record nonzero lost bits. */
1138 else if (c == 'p' || c == 'P')
1142 /* Sign of exponent. */
1149 /* Value of exponent.
1150 The exponent field is a decimal integer. */
1151 while (ISDIGIT (*p))
1153 k = (*p++ & CHARMASK) - '0';
1154 expon = 10 * expon + k;
1158 /* F suffix is ambiguous in the significand part
1159 so it must appear after the decimal exponent field. */
1160 if (*p == 'f' || *p == 'F')
1168 else if (c == 'l' || c == 'L')
1177 /* Abort if last character read was not legitimate. */
1179 if ((c != '\0' && c != ' ' && c != '\n' && c != '\r') || (decpt > 1))
1182 /* There must be either one decimal point or one p. */
1183 if (decpt == 0 && gotp == 0)
1187 if (high == 0 && low == 0)
1199 /* Leave a high guard bit for carry-out. */
1200 if ((high & 0x80000000) != 0)
1203 low = (low >> 1) | (high << 31);
1208 if ((high & 0xffff8000) == 0)
1210 high = (high << 16) + ((low >> 16) & 0xffff);
1215 while ((high & 0xc0000000) == 0)
1217 high = (high << 1) + ((low >> 31) & 1);
1222 if (isfloat || GET_MODE_SIZE (mode) == UNITS_PER_WORD)
1224 /* Keep 24 bits precision, bits 0x7fffff80.
1225 Rounding bit is 0x40. */
1226 lost = lost | low | (high & 0x3f);
1230 if ((high & 0x80) || lost)
1237 /* We need real.c to do long double formats, so here default
1238 to double precision. */
1239 #if HOST_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
1241 Keep 53 bits precision, bits 0x7fffffff fffffc00.
1242 Rounding bit is low word 0x200. */
1243 lost = lost | (low & 0x1ff);
1246 if ((low & 0x400) || lost)
1248 low = (low + 0x200) & 0xfffffc00;
1255 /* Assume it's a VAX with 56-bit significand,
1256 bits 0x7fffffff ffffff80. */
1257 lost = lost | (low & 0x7f);
1260 if ((low & 0x80) || lost)
1262 low = (low + 0x40) & 0xffffff80;
1272 ip = REAL_VALUE_LDEXP (ip, 32) + (double) low;
1273 /* Apply shifts and exponent value as power of 2. */
1274 ip = REAL_VALUE_LDEXP (ip, expon - (nrmcount + frexpon));
1281 #endif /* no REAL_ARITHMETIC */
1283 /* Given T, an expression, return the negation of T. Allow for T to be
1284 null, in which case return null. */
1296 type = TREE_TYPE (t);
1297 STRIP_SIGN_NOPS (t);
1299 switch (TREE_CODE (t))
1303 if (! TREE_UNSIGNED (type)
1304 && 0 != (tem = fold (build1 (NEGATE_EXPR, type, t)))
1305 && ! TREE_OVERFLOW (tem))
1310 return convert (type, TREE_OPERAND (t, 0));
1313 /* - (A - B) -> B - A */
1314 if (! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
1315 return convert (type,
1316 fold (build (MINUS_EXPR, TREE_TYPE (t),
1317 TREE_OPERAND (t, 1),
1318 TREE_OPERAND (t, 0))));
1325 return convert (type, build1 (NEGATE_EXPR, TREE_TYPE (t), t));
1328 /* Split a tree IN into a constant, literal and variable parts that could be
1329 combined with CODE to make IN. "constant" means an expression with
1330 TREE_CONSTANT but that isn't an actual constant. CODE must be a
1331 commutative arithmetic operation. Store the constant part into *CONP,
1332 the literal in &LITP and return the variable part. If a part isn't
1333 present, set it to null. If the tree does not decompose in this way,
1334 return the entire tree as the variable part and the other parts as null.
1336 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
1337 case, we negate an operand that was subtracted. If NEGATE_P is true, we
1338 are negating all of IN.
1340 If IN is itself a literal or constant, return it as appropriate.
1342 Note that we do not guarantee that any of the three values will be the
1343 same type as IN, but they will have the same signedness and mode. */
1346 split_tree (in, code, conp, litp, negate_p)
1348 enum tree_code code;
1357 /* Strip any conversions that don't change the machine mode or signedness. */
1358 STRIP_SIGN_NOPS (in);
1360 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST)
1362 else if (TREE_CODE (in) == code
1363 || (! FLOAT_TYPE_P (TREE_TYPE (in))
1364 /* We can associate addition and subtraction together (even
1365 though the C standard doesn't say so) for integers because
1366 the value is not affected. For reals, the value might be
1367 affected, so we can't. */
1368 && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
1369 || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
1371 tree op0 = TREE_OPERAND (in, 0);
1372 tree op1 = TREE_OPERAND (in, 1);
1373 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
1374 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
1376 /* First see if either of the operands is a literal, then a constant. */
1377 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST)
1378 *litp = op0, op0 = 0;
1379 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST)
1380 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
1382 if (op0 != 0 && TREE_CONSTANT (op0))
1383 *conp = op0, op0 = 0;
1384 else if (op1 != 0 && TREE_CONSTANT (op1))
1385 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
1387 /* If we haven't dealt with either operand, this is not a case we can
1388 decompose. Otherwise, VAR is either of the ones remaining, if any. */
1389 if (op0 != 0 && op1 != 0)
1394 var = op1, neg_var_p = neg1_p;
1396 /* Now do any needed negations. */
1397 if (neg_litp_p) *litp = negate_expr (*litp);
1398 if (neg_conp_p) *conp = negate_expr (*conp);
1399 if (neg_var_p) var = negate_expr (var);
1401 else if (TREE_CONSTANT (in))
1408 var = negate_expr (var);
1409 *conp = negate_expr (*conp);
1410 *litp = negate_expr (*litp);
1416 /* Re-associate trees split by the above function. T1 and T2 are either
1417 expressions to associate or null. Return the new expression, if any. If
1418 we build an operation, do it in TYPE and with CODE, except if CODE is a
1419 MINUS_EXPR, in which case we use PLUS_EXPR since split_tree will already
1420 have taken care of the negations. */
1423 associate_trees (t1, t2, code, type)
1425 enum tree_code code;
1433 if (code == MINUS_EXPR)
1436 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
1437 try to fold this since we will have infinite recursion. But do
1438 deal with any NEGATE_EXPRs. */
1439 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
1440 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
1442 if (TREE_CODE (t1) == NEGATE_EXPR)
1443 return build (MINUS_EXPR, type, convert (type, t2),
1444 convert (type, TREE_OPERAND (t1, 0)));
1445 else if (TREE_CODE (t2) == NEGATE_EXPR)
1446 return build (MINUS_EXPR, type, convert (type, t1),
1447 convert (type, TREE_OPERAND (t2, 0)));
1449 return build (code, type, convert (type, t1), convert (type, t2));
1452 return fold (build (code, type, convert (type, t1), convert (type, t2)));
1455 /* Combine two integer constants ARG1 and ARG2 under operation CODE
1456 to produce a new constant.
1458 If NOTRUNC is nonzero, do not truncate the result to fit the data type.
1459 If FORSIZE is nonzero, compute overflow for unsigned types. */
1462 int_const_binop (code, arg1, arg2, notrunc, forsize)
1463 enum tree_code code;
1464 register tree arg1, arg2;
1465 int notrunc, forsize;
1467 unsigned HOST_WIDE_INT int1l, int2l;
1468 HOST_WIDE_INT int1h, int2h;
1469 unsigned HOST_WIDE_INT low;
1471 unsigned HOST_WIDE_INT garbagel;
1472 HOST_WIDE_INT garbageh;
1474 int uns = TREE_UNSIGNED (TREE_TYPE (arg1));
1476 int no_overflow = 0;
1478 int1l = TREE_INT_CST_LOW (arg1);
1479 int1h = TREE_INT_CST_HIGH (arg1);
1480 int2l = TREE_INT_CST_LOW (arg2);
1481 int2h = TREE_INT_CST_HIGH (arg2);
1486 low = int1l | int2l, hi = int1h | int2h;
1490 low = int1l ^ int2l, hi = int1h ^ int2h;
1494 low = int1l & int2l, hi = int1h & int2h;
1497 case BIT_ANDTC_EXPR:
1498 low = int1l & ~int2l, hi = int1h & ~int2h;
1504 /* It's unclear from the C standard whether shifts can overflow.
1505 The following code ignores overflow; perhaps a C standard
1506 interpretation ruling is needed. */
1507 lshift_double (int1l, int1h, int2l, TYPE_PRECISION (TREE_TYPE (arg1)),
1515 lrotate_double (int1l, int1h, int2l, TYPE_PRECISION (TREE_TYPE (arg1)),
1520 overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
1524 neg_double (int2l, int2h, &low, &hi);
1525 add_double (int1l, int1h, low, hi, &low, &hi);
1526 overflow = OVERFLOW_SUM_SIGN (hi, int2h, int1h);
1530 overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
1533 case TRUNC_DIV_EXPR:
1534 case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1535 case EXACT_DIV_EXPR:
1536 /* This is a shortcut for a common special case. */
1537 if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1538 && ! TREE_CONSTANT_OVERFLOW (arg1)
1539 && ! TREE_CONSTANT_OVERFLOW (arg2)
1540 && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1542 if (code == CEIL_DIV_EXPR)
1545 low = int1l / int2l, hi = 0;
1549 /* ... fall through ... */
1551 case ROUND_DIV_EXPR:
1552 if (int2h == 0 && int2l == 1)
1554 low = int1l, hi = int1h;
1557 if (int1l == int2l && int1h == int2h
1558 && ! (int1l == 0 && int1h == 0))
1563 overflow = div_and_round_double (code, uns,
1564 int1l, int1h, int2l, int2h,
1565 &low, &hi, &garbagel, &garbageh);
1568 case TRUNC_MOD_EXPR:
1569 case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1570 /* This is a shortcut for a common special case. */
1571 if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1572 && ! TREE_CONSTANT_OVERFLOW (arg1)
1573 && ! TREE_CONSTANT_OVERFLOW (arg2)
1574 && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1576 if (code == CEIL_MOD_EXPR)
1578 low = int1l % int2l, hi = 0;
1582 /* ... fall through ... */
1584 case ROUND_MOD_EXPR:
1585 overflow = div_and_round_double (code, uns,
1586 int1l, int1h, int2l, int2h,
1587 &garbagel, &garbageh, &low, &hi);
1593 low = (((unsigned HOST_WIDE_INT) int1h
1594 < (unsigned HOST_WIDE_INT) int2h)
1595 || (((unsigned HOST_WIDE_INT) int1h
1596 == (unsigned HOST_WIDE_INT) int2h)
1599 low = (int1h < int2h
1600 || (int1h == int2h && int1l < int2l));
1602 if (low == (code == MIN_EXPR))
1603 low = int1l, hi = int1h;
1605 low = int2l, hi = int2h;
1612 if (forsize && hi == 0 && low < 10000
1613 && overflow == 0 && ! TREE_OVERFLOW (arg1) && ! TREE_OVERFLOW (arg2))
1614 return size_int_type_wide (low, TREE_TYPE (arg1));
1617 t = build_int_2 (low, hi);
1618 TREE_TYPE (t) = TREE_TYPE (arg1);
1622 = ((notrunc ? (!uns || forsize) && overflow
1623 : force_fit_type (t, (!uns || forsize) && overflow) && ! no_overflow)
1624 | TREE_OVERFLOW (arg1)
1625 | TREE_OVERFLOW (arg2));
1627 /* If we're doing a size calculation, unsigned arithmetic does overflow.
1628 So check if force_fit_type truncated the value. */
1630 && ! TREE_OVERFLOW (t)
1631 && (TREE_INT_CST_HIGH (t) != hi
1632 || TREE_INT_CST_LOW (t) != low))
1633 TREE_OVERFLOW (t) = 1;
1635 TREE_CONSTANT_OVERFLOW (t) = (TREE_OVERFLOW (t)
1636 | TREE_CONSTANT_OVERFLOW (arg1)
1637 | TREE_CONSTANT_OVERFLOW (arg2));
1641 /* Define input and output argument for const_binop_1. */
1644 enum tree_code code; /* Input: tree code for operation. */
1645 tree type; /* Input: tree type for operation. */
1646 REAL_VALUE_TYPE d1, d2; /* Input: floating point operands. */
1647 tree t; /* Output: constant for result. */
1650 /* Do the real arithmetic for const_binop while protected by a
1651 float overflow handler. */
1654 const_binop_1 (data)
1657 struct cb_args *args = (struct cb_args *) data;
1658 REAL_VALUE_TYPE value;
1660 #ifdef REAL_ARITHMETIC
1661 REAL_ARITHMETIC (value, args->code, args->d1, args->d2);
1666 value = args->d1 + args->d2;
1670 value = args->d1 - args->d2;
1674 value = args->d1 * args->d2;
1678 #ifndef REAL_INFINITY
1683 value = args->d1 / args->d2;
1687 value = MIN (args->d1, args->d2);
1691 value = MAX (args->d1, args->d2);
1697 #endif /* no REAL_ARITHMETIC */
1700 = build_real (args->type,
1701 real_value_truncate (TYPE_MODE (args->type), value));
1704 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1705 constant. We assume ARG1 and ARG2 have the same data type, or at least
1706 are the same kind of constant and the same machine mode.
1708 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
1711 const_binop (code, arg1, arg2, notrunc)
1712 enum tree_code code;
1713 register tree arg1, arg2;
1719 if (TREE_CODE (arg1) == INTEGER_CST)
1720 return int_const_binop (code, arg1, arg2, notrunc, 0);
1722 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1723 if (TREE_CODE (arg1) == REAL_CST)
1729 struct cb_args args;
1731 d1 = TREE_REAL_CST (arg1);
1732 d2 = TREE_REAL_CST (arg2);
1734 /* If either operand is a NaN, just return it. Otherwise, set up
1735 for floating-point trap; we return an overflow. */
1736 if (REAL_VALUE_ISNAN (d1))
1738 else if (REAL_VALUE_ISNAN (d2))
1741 /* Setup input for const_binop_1() */
1742 args.type = TREE_TYPE (arg1);
1747 if (do_float_handler (const_binop_1, (PTR) &args))
1748 /* Receive output from const_binop_1. */
1752 /* We got an exception from const_binop_1. */
1753 t = copy_node (arg1);
1758 = (force_fit_type (t, overflow)
1759 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1760 TREE_CONSTANT_OVERFLOW (t)
1762 | TREE_CONSTANT_OVERFLOW (arg1)
1763 | TREE_CONSTANT_OVERFLOW (arg2);
1766 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
1767 if (TREE_CODE (arg1) == COMPLEX_CST)
1769 register tree type = TREE_TYPE (arg1);
1770 register tree r1 = TREE_REALPART (arg1);
1771 register tree i1 = TREE_IMAGPART (arg1);
1772 register tree r2 = TREE_REALPART (arg2);
1773 register tree i2 = TREE_IMAGPART (arg2);
1779 t = build_complex (type,
1780 const_binop (PLUS_EXPR, r1, r2, notrunc),
1781 const_binop (PLUS_EXPR, i1, i2, notrunc));
1785 t = build_complex (type,
1786 const_binop (MINUS_EXPR, r1, r2, notrunc),
1787 const_binop (MINUS_EXPR, i1, i2, notrunc));
1791 t = build_complex (type,
1792 const_binop (MINUS_EXPR,
1793 const_binop (MULT_EXPR,
1795 const_binop (MULT_EXPR,
1798 const_binop (PLUS_EXPR,
1799 const_binop (MULT_EXPR,
1801 const_binop (MULT_EXPR,
1808 register tree magsquared
1809 = const_binop (PLUS_EXPR,
1810 const_binop (MULT_EXPR, r2, r2, notrunc),
1811 const_binop (MULT_EXPR, i2, i2, notrunc),
1814 t = build_complex (type,
1816 (INTEGRAL_TYPE_P (TREE_TYPE (r1))
1817 ? TRUNC_DIV_EXPR : RDIV_EXPR,
1818 const_binop (PLUS_EXPR,
1819 const_binop (MULT_EXPR, r1, r2,
1821 const_binop (MULT_EXPR, i1, i2,
1824 magsquared, notrunc),
1826 (INTEGRAL_TYPE_P (TREE_TYPE (r1))
1827 ? TRUNC_DIV_EXPR : RDIV_EXPR,
1828 const_binop (MINUS_EXPR,
1829 const_binop (MULT_EXPR, i1, r2,
1831 const_binop (MULT_EXPR, r1, i2,
1834 magsquared, notrunc));
1846 /* Return an INTEGER_CST with value whose low-order HOST_BITS_PER_WIDE_INT
1847 bits are given by NUMBER and of the sizetype represented by KIND. */
1850 size_int_wide (number, kind)
1851 HOST_WIDE_INT number;
1852 enum size_type_kind kind;
1854 return size_int_type_wide (number, sizetype_tab[(int) kind]);
1857 /* Likewise, but the desired type is specified explicitly. */
1860 size_int_type_wide (number, type)
1861 HOST_WIDE_INT number;
1864 /* Type-size nodes already made for small sizes. */
1865 static tree size_table[2048 + 1];
1866 static int init_p = 0;
1871 ggc_add_tree_root ((tree *) size_table,
1872 sizeof size_table / sizeof (tree));
1876 /* If this is a positive number that fits in the table we use to hold
1877 cached entries, see if it is already in the table and put it there
1879 if (number >= 0 && number < (int) ARRAY_SIZE (size_table))
1881 if (size_table[number] != 0)
1882 for (t = size_table[number]; t != 0; t = TREE_CHAIN (t))
1883 if (TREE_TYPE (t) == type)
1886 t = build_int_2 (number, 0);
1887 TREE_TYPE (t) = type;
1888 TREE_CHAIN (t) = size_table[number];
1889 size_table[number] = t;
1894 t = build_int_2 (number, number < 0 ? -1 : 0);
1895 TREE_TYPE (t) = type;
1896 TREE_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (t) = force_fit_type (t, 0);
1900 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1901 is a tree code. The type of the result is taken from the operands.
1902 Both must be the same type integer type and it must be a size type.
1903 If the operands are constant, so is the result. */
1906 size_binop (code, arg0, arg1)
1907 enum tree_code code;
1910 tree type = TREE_TYPE (arg0);
1912 if (TREE_CODE (type) != INTEGER_TYPE || ! TYPE_IS_SIZETYPE (type)
1913 || type != TREE_TYPE (arg1))
1916 /* Handle the special case of two integer constants faster. */
1917 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1919 /* And some specific cases even faster than that. */
1920 if (code == PLUS_EXPR && integer_zerop (arg0))
1922 else if ((code == MINUS_EXPR || code == PLUS_EXPR)
1923 && integer_zerop (arg1))
1925 else if (code == MULT_EXPR && integer_onep (arg0))
1928 /* Handle general case of two integer constants. */
1929 return int_const_binop (code, arg0, arg1, 0, 1);
1932 if (arg0 == error_mark_node || arg1 == error_mark_node)
1933 return error_mark_node;
1935 return fold (build (code, type, arg0, arg1));
1938 /* Given two values, either both of sizetype or both of bitsizetype,
1939 compute the difference between the two values. Return the value
1940 in signed type corresponding to the type of the operands. */
1943 size_diffop (arg0, arg1)
1946 tree type = TREE_TYPE (arg0);
1949 if (TREE_CODE (type) != INTEGER_TYPE || ! TYPE_IS_SIZETYPE (type)
1950 || type != TREE_TYPE (arg1))
1953 /* If the type is already signed, just do the simple thing. */
1954 if (! TREE_UNSIGNED (type))
1955 return size_binop (MINUS_EXPR, arg0, arg1);
1957 ctype = (type == bitsizetype || type == ubitsizetype
1958 ? sbitsizetype : ssizetype);
1960 /* If either operand is not a constant, do the conversions to the signed
1961 type and subtract. The hardware will do the right thing with any
1962 overflow in the subtraction. */
1963 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1964 return size_binop (MINUS_EXPR, convert (ctype, arg0),
1965 convert (ctype, arg1));
1967 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1968 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1969 overflow) and negate (which can't either). Special-case a result
1970 of zero while we're here. */
1971 if (tree_int_cst_equal (arg0, arg1))
1972 return convert (ctype, integer_zero_node);
1973 else if (tree_int_cst_lt (arg1, arg0))
1974 return convert (ctype, size_binop (MINUS_EXPR, arg0, arg1));
1976 return size_binop (MINUS_EXPR, convert (ctype, integer_zero_node),
1977 convert (ctype, size_binop (MINUS_EXPR, arg1, arg0)));
1980 /* This structure is used to communicate arguments to fold_convert_1. */
1983 tree arg1; /* Input: value to convert. */
1984 tree type; /* Input: type to convert value to. */
1985 tree t; /* Ouput: result of conversion. */
1988 /* Function to convert floating-point constants, protected by floating
1989 point exception handler. */
1992 fold_convert_1 (data)
1995 struct fc_args *args = (struct fc_args *) data;
1997 args->t = build_real (args->type,
1998 real_value_truncate (TYPE_MODE (args->type),
1999 TREE_REAL_CST (args->arg1)));
2002 /* Given T, a tree representing type conversion of ARG1, a constant,
2003 return a constant tree representing the result of conversion. */
2006 fold_convert (t, arg1)
2010 register tree type = TREE_TYPE (t);
2013 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
2015 if (TREE_CODE (arg1) == INTEGER_CST)
2017 /* If we would build a constant wider than GCC supports,
2018 leave the conversion unfolded. */
2019 if (TYPE_PRECISION (type) > 2 * HOST_BITS_PER_WIDE_INT)
2022 /* If we are trying to make a sizetype for a small integer, use
2023 size_int to pick up cached types to reduce duplicate nodes. */
2024 if (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type)
2025 && !TREE_CONSTANT_OVERFLOW (arg1)
2026 && compare_tree_int (arg1, 10000) < 0)
2027 return size_int_type_wide (TREE_INT_CST_LOW (arg1), type);
2029 /* Given an integer constant, make new constant with new type,
2030 appropriately sign-extended or truncated. */
2031 t = build_int_2 (TREE_INT_CST_LOW (arg1),
2032 TREE_INT_CST_HIGH (arg1));
2033 TREE_TYPE (t) = type;
2034 /* Indicate an overflow if (1) ARG1 already overflowed,
2035 or (2) force_fit_type indicates an overflow.
2036 Tell force_fit_type that an overflow has already occurred
2037 if ARG1 is a too-large unsigned value and T is signed.
2038 But don't indicate an overflow if converting a pointer. */
2040 = ((force_fit_type (t,
2041 (TREE_INT_CST_HIGH (arg1) < 0
2042 && (TREE_UNSIGNED (type)
2043 < TREE_UNSIGNED (TREE_TYPE (arg1)))))
2044 && ! POINTER_TYPE_P (TREE_TYPE (arg1)))
2045 || TREE_OVERFLOW (arg1));
2046 TREE_CONSTANT_OVERFLOW (t)
2047 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
2049 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
2050 else if (TREE_CODE (arg1) == REAL_CST)
2052 /* Don't initialize these, use assignments.
2053 Initialized local aggregates don't work on old compilers. */
2057 tree type1 = TREE_TYPE (arg1);
2060 x = TREE_REAL_CST (arg1);
2061 l = real_value_from_int_cst (type1, TYPE_MIN_VALUE (type));
2063 no_upper_bound = (TYPE_MAX_VALUE (type) == NULL);
2064 if (!no_upper_bound)
2065 u = real_value_from_int_cst (type1, TYPE_MAX_VALUE (type));
2067 /* See if X will be in range after truncation towards 0.
2068 To compensate for truncation, move the bounds away from 0,
2069 but reject if X exactly equals the adjusted bounds. */
2070 #ifdef REAL_ARITHMETIC
2071 REAL_ARITHMETIC (l, MINUS_EXPR, l, dconst1);
2072 if (!no_upper_bound)
2073 REAL_ARITHMETIC (u, PLUS_EXPR, u, dconst1);
2076 if (!no_upper_bound)
2079 /* If X is a NaN, use zero instead and show we have an overflow.
2080 Otherwise, range check. */
2081 if (REAL_VALUE_ISNAN (x))
2082 overflow = 1, x = dconst0;
2083 else if (! (REAL_VALUES_LESS (l, x)
2085 && REAL_VALUES_LESS (x, u)))
2088 #ifndef REAL_ARITHMETIC
2090 HOST_WIDE_INT low, high;
2091 HOST_WIDE_INT half_word
2092 = (HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2);
2097 high = (HOST_WIDE_INT) (x / half_word / half_word);
2098 x -= (REAL_VALUE_TYPE) high * half_word * half_word;
2099 if (x >= (REAL_VALUE_TYPE) half_word * half_word / 2)
2101 low = x - (REAL_VALUE_TYPE) half_word * half_word / 2;
2102 low |= (HOST_WIDE_INT) -1 << (HOST_BITS_PER_WIDE_INT - 1);
2105 low = (HOST_WIDE_INT) x;
2106 if (TREE_REAL_CST (arg1) < 0)
2107 neg_double (low, high, &low, &high);
2108 t = build_int_2 (low, high);
2112 HOST_WIDE_INT low, high;
2113 REAL_VALUE_TO_INT (&low, &high, x);
2114 t = build_int_2 (low, high);
2117 TREE_TYPE (t) = type;
2119 = TREE_OVERFLOW (arg1) | force_fit_type (t, overflow);
2120 TREE_CONSTANT_OVERFLOW (t)
2121 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
2123 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
2124 TREE_TYPE (t) = type;
2126 else if (TREE_CODE (type) == REAL_TYPE)
2128 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
2129 if (TREE_CODE (arg1) == INTEGER_CST)
2130 return build_real_from_int_cst (type, arg1);
2131 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
2132 if (TREE_CODE (arg1) == REAL_CST)
2134 struct fc_args args;
2136 if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1)))
2139 TREE_TYPE (arg1) = type;
2143 /* Setup input for fold_convert_1() */
2147 if (do_float_handler (fold_convert_1, (PTR) &args))
2149 /* Receive output from fold_convert_1() */
2154 /* We got an exception from fold_convert_1() */
2156 t = copy_node (arg1);
2160 = TREE_OVERFLOW (arg1) | force_fit_type (t, overflow);
2161 TREE_CONSTANT_OVERFLOW (t)
2162 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
2166 TREE_CONSTANT (t) = 1;
2170 /* Return an expr equal to X but certainly not valid as an lvalue. */
2178 /* These things are certainly not lvalues. */
2179 if (TREE_CODE (x) == NON_LVALUE_EXPR
2180 || TREE_CODE (x) == INTEGER_CST
2181 || TREE_CODE (x) == REAL_CST
2182 || TREE_CODE (x) == STRING_CST
2183 || TREE_CODE (x) == ADDR_EXPR)
2186 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
2187 TREE_CONSTANT (result) = TREE_CONSTANT (x);
2191 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2192 Zero means allow extended lvalues. */
2194 int pedantic_lvalues;
2196 /* When pedantic, return an expr equal to X but certainly not valid as a
2197 pedantic lvalue. Otherwise, return X. */
2200 pedantic_non_lvalue (x)
2203 if (pedantic_lvalues)
2204 return non_lvalue (x);
2209 /* Given a tree comparison code, return the code that is the logical inverse
2210 of the given code. It is not safe to do this for floating-point
2211 comparisons, except for NE_EXPR and EQ_EXPR. */
2213 static enum tree_code
2214 invert_tree_comparison (code)
2215 enum tree_code code;
2236 /* Similar, but return the comparison that results if the operands are
2237 swapped. This is safe for floating-point. */
2239 static enum tree_code
2240 swap_tree_comparison (code)
2241 enum tree_code code;
2261 /* Return nonzero if CODE is a tree code that represents a truth value. */
2264 truth_value_p (code)
2265 enum tree_code code;
2267 return (TREE_CODE_CLASS (code) == '<'
2268 || code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
2269 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
2270 || code == TRUTH_XOR_EXPR || code == TRUTH_NOT_EXPR);
2273 /* Return nonzero if two operands are necessarily equal.
2274 If ONLY_CONST is non-zero, only return non-zero for constants.
2275 This function tests whether the operands are indistinguishable;
2276 it does not test whether they are equal using C's == operation.
2277 The distinction is important for IEEE floating point, because
2278 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2279 (2) two NaNs may be indistinguishable, but NaN!=NaN. */
2282 operand_equal_p (arg0, arg1, only_const)
2286 /* If both types don't have the same signedness, then we can't consider
2287 them equal. We must check this before the STRIP_NOPS calls
2288 because they may change the signedness of the arguments. */
2289 if (TREE_UNSIGNED (TREE_TYPE (arg0)) != TREE_UNSIGNED (TREE_TYPE (arg1)))
2295 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2296 /* This is needed for conversions and for COMPONENT_REF.
2297 Might as well play it safe and always test this. */
2298 || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2299 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2300 || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
2303 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2304 We don't care about side effects in that case because the SAVE_EXPR
2305 takes care of that for us. In all other cases, two expressions are
2306 equal if they have no side effects. If we have two identical
2307 expressions with side effects that should be treated the same due
2308 to the only side effects being identical SAVE_EXPR's, that will
2309 be detected in the recursive calls below. */
2310 if (arg0 == arg1 && ! only_const
2311 && (TREE_CODE (arg0) == SAVE_EXPR
2312 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2315 /* Next handle constant cases, those for which we can return 1 even
2316 if ONLY_CONST is set. */
2317 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2318 switch (TREE_CODE (arg0))
2321 return (! TREE_CONSTANT_OVERFLOW (arg0)
2322 && ! TREE_CONSTANT_OVERFLOW (arg1)
2323 && tree_int_cst_equal (arg0, arg1));
2326 return (! TREE_CONSTANT_OVERFLOW (arg0)
2327 && ! TREE_CONSTANT_OVERFLOW (arg1)
2328 && REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
2329 TREE_REAL_CST (arg1)));
2332 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2334 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2338 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2339 && ! memcmp (TREE_STRING_POINTER (arg0),
2340 TREE_STRING_POINTER (arg1),
2341 TREE_STRING_LENGTH (arg0)));
2344 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2353 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2356 /* Two conversions are equal only if signedness and modes match. */
2357 if ((TREE_CODE (arg0) == NOP_EXPR || TREE_CODE (arg0) == CONVERT_EXPR)
2358 && (TREE_UNSIGNED (TREE_TYPE (arg0))
2359 != TREE_UNSIGNED (TREE_TYPE (arg1))))
2362 return operand_equal_p (TREE_OPERAND (arg0, 0),
2363 TREE_OPERAND (arg1, 0), 0);
2367 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0)
2368 && operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1),
2372 /* For commutative ops, allow the other order. */
2373 return ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MULT_EXPR
2374 || TREE_CODE (arg0) == MIN_EXPR || TREE_CODE (arg0) == MAX_EXPR
2375 || TREE_CODE (arg0) == BIT_IOR_EXPR
2376 || TREE_CODE (arg0) == BIT_XOR_EXPR
2377 || TREE_CODE (arg0) == BIT_AND_EXPR
2378 || TREE_CODE (arg0) == NE_EXPR || TREE_CODE (arg0) == EQ_EXPR)
2379 && operand_equal_p (TREE_OPERAND (arg0, 0),
2380 TREE_OPERAND (arg1, 1), 0)
2381 && operand_equal_p (TREE_OPERAND (arg0, 1),
2382 TREE_OPERAND (arg1, 0), 0));
2385 /* If either of the pointer (or reference) expressions we are dereferencing
2386 contain a side effect, these cannot be equal. */
2387 if (TREE_SIDE_EFFECTS (arg0)
2388 || TREE_SIDE_EFFECTS (arg1))
2391 switch (TREE_CODE (arg0))
2394 return operand_equal_p (TREE_OPERAND (arg0, 0),
2395 TREE_OPERAND (arg1, 0), 0);
2399 return (operand_equal_p (TREE_OPERAND (arg0, 0),
2400 TREE_OPERAND (arg1, 0), 0)
2401 && operand_equal_p (TREE_OPERAND (arg0, 1),
2402 TREE_OPERAND (arg1, 1), 0));
2405 return (operand_equal_p (TREE_OPERAND (arg0, 0),
2406 TREE_OPERAND (arg1, 0), 0)
2407 && operand_equal_p (TREE_OPERAND (arg0, 1),
2408 TREE_OPERAND (arg1, 1), 0)
2409 && operand_equal_p (TREE_OPERAND (arg0, 2),
2410 TREE_OPERAND (arg1, 2), 0));
2416 if (TREE_CODE (arg0) == RTL_EXPR)
2417 return rtx_equal_p (RTL_EXPR_RTL (arg0), RTL_EXPR_RTL (arg1));
2425 /* Similar to operand_equal_p, but see if ARG0 might have been made by
2426 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
2428 When in doubt, return 0. */
2431 operand_equal_for_comparison_p (arg0, arg1, other)
2435 int unsignedp1, unsignedpo;
2436 tree primarg0, primarg1, primother;
2437 unsigned int correct_width;
2439 if (operand_equal_p (arg0, arg1, 0))
2442 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
2443 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
2446 /* Discard any conversions that don't change the modes of ARG0 and ARG1
2447 and see if the inner values are the same. This removes any
2448 signedness comparison, which doesn't matter here. */
2449 primarg0 = arg0, primarg1 = arg1;
2450 STRIP_NOPS (primarg0);
2451 STRIP_NOPS (primarg1);
2452 if (operand_equal_p (primarg0, primarg1, 0))
2455 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
2456 actual comparison operand, ARG0.
2458 First throw away any conversions to wider types
2459 already present in the operands. */
2461 primarg1 = get_narrower (arg1, &unsignedp1);
2462 primother = get_narrower (other, &unsignedpo);
2464 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
2465 if (unsignedp1 == unsignedpo
2466 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
2467 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
2469 tree type = TREE_TYPE (arg0);
2471 /* Make sure shorter operand is extended the right way
2472 to match the longer operand. */
2473 primarg1 = convert (signed_or_unsigned_type (unsignedp1,
2474 TREE_TYPE (primarg1)),
2477 if (operand_equal_p (arg0, convert (type, primarg1), 0))
2484 /* See if ARG is an expression that is either a comparison or is performing
2485 arithmetic on comparisons. The comparisons must only be comparing
2486 two different values, which will be stored in *CVAL1 and *CVAL2; if
2487 they are non-zero it means that some operands have already been found.
2488 No variables may be used anywhere else in the expression except in the
2489 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
2490 the expression and save_expr needs to be called with CVAL1 and CVAL2.
2492 If this is true, return 1. Otherwise, return zero. */
2495 twoval_comparison_p (arg, cval1, cval2, save_p)
2497 tree *cval1, *cval2;
2500 enum tree_code code = TREE_CODE (arg);
2501 char class = TREE_CODE_CLASS (code);
2503 /* We can handle some of the 'e' cases here. */
2504 if (class == 'e' && code == TRUTH_NOT_EXPR)
2506 else if (class == 'e'
2507 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
2508 || code == COMPOUND_EXPR))
2511 else if (class == 'e' && code == SAVE_EXPR && SAVE_EXPR_RTL (arg) == 0
2512 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
2514 /* If we've already found a CVAL1 or CVAL2, this expression is
2515 two complex to handle. */
2516 if (*cval1 || *cval2)
2526 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
2529 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
2530 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2531 cval1, cval2, save_p));
2537 if (code == COND_EXPR)
2538 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
2539 cval1, cval2, save_p)
2540 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2541 cval1, cval2, save_p)
2542 && twoval_comparison_p (TREE_OPERAND (arg, 2),
2543 cval1, cval2, save_p));
2547 /* First see if we can handle the first operand, then the second. For
2548 the second operand, we know *CVAL1 can't be zero. It must be that
2549 one side of the comparison is each of the values; test for the
2550 case where this isn't true by failing if the two operands
2553 if (operand_equal_p (TREE_OPERAND (arg, 0),
2554 TREE_OPERAND (arg, 1), 0))
2558 *cval1 = TREE_OPERAND (arg, 0);
2559 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
2561 else if (*cval2 == 0)
2562 *cval2 = TREE_OPERAND (arg, 0);
2563 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
2568 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
2570 else if (*cval2 == 0)
2571 *cval2 = TREE_OPERAND (arg, 1);
2572 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
2584 /* ARG is a tree that is known to contain just arithmetic operations and
2585 comparisons. Evaluate the operations in the tree substituting NEW0 for
2586 any occurrence of OLD0 as an operand of a comparison and likewise for
2590 eval_subst (arg, old0, new0, old1, new1)
2592 tree old0, new0, old1, new1;
2594 tree type = TREE_TYPE (arg);
2595 enum tree_code code = TREE_CODE (arg);
2596 char class = TREE_CODE_CLASS (code);
2598 /* We can handle some of the 'e' cases here. */
2599 if (class == 'e' && code == TRUTH_NOT_EXPR)
2601 else if (class == 'e'
2602 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2608 return fold (build1 (code, type,
2609 eval_subst (TREE_OPERAND (arg, 0),
2610 old0, new0, old1, new1)));
2613 return fold (build (code, type,
2614 eval_subst (TREE_OPERAND (arg, 0),
2615 old0, new0, old1, new1),
2616 eval_subst (TREE_OPERAND (arg, 1),
2617 old0, new0, old1, new1)));
2623 return eval_subst (TREE_OPERAND (arg, 0), old0, new0, old1, new1);
2626 return eval_subst (TREE_OPERAND (arg, 1), old0, new0, old1, new1);
2629 return fold (build (code, type,
2630 eval_subst (TREE_OPERAND (arg, 0),
2631 old0, new0, old1, new1),
2632 eval_subst (TREE_OPERAND (arg, 1),
2633 old0, new0, old1, new1),
2634 eval_subst (TREE_OPERAND (arg, 2),
2635 old0, new0, old1, new1)));
2639 /* fall through - ??? */
2643 tree arg0 = TREE_OPERAND (arg, 0);
2644 tree arg1 = TREE_OPERAND (arg, 1);
2646 /* We need to check both for exact equality and tree equality. The
2647 former will be true if the operand has a side-effect. In that
2648 case, we know the operand occurred exactly once. */
2650 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
2652 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
2655 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
2657 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
2660 return fold (build (code, type, arg0, arg1));
2668 /* Return a tree for the case when the result of an expression is RESULT
2669 converted to TYPE and OMITTED was previously an operand of the expression
2670 but is now not needed (e.g., we folded OMITTED * 0).
2672 If OMITTED has side effects, we must evaluate it. Otherwise, just do
2673 the conversion of RESULT to TYPE. */
2676 omit_one_operand (type, result, omitted)
2677 tree type, result, omitted;
2679 tree t = convert (type, result);
2681 if (TREE_SIDE_EFFECTS (omitted))
2682 return build (COMPOUND_EXPR, type, omitted, t);
2684 return non_lvalue (t);
2687 /* Similar, but call pedantic_non_lvalue instead of non_lvalue. */
2690 pedantic_omit_one_operand (type, result, omitted)
2691 tree type, result, omitted;
2693 tree t = convert (type, result);
2695 if (TREE_SIDE_EFFECTS (omitted))
2696 return build (COMPOUND_EXPR, type, omitted, t);
2698 return pedantic_non_lvalue (t);
2701 /* Return a simplified tree node for the truth-negation of ARG. This
2702 never alters ARG itself. We assume that ARG is an operation that
2703 returns a truth value (0 or 1). */
2706 invert_truthvalue (arg)
2709 tree type = TREE_TYPE (arg);
2710 enum tree_code code = TREE_CODE (arg);
2712 if (code == ERROR_MARK)
2715 /* If this is a comparison, we can simply invert it, except for
2716 floating-point non-equality comparisons, in which case we just
2717 enclose a TRUTH_NOT_EXPR around what we have. */
2719 if (TREE_CODE_CLASS (code) == '<')
2721 if (FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg, 0)))
2722 && !flag_unsafe_math_optimizations
2725 return build1 (TRUTH_NOT_EXPR, type, arg);
2727 return build (invert_tree_comparison (code), type,
2728 TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
2734 return convert (type, build_int_2 (integer_zerop (arg), 0));
2736 case TRUTH_AND_EXPR:
2737 return build (TRUTH_OR_EXPR, type,
2738 invert_truthvalue (TREE_OPERAND (arg, 0)),
2739 invert_truthvalue (TREE_OPERAND (arg, 1)));
2742 return build (TRUTH_AND_EXPR, type,
2743 invert_truthvalue (TREE_OPERAND (arg, 0)),
2744 invert_truthvalue (TREE_OPERAND (arg, 1)));
2746 case TRUTH_XOR_EXPR:
2747 /* Here we can invert either operand. We invert the first operand
2748 unless the second operand is a TRUTH_NOT_EXPR in which case our
2749 result is the XOR of the first operand with the inside of the
2750 negation of the second operand. */
2752 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
2753 return build (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
2754 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
2756 return build (TRUTH_XOR_EXPR, type,
2757 invert_truthvalue (TREE_OPERAND (arg, 0)),
2758 TREE_OPERAND (arg, 1));
2760 case TRUTH_ANDIF_EXPR:
2761 return build (TRUTH_ORIF_EXPR, type,
2762 invert_truthvalue (TREE_OPERAND (arg, 0)),
2763 invert_truthvalue (TREE_OPERAND (arg, 1)));
2765 case TRUTH_ORIF_EXPR:
2766 return build (TRUTH_ANDIF_EXPR, type,
2767 invert_truthvalue (TREE_OPERAND (arg, 0)),
2768 invert_truthvalue (TREE_OPERAND (arg, 1)));
2770 case TRUTH_NOT_EXPR:
2771 return TREE_OPERAND (arg, 0);
2774 return build (COND_EXPR, type, TREE_OPERAND (arg, 0),
2775 invert_truthvalue (TREE_OPERAND (arg, 1)),
2776 invert_truthvalue (TREE_OPERAND (arg, 2)));
2779 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0),
2780 invert_truthvalue (TREE_OPERAND (arg, 1)));
2782 case WITH_RECORD_EXPR:
2783 return build (WITH_RECORD_EXPR, type,
2784 invert_truthvalue (TREE_OPERAND (arg, 0)),
2785 TREE_OPERAND (arg, 1));
2787 case NON_LVALUE_EXPR:
2788 return invert_truthvalue (TREE_OPERAND (arg, 0));
2793 return build1 (TREE_CODE (arg), type,
2794 invert_truthvalue (TREE_OPERAND (arg, 0)));
2797 if (!integer_onep (TREE_OPERAND (arg, 1)))
2799 return build (EQ_EXPR, type, arg, convert (type, integer_zero_node));
2802 return build1 (TRUTH_NOT_EXPR, type, arg);
2804 case CLEANUP_POINT_EXPR:
2805 return build1 (CLEANUP_POINT_EXPR, type,
2806 invert_truthvalue (TREE_OPERAND (arg, 0)));
2811 if (TREE_CODE (TREE_TYPE (arg)) != BOOLEAN_TYPE)
2813 return build1 (TRUTH_NOT_EXPR, type, arg);
2816 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
2817 operands are another bit-wise operation with a common input. If so,
2818 distribute the bit operations to save an operation and possibly two if
2819 constants are involved. For example, convert
2820 (A | B) & (A | C) into A | (B & C)
2821 Further simplification will occur if B and C are constants.
2823 If this optimization cannot be done, 0 will be returned. */
2826 distribute_bit_expr (code, type, arg0, arg1)
2827 enum tree_code code;
2834 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2835 || TREE_CODE (arg0) == code
2836 || (TREE_CODE (arg0) != BIT_AND_EXPR
2837 && TREE_CODE (arg0) != BIT_IOR_EXPR))
2840 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
2842 common = TREE_OPERAND (arg0, 0);
2843 left = TREE_OPERAND (arg0, 1);
2844 right = TREE_OPERAND (arg1, 1);
2846 else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
2848 common = TREE_OPERAND (arg0, 0);
2849 left = TREE_OPERAND (arg0, 1);
2850 right = TREE_OPERAND (arg1, 0);
2852 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
2854 common = TREE_OPERAND (arg0, 1);
2855 left = TREE_OPERAND (arg0, 0);
2856 right = TREE_OPERAND (arg1, 1);
2858 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
2860 common = TREE_OPERAND (arg0, 1);
2861 left = TREE_OPERAND (arg0, 0);
2862 right = TREE_OPERAND (arg1, 0);
2867 return fold (build (TREE_CODE (arg0), type, common,
2868 fold (build (code, type, left, right))));
2871 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
2872 starting at BITPOS. The field is unsigned if UNSIGNEDP is non-zero. */
2875 make_bit_field_ref (inner, type, bitsize, bitpos, unsignedp)
2878 int bitsize, bitpos;
2881 tree result = build (BIT_FIELD_REF, type, inner,
2882 size_int (bitsize), bitsize_int (bitpos));
2884 TREE_UNSIGNED (result) = unsignedp;
2889 /* Optimize a bit-field compare.
2891 There are two cases: First is a compare against a constant and the
2892 second is a comparison of two items where the fields are at the same
2893 bit position relative to the start of a chunk (byte, halfword, word)
2894 large enough to contain it. In these cases we can avoid the shift
2895 implicit in bitfield extractions.
2897 For constants, we emit a compare of the shifted constant with the
2898 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
2899 compared. For two fields at the same position, we do the ANDs with the
2900 similar mask and compare the result of the ANDs.
2902 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
2903 COMPARE_TYPE is the type of the comparison, and LHS and RHS
2904 are the left and right operands of the comparison, respectively.
2906 If the optimization described above can be done, we return the resulting
2907 tree. Otherwise we return zero. */
2910 optimize_bit_field_compare (code, compare_type, lhs, rhs)
2911 enum tree_code code;
2915 HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
2916 tree type = TREE_TYPE (lhs);
2917 tree signed_type, unsigned_type;
2918 int const_p = TREE_CODE (rhs) == INTEGER_CST;
2919 enum machine_mode lmode, rmode, nmode;
2920 int lunsignedp, runsignedp;
2921 int lvolatilep = 0, rvolatilep = 0;
2922 unsigned int alignment;
2923 tree linner, rinner = NULL_TREE;
2927 /* Get all the information about the extractions being done. If the bit size
2928 if the same as the size of the underlying object, we aren't doing an
2929 extraction at all and so can do nothing. We also don't want to
2930 do anything if the inner expression is a PLACEHOLDER_EXPR since we
2931 then will no longer be able to replace it. */
2932 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
2933 &lunsignedp, &lvolatilep, &alignment);
2934 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
2935 || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
2940 /* If this is not a constant, we can only do something if bit positions,
2941 sizes, and signedness are the same. */
2942 rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
2943 &runsignedp, &rvolatilep, &alignment);
2945 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
2946 || lunsignedp != runsignedp || offset != 0
2947 || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
2951 /* See if we can find a mode to refer to this field. We should be able to,
2952 but fail if we can't. */
2953 nmode = get_best_mode (lbitsize, lbitpos,
2954 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
2955 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
2956 TYPE_ALIGN (TREE_TYPE (rinner))),
2957 word_mode, lvolatilep || rvolatilep);
2958 if (nmode == VOIDmode)
2961 /* Set signed and unsigned types of the precision of this mode for the
2963 signed_type = type_for_mode (nmode, 0);
2964 unsigned_type = type_for_mode (nmode, 1);
2966 /* Compute the bit position and size for the new reference and our offset
2967 within it. If the new reference is the same size as the original, we
2968 won't optimize anything, so return zero. */
2969 nbitsize = GET_MODE_BITSIZE (nmode);
2970 nbitpos = lbitpos & ~ (nbitsize - 1);
2972 if (nbitsize == lbitsize)
2975 if (BYTES_BIG_ENDIAN)
2976 lbitpos = nbitsize - lbitsize - lbitpos;
2978 /* Make the mask to be used against the extracted field. */
2979 mask = build_int_2 (~0, ~0);
2980 TREE_TYPE (mask) = unsigned_type;
2981 force_fit_type (mask, 0);
2982 mask = convert (unsigned_type, mask);
2983 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize), 0);
2984 mask = const_binop (RSHIFT_EXPR, mask,
2985 size_int (nbitsize - lbitsize - lbitpos), 0);
2988 /* If not comparing with constant, just rework the comparison
2990 return build (code, compare_type,
2991 build (BIT_AND_EXPR, unsigned_type,
2992 make_bit_field_ref (linner, unsigned_type,
2993 nbitsize, nbitpos, 1),
2995 build (BIT_AND_EXPR, unsigned_type,
2996 make_bit_field_ref (rinner, unsigned_type,
2997 nbitsize, nbitpos, 1),
3000 /* Otherwise, we are handling the constant case. See if the constant is too
3001 big for the field. Warn and return a tree of for 0 (false) if so. We do
3002 this not only for its own sake, but to avoid having to test for this
3003 error case below. If we didn't, we might generate wrong code.
3005 For unsigned fields, the constant shifted right by the field length should
3006 be all zero. For signed fields, the high-order bits should agree with
3011 if (! integer_zerop (const_binop (RSHIFT_EXPR,
3012 convert (unsigned_type, rhs),
3013 size_int (lbitsize), 0)))
3015 warning ("comparison is always %d due to width of bitfield",
3017 return convert (compare_type,
3019 ? integer_one_node : integer_zero_node));
3024 tree tem = const_binop (RSHIFT_EXPR, convert (signed_type, rhs),
3025 size_int (lbitsize - 1), 0);
3026 if (! integer_zerop (tem) && ! integer_all_onesp (tem))
3028 warning ("comparison is always %d due to width of bitfield",
3030 return convert (compare_type,
3032 ? integer_one_node : integer_zero_node));
3036 /* Single-bit compares should always be against zero. */
3037 if (lbitsize == 1 && ! integer_zerop (rhs))
3039 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
3040 rhs = convert (type, integer_zero_node);
3043 /* Make a new bitfield reference, shift the constant over the
3044 appropriate number of bits and mask it with the computed mask
3045 (in case this was a signed field). If we changed it, make a new one. */
3046 lhs = make_bit_field_ref (linner, unsigned_type, nbitsize, nbitpos, 1);
3049 TREE_SIDE_EFFECTS (lhs) = 1;
3050 TREE_THIS_VOLATILE (lhs) = 1;
3053 rhs = fold (const_binop (BIT_AND_EXPR,
3054 const_binop (LSHIFT_EXPR,
3055 convert (unsigned_type, rhs),
3056 size_int (lbitpos), 0),
3059 return build (code, compare_type,
3060 build (BIT_AND_EXPR, unsigned_type, lhs, mask),
3064 /* Subroutine for fold_truthop: decode a field reference.
3066 If EXP is a comparison reference, we return the innermost reference.
3068 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3069 set to the starting bit number.
3071 If the innermost field can be completely contained in a mode-sized
3072 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
3074 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3075 otherwise it is not changed.
3077 *PUNSIGNEDP is set to the signedness of the field.
3079 *PMASK is set to the mask used. This is either contained in a
3080 BIT_AND_EXPR or derived from the width of the field.
3082 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
3084 Return 0 if this is not a component reference or is one that we can't
3085 do anything with. */
3088 decode_field_reference (exp, pbitsize, pbitpos, pmode, punsignedp,
3089 pvolatilep, pmask, pand_mask)
3091 HOST_WIDE_INT *pbitsize, *pbitpos;
3092 enum machine_mode *pmode;
3093 int *punsignedp, *pvolatilep;
3098 tree mask, inner, offset;
3100 unsigned int precision;
3101 unsigned int alignment;
3103 /* All the optimizations using this function assume integer fields.
3104 There are problems with FP fields since the type_for_size call
3105 below can fail for, e.g., XFmode. */
3106 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
3111 if (TREE_CODE (exp) == BIT_AND_EXPR)
3113 and_mask = TREE_OPERAND (exp, 1);
3114 exp = TREE_OPERAND (exp, 0);
3115 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
3116 if (TREE_CODE (and_mask) != INTEGER_CST)
3120 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
3121 punsignedp, pvolatilep, &alignment);
3122 if ((inner == exp && and_mask == 0)
3123 || *pbitsize < 0 || offset != 0
3124 || TREE_CODE (inner) == PLACEHOLDER_EXPR)
3127 /* Compute the mask to access the bitfield. */
3128 unsigned_type = type_for_size (*pbitsize, 1);
3129 precision = TYPE_PRECISION (unsigned_type);
3131 mask = build_int_2 (~0, ~0);
3132 TREE_TYPE (mask) = unsigned_type;
3133 force_fit_type (mask, 0);
3134 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3135 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3137 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
3139 mask = fold (build (BIT_AND_EXPR, unsigned_type,
3140 convert (unsigned_type, and_mask), mask));
3143 *pand_mask = and_mask;
3147 /* Return non-zero if MASK represents a mask of SIZE ones in the low-order
3151 all_ones_mask_p (mask, size)
3155 tree type = TREE_TYPE (mask);
3156 unsigned int precision = TYPE_PRECISION (type);
3159 tmask = build_int_2 (~0, ~0);
3160 TREE_TYPE (tmask) = signed_type (type);
3161 force_fit_type (tmask, 0);
3163 tree_int_cst_equal (mask,
3164 const_binop (RSHIFT_EXPR,
3165 const_binop (LSHIFT_EXPR, tmask,
3166 size_int (precision - size),
3168 size_int (precision - size), 0));
3171 /* Subroutine for fold_truthop: determine if an operand is simple enough
3172 to be evaluated unconditionally. */
3175 simple_operand_p (exp)
3178 /* Strip any conversions that don't change the machine mode. */
3179 while ((TREE_CODE (exp) == NOP_EXPR
3180 || TREE_CODE (exp) == CONVERT_EXPR)
3181 && (TYPE_MODE (TREE_TYPE (exp))
3182 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)))))
3183 exp = TREE_OPERAND (exp, 0);
3185 return (TREE_CODE_CLASS (TREE_CODE (exp)) == 'c'
3187 && ! TREE_ADDRESSABLE (exp)
3188 && ! TREE_THIS_VOLATILE (exp)
3189 && ! DECL_NONLOCAL (exp)
3190 /* Don't regard global variables as simple. They may be
3191 allocated in ways unknown to the compiler (shared memory,
3192 #pragma weak, etc). */
3193 && ! TREE_PUBLIC (exp)
3194 && ! DECL_EXTERNAL (exp)
3195 /* Loading a static variable is unduly expensive, but global
3196 registers aren't expensive. */
3197 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
3200 /* The following functions are subroutines to fold_range_test and allow it to
3201 try to change a logical combination of comparisons into a range test.
3204 X == 2 || X == 3 || X == 4 || X == 5
3208 (unsigned) (X - 2) <= 3
3210 We describe each set of comparisons as being either inside or outside
3211 a range, using a variable named like IN_P, and then describe the
3212 range with a lower and upper bound. If one of the bounds is omitted,
3213 it represents either the highest or lowest value of the type.
3215 In the comments below, we represent a range by two numbers in brackets
3216 preceded by a "+" to designate being inside that range, or a "-" to
3217 designate being outside that range, so the condition can be inverted by
3218 flipping the prefix. An omitted bound is represented by a "-". For
3219 example, "- [-, 10]" means being outside the range starting at the lowest
3220 possible value and ending at 10, in other words, being greater than 10.
3221 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
3224 We set up things so that the missing bounds are handled in a consistent
3225 manner so neither a missing bound nor "true" and "false" need to be
3226 handled using a special case. */
3228 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
3229 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
3230 and UPPER1_P are nonzero if the respective argument is an upper bound
3231 and zero for a lower. TYPE, if nonzero, is the type of the result; it
3232 must be specified for a comparison. ARG1 will be converted to ARG0's
3233 type if both are specified. */
3236 range_binop (code, type, arg0, upper0_p, arg1, upper1_p)
3237 enum tree_code code;
3240 int upper0_p, upper1_p;
3246 /* If neither arg represents infinity, do the normal operation.
3247 Else, if not a comparison, return infinity. Else handle the special
3248 comparison rules. Note that most of the cases below won't occur, but
3249 are handled for consistency. */
3251 if (arg0 != 0 && arg1 != 0)
3253 tem = fold (build (code, type != 0 ? type : TREE_TYPE (arg0),
3254 arg0, convert (TREE_TYPE (arg0), arg1)));
3256 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
3259 if (TREE_CODE_CLASS (code) != '<')
3262 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
3263 for neither. In real maths, we cannot assume open ended ranges are
3264 the same. But, this is computer arithmetic, where numbers are finite.
3265 We can therefore make the transformation of any unbounded range with
3266 the value Z, Z being greater than any representable number. This permits
3267 us to treat unbounded ranges as equal. */
3268 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
3269 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
3273 result = sgn0 == sgn1;
3276 result = sgn0 != sgn1;
3279 result = sgn0 < sgn1;
3282 result = sgn0 <= sgn1;
3285 result = sgn0 > sgn1;
3288 result = sgn0 >= sgn1;
3294 return convert (type, result ? integer_one_node : integer_zero_node);
3297 /* Given EXP, a logical expression, set the range it is testing into
3298 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
3299 actually being tested. *PLOW and *PHIGH will be made of the same type
3300 as the returned expression. If EXP is not a comparison, we will most
3301 likely not be returning a useful value and range. */
3304 make_range (exp, pin_p, plow, phigh)
3309 enum tree_code code;
3310 tree arg0 = NULL_TREE, arg1 = NULL_TREE, type = NULL_TREE;
3311 tree orig_type = NULL_TREE;
3313 tree low, high, n_low, n_high;
3315 /* Start with simply saying "EXP != 0" and then look at the code of EXP
3316 and see if we can refine the range. Some of the cases below may not
3317 happen, but it doesn't seem worth worrying about this. We "continue"
3318 the outer loop when we've changed something; otherwise we "break"
3319 the switch, which will "break" the while. */
3321 in_p = 0, low = high = convert (TREE_TYPE (exp), integer_zero_node);
3325 code = TREE_CODE (exp);
3327 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
3329 arg0 = TREE_OPERAND (exp, 0);
3330 if (TREE_CODE_CLASS (code) == '<'
3331 || TREE_CODE_CLASS (code) == '1'
3332 || TREE_CODE_CLASS (code) == '2')
3333 type = TREE_TYPE (arg0);
3334 if (TREE_CODE_CLASS (code) == '2'
3335 || TREE_CODE_CLASS (code) == '<'
3336 || (TREE_CODE_CLASS (code) == 'e'
3337 && TREE_CODE_LENGTH (code) > 1))
3338 arg1 = TREE_OPERAND (exp, 1);
3341 /* Set ORIG_TYPE as soon as TYPE is non-null so that we do not
3342 lose a cast by accident. */
3343 if (type != NULL_TREE && orig_type == NULL_TREE)
3348 case TRUTH_NOT_EXPR:
3349 in_p = ! in_p, exp = arg0;
3352 case EQ_EXPR: case NE_EXPR:
3353 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
3354 /* We can only do something if the range is testing for zero
3355 and if the second operand is an integer constant. Note that
3356 saying something is "in" the range we make is done by
3357 complementing IN_P since it will set in the initial case of
3358 being not equal to zero; "out" is leaving it alone. */
3359 if (low == 0 || high == 0
3360 || ! integer_zerop (low) || ! integer_zerop (high)
3361 || TREE_CODE (arg1) != INTEGER_CST)
3366 case NE_EXPR: /* - [c, c] */
3369 case EQ_EXPR: /* + [c, c] */
3370 in_p = ! in_p, low = high = arg1;
3372 case GT_EXPR: /* - [-, c] */
3373 low = 0, high = arg1;
3375 case GE_EXPR: /* + [c, -] */
3376 in_p = ! in_p, low = arg1, high = 0;
3378 case LT_EXPR: /* - [c, -] */
3379 low = arg1, high = 0;
3381 case LE_EXPR: /* + [-, c] */
3382 in_p = ! in_p, low = 0, high = arg1;
3390 /* If this is an unsigned comparison, we also know that EXP is
3391 greater than or equal to zero. We base the range tests we make
3392 on that fact, so we record it here so we can parse existing
3394 if (TREE_UNSIGNED (type) && (low == 0 || high == 0))
3396 if (! merge_ranges (&n_in_p, &n_low, &n_high, in_p, low, high,
3397 1, convert (type, integer_zero_node),
3401 in_p = n_in_p, low = n_low, high = n_high;
3403 /* If the high bound is missing, but we
3404 have a low bound, reverse the range so
3405 it goes from zero to the low bound minus 1. */
3406 if (high == 0 && low)
3409 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
3410 integer_one_node, 0);
3411 low = convert (type, integer_zero_node);
3417 /* (-x) IN [a,b] -> x in [-b, -a] */
3418 n_low = range_binop (MINUS_EXPR, type,
3419 convert (type, integer_zero_node), 0, high, 1);
3420 n_high = range_binop (MINUS_EXPR, type,
3421 convert (type, integer_zero_node), 0, low, 0);
3422 low = n_low, high = n_high;
3428 exp = build (MINUS_EXPR, type, negate_expr (arg0),
3429 convert (type, integer_one_node));
3432 case PLUS_EXPR: case MINUS_EXPR:
3433 if (TREE_CODE (arg1) != INTEGER_CST)
3436 /* If EXP is signed, any overflow in the computation is undefined,
3437 so we don't worry about it so long as our computations on
3438 the bounds don't overflow. For unsigned, overflow is defined
3439 and this is exactly the right thing. */
3440 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3441 type, low, 0, arg1, 0);
3442 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3443 type, high, 1, arg1, 0);
3444 if ((n_low != 0 && TREE_OVERFLOW (n_low))
3445 || (n_high != 0 && TREE_OVERFLOW (n_high)))
3448 /* Check for an unsigned range which has wrapped around the maximum
3449 value thus making n_high < n_low, and normalize it. */
3450 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
3452 low = range_binop (PLUS_EXPR, type, n_high, 0,
3453 integer_one_node, 0);
3454 high = range_binop (MINUS_EXPR, type, n_low, 0,
3455 integer_one_node, 0);
3457 /* If the range is of the form +/- [ x+1, x ], we won't
3458 be able to normalize it. But then, it represents the
3459 whole range or the empty set, so make it
3461 if (tree_int_cst_equal (n_low, low)
3462 && tree_int_cst_equal (n_high, high))
3468 low = n_low, high = n_high;
3473 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
3474 if (TYPE_PRECISION (type) > TYPE_PRECISION (orig_type))
3477 if (! INTEGRAL_TYPE_P (type)
3478 || (low != 0 && ! int_fits_type_p (low, type))
3479 || (high != 0 && ! int_fits_type_p (high, type)))
3482 n_low = low, n_high = high;
3485 n_low = convert (type, n_low);
3488 n_high = convert (type, n_high);
3490 /* If we're converting from an unsigned to a signed type,
3491 we will be doing the comparison as unsigned. The tests above
3492 have already verified that LOW and HIGH are both positive.
3494 So we have to make sure that the original unsigned value will
3495 be interpreted as positive. */
3496 if (TREE_UNSIGNED (type) && ! TREE_UNSIGNED (TREE_TYPE (exp)))
3498 tree equiv_type = type_for_mode (TYPE_MODE (type), 1);
3501 /* A range without an upper bound is, naturally, unbounded.
3502 Since convert would have cropped a very large value, use
3503 the max value for the destination type. */
3505 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
3506 : TYPE_MAX_VALUE (type);
3508 high_positive = fold (build (RSHIFT_EXPR, type,
3509 convert (type, high_positive),
3510 convert (type, integer_one_node)));
3512 /* If the low bound is specified, "and" the range with the
3513 range for which the original unsigned value will be
3517 if (! merge_ranges (&n_in_p, &n_low, &n_high,
3519 1, convert (type, integer_zero_node),
3523 in_p = (n_in_p == in_p);
3527 /* Otherwise, "or" the range with the range of the input
3528 that will be interpreted as negative. */
3529 if (! merge_ranges (&n_in_p, &n_low, &n_high,
3531 1, convert (type, integer_zero_node),
3535 in_p = (in_p != n_in_p);
3540 low = n_low, high = n_high;
3550 /* If EXP is a constant, we can evaluate whether this is true or false. */
3551 if (TREE_CODE (exp) == INTEGER_CST)
3553 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
3555 && integer_onep (range_binop (LE_EXPR, integer_type_node,
3561 *pin_p = in_p, *plow = low, *phigh = high;
3565 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
3566 type, TYPE, return an expression to test if EXP is in (or out of, depending
3567 on IN_P) the range. */
3570 build_range_check (type, exp, in_p, low, high)
3576 tree etype = TREE_TYPE (exp);
3580 && (0 != (value = build_range_check (type, exp, 1, low, high))))
3581 return invert_truthvalue (value);
3583 else if (low == 0 && high == 0)
3584 return convert (type, integer_one_node);
3587 return fold (build (LE_EXPR, type, exp, high));
3590 return fold (build (GE_EXPR, type, exp, low));
3592 else if (operand_equal_p (low, high, 0))
3593 return fold (build (EQ_EXPR, type, exp, low));
3595 else if (TREE_UNSIGNED (etype) && integer_zerop (low))
3596 return build_range_check (type, exp, 1, 0, high);
3598 else if (integer_zerop (low))
3600 utype = unsigned_type (etype);
3601 return build_range_check (type, convert (utype, exp), 1, 0,
3602 convert (utype, high));
3605 else if (0 != (value = const_binop (MINUS_EXPR, high, low, 0))
3606 && ! TREE_OVERFLOW (value))
3607 return build_range_check (type,
3608 fold (build (MINUS_EXPR, etype, exp, low)),
3609 1, convert (etype, integer_zero_node), value);
3614 /* Given two ranges, see if we can merge them into one. Return 1 if we
3615 can, 0 if we can't. Set the output range into the specified parameters. */
3618 merge_ranges (pin_p, plow, phigh, in0_p, low0, high0, in1_p, low1, high1)
3622 tree low0, high0, low1, high1;
3630 int lowequal = ((low0 == 0 && low1 == 0)
3631 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
3632 low0, 0, low1, 0)));
3633 int highequal = ((high0 == 0 && high1 == 0)
3634 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
3635 high0, 1, high1, 1)));
3637 /* Make range 0 be the range that starts first, or ends last if they
3638 start at the same value. Swap them if it isn't. */
3639 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
3642 && integer_onep (range_binop (GT_EXPR, integer_type_node,
3643 high1, 1, high0, 1))))
3645 temp = in0_p, in0_p = in1_p, in1_p = temp;
3646 tem = low0, low0 = low1, low1 = tem;
3647 tem = high0, high0 = high1, high1 = tem;
3650 /* Now flag two cases, whether the ranges are disjoint or whether the
3651 second range is totally subsumed in the first. Note that the tests
3652 below are simplified by the ones above. */
3653 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
3654 high0, 1, low1, 0));
3655 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
3656 high1, 1, high0, 1));
3658 /* We now have four cases, depending on whether we are including or
3659 excluding the two ranges. */
3662 /* If they don't overlap, the result is false. If the second range
3663 is a subset it is the result. Otherwise, the range is from the start
3664 of the second to the end of the first. */
3666 in_p = 0, low = high = 0;
3668 in_p = 1, low = low1, high = high1;
3670 in_p = 1, low = low1, high = high0;
3673 else if (in0_p && ! in1_p)
3675 /* If they don't overlap, the result is the first range. If they are
3676 equal, the result is false. If the second range is a subset of the
3677 first, and the ranges begin at the same place, we go from just after
3678 the end of the first range to the end of the second. If the second
3679 range is not a subset of the first, or if it is a subset and both
3680 ranges end at the same place, the range starts at the start of the
3681 first range and ends just before the second range.
3682 Otherwise, we can't describe this as a single range. */
3684 in_p = 1, low = low0, high = high0;
3685 else if (lowequal && highequal)
3686 in_p = 0, low = high = 0;
3687 else if (subset && lowequal)
3689 in_p = 1, high = high0;
3690 low = range_binop (PLUS_EXPR, NULL_TREE, high1, 0,
3691 integer_one_node, 0);
3693 else if (! subset || highequal)
3695 in_p = 1, low = low0;
3696 high = range_binop (MINUS_EXPR, NULL_TREE, low1, 0,
3697 integer_one_node, 0);
3703 else if (! in0_p && in1_p)
3705 /* If they don't overlap, the result is the second range. If the second
3706 is a subset of the first, the result is false. Otherwise,
3707 the range starts just after the first range and ends at the
3708 end of the second. */
3710 in_p = 1, low = low1, high = high1;
3711 else if (subset || highequal)
3712 in_p = 0, low = high = 0;
3715 in_p = 1, high = high1;
3716 low = range_binop (PLUS_EXPR, NULL_TREE, high0, 1,
3717 integer_one_node, 0);
3723 /* The case where we are excluding both ranges. Here the complex case
3724 is if they don't overlap. In that case, the only time we have a
3725 range is if they are adjacent. If the second is a subset of the
3726 first, the result is the first. Otherwise, the range to exclude
3727 starts at the beginning of the first range and ends at the end of the
3731 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
3732 range_binop (PLUS_EXPR, NULL_TREE,
3734 integer_one_node, 1),
3736 in_p = 0, low = low0, high = high1;
3741 in_p = 0, low = low0, high = high0;
3743 in_p = 0, low = low0, high = high1;
3746 *pin_p = in_p, *plow = low, *phigh = high;
3750 /* EXP is some logical combination of boolean tests. See if we can
3751 merge it into some range test. Return the new tree if so. */
3754 fold_range_test (exp)
3757 int or_op = (TREE_CODE (exp) == TRUTH_ORIF_EXPR
3758 || TREE_CODE (exp) == TRUTH_OR_EXPR);
3759 int in0_p, in1_p, in_p;
3760 tree low0, low1, low, high0, high1, high;
3761 tree lhs = make_range (TREE_OPERAND (exp, 0), &in0_p, &low0, &high0);
3762 tree rhs = make_range (TREE_OPERAND (exp, 1), &in1_p, &low1, &high1);
3765 /* If this is an OR operation, invert both sides; we will invert
3766 again at the end. */
3768 in0_p = ! in0_p, in1_p = ! in1_p;
3770 /* If both expressions are the same, if we can merge the ranges, and we
3771 can build the range test, return it or it inverted. If one of the
3772 ranges is always true or always false, consider it to be the same
3773 expression as the other. */
3774 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
3775 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
3777 && 0 != (tem = (build_range_check (TREE_TYPE (exp),
3779 : rhs != 0 ? rhs : integer_zero_node,
3781 return or_op ? invert_truthvalue (tem) : tem;
3783 /* On machines where the branch cost is expensive, if this is a
3784 short-circuited branch and the underlying object on both sides
3785 is the same, make a non-short-circuit operation. */
3786 else if (BRANCH_COST >= 2
3787 && lhs != 0 && rhs != 0
3788 && (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
3789 || TREE_CODE (exp) == TRUTH_ORIF_EXPR)
3790 && operand_equal_p (lhs, rhs, 0))
3792 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
3793 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
3794 which cases we can't do this. */
3795 if (simple_operand_p (lhs))
3796 return build (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
3797 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
3798 TREE_TYPE (exp), TREE_OPERAND (exp, 0),
3799 TREE_OPERAND (exp, 1));
3801 else if (global_bindings_p () == 0
3802 && ! contains_placeholder_p (lhs))
3804 tree common = save_expr (lhs);
3806 if (0 != (lhs = build_range_check (TREE_TYPE (exp), common,
3807 or_op ? ! in0_p : in0_p,
3809 && (0 != (rhs = build_range_check (TREE_TYPE (exp), common,
3810 or_op ? ! in1_p : in1_p,
3812 return build (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
3813 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
3814 TREE_TYPE (exp), lhs, rhs);
3821 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
3822 bit value. Arrange things so the extra bits will be set to zero if and
3823 only if C is signed-extended to its full width. If MASK is nonzero,
3824 it is an INTEGER_CST that should be AND'ed with the extra bits. */
3827 unextend (c, p, unsignedp, mask)
3833 tree type = TREE_TYPE (c);
3834 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
3837 if (p == modesize || unsignedp)
3840 /* We work by getting just the sign bit into the low-order bit, then
3841 into the high-order bit, then sign-extend. We then XOR that value
3843 temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
3844 temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
3846 /* We must use a signed type in order to get an arithmetic right shift.
3847 However, we must also avoid introducing accidental overflows, so that
3848 a subsequent call to integer_zerop will work. Hence we must
3849 do the type conversion here. At this point, the constant is either
3850 zero or one, and the conversion to a signed type can never overflow.
3851 We could get an overflow if this conversion is done anywhere else. */
3852 if (TREE_UNSIGNED (type))
3853 temp = convert (signed_type (type), temp);
3855 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
3856 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
3858 temp = const_binop (BIT_AND_EXPR, temp, convert (TREE_TYPE (c), mask), 0);
3859 /* If necessary, convert the type back to match the type of C. */
3860 if (TREE_UNSIGNED (type))
3861 temp = convert (type, temp);
3863 return convert (type, const_binop (BIT_XOR_EXPR, c, temp, 0));
3866 /* Find ways of folding logical expressions of LHS and RHS:
3867 Try to merge two comparisons to the same innermost item.
3868 Look for range tests like "ch >= '0' && ch <= '9'".
3869 Look for combinations of simple terms on machines with expensive branches
3870 and evaluate the RHS unconditionally.
3872 For example, if we have p->a == 2 && p->b == 4 and we can make an
3873 object large enough to span both A and B, we can do this with a comparison
3874 against the object ANDed with the a mask.
3876 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
3877 operations to do this with one comparison.
3879 We check for both normal comparisons and the BIT_AND_EXPRs made this by
3880 function and the one above.
3882 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
3883 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
3885 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
3888 We return the simplified tree or 0 if no optimization is possible. */
3891 fold_truthop (code, truth_type, lhs, rhs)
3892 enum tree_code code;
3893 tree truth_type, lhs, rhs;
3895 /* If this is the "or" of two comparisons, we can do something if
3896 the comparisons are NE_EXPR. If this is the "and", we can do something
3897 if the comparisons are EQ_EXPR. I.e.,
3898 (a->b == 2 && a->c == 4) can become (a->new == NEW).
3900 WANTED_CODE is this operation code. For single bit fields, we can
3901 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
3902 comparison for one-bit fields. */
3904 enum tree_code wanted_code;
3905 enum tree_code lcode, rcode;
3906 tree ll_arg, lr_arg, rl_arg, rr_arg;
3907 tree ll_inner, lr_inner, rl_inner, rr_inner;
3908 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
3909 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
3910 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
3911 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
3912 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
3913 enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
3914 enum machine_mode lnmode, rnmode;
3915 tree ll_mask, lr_mask, rl_mask, rr_mask;
3916 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
3917 tree l_const, r_const;
3918 tree lntype, rntype, result;
3919 int first_bit, end_bit;
3922 /* Start by getting the comparison codes. Fail if anything is volatile.
3923 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
3924 it were surrounded with a NE_EXPR. */
3926 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
3929 lcode = TREE_CODE (lhs);
3930 rcode = TREE_CODE (rhs);
3932 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
3933 lcode = NE_EXPR, lhs = build (NE_EXPR, truth_type, lhs, integer_zero_node);
3935 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
3936 rcode = NE_EXPR, rhs = build (NE_EXPR, truth_type, rhs, integer_zero_node);
3938 if (TREE_CODE_CLASS (lcode) != '<' || TREE_CODE_CLASS (rcode) != '<')
3941 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
3942 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
3944 ll_arg = TREE_OPERAND (lhs, 0);
3945 lr_arg = TREE_OPERAND (lhs, 1);
3946 rl_arg = TREE_OPERAND (rhs, 0);
3947 rr_arg = TREE_OPERAND (rhs, 1);
3949 /* If the RHS can be evaluated unconditionally and its operands are
3950 simple, it wins to evaluate the RHS unconditionally on machines
3951 with expensive branches. In this case, this isn't a comparison
3952 that can be merged. Avoid doing this if the RHS is a floating-point
3953 comparison since those can trap. */
3955 if (BRANCH_COST >= 2
3956 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
3957 && simple_operand_p (rl_arg)
3958 && simple_operand_p (rr_arg))
3959 return build (code, truth_type, lhs, rhs);
3961 /* See if the comparisons can be merged. Then get all the parameters for
3964 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
3965 || (rcode != EQ_EXPR && rcode != NE_EXPR))
3969 ll_inner = decode_field_reference (ll_arg,
3970 &ll_bitsize, &ll_bitpos, &ll_mode,
3971 &ll_unsignedp, &volatilep, &ll_mask,
3973 lr_inner = decode_field_reference (lr_arg,
3974 &lr_bitsize, &lr_bitpos, &lr_mode,
3975 &lr_unsignedp, &volatilep, &lr_mask,
3977 rl_inner = decode_field_reference (rl_arg,
3978 &rl_bitsize, &rl_bitpos, &rl_mode,
3979 &rl_unsignedp, &volatilep, &rl_mask,
3981 rr_inner = decode_field_reference (rr_arg,
3982 &rr_bitsize, &rr_bitpos, &rr_mode,
3983 &rr_unsignedp, &volatilep, &rr_mask,
3986 /* It must be true that the inner operation on the lhs of each
3987 comparison must be the same if we are to be able to do anything.
3988 Then see if we have constants. If not, the same must be true for
3990 if (volatilep || ll_inner == 0 || rl_inner == 0
3991 || ! operand_equal_p (ll_inner, rl_inner, 0))
3994 if (TREE_CODE (lr_arg) == INTEGER_CST
3995 && TREE_CODE (rr_arg) == INTEGER_CST)
3996 l_const = lr_arg, r_const = rr_arg;
3997 else if (lr_inner == 0 || rr_inner == 0
3998 || ! operand_equal_p (lr_inner, rr_inner, 0))
4001 l_const = r_const = 0;
4003 /* If either comparison code is not correct for our logical operation,
4004 fail. However, we can convert a one-bit comparison against zero into
4005 the opposite comparison against that bit being set in the field. */
4007 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
4008 if (lcode != wanted_code)
4010 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
4012 /* Make the left operand unsigned, since we are only interested
4013 in the value of one bit. Otherwise we are doing the wrong
4022 /* This is analogous to the code for l_const above. */
4023 if (rcode != wanted_code)
4025 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
4034 /* See if we can find a mode that contains both fields being compared on
4035 the left. If we can't, fail. Otherwise, update all constants and masks
4036 to be relative to a field of that size. */
4037 first_bit = MIN (ll_bitpos, rl_bitpos);
4038 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
4039 lnmode = get_best_mode (end_bit - first_bit, first_bit,
4040 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
4042 if (lnmode == VOIDmode)
4045 lnbitsize = GET_MODE_BITSIZE (lnmode);
4046 lnbitpos = first_bit & ~ (lnbitsize - 1);
4047 lntype = type_for_size (lnbitsize, 1);
4048 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
4050 if (BYTES_BIG_ENDIAN)
4052 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
4053 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
4056 ll_mask = const_binop (LSHIFT_EXPR, convert (lntype, ll_mask),
4057 size_int (xll_bitpos), 0);
4058 rl_mask = const_binop (LSHIFT_EXPR, convert (lntype, rl_mask),
4059 size_int (xrl_bitpos), 0);
4063 l_const = convert (lntype, l_const);
4064 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
4065 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
4066 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
4067 fold (build1 (BIT_NOT_EXPR,
4071 warning ("comparison is always %d", wanted_code == NE_EXPR);
4073 return convert (truth_type,
4074 wanted_code == NE_EXPR
4075 ? integer_one_node : integer_zero_node);
4080 r_const = convert (lntype, r_const);
4081 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
4082 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
4083 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
4084 fold (build1 (BIT_NOT_EXPR,
4088 warning ("comparison is always %d", wanted_code == NE_EXPR);
4090 return convert (truth_type,
4091 wanted_code == NE_EXPR
4092 ? integer_one_node : integer_zero_node);
4096 /* If the right sides are not constant, do the same for it. Also,
4097 disallow this optimization if a size or signedness mismatch occurs
4098 between the left and right sides. */
4101 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
4102 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
4103 /* Make sure the two fields on the right
4104 correspond to the left without being swapped. */
4105 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
4108 first_bit = MIN (lr_bitpos, rr_bitpos);
4109 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
4110 rnmode = get_best_mode (end_bit - first_bit, first_bit,
4111 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
4113 if (rnmode == VOIDmode)
4116 rnbitsize = GET_MODE_BITSIZE (rnmode);
4117 rnbitpos = first_bit & ~ (rnbitsize - 1);
4118 rntype = type_for_size (rnbitsize, 1);
4119 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
4121 if (BYTES_BIG_ENDIAN)
4123 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
4124 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
4127 lr_mask = const_binop (LSHIFT_EXPR, convert (rntype, lr_mask),
4128 size_int (xlr_bitpos), 0);
4129 rr_mask = const_binop (LSHIFT_EXPR, convert (rntype, rr_mask),
4130 size_int (xrr_bitpos), 0);
4132 /* Make a mask that corresponds to both fields being compared.
4133 Do this for both items being compared. If the operands are the
4134 same size and the bits being compared are in the same position
4135 then we can do this by masking both and comparing the masked
4137 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
4138 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0);
4139 if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
4141 lhs = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
4142 ll_unsignedp || rl_unsignedp);
4143 if (! all_ones_mask_p (ll_mask, lnbitsize))
4144 lhs = build (BIT_AND_EXPR, lntype, lhs, ll_mask);
4146 rhs = make_bit_field_ref (lr_inner, rntype, rnbitsize, rnbitpos,
4147 lr_unsignedp || rr_unsignedp);
4148 if (! all_ones_mask_p (lr_mask, rnbitsize))
4149 rhs = build (BIT_AND_EXPR, rntype, rhs, lr_mask);
4151 return build (wanted_code, truth_type, lhs, rhs);
4154 /* There is still another way we can do something: If both pairs of
4155 fields being compared are adjacent, we may be able to make a wider
4156 field containing them both.
4158 Note that we still must mask the lhs/rhs expressions. Furthermore,
4159 the mask must be shifted to account for the shift done by
4160 make_bit_field_ref. */
4161 if ((ll_bitsize + ll_bitpos == rl_bitpos
4162 && lr_bitsize + lr_bitpos == rr_bitpos)
4163 || (ll_bitpos == rl_bitpos + rl_bitsize
4164 && lr_bitpos == rr_bitpos + rr_bitsize))
4168 lhs = make_bit_field_ref (ll_inner, lntype, ll_bitsize + rl_bitsize,
4169 MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
4170 rhs = make_bit_field_ref (lr_inner, rntype, lr_bitsize + rr_bitsize,
4171 MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
4173 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
4174 size_int (MIN (xll_bitpos, xrl_bitpos)), 0);
4175 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
4176 size_int (MIN (xlr_bitpos, xrr_bitpos)), 0);
4178 /* Convert to the smaller type before masking out unwanted bits. */
4180 if (lntype != rntype)
4182 if (lnbitsize > rnbitsize)
4184 lhs = convert (rntype, lhs);
4185 ll_mask = convert (rntype, ll_mask);
4188 else if (lnbitsize < rnbitsize)
4190 rhs = convert (lntype, rhs);
4191 lr_mask = convert (lntype, lr_mask);
4196 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
4197 lhs = build (BIT_AND_EXPR, type, lhs, ll_mask);
4199 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
4200 rhs = build (BIT_AND_EXPR, type, rhs, lr_mask);
4202 return build (wanted_code, truth_type, lhs, rhs);
4208 /* Handle the case of comparisons with constants. If there is something in
4209 common between the masks, those bits of the constants must be the same.
4210 If not, the condition is always false. Test for this to avoid generating
4211 incorrect code below. */
4212 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
4213 if (! integer_zerop (result)
4214 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
4215 const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
4217 if (wanted_code == NE_EXPR)
4219 warning ("`or' of unmatched not-equal tests is always 1");
4220 return convert (truth_type, integer_one_node);
4224 warning ("`and' of mutually exclusive equal-tests is always 0");
4225 return convert (truth_type, integer_zero_node);
4229 /* Construct the expression we will return. First get the component
4230 reference we will make. Unless the mask is all ones the width of
4231 that field, perform the mask operation. Then compare with the
4233 result = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
4234 ll_unsignedp || rl_unsignedp);
4236 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
4237 if (! all_ones_mask_p (ll_mask, lnbitsize))
4238 result = build (BIT_AND_EXPR, lntype, result, ll_mask);
4240 return build (wanted_code, truth_type, result,
4241 const_binop (BIT_IOR_EXPR, l_const, r_const, 0));
4244 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
4248 optimize_minmax_comparison (t)
4251 tree type = TREE_TYPE (t);
4252 tree arg0 = TREE_OPERAND (t, 0);
4253 enum tree_code op_code;
4254 tree comp_const = TREE_OPERAND (t, 1);
4256 int consts_equal, consts_lt;
4259 STRIP_SIGN_NOPS (arg0);
4261 op_code = TREE_CODE (arg0);
4262 minmax_const = TREE_OPERAND (arg0, 1);
4263 consts_equal = tree_int_cst_equal (minmax_const, comp_const);
4264 consts_lt = tree_int_cst_lt (minmax_const, comp_const);
4265 inner = TREE_OPERAND (arg0, 0);
4267 /* If something does not permit us to optimize, return the original tree. */
4268 if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
4269 || TREE_CODE (comp_const) != INTEGER_CST
4270 || TREE_CONSTANT_OVERFLOW (comp_const)
4271 || TREE_CODE (minmax_const) != INTEGER_CST
4272 || TREE_CONSTANT_OVERFLOW (minmax_const))
4275 /* Now handle all the various comparison codes. We only handle EQ_EXPR
4276 and GT_EXPR, doing the rest with recursive calls using logical
4278 switch (TREE_CODE (t))
4280 case NE_EXPR: case LT_EXPR: case LE_EXPR:
4282 invert_truthvalue (optimize_minmax_comparison (invert_truthvalue (t)));
4286 fold (build (TRUTH_ORIF_EXPR, type,
4287 optimize_minmax_comparison
4288 (build (EQ_EXPR, type, arg0, comp_const)),
4289 optimize_minmax_comparison
4290 (build (GT_EXPR, type, arg0, comp_const))));
4293 if (op_code == MAX_EXPR && consts_equal)
4294 /* MAX (X, 0) == 0 -> X <= 0 */
4295 return fold (build (LE_EXPR, type, inner, comp_const));
4297 else if (op_code == MAX_EXPR && consts_lt)
4298 /* MAX (X, 0) == 5 -> X == 5 */
4299 return fold (build (EQ_EXPR, type, inner, comp_const));
4301 else if (op_code == MAX_EXPR)
4302 /* MAX (X, 0) == -1 -> false */
4303 return omit_one_operand (type, integer_zero_node, inner);
4305 else if (consts_equal)
4306 /* MIN (X, 0) == 0 -> X >= 0 */
4307 return fold (build (GE_EXPR, type, inner, comp_const));
4310 /* MIN (X, 0) == 5 -> false */
4311 return omit_one_operand (type, integer_zero_node, inner);
4314 /* MIN (X, 0) == -1 -> X == -1 */
4315 return fold (build (EQ_EXPR, type, inner, comp_const));
4318 if (op_code == MAX_EXPR && (consts_equal || consts_lt))
4319 /* MAX (X, 0) > 0 -> X > 0
4320 MAX (X, 0) > 5 -> X > 5 */
4321 return fold (build (GT_EXPR, type, inner, comp_const));
4323 else if (op_code == MAX_EXPR)
4324 /* MAX (X, 0) > -1 -> true */
4325 return omit_one_operand (type, integer_one_node, inner);
4327 else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
4328 /* MIN (X, 0) > 0 -> false
4329 MIN (X, 0) > 5 -> false */
4330 return omit_one_operand (type, integer_zero_node, inner);
4333 /* MIN (X, 0) > -1 -> X > -1 */
4334 return fold (build (GT_EXPR, type, inner, comp_const));
4341 /* T is an integer expression that is being multiplied, divided, or taken a
4342 modulus (CODE says which and what kind of divide or modulus) by a
4343 constant C. See if we can eliminate that operation by folding it with
4344 other operations already in T. WIDE_TYPE, if non-null, is a type that
4345 should be used for the computation if wider than our type.
4347 For example, if we are dividing (X * 8) + (Y + 16) by 4, we can return
4348 (X * 2) + (Y + 4). We must, however, be assured that either the original
4349 expression would not overflow or that overflow is undefined for the type
4350 in the language in question.
4352 We also canonicalize (X + 7) * 4 into X * 4 + 28 in the hope that either
4353 the machine has a multiply-accumulate insn or that this is part of an
4354 addressing calculation.
4356 If we return a non-null expression, it is an equivalent form of the
4357 original computation, but need not be in the original type. */
4360 extract_muldiv (t, c, code, wide_type)
4363 enum tree_code code;
4366 tree type = TREE_TYPE (t);
4367 enum tree_code tcode = TREE_CODE (t);
4368 tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
4369 > GET_MODE_SIZE (TYPE_MODE (type)))
4370 ? wide_type : type);
4372 int same_p = tcode == code;
4373 tree op0 = NULL_TREE, op1 = NULL_TREE;
4375 /* Don't deal with constants of zero here; they confuse the code below. */
4376 if (integer_zerop (c))
4379 if (TREE_CODE_CLASS (tcode) == '1')
4380 op0 = TREE_OPERAND (t, 0);
4382 if (TREE_CODE_CLASS (tcode) == '2')
4383 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
4385 /* Note that we need not handle conditional operations here since fold
4386 already handles those cases. So just do arithmetic here. */
4390 /* For a constant, we can always simplify if we are a multiply
4391 or (for divide and modulus) if it is a multiple of our constant. */
4392 if (code == MULT_EXPR
4393 || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c, 0)))
4394 return const_binop (code, convert (ctype, t), convert (ctype, c), 0);
4397 case CONVERT_EXPR: case NON_LVALUE_EXPR: case NOP_EXPR:
4398 /* If op0 is an expression, and is unsigned, and the type is
4399 smaller than ctype, then we cannot widen the expression. */
4400 if ((TREE_CODE_CLASS (TREE_CODE (op0)) == '<'
4401 || TREE_CODE_CLASS (TREE_CODE (op0)) == '1'
4402 || TREE_CODE_CLASS (TREE_CODE (op0)) == '2'
4403 || TREE_CODE_CLASS (TREE_CODE (op0)) == 'e')
4404 && TREE_UNSIGNED (TREE_TYPE (op0))
4405 && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
4406 && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
4407 && (GET_MODE_SIZE (TYPE_MODE (ctype))
4408 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))))
4411 /* Pass the constant down and see if we can make a simplification. If
4412 we can, replace this expression with the inner simplification for
4413 possible later conversion to our or some other type. */
4414 if (0 != (t1 = extract_muldiv (op0, convert (TREE_TYPE (op0), c), code,
4415 code == MULT_EXPR ? ctype : NULL_TREE)))
4419 case NEGATE_EXPR: case ABS_EXPR:
4420 if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
4421 return fold (build1 (tcode, ctype, convert (ctype, t1)));
4424 case MIN_EXPR: case MAX_EXPR:
4425 /* If widening the type changes the signedness, then we can't perform
4426 this optimization as that changes the result. */
4427 if (TREE_UNSIGNED (ctype) != TREE_UNSIGNED (type))
4430 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
4431 if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0
4432 && (t2 = extract_muldiv (op1, c, code, wide_type)) != 0)
4434 if (tree_int_cst_sgn (c) < 0)
4435 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
4437 return fold (build (tcode, ctype, convert (ctype, t1),
4438 convert (ctype, t2)));
4442 case WITH_RECORD_EXPR:
4443 if ((t1 = extract_muldiv (TREE_OPERAND (t, 0), c, code, wide_type)) != 0)
4444 return build (WITH_RECORD_EXPR, TREE_TYPE (t1), t1,
4445 TREE_OPERAND (t, 1));
4449 /* If this has not been evaluated and the operand has no side effects,
4450 we can see if we can do something inside it and make a new one.
4451 Note that this test is overly conservative since we can do this
4452 if the only reason it had side effects is that it was another
4453 similar SAVE_EXPR, but that isn't worth bothering with. */
4454 if (SAVE_EXPR_RTL (t) == 0 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0))
4455 && 0 != (t1 = extract_muldiv (TREE_OPERAND (t, 0), c, code,
4458 t1 = save_expr (t1);
4459 if (SAVE_EXPR_PERSISTENT_P (t) && TREE_CODE (t1) == SAVE_EXPR)
4460 SAVE_EXPR_PERSISTENT_P (t1) = 1;
4461 if (is_pending_size (t))
4462 put_pending_size (t1);
4467 case LSHIFT_EXPR: case RSHIFT_EXPR:
4468 /* If the second operand is constant, this is a multiplication
4469 or floor division, by a power of two, so we can treat it that
4470 way unless the multiplier or divisor overflows. */
4471 if (TREE_CODE (op1) == INTEGER_CST
4472 /* const_binop may not detect overflow correctly,
4473 so check for it explicitly here. */
4474 && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
4475 && TREE_INT_CST_HIGH (op1) == 0
4476 && 0 != (t1 = convert (ctype,
4477 const_binop (LSHIFT_EXPR, size_one_node,
4479 && ! TREE_OVERFLOW (t1))
4480 return extract_muldiv (build (tcode == LSHIFT_EXPR
4481 ? MULT_EXPR : FLOOR_DIV_EXPR,
4482 ctype, convert (ctype, op0), t1),
4483 c, code, wide_type);
4486 case PLUS_EXPR: case MINUS_EXPR:
4487 /* See if we can eliminate the operation on both sides. If we can, we
4488 can return a new PLUS or MINUS. If we can't, the only remaining
4489 cases where we can do anything are if the second operand is a
4491 t1 = extract_muldiv (op0, c, code, wide_type);
4492 t2 = extract_muldiv (op1, c, code, wide_type);
4493 if (t1 != 0 && t2 != 0
4494 && (code == MULT_EXPR
4495 /* If not multiplication, we can only do this if either operand
4496 is divisible by c. */
4497 || multiple_of_p (ctype, op0, c)
4498 || multiple_of_p (ctype, op1, c)))
4499 return fold (build (tcode, ctype, convert (ctype, t1),
4500 convert (ctype, t2)));
4502 /* If this was a subtraction, negate OP1 and set it to be an addition.
4503 This simplifies the logic below. */
4504 if (tcode == MINUS_EXPR)
4505 tcode = PLUS_EXPR, op1 = negate_expr (op1);
4507 if (TREE_CODE (op1) != INTEGER_CST)
4510 /* If either OP1 or C are negative, this optimization is not safe for
4511 some of the division and remainder types while for others we need
4512 to change the code. */
4513 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
4515 if (code == CEIL_DIV_EXPR)
4516 code = FLOOR_DIV_EXPR;
4517 else if (code == CEIL_MOD_EXPR)
4518 code = FLOOR_MOD_EXPR;
4519 else if (code == FLOOR_DIV_EXPR)
4520 code = CEIL_DIV_EXPR;
4521 else if (code == FLOOR_MOD_EXPR)
4522 code = CEIL_MOD_EXPR;
4523 else if (code != MULT_EXPR)
4527 /* If it's a multiply or a division/modulus operation of a multiple
4528 of our constant, do the operation and verify it doesn't overflow. */
4529 if (code == MULT_EXPR
4530 || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
4532 op1 = const_binop (code, convert (ctype, op1), convert (ctype, c), 0);
4533 if (op1 == 0 || TREE_OVERFLOW (op1))
4539 /* If we have an unsigned type is not a sizetype, we cannot widen
4540 the operation since it will change the result if the original
4541 computation overflowed. */
4542 if (TREE_UNSIGNED (ctype)
4543 && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
4547 /* If we were able to eliminate our operation from the first side,
4548 apply our operation to the second side and reform the PLUS. */
4549 if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
4550 return fold (build (tcode, ctype, convert (ctype, t1), op1));
4552 /* The last case is if we are a multiply. In that case, we can
4553 apply the distributive law to commute the multiply and addition
4554 if the multiplication of the constants doesn't overflow. */
4555 if (code == MULT_EXPR)
4556 return fold (build (tcode, ctype, fold (build (code, ctype,
4557 convert (ctype, op0),
4558 convert (ctype, c))),
4564 /* We have a special case here if we are doing something like
4565 (C * 8) % 4 since we know that's zero. */
4566 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
4567 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
4568 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
4569 && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
4570 return omit_one_operand (type, integer_zero_node, op0);
4572 /* ... fall through ... */
4574 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
4575 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
4576 /* If we can extract our operation from the LHS, do so and return a
4577 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
4578 do something only if the second operand is a constant. */
4580 && (t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
4581 return fold (build (tcode, ctype, convert (ctype, t1),
4582 convert (ctype, op1)));
4583 else if (tcode == MULT_EXPR && code == MULT_EXPR
4584 && (t1 = extract_muldiv (op1, c, code, wide_type)) != 0)
4585 return fold (build (tcode, ctype, convert (ctype, op0),
4586 convert (ctype, t1)));
4587 else if (TREE_CODE (op1) != INTEGER_CST)
4590 /* If these are the same operation types, we can associate them
4591 assuming no overflow. */
4593 && 0 != (t1 = const_binop (MULT_EXPR, convert (ctype, op1),
4594 convert (ctype, c), 0))
4595 && ! TREE_OVERFLOW (t1))
4596 return fold (build (tcode, ctype, convert (ctype, op0), t1));
4598 /* If these operations "cancel" each other, we have the main
4599 optimizations of this pass, which occur when either constant is a
4600 multiple of the other, in which case we replace this with either an
4601 operation or CODE or TCODE.
4603 If we have an unsigned type that is not a sizetype, we canot do
4604 this since it will change the result if the original computation
4606 if ((! TREE_UNSIGNED (ctype)
4607 || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
4608 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
4609 || (tcode == MULT_EXPR
4610 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
4611 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR)))
4613 if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
4614 return fold (build (tcode, ctype, convert (ctype, op0),
4616 const_binop (TRUNC_DIV_EXPR,
4618 else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1, 0)))
4619 return fold (build (code, ctype, convert (ctype, op0),
4621 const_binop (TRUNC_DIV_EXPR,
4633 /* If T contains a COMPOUND_EXPR which was inserted merely to evaluate
4634 S, a SAVE_EXPR, return the expression actually being evaluated. Note
4635 that we may sometimes modify the tree. */
4638 strip_compound_expr (t, s)
4642 enum tree_code code = TREE_CODE (t);
4644 /* See if this is the COMPOUND_EXPR we want to eliminate. */
4645 if (code == COMPOUND_EXPR && TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR
4646 && TREE_OPERAND (TREE_OPERAND (t, 0), 0) == s)
4647 return TREE_OPERAND (t, 1);
4649 /* See if this is a COND_EXPR or a simple arithmetic operator. We
4650 don't bother handling any other types. */
4651 else if (code == COND_EXPR)
4653 TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s);
4654 TREE_OPERAND (t, 1) = strip_compound_expr (TREE_OPERAND (t, 1), s);
4655 TREE_OPERAND (t, 2) = strip_compound_expr (TREE_OPERAND (t, 2), s);
4657 else if (TREE_CODE_CLASS (code) == '1')
4658 TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s);
4659 else if (TREE_CODE_CLASS (code) == '<'
4660 || TREE_CODE_CLASS (code) == '2')
4662 TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s);
4663 TREE_OPERAND (t, 1) = strip_compound_expr (TREE_OPERAND (t, 1), s);
4669 /* Return a node which has the indicated constant VALUE (either 0 or
4670 1), and is of the indicated TYPE. */
4673 constant_boolean_node (value, type)
4677 if (type == integer_type_node)
4678 return value ? integer_one_node : integer_zero_node;
4679 else if (TREE_CODE (type) == BOOLEAN_TYPE)
4680 return truthvalue_conversion (value ? integer_one_node :
4684 tree t = build_int_2 (value, 0);
4686 TREE_TYPE (t) = type;
4691 /* Utility function for the following routine, to see how complex a nesting of
4692 COND_EXPRs can be. EXPR is the expression and LIMIT is a count beyond which
4693 we don't care (to avoid spending too much time on complex expressions.). */
4696 count_cond (expr, lim)
4702 if (TREE_CODE (expr) != COND_EXPR)
4707 ctrue = count_cond (TREE_OPERAND (expr, 1), lim - 1);
4708 cfalse = count_cond (TREE_OPERAND (expr, 2), lim - 1 - ctrue);
4709 return MIN (lim, 1 + ctrue + cfalse);
4712 /* Transform `a + (b ? x : y)' into `x ? (a + b) : (a + y)'.
4713 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
4714 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
4715 expression, and ARG to `a'. If COND_FIRST_P is non-zero, then the
4716 COND is the first argument to CODE; otherwise (as in the example
4717 given here), it is the second argument. TYPE is the type of the
4718 original expression. */
4721 fold_binary_op_with_conditional_arg (code, type, cond, arg, cond_first_p)
4722 enum tree_code code;
4728 tree test, true_value, false_value;
4729 tree lhs = NULL_TREE;
4730 tree rhs = NULL_TREE;
4731 /* In the end, we'll produce a COND_EXPR. Both arms of the
4732 conditional expression will be binary operations. The left-hand
4733 side of the expression to be executed if the condition is true
4734 will be pointed to by TRUE_LHS. Similarly, the right-hand side
4735 of the expression to be executed if the condition is true will be
4736 pointed to by TRUE_RHS. FALSE_LHS and FALSE_RHS are analagous --
4737 but apply to the expression to be executed if the conditional is
4743 /* These are the codes to use for the left-hand side and right-hand
4744 side of the COND_EXPR. Normally, they are the same as CODE. */
4745 enum tree_code lhs_code = code;
4746 enum tree_code rhs_code = code;
4747 /* And these are the types of the expressions. */
4748 tree lhs_type = type;
4749 tree rhs_type = type;
4753 true_rhs = false_rhs = &arg;
4754 true_lhs = &true_value;
4755 false_lhs = &false_value;
4759 true_lhs = false_lhs = &arg;
4760 true_rhs = &true_value;
4761 false_rhs = &false_value;
4764 if (TREE_CODE (cond) == COND_EXPR)
4766 test = TREE_OPERAND (cond, 0);
4767 true_value = TREE_OPERAND (cond, 1);
4768 false_value = TREE_OPERAND (cond, 2);
4769 /* If this operand throws an expression, then it does not make
4770 sense to try to perform a logical or arithmetic operation
4771 involving it. Instead of building `a + throw 3' for example,
4772 we simply build `a, throw 3'. */
4773 if (VOID_TYPE_P (TREE_TYPE (true_value)))
4775 lhs_code = COMPOUND_EXPR;
4777 lhs_type = void_type_node;
4779 if (VOID_TYPE_P (TREE_TYPE (false_value)))
4781 rhs_code = COMPOUND_EXPR;
4783 rhs_type = void_type_node;
4788 tree testtype = TREE_TYPE (cond);
4790 true_value = convert (testtype, integer_one_node);
4791 false_value = convert (testtype, integer_zero_node);
4794 /* If ARG is complex we want to make sure we only evaluate
4795 it once. Though this is only required if it is volatile, it
4796 might be more efficient even if it is not. However, if we
4797 succeed in folding one part to a constant, we do not need
4798 to make this SAVE_EXPR. Since we do this optimization
4799 primarily to see if we do end up with constant and this
4800 SAVE_EXPR interferes with later optimizations, suppressing
4801 it when we can is important.
4803 If we are not in a function, we can't make a SAVE_EXPR, so don't
4804 try to do so. Don't try to see if the result is a constant
4805 if an arm is a COND_EXPR since we get exponential behavior
4808 if (TREE_CODE (arg) != SAVE_EXPR && ! TREE_CONSTANT (arg)
4809 && global_bindings_p () == 0
4810 && ((TREE_CODE (arg) != VAR_DECL
4811 && TREE_CODE (arg) != PARM_DECL)
4812 || TREE_SIDE_EFFECTS (arg)))
4814 if (TREE_CODE (true_value) != COND_EXPR)
4815 lhs = fold (build (lhs_code, lhs_type, *true_lhs, *true_rhs));
4817 if (TREE_CODE (false_value) != COND_EXPR)
4818 rhs = fold (build (rhs_code, rhs_type, *false_lhs, *false_rhs));
4820 if ((lhs == 0 || ! TREE_CONSTANT (lhs))
4821 && (rhs == 0 || !TREE_CONSTANT (rhs)))
4822 arg = save_expr (arg), lhs = rhs = 0;
4826 lhs = fold (build (lhs_code, lhs_type, *true_lhs, *true_rhs));
4828 rhs = fold (build (rhs_code, rhs_type, *false_lhs, *false_rhs));
4830 test = fold (build (COND_EXPR, type, test, lhs, rhs));
4832 if (TREE_CODE (arg) == SAVE_EXPR)
4833 return build (COMPOUND_EXPR, type,
4834 convert (void_type_node, arg),
4835 strip_compound_expr (test, arg));
4837 return convert (type, test);
4841 /* Perform constant folding and related simplification of EXPR.
4842 The related simplifications include x*1 => x, x*0 => 0, etc.,
4843 and application of the associative law.
4844 NOP_EXPR conversions may be removed freely (as long as we
4845 are careful not to change the C type of the overall expression)
4846 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
4847 but we can constant-fold them if they have constant operands. */
4853 register tree t = expr;
4854 tree t1 = NULL_TREE;
4856 tree type = TREE_TYPE (expr);
4857 register tree arg0 = NULL_TREE, arg1 = NULL_TREE;
4858 register enum tree_code code = TREE_CODE (t);
4859 register int kind = TREE_CODE_CLASS (code);
4861 /* WINS will be nonzero when the switch is done
4862 if all operands are constant. */
4865 /* Don't try to process an RTL_EXPR since its operands aren't trees.
4866 Likewise for a SAVE_EXPR that's already been evaluated. */
4867 if (code == RTL_EXPR || (code == SAVE_EXPR && SAVE_EXPR_RTL (t)) != 0)
4870 /* Return right away if a constant. */
4874 #ifdef MAX_INTEGER_COMPUTATION_MODE
4875 check_max_integer_computation_mode (expr);
4878 if (code == NOP_EXPR || code == FLOAT_EXPR || code == CONVERT_EXPR)
4882 /* Special case for conversion ops that can have fixed point args. */
4883 arg0 = TREE_OPERAND (t, 0);
4885 /* Don't use STRIP_NOPS, because signedness of argument type matters. */
4887 STRIP_SIGN_NOPS (arg0);
4889 if (arg0 != 0 && TREE_CODE (arg0) == COMPLEX_CST)
4890 subop = TREE_REALPART (arg0);
4894 if (subop != 0 && TREE_CODE (subop) != INTEGER_CST
4895 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
4896 && TREE_CODE (subop) != REAL_CST
4897 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
4899 /* Note that TREE_CONSTANT isn't enough:
4900 static var addresses are constant but we can't
4901 do arithmetic on them. */
4904 else if (IS_EXPR_CODE_CLASS (kind) || kind == 'r')
4906 register int len = TREE_CODE_LENGTH (code);
4908 for (i = 0; i < len; i++)
4910 tree op = TREE_OPERAND (t, i);
4914 continue; /* Valid for CALL_EXPR, at least. */
4916 if (kind == '<' || code == RSHIFT_EXPR)
4918 /* Signedness matters here. Perhaps we can refine this
4920 STRIP_SIGN_NOPS (op);
4923 /* Strip any conversions that don't change the mode. */
4926 if (TREE_CODE (op) == COMPLEX_CST)
4927 subop = TREE_REALPART (op);
4931 if (TREE_CODE (subop) != INTEGER_CST
4932 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
4933 && TREE_CODE (subop) != REAL_CST
4934 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
4936 /* Note that TREE_CONSTANT isn't enough:
4937 static var addresses are constant but we can't
4938 do arithmetic on them. */
4948 /* If this is a commutative operation, and ARG0 is a constant, move it
4949 to ARG1 to reduce the number of tests below. */
4950 if ((code == PLUS_EXPR || code == MULT_EXPR || code == MIN_EXPR
4951 || code == MAX_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR
4952 || code == BIT_AND_EXPR)
4953 && (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST))
4955 tem = arg0; arg0 = arg1; arg1 = tem;
4957 tem = TREE_OPERAND (t, 0); TREE_OPERAND (t, 0) = TREE_OPERAND (t, 1);
4958 TREE_OPERAND (t, 1) = tem;
4961 /* Now WINS is set as described above,
4962 ARG0 is the first operand of EXPR,
4963 and ARG1 is the second operand (if it has more than one operand).
4965 First check for cases where an arithmetic operation is applied to a
4966 compound, conditional, or comparison operation. Push the arithmetic
4967 operation inside the compound or conditional to see if any folding
4968 can then be done. Convert comparison to conditional for this purpose.
4969 The also optimizes non-constant cases that used to be done in
4972 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
4973 one of the operands is a comparison and the other is a comparison, a
4974 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
4975 code below would make the expression more complex. Change it to a
4976 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
4977 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
4979 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
4980 || code == EQ_EXPR || code == NE_EXPR)
4981 && ((truth_value_p (TREE_CODE (arg0))
4982 && (truth_value_p (TREE_CODE (arg1))
4983 || (TREE_CODE (arg1) == BIT_AND_EXPR
4984 && integer_onep (TREE_OPERAND (arg1, 1)))))
4985 || (truth_value_p (TREE_CODE (arg1))
4986 && (truth_value_p (TREE_CODE (arg0))
4987 || (TREE_CODE (arg0) == BIT_AND_EXPR
4988 && integer_onep (TREE_OPERAND (arg0, 1)))))))
4990 t = fold (build (code == BIT_AND_EXPR ? TRUTH_AND_EXPR
4991 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
4995 if (code == EQ_EXPR)
4996 t = invert_truthvalue (t);
5001 if (TREE_CODE_CLASS (code) == '1')
5003 if (TREE_CODE (arg0) == COMPOUND_EXPR)
5004 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
5005 fold (build1 (code, type, TREE_OPERAND (arg0, 1))));
5006 else if (TREE_CODE (arg0) == COND_EXPR)
5008 t = fold (build (COND_EXPR, type, TREE_OPERAND (arg0, 0),
5009 fold (build1 (code, type, TREE_OPERAND (arg0, 1))),
5010 fold (build1 (code, type, TREE_OPERAND (arg0, 2)))));
5012 /* If this was a conversion, and all we did was to move into
5013 inside the COND_EXPR, bring it back out. But leave it if
5014 it is a conversion from integer to integer and the
5015 result precision is no wider than a word since such a
5016 conversion is cheap and may be optimized away by combine,
5017 while it couldn't if it were outside the COND_EXPR. Then return
5018 so we don't get into an infinite recursion loop taking the
5019 conversion out and then back in. */
5021 if ((code == NOP_EXPR || code == CONVERT_EXPR
5022 || code == NON_LVALUE_EXPR)
5023 && TREE_CODE (t) == COND_EXPR
5024 && TREE_CODE (TREE_OPERAND (t, 1)) == code
5025 && TREE_CODE (TREE_OPERAND (t, 2)) == code
5026 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0))
5027 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 2), 0)))
5028 && ! (INTEGRAL_TYPE_P (TREE_TYPE (t))
5030 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0))))
5031 && TYPE_PRECISION (TREE_TYPE (t)) <= BITS_PER_WORD))
5032 t = build1 (code, type,
5034 TREE_TYPE (TREE_OPERAND
5035 (TREE_OPERAND (t, 1), 0)),
5036 TREE_OPERAND (t, 0),
5037 TREE_OPERAND (TREE_OPERAND (t, 1), 0),
5038 TREE_OPERAND (TREE_OPERAND (t, 2), 0)));
5041 else if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<')
5042 return fold (build (COND_EXPR, type, arg0,
5043 fold (build1 (code, type, integer_one_node)),
5044 fold (build1 (code, type, integer_zero_node))));
5046 else if (TREE_CODE_CLASS (code) == '2'
5047 || TREE_CODE_CLASS (code) == '<')
5049 if (TREE_CODE (arg1) == COMPOUND_EXPR)
5050 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
5051 fold (build (code, type,
5052 arg0, TREE_OPERAND (arg1, 1))));
5053 else if ((TREE_CODE (arg1) == COND_EXPR
5054 || (TREE_CODE_CLASS (TREE_CODE (arg1)) == '<'
5055 && TREE_CODE_CLASS (code) != '<'))
5056 && (TREE_CODE (arg0) != COND_EXPR
5057 || count_cond (arg0, 25) + count_cond (arg1, 25) <= 25)
5058 && (! TREE_SIDE_EFFECTS (arg0)
5059 || (global_bindings_p () == 0
5060 && ! contains_placeholder_p (arg0))))
5062 fold_binary_op_with_conditional_arg (code, type, arg1, arg0,
5063 /*cond_first_p=*/0);
5064 else if (TREE_CODE (arg0) == COMPOUND_EXPR)
5065 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
5066 fold (build (code, type, TREE_OPERAND (arg0, 1), arg1)));
5067 else if ((TREE_CODE (arg0) == COND_EXPR
5068 || (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
5069 && TREE_CODE_CLASS (code) != '<'))
5070 && (TREE_CODE (arg1) != COND_EXPR
5071 || count_cond (arg0, 25) + count_cond (arg1, 25) <= 25)
5072 && (! TREE_SIDE_EFFECTS (arg1)
5073 || (global_bindings_p () == 0
5074 && ! contains_placeholder_p (arg1))))
5076 fold_binary_op_with_conditional_arg (code, type, arg0, arg1,
5077 /*cond_first_p=*/1);
5079 else if (TREE_CODE_CLASS (code) == '<'
5080 && TREE_CODE (arg0) == COMPOUND_EXPR)
5081 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
5082 fold (build (code, type, TREE_OPERAND (arg0, 1), arg1)));
5083 else if (TREE_CODE_CLASS (code) == '<'
5084 && TREE_CODE (arg1) == COMPOUND_EXPR)
5085 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
5086 fold (build (code, type, arg0, TREE_OPERAND (arg1, 1))));
5098 return fold (DECL_INITIAL (t));
5103 case FIX_TRUNC_EXPR:
5104 /* Other kinds of FIX are not handled properly by fold_convert. */
5106 if (TREE_TYPE (TREE_OPERAND (t, 0)) == TREE_TYPE (t))
5107 return TREE_OPERAND (t, 0);
5109 /* Handle cases of two conversions in a row. */
5110 if (TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
5111 || TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR)
5113 tree inside_type = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5114 tree inter_type = TREE_TYPE (TREE_OPERAND (t, 0));
5115 tree final_type = TREE_TYPE (t);
5116 int inside_int = INTEGRAL_TYPE_P (inside_type);
5117 int inside_ptr = POINTER_TYPE_P (inside_type);
5118 int inside_float = FLOAT_TYPE_P (inside_type);
5119 unsigned int inside_prec = TYPE_PRECISION (inside_type);
5120 int inside_unsignedp = TREE_UNSIGNED (inside_type);
5121 int inter_int = INTEGRAL_TYPE_P (inter_type);
5122 int inter_ptr = POINTER_TYPE_P (inter_type);
5123 int inter_float = FLOAT_TYPE_P (inter_type);
5124 unsigned int inter_prec = TYPE_PRECISION (inter_type);
5125 int inter_unsignedp = TREE_UNSIGNED (inter_type);
5126 int final_int = INTEGRAL_TYPE_P (final_type);
5127 int final_ptr = POINTER_TYPE_P (final_type);
5128 int final_float = FLOAT_TYPE_P (final_type);
5129 unsigned int final_prec = TYPE_PRECISION (final_type);
5130 int final_unsignedp = TREE_UNSIGNED (final_type);
5132 /* In addition to the cases of two conversions in a row
5133 handled below, if we are converting something to its own
5134 type via an object of identical or wider precision, neither
5135 conversion is needed. */
5136 if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (final_type)
5137 && ((inter_int && final_int) || (inter_float && final_float))
5138 && inter_prec >= final_prec)
5139 return convert (final_type, TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5141 /* Likewise, if the intermediate and final types are either both
5142 float or both integer, we don't need the middle conversion if
5143 it is wider than the final type and doesn't change the signedness
5144 (for integers). Avoid this if the final type is a pointer
5145 since then we sometimes need the inner conversion. Likewise if
5146 the outer has a precision not equal to the size of its mode. */
5147 if ((((inter_int || inter_ptr) && (inside_int || inside_ptr))
5148 || (inter_float && inside_float))
5149 && inter_prec >= inside_prec
5150 && (inter_float || inter_unsignedp == inside_unsignedp)
5151 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (final_type))
5152 && TYPE_MODE (final_type) == TYPE_MODE (inter_type))
5154 return convert (final_type, TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5156 /* If we have a sign-extension of a zero-extended value, we can
5157 replace that by a single zero-extension. */
5158 if (inside_int && inter_int && final_int
5159 && inside_prec < inter_prec && inter_prec < final_prec
5160 && inside_unsignedp && !inter_unsignedp)
5161 return convert (final_type, TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5163 /* Two conversions in a row are not needed unless:
5164 - some conversion is floating-point (overstrict for now), or
5165 - the intermediate type is narrower than both initial and
5167 - the intermediate type and innermost type differ in signedness,
5168 and the outermost type is wider than the intermediate, or
5169 - the initial type is a pointer type and the precisions of the
5170 intermediate and final types differ, or
5171 - the final type is a pointer type and the precisions of the
5172 initial and intermediate types differ. */
5173 if (! inside_float && ! inter_float && ! final_float
5174 && (inter_prec > inside_prec || inter_prec > final_prec)
5175 && ! (inside_int && inter_int
5176 && inter_unsignedp != inside_unsignedp
5177 && inter_prec < final_prec)
5178 && ((inter_unsignedp && inter_prec > inside_prec)
5179 == (final_unsignedp && final_prec > inter_prec))
5180 && ! (inside_ptr && inter_prec != final_prec)
5181 && ! (final_ptr && inside_prec != inter_prec)
5182 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (final_type))
5183 && TYPE_MODE (final_type) == TYPE_MODE (inter_type))
5185 return convert (final_type, TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5188 if (TREE_CODE (TREE_OPERAND (t, 0)) == MODIFY_EXPR
5189 && TREE_CONSTANT (TREE_OPERAND (TREE_OPERAND (t, 0), 1))
5190 /* Detect assigning a bitfield. */
5191 && !(TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) == COMPONENT_REF
5192 && DECL_BIT_FIELD (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 1))))
5194 /* Don't leave an assignment inside a conversion
5195 unless assigning a bitfield. */
5196 tree prev = TREE_OPERAND (t, 0);
5197 TREE_OPERAND (t, 0) = TREE_OPERAND (prev, 1);
5198 /* First do the assignment, then return converted constant. */
5199 t = build (COMPOUND_EXPR, TREE_TYPE (t), prev, fold (t));
5205 TREE_CONSTANT (t) = TREE_CONSTANT (arg0);
5208 return fold_convert (t, arg0);
5210 #if 0 /* This loses on &"foo"[0]. */
5215 /* Fold an expression like: "foo"[2] */
5216 if (TREE_CODE (arg0) == STRING_CST
5217 && TREE_CODE (arg1) == INTEGER_CST
5218 && compare_tree_int (arg1, TREE_STRING_LENGTH (arg0)) < 0)
5220 t = build_int_2 (TREE_STRING_POINTER (arg0)[TREE_INT_CST_LOW (arg))], 0);
5221 TREE_TYPE (t) = TREE_TYPE (TREE_TYPE (arg0));
5222 force_fit_type (t, 0);
5229 if (TREE_CODE (arg0) == CONSTRUCTOR)
5231 tree m = purpose_member (arg1, CONSTRUCTOR_ELTS (arg0));
5238 TREE_CONSTANT (t) = wins;
5244 if (TREE_CODE (arg0) == INTEGER_CST)
5246 unsigned HOST_WIDE_INT low;
5248 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
5249 TREE_INT_CST_HIGH (arg0),
5251 t = build_int_2 (low, high);
5252 TREE_TYPE (t) = type;
5254 = (TREE_OVERFLOW (arg0)
5255 | force_fit_type (t, overflow && !TREE_UNSIGNED (type)));
5256 TREE_CONSTANT_OVERFLOW (t)
5257 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg0);
5259 else if (TREE_CODE (arg0) == REAL_CST)
5260 t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
5262 else if (TREE_CODE (arg0) == NEGATE_EXPR)
5263 return TREE_OPERAND (arg0, 0);
5265 /* Convert - (a - b) to (b - a) for non-floating-point. */
5266 else if (TREE_CODE (arg0) == MINUS_EXPR
5267 && (! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations))
5268 return build (MINUS_EXPR, type, TREE_OPERAND (arg0, 1),
5269 TREE_OPERAND (arg0, 0));
5276 if (TREE_CODE (arg0) == INTEGER_CST)
5278 if (! TREE_UNSIGNED (type)
5279 && TREE_INT_CST_HIGH (arg0) < 0)
5281 unsigned HOST_WIDE_INT low;
5283 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
5284 TREE_INT_CST_HIGH (arg0),
5286 t = build_int_2 (low, high);
5287 TREE_TYPE (t) = type;
5289 = (TREE_OVERFLOW (arg0)
5290 | force_fit_type (t, overflow));
5291 TREE_CONSTANT_OVERFLOW (t)
5292 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg0);
5295 else if (TREE_CODE (arg0) == REAL_CST)
5297 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
5298 t = build_real (type,
5299 REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
5302 else if (TREE_CODE (arg0) == ABS_EXPR || TREE_CODE (arg0) == NEGATE_EXPR)
5303 return build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0));
5307 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
5308 return convert (type, arg0);
5309 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
5310 return build (COMPLEX_EXPR, type,
5311 TREE_OPERAND (arg0, 0),
5312 negate_expr (TREE_OPERAND (arg0, 1)));
5313 else if (TREE_CODE (arg0) == COMPLEX_CST)
5314 return build_complex (type, TREE_REALPART (arg0),
5315 negate_expr (TREE_IMAGPART (arg0)));
5316 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
5317 return fold (build (TREE_CODE (arg0), type,
5318 fold (build1 (CONJ_EXPR, type,
5319 TREE_OPERAND (arg0, 0))),
5320 fold (build1 (CONJ_EXPR,
5321 type, TREE_OPERAND (arg0, 1)))));
5322 else if (TREE_CODE (arg0) == CONJ_EXPR)
5323 return TREE_OPERAND (arg0, 0);
5329 t = build_int_2 (~ TREE_INT_CST_LOW (arg0),
5330 ~ TREE_INT_CST_HIGH (arg0));
5331 TREE_TYPE (t) = type;
5332 force_fit_type (t, 0);
5333 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg0);
5334 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (arg0);
5336 else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
5337 return TREE_OPERAND (arg0, 0);
5341 /* A + (-B) -> A - B */
5342 if (TREE_CODE (arg1) == NEGATE_EXPR)
5343 return fold (build (MINUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
5344 /* (-A) + B -> B - A */
5345 if (TREE_CODE (arg0) == NEGATE_EXPR)
5346 return fold (build (MINUS_EXPR, type, arg1, TREE_OPERAND (arg0, 0)));
5347 else if (! FLOAT_TYPE_P (type))
5349 if (integer_zerop (arg1))
5350 return non_lvalue (convert (type, arg0));
5352 /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
5353 with a constant, and the two constants have no bits in common,
5354 we should treat this as a BIT_IOR_EXPR since this may produce more
5356 if (TREE_CODE (arg0) == BIT_AND_EXPR
5357 && TREE_CODE (arg1) == BIT_AND_EXPR
5358 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
5359 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
5360 && integer_zerop (const_binop (BIT_AND_EXPR,
5361 TREE_OPERAND (arg0, 1),
5362 TREE_OPERAND (arg1, 1), 0)))
5364 code = BIT_IOR_EXPR;
5368 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
5369 (plus (plus (mult) (mult)) (foo)) so that we can
5370 take advantage of the factoring cases below. */
5371 if ((TREE_CODE (arg0) == PLUS_EXPR
5372 && TREE_CODE (arg1) == MULT_EXPR)
5373 || (TREE_CODE (arg1) == PLUS_EXPR
5374 && TREE_CODE (arg0) == MULT_EXPR))
5376 tree parg0, parg1, parg, marg;
5378 if (TREE_CODE (arg0) == PLUS_EXPR)
5379 parg = arg0, marg = arg1;
5381 parg = arg1, marg = arg0;
5382 parg0 = TREE_OPERAND (parg, 0);
5383 parg1 = TREE_OPERAND (parg, 1);
5387 if (TREE_CODE (parg0) == MULT_EXPR
5388 && TREE_CODE (parg1) != MULT_EXPR)
5389 return fold (build (PLUS_EXPR, type,
5390 fold (build (PLUS_EXPR, type, parg0, marg)),
5392 if (TREE_CODE (parg0) != MULT_EXPR
5393 && TREE_CODE (parg1) == MULT_EXPR)
5394 return fold (build (PLUS_EXPR, type,
5395 fold (build (PLUS_EXPR, type, parg1, marg)),
5399 if (TREE_CODE (arg0) == MULT_EXPR && TREE_CODE (arg1) == MULT_EXPR)
5401 tree arg00, arg01, arg10, arg11;
5402 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
5404 /* (A * C) + (B * C) -> (A+B) * C.
5405 We are most concerned about the case where C is a constant,
5406 but other combinations show up during loop reduction. Since
5407 it is not difficult, try all four possibilities. */
5409 arg00 = TREE_OPERAND (arg0, 0);
5410 arg01 = TREE_OPERAND (arg0, 1);
5411 arg10 = TREE_OPERAND (arg1, 0);
5412 arg11 = TREE_OPERAND (arg1, 1);
5415 if (operand_equal_p (arg01, arg11, 0))
5416 same = arg01, alt0 = arg00, alt1 = arg10;
5417 else if (operand_equal_p (arg00, arg10, 0))
5418 same = arg00, alt0 = arg01, alt1 = arg11;
5419 else if (operand_equal_p (arg00, arg11, 0))
5420 same = arg00, alt0 = arg01, alt1 = arg10;
5421 else if (operand_equal_p (arg01, arg10, 0))
5422 same = arg01, alt0 = arg00, alt1 = arg11;
5424 /* No identical multiplicands; see if we can find a common
5425 power-of-two factor in non-power-of-two multiplies. This
5426 can help in multi-dimensional array access. */
5427 else if (TREE_CODE (arg01) == INTEGER_CST
5428 && TREE_CODE (arg11) == INTEGER_CST
5429 && TREE_INT_CST_HIGH (arg01) == 0
5430 && TREE_INT_CST_HIGH (arg11) == 0)
5432 HOST_WIDE_INT int01, int11, tmp;
5433 int01 = TREE_INT_CST_LOW (arg01);
5434 int11 = TREE_INT_CST_LOW (arg11);
5436 /* Move min of absolute values to int11. */
5437 if ((int01 >= 0 ? int01 : -int01)
5438 < (int11 >= 0 ? int11 : -int11))
5440 tmp = int01, int01 = int11, int11 = tmp;
5441 alt0 = arg00, arg00 = arg10, arg10 = alt0;
5442 alt0 = arg01, arg01 = arg11, arg11 = alt0;
5445 if (exact_log2 (int11) > 0 && int01 % int11 == 0)
5447 alt0 = fold (build (MULT_EXPR, type, arg00,
5448 build_int_2 (int01 / int11, 0)));
5455 return fold (build (MULT_EXPR, type,
5456 fold (build (PLUS_EXPR, type, alt0, alt1)),
5460 /* In IEEE floating point, x+0 may not equal x. */
5461 else if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
5462 || flag_unsafe_math_optimizations)
5463 && real_zerop (arg1))
5464 return non_lvalue (convert (type, arg0));
5465 /* x+(-0) equals x, even for IEEE. */
5466 else if (TREE_CODE (arg1) == REAL_CST
5467 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (arg1)))
5468 return non_lvalue (convert (type, arg0));
5471 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
5472 is a rotate of A by C1 bits. */
5473 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
5474 is a rotate of A by B bits. */
5476 register enum tree_code code0, code1;
5477 code0 = TREE_CODE (arg0);
5478 code1 = TREE_CODE (arg1);
5479 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
5480 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
5481 && operand_equal_p (TREE_OPERAND (arg0, 0),
5482 TREE_OPERAND (arg1, 0), 0)
5483 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
5485 register tree tree01, tree11;
5486 register enum tree_code code01, code11;
5488 tree01 = TREE_OPERAND (arg0, 1);
5489 tree11 = TREE_OPERAND (arg1, 1);
5490 STRIP_NOPS (tree01);
5491 STRIP_NOPS (tree11);
5492 code01 = TREE_CODE (tree01);
5493 code11 = TREE_CODE (tree11);
5494 if (code01 == INTEGER_CST
5495 && code11 == INTEGER_CST
5496 && TREE_INT_CST_HIGH (tree01) == 0
5497 && TREE_INT_CST_HIGH (tree11) == 0
5498 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
5499 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
5500 return build (LROTATE_EXPR, type, TREE_OPERAND (arg0, 0),
5501 code0 == LSHIFT_EXPR ? tree01 : tree11);
5502 else if (code11 == MINUS_EXPR)
5504 tree tree110, tree111;
5505 tree110 = TREE_OPERAND (tree11, 0);
5506 tree111 = TREE_OPERAND (tree11, 1);
5507 STRIP_NOPS (tree110);
5508 STRIP_NOPS (tree111);
5509 if (TREE_CODE (tree110) == INTEGER_CST
5510 && 0 == compare_tree_int (tree110,
5512 (TREE_TYPE (TREE_OPERAND
5514 && operand_equal_p (tree01, tree111, 0))
5515 return build ((code0 == LSHIFT_EXPR
5518 type, TREE_OPERAND (arg0, 0), tree01);
5520 else if (code01 == MINUS_EXPR)
5522 tree tree010, tree011;
5523 tree010 = TREE_OPERAND (tree01, 0);
5524 tree011 = TREE_OPERAND (tree01, 1);
5525 STRIP_NOPS (tree010);
5526 STRIP_NOPS (tree011);
5527 if (TREE_CODE (tree010) == INTEGER_CST
5528 && 0 == compare_tree_int (tree010,
5530 (TREE_TYPE (TREE_OPERAND
5532 && operand_equal_p (tree11, tree011, 0))
5533 return build ((code0 != LSHIFT_EXPR
5536 type, TREE_OPERAND (arg0, 0), tree11);
5542 /* In most languages, can't associate operations on floats through
5543 parentheses. Rather than remember where the parentheses were, we
5544 don't associate floats at all. It shouldn't matter much. However,
5545 associating multiplications is only very slightly inaccurate, so do
5546 that if -funsafe-math-optimizations is specified. */
5549 && (! FLOAT_TYPE_P (type)
5550 || (flag_unsafe_math_optimizations && code == MULT_EXPR)))
5552 tree var0, con0, lit0, var1, con1, lit1;
5554 /* Split both trees into variables, constants, and literals. Then
5555 associate each group together, the constants with literals,
5556 then the result with variables. This increases the chances of
5557 literals being recombined later and of generating relocatable
5558 expressions for the sum of a constant and literal. */
5559 var0 = split_tree (arg0, code, &con0, &lit0, 0);
5560 var1 = split_tree (arg1, code, &con1, &lit1, code == MINUS_EXPR);
5562 /* Only do something if we found more than two objects. Otherwise,
5563 nothing has changed and we risk infinite recursion. */
5564 if (2 < ((var0 != 0) + (var1 != 0) + (con0 != 0) + (con1 != 0)
5565 + (lit0 != 0) + (lit1 != 0)))
5567 var0 = associate_trees (var0, var1, code, type);
5568 con0 = associate_trees (con0, con1, code, type);
5569 lit0 = associate_trees (lit0, lit1, code, type);
5570 con0 = associate_trees (con0, lit0, code, type);
5571 return convert (type, associate_trees (var0, con0, code, type));
5576 #if defined (REAL_IS_NOT_DOUBLE) && ! defined (REAL_ARITHMETIC)
5577 if (TREE_CODE (arg1) == REAL_CST)
5579 #endif /* REAL_IS_NOT_DOUBLE, and no REAL_ARITHMETIC */
5581 t1 = const_binop (code, arg0, arg1, 0);
5582 if (t1 != NULL_TREE)
5584 /* The return value should always have
5585 the same type as the original expression. */
5586 if (TREE_TYPE (t1) != TREE_TYPE (t))
5587 t1 = convert (TREE_TYPE (t), t1);
5594 /* A - (-B) -> A + B */
5595 if (TREE_CODE (arg1) == NEGATE_EXPR)
5596 return fold (build (PLUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
5597 /* (-A) - CST -> (-CST) - A for floating point (what about ints ?) */
5598 if (TREE_CODE (arg0) == NEGATE_EXPR && TREE_CODE (arg1) == REAL_CST)
5600 fold (build (MINUS_EXPR, type,
5601 build_real (TREE_TYPE (arg1),
5602 REAL_VALUE_NEGATE (TREE_REAL_CST (arg1))),
5603 TREE_OPERAND (arg0, 0)));
5605 if (! FLOAT_TYPE_P (type))
5607 if (! wins && integer_zerop (arg0))
5608 return negate_expr (convert (type, arg1));
5609 if (integer_zerop (arg1))
5610 return non_lvalue (convert (type, arg0));
5612 /* (A * C) - (B * C) -> (A-B) * C. Since we are most concerned
5613 about the case where C is a constant, just try one of the
5614 four possibilities. */
5616 if (TREE_CODE (arg0) == MULT_EXPR && TREE_CODE (arg1) == MULT_EXPR
5617 && operand_equal_p (TREE_OPERAND (arg0, 1),
5618 TREE_OPERAND (arg1, 1), 0))
5619 return fold (build (MULT_EXPR, type,
5620 fold (build (MINUS_EXPR, type,
5621 TREE_OPERAND (arg0, 0),
5622 TREE_OPERAND (arg1, 0))),
5623 TREE_OPERAND (arg0, 1)));
5626 else if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
5627 || flag_unsafe_math_optimizations)
5629 /* Except with IEEE floating point, 0-x equals -x. */
5630 if (! wins && real_zerop (arg0))
5631 return negate_expr (convert (type, arg1));
5632 /* Except with IEEE floating point, x-0 equals x. */
5633 if (real_zerop (arg1))
5634 return non_lvalue (convert (type, arg0));
5637 /* Fold &x - &x. This can happen from &x.foo - &x.
5638 This is unsafe for certain floats even in non-IEEE formats.
5639 In IEEE, it is unsafe because it does wrong for NaNs.
5640 Also note that operand_equal_p is always false if an operand
5643 if ((! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
5644 && operand_equal_p (arg0, arg1, 0))
5645 return convert (type, integer_zero_node);
5650 /* (-A) * (-B) -> A * B */
5651 if (TREE_CODE (arg0) == NEGATE_EXPR && TREE_CODE (arg1) == NEGATE_EXPR)
5652 return fold (build (MULT_EXPR, type, TREE_OPERAND (arg0, 0),
5653 TREE_OPERAND (arg1, 0)));
5655 if (! FLOAT_TYPE_P (type))
5657 if (integer_zerop (arg1))
5658 return omit_one_operand (type, arg1, arg0);
5659 if (integer_onep (arg1))
5660 return non_lvalue (convert (type, arg0));
5662 /* (a * (1 << b)) is (a << b) */
5663 if (TREE_CODE (arg1) == LSHIFT_EXPR
5664 && integer_onep (TREE_OPERAND (arg1, 0)))
5665 return fold (build (LSHIFT_EXPR, type, arg0,
5666 TREE_OPERAND (arg1, 1)));
5667 if (TREE_CODE (arg0) == LSHIFT_EXPR
5668 && integer_onep (TREE_OPERAND (arg0, 0)))
5669 return fold (build (LSHIFT_EXPR, type, arg1,
5670 TREE_OPERAND (arg0, 1)));
5672 if (TREE_CODE (arg1) == INTEGER_CST
5673 && 0 != (tem = extract_muldiv (TREE_OPERAND (t, 0), arg1,
5675 return convert (type, tem);
5680 /* x*0 is 0, except for IEEE floating point. */
5681 if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
5682 || flag_unsafe_math_optimizations)
5683 && real_zerop (arg1))
5684 return omit_one_operand (type, arg1, arg0);
5685 /* In IEEE floating point, x*1 is not equivalent to x for snans.
5686 However, ANSI says we can drop signals,
5687 so we can do this anyway. */
5688 if (real_onep (arg1))
5689 return non_lvalue (convert (type, arg0));
5691 if (! wins && real_twop (arg1) && global_bindings_p () == 0
5692 && ! contains_placeholder_p (arg0))
5694 tree arg = save_expr (arg0);
5695 return build (PLUS_EXPR, type, arg, arg);
5702 if (integer_all_onesp (arg1))
5703 return omit_one_operand (type, arg1, arg0);
5704 if (integer_zerop (arg1))
5705 return non_lvalue (convert (type, arg0));
5706 t1 = distribute_bit_expr (code, type, arg0, arg1);
5707 if (t1 != NULL_TREE)
5710 /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
5712 This results in more efficient code for machines without a NAND
5713 instruction. Combine will canonicalize to the first form
5714 which will allow use of NAND instructions provided by the
5715 backend if they exist. */
5716 if (TREE_CODE (arg0) == BIT_NOT_EXPR
5717 && TREE_CODE (arg1) == BIT_NOT_EXPR)
5719 return fold (build1 (BIT_NOT_EXPR, type,
5720 build (BIT_AND_EXPR, type,
5721 TREE_OPERAND (arg0, 0),
5722 TREE_OPERAND (arg1, 0))));
5725 /* See if this can be simplified into a rotate first. If that
5726 is unsuccessful continue in the association code. */
5730 if (integer_zerop (arg1))
5731 return non_lvalue (convert (type, arg0));
5732 if (integer_all_onesp (arg1))
5733 return fold (build1 (BIT_NOT_EXPR, type, arg0));
5735 /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
5736 with a constant, and the two constants have no bits in common,
5737 we should treat this as a BIT_IOR_EXPR since this may produce more
5739 if (TREE_CODE (arg0) == BIT_AND_EXPR
5740 && TREE_CODE (arg1) == BIT_AND_EXPR
5741 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
5742 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
5743 && integer_zerop (const_binop (BIT_AND_EXPR,
5744 TREE_OPERAND (arg0, 1),
5745 TREE_OPERAND (arg1, 1), 0)))
5747 code = BIT_IOR_EXPR;
5751 /* See if this can be simplified into a rotate first. If that
5752 is unsuccessful continue in the association code. */
5757 if (integer_all_onesp (arg1))
5758 return non_lvalue (convert (type, arg0));
5759 if (integer_zerop (arg1))
5760 return omit_one_operand (type, arg1, arg0);
5761 t1 = distribute_bit_expr (code, type, arg0, arg1);
5762 if (t1 != NULL_TREE)
5764 /* Simplify ((int)c & 0x377) into (int)c, if c is unsigned char. */
5765 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == NOP_EXPR
5766 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0))))
5769 = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)));
5771 if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
5772 && (~TREE_INT_CST_LOW (arg0)
5773 & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
5774 return build1 (NOP_EXPR, type, TREE_OPERAND (arg1, 0));
5776 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
5777 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
5780 = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
5782 if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
5783 && (~TREE_INT_CST_LOW (arg1)
5784 & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
5785 return build1 (NOP_EXPR, type, TREE_OPERAND (arg0, 0));
5788 /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
5790 This results in more efficient code for machines without a NOR
5791 instruction. Combine will canonicalize to the first form
5792 which will allow use of NOR instructions provided by the
5793 backend if they exist. */
5794 if (TREE_CODE (arg0) == BIT_NOT_EXPR
5795 && TREE_CODE (arg1) == BIT_NOT_EXPR)
5797 return fold (build1 (BIT_NOT_EXPR, type,
5798 build (BIT_IOR_EXPR, type,
5799 TREE_OPERAND (arg0, 0),
5800 TREE_OPERAND (arg1, 0))));
5805 case BIT_ANDTC_EXPR:
5806 if (integer_all_onesp (arg0))
5807 return non_lvalue (convert (type, arg1));
5808 if (integer_zerop (arg0))
5809 return omit_one_operand (type, arg0, arg1);
5810 if (TREE_CODE (arg1) == INTEGER_CST)
5812 arg1 = fold (build1 (BIT_NOT_EXPR, type, arg1));
5813 code = BIT_AND_EXPR;
5819 /* In most cases, do nothing with a divide by zero. */
5820 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
5821 #ifndef REAL_INFINITY
5822 if (TREE_CODE (arg1) == REAL_CST && real_zerop (arg1))
5825 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
5827 /* (-A) / (-B) -> A / B */
5828 if (TREE_CODE (arg0) == NEGATE_EXPR && TREE_CODE (arg1) == NEGATE_EXPR)
5829 return fold (build (RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
5830 TREE_OPERAND (arg1, 0)));
5832 /* In IEEE floating point, x/1 is not equivalent to x for snans.
5833 However, ANSI says we can drop signals, so we can do this anyway. */
5834 if (real_onep (arg1))
5835 return non_lvalue (convert (type, arg0));
5837 /* If ARG1 is a constant, we can convert this to a multiply by the
5838 reciprocal. This does not have the same rounding properties,
5839 so only do this if -funsafe-math-optimizations. We can actually
5840 always safely do it if ARG1 is a power of two, but it's hard to
5841 tell if it is or not in a portable manner. */
5842 if (TREE_CODE (arg1) == REAL_CST)
5844 if (flag_unsafe_math_optimizations
5845 && 0 != (tem = const_binop (code, build_real (type, dconst1),
5847 return fold (build (MULT_EXPR, type, arg0, tem));
5848 /* Find the reciprocal if optimizing and the result is exact. */
5852 r = TREE_REAL_CST (arg1);
5853 if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
5855 tem = build_real (type, r);
5856 return fold (build (MULT_EXPR, type, arg0, tem));
5862 case TRUNC_DIV_EXPR:
5863 case ROUND_DIV_EXPR:
5864 case FLOOR_DIV_EXPR:
5866 case EXACT_DIV_EXPR:
5867 if (integer_onep (arg1))
5868 return non_lvalue (convert (type, arg0));
5869 if (integer_zerop (arg1))
5872 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
5873 operation, EXACT_DIV_EXPR.
5875 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
5876 At one time others generated faster code, it's not clear if they do
5877 after the last round to changes to the DIV code in expmed.c. */
5878 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
5879 && multiple_of_p (type, arg0, arg1))
5880 return fold (build (EXACT_DIV_EXPR, type, arg0, arg1));
5882 if (TREE_CODE (arg1) == INTEGER_CST
5883 && 0 != (tem = extract_muldiv (TREE_OPERAND (t, 0), arg1,
5885 return convert (type, tem);
5890 case FLOOR_MOD_EXPR:
5891 case ROUND_MOD_EXPR:
5892 case TRUNC_MOD_EXPR:
5893 if (integer_onep (arg1))
5894 return omit_one_operand (type, integer_zero_node, arg0);
5895 if (integer_zerop (arg1))
5898 if (TREE_CODE (arg1) == INTEGER_CST
5899 && 0 != (tem = extract_muldiv (TREE_OPERAND (t, 0), arg1,
5901 return convert (type, tem);
5909 if (integer_zerop (arg1))
5910 return non_lvalue (convert (type, arg0));
5911 /* Since negative shift count is not well-defined,
5912 don't try to compute it in the compiler. */
5913 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
5915 /* Rewrite an LROTATE_EXPR by a constant into an
5916 RROTATE_EXPR by a new constant. */
5917 if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
5919 TREE_SET_CODE (t, RROTATE_EXPR);
5920 code = RROTATE_EXPR;
5921 TREE_OPERAND (t, 1) = arg1
5924 convert (TREE_TYPE (arg1),
5925 build_int_2 (GET_MODE_BITSIZE (TYPE_MODE (type)), 0)),
5927 if (tree_int_cst_sgn (arg1) < 0)
5931 /* If we have a rotate of a bit operation with the rotate count and
5932 the second operand of the bit operation both constant,
5933 permute the two operations. */
5934 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
5935 && (TREE_CODE (arg0) == BIT_AND_EXPR
5936 || TREE_CODE (arg0) == BIT_ANDTC_EXPR
5937 || TREE_CODE (arg0) == BIT_IOR_EXPR
5938 || TREE_CODE (arg0) == BIT_XOR_EXPR)
5939 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
5940 return fold (build (TREE_CODE (arg0), type,
5941 fold (build (code, type,
5942 TREE_OPERAND (arg0, 0), arg1)),
5943 fold (build (code, type,
5944 TREE_OPERAND (arg0, 1), arg1))));
5946 /* Two consecutive rotates adding up to the width of the mode can
5948 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
5949 && TREE_CODE (arg0) == RROTATE_EXPR
5950 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
5951 && TREE_INT_CST_HIGH (arg1) == 0
5952 && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
5953 && ((TREE_INT_CST_LOW (arg1)
5954 + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
5955 == (unsigned int) GET_MODE_BITSIZE (TYPE_MODE (type))))
5956 return TREE_OPERAND (arg0, 0);
5961 if (operand_equal_p (arg0, arg1, 0))
5962 return omit_one_operand (type, arg0, arg1);
5963 if (INTEGRAL_TYPE_P (type)
5964 && operand_equal_p (arg1, TYPE_MIN_VALUE (type), 1))
5965 return omit_one_operand (type, arg1, arg0);
5969 if (operand_equal_p (arg0, arg1, 0))
5970 return omit_one_operand (type, arg0, arg1);
5971 if (INTEGRAL_TYPE_P (type)
5972 && TYPE_MAX_VALUE (type)
5973 && operand_equal_p (arg1, TYPE_MAX_VALUE (type), 1))
5974 return omit_one_operand (type, arg1, arg0);
5977 case TRUTH_NOT_EXPR:
5978 /* Note that the operand of this must be an int
5979 and its values must be 0 or 1.
5980 ("true" is a fixed value perhaps depending on the language,
5981 but we don't handle values other than 1 correctly yet.) */
5982 tem = invert_truthvalue (arg0);
5983 /* Avoid infinite recursion. */
5984 if (TREE_CODE (tem) == TRUTH_NOT_EXPR)
5986 return convert (type, tem);
5988 case TRUTH_ANDIF_EXPR:
5989 /* Note that the operands of this must be ints
5990 and their values must be 0 or 1.
5991 ("true" is a fixed value perhaps depending on the language.) */
5992 /* If first arg is constant zero, return it. */
5993 if (integer_zerop (arg0))
5994 return convert (type, arg0);
5995 case TRUTH_AND_EXPR:
5996 /* If either arg is constant true, drop it. */
5997 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
5998 return non_lvalue (convert (type, arg1));
5999 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
6000 /* Preserve sequence points. */
6001 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
6002 return non_lvalue (convert (type, arg0));
6003 /* If second arg is constant zero, result is zero, but first arg
6004 must be evaluated. */
6005 if (integer_zerop (arg1))
6006 return omit_one_operand (type, arg1, arg0);
6007 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
6008 case will be handled here. */
6009 if (integer_zerop (arg0))
6010 return omit_one_operand (type, arg0, arg1);
6013 /* We only do these simplifications if we are optimizing. */
6017 /* Check for things like (A || B) && (A || C). We can convert this
6018 to A || (B && C). Note that either operator can be any of the four
6019 truth and/or operations and the transformation will still be
6020 valid. Also note that we only care about order for the
6021 ANDIF and ORIF operators. If B contains side effects, this
6022 might change the truth-value of A. */
6023 if (TREE_CODE (arg0) == TREE_CODE (arg1)
6024 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
6025 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
6026 || TREE_CODE (arg0) == TRUTH_AND_EXPR
6027 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
6028 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
6030 tree a00 = TREE_OPERAND (arg0, 0);
6031 tree a01 = TREE_OPERAND (arg0, 1);
6032 tree a10 = TREE_OPERAND (arg1, 0);
6033 tree a11 = TREE_OPERAND (arg1, 1);
6034 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
6035 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
6036 && (code == TRUTH_AND_EXPR
6037 || code == TRUTH_OR_EXPR));
6039 if (operand_equal_p (a00, a10, 0))
6040 return fold (build (TREE_CODE (arg0), type, a00,
6041 fold (build (code, type, a01, a11))));
6042 else if (commutative && operand_equal_p (a00, a11, 0))
6043 return fold (build (TREE_CODE (arg0), type, a00,
6044 fold (build (code, type, a01, a10))));
6045 else if (commutative && operand_equal_p (a01, a10, 0))
6046 return fold (build (TREE_CODE (arg0), type, a01,
6047 fold (build (code, type, a00, a11))));
6049 /* This case if tricky because we must either have commutative
6050 operators or else A10 must not have side-effects. */
6052 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
6053 && operand_equal_p (a01, a11, 0))
6054 return fold (build (TREE_CODE (arg0), type,
6055 fold (build (code, type, a00, a10)),
6059 /* See if we can build a range comparison. */
6060 if (0 != (tem = fold_range_test (t)))
6063 /* Check for the possibility of merging component references. If our
6064 lhs is another similar operation, try to merge its rhs with our
6065 rhs. Then try to merge our lhs and rhs. */
6066 if (TREE_CODE (arg0) == code
6067 && 0 != (tem = fold_truthop (code, type,
6068 TREE_OPERAND (arg0, 1), arg1)))
6069 return fold (build (code, type, TREE_OPERAND (arg0, 0), tem));
6071 if ((tem = fold_truthop (code, type, arg0, arg1)) != 0)
6076 case TRUTH_ORIF_EXPR:
6077 /* Note that the operands of this must be ints
6078 and their values must be 0 or true.
6079 ("true" is a fixed value perhaps depending on the language.) */
6080 /* If first arg is constant true, return it. */
6081 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
6082 return convert (type, arg0);
6084 /* If either arg is constant zero, drop it. */
6085 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
6086 return non_lvalue (convert (type, arg1));
6087 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
6088 /* Preserve sequence points. */
6089 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
6090 return non_lvalue (convert (type, arg0));
6091 /* If second arg is constant true, result is true, but we must
6092 evaluate first arg. */
6093 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
6094 return omit_one_operand (type, arg1, arg0);
6095 /* Likewise for first arg, but note this only occurs here for
6097 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
6098 return omit_one_operand (type, arg0, arg1);
6101 case TRUTH_XOR_EXPR:
6102 /* If either arg is constant zero, drop it. */
6103 if (integer_zerop (arg0))
6104 return non_lvalue (convert (type, arg1));
6105 if (integer_zerop (arg1))
6106 return non_lvalue (convert (type, arg0));
6107 /* If either arg is constant true, this is a logical inversion. */
6108 if (integer_onep (arg0))
6109 return non_lvalue (convert (type, invert_truthvalue (arg1)));
6110 if (integer_onep (arg1))
6111 return non_lvalue (convert (type, invert_truthvalue (arg0)));
6120 if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
6122 /* (-a) CMP (-b) -> b CMP a */
6123 if (TREE_CODE (arg0) == NEGATE_EXPR
6124 && TREE_CODE (arg1) == NEGATE_EXPR)
6125 return fold (build (code, type, TREE_OPERAND (arg1, 0),
6126 TREE_OPERAND (arg0, 0)));
6127 /* (-a) CMP CST -> a swap(CMP) (-CST) */
6128 if (TREE_CODE (arg0) == NEGATE_EXPR && TREE_CODE (arg1) == REAL_CST)
6131 (swap_tree_comparison (code), type,
6132 TREE_OPERAND (arg0, 0),
6133 build_real (TREE_TYPE (arg1),
6134 REAL_VALUE_NEGATE (TREE_REAL_CST (arg1)))));
6135 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
6136 /* a CMP (-0) -> a CMP 0 */
6137 if (TREE_CODE (arg1) == REAL_CST
6138 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (arg1)))
6139 return fold (build (code, type, arg0,
6140 build_real (TREE_TYPE (arg1), dconst0)));
6143 /* If one arg is a constant integer, put it last. */
6144 if (TREE_CODE (arg0) == INTEGER_CST
6145 && TREE_CODE (arg1) != INTEGER_CST)
6147 TREE_OPERAND (t, 0) = arg1;
6148 TREE_OPERAND (t, 1) = arg0;
6149 arg0 = TREE_OPERAND (t, 0);
6150 arg1 = TREE_OPERAND (t, 1);
6151 code = swap_tree_comparison (code);
6152 TREE_SET_CODE (t, code);
6155 /* Convert foo++ == CONST into ++foo == CONST + INCR.
6156 First, see if one arg is constant; find the constant arg
6157 and the other one. */
6159 tree constop = 0, varop = NULL_TREE;
6160 int constopnum = -1;
6162 if (TREE_CONSTANT (arg1))
6163 constopnum = 1, constop = arg1, varop = arg0;
6164 if (TREE_CONSTANT (arg0))
6165 constopnum = 0, constop = arg0, varop = arg1;
6167 if (constop && TREE_CODE (varop) == POSTINCREMENT_EXPR)
6169 /* This optimization is invalid for ordered comparisons
6170 if CONST+INCR overflows or if foo+incr might overflow.
6171 This optimization is invalid for floating point due to rounding.
6172 For pointer types we assume overflow doesn't happen. */
6173 if (POINTER_TYPE_P (TREE_TYPE (varop))
6174 || (! FLOAT_TYPE_P (TREE_TYPE (varop))
6175 && (code == EQ_EXPR || code == NE_EXPR)))
6178 = fold (build (PLUS_EXPR, TREE_TYPE (varop),
6179 constop, TREE_OPERAND (varop, 1)));
6181 /* Do not overwrite the current varop to be a preincrement,
6182 create a new node so that we won't confuse our caller who
6183 might create trees and throw them away, reusing the
6184 arguments that they passed to build. This shows up in
6185 the THEN or ELSE parts of ?: being postincrements. */
6186 varop = build (PREINCREMENT_EXPR, TREE_TYPE (varop),
6187 TREE_OPERAND (varop, 0),
6188 TREE_OPERAND (varop, 1));
6190 /* If VAROP is a reference to a bitfield, we must mask
6191 the constant by the width of the field. */
6192 if (TREE_CODE (TREE_OPERAND (varop, 0)) == COMPONENT_REF
6193 && DECL_BIT_FIELD(TREE_OPERAND
6194 (TREE_OPERAND (varop, 0), 1)))
6197 = TREE_INT_CST_LOW (DECL_SIZE
6199 (TREE_OPERAND (varop, 0), 1)));
6200 tree mask, unsigned_type;
6201 unsigned int precision;
6202 tree folded_compare;
6204 /* First check whether the comparison would come out
6205 always the same. If we don't do that we would
6206 change the meaning with the masking. */
6207 if (constopnum == 0)
6208 folded_compare = fold (build (code, type, constop,
6209 TREE_OPERAND (varop, 0)));
6211 folded_compare = fold (build (code, type,
6212 TREE_OPERAND (varop, 0),
6214 if (integer_zerop (folded_compare)
6215 || integer_onep (folded_compare))
6216 return omit_one_operand (type, folded_compare, varop);
6218 unsigned_type = type_for_size (size, 1);
6219 precision = TYPE_PRECISION (unsigned_type);
6220 mask = build_int_2 (~0, ~0);
6221 TREE_TYPE (mask) = unsigned_type;
6222 force_fit_type (mask, 0);
6223 mask = const_binop (RSHIFT_EXPR, mask,
6224 size_int (precision - size), 0);
6225 newconst = fold (build (BIT_AND_EXPR,
6226 TREE_TYPE (varop), newconst,
6227 convert (TREE_TYPE (varop),
6231 t = build (code, type,
6232 (constopnum == 0) ? newconst : varop,
6233 (constopnum == 1) ? newconst : varop);
6237 else if (constop && TREE_CODE (varop) == POSTDECREMENT_EXPR)
6239 if (POINTER_TYPE_P (TREE_TYPE (varop))
6240 || (! FLOAT_TYPE_P (TREE_TYPE (varop))
6241 && (code == EQ_EXPR || code == NE_EXPR)))
6244 = fold (build (MINUS_EXPR, TREE_TYPE (varop),
6245 constop, TREE_OPERAND (varop, 1)));
6247 /* Do not overwrite the current varop to be a predecrement,
6248 create a new node so that we won't confuse our caller who
6249 might create trees and throw them away, reusing the
6250 arguments that they passed to build. This shows up in
6251 the THEN or ELSE parts of ?: being postdecrements. */
6252 varop = build (PREDECREMENT_EXPR, TREE_TYPE (varop),
6253 TREE_OPERAND (varop, 0),
6254 TREE_OPERAND (varop, 1));
6256 if (TREE_CODE (TREE_OPERAND (varop, 0)) == COMPONENT_REF
6257 && DECL_BIT_FIELD(TREE_OPERAND
6258 (TREE_OPERAND (varop, 0), 1)))
6261 = TREE_INT_CST_LOW (DECL_SIZE
6263 (TREE_OPERAND (varop, 0), 1)));
6264 tree mask, unsigned_type;
6265 unsigned int precision;
6266 tree folded_compare;
6268 if (constopnum == 0)
6269 folded_compare = fold (build (code, type, constop,
6270 TREE_OPERAND (varop, 0)));
6272 folded_compare = fold (build (code, type,
6273 TREE_OPERAND (varop, 0),
6275 if (integer_zerop (folded_compare)
6276 || integer_onep (folded_compare))
6277 return omit_one_operand (type, folded_compare, varop);
6279 unsigned_type = type_for_size (size, 1);
6280 precision = TYPE_PRECISION (unsigned_type);
6281 mask = build_int_2 (~0, ~0);
6282 TREE_TYPE (mask) = TREE_TYPE (varop);
6283 force_fit_type (mask, 0);
6284 mask = const_binop (RSHIFT_EXPR, mask,
6285 size_int (precision - size), 0);
6286 newconst = fold (build (BIT_AND_EXPR,
6287 TREE_TYPE (varop), newconst,
6288 convert (TREE_TYPE (varop),
6292 t = build (code, type,
6293 (constopnum == 0) ? newconst : varop,
6294 (constopnum == 1) ? newconst : varop);
6300 /* Change X >= CST to X > (CST - 1) if CST is positive. */
6301 if (TREE_CODE (arg1) == INTEGER_CST
6302 && TREE_CODE (arg0) != INTEGER_CST
6303 && tree_int_cst_sgn (arg1) > 0)
6305 switch (TREE_CODE (t))
6309 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
6310 t = build (code, type, TREE_OPERAND (t, 0), arg1);
6315 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
6316 t = build (code, type, TREE_OPERAND (t, 0), arg1);
6324 /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
6325 a MINUS_EXPR of a constant, we can convert it into a comparison with
6326 a revised constant as long as no overflow occurs. */
6327 if ((code == EQ_EXPR || code == NE_EXPR)
6328 && TREE_CODE (arg1) == INTEGER_CST
6329 && (TREE_CODE (arg0) == PLUS_EXPR
6330 || TREE_CODE (arg0) == MINUS_EXPR)
6331 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
6332 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
6333 ? MINUS_EXPR : PLUS_EXPR,
6334 arg1, TREE_OPERAND (arg0, 1), 0))
6335 && ! TREE_CONSTANT_OVERFLOW (tem))
6336 return fold (build (code, type, TREE_OPERAND (arg0, 0), tem));
6338 /* Similarly for a NEGATE_EXPR. */
6339 else if ((code == EQ_EXPR || code == NE_EXPR)
6340 && TREE_CODE (arg0) == NEGATE_EXPR
6341 && TREE_CODE (arg1) == INTEGER_CST
6342 && 0 != (tem = negate_expr (arg1))
6343 && TREE_CODE (tem) == INTEGER_CST
6344 && ! TREE_CONSTANT_OVERFLOW (tem))
6345 return fold (build (code, type, TREE_OPERAND (arg0, 0), tem));
6347 /* If we have X - Y == 0, we can convert that to X == Y and similarly
6348 for !=. Don't do this for ordered comparisons due to overflow. */
6349 else if ((code == NE_EXPR || code == EQ_EXPR)
6350 && integer_zerop (arg1) && TREE_CODE (arg0) == MINUS_EXPR)
6351 return fold (build (code, type,
6352 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1)));
6354 /* If we are widening one operand of an integer comparison,
6355 see if the other operand is similarly being widened. Perhaps we
6356 can do the comparison in the narrower type. */
6357 else if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
6358 && TREE_CODE (arg0) == NOP_EXPR
6359 && (tem = get_unwidened (arg0, NULL_TREE)) != arg0
6360 && (t1 = get_unwidened (arg1, TREE_TYPE (tem))) != 0
6361 && (TREE_TYPE (t1) == TREE_TYPE (tem)
6362 || (TREE_CODE (t1) == INTEGER_CST
6363 && int_fits_type_p (t1, TREE_TYPE (tem)))))
6364 return fold (build (code, type, tem, convert (TREE_TYPE (tem), t1)));
6366 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
6367 constant, we can simplify it. */
6368 else if (TREE_CODE (arg1) == INTEGER_CST
6369 && (TREE_CODE (arg0) == MIN_EXPR
6370 || TREE_CODE (arg0) == MAX_EXPR)
6371 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
6372 return optimize_minmax_comparison (t);
6374 /* If we are comparing an ABS_EXPR with a constant, we can
6375 convert all the cases into explicit comparisons, but they may
6376 well not be faster than doing the ABS and one comparison.
6377 But ABS (X) <= C is a range comparison, which becomes a subtraction
6378 and a comparison, and is probably faster. */
6379 else if (code == LE_EXPR && TREE_CODE (arg1) == INTEGER_CST
6380 && TREE_CODE (arg0) == ABS_EXPR
6381 && ! TREE_SIDE_EFFECTS (arg0)
6382 && (0 != (tem = negate_expr (arg1)))
6383 && TREE_CODE (tem) == INTEGER_CST
6384 && ! TREE_CONSTANT_OVERFLOW (tem))
6385 return fold (build (TRUTH_ANDIF_EXPR, type,
6386 build (GE_EXPR, type, TREE_OPERAND (arg0, 0), tem),
6387 build (LE_EXPR, type,
6388 TREE_OPERAND (arg0, 0), arg1)));
6390 /* If this is an EQ or NE comparison with zero and ARG0 is
6391 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
6392 two operations, but the latter can be done in one less insn
6393 on machines that have only two-operand insns or on which a
6394 constant cannot be the first operand. */
6395 if (integer_zerop (arg1) && (code == EQ_EXPR || code == NE_EXPR)
6396 && TREE_CODE (arg0) == BIT_AND_EXPR)
6398 if (TREE_CODE (TREE_OPERAND (arg0, 0)) == LSHIFT_EXPR
6399 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 0), 0)))
6401 fold (build (code, type,
6402 build (BIT_AND_EXPR, TREE_TYPE (arg0),
6404 TREE_TYPE (TREE_OPERAND (arg0, 0)),
6405 TREE_OPERAND (arg0, 1),
6406 TREE_OPERAND (TREE_OPERAND (arg0, 0), 1)),
6407 convert (TREE_TYPE (arg0),
6410 else if (TREE_CODE (TREE_OPERAND (arg0, 1)) == LSHIFT_EXPR
6411 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 1), 0)))
6413 fold (build (code, type,
6414 build (BIT_AND_EXPR, TREE_TYPE (arg0),
6416 TREE_TYPE (TREE_OPERAND (arg0, 1)),
6417 TREE_OPERAND (arg0, 0),
6418 TREE_OPERAND (TREE_OPERAND (arg0, 1), 1)),
6419 convert (TREE_TYPE (arg0),
6424 /* If this is an NE or EQ comparison of zero against the result of a
6425 signed MOD operation whose second operand is a power of 2, make
6426 the MOD operation unsigned since it is simpler and equivalent. */
6427 if ((code == NE_EXPR || code == EQ_EXPR)
6428 && integer_zerop (arg1)
6429 && ! TREE_UNSIGNED (TREE_TYPE (arg0))
6430 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
6431 || TREE_CODE (arg0) == CEIL_MOD_EXPR
6432 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
6433 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
6434 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6436 tree newtype = unsigned_type (TREE_TYPE (arg0));
6437 tree newmod = build (TREE_CODE (arg0), newtype,
6438 convert (newtype, TREE_OPERAND (arg0, 0)),
6439 convert (newtype, TREE_OPERAND (arg0, 1)));
6441 return build (code, type, newmod, convert (newtype, arg1));
6444 /* If this is an NE comparison of zero with an AND of one, remove the
6445 comparison since the AND will give the correct value. */
6446 if (code == NE_EXPR && integer_zerop (arg1)
6447 && TREE_CODE (arg0) == BIT_AND_EXPR
6448 && integer_onep (TREE_OPERAND (arg0, 1)))
6449 return convert (type, arg0);
6451 /* If we have (A & C) == C where C is a power of 2, convert this into
6452 (A & C) != 0. Similarly for NE_EXPR. */
6453 if ((code == EQ_EXPR || code == NE_EXPR)
6454 && TREE_CODE (arg0) == BIT_AND_EXPR
6455 && integer_pow2p (TREE_OPERAND (arg0, 1))
6456 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
6457 return build (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
6458 arg0, integer_zero_node);
6460 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
6461 and similarly for >= into !=. */
6462 if ((code == LT_EXPR || code == GE_EXPR)
6463 && TREE_UNSIGNED (TREE_TYPE (arg0))
6464 && TREE_CODE (arg1) == LSHIFT_EXPR
6465 && integer_onep (TREE_OPERAND (arg1, 0)))
6466 return build (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
6467 build (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
6468 TREE_OPERAND (arg1, 1)),
6469 convert (TREE_TYPE (arg0), integer_zero_node));
6471 else if ((code == LT_EXPR || code == GE_EXPR)
6472 && TREE_UNSIGNED (TREE_TYPE (arg0))
6473 && (TREE_CODE (arg1) == NOP_EXPR
6474 || TREE_CODE (arg1) == CONVERT_EXPR)
6475 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
6476 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
6478 build (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
6479 convert (TREE_TYPE (arg0),
6480 build (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
6481 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1))),
6482 convert (TREE_TYPE (arg0), integer_zero_node));
6484 /* Simplify comparison of something with itself. (For IEEE
6485 floating-point, we can only do some of these simplifications.) */
6486 if (operand_equal_p (arg0, arg1, 0))
6493 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
6494 return constant_boolean_node (1, type);
6496 TREE_SET_CODE (t, code);
6500 /* For NE, we can only do this simplification if integer. */
6501 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
6503 /* ... fall through ... */
6506 return constant_boolean_node (0, type);
6512 /* An unsigned comparison against 0 can be simplified. */
6513 if (integer_zerop (arg1)
6514 && (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
6515 || POINTER_TYPE_P (TREE_TYPE (arg1)))
6516 && TREE_UNSIGNED (TREE_TYPE (arg1)))
6518 switch (TREE_CODE (t))
6522 TREE_SET_CODE (t, NE_EXPR);
6526 TREE_SET_CODE (t, EQ_EXPR);
6529 return omit_one_operand (type,
6530 convert (type, integer_one_node),
6533 return omit_one_operand (type,
6534 convert (type, integer_zero_node),
6541 /* Comparisons with the highest or lowest possible integer of
6542 the specified size will have known values and an unsigned
6543 <= 0x7fffffff can be simplified. */
6545 int width = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg1)));
6547 if (TREE_CODE (arg1) == INTEGER_CST
6548 && ! TREE_CONSTANT_OVERFLOW (arg1)
6549 && width <= HOST_BITS_PER_WIDE_INT
6550 && (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
6551 || POINTER_TYPE_P (TREE_TYPE (arg1))))
6553 if (TREE_INT_CST_HIGH (arg1) == 0
6554 && (TREE_INT_CST_LOW (arg1)
6555 == ((unsigned HOST_WIDE_INT) 1 << (width - 1)) - 1)
6556 && ! TREE_UNSIGNED (TREE_TYPE (arg1)))
6557 switch (TREE_CODE (t))
6560 return omit_one_operand (type,
6561 convert (type, integer_zero_node),
6564 TREE_SET_CODE (t, EQ_EXPR);
6568 return omit_one_operand (type,
6569 convert (type, integer_one_node),
6572 TREE_SET_CODE (t, NE_EXPR);
6579 else if (TREE_INT_CST_HIGH (arg1) == -1
6580 && (- TREE_INT_CST_LOW (arg1)
6581 == ((unsigned HOST_WIDE_INT) 1 << (width - 1)))
6582 && ! TREE_UNSIGNED (TREE_TYPE (arg1)))
6583 switch (TREE_CODE (t))
6586 return omit_one_operand (type,
6587 convert (type, integer_zero_node),
6590 TREE_SET_CODE (t, EQ_EXPR);
6594 return omit_one_operand (type,
6595 convert (type, integer_one_node),
6598 TREE_SET_CODE (t, NE_EXPR);
6605 else if (TREE_INT_CST_HIGH (arg1) == 0
6606 && (TREE_INT_CST_LOW (arg1)
6607 == ((unsigned HOST_WIDE_INT) 1 << (width - 1)) - 1)
6608 && TREE_UNSIGNED (TREE_TYPE (arg1)))
6610 switch (TREE_CODE (t))
6613 return fold (build (GE_EXPR, type,
6614 convert (signed_type (TREE_TYPE (arg0)),
6616 convert (signed_type (TREE_TYPE (arg1)),
6617 integer_zero_node)));
6619 return fold (build (LT_EXPR, type,
6620 convert (signed_type (TREE_TYPE (arg0)),
6622 convert (signed_type (TREE_TYPE (arg1)),
6623 integer_zero_node)));
6631 /* If we are comparing an expression that just has comparisons
6632 of two integer values, arithmetic expressions of those comparisons,
6633 and constants, we can simplify it. There are only three cases
6634 to check: the two values can either be equal, the first can be
6635 greater, or the second can be greater. Fold the expression for
6636 those three values. Since each value must be 0 or 1, we have
6637 eight possibilities, each of which corresponds to the constant 0
6638 or 1 or one of the six possible comparisons.
6640 This handles common cases like (a > b) == 0 but also handles
6641 expressions like ((x > y) - (y > x)) > 0, which supposedly
6642 occur in macroized code. */
6644 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
6646 tree cval1 = 0, cval2 = 0;
6649 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
6650 /* Don't handle degenerate cases here; they should already
6651 have been handled anyway. */
6652 && cval1 != 0 && cval2 != 0
6653 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
6654 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
6655 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
6656 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
6657 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
6658 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
6659 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
6661 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
6662 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
6664 /* We can't just pass T to eval_subst in case cval1 or cval2
6665 was the same as ARG1. */
6668 = fold (build (code, type,
6669 eval_subst (arg0, cval1, maxval, cval2, minval),
6672 = fold (build (code, type,
6673 eval_subst (arg0, cval1, maxval, cval2, maxval),
6676 = fold (build (code, type,
6677 eval_subst (arg0, cval1, minval, cval2, maxval),
6680 /* All three of these results should be 0 or 1. Confirm they
6681 are. Then use those values to select the proper code
6684 if ((integer_zerop (high_result)
6685 || integer_onep (high_result))
6686 && (integer_zerop (equal_result)
6687 || integer_onep (equal_result))
6688 && (integer_zerop (low_result)
6689 || integer_onep (low_result)))
6691 /* Make a 3-bit mask with the high-order bit being the
6692 value for `>', the next for '=', and the low for '<'. */
6693 switch ((integer_onep (high_result) * 4)
6694 + (integer_onep (equal_result) * 2)
6695 + integer_onep (low_result))
6699 return omit_one_operand (type, integer_zero_node, arg0);
6720 return omit_one_operand (type, integer_one_node, arg0);
6723 t = build (code, type, cval1, cval2);
6725 return save_expr (t);
6732 /* If this is a comparison of a field, we may be able to simplify it. */
6733 if ((TREE_CODE (arg0) == COMPONENT_REF
6734 || TREE_CODE (arg0) == BIT_FIELD_REF)
6735 && (code == EQ_EXPR || code == NE_EXPR)
6736 /* Handle the constant case even without -O
6737 to make sure the warnings are given. */
6738 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
6740 t1 = optimize_bit_field_compare (code, type, arg0, arg1);
6744 /* If this is a comparison of complex values and either or both sides
6745 are a COMPLEX_EXPR or COMPLEX_CST, it is best to split up the
6746 comparisons and join them with a TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR.
6747 This may prevent needless evaluations. */
6748 if ((code == EQ_EXPR || code == NE_EXPR)
6749 && TREE_CODE (TREE_TYPE (arg0)) == COMPLEX_TYPE
6750 && (TREE_CODE (arg0) == COMPLEX_EXPR
6751 || TREE_CODE (arg1) == COMPLEX_EXPR
6752 || TREE_CODE (arg0) == COMPLEX_CST
6753 || TREE_CODE (arg1) == COMPLEX_CST))
6755 tree subtype = TREE_TYPE (TREE_TYPE (arg0));
6756 tree real0, imag0, real1, imag1;
6758 arg0 = save_expr (arg0);
6759 arg1 = save_expr (arg1);
6760 real0 = fold (build1 (REALPART_EXPR, subtype, arg0));
6761 imag0 = fold (build1 (IMAGPART_EXPR, subtype, arg0));
6762 real1 = fold (build1 (REALPART_EXPR, subtype, arg1));
6763 imag1 = fold (build1 (IMAGPART_EXPR, subtype, arg1));
6765 return fold (build ((code == EQ_EXPR ? TRUTH_ANDIF_EXPR
6768 fold (build (code, type, real0, real1)),
6769 fold (build (code, type, imag0, imag1))));
6772 /* From here on, the only cases we handle are when the result is
6773 known to be a constant.
6775 To compute GT, swap the arguments and do LT.
6776 To compute GE, do LT and invert the result.
6777 To compute LE, swap the arguments, do LT and invert the result.
6778 To compute NE, do EQ and invert the result.
6780 Therefore, the code below must handle only EQ and LT. */
6782 if (code == LE_EXPR || code == GT_EXPR)
6784 tem = arg0, arg0 = arg1, arg1 = tem;
6785 code = swap_tree_comparison (code);
6788 /* Note that it is safe to invert for real values here because we
6789 will check below in the one case that it matters. */
6793 if (code == NE_EXPR || code == GE_EXPR)
6796 code = invert_tree_comparison (code);
6799 /* Compute a result for LT or EQ if args permit;
6800 otherwise return T. */
6801 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
6803 if (code == EQ_EXPR)
6804 t1 = build_int_2 (tree_int_cst_equal (arg0, arg1), 0);
6806 t1 = build_int_2 ((TREE_UNSIGNED (TREE_TYPE (arg0))
6807 ? INT_CST_LT_UNSIGNED (arg0, arg1)
6808 : INT_CST_LT (arg0, arg1)),
6812 #if 0 /* This is no longer useful, but breaks some real code. */
6813 /* Assume a nonexplicit constant cannot equal an explicit one,
6814 since such code would be undefined anyway.
6815 Exception: on sysvr4, using #pragma weak,
6816 a label can come out as 0. */
6817 else if (TREE_CODE (arg1) == INTEGER_CST
6818 && !integer_zerop (arg1)
6819 && TREE_CONSTANT (arg0)
6820 && TREE_CODE (arg0) == ADDR_EXPR
6822 t1 = build_int_2 (0, 0);
6824 /* Two real constants can be compared explicitly. */
6825 else if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
6827 /* If either operand is a NaN, the result is false with two
6828 exceptions: First, an NE_EXPR is true on NaNs, but that case
6829 is already handled correctly since we will be inverting the
6830 result for NE_EXPR. Second, if we had inverted a LE_EXPR
6831 or a GE_EXPR into a LT_EXPR, we must return true so that it
6832 will be inverted into false. */
6834 if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
6835 || REAL_VALUE_ISNAN (TREE_REAL_CST (arg1)))
6836 t1 = build_int_2 (invert && code == LT_EXPR, 0);
6838 else if (code == EQ_EXPR)
6839 t1 = build_int_2 (REAL_VALUES_EQUAL (TREE_REAL_CST (arg0),
6840 TREE_REAL_CST (arg1)),
6843 t1 = build_int_2 (REAL_VALUES_LESS (TREE_REAL_CST (arg0),
6844 TREE_REAL_CST (arg1)),
6848 if (t1 == NULL_TREE)
6852 TREE_INT_CST_LOW (t1) ^= 1;
6854 TREE_TYPE (t1) = type;
6855 if (TREE_CODE (type) == BOOLEAN_TYPE)
6856 return truthvalue_conversion (t1);
6860 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
6861 so all simple results must be passed through pedantic_non_lvalue. */
6862 if (TREE_CODE (arg0) == INTEGER_CST)
6863 return pedantic_non_lvalue
6864 (TREE_OPERAND (t, (integer_zerop (arg0) ? 2 : 1)));
6865 else if (operand_equal_p (arg1, TREE_OPERAND (expr, 2), 0))
6866 return pedantic_omit_one_operand (type, arg1, arg0);
6868 /* If the second operand is zero, invert the comparison and swap
6869 the second and third operands. Likewise if the second operand
6870 is constant and the third is not or if the third operand is
6871 equivalent to the first operand of the comparison. */
6873 if (integer_zerop (arg1)
6874 || (TREE_CONSTANT (arg1) && ! TREE_CONSTANT (TREE_OPERAND (t, 2)))
6875 || (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
6876 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
6877 TREE_OPERAND (t, 2),
6878 TREE_OPERAND (arg0, 1))))
6880 /* See if this can be inverted. If it can't, possibly because
6881 it was a floating-point inequality comparison, don't do
6883 tem = invert_truthvalue (arg0);
6885 if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
6887 t = build (code, type, tem,
6888 TREE_OPERAND (t, 2), TREE_OPERAND (t, 1));
6890 /* arg1 should be the first argument of the new T. */
6891 arg1 = TREE_OPERAND (t, 1);
6896 /* If we have A op B ? A : C, we may be able to convert this to a
6897 simpler expression, depending on the operation and the values
6898 of B and C. IEEE floating point prevents this though,
6899 because A or B might be -0.0 or a NaN. */
6901 if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
6902 && (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
6903 || ! FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 0)))
6904 || flag_unsafe_math_optimizations)
6905 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
6906 arg1, TREE_OPERAND (arg0, 1)))
6908 tree arg2 = TREE_OPERAND (t, 2);
6909 enum tree_code comp_code = TREE_CODE (arg0);
6913 /* If we have A op 0 ? A : -A, this is A, -A, abs (A), or abs (-A),
6914 depending on the comparison operation. */
6915 if ((FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 1)))
6916 ? real_zerop (TREE_OPERAND (arg0, 1))
6917 : integer_zerop (TREE_OPERAND (arg0, 1)))
6918 && TREE_CODE (arg2) == NEGATE_EXPR
6919 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
6927 (convert (TREE_TYPE (TREE_OPERAND (t, 1)),
6931 return pedantic_non_lvalue (convert (type, arg1));
6934 if (TREE_UNSIGNED (TREE_TYPE (arg1)))
6935 arg1 = convert (signed_type (TREE_TYPE (arg1)), arg1);
6936 return pedantic_non_lvalue
6937 (convert (type, fold (build1 (ABS_EXPR,
6938 TREE_TYPE (arg1), arg1))));
6941 if (TREE_UNSIGNED (TREE_TYPE (arg1)))
6942 arg1 = convert (signed_type (TREE_TYPE (arg1)), arg1);
6943 return pedantic_non_lvalue
6944 (negate_expr (convert (type,
6945 fold (build1 (ABS_EXPR,
6952 /* If this is A != 0 ? A : 0, this is simply A. For ==, it is
6955 if (integer_zerop (TREE_OPERAND (arg0, 1)) && integer_zerop (arg2))
6957 if (comp_code == NE_EXPR)
6958 return pedantic_non_lvalue (convert (type, arg1));
6959 else if (comp_code == EQ_EXPR)
6960 return pedantic_non_lvalue (convert (type, integer_zero_node));
6963 /* If this is A op B ? A : B, this is either A, B, min (A, B),
6964 or max (A, B), depending on the operation. */
6966 if (operand_equal_for_comparison_p (TREE_OPERAND (arg0, 1),
6967 arg2, TREE_OPERAND (arg0, 0)))
6969 tree comp_op0 = TREE_OPERAND (arg0, 0);
6970 tree comp_op1 = TREE_OPERAND (arg0, 1);
6971 tree comp_type = TREE_TYPE (comp_op0);
6973 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
6974 if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
6980 return pedantic_non_lvalue (convert (type, arg2));
6982 return pedantic_non_lvalue (convert (type, arg1));
6985 /* In C++ a ?: expression can be an lvalue, so put the
6986 operand which will be used if they are equal first
6987 so that we can convert this back to the
6988 corresponding COND_EXPR. */
6989 return pedantic_non_lvalue
6990 (convert (type, fold (build (MIN_EXPR, comp_type,
6991 (comp_code == LE_EXPR
6992 ? comp_op0 : comp_op1),
6993 (comp_code == LE_EXPR
6994 ? comp_op1 : comp_op0)))));
6998 return pedantic_non_lvalue
6999 (convert (type, fold (build (MAX_EXPR, comp_type,
7000 (comp_code == GE_EXPR
7001 ? comp_op0 : comp_op1),
7002 (comp_code == GE_EXPR
7003 ? comp_op1 : comp_op0)))));
7010 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
7011 we might still be able to simplify this. For example,
7012 if C1 is one less or one more than C2, this might have started
7013 out as a MIN or MAX and been transformed by this function.
7014 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
7016 if (INTEGRAL_TYPE_P (type)
7017 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7018 && TREE_CODE (arg2) == INTEGER_CST)
7022 /* We can replace A with C1 in this case. */
7023 arg1 = convert (type, TREE_OPERAND (arg0, 1));
7024 t = build (code, type, TREE_OPERAND (t, 0), arg1,
7025 TREE_OPERAND (t, 2));
7029 /* If C1 is C2 + 1, this is min(A, C2). */
7030 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type), 1)
7031 && operand_equal_p (TREE_OPERAND (arg0, 1),
7032 const_binop (PLUS_EXPR, arg2,
7033 integer_one_node, 0), 1))
7034 return pedantic_non_lvalue
7035 (fold (build (MIN_EXPR, type, arg1, arg2)));
7039 /* If C1 is C2 - 1, this is min(A, C2). */
7040 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type), 1)
7041 && operand_equal_p (TREE_OPERAND (arg0, 1),
7042 const_binop (MINUS_EXPR, arg2,
7043 integer_one_node, 0), 1))
7044 return pedantic_non_lvalue
7045 (fold (build (MIN_EXPR, type, arg1, arg2)));
7049 /* If C1 is C2 - 1, this is max(A, C2). */
7050 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type), 1)
7051 && operand_equal_p (TREE_OPERAND (arg0, 1),
7052 const_binop (MINUS_EXPR, arg2,
7053 integer_one_node, 0), 1))
7054 return pedantic_non_lvalue
7055 (fold (build (MAX_EXPR, type, arg1, arg2)));
7059 /* If C1 is C2 + 1, this is max(A, C2). */
7060 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type), 1)
7061 && operand_equal_p (TREE_OPERAND (arg0, 1),
7062 const_binop (PLUS_EXPR, arg2,
7063 integer_one_node, 0), 1))
7064 return pedantic_non_lvalue
7065 (fold (build (MAX_EXPR, type, arg1, arg2)));
7074 /* If the second operand is simpler than the third, swap them
7075 since that produces better jump optimization results. */
7076 if ((TREE_CONSTANT (arg1) || DECL_P (arg1)
7077 || TREE_CODE (arg1) == SAVE_EXPR)
7078 && ! (TREE_CONSTANT (TREE_OPERAND (t, 2))
7079 || DECL_P (TREE_OPERAND (t, 2))
7080 || TREE_CODE (TREE_OPERAND (t, 2)) == SAVE_EXPR))
7082 /* See if this can be inverted. If it can't, possibly because
7083 it was a floating-point inequality comparison, don't do
7085 tem = invert_truthvalue (arg0);
7087 if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
7089 t = build (code, type, tem,
7090 TREE_OPERAND (t, 2), TREE_OPERAND (t, 1));
7092 /* arg1 should be the first argument of the new T. */
7093 arg1 = TREE_OPERAND (t, 1);
7098 /* Convert A ? 1 : 0 to simply A. */
7099 if (integer_onep (TREE_OPERAND (t, 1))
7100 && integer_zerop (TREE_OPERAND (t, 2))
7101 /* If we try to convert TREE_OPERAND (t, 0) to our type, the
7102 call to fold will try to move the conversion inside
7103 a COND, which will recurse. In that case, the COND_EXPR
7104 is probably the best choice, so leave it alone. */
7105 && type == TREE_TYPE (arg0))
7106 return pedantic_non_lvalue (arg0);
7108 /* Look for expressions of the form A & 2 ? 2 : 0. The result of this
7109 operation is simply A & 2. */
7111 if (integer_zerop (TREE_OPERAND (t, 2))
7112 && TREE_CODE (arg0) == NE_EXPR
7113 && integer_zerop (TREE_OPERAND (arg0, 1))
7114 && integer_pow2p (arg1)
7115 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
7116 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
7118 return pedantic_non_lvalue (convert (type, TREE_OPERAND (arg0, 0)));
7123 /* When pedantic, a compound expression can be neither an lvalue
7124 nor an integer constant expression. */
7125 if (TREE_SIDE_EFFECTS (arg0) || pedantic)
7127 /* Don't let (0, 0) be null pointer constant. */
7128 if (integer_zerop (arg1))
7129 return build1 (NOP_EXPR, type, arg1);
7130 return convert (type, arg1);
7134 return build_complex (type, arg0, arg1);
7138 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
7140 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
7141 return omit_one_operand (type, TREE_OPERAND (arg0, 0),
7142 TREE_OPERAND (arg0, 1));
7143 else if (TREE_CODE (arg0) == COMPLEX_CST)
7144 return TREE_REALPART (arg0);
7145 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
7146 return fold (build (TREE_CODE (arg0), type,
7147 fold (build1 (REALPART_EXPR, type,
7148 TREE_OPERAND (arg0, 0))),
7149 fold (build1 (REALPART_EXPR,
7150 type, TREE_OPERAND (arg0, 1)))));
7154 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
7155 return convert (type, integer_zero_node);
7156 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
7157 return omit_one_operand (type, TREE_OPERAND (arg0, 1),
7158 TREE_OPERAND (arg0, 0));
7159 else if (TREE_CODE (arg0) == COMPLEX_CST)
7160 return TREE_IMAGPART (arg0);
7161 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
7162 return fold (build (TREE_CODE (arg0), type,
7163 fold (build1 (IMAGPART_EXPR, type,
7164 TREE_OPERAND (arg0, 0))),
7165 fold (build1 (IMAGPART_EXPR, type,
7166 TREE_OPERAND (arg0, 1)))));
7169 /* Pull arithmetic ops out of the CLEANUP_POINT_EXPR where
7171 case CLEANUP_POINT_EXPR:
7172 if (! has_cleanups (arg0))
7173 return TREE_OPERAND (t, 0);
7176 enum tree_code code0 = TREE_CODE (arg0);
7177 int kind0 = TREE_CODE_CLASS (code0);
7178 tree arg00 = TREE_OPERAND (arg0, 0);
7181 if (kind0 == '1' || code0 == TRUTH_NOT_EXPR)
7182 return fold (build1 (code0, type,
7183 fold (build1 (CLEANUP_POINT_EXPR,
7184 TREE_TYPE (arg00), arg00))));
7186 if (kind0 == '<' || kind0 == '2'
7187 || code0 == TRUTH_ANDIF_EXPR || code0 == TRUTH_ORIF_EXPR
7188 || code0 == TRUTH_AND_EXPR || code0 == TRUTH_OR_EXPR
7189 || code0 == TRUTH_XOR_EXPR)
7191 arg01 = TREE_OPERAND (arg0, 1);
7193 if (TREE_CONSTANT (arg00)
7194 || ((code0 == TRUTH_ANDIF_EXPR || code0 == TRUTH_ORIF_EXPR)
7195 && ! has_cleanups (arg00)))
7196 return fold (build (code0, type, arg00,
7197 fold (build1 (CLEANUP_POINT_EXPR,
7198 TREE_TYPE (arg01), arg01))));
7200 if (TREE_CONSTANT (arg01))
7201 return fold (build (code0, type,
7202 fold (build1 (CLEANUP_POINT_EXPR,
7203 TREE_TYPE (arg00), arg00)),
7211 /* Check for a built-in function. */
7212 if (TREE_CODE (TREE_OPERAND (expr, 0)) == ADDR_EXPR
7213 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (expr, 0), 0))
7215 && DECL_BUILT_IN (TREE_OPERAND (TREE_OPERAND (expr, 0), 0)))
7217 tree tmp = fold_builtin (expr);
7225 } /* switch (code) */
7228 /* Determine if first argument is a multiple of second argument. Return 0 if
7229 it is not, or we cannot easily determined it to be.
7231 An example of the sort of thing we care about (at this point; this routine
7232 could surely be made more general, and expanded to do what the *_DIV_EXPR's
7233 fold cases do now) is discovering that
7235 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
7241 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
7243 This code also handles discovering that
7245 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
7247 is a multiple of 8 so we don't have to worry about dealing with a
7250 Note that we *look* inside a SAVE_EXPR only to determine how it was
7251 calculated; it is not safe for fold to do much of anything else with the
7252 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
7253 at run time. For example, the latter example above *cannot* be implemented
7254 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
7255 evaluation time of the original SAVE_EXPR is not necessarily the same at
7256 the time the new expression is evaluated. The only optimization of this
7257 sort that would be valid is changing
7259 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
7263 SAVE_EXPR (I) * SAVE_EXPR (J)
7265 (where the same SAVE_EXPR (J) is used in the original and the
7266 transformed version). */
7269 multiple_of_p (type, top, bottom)
7274 if (operand_equal_p (top, bottom, 0))
7277 if (TREE_CODE (type) != INTEGER_TYPE)
7280 switch (TREE_CODE (top))
7283 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
7284 || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
7288 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
7289 && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
7292 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
7296 op1 = TREE_OPERAND (top, 1);
7297 /* const_binop may not detect overflow correctly,
7298 so check for it explicitly here. */
7299 if (TYPE_PRECISION (TREE_TYPE (size_one_node))
7300 > TREE_INT_CST_LOW (op1)
7301 && TREE_INT_CST_HIGH (op1) == 0
7302 && 0 != (t1 = convert (type,
7303 const_binop (LSHIFT_EXPR, size_one_node,
7305 && ! TREE_OVERFLOW (t1))
7306 return multiple_of_p (type, t1, bottom);
7311 /* Can't handle conversions from non-integral or wider integral type. */
7312 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
7313 || (TYPE_PRECISION (type)
7314 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
7317 /* .. fall through ... */
7320 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
7323 if (TREE_CODE (bottom) != INTEGER_CST
7324 || (TREE_UNSIGNED (type)
7325 && (tree_int_cst_sgn (top) < 0
7326 || tree_int_cst_sgn (bottom) < 0)))
7328 return integer_zerop (const_binop (TRUNC_MOD_EXPR,
7336 /* Return true if `t' is known to be non-negative. */
7339 tree_expr_nonnegative_p (t)
7342 switch (TREE_CODE (t))
7345 return tree_int_cst_sgn (t) >= 0;
7347 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1))
7348 && tree_expr_nonnegative_p (TREE_OPERAND (t, 2));
7350 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
7352 return rtl_expr_nonnegative_p (RTL_EXPR_RTL (t));
7355 if (truth_value_p (TREE_CODE (t)))
7356 /* Truth values evaluate to 0 or 1, which is nonnegative. */
7359 /* We don't know sign of `t', so be conservative and return false. */
7364 /* Return true if `r' is known to be non-negative.
7365 Only handles constants at the moment. */
7368 rtl_expr_nonnegative_p (r)
7371 switch (GET_CODE (r))
7374 return INTVAL (r) >= 0;
7377 if (GET_MODE (r) == VOIDmode)
7378 return CONST_DOUBLE_HIGH (r) >= 0;
7383 /* These are always nonnegative. */