1 /* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987, 1988, 1992, 1993 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20 /*@@ Fix lossage on folding division of big integers. */
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. */
31 /* The entry points in this file are fold, size_int and 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'. */
48 /* Handle floating overflow for `const_binop'. */
49 static jmp_buf float_error;
51 static void encode PROTO((short *, HOST_WIDE_INT, HOST_WIDE_INT));
52 static void decode PROTO((short *, HOST_WIDE_INT *, HOST_WIDE_INT *));
53 static int div_and_round_double PROTO((enum tree_code, int, HOST_WIDE_INT,
54 HOST_WIDE_INT, HOST_WIDE_INT,
55 HOST_WIDE_INT, HOST_WIDE_INT *,
56 HOST_WIDE_INT *, HOST_WIDE_INT *,
58 static int split_tree PROTO((tree, enum tree_code, tree *, tree *, int *));
59 static tree const_binop PROTO((enum tree_code, tree, tree, int));
60 static tree fold_convert PROTO((tree, tree));
61 static enum tree_code invert_tree_comparison PROTO((enum tree_code));
62 static enum tree_code swap_tree_comparison PROTO((enum tree_code));
63 static int operand_equal_for_comparison_p PROTO((tree, tree, tree));
64 static int twoval_comparison_p PROTO((tree, tree *, tree *));
65 static tree eval_subst PROTO((tree, tree, tree, tree, tree));
66 static tree omit_one_operand PROTO((tree, tree, tree));
67 static tree distribute_bit_expr PROTO((enum tree_code, tree, tree, tree));
68 static tree make_bit_field_ref PROTO((tree, tree, int, int, int));
69 static tree optimize_bit_field_compare PROTO((enum tree_code, tree,
71 static tree decode_field_reference PROTO((tree, int *, int *,
72 enum machine_mode *, int *,
74 static int all_ones_mask_p PROTO((tree, int));
75 static int simple_operand_p PROTO((tree));
76 static tree range_test PROTO((enum tree_code, tree, enum tree_code,
77 enum tree_code, tree, tree, tree));
78 static tree fold_truthop PROTO((enum tree_code, tree, tree, tree));
84 /* Yield nonzero if a signed left shift of A by B bits overflows. */
85 #define left_shift_overflows(a, b) ((a) != ((a) << (b)) >> (b))
87 /* Suppose A1 + B1 = SUM1, using 2's complement arithmetic ignoring overflow.
88 Suppose A, B and SUM have the same respective signs as A1, B1, and SUM1.
89 Then this yields nonzero if overflow occurred during the addition.
90 Overflow occurs if A and B have the same sign, but A and SUM differ in sign.
91 Use `^' to test whether signs differ, and `< 0' to isolate the sign. */
92 #define overflow_sum_sign(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
94 /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
95 We do that by representing the two-word integer as MAX_SHORTS shorts,
96 with only 8 bits stored in each short, as a positive number. */
98 /* Unpack a two-word integer into MAX_SHORTS shorts.
99 LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
100 SHORTS points to the array of shorts. */
103 encode (shorts, low, hi)
105 HOST_WIDE_INT low, hi;
109 for (i = 0; i < MAX_SHORTS / 2; i++)
111 shorts[i] = (low >> (i * 8)) & 0xff;
112 shorts[i + MAX_SHORTS / 2] = (hi >> (i * 8) & 0xff);
116 /* Pack an array of MAX_SHORTS shorts into a two-word integer.
117 SHORTS points to the array of shorts.
118 The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces. */
121 decode (shorts, low, hi)
123 HOST_WIDE_INT *low, *hi;
126 HOST_WIDE_INT lv = 0, hv = 0;
128 for (i = 0; i < MAX_SHORTS / 2; i++)
130 lv |= (HOST_WIDE_INT) shorts[i] << (i * 8);
131 hv |= (HOST_WIDE_INT) shorts[i + MAX_SHORTS / 2] << (i * 8);
137 /* Make the integer constant T valid for its type
138 by setting to 0 or 1 all the bits in the constant
139 that don't belong in the type.
140 Yield 1 if a signed overflow occurs, 0 otherwise.
141 If OVERFLOW is nonzero, a signed overflow has already occurred
142 in calculating T, so propagate it. */
145 force_fit_type (t, overflow)
149 HOST_WIDE_INT low, high;
152 if (TREE_CODE (t) != INTEGER_CST)
155 low = TREE_INT_CST_LOW (t);
156 high = TREE_INT_CST_HIGH (t);
158 if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
161 prec = TYPE_PRECISION (TREE_TYPE (t));
163 /* First clear all bits that are beyond the type's precision. */
165 if (prec == 2 * HOST_BITS_PER_WIDE_INT)
167 else if (prec > HOST_BITS_PER_WIDE_INT)
169 TREE_INT_CST_HIGH (t)
170 &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
174 TREE_INT_CST_HIGH (t) = 0;
175 if (prec < HOST_BITS_PER_WIDE_INT)
176 TREE_INT_CST_LOW (t) &= ~((HOST_WIDE_INT) (-1) << prec);
179 /* Unsigned types do not suffer sign extension or overflow. */
180 if (TREE_UNSIGNED (TREE_TYPE (t)))
183 /* If the value's sign bit is set, extend the sign. */
184 if (prec != 2 * HOST_BITS_PER_WIDE_INT
185 && (prec > HOST_BITS_PER_WIDE_INT
186 ? (TREE_INT_CST_HIGH (t)
187 & ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)))
188 : TREE_INT_CST_LOW (t) & ((HOST_WIDE_INT) 1 << (prec - 1))))
190 /* Value is negative:
191 set to 1 all the bits that are outside this type's precision. */
192 if (prec > HOST_BITS_PER_WIDE_INT)
194 TREE_INT_CST_HIGH (t)
195 |= ((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
199 TREE_INT_CST_HIGH (t) = -1;
200 if (prec < HOST_BITS_PER_WIDE_INT)
201 TREE_INT_CST_LOW (t) |= ((HOST_WIDE_INT) (-1) << prec);
205 /* Yield nonzero if signed overflow occurred. */
207 ((overflow | (low ^ TREE_INT_CST_LOW (t)) | (high ^ TREE_INT_CST_HIGH (t)))
211 /* Add two doubleword integers with doubleword result.
212 Each argument is given as two `HOST_WIDE_INT' pieces.
213 One argument is L1 and H1; the other, L2 and H2.
214 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.
215 We use the 8-shorts representation internally. */
218 add_double (l1, h1, l2, h2, lv, hv)
219 HOST_WIDE_INT l1, h1, l2, h2;
220 HOST_WIDE_INT *lv, *hv;
222 short arg1[MAX_SHORTS];
223 short arg2[MAX_SHORTS];
224 register int carry = 0;
227 encode (arg1, l1, h1);
228 encode (arg2, l2, h2);
230 for (i = 0; i < MAX_SHORTS; i++)
232 carry += arg1[i] + arg2[i];
233 arg1[i] = carry & 0xff;
237 decode (arg1, lv, hv);
238 return overflow_sum_sign (h1, h2, *hv);
241 /* Negate a doubleword integer with doubleword result.
242 Return nonzero if the operation overflows, assuming it's signed.
243 The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
244 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.
245 We use the 8-shorts representation internally. */
248 neg_double (l1, h1, lv, hv)
249 HOST_WIDE_INT l1, h1;
250 HOST_WIDE_INT *lv, *hv;
256 return (*hv & h1) < 0;
266 /* Multiply two doubleword integers with doubleword result.
267 Return nonzero if the operation overflows, assuming it's signed.
268 Each argument is given as two `HOST_WIDE_INT' pieces.
269 One argument is L1 and H1; the other, L2 and H2.
270 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.
271 We use the 8-shorts representation internally. */
274 mul_double (l1, h1, l2, h2, lv, hv)
275 HOST_WIDE_INT l1, h1, l2, h2;
276 HOST_WIDE_INT *lv, *hv;
278 short arg1[MAX_SHORTS];
279 short arg2[MAX_SHORTS];
280 short prod[MAX_SHORTS * 2];
281 register int carry = 0;
282 register int i, j, k;
283 HOST_WIDE_INT toplow, tophigh, neglow, neghigh;
285 /* These cases are used extensively, arising from pointer combinations. */
290 int overflow = left_shift_overflows (h1, 1);
291 unsigned HOST_WIDE_INT temp = l1 + l1;
292 *hv = (h1 << 1) + (temp < l1);
298 int overflow = left_shift_overflows (h1, 2);
299 unsigned HOST_WIDE_INT temp = l1 + l1;
300 h1 = (h1 << 2) + ((temp < l1) << 1);
310 int overflow = left_shift_overflows (h1, 3);
311 unsigned HOST_WIDE_INT temp = l1 + l1;
312 h1 = (h1 << 3) + ((temp < l1) << 2);
315 h1 += (temp < l1) << 1;
325 encode (arg1, l1, h1);
326 encode (arg2, l2, h2);
328 bzero (prod, sizeof prod);
330 for (i = 0; i < MAX_SHORTS; i++)
331 for (j = 0; j < MAX_SHORTS; j++)
334 carry = arg1[i] * arg2[j];
338 prod[k] = carry & 0xff;
344 decode (prod, lv, hv); /* This ignores
345 prod[MAX_SHORTS] -> prod[MAX_SHORTS*2-1] */
347 /* Check for overflow by calculating the top half of the answer in full;
348 it should agree with the low half's sign bit. */
349 decode (prod+MAX_SHORTS, &toplow, &tophigh);
352 neg_double (l2, h2, &neglow, &neghigh);
353 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
357 neg_double (l1, h1, &neglow, &neghigh);
358 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
360 return (*hv < 0 ? ~(toplow & tophigh) : toplow | tophigh) != 0;
363 /* Shift the doubleword integer in L1, H1 left by COUNT places
364 keeping only PREC bits of result.
365 Shift right if COUNT is negative.
366 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
367 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
370 lshift_double (l1, h1, count, prec, lv, hv, arith)
371 HOST_WIDE_INT l1, h1, count;
373 HOST_WIDE_INT *lv, *hv;
376 short arg1[MAX_SHORTS];
382 rshift_double (l1, h1, - count, prec, lv, hv, arith);
386 encode (arg1, l1, h1);
394 for (i = 0; i < MAX_SHORTS; i++)
396 carry += arg1[i] << 1;
397 arg1[i] = carry & 0xff;
403 decode (arg1, lv, hv);
406 /* Shift the doubleword integer in L1, H1 right by COUNT places
407 keeping only PREC bits of result. COUNT must be positive.
408 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
409 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
412 rshift_double (l1, h1, count, prec, lv, hv, arith)
413 HOST_WIDE_INT l1, h1, count;
415 HOST_WIDE_INT *lv, *hv;
418 short arg1[MAX_SHORTS];
422 encode (arg1, l1, h1);
429 carry = arith && arg1[7] >> 7;
430 for (i = MAX_SHORTS - 1; i >= 0; i--)
434 arg1[i] = (carry >> 1) & 0xff;
439 decode (arg1, lv, hv);
442 /* Rotate the doubldword integer in L1, H1 left by COUNT places
443 keeping only PREC bits of result.
444 Rotate right if COUNT is negative.
445 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
448 lrotate_double (l1, h1, count, prec, lv, hv)
449 HOST_WIDE_INT l1, h1, count;
451 HOST_WIDE_INT *lv, *hv;
453 short arg1[MAX_SHORTS];
459 rrotate_double (l1, h1, - count, prec, lv, hv);
463 encode (arg1, l1, h1);
468 carry = arg1[MAX_SHORTS - 1] >> 7;
471 for (i = 0; i < MAX_SHORTS; i++)
473 carry += arg1[i] << 1;
474 arg1[i] = carry & 0xff;
480 decode (arg1, lv, hv);
483 /* Rotate the doubleword integer in L1, H1 left by COUNT places
484 keeping only PREC bits of result. COUNT must be positive.
485 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
488 rrotate_double (l1, h1, count, prec, lv, hv)
489 HOST_WIDE_INT l1, h1, count;
491 HOST_WIDE_INT *lv, *hv;
493 short arg1[MAX_SHORTS];
497 encode (arg1, l1, h1);
505 for (i = MAX_SHORTS - 1; i >= 0; i--)
509 arg1[i] = (carry >> 1) & 0xff;
514 decode (arg1, lv, hv);
517 /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
518 for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
519 CODE is a tree code for a kind of division, one of
520 TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
522 It controls how the quotient is rounded to a integer.
523 Return nonzero if the operation overflows.
524 UNS nonzero says do unsigned division. */
527 div_and_round_double (code, uns,
528 lnum_orig, hnum_orig, lden_orig, hden_orig,
529 lquo, hquo, lrem, hrem)
532 HOST_WIDE_INT lnum_orig, hnum_orig; /* num == numerator == dividend */
533 HOST_WIDE_INT lden_orig, hden_orig; /* den == denominator == divisor */
534 HOST_WIDE_INT *lquo, *hquo, *lrem, *hrem;
537 short num[MAX_SHORTS + 1]; /* extra element for scaling. */
538 short den[MAX_SHORTS], quo[MAX_SHORTS];
539 register int i, j, work;
540 register int carry = 0;
541 HOST_WIDE_INT lnum = lnum_orig;
542 HOST_WIDE_INT hnum = hnum_orig;
543 HOST_WIDE_INT lden = lden_orig;
544 HOST_WIDE_INT hden = hden_orig;
547 if ((hden == 0) && (lden == 0))
550 /* calculate quotient sign and convert operands to unsigned. */
556 /* (minimum integer) / (-1) is the only overflow case. */
557 if (neg_double (lnum, hnum, &lnum, &hnum) && (lden & hden) == -1)
563 neg_double (lden, hden, &lden, &hden);
567 if (hnum == 0 && hden == 0)
568 { /* single precision */
570 /* This unsigned division rounds toward zero. */
571 *lquo = lnum / (unsigned HOST_WIDE_INT) lden;
576 { /* trivial case: dividend < divisor */
577 /* hden != 0 already checked. */
584 bzero (quo, sizeof quo);
586 bzero (num, sizeof num); /* to zero 9th element */
587 bzero (den, sizeof den);
589 encode (num, lnum, hnum);
590 encode (den, lden, hden);
592 /* This code requires more than just hden == 0.
593 We also have to require that we don't need more than three bytes
594 to hold CARRY. If we ever did need four bytes to hold it, we
595 would lose part of it when computing WORK on the next round. */
596 if (hden == 0 && (((unsigned HOST_WIDE_INT) lden << 8) >> 8) == lden)
597 { /* simpler algorithm */
598 /* hnum != 0 already checked. */
599 for (i = MAX_SHORTS - 1; i >= 0; i--)
601 work = num[i] + (carry << 8);
602 quo[i] = work / (unsigned HOST_WIDE_INT) lden;
603 carry = work % (unsigned HOST_WIDE_INT) lden;
606 else { /* full double precision,
607 with thanks to Don Knuth's
608 "Seminumerical Algorithms". */
610 int quo_est, scale, num_hi_sig, den_hi_sig, quo_hi_sig;
612 /* Find the highest non-zero divisor digit. */
613 for (i = MAX_SHORTS - 1; ; i--)
618 for (i = MAX_SHORTS - 1; ; i--)
623 quo_hi_sig = num_hi_sig - den_hi_sig + 1;
625 /* Insure that the first digit of the divisor is at least BASE/2.
626 This is required by the quotient digit estimation algorithm. */
628 scale = BASE / (den[den_hi_sig] + 1);
629 if (scale > 1) { /* scale divisor and dividend */
631 for (i = 0; i <= MAX_SHORTS - 1; i++) {
632 work = (num[i] * scale) + carry;
633 num[i] = work & 0xff;
635 if (num[i] != 0) num_hi_sig = i;
638 for (i = 0; i <= MAX_SHORTS - 1; i++) {
639 work = (den[i] * scale) + carry;
640 den[i] = work & 0xff;
642 if (den[i] != 0) den_hi_sig = i;
647 for (i = quo_hi_sig; i > 0; i--) {
648 /* guess the next quotient digit, quo_est, by dividing the first
649 two remaining dividend digits by the high order quotient digit.
650 quo_est is never low and is at most 2 high. */
652 int num_hi; /* index of highest remaining dividend digit */
654 num_hi = i + den_hi_sig;
656 work = (num[num_hi] * BASE) + (num_hi > 0 ? num[num_hi - 1] : 0);
657 if (num[num_hi] != den[den_hi_sig]) {
658 quo_est = work / den[den_hi_sig];
664 /* refine quo_est so it's usually correct, and at most one high. */
665 while ((den[den_hi_sig - 1] * quo_est)
666 > (((work - (quo_est * den[den_hi_sig])) * BASE)
667 + ((num_hi - 1) > 0 ? num[num_hi - 2] : 0)))
670 /* Try QUO_EST as the quotient digit, by multiplying the
671 divisor by QUO_EST and subtracting from the remaining dividend.
672 Keep in mind that QUO_EST is the I - 1st digit. */
676 for (j = 0; j <= den_hi_sig; j++)
680 work = num[i + j - 1] - (quo_est * den[j]) + carry;
688 num[i + j - 1] = digit;
691 /* if quo_est was high by one, then num[i] went negative and
692 we need to correct things. */
697 carry = 0; /* add divisor back in */
698 for (j = 0; j <= den_hi_sig; j++)
700 work = num[i + j - 1] + den[j] + carry;
710 num[i + j - 1] = work;
712 num [num_hi] += carry;
715 /* store the quotient digit. */
716 quo[i - 1] = quo_est;
720 decode (quo, lquo, hquo);
723 /* if result is negative, make it so. */
725 neg_double (*lquo, *hquo, lquo, hquo);
727 /* compute trial remainder: rem = num - (quo * den) */
728 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
729 neg_double (*lrem, *hrem, lrem, hrem);
730 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
735 case TRUNC_MOD_EXPR: /* round toward zero */
736 case EXACT_DIV_EXPR: /* for this one, it shouldn't matter */
740 case FLOOR_MOD_EXPR: /* round toward negative infinity */
741 if (quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio < 0 && rem != 0 */
744 add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1,
747 else return overflow;
751 case CEIL_MOD_EXPR: /* round toward positive infinity */
752 if (!quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio > 0 && rem != 0 */
754 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
757 else return overflow;
761 case ROUND_MOD_EXPR: /* round to closest integer */
763 HOST_WIDE_INT labs_rem = *lrem, habs_rem = *hrem;
764 HOST_WIDE_INT labs_den = lden, habs_den = hden, ltwice, htwice;
766 /* get absolute values */
767 if (*hrem < 0) neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
768 if (hden < 0) neg_double (lden, hden, &labs_den, &habs_den);
770 /* if (2 * abs (lrem) >= abs (lden)) */
771 mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0,
772 labs_rem, habs_rem, <wice, &htwice);
773 if (((unsigned HOST_WIDE_INT) habs_den
774 < (unsigned HOST_WIDE_INT) htwice)
775 || (((unsigned HOST_WIDE_INT) habs_den
776 == (unsigned HOST_WIDE_INT) htwice)
777 && ((HOST_WIDE_INT unsigned) labs_den
778 < (unsigned HOST_WIDE_INT) ltwice)))
782 add_double (*lquo, *hquo,
783 (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
786 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
789 else return overflow;
797 /* compute true remainder: rem = num - (quo * den) */
798 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
799 neg_double (*lrem, *hrem, lrem, hrem);
800 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
804 #ifndef REAL_ARITHMETIC
805 /* Effectively truncate a real value to represent
806 the nearest possible value in a narrower mode.
807 The result is actually represented in the same data type as the argument,
808 but its value is usually different. */
811 real_value_truncate (mode, arg)
812 enum machine_mode mode;
816 /* Make sure the value is actually stored in memory before we turn off
820 REAL_VALUE_TYPE value;
821 jmp_buf handler, old_handler;
824 if (setjmp (handler))
826 error ("floating overflow");
829 handled = push_float_handler (handler, old_handler);
830 value = REAL_VALUE_TRUNCATE (mode, arg);
831 pop_float_handler (handled, old_handler);
835 #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
837 /* Check for infinity in an IEEE double precision number. */
843 /* The IEEE 64-bit double format. */
848 unsigned exponent : 11;
849 unsigned mantissa1 : 20;
854 unsigned mantissa1 : 20;
855 unsigned exponent : 11;
861 if (u.big_endian.sign == 1)
864 return (u.big_endian.exponent == 2047
865 && u.big_endian.mantissa1 == 0
866 && u.big_endian.mantissa2 == 0);
871 return (u.little_endian.exponent == 2047
872 && u.little_endian.mantissa1 == 0
873 && u.little_endian.mantissa2 == 0);
877 /* Check whether an IEEE double precision number is a NaN. */
883 /* The IEEE 64-bit double format. */
888 unsigned exponent : 11;
889 unsigned mantissa1 : 20;
894 unsigned mantissa1 : 20;
895 unsigned exponent : 11;
901 if (u.big_endian.sign == 1)
904 return (u.big_endian.exponent == 2047
905 && (u.big_endian.mantissa1 != 0
906 || u.big_endian.mantissa2 != 0));
911 return (u.little_endian.exponent == 2047
912 && (u.little_endian.mantissa1 != 0
913 || u.little_endian.mantissa2 != 0));
917 /* Check for a negative IEEE double precision number. */
923 /* The IEEE 64-bit double format. */
928 unsigned exponent : 11;
929 unsigned mantissa1 : 20;
934 unsigned mantissa1 : 20;
935 unsigned exponent : 11;
941 if (u.big_endian.sign == 1)
944 return u.big_endian.sign;
949 return u.little_endian.sign;
952 #else /* Target not IEEE */
954 /* Let's assume other float formats don't have infinity.
955 (This can be overridden by redefining REAL_VALUE_ISINF.) */
963 /* Let's assume other float formats don't have NaNs.
964 (This can be overridden by redefining REAL_VALUE_ISNAN.) */
972 /* Let's assume other float formats don't have minus zero.
973 (This can be overridden by redefining REAL_VALUE_NEGATIVE.) */
980 #endif /* Target not IEEE */
981 #endif /* no REAL_ARITHMETIC */
983 /* Split a tree IN into a constant and a variable part
984 that could be combined with CODE to make IN.
985 CODE must be a commutative arithmetic operation.
986 Store the constant part into *CONP and the variable in &VARP.
987 Return 1 if this was done; zero means the tree IN did not decompose
990 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR.
991 Therefore, we must tell the caller whether the variable part
992 was subtracted. We do this by storing 1 or -1 into *VARSIGNP.
993 The value stored is the coefficient for the variable term.
994 The constant term we return should always be added;
995 we negate it if necessary. */
998 split_tree (in, code, varp, conp, varsignp)
1000 enum tree_code code;
1004 register tree outtype = TREE_TYPE (in);
1008 /* Strip any conversions that don't change the machine mode. */
1009 while ((TREE_CODE (in) == NOP_EXPR
1010 || TREE_CODE (in) == CONVERT_EXPR)
1011 && (TYPE_MODE (TREE_TYPE (in))
1012 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (in, 0)))))
1013 in = TREE_OPERAND (in, 0);
1015 if (TREE_CODE (in) == code
1016 || (! FLOAT_TYPE_P (TREE_TYPE (in))
1017 /* We can associate addition and subtraction together
1018 (even though the C standard doesn't say so)
1019 for integers because the value is not affected.
1020 For reals, the value might be affected, so we can't. */
1021 && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
1022 || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
1024 enum tree_code code = TREE_CODE (TREE_OPERAND (in, 0));
1025 if (code == INTEGER_CST)
1027 *conp = TREE_OPERAND (in, 0);
1028 *varp = TREE_OPERAND (in, 1);
1029 if (TYPE_MODE (TREE_TYPE (*varp)) != TYPE_MODE (outtype)
1030 && TREE_TYPE (*varp) != outtype)
1031 *varp = convert (outtype, *varp);
1032 *varsignp = (TREE_CODE (in) == MINUS_EXPR) ? -1 : 1;
1035 if (TREE_CONSTANT (TREE_OPERAND (in, 1)))
1037 *conp = TREE_OPERAND (in, 1);
1038 *varp = TREE_OPERAND (in, 0);
1040 if (TYPE_MODE (TREE_TYPE (*varp)) != TYPE_MODE (outtype)
1041 && TREE_TYPE (*varp) != outtype)
1042 *varp = convert (outtype, *varp);
1043 if (TREE_CODE (in) == MINUS_EXPR)
1045 /* If operation is subtraction and constant is second,
1046 must negate it to get an additive constant.
1047 And this cannot be done unless it is a manifest constant.
1048 It could also be the address of a static variable.
1049 We cannot negate that, so give up. */
1050 if (TREE_CODE (*conp) == INTEGER_CST)
1051 /* Subtracting from integer_zero_node loses for long long. */
1052 *conp = fold (build1 (NEGATE_EXPR, TREE_TYPE (*conp), *conp));
1058 if (TREE_CONSTANT (TREE_OPERAND (in, 0)))
1060 *conp = TREE_OPERAND (in, 0);
1061 *varp = TREE_OPERAND (in, 1);
1062 if (TYPE_MODE (TREE_TYPE (*varp)) != TYPE_MODE (outtype)
1063 && TREE_TYPE (*varp) != outtype)
1064 *varp = convert (outtype, *varp);
1065 *varsignp = (TREE_CODE (in) == MINUS_EXPR) ? -1 : 1;
1072 /* Combine two constants NUM and ARG2 under operation CODE
1073 to produce a new constant.
1074 We assume ARG1 and ARG2 have the same data type,
1075 or at least are the same kind of constant and the same machine mode.
1077 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
1080 const_binop (code, arg1, arg2, notrunc)
1081 enum tree_code code;
1082 register tree arg1, arg2;
1085 if (TREE_CODE (arg1) == INTEGER_CST)
1087 register HOST_WIDE_INT int1l = TREE_INT_CST_LOW (arg1);
1088 register HOST_WIDE_INT int1h = TREE_INT_CST_HIGH (arg1);
1089 HOST_WIDE_INT int2l = TREE_INT_CST_LOW (arg2);
1090 HOST_WIDE_INT int2h = TREE_INT_CST_HIGH (arg2);
1091 HOST_WIDE_INT low, hi;
1092 HOST_WIDE_INT garbagel, garbageh;
1094 int uns = TREE_UNSIGNED (TREE_TYPE (arg1));
1100 t = build_int_2 (int1l | int2l, int1h | int2h);
1104 t = build_int_2 (int1l ^ int2l, int1h ^ int2h);
1108 t = build_int_2 (int1l & int2l, int1h & int2h);
1111 case BIT_ANDTC_EXPR:
1112 t = build_int_2 (int1l & ~int2l, int1h & ~int2h);
1118 /* It's unclear from the C standard whether shifts can overflow.
1119 The following code ignores overflow; perhaps a C standard
1120 interpretation ruling is needed. */
1121 lshift_double (int1l, int1h, int2l,
1122 TYPE_PRECISION (TREE_TYPE (arg1)),
1125 t = build_int_2 (low, hi);
1126 TREE_TYPE (t) = TREE_TYPE (arg1);
1128 force_fit_type (t, 0);
1129 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1130 TREE_CONSTANT_OVERFLOW (t)
1131 = TREE_CONSTANT_OVERFLOW (arg1) | TREE_CONSTANT_OVERFLOW (arg2);
1137 lrotate_double (int1l, int1h, int2l,
1138 TYPE_PRECISION (TREE_TYPE (arg1)),
1140 t = build_int_2 (low, hi);
1147 if ((unsigned HOST_WIDE_INT) int2l < int1l)
1150 overflow = int2h < hi;
1152 t = build_int_2 (int2l, int2h);
1158 if ((unsigned HOST_WIDE_INT) int1l < int2l)
1161 overflow = int1h < hi;
1163 t = build_int_2 (int1l, int1h);
1166 overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
1167 t = build_int_2 (low, hi);
1171 if (int2h == 0 && int2l == 0)
1173 t = build_int_2 (int1l, int1h);
1176 neg_double (int2l, int2h, &low, &hi);
1177 add_double (int1l, int1h, low, hi, &low, &hi);
1178 overflow = overflow_sum_sign (hi, int2h, int1h);
1179 t = build_int_2 (low, hi);
1183 /* Optimize simple cases. */
1186 unsigned HOST_WIDE_INT temp;
1191 t = build_int_2 (0, 0);
1194 t = build_int_2 (int2l, int2h);
1197 overflow = left_shift_overflows (int2h, 1);
1198 temp = int2l + int2l;
1199 int2h = (int2h << 1) + (temp < int2l);
1200 t = build_int_2 (temp, int2h);
1202 #if 0 /* This code can lose carries. */
1204 temp = int2l + int2l + int2l;
1205 int2h = int2h * 3 + (temp < int2l);
1206 t = build_int_2 (temp, int2h);
1210 overflow = left_shift_overflows (int2h, 2);
1211 temp = int2l + int2l;
1212 int2h = (int2h << 2) + ((temp < int2l) << 1);
1215 int2h += (temp < int2l);
1216 t = build_int_2 (temp, int2h);
1219 overflow = left_shift_overflows (int2h, 3);
1220 temp = int2l + int2l;
1221 int2h = (int2h << 3) + ((temp < int2l) << 2);
1224 int2h += (temp < int2l) << 1;
1227 int2h += (temp < int2l);
1228 t = build_int_2 (temp, int2h);
1239 t = build_int_2 (0, 0);
1244 t = build_int_2 (int1l, int1h);
1249 overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
1250 t = build_int_2 (low, hi);
1253 case TRUNC_DIV_EXPR:
1254 case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1255 case EXACT_DIV_EXPR:
1256 /* This is a shortcut for a common special case.
1257 It reduces the number of tree nodes generated
1259 if (int2h == 0 && int2l > 0
1260 && TREE_TYPE (arg1) == sizetype
1261 && int1h == 0 && int1l >= 0)
1263 if (code == CEIL_DIV_EXPR)
1265 return size_int (int1l / int2l);
1267 case ROUND_DIV_EXPR:
1268 if (int2h == 0 && int2l == 1)
1270 t = build_int_2 (int1l, int1h);
1273 if (int1l == int2l && int1h == int2h)
1275 if ((int1l | int1h) == 0)
1277 t = build_int_2 (1, 0);
1280 overflow = div_and_round_double (code, uns,
1281 int1l, int1h, int2l, int2h,
1282 &low, &hi, &garbagel, &garbageh);
1283 t = build_int_2 (low, hi);
1286 case TRUNC_MOD_EXPR: case ROUND_MOD_EXPR:
1287 case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1288 overflow = div_and_round_double (code, uns,
1289 int1l, int1h, int2l, int2h,
1290 &garbagel, &garbageh, &low, &hi);
1291 t = build_int_2 (low, hi);
1298 low = (((unsigned HOST_WIDE_INT) int1h
1299 < (unsigned HOST_WIDE_INT) int2h)
1300 || (((unsigned HOST_WIDE_INT) int1h
1301 == (unsigned HOST_WIDE_INT) int2h)
1302 && ((unsigned HOST_WIDE_INT) int1l
1303 < (unsigned HOST_WIDE_INT) int2l)));
1307 low = ((int1h < int2h)
1308 || ((int1h == int2h)
1309 && ((unsigned HOST_WIDE_INT) int1l
1310 < (unsigned HOST_WIDE_INT) int2l)));
1312 if (low == (code == MIN_EXPR))
1313 t = build_int_2 (int1l, int1h);
1315 t = build_int_2 (int2l, int2h);
1322 TREE_TYPE (t) = TREE_TYPE (arg1);
1324 = ((notrunc ? !uns && overflow : force_fit_type (t, overflow))
1325 | TREE_OVERFLOW (arg1)
1326 | TREE_OVERFLOW (arg2));
1327 TREE_CONSTANT_OVERFLOW (t) = (TREE_OVERFLOW (t)
1328 | TREE_CONSTANT_OVERFLOW (arg1)
1329 | TREE_CONSTANT_OVERFLOW (arg2));
1332 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1333 if (TREE_CODE (arg1) == REAL_CST)
1337 REAL_VALUE_TYPE value;
1340 d1 = TREE_REAL_CST (arg1);
1341 d2 = TREE_REAL_CST (arg2);
1342 if (setjmp (float_error))
1344 pedwarn ("floating overflow in constant expression");
1345 return build (code, TREE_TYPE (arg1), arg1, arg2);
1347 set_float_handler (float_error);
1349 #ifdef REAL_ARITHMETIC
1350 REAL_ARITHMETIC (value, code, d1, d2);
1367 #ifndef REAL_INFINITY
1376 value = MIN (d1, d2);
1380 value = MAX (d1, d2);
1386 #endif /* no REAL_ARITHMETIC */
1387 t = build_real (TREE_TYPE (arg1),
1388 real_value_truncate (TYPE_MODE (TREE_TYPE (arg1)), value));
1389 set_float_handler (NULL_PTR);
1392 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
1393 if (TREE_CODE (arg1) == COMPLEX_CST)
1395 register tree r1 = TREE_REALPART (arg1);
1396 register tree i1 = TREE_IMAGPART (arg1);
1397 register tree r2 = TREE_REALPART (arg2);
1398 register tree i2 = TREE_IMAGPART (arg2);
1404 t = build_complex (const_binop (PLUS_EXPR, r1, r2, notrunc),
1405 const_binop (PLUS_EXPR, i1, i2, notrunc));
1409 t = build_complex (const_binop (MINUS_EXPR, r1, r2, notrunc),
1410 const_binop (MINUS_EXPR, i1, i2, notrunc));
1414 t = build_complex (const_binop (MINUS_EXPR,
1415 const_binop (MULT_EXPR,
1417 const_binop (MULT_EXPR,
1420 const_binop (PLUS_EXPR,
1421 const_binop (MULT_EXPR,
1423 const_binop (MULT_EXPR,
1430 register tree magsquared
1431 = const_binop (PLUS_EXPR,
1432 const_binop (MULT_EXPR, r2, r2, notrunc),
1433 const_binop (MULT_EXPR, i2, i2, notrunc),
1435 t = build_complex (const_binop (RDIV_EXPR,
1436 const_binop (PLUS_EXPR,
1437 const_binop (MULT_EXPR, r1, r2, notrunc),
1438 const_binop (MULT_EXPR, i1, i2, notrunc),
1440 magsquared, notrunc),
1441 const_binop (RDIV_EXPR,
1442 const_binop (MINUS_EXPR,
1443 const_binop (MULT_EXPR, i1, r2, notrunc),
1444 const_binop (MULT_EXPR, r1, i2, notrunc),
1446 magsquared, notrunc));
1453 TREE_TYPE (t) = TREE_TYPE (arg1);
1459 /* Return an INTEGER_CST with value V and type from `sizetype'. */
1463 unsigned int number;
1466 /* Type-size nodes already made for small sizes. */
1467 static tree size_table[2*HOST_BITS_PER_WIDE_INT + 1];
1469 if (number < 2*HOST_BITS_PER_WIDE_INT + 1
1470 && size_table[number] != 0)
1471 return size_table[number];
1472 if (number < 2*HOST_BITS_PER_WIDE_INT + 1)
1474 push_obstacks_nochange ();
1475 /* Make this a permanent node. */
1476 end_temporary_allocation ();
1477 t = build_int_2 (number, 0);
1478 TREE_TYPE (t) = sizetype;
1479 size_table[number] = t;
1484 t = build_int_2 (number, 0);
1485 TREE_TYPE (t) = sizetype;
1490 /* Combine operands OP1 and OP2 with arithmetic operation CODE.
1491 CODE is a tree code. Data type is taken from `sizetype',
1492 If the operands are constant, so is the result. */
1495 size_binop (code, arg0, arg1)
1496 enum tree_code code;
1499 /* Handle the special case of two integer constants faster. */
1500 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1502 /* And some specific cases even faster than that. */
1503 if (code == PLUS_EXPR
1504 && TREE_INT_CST_LOW (arg0) == 0
1505 && TREE_INT_CST_HIGH (arg0) == 0)
1507 if (code == MINUS_EXPR
1508 && TREE_INT_CST_LOW (arg1) == 0
1509 && TREE_INT_CST_HIGH (arg1) == 0)
1511 if (code == MULT_EXPR
1512 && TREE_INT_CST_LOW (arg0) == 1
1513 && TREE_INT_CST_HIGH (arg0) == 0)
1515 /* Handle general case of two integer constants. */
1516 return const_binop (code, arg0, arg1, 1);
1519 if (arg0 == error_mark_node || arg1 == error_mark_node)
1520 return error_mark_node;
1522 return fold (build (code, sizetype, arg0, arg1));
1525 /* Given T, a tree representing type conversion of ARG1, a constant,
1526 return a constant tree representing the result of conversion. */
1529 fold_convert (t, arg1)
1533 register tree type = TREE_TYPE (t);
1535 if (TREE_CODE (type) == POINTER_TYPE || INTEGRAL_TYPE_P (type))
1537 if (TREE_CODE (arg1) == INTEGER_CST)
1539 /* Given an integer constant, make new constant with new type,
1540 appropriately sign-extended or truncated. */
1541 t = build_int_2 (TREE_INT_CST_LOW (arg1),
1542 TREE_INT_CST_HIGH (arg1));
1543 TREE_TYPE (t) = type;
1544 /* Indicate an overflow if (1) ARG1 already overflowed,
1545 or (2) force_fit_type indicates an overflow.
1546 Tell force_fit_type that an overflow has already occurred
1547 if ARG1 is a too-large unsigned value and T is signed. */
1549 = (TREE_OVERFLOW (arg1)
1550 | force_fit_type (t,
1551 (TREE_INT_CST_HIGH (arg1) < 0
1552 & (TREE_UNSIGNED (type)
1553 < TREE_UNSIGNED (TREE_TYPE (arg1))))));
1554 TREE_CONSTANT_OVERFLOW (t)
1555 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
1557 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1558 else if (TREE_CODE (arg1) == REAL_CST)
1560 REAL_VALUE_TYPE l, x, u;
1562 l = real_value_from_int_cst (TYPE_MIN_VALUE (type));
1563 x = TREE_REAL_CST (arg1);
1564 u = real_value_from_int_cst (TYPE_MAX_VALUE (type));
1566 /* See if X will be in range after truncation towards 0.
1567 To compensate for truncation, move the bounds away from 0,
1568 but reject if X exactly equals the adjusted bounds. */
1569 #ifdef REAL_ARITHMETIC
1570 REAL_ARITHMETIC (l, MINUS_EXPR, l, dconst1);
1571 REAL_ARITHMETIC (u, PLUS_EXPR, u, dconst1);
1576 if (! (REAL_VALUES_LESS (l, x) && REAL_VALUES_LESS (x, u)))
1578 pedwarn ("real constant out of range for integer conversion");
1581 #ifndef REAL_ARITHMETIC
1584 HOST_WIDE_INT low, high;
1585 HOST_WIDE_INT half_word
1586 = (HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2);
1588 d = TREE_REAL_CST (arg1);
1592 high = (HOST_WIDE_INT) (d / half_word / half_word);
1593 d -= (REAL_VALUE_TYPE) high * half_word * half_word;
1594 if (d >= (REAL_VALUE_TYPE) half_word * half_word / 2)
1596 low = d - (REAL_VALUE_TYPE) half_word * half_word / 2;
1597 low |= (HOST_WIDE_INT) -1 << (HOST_BITS_PER_WIDE_INT - 1);
1600 low = (HOST_WIDE_INT) d;
1601 if (TREE_REAL_CST (arg1) < 0)
1602 neg_double (low, high, &low, &high);
1603 t = build_int_2 (low, high);
1607 HOST_WIDE_INT low, high;
1608 REAL_VALUE_TO_INT (&low, &high, (TREE_REAL_CST (arg1)));
1609 t = build_int_2 (low, high);
1612 TREE_TYPE (t) = type;
1613 force_fit_type (t, 0);
1615 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
1616 TREE_TYPE (t) = type;
1618 else if (TREE_CODE (type) == REAL_TYPE)
1620 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1621 if (TREE_CODE (arg1) == INTEGER_CST)
1622 return build_real_from_int_cst (type, arg1);
1623 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
1624 if (TREE_CODE (arg1) == REAL_CST)
1626 if (setjmp (float_error))
1628 pedwarn ("floating overflow in constant expression");
1631 set_float_handler (float_error);
1633 t = build_real (type, real_value_truncate (TYPE_MODE (type),
1634 TREE_REAL_CST (arg1)));
1635 set_float_handler (NULL_PTR);
1639 TREE_CONSTANT (t) = 1;
1643 /* Return an expr equal to X but certainly not valid as an lvalue.
1644 Also make sure it is not valid as an null pointer constant. */
1652 /* These things are certainly not lvalues. */
1653 if (TREE_CODE (x) == NON_LVALUE_EXPR
1654 || TREE_CODE (x) == INTEGER_CST
1655 || TREE_CODE (x) == REAL_CST
1656 || TREE_CODE (x) == STRING_CST
1657 || TREE_CODE (x) == ADDR_EXPR)
1659 if (TREE_CODE (x) == INTEGER_CST && integer_zerop (x))
1661 /* Use NOP_EXPR instead of NON_LVALUE_EXPR
1662 so convert_for_assignment won't strip it.
1663 This is so this 0 won't be treated as a null pointer constant. */
1664 result = build1 (NOP_EXPR, TREE_TYPE (x), x);
1665 TREE_CONSTANT (result) = TREE_CONSTANT (x);
1671 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
1672 TREE_CONSTANT (result) = TREE_CONSTANT (x);
1676 /* When pedantic, return an expr equal to X but certainly not valid as a
1677 pedantic lvalue. Otherwise, return X. */
1680 pedantic_non_lvalue (x)
1684 return non_lvalue (x);
1689 /* Given a tree comparison code, return the code that is the logical inverse
1690 of the given code. It is not safe to do this for floating-point
1691 comparisons, except for NE_EXPR and EQ_EXPR. */
1693 static enum tree_code
1694 invert_tree_comparison (code)
1695 enum tree_code code;
1716 /* Similar, but return the comparison that results if the operands are
1717 swapped. This is safe for floating-point. */
1719 static enum tree_code
1720 swap_tree_comparison (code)
1721 enum tree_code code;
1741 /* Return nonzero if two operands are necessarily equal.
1742 If ONLY_CONST is non-zero, only return non-zero for constants.
1743 This function tests whether the operands are indistinguishable;
1744 it does not test whether they are equal using C's == operation.
1745 The distinction is important for IEEE floating point, because
1746 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
1747 (2) two NaNs may be indistinguishable, but NaN!=NaN. */
1750 operand_equal_p (arg0, arg1, only_const)
1754 /* If both types don't have the same signedness, then we can't consider
1755 them equal. We must check this before the STRIP_NOPS calls
1756 because they may change the signedness of the arguments. */
1757 if (TREE_UNSIGNED (TREE_TYPE (arg0)) != TREE_UNSIGNED (TREE_TYPE (arg1)))
1763 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
1764 We don't care about side effects in that case because the SAVE_EXPR
1765 takes care of that for us. */
1766 if (TREE_CODE (arg0) == SAVE_EXPR && arg0 == arg1)
1767 return ! only_const;
1769 if (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1))
1772 if (TREE_CODE (arg0) == TREE_CODE (arg1)
1773 && TREE_CODE (arg0) == ADDR_EXPR
1774 && TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0))
1777 if (TREE_CODE (arg0) == TREE_CODE (arg1)
1778 && TREE_CODE (arg0) == INTEGER_CST
1779 && TREE_INT_CST_LOW (arg0) == TREE_INT_CST_LOW (arg1)
1780 && TREE_INT_CST_HIGH (arg0) == TREE_INT_CST_HIGH (arg1))
1783 /* Detect when real constants are equal. */
1784 if (TREE_CODE (arg0) == TREE_CODE (arg1)
1785 && TREE_CODE (arg0) == REAL_CST)
1786 return !bcmp (&TREE_REAL_CST (arg0), &TREE_REAL_CST (arg1),
1787 sizeof (REAL_VALUE_TYPE));
1795 if (TREE_CODE (arg0) != TREE_CODE (arg1))
1797 /* This is needed for conversions and for COMPONENT_REF.
1798 Might as well play it safe and always test this. */
1799 if (TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
1802 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
1805 /* Two conversions are equal only if signedness and modes match. */
1806 if ((TREE_CODE (arg0) == NOP_EXPR || TREE_CODE (arg0) == CONVERT_EXPR)
1807 && (TREE_UNSIGNED (TREE_TYPE (arg0))
1808 != TREE_UNSIGNED (TREE_TYPE (arg1))))
1811 return operand_equal_p (TREE_OPERAND (arg0, 0),
1812 TREE_OPERAND (arg1, 0), 0);
1816 return (operand_equal_p (TREE_OPERAND (arg0, 0),
1817 TREE_OPERAND (arg1, 0), 0)
1818 && operand_equal_p (TREE_OPERAND (arg0, 1),
1819 TREE_OPERAND (arg1, 1), 0));
1822 switch (TREE_CODE (arg0))
1825 return operand_equal_p (TREE_OPERAND (arg0, 0),
1826 TREE_OPERAND (arg1, 0), 0);
1830 return (operand_equal_p (TREE_OPERAND (arg0, 0),
1831 TREE_OPERAND (arg1, 0), 0)
1832 && operand_equal_p (TREE_OPERAND (arg0, 1),
1833 TREE_OPERAND (arg1, 1), 0));
1836 return (operand_equal_p (TREE_OPERAND (arg0, 0),
1837 TREE_OPERAND (arg1, 0), 0)
1838 && operand_equal_p (TREE_OPERAND (arg0, 1),
1839 TREE_OPERAND (arg1, 1), 0)
1840 && operand_equal_p (TREE_OPERAND (arg0, 2),
1841 TREE_OPERAND (arg1, 2), 0));
1849 /* Similar to operand_equal_p, but see if ARG0 might have been made by
1850 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
1852 When in doubt, return 0. */
1855 operand_equal_for_comparison_p (arg0, arg1, other)
1859 int unsignedp1, unsignedpo;
1860 tree primarg1, primother;
1863 if (operand_equal_p (arg0, arg1, 0))
1866 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
1869 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
1870 actual comparison operand, ARG0.
1872 First throw away any conversions to wider types
1873 already present in the operands. */
1875 primarg1 = get_narrower (arg1, &unsignedp1);
1876 primother = get_narrower (other, &unsignedpo);
1878 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
1879 if (unsignedp1 == unsignedpo
1880 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
1881 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
1883 tree type = TREE_TYPE (arg0);
1885 /* Make sure shorter operand is extended the right way
1886 to match the longer operand. */
1887 primarg1 = convert (signed_or_unsigned_type (unsignedp1,
1888 TREE_TYPE (primarg1)),
1891 if (operand_equal_p (arg0, convert (type, primarg1), 0))
1898 /* See if ARG is an expression that is either a comparison or is performing
1899 arithmetic on comparisons. The comparisons must only be comparing
1900 two different values, which will be stored in *CVAL1 and *CVAL2; if
1901 they are non-zero it means that some operands have already been found.
1902 No variables may be used anywhere else in the expression except in the
1905 If this is true, return 1. Otherwise, return zero. */
1908 twoval_comparison_p (arg, cval1, cval2)
1910 tree *cval1, *cval2;
1912 enum tree_code code = TREE_CODE (arg);
1913 char class = TREE_CODE_CLASS (code);
1915 /* We can handle some of the 'e' cases here. */
1917 && (code == TRUTH_NOT_EXPR
1918 || (code == SAVE_EXPR && SAVE_EXPR_RTL (arg) == 0)))
1920 else if (class == 'e'
1921 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
1922 || code == COMPOUND_EXPR))
1928 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2);
1931 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
1932 && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2));
1938 if (code == COND_EXPR)
1939 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
1940 && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2)
1941 && twoval_comparison_p (TREE_OPERAND (arg, 2),
1946 /* First see if we can handle the first operand, then the second. For
1947 the second operand, we know *CVAL1 can't be zero. It must be that
1948 one side of the comparison is each of the values; test for the
1949 case where this isn't true by failing if the two operands
1952 if (operand_equal_p (TREE_OPERAND (arg, 0),
1953 TREE_OPERAND (arg, 1), 0))
1957 *cval1 = TREE_OPERAND (arg, 0);
1958 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
1960 else if (*cval2 == 0)
1961 *cval2 = TREE_OPERAND (arg, 0);
1962 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
1967 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
1969 else if (*cval2 == 0)
1970 *cval2 = TREE_OPERAND (arg, 1);
1971 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
1982 /* ARG is a tree that is known to contain just arithmetic operations and
1983 comparisons. Evaluate the operations in the tree substituting NEW0 for
1984 any occurrence of OLD0 as an operand of a comparison and likewise for
1988 eval_subst (arg, old0, new0, old1, new1)
1990 tree old0, new0, old1, new1;
1992 tree type = TREE_TYPE (arg);
1993 enum tree_code code = TREE_CODE (arg);
1994 char class = TREE_CODE_CLASS (code);
1996 /* We can handle some of the 'e' cases here. */
1997 if (class == 'e' && code == TRUTH_NOT_EXPR)
1999 else if (class == 'e'
2000 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2006 return fold (build1 (code, type,
2007 eval_subst (TREE_OPERAND (arg, 0),
2008 old0, new0, old1, new1)));
2011 return fold (build (code, type,
2012 eval_subst (TREE_OPERAND (arg, 0),
2013 old0, new0, old1, new1),
2014 eval_subst (TREE_OPERAND (arg, 1),
2015 old0, new0, old1, new1)));
2021 return eval_subst (TREE_OPERAND (arg, 0), old0, new0, old1, new1);
2024 return eval_subst (TREE_OPERAND (arg, 1), old0, new0, old1, new1);
2027 return fold (build (code, type,
2028 eval_subst (TREE_OPERAND (arg, 0),
2029 old0, new0, old1, new1),
2030 eval_subst (TREE_OPERAND (arg, 1),
2031 old0, new0, old1, new1),
2032 eval_subst (TREE_OPERAND (arg, 2),
2033 old0, new0, old1, new1)));
2038 tree arg0 = TREE_OPERAND (arg, 0);
2039 tree arg1 = TREE_OPERAND (arg, 1);
2041 /* We need to check both for exact equality and tree equality. The
2042 former will be true if the operand has a side-effect. In that
2043 case, we know the operand occurred exactly once. */
2045 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
2047 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
2050 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
2052 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
2055 return fold (build (code, type, arg0, arg1));
2062 /* Return a tree for the case when the result of an expression is RESULT
2063 converted to TYPE and OMITTED was previously an operand of the expression
2064 but is now not needed (e.g., we folded OMITTED * 0).
2066 If OMITTED has side effects, we must evaluate it. Otherwise, just do
2067 the conversion of RESULT to TYPE. */
2070 omit_one_operand (type, result, omitted)
2071 tree type, result, omitted;
2073 tree t = convert (type, result);
2075 if (TREE_SIDE_EFFECTS (omitted))
2076 return build (COMPOUND_EXPR, type, omitted, t);
2078 return non_lvalue (t);
2081 /* Return a simplified tree node for the truth-negation of ARG. This
2082 never alters ARG itself. We assume that ARG is an operation that
2083 returns a truth value (0 or 1). */
2086 invert_truthvalue (arg)
2089 tree type = TREE_TYPE (arg);
2090 enum tree_code code = TREE_CODE (arg);
2092 if (code == ERROR_MARK)
2095 /* If this is a comparison, we can simply invert it, except for
2096 floating-point non-equality comparisons, in which case we just
2097 enclose a TRUTH_NOT_EXPR around what we have. */
2099 if (TREE_CODE_CLASS (code) == '<')
2101 if (FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg, 0)))
2102 && code != NE_EXPR && code != EQ_EXPR)
2103 return build1 (TRUTH_NOT_EXPR, type, arg);
2105 return build (invert_tree_comparison (code), type,
2106 TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
2112 return convert (type, build_int_2 (TREE_INT_CST_LOW (arg) == 0
2113 && TREE_INT_CST_HIGH (arg) == 0, 0));
2115 case TRUTH_AND_EXPR:
2116 return build (TRUTH_OR_EXPR, type,
2117 invert_truthvalue (TREE_OPERAND (arg, 0)),
2118 invert_truthvalue (TREE_OPERAND (arg, 1)));
2121 return build (TRUTH_AND_EXPR, type,
2122 invert_truthvalue (TREE_OPERAND (arg, 0)),
2123 invert_truthvalue (TREE_OPERAND (arg, 1)));
2125 case TRUTH_XOR_EXPR:
2126 /* Here we can invert either operand. We invert the first operand
2127 unless the second operand is a TRUTH_NOT_EXPR in which case our
2128 result is the XOR of the first operand with the inside of the
2129 negation of the second operand. */
2131 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
2132 return build (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
2133 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
2135 return build (TRUTH_XOR_EXPR, type,
2136 invert_truthvalue (TREE_OPERAND (arg, 0)),
2137 TREE_OPERAND (arg, 1));
2139 case TRUTH_ANDIF_EXPR:
2140 return build (TRUTH_ORIF_EXPR, type,
2141 invert_truthvalue (TREE_OPERAND (arg, 0)),
2142 invert_truthvalue (TREE_OPERAND (arg, 1)));
2144 case TRUTH_ORIF_EXPR:
2145 return build (TRUTH_ANDIF_EXPR, type,
2146 invert_truthvalue (TREE_OPERAND (arg, 0)),
2147 invert_truthvalue (TREE_OPERAND (arg, 1)));
2149 case TRUTH_NOT_EXPR:
2150 return TREE_OPERAND (arg, 0);
2153 return build (COND_EXPR, type, TREE_OPERAND (arg, 0),
2154 invert_truthvalue (TREE_OPERAND (arg, 1)),
2155 invert_truthvalue (TREE_OPERAND (arg, 2)));
2158 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0),
2159 invert_truthvalue (TREE_OPERAND (arg, 1)));
2161 case NON_LVALUE_EXPR:
2162 return invert_truthvalue (TREE_OPERAND (arg, 0));
2167 return build1 (TREE_CODE (arg), type,
2168 invert_truthvalue (TREE_OPERAND (arg, 0)));
2171 if (! integer_onep (TREE_OPERAND (arg, 1)))
2173 return build (EQ_EXPR, type, arg, convert (type, integer_zero_node));
2179 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
2180 operands are another bit-wise operation with a common input. If so,
2181 distribute the bit operations to save an operation and possibly two if
2182 constants are involved. For example, convert
2183 (A | B) & (A | C) into A | (B & C)
2184 Further simplification will occur if B and C are constants.
2186 If this optimization cannot be done, 0 will be returned. */
2189 distribute_bit_expr (code, type, arg0, arg1)
2190 enum tree_code code;
2197 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2198 || TREE_CODE (arg0) == code
2199 || (TREE_CODE (arg0) != BIT_AND_EXPR
2200 && TREE_CODE (arg0) != BIT_IOR_EXPR))
2203 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
2205 common = TREE_OPERAND (arg0, 0);
2206 left = TREE_OPERAND (arg0, 1);
2207 right = TREE_OPERAND (arg1, 1);
2209 else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
2211 common = TREE_OPERAND (arg0, 0);
2212 left = TREE_OPERAND (arg0, 1);
2213 right = TREE_OPERAND (arg1, 0);
2215 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
2217 common = TREE_OPERAND (arg0, 1);
2218 left = TREE_OPERAND (arg0, 0);
2219 right = TREE_OPERAND (arg1, 1);
2221 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
2223 common = TREE_OPERAND (arg0, 1);
2224 left = TREE_OPERAND (arg0, 0);
2225 right = TREE_OPERAND (arg1, 0);
2230 return fold (build (TREE_CODE (arg0), type, common,
2231 fold (build (code, type, left, right))));
2234 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
2235 starting at BITPOS. The field is unsigned if UNSIGNEDP is non-zero. */
2238 make_bit_field_ref (inner, type, bitsize, bitpos, unsignedp)
2241 int bitsize, bitpos;
2244 tree result = build (BIT_FIELD_REF, type, inner,
2245 size_int (bitsize), size_int (bitpos));
2247 TREE_UNSIGNED (result) = unsignedp;
2252 /* Optimize a bit-field compare.
2254 There are two cases: First is a compare against a constant and the
2255 second is a comparison of two items where the fields are at the same
2256 bit position relative to the start of a chunk (byte, halfword, word)
2257 large enough to contain it. In these cases we can avoid the shift
2258 implicit in bitfield extractions.
2260 For constants, we emit a compare of the shifted constant with the
2261 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
2262 compared. For two fields at the same position, we do the ANDs with the
2263 similar mask and compare the result of the ANDs.
2265 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
2266 COMPARE_TYPE is the type of the comparison, and LHS and RHS
2267 are the left and right operands of the comparison, respectively.
2269 If the optimization described above can be done, we return the resulting
2270 tree. Otherwise we return zero. */
2273 optimize_bit_field_compare (code, compare_type, lhs, rhs)
2274 enum tree_code code;
2278 int lbitpos, lbitsize, rbitpos, rbitsize;
2279 int lnbitpos, lnbitsize, rnbitpos, rnbitsize;
2280 tree type = TREE_TYPE (lhs);
2281 tree signed_type, unsigned_type;
2282 int const_p = TREE_CODE (rhs) == INTEGER_CST;
2283 enum machine_mode lmode, rmode, lnmode, rnmode;
2284 int lunsignedp, runsignedp;
2285 int lvolatilep = 0, rvolatilep = 0;
2286 tree linner, rinner;
2290 /* Get all the information about the extractions being done. If the bit size
2291 if the same as the size of the underlying object, we aren't doing an
2292 extraction at all and so can do nothing. */
2293 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
2294 &lunsignedp, &lvolatilep);
2295 if (lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
2301 /* If this is not a constant, we can only do something if bit positions,
2302 sizes, and signedness are the same. */
2303 rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset,
2304 &rmode, &runsignedp, &rvolatilep);
2306 if (lbitpos != rbitpos || lbitsize != rbitsize
2307 || lunsignedp != runsignedp || offset != 0)
2311 /* See if we can find a mode to refer to this field. We should be able to,
2312 but fail if we can't. */
2313 lnmode = get_best_mode (lbitsize, lbitpos,
2314 TYPE_ALIGN (TREE_TYPE (linner)), word_mode,
2316 if (lnmode == VOIDmode)
2319 /* Set signed and unsigned types of the precision of this mode for the
2321 signed_type = type_for_mode (lnmode, 0);
2322 unsigned_type = type_for_mode (lnmode, 1);
2326 rnmode = get_best_mode (rbitsize, rbitpos,
2327 TYPE_ALIGN (TREE_TYPE (rinner)), word_mode,
2329 if (rnmode == VOIDmode)
2333 /* Compute the bit position and size for the new reference and our offset
2334 within it. If the new reference is the same size as the original, we
2335 won't optimize anything, so return zero. */
2336 lnbitsize = GET_MODE_BITSIZE (lnmode);
2337 lnbitpos = lbitpos & ~ (lnbitsize - 1);
2338 lbitpos -= lnbitpos;
2339 if (lnbitsize == lbitsize)
2344 rnbitsize = GET_MODE_BITSIZE (rnmode);
2345 rnbitpos = rbitpos & ~ (rnbitsize - 1);
2346 rbitpos -= rnbitpos;
2347 if (rnbitsize == rbitsize)
2351 #if BYTES_BIG_ENDIAN
2352 lbitpos = lnbitsize - lbitsize - lbitpos;
2355 /* Make the mask to be used against the extracted field. */
2356 mask = build_int_2 (~0, ~0);
2357 TREE_TYPE (mask) = unsigned_type;
2358 force_fit_type (mask, 0);
2359 mask = convert (unsigned_type, mask);
2360 mask = const_binop (LSHIFT_EXPR, mask, size_int (lnbitsize - lbitsize), 0);
2361 mask = const_binop (RSHIFT_EXPR, mask,
2362 size_int (lnbitsize - lbitsize - lbitpos), 0);
2365 /* If not comparing with constant, just rework the comparison
2367 return build (code, compare_type,
2368 build (BIT_AND_EXPR, unsigned_type,
2369 make_bit_field_ref (linner, unsigned_type,
2370 lnbitsize, lnbitpos, 1),
2372 build (BIT_AND_EXPR, unsigned_type,
2373 make_bit_field_ref (rinner, unsigned_type,
2374 rnbitsize, rnbitpos, 1),
2377 /* Otherwise, we are handling the constant case. See if the constant is too
2378 big for the field. Warn and return a tree of for 0 (false) if so. We do
2379 this not only for its own sake, but to avoid having to test for this
2380 error case below. If we didn't, we might generate wrong code.
2382 For unsigned fields, the constant shifted right by the field length should
2383 be all zero. For signed fields, the high-order bits should agree with
2388 if (! integer_zerop (const_binop (RSHIFT_EXPR,
2389 convert (unsigned_type, rhs),
2390 size_int (lbitsize), 0)))
2392 warning ("comparison is always %s due to width of bitfield",
2393 code == NE_EXPR ? "one" : "zero");
2394 return convert (compare_type,
2396 ? integer_one_node : integer_zero_node));
2401 tree tem = const_binop (RSHIFT_EXPR, convert (signed_type, rhs),
2402 size_int (lbitsize - 1), 0);
2403 if (! integer_zerop (tem) && ! integer_all_onesp (tem))
2405 warning ("comparison is always %s due to width of bitfield",
2406 code == NE_EXPR ? "one" : "zero");
2407 return convert (compare_type,
2409 ? integer_one_node : integer_zero_node));
2413 /* Single-bit compares should always be against zero. */
2414 if (lbitsize == 1 && ! integer_zerop (rhs))
2416 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
2417 rhs = convert (type, integer_zero_node);
2420 /* Make a new bitfield reference, shift the constant over the
2421 appropriate number of bits and mask it with the computed mask
2422 (in case this was a signed field). If we changed it, make a new one. */
2423 lhs = make_bit_field_ref (linner, unsigned_type, lnbitsize, lnbitpos, 1);
2426 TREE_SIDE_EFFECTS (lhs) = 1;
2427 TREE_THIS_VOLATILE (lhs) = 1;
2430 rhs = fold (const_binop (BIT_AND_EXPR,
2431 const_binop (LSHIFT_EXPR,
2432 convert (unsigned_type, rhs),
2433 size_int (lbitpos), 0),
2436 return build (code, compare_type,
2437 build (BIT_AND_EXPR, unsigned_type, lhs, mask),
2441 /* Subroutine for fold_truthop: decode a field reference.
2443 If EXP is a comparison reference, we return the innermost reference.
2445 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
2446 set to the starting bit number.
2448 If the innermost field can be completely contained in a mode-sized
2449 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
2451 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
2452 otherwise it is not changed.
2454 *PUNSIGNEDP is set to the signedness of the field.
2456 *PMASK is set to the mask used. This is either contained in a
2457 BIT_AND_EXPR or derived from the width of the field.
2459 Return 0 if this is not a component reference or is one that we can't
2460 do anything with. */
2463 decode_field_reference (exp, pbitsize, pbitpos, pmode, punsignedp,
2466 int *pbitsize, *pbitpos;
2467 enum machine_mode *pmode;
2468 int *punsignedp, *pvolatilep;
2475 /* All the optimizations using this function assume integer fields.
2476 There are problems with FP fields since the type_for_size call
2477 below can fail for, e.g., XFmode. */
2478 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
2483 if (TREE_CODE (exp) == BIT_AND_EXPR)
2485 mask = TREE_OPERAND (exp, 1);
2486 exp = TREE_OPERAND (exp, 0);
2487 STRIP_NOPS (exp); STRIP_NOPS (mask);
2488 if (TREE_CODE (mask) != INTEGER_CST)
2492 if (TREE_CODE (exp) != COMPONENT_REF && TREE_CODE (exp) != ARRAY_REF
2493 && TREE_CODE (exp) != BIT_FIELD_REF)
2496 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
2497 punsignedp, pvolatilep);
2498 if (*pbitsize < 0 || offset != 0)
2503 tree unsigned_type = type_for_size (*pbitsize, 1);
2504 int precision = TYPE_PRECISION (unsigned_type);
2506 mask = build_int_2 (~0, ~0);
2507 TREE_TYPE (mask) = unsigned_type;
2508 force_fit_type (mask, 0);
2509 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
2510 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
2517 /* Return non-zero if MASK represents a mask of SIZE ones in the low-order
2521 all_ones_mask_p (mask, size)
2525 tree type = TREE_TYPE (mask);
2526 int precision = TYPE_PRECISION (type);
2529 tmask = build_int_2 (~0, ~0);
2530 TREE_TYPE (tmask) = signed_type (type);
2531 force_fit_type (tmask, 0);
2533 operand_equal_p (mask,
2534 const_binop (RSHIFT_EXPR,
2535 const_binop (LSHIFT_EXPR, tmask,
2536 size_int (precision - size), 0),
2537 size_int (precision - size), 0),
2541 /* Subroutine for fold_truthop: determine if an operand is simple enough
2542 to be evaluated unconditionally. */
2545 simple_operand_p (exp)
2548 /* Strip any conversions that don't change the machine mode. */
2549 while ((TREE_CODE (exp) == NOP_EXPR
2550 || TREE_CODE (exp) == CONVERT_EXPR)
2551 && (TYPE_MODE (TREE_TYPE (exp))
2552 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)))))
2553 exp = TREE_OPERAND (exp, 0);
2555 return (TREE_CODE_CLASS (TREE_CODE (exp)) == 'c'
2556 || (TREE_CODE_CLASS (TREE_CODE (exp)) == 'd'
2557 && ! TREE_ADDRESSABLE (exp)
2558 && ! TREE_THIS_VOLATILE (exp)
2559 && ! DECL_NONLOCAL (exp)
2560 /* Don't regard global variables as simple. They may be
2561 allocated in ways unknown to the compiler (shared memory,
2562 #pragma weak, etc). */
2563 && ! TREE_PUBLIC (exp)
2564 && ! DECL_EXTERNAL (exp)
2565 /* Loading a static variable is unduly expensive, but global
2566 registers aren't expensive. */
2567 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
2570 /* Subroutine for fold_truthop: try to optimize a range test.
2572 For example, "i >= 2 && i =< 9" can be done as "(unsigned) (i - 2) <= 7".
2574 JCODE is the logical combination of the two terms. It is TRUTH_AND_EXPR
2575 (representing TRUTH_ANDIF_EXPR and TRUTH_AND_EXPR) or TRUTH_OR_EXPR
2576 (representing TRUTH_ORIF_EXPR and TRUTH_OR_EXPR). TYPE is the type of
2579 VAR is the value being tested. LO_CODE and HI_CODE are the comparison
2580 operators comparing VAR to LO_CST and HI_CST. LO_CST is known to be no
2581 larger than HI_CST (they may be equal).
2583 We return the simplified tree or 0 if no optimization is possible. */
2586 range_test (jcode, type, lo_code, hi_code, var, lo_cst, hi_cst)
2587 enum tree_code jcode, lo_code, hi_code;
2588 tree type, var, lo_cst, hi_cst;
2591 enum tree_code rcode;
2593 /* See if this is a range test and normalize the constant terms. */
2595 if (jcode == TRUTH_AND_EXPR)
2600 /* See if we have VAR != CST && VAR != CST+1. */
2601 if (! (hi_code == NE_EXPR
2602 && TREE_INT_CST_LOW (hi_cst) - TREE_INT_CST_LOW (lo_cst) == 1
2603 && tree_int_cst_equal (integer_one_node,
2604 const_binop (MINUS_EXPR,
2605 hi_cst, lo_cst, 0))))
2613 if (hi_code == LT_EXPR)
2614 hi_cst = const_binop (MINUS_EXPR, hi_cst, integer_one_node, 0);
2615 else if (hi_code != LE_EXPR)
2618 if (lo_code == GT_EXPR)
2619 lo_cst = const_binop (PLUS_EXPR, lo_cst, integer_one_node, 0);
2621 /* We now have VAR >= LO_CST && VAR <= HI_CST. */
2634 /* See if we have VAR == CST || VAR == CST+1. */
2635 if (! (hi_code == EQ_EXPR
2636 && TREE_INT_CST_LOW (hi_cst) - TREE_INT_CST_LOW (lo_cst) == 1
2637 && tree_int_cst_equal (integer_one_node,
2638 const_binop (MINUS_EXPR,
2639 hi_cst, lo_cst, 0))))
2647 if (hi_code == GE_EXPR)
2648 hi_cst = const_binop (MINUS_EXPR, hi_cst, integer_one_node, 0);
2649 else if (hi_code != GT_EXPR)
2652 if (lo_code == LE_EXPR)
2653 lo_cst = const_binop (PLUS_EXPR, lo_cst, integer_one_node, 0);
2655 /* We now have VAR < LO_CST || VAR > HI_CST. */
2664 /* When normalizing, it is possible to both increment the smaller constant
2665 and decrement the larger constant. See if they are still ordered. */
2666 if (tree_int_cst_lt (hi_cst, lo_cst))
2669 /* Fail if VAR isn't an integer. */
2670 utype = TREE_TYPE (var);
2671 if (! INTEGRAL_TYPE_P (utype))
2674 /* The range test is invalid if subtracting the two constants results
2675 in overflow. This can happen in traditional mode. */
2676 if (! int_fits_type_p (hi_cst, TREE_TYPE (var))
2677 || ! int_fits_type_p (lo_cst, TREE_TYPE (var)))
2680 if (! TREE_UNSIGNED (utype))
2682 utype = unsigned_type (utype);
2683 var = convert (utype, var);
2684 lo_cst = convert (utype, lo_cst);
2685 hi_cst = convert (utype, hi_cst);
2688 return fold (convert (type,
2689 build (rcode, utype,
2690 build (MINUS_EXPR, utype, var, lo_cst),
2691 const_binop (MINUS_EXPR, hi_cst, lo_cst, 0))));
2694 /* Find ways of folding logical expressions of LHS and RHS:
2695 Try to merge two comparisons to the same innermost item.
2696 Look for range tests like "ch >= '0' && ch <= '9'".
2697 Look for combinations of simple terms on machines with expensive branches
2698 and evaluate the RHS unconditionally.
2700 For example, if we have p->a == 2 && p->b == 4 and we can make an
2701 object large enough to span both A and B, we can do this with a comparison
2702 against the object ANDed with the a mask.
2704 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
2705 operations to do this with one comparison.
2707 We check for both normal comparisons and the BIT_AND_EXPRs made this by
2708 function and the one above.
2710 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
2711 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
2713 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
2716 We return the simplified tree or 0 if no optimization is possible. */
2719 fold_truthop (code, truth_type, lhs, rhs)
2720 enum tree_code code;
2721 tree truth_type, lhs, rhs;
2723 /* If this is the "or" of two comparisons, we can do something if we
2724 the comparisons are NE_EXPR. If this is the "and", we can do something
2725 if the comparisons are EQ_EXPR. I.e.,
2726 (a->b == 2 && a->c == 4) can become (a->new == NEW).
2728 WANTED_CODE is this operation code. For single bit fields, we can
2729 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
2730 comparison for one-bit fields. */
2732 enum tree_code wanted_code;
2733 enum tree_code lcode, rcode;
2734 tree ll_arg, lr_arg, rl_arg, rr_arg;
2735 tree ll_inner, lr_inner, rl_inner, rr_inner;
2736 int ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
2737 int rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
2738 int xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
2739 int lnbitsize, lnbitpos, rnbitsize, rnbitpos;
2740 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
2741 enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
2742 enum machine_mode lnmode, rnmode;
2743 tree ll_mask, lr_mask, rl_mask, rr_mask;
2744 tree l_const, r_const;
2746 int first_bit, end_bit;
2749 /* Start by getting the comparison codes and seeing if this looks like
2750 a range test. Fail if anything is volatile. If one operand is a
2751 BIT_AND_EXPR with the constant one, treat it as if it were surrounded
2754 if (TREE_SIDE_EFFECTS (lhs)
2755 || TREE_SIDE_EFFECTS (rhs))
2758 lcode = TREE_CODE (lhs);
2759 rcode = TREE_CODE (rhs);
2761 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
2762 lcode = NE_EXPR, lhs = build (NE_EXPR, truth_type, lhs, integer_zero_node);
2764 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
2765 rcode = NE_EXPR, rhs = build (NE_EXPR, truth_type, rhs, integer_zero_node);
2767 if (TREE_CODE_CLASS (lcode) != '<'
2768 || TREE_CODE_CLASS (rcode) != '<')
2771 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
2772 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
2774 ll_arg = TREE_OPERAND (lhs, 0);
2775 lr_arg = TREE_OPERAND (lhs, 1);
2776 rl_arg = TREE_OPERAND (rhs, 0);
2777 rr_arg = TREE_OPERAND (rhs, 1);
2779 if (TREE_CODE (lr_arg) == INTEGER_CST
2780 && TREE_CODE (rr_arg) == INTEGER_CST
2781 && operand_equal_p (ll_arg, rl_arg, 0))
2783 if (tree_int_cst_lt (lr_arg, rr_arg))
2784 result = range_test (code, truth_type, lcode, rcode,
2785 ll_arg, lr_arg, rr_arg);
2787 result = range_test (code, truth_type, rcode, lcode,
2788 ll_arg, rr_arg, lr_arg);
2790 /* If this isn't a range test, it also isn't a comparison that
2791 can be merged. However, it wins to evaluate the RHS unconditionally
2792 on machines with expensive branches. */
2794 if (result == 0 && BRANCH_COST >= 2)
2796 if (TREE_CODE (ll_arg) != VAR_DECL
2797 && TREE_CODE (ll_arg) != PARM_DECL)
2799 /* Avoid evaluating the variable part twice. */
2800 ll_arg = save_expr (ll_arg);
2801 lhs = build (lcode, TREE_TYPE (lhs), ll_arg, lr_arg);
2802 rhs = build (rcode, TREE_TYPE (rhs), ll_arg, rr_arg);
2804 return build (code, truth_type, lhs, rhs);
2809 /* If the RHS can be evaluated unconditionally and its operands are
2810 simple, it wins to evaluate the RHS unconditionally on machines
2811 with expensive branches. In this case, this isn't a comparison
2812 that can be merged. */
2814 /* @@ I'm not sure it wins on the m88110 to do this if the comparisons
2815 are with zero (tmw). */
2817 if (BRANCH_COST >= 2
2818 && INTEGRAL_TYPE_P (TREE_TYPE (rhs))
2819 && simple_operand_p (rl_arg)
2820 && simple_operand_p (rr_arg))
2821 return build (code, truth_type, lhs, rhs);
2823 /* See if the comparisons can be merged. Then get all the parameters for
2826 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
2827 || (rcode != EQ_EXPR && rcode != NE_EXPR))
2831 ll_inner = decode_field_reference (ll_arg,
2832 &ll_bitsize, &ll_bitpos, &ll_mode,
2833 &ll_unsignedp, &volatilep, &ll_mask);
2834 lr_inner = decode_field_reference (lr_arg,
2835 &lr_bitsize, &lr_bitpos, &lr_mode,
2836 &lr_unsignedp, &volatilep, &lr_mask);
2837 rl_inner = decode_field_reference (rl_arg,
2838 &rl_bitsize, &rl_bitpos, &rl_mode,
2839 &rl_unsignedp, &volatilep, &rl_mask);
2840 rr_inner = decode_field_reference (rr_arg,
2841 &rr_bitsize, &rr_bitpos, &rr_mode,
2842 &rr_unsignedp, &volatilep, &rr_mask);
2844 /* It must be true that the inner operation on the lhs of each
2845 comparison must be the same if we are to be able to do anything.
2846 Then see if we have constants. If not, the same must be true for
2848 if (volatilep || ll_inner == 0 || rl_inner == 0
2849 || ! operand_equal_p (ll_inner, rl_inner, 0))
2852 if (TREE_CODE (lr_arg) == INTEGER_CST
2853 && TREE_CODE (rr_arg) == INTEGER_CST)
2854 l_const = lr_arg, r_const = rr_arg;
2855 else if (lr_inner == 0 || rr_inner == 0
2856 || ! operand_equal_p (lr_inner, rr_inner, 0))
2859 l_const = r_const = 0;
2861 /* If either comparison code is not correct for our logical operation,
2862 fail. However, we can convert a one-bit comparison against zero into
2863 the opposite comparison against that bit being set in the field. */
2865 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
2866 if (lcode != wanted_code)
2868 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
2874 if (rcode != wanted_code)
2876 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
2882 /* See if we can find a mode that contains both fields being compared on
2883 the left. If we can't, fail. Otherwise, update all constants and masks
2884 to be relative to a field of that size. */
2885 first_bit = MIN (ll_bitpos, rl_bitpos);
2886 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
2887 lnmode = get_best_mode (end_bit - first_bit, first_bit,
2888 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
2890 if (lnmode == VOIDmode)
2893 lnbitsize = GET_MODE_BITSIZE (lnmode);
2894 lnbitpos = first_bit & ~ (lnbitsize - 1);
2895 type = type_for_size (lnbitsize, 1);
2896 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
2898 #if BYTES_BIG_ENDIAN
2899 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
2900 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
2903 ll_mask = const_binop (LSHIFT_EXPR, convert (type, ll_mask),
2904 size_int (xll_bitpos), 0);
2905 rl_mask = const_binop (LSHIFT_EXPR, convert (type, rl_mask),
2906 size_int (xrl_bitpos), 0);
2908 /* Make sure the constants are interpreted as unsigned, so we
2909 don't have sign bits outside the range of their type. */
2913 l_const = convert (unsigned_type (TREE_TYPE (l_const)), l_const);
2914 l_const = const_binop (LSHIFT_EXPR, convert (type, l_const),
2915 size_int (xll_bitpos), 0);
2919 r_const = convert (unsigned_type (TREE_TYPE (r_const)), r_const);
2920 r_const = const_binop (LSHIFT_EXPR, convert (type, r_const),
2921 size_int (xrl_bitpos), 0);
2924 /* If the right sides are not constant, do the same for it. Also,
2925 disallow this optimization if a size or signedness mismatch occurs
2926 between the left and right sides. */
2929 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
2930 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
2931 /* Make sure the two fields on the right
2932 correspond to the left without being swapped. */
2933 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
2936 first_bit = MIN (lr_bitpos, rr_bitpos);
2937 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
2938 rnmode = get_best_mode (end_bit - first_bit, first_bit,
2939 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
2941 if (rnmode == VOIDmode)
2944 rnbitsize = GET_MODE_BITSIZE (rnmode);
2945 rnbitpos = first_bit & ~ (rnbitsize - 1);
2946 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
2948 #if BYTES_BIG_ENDIAN
2949 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
2950 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
2953 lr_mask = const_binop (LSHIFT_EXPR, convert (type, lr_mask),
2954 size_int (xlr_bitpos), 0);
2955 rr_mask = const_binop (LSHIFT_EXPR, convert (type, rr_mask),
2956 size_int (xrr_bitpos), 0);
2958 /* Make a mask that corresponds to both fields being compared.
2959 Do this for both items being compared. If the masks agree,
2960 we can do this by masking both and comparing the masked
2962 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
2963 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0);
2964 if (operand_equal_p (ll_mask, lr_mask, 0) && lnbitsize == rnbitsize)
2966 lhs = make_bit_field_ref (ll_inner, type, lnbitsize, lnbitpos,
2967 ll_unsignedp || rl_unsignedp);
2968 rhs = make_bit_field_ref (lr_inner, type, rnbitsize, rnbitpos,
2969 lr_unsignedp || rr_unsignedp);
2970 if (! all_ones_mask_p (ll_mask, lnbitsize))
2972 lhs = build (BIT_AND_EXPR, type, lhs, ll_mask);
2973 rhs = build (BIT_AND_EXPR, type, rhs, ll_mask);
2975 return build (wanted_code, truth_type, lhs, rhs);
2978 /* There is still another way we can do something: If both pairs of
2979 fields being compared are adjacent, we may be able to make a wider
2980 field containing them both. */
2981 if ((ll_bitsize + ll_bitpos == rl_bitpos
2982 && lr_bitsize + lr_bitpos == rr_bitpos)
2983 || (ll_bitpos == rl_bitpos + rl_bitsize
2984 && lr_bitpos == rr_bitpos + rr_bitsize))
2985 return build (wanted_code, truth_type,
2986 make_bit_field_ref (ll_inner, type,
2987 ll_bitsize + rl_bitsize,
2988 MIN (ll_bitpos, rl_bitpos),
2990 make_bit_field_ref (lr_inner, type,
2991 lr_bitsize + rr_bitsize,
2992 MIN (lr_bitpos, rr_bitpos),
2998 /* Handle the case of comparisons with constants. If there is something in
2999 common between the masks, those bits of the constants must be the same.
3000 If not, the condition is always false. Test for this to avoid generating
3001 incorrect code below. */
3002 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
3003 if (! integer_zerop (result)
3004 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
3005 const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
3007 if (wanted_code == NE_EXPR)
3009 warning ("`or' of unmatched not-equal tests is always 1");
3010 return convert (truth_type, integer_one_node);
3014 warning ("`and' of mutually exclusive equal-tests is always zero");
3015 return convert (truth_type, integer_zero_node);
3019 /* Construct the expression we will return. First get the component
3020 reference we will make. Unless the mask is all ones the width of
3021 that field, perform the mask operation. Then compare with the
3023 result = make_bit_field_ref (ll_inner, type, lnbitsize, lnbitpos,
3024 ll_unsignedp || rl_unsignedp);
3026 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
3027 if (! all_ones_mask_p (ll_mask, lnbitsize))
3028 result = build (BIT_AND_EXPR, type, result, ll_mask);
3030 return build (wanted_code, truth_type, result,
3031 const_binop (BIT_IOR_EXPR, l_const, r_const, 0));
3034 /* Perform constant folding and related simplification of EXPR.
3035 The related simplifications include x*1 => x, x*0 => 0, etc.,
3036 and application of the associative law.
3037 NOP_EXPR conversions may be removed freely (as long as we
3038 are careful not to change the C type of the overall expression)
3039 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
3040 but we can constant-fold them if they have constant operands. */
3046 register tree t = expr;
3047 tree t1 = NULL_TREE;
3049 tree type = TREE_TYPE (expr);
3050 register tree arg0, arg1;
3051 register enum tree_code code = TREE_CODE (t);
3055 /* WINS will be nonzero when the switch is done
3056 if all operands are constant. */
3060 /* Return right away if already constant. */
3061 if (TREE_CONSTANT (t))
3063 if (code == CONST_DECL)
3064 return DECL_INITIAL (t);
3068 kind = TREE_CODE_CLASS (code);
3069 if (code == NOP_EXPR || code == FLOAT_EXPR || code == CONVERT_EXPR)
3073 /* Special case for conversion ops that can have fixed point args. */
3074 arg0 = TREE_OPERAND (t, 0);
3076 /* Don't use STRIP_NOPS, because signedness of argument type matters. */
3078 STRIP_TYPE_NOPS (arg0);
3080 if (arg0 != 0 && TREE_CODE (arg0) == COMPLEX_CST)
3081 subop = TREE_REALPART (arg0);
3085 if (subop != 0 && TREE_CODE (subop) != INTEGER_CST
3086 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
3087 && TREE_CODE (subop) != REAL_CST
3088 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
3090 /* Note that TREE_CONSTANT isn't enough:
3091 static var addresses are constant but we can't
3092 do arithmetic on them. */
3095 else if (kind == 'e' || kind == '<'
3096 || kind == '1' || kind == '2' || kind == 'r')
3098 register int len = tree_code_length[(int) code];
3100 for (i = 0; i < len; i++)
3102 tree op = TREE_OPERAND (t, i);
3106 continue; /* Valid for CALL_EXPR, at least. */
3108 if (kind == '<' || code == RSHIFT_EXPR)
3110 /* Signedness matters here. Perhaps we can refine this
3112 STRIP_TYPE_NOPS (op);
3116 /* Strip any conversions that don't change the mode. */
3120 if (TREE_CODE (op) == COMPLEX_CST)
3121 subop = TREE_REALPART (op);
3125 if (TREE_CODE (subop) != INTEGER_CST
3126 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
3127 && TREE_CODE (subop) != REAL_CST
3128 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
3130 /* Note that TREE_CONSTANT isn't enough:
3131 static var addresses are constant but we can't
3132 do arithmetic on them. */
3142 /* If this is a commutative operation, and ARG0 is a constant, move it
3143 to ARG1 to reduce the number of tests below. */
3144 if ((code == PLUS_EXPR || code == MULT_EXPR || code == MIN_EXPR
3145 || code == MAX_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR
3146 || code == BIT_AND_EXPR)
3147 && (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST))
3149 tem = arg0; arg0 = arg1; arg1 = tem;
3151 tem = TREE_OPERAND (t, 0); TREE_OPERAND (t, 0) = TREE_OPERAND (t, 1);
3152 TREE_OPERAND (t, 1) = tem;
3155 /* Now WINS is set as described above,
3156 ARG0 is the first operand of EXPR,
3157 and ARG1 is the second operand (if it has more than one operand).
3159 First check for cases where an arithmetic operation is applied to a
3160 compound, conditional, or comparison operation. Push the arithmetic
3161 operation inside the compound or conditional to see if any folding
3162 can then be done. Convert comparison to conditional for this purpose.
3163 The also optimizes non-constant cases that used to be done in
3166 Before we do that, see if this is a BIT_AND_EXPR or a BIT_OR_EXPR,
3167 one of the operands is a comparison and the other is either a comparison
3168 or a BIT_AND_EXPR with the constant 1. In that case, the code below
3169 would make the expression more complex. Change it to a
3170 TRUTH_{AND,OR}_EXPR. */
3172 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR)
3173 && ((TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
3174 && (TREE_CODE_CLASS (TREE_CODE (arg1)) == '<'
3175 || (TREE_CODE (arg1) == BIT_AND_EXPR
3176 && integer_onep (TREE_OPERAND (arg1, 1)))))
3177 || (TREE_CODE_CLASS (TREE_CODE (arg1)) == '<'
3178 && (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
3179 || (TREE_CODE (arg0) == BIT_AND_EXPR
3180 && integer_onep (TREE_OPERAND (arg0, 1)))))))
3181 return fold (build (code == BIT_AND_EXPR ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
3184 if (TREE_CODE_CLASS (code) == '1')
3186 if (TREE_CODE (arg0) == COMPOUND_EXPR)
3187 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
3188 fold (build1 (code, type, TREE_OPERAND (arg0, 1))));
3189 else if (TREE_CODE (arg0) == COND_EXPR)
3191 t = fold (build (COND_EXPR, type, TREE_OPERAND (arg0, 0),
3192 fold (build1 (code, type, TREE_OPERAND (arg0, 1))),
3193 fold (build1 (code, type, TREE_OPERAND (arg0, 2)))));
3195 /* If this was a conversion, and all we did was to move into
3196 inside the COND_EXPR, bring it back out. Then return so we
3197 don't get into an infinite recursion loop taking the conversion
3198 out and then back in. */
3200 if ((code == NOP_EXPR || code == CONVERT_EXPR
3201 || code == NON_LVALUE_EXPR)
3202 && TREE_CODE (t) == COND_EXPR
3203 && TREE_CODE (TREE_OPERAND (t, 1)) == code
3204 && TREE_CODE (TREE_OPERAND (t, 2)) == code
3205 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0))
3206 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 2), 0))))
3207 t = build1 (code, type,
3209 TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0)),
3210 TREE_OPERAND (t, 0),
3211 TREE_OPERAND (TREE_OPERAND (t, 1), 0),
3212 TREE_OPERAND (TREE_OPERAND (t, 2), 0)));
3215 else if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<')
3216 return fold (build (COND_EXPR, type, arg0,
3217 fold (build1 (code, type, integer_one_node)),
3218 fold (build1 (code, type, integer_zero_node))));
3220 else if (TREE_CODE_CLASS (code) == '2'
3221 || TREE_CODE_CLASS (code) == '<')
3223 if (TREE_CODE (arg1) == COMPOUND_EXPR)
3224 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
3225 fold (build (code, type,
3226 arg0, TREE_OPERAND (arg1, 1))));
3227 else if (TREE_CODE (arg1) == COND_EXPR
3228 || TREE_CODE_CLASS (TREE_CODE (arg1)) == '<')
3230 tree test, true_value, false_value;
3232 if (TREE_CODE (arg1) == COND_EXPR)
3234 test = TREE_OPERAND (arg1, 0);
3235 true_value = TREE_OPERAND (arg1, 1);
3236 false_value = TREE_OPERAND (arg1, 2);
3241 true_value = integer_one_node;
3242 false_value = integer_zero_node;
3245 /* If ARG0 is complex we want to make sure we only evaluate
3246 it once. Though this is only required if it is volatile, it
3247 might be more efficient even if it is not. However, if we
3248 succeed in folding one part to a constant, we do not need
3249 to make this SAVE_EXPR. Since we do this optimization
3250 primarily to see if we do end up with constant and this
3251 SAVE_EXPR interfers with later optimizations, suppressing
3252 it when we can is important. */
3254 if ((TREE_CODE (arg0) != VAR_DECL && TREE_CODE (arg0) != PARM_DECL)
3255 || TREE_SIDE_EFFECTS (arg0))
3257 tree lhs = fold (build (code, type, arg0, true_value));
3258 tree rhs = fold (build (code, type, arg0, false_value));
3260 if (TREE_CONSTANT (lhs) || TREE_CONSTANT (rhs))
3261 return fold (build (COND_EXPR, type, test, lhs, rhs));
3263 arg0 = save_expr (arg0);
3266 test = fold (build (COND_EXPR, type, test,
3267 fold (build (code, type, arg0, true_value)),
3268 fold (build (code, type, arg0, false_value))));
3269 if (TREE_CODE (arg0) == SAVE_EXPR)
3270 return build (COMPOUND_EXPR, type,
3271 convert (void_type_node, arg0), test);
3273 return convert (type, test);
3276 else if (TREE_CODE (arg0) == COMPOUND_EXPR)
3277 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
3278 fold (build (code, type, TREE_OPERAND (arg0, 1), arg1)));
3279 else if (TREE_CODE (arg0) == COND_EXPR
3280 || TREE_CODE_CLASS (TREE_CODE (arg0)) == '<')
3282 tree test, true_value, false_value;
3284 if (TREE_CODE (arg0) == COND_EXPR)
3286 test = TREE_OPERAND (arg0, 0);
3287 true_value = TREE_OPERAND (arg0, 1);
3288 false_value = TREE_OPERAND (arg0, 2);
3293 true_value = integer_one_node;
3294 false_value = integer_zero_node;
3297 if ((TREE_CODE (arg1) != VAR_DECL && TREE_CODE (arg1) != PARM_DECL)
3298 || TREE_SIDE_EFFECTS (arg1))
3300 tree lhs = fold (build (code, type, true_value, arg1));
3301 tree rhs = fold (build (code, type, false_value, arg1));
3303 if (TREE_CONSTANT (lhs) || TREE_CONSTANT (rhs))
3304 return fold (build (COND_EXPR, type, test, lhs, rhs));
3306 arg1 = save_expr (arg1);
3309 test = fold (build (COND_EXPR, type, test,
3310 fold (build (code, type, true_value, arg1)),
3311 fold (build (code, type, false_value, arg1))));
3312 if (TREE_CODE (arg1) == SAVE_EXPR)
3313 return build (COMPOUND_EXPR, type,
3314 convert (void_type_node, arg1), test);
3316 return convert (type, test);
3319 else if (TREE_CODE_CLASS (code) == '<'
3320 && TREE_CODE (arg0) == COMPOUND_EXPR)
3321 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
3322 fold (build (code, type, TREE_OPERAND (arg0, 1), arg1)));
3323 else if (TREE_CODE_CLASS (code) == '<'
3324 && TREE_CODE (arg1) == COMPOUND_EXPR)
3325 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
3326 fold (build (code, type, arg0, TREE_OPERAND (arg1, 1))));
3338 return fold (DECL_INITIAL (t));
3343 case FIX_TRUNC_EXPR:
3344 /* Other kinds of FIX are not handled properly by fold_convert. */
3346 /* In addition to the cases of two conversions in a row
3347 handled below, if we are converting something to its own
3348 type via an object of identical or wider precision, neither
3349 conversion is needed. */
3350 if ((TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
3351 || TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR)
3352 && TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) == TREE_TYPE (t)
3353 && ((INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0)))
3354 && INTEGRAL_TYPE_P (TREE_TYPE (t)))
3355 || (FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0)))
3356 && FLOAT_TYPE_P (TREE_TYPE (t))))
3357 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0)))
3358 >= TYPE_PRECISION (TREE_TYPE (t))))
3359 return TREE_OPERAND (TREE_OPERAND (t, 0), 0);
3361 /* Two conversions in a row are not needed unless:
3362 - the intermediate type is narrower than both initial and final, or
3363 - the intermediate type and innermost type differ in signedness,
3364 and the outermost type is wider than the intermediate, or
3365 - the initial type is a pointer type and the precisions of the
3366 intermediate and final types differ, or
3367 - the final type is a pointer type and the precisions of the
3368 initial and intermediate types differ. */
3369 if ((TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
3370 || TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR)
3371 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0)))
3372 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
3374 TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0)))
3375 > TYPE_PRECISION (TREE_TYPE (t)))
3376 && ! ((TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
3378 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
3380 && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t, 0)))
3381 != TREE_UNSIGNED (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
3382 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0)))
3383 < TYPE_PRECISION (TREE_TYPE (t))))
3384 && ((TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t, 0)))
3385 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0)))
3386 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))))
3388 (TREE_UNSIGNED (TREE_TYPE (t))
3389 && (TYPE_PRECISION (TREE_TYPE (t))
3390 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0))))))
3391 && ! ((TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
3393 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0)))
3394 != TYPE_PRECISION (TREE_TYPE (t))))
3395 && ! (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE
3396 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
3397 != TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0))))))
3398 return convert (TREE_TYPE (t), TREE_OPERAND (TREE_OPERAND (t, 0), 0));
3400 if (TREE_CODE (TREE_OPERAND (t, 0)) == MODIFY_EXPR
3401 && TREE_CONSTANT (TREE_OPERAND (TREE_OPERAND (t, 0), 1))
3402 /* Detect assigning a bitfield. */
3403 && !(TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) == COMPONENT_REF
3404 && DECL_BIT_FIELD (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 1))))
3406 /* Don't leave an assignment inside a conversion
3407 unless assigning a bitfield. */
3408 tree prev = TREE_OPERAND (t, 0);
3409 TREE_OPERAND (t, 0) = TREE_OPERAND (prev, 1);
3410 /* First do the assignment, then return converted constant. */
3411 t = build (COMPOUND_EXPR, TREE_TYPE (t), prev, fold (t));
3417 TREE_CONSTANT (t) = TREE_CONSTANT (arg0);
3420 return fold_convert (t, arg0);
3422 #if 0 /* This loses on &"foo"[0]. */
3427 /* Fold an expression like: "foo"[2] */
3428 if (TREE_CODE (arg0) == STRING_CST
3429 && TREE_CODE (arg1) == INTEGER_CST
3430 && !TREE_INT_CST_HIGH (arg1)
3431 && (i = TREE_INT_CST_LOW (arg1)) < TREE_STRING_LENGTH (arg0))
3433 t = build_int_2 (TREE_STRING_POINTER (arg0)[i], 0);
3434 TREE_TYPE (t) = TREE_TYPE (TREE_TYPE (arg0));
3435 force_fit_type (t, 0);
3442 TREE_CONSTANT (t) = wins;
3448 if (TREE_CODE (arg0) == INTEGER_CST)
3450 HOST_WIDE_INT low, high;
3451 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
3452 TREE_INT_CST_HIGH (arg0),
3454 t = build_int_2 (low, high);
3455 TREE_TYPE (t) = type;
3457 = (TREE_OVERFLOW (arg0)
3458 | force_fit_type (t, overflow));
3459 TREE_CONSTANT_OVERFLOW (t)
3460 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg0);
3462 else if (TREE_CODE (arg0) == REAL_CST)
3463 t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
3464 TREE_TYPE (t) = type;
3466 else if (TREE_CODE (arg0) == NEGATE_EXPR)
3467 return TREE_OPERAND (arg0, 0);
3469 /* Convert - (a - b) to (b - a) for non-floating-point. */
3470 else if (TREE_CODE (arg0) == MINUS_EXPR && ! FLOAT_TYPE_P (type))
3471 return build (MINUS_EXPR, type, TREE_OPERAND (arg0, 1),
3472 TREE_OPERAND (arg0, 0));
3479 if (TREE_CODE (arg0) == INTEGER_CST)
3481 if (! TREE_UNSIGNED (type)
3482 && TREE_INT_CST_HIGH (arg0) < 0)
3484 HOST_WIDE_INT low, high;
3485 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
3486 TREE_INT_CST_HIGH (arg0),
3488 t = build_int_2 (low, high);
3489 TREE_TYPE (t) = type;
3491 = (TREE_OVERFLOW (arg0)
3492 | force_fit_type (t, overflow));
3493 TREE_CONSTANT_OVERFLOW (t)
3494 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg0);
3497 else if (TREE_CODE (arg0) == REAL_CST)
3499 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
3500 t = build_real (type,
3501 REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
3503 TREE_TYPE (t) = type;
3505 else if (TREE_CODE (arg0) == ABS_EXPR || TREE_CODE (arg0) == NEGATE_EXPR)
3506 return build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0));
3510 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
3512 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
3513 return build (COMPLEX_EXPR, TREE_TYPE (arg0),
3514 TREE_OPERAND (arg0, 0),
3515 fold (build1 (NEGATE_EXPR,
3516 TREE_TYPE (TREE_TYPE (arg0)),
3517 TREE_OPERAND (arg0, 1))));
3518 else if (TREE_CODE (arg0) == COMPLEX_CST)
3519 return build_complex (TREE_OPERAND (arg0, 0),
3520 fold (build1 (NEGATE_EXPR,
3521 TREE_TYPE (TREE_TYPE (arg0)),
3522 TREE_OPERAND (arg0, 1))));
3523 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
3524 return fold (build (TREE_CODE (arg0), type,
3525 fold (build1 (CONJ_EXPR, type,
3526 TREE_OPERAND (arg0, 0))),
3527 fold (build1 (CONJ_EXPR,
3528 type, TREE_OPERAND (arg0, 1)))));
3529 else if (TREE_CODE (arg0) == CONJ_EXPR)
3530 return TREE_OPERAND (arg0, 0);
3536 if (TREE_CODE (arg0) == INTEGER_CST)
3537 t = build_int_2 (~ TREE_INT_CST_LOW (arg0),
3538 ~ TREE_INT_CST_HIGH (arg0));
3539 TREE_TYPE (t) = type;
3540 force_fit_type (t, 0);
3541 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg0);
3542 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (arg0);
3544 else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
3545 return TREE_OPERAND (arg0, 0);
3549 /* A + (-B) -> A - B */
3550 if (TREE_CODE (arg1) == NEGATE_EXPR)
3551 return fold (build (MINUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
3552 else if (! FLOAT_TYPE_P (type))
3554 if (integer_zerop (arg1))
3555 return non_lvalue (convert (type, arg0));
3557 /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
3558 with a constant, and the two constants have no bits in common,
3559 we should treat this as a BIT_IOR_EXPR since this may produce more
3561 if (TREE_CODE (arg0) == BIT_AND_EXPR
3562 && TREE_CODE (arg1) == BIT_AND_EXPR
3563 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
3564 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
3565 && integer_zerop (const_binop (BIT_AND_EXPR,
3566 TREE_OPERAND (arg0, 1),
3567 TREE_OPERAND (arg1, 1), 0)))
3569 code = BIT_IOR_EXPR;
3573 /* (A * C) + (B * C) -> (A+B) * C. Since we are most concerned
3574 about the case where C is a constant, just try one of the
3575 four possibilities. */
3577 if (TREE_CODE (arg0) == MULT_EXPR && TREE_CODE (arg1) == MULT_EXPR
3578 && operand_equal_p (TREE_OPERAND (arg0, 1),
3579 TREE_OPERAND (arg1, 1), 0))
3580 return fold (build (MULT_EXPR, type,
3581 fold (build (PLUS_EXPR, type,
3582 TREE_OPERAND (arg0, 0),
3583 TREE_OPERAND (arg1, 0))),
3584 TREE_OPERAND (arg0, 1)));
3586 /* In IEEE floating point, x+0 may not equal x. */
3587 else if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
3588 && real_zerop (arg1))
3589 return non_lvalue (convert (type, arg0));
3591 /* In most languages, can't associate operations on floats
3592 through parentheses. Rather than remember where the parentheses
3593 were, we don't associate floats at all. It shouldn't matter much. */
3594 if (FLOAT_TYPE_P (type))
3596 /* The varsign == -1 cases happen only for addition and subtraction.
3597 It says that the arg that was split was really CON minus VAR.
3598 The rest of the code applies to all associative operations. */
3604 if (split_tree (arg0, code, &var, &con, &varsign))
3608 /* EXPR is (CON-VAR) +- ARG1. */
3609 /* If it is + and VAR==ARG1, return just CONST. */
3610 if (code == PLUS_EXPR && operand_equal_p (var, arg1, 0))
3611 return convert (TREE_TYPE (t), con);
3613 /* If ARG0 is a constant, don't change things around;
3614 instead keep all the constant computations together. */
3616 if (TREE_CONSTANT (arg0))
3619 /* Otherwise return (CON +- ARG1) - VAR. */
3620 TREE_SET_CODE (t, MINUS_EXPR);
3621 TREE_OPERAND (t, 1) = var;
3623 = fold (build (code, TREE_TYPE (t), con, arg1));
3627 /* EXPR is (VAR+CON) +- ARG1. */
3628 /* If it is - and VAR==ARG1, return just CONST. */
3629 if (code == MINUS_EXPR && operand_equal_p (var, arg1, 0))
3630 return convert (TREE_TYPE (t), con);
3632 /* If ARG0 is a constant, don't change things around;
3633 instead keep all the constant computations together. */
3635 if (TREE_CONSTANT (arg0))
3638 /* Otherwise return VAR +- (ARG1 +- CON). */
3639 TREE_OPERAND (t, 1) = tem
3640 = fold (build (code, TREE_TYPE (t), arg1, con));
3641 TREE_OPERAND (t, 0) = var;
3642 if (integer_zerop (tem)
3643 && (code == PLUS_EXPR || code == MINUS_EXPR))
3644 return convert (type, var);
3645 /* If we have x +/- (c - d) [c an explicit integer]
3646 change it to x -/+ (d - c) since if d is relocatable
3647 then the latter can be a single immediate insn
3648 and the former cannot. */
3649 if (TREE_CODE (tem) == MINUS_EXPR
3650 && TREE_CODE (TREE_OPERAND (tem, 0)) == INTEGER_CST)
3652 tree tem1 = TREE_OPERAND (tem, 1);
3653 TREE_OPERAND (tem, 1) = TREE_OPERAND (tem, 0);
3654 TREE_OPERAND (tem, 0) = tem1;
3656 (code == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR));
3662 if (split_tree (arg1, code, &var, &con, &varsign))
3664 /* EXPR is ARG0 +- (CON +- VAR). */
3665 if (TREE_CODE (t) == MINUS_EXPR
3666 && operand_equal_p (var, arg0, 0))
3668 /* If VAR and ARG0 cancel, return just CON or -CON. */
3669 if (code == PLUS_EXPR)
3670 return convert (TREE_TYPE (t), con);
3671 return fold (build1 (NEGATE_EXPR, TREE_TYPE (t),
3672 convert (TREE_TYPE (t), con)));
3674 if (TREE_CONSTANT (arg1))
3678 (code == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR));
3680 = fold (build (code, TREE_TYPE (t), arg0, con));
3681 TREE_OPERAND (t, 1) = var;
3682 if (integer_zerop (TREE_OPERAND (t, 0))
3683 && TREE_CODE (t) == PLUS_EXPR)
3684 return convert (TREE_TYPE (t), var);
3689 #if defined (REAL_IS_NOT_DOUBLE) && ! defined (REAL_ARITHMETIC)
3690 if (TREE_CODE (arg1) == REAL_CST)
3692 #endif /* REAL_IS_NOT_DOUBLE, and no REAL_ARITHMETIC */
3694 t1 = const_binop (code, arg0, arg1, 0);
3695 if (t1 != NULL_TREE)
3697 /* The return value should always have
3698 the same type as the original expression. */
3699 TREE_TYPE (t1) = TREE_TYPE (t);
3705 if (! FLOAT_TYPE_P (type))
3707 if (! wins && integer_zerop (arg0))
3708 return build1 (NEGATE_EXPR, type, arg1);
3709 if (integer_zerop (arg1))
3710 return non_lvalue (convert (type, arg0));
3712 /* (A * C) - (B * C) -> (A-B) * C. Since we are most concerned
3713 about the case where C is a constant, just try one of the
3714 four possibilities. */
3716 if (TREE_CODE (arg0) == MULT_EXPR && TREE_CODE (arg1) == MULT_EXPR
3717 && operand_equal_p (TREE_OPERAND (arg0, 1),
3718 TREE_OPERAND (arg1, 1), 0))
3719 return fold (build (MULT_EXPR, type,
3720 fold (build (MINUS_EXPR, type,
3721 TREE_OPERAND (arg0, 0),
3722 TREE_OPERAND (arg1, 0))),
3723 TREE_OPERAND (arg0, 1)));
3725 /* Convert A - (-B) to A + B. */
3726 else if (TREE_CODE (arg1) == NEGATE_EXPR)
3727 return fold (build (PLUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
3728 else if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT)
3730 /* Except with IEEE floating point, 0-x equals -x. */
3731 if (! wins && real_zerop (arg0))
3732 return build1 (NEGATE_EXPR, type, arg1);
3733 /* Except with IEEE floating point, x-0 equals x. */
3734 if (real_zerop (arg1))
3735 return non_lvalue (convert (type, arg0));
3737 /* Fold &x - &x. This can happen from &x.foo - &x.
3738 This is unsafe for certain floats even in non-IEEE formats.
3739 In IEEE, it is unsafe because it does wrong for NaNs.
3740 Also note that operand_equal_p is always false if an operand
3743 if (operand_equal_p (arg0, arg1, FLOAT_TYPE_P (type)))
3744 return convert (type, integer_zero_node);
3749 if (! FLOAT_TYPE_P (type))
3751 if (integer_zerop (arg1))
3752 return omit_one_operand (type, arg1, arg0);
3753 if (integer_onep (arg1))
3754 return non_lvalue (convert (type, arg0));
3756 /* (a * (1 << b)) is (a << b) */
3757 if (TREE_CODE (arg1) == LSHIFT_EXPR
3758 && integer_onep (TREE_OPERAND (arg1, 0)))
3759 return fold (build (LSHIFT_EXPR, type, arg0,
3760 TREE_OPERAND (arg1, 1)));
3761 if (TREE_CODE (arg0) == LSHIFT_EXPR
3762 && integer_onep (TREE_OPERAND (arg0, 0)))
3763 return fold (build (LSHIFT_EXPR, type, arg1,
3764 TREE_OPERAND (arg0, 1)));
3768 /* x*0 is 0, except for IEEE floating point. */
3769 if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
3770 && real_zerop (arg1))
3771 return omit_one_operand (type, arg1, arg0);
3772 /* In IEEE floating point, x*1 is not equivalent to x for snans.
3773 However, ANSI says we can drop signals,
3774 so we can do this anyway. */
3775 if (real_onep (arg1))
3776 return non_lvalue (convert (type, arg0));
3778 if (! wins && real_twop (arg1))
3780 tree arg = save_expr (arg0);
3781 return build (PLUS_EXPR, type, arg, arg);
3788 if (integer_all_onesp (arg1))
3789 return omit_one_operand (type, arg1, arg0);
3790 if (integer_zerop (arg1))
3791 return non_lvalue (convert (type, arg0));
3792 t1 = distribute_bit_expr (code, type, arg0, arg1);
3793 if (t1 != NULL_TREE)
3796 /* (a << C1) | (a >> C2) if A is unsigned and C1+C2 is the size of A
3797 is a rotate of A by C1 bits. */
3799 if ((TREE_CODE (arg0) == RSHIFT_EXPR
3800 || TREE_CODE (arg0) == LSHIFT_EXPR)
3801 && (TREE_CODE (arg1) == RSHIFT_EXPR
3802 || TREE_CODE (arg1) == LSHIFT_EXPR)
3803 && TREE_CODE (arg0) != TREE_CODE (arg1)
3804 && operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1,0), 0)
3805 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0)))
3806 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
3807 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
3808 && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
3809 && TREE_INT_CST_HIGH (TREE_OPERAND (arg1, 1)) == 0
3810 && ((TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
3811 + TREE_INT_CST_LOW (TREE_OPERAND (arg1, 1)))
3812 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
3813 return build (LROTATE_EXPR, type, TREE_OPERAND (arg0, 0),
3814 TREE_CODE (arg0) == LSHIFT_EXPR
3815 ? TREE_OPERAND (arg0, 1) : TREE_OPERAND (arg1, 1));
3820 if (integer_zerop (arg1))
3821 return non_lvalue (convert (type, arg0));
3822 if (integer_all_onesp (arg1))
3823 return fold (build1 (BIT_NOT_EXPR, type, arg0));
3828 if (integer_all_onesp (arg1))
3829 return non_lvalue (convert (type, arg0));
3830 if (integer_zerop (arg1))
3831 return omit_one_operand (type, arg1, arg0);
3832 t1 = distribute_bit_expr (code, type, arg0, arg1);
3833 if (t1 != NULL_TREE)
3835 /* Simplify ((int)c & 0x377) into (int)c, if c is unsigned char. */
3836 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == NOP_EXPR
3837 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0))))
3839 int prec = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)));
3840 if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
3841 && (~TREE_INT_CST_LOW (arg0)
3842 & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
3843 return build1 (NOP_EXPR, type, TREE_OPERAND (arg1, 0));
3845 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
3846 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
3848 int prec = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
3849 if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
3850 && (~TREE_INT_CST_LOW (arg1)
3851 & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
3852 return build1 (NOP_EXPR, type, TREE_OPERAND (arg0, 0));
3856 case BIT_ANDTC_EXPR:
3857 if (integer_all_onesp (arg0))
3858 return non_lvalue (convert (type, arg1));
3859 if (integer_zerop (arg0))
3860 return omit_one_operand (type, arg0, arg1);
3861 if (TREE_CODE (arg1) == INTEGER_CST)
3863 arg1 = fold (build1 (BIT_NOT_EXPR, type, arg1));
3864 code = BIT_AND_EXPR;
3869 case TRUNC_DIV_EXPR:
3870 case ROUND_DIV_EXPR:
3871 case FLOOR_DIV_EXPR:
3873 case EXACT_DIV_EXPR:
3875 if (integer_onep (arg1))
3876 return non_lvalue (convert (type, arg0));
3877 if (integer_zerop (arg1))
3880 /* Look for ((a * C1) / C3) or (((a * C1) + C2) / C3),
3881 where C1 % C3 == 0 or C3 % C1 == 0. We can simplify these
3882 expressions, which often appear in the offsets or sizes of
3883 objects with a varying size. Only deal with positive divisors
3886 Look for NOPs and SAVE_EXPRs inside. */
3888 if (TREE_CODE (arg1) == INTEGER_CST
3889 && tree_int_cst_lt (integer_zero_node, arg1))
3891 int have_save_expr = 0;
3892 tree c2 = integer_zero_node;
3895 if (TREE_CODE (xarg0) == SAVE_EXPR)
3896 have_save_expr = 1, xarg0 = TREE_OPERAND (xarg0, 0);
3900 if (TREE_CODE (xarg0) == PLUS_EXPR
3901 && TREE_CODE (TREE_OPERAND (xarg0, 1)) == INTEGER_CST)
3902 c2 = TREE_OPERAND (xarg0, 1), xarg0 = TREE_OPERAND (xarg0, 0);
3903 else if (TREE_CODE (xarg0) == MINUS_EXPR
3904 && TREE_CODE (TREE_OPERAND (xarg0, 1)) == INTEGER_CST)
3906 c2 = fold (build1 (NEGATE_EXPR, type, TREE_OPERAND (xarg0, 1)));
3907 xarg0 = TREE_OPERAND (xarg0, 0);
3910 if (TREE_CODE (xarg0) == SAVE_EXPR)
3911 have_save_expr = 1, xarg0 = TREE_OPERAND (xarg0, 0);
3915 if (TREE_CODE (xarg0) == MULT_EXPR
3916 && TREE_CODE (TREE_OPERAND (xarg0, 1)) == INTEGER_CST
3917 && tree_int_cst_lt (integer_zero_node, TREE_OPERAND (xarg0, 1))
3918 && (integer_zerop (const_binop (TRUNC_MOD_EXPR,
3919 TREE_OPERAND (xarg0, 1), arg1, 1))
3920 || integer_zerop (const_binop (TRUNC_MOD_EXPR, arg1,
3921 TREE_OPERAND (xarg0, 1), 1))))
3923 tree outer_div = integer_one_node;
3924 tree c1 = TREE_OPERAND (xarg0, 1);
3927 /* If C3 > C1, set them equal and do a divide by
3928 C3/C1 at the end of the operation. */
3929 if (tree_int_cst_lt (c1, c3))
3930 outer_div = const_binop (code, c3, c1, 0), c3 = c1;
3932 /* The result is A * (C1/C3) + (C2/C3). */
3933 t = fold (build (PLUS_EXPR, type,
3934 fold (build (MULT_EXPR, type,
3935 TREE_OPERAND (xarg0, 0),
3936 const_binop (code, c1, c3, 1))),
3937 const_binop (code, c2, c3, 1)));
3939 if (! integer_onep (outer_div))
3940 t = fold (build (code, type, t, outer_div));
3949 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
3950 #ifndef REAL_INFINITY
3951 if (TREE_CODE (arg1) == REAL_CST
3952 && real_zerop (arg1))
3955 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
3960 case FLOOR_MOD_EXPR:
3961 case ROUND_MOD_EXPR:
3962 case TRUNC_MOD_EXPR:
3963 if (integer_onep (arg1))
3964 return omit_one_operand (type, integer_zero_node, arg0);
3965 if (integer_zerop (arg1))
3968 /* Look for ((a * C1) % C3) or (((a * C1) + C2) % C3),
3969 where C1 % C3 == 0. Handle similarly to the division case,
3970 but don't bother with SAVE_EXPRs. */
3972 if (TREE_CODE (arg1) == INTEGER_CST
3973 && ! integer_zerop (arg1))
3975 tree c2 = integer_zero_node;
3978 if (TREE_CODE (xarg0) == PLUS_EXPR
3979 && TREE_CODE (TREE_OPERAND (xarg0, 1)) == INTEGER_CST)
3980 c2 = TREE_OPERAND (xarg0, 1), xarg0 = TREE_OPERAND (xarg0, 0);
3981 else if (TREE_CODE (xarg0) == MINUS_EXPR
3982 && TREE_CODE (TREE_OPERAND (xarg0, 1)) == INTEGER_CST)
3984 c2 = fold (build1 (NEGATE_EXPR, type, TREE_OPERAND (xarg0, 1)));
3985 xarg0 = TREE_OPERAND (xarg0, 0);
3990 if (TREE_CODE (xarg0) == MULT_EXPR
3991 && TREE_CODE (TREE_OPERAND (xarg0, 1)) == INTEGER_CST
3992 && integer_zerop (const_binop (TRUNC_MOD_EXPR,
3993 TREE_OPERAND (xarg0, 1),
3995 /* The result is (C2%C3). */
3996 return omit_one_operand (type, const_binop (code, c2, arg1, 1),
3997 TREE_OPERAND (xarg0, 0));
4006 if (integer_zerop (arg1))
4007 return non_lvalue (convert (type, arg0));
4008 /* Since negative shift count is not well-defined,
4009 don't try to compute it in the compiler. */
4010 if (tree_int_cst_lt (arg1, integer_zero_node))
4015 if (operand_equal_p (arg0, arg1, 0))
4017 if (INTEGRAL_TYPE_P (type)
4018 && operand_equal_p (arg1, TYPE_MIN_VALUE (type), 1))
4019 return omit_one_operand (type, arg1, arg0);
4023 if (operand_equal_p (arg0, arg1, 0))
4025 if (INTEGRAL_TYPE_P (type)
4026 && operand_equal_p (arg1, TYPE_MAX_VALUE (type), 1))
4027 return omit_one_operand (type, arg1, arg0);
4030 case TRUTH_NOT_EXPR:
4031 /* Note that the operand of this must be an int
4032 and its values must be 0 or 1.
4033 ("true" is a fixed value perhaps depending on the language,
4034 but we don't handle values other than 1 correctly yet.) */
4035 return invert_truthvalue (arg0);
4037 case TRUTH_ANDIF_EXPR:
4038 /* Note that the operands of this must be ints
4039 and their values must be 0 or 1.
4040 ("true" is a fixed value perhaps depending on the language.) */
4041 /* If first arg is constant zero, return it. */
4042 if (integer_zerop (arg0))
4044 case TRUTH_AND_EXPR:
4045 /* If either arg is constant true, drop it. */
4046 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
4047 return non_lvalue (arg1);
4048 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
4049 return non_lvalue (arg0);
4050 /* If second arg is constant zero, result is zero, but first arg
4051 must be evaluated. */
4052 if (integer_zerop (arg1))
4053 return omit_one_operand (type, arg1, arg0);
4056 /* Check for the possibility of merging component references. If our
4057 lhs is another similar operation, try to merge its rhs with our
4058 rhs. Then try to merge our lhs and rhs. */
4061 if (TREE_CODE (arg0) == code)
4063 tem = fold_truthop (code, type,
4064 TREE_OPERAND (arg0, 1), arg1);
4066 return fold (build (code, type, TREE_OPERAND (arg0, 0), tem));
4069 tem = fold_truthop (code, type, arg0, arg1);
4075 case TRUTH_ORIF_EXPR:
4076 /* Note that the operands of this must be ints
4077 and their values must be 0 or true.
4078 ("true" is a fixed value perhaps depending on the language.) */
4079 /* If first arg is constant true, return it. */
4080 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
4083 /* If either arg is constant zero, drop it. */
4084 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
4085 return non_lvalue (arg1);
4086 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1))
4087 return non_lvalue (arg0);
4088 /* If second arg is constant true, result is true, but we must
4089 evaluate first arg. */
4090 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
4091 return omit_one_operand (type, arg1, arg0);
4094 case TRUTH_XOR_EXPR:
4095 /* If either arg is constant zero, drop it. */
4096 if (integer_zerop (arg0))
4097 return non_lvalue (arg1);
4098 if (integer_zerop (arg1))
4099 return non_lvalue (arg0);
4100 /* If either arg is constant true, this is a logical inversion. */
4101 if (integer_onep (arg0))
4102 return non_lvalue (invert_truthvalue (arg1));
4103 if (integer_onep (arg1))
4104 return non_lvalue (invert_truthvalue (arg0));
4113 /* If one arg is a constant integer, put it last. */
4114 if (TREE_CODE (arg0) == INTEGER_CST
4115 && TREE_CODE (arg1) != INTEGER_CST)
4117 TREE_OPERAND (t, 0) = arg1;
4118 TREE_OPERAND (t, 1) = arg0;
4119 arg0 = TREE_OPERAND (t, 0);
4120 arg1 = TREE_OPERAND (t, 1);
4121 code = swap_tree_comparison (code);
4122 TREE_SET_CODE (t, code);
4125 /* Convert foo++ == CONST into ++foo == CONST + INCR.
4126 First, see if one arg is constant; find the constant arg
4127 and the other one. */
4129 tree constop = 0, varop;
4132 if (TREE_CONSTANT (arg1))
4133 constoploc = &TREE_OPERAND (t, 1), constop = arg1, varop = arg0;
4134 if (TREE_CONSTANT (arg0))
4135 constoploc = &TREE_OPERAND (t, 0), constop = arg0, varop = arg1;
4137 if (constop && TREE_CODE (varop) == POSTINCREMENT_EXPR)
4139 /* This optimization is invalid for ordered comparisons
4140 if CONST+INCR overflows or if foo+incr might overflow.
4141 This optimization is invalid for floating point due to rounding.
4142 For pointer types we assume overflow doesn't happen. */
4143 if (TREE_CODE (TREE_TYPE (varop)) == POINTER_TYPE
4144 || (! FLOAT_TYPE_P (TREE_TYPE (varop))
4145 && (code == EQ_EXPR || code == NE_EXPR)))
4148 = fold (build (PLUS_EXPR, TREE_TYPE (varop),
4149 constop, TREE_OPERAND (varop, 1)));
4150 TREE_SET_CODE (varop, PREINCREMENT_EXPR);
4151 *constoploc = newconst;
4155 else if (constop && TREE_CODE (varop) == POSTDECREMENT_EXPR)
4157 if (TREE_CODE (TREE_TYPE (varop)) == POINTER_TYPE
4158 || (! FLOAT_TYPE_P (TREE_TYPE (varop))
4159 && (code == EQ_EXPR || code == NE_EXPR)))
4162 = fold (build (MINUS_EXPR, TREE_TYPE (varop),
4163 constop, TREE_OPERAND (varop, 1)));
4164 TREE_SET_CODE (varop, PREDECREMENT_EXPR);
4165 *constoploc = newconst;
4171 /* Change X >= CST to X > (CST - 1) if CST is positive. */
4172 if (TREE_CODE (arg1) == INTEGER_CST
4173 && TREE_CODE (arg0) != INTEGER_CST
4174 && ! tree_int_cst_lt (arg1, integer_one_node))
4176 switch (TREE_CODE (t))
4180 TREE_SET_CODE (t, code);
4181 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
4182 TREE_OPERAND (t, 1) = arg1;
4187 TREE_SET_CODE (t, code);
4188 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
4189 TREE_OPERAND (t, 1) = arg1;
4193 /* If this is an EQ or NE comparison with zero and ARG0 is
4194 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
4195 two operations, but the latter can be done in one less insn
4196 one machine that have only two-operand insns or on which a
4197 constant cannot be the first operand. */
4198 if (integer_zerop (arg1) && (code == EQ_EXPR || code == NE_EXPR)
4199 && TREE_CODE (arg0) == BIT_AND_EXPR)
4201 if (TREE_CODE (TREE_OPERAND (arg0, 0)) == LSHIFT_EXPR
4202 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 0), 0)))
4204 fold (build (code, type,
4205 build (BIT_AND_EXPR, TREE_TYPE (arg0),
4207 TREE_TYPE (TREE_OPERAND (arg0, 0)),
4208 TREE_OPERAND (arg0, 1),
4209 TREE_OPERAND (TREE_OPERAND (arg0, 0), 1)),
4210 convert (TREE_TYPE (arg0),
4213 else if (TREE_CODE (TREE_OPERAND (arg0, 1)) == LSHIFT_EXPR
4214 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 1), 0)))
4216 fold (build (code, type,
4217 build (BIT_AND_EXPR, TREE_TYPE (arg0),
4219 TREE_TYPE (TREE_OPERAND (arg0, 1)),
4220 TREE_OPERAND (arg0, 0),
4221 TREE_OPERAND (TREE_OPERAND (arg0, 1), 1)),
4222 convert (TREE_TYPE (arg0),
4227 /* It would be nice to do this since it generates better code.
4228 Unfortunately, it doesn't produce the correct result if the
4229 first operand is negative. */
4231 /* If this is an NE or EQ comparison of zero against the result of a
4232 signed MOD operation, make the MOD operation unsigned since it
4233 is simpler and equivalent. */
4234 if ((code == NE_EXPR || code == EQ_EXPR)
4235 && integer_zerop (arg1)
4236 && ! TREE_UNSIGNED (TREE_TYPE (arg0))
4237 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
4238 || TREE_CODE (arg0) == CEIL_MOD_EXPR
4239 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
4240 || TREE_CODE (arg0) == ROUND_MOD_EXPR))
4242 tree newtype = unsigned_type (TREE_TYPE (arg0));
4243 tree newmod = build (TREE_CODE (arg0), newtype,
4244 convert (newtype, TREE_OPERAND (arg0, 0)),
4245 convert (newtype, TREE_OPERAND (arg0, 1)));
4247 return build (code, type, newmod, convert (newtype, arg1));
4251 /* If this is an NE comparison of zero with an AND of one, remove the
4252 comparison since the AND will give the correct value. */
4253 if (code == NE_EXPR && integer_zerop (arg1)
4254 && TREE_CODE (arg0) == BIT_AND_EXPR
4255 && integer_onep (TREE_OPERAND (arg0, 1)))
4256 return convert (type, arg0);
4258 /* If we have (A & C) == C where C is a power of 2, convert this into
4259 (A & C) != 0. Similarly for NE_EXPR. */
4260 if ((code == EQ_EXPR || code == NE_EXPR)
4261 && TREE_CODE (arg0) == BIT_AND_EXPR
4262 && integer_pow2p (TREE_OPERAND (arg0, 1))
4263 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
4264 return build (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
4265 arg0, integer_zero_node);
4267 /* Simplify comparison of something with itself. (For IEEE
4268 floating-point, we can only do some of these simplifications.) */
4269 if (operand_equal_p (arg0, arg1, 0))
4276 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
4278 t = build_int_2 (1, 0);
4279 TREE_TYPE (t) = type;
4283 TREE_SET_CODE (t, code);
4287 /* For NE, we can only do this simplification if integer. */
4288 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
4290 /* ... fall through ... */
4293 t = build_int_2 (0, 0);
4294 TREE_TYPE (t) = type;
4299 /* An unsigned comparison against 0 can be simplified. */
4300 if (integer_zerop (arg1)
4301 && (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
4302 || TREE_CODE (TREE_TYPE (arg1)) == POINTER_TYPE)
4303 && TREE_UNSIGNED (TREE_TYPE (arg1)))
4305 switch (TREE_CODE (t))
4309 TREE_SET_CODE (t, NE_EXPR);
4313 TREE_SET_CODE (t, EQ_EXPR);
4316 return omit_one_operand (type,
4317 convert (type, integer_one_node),
4320 return omit_one_operand (type,
4321 convert (type, integer_zero_node),
4326 /* If we are comparing an expression that just has comparisons
4327 of two integer values, arithmetic expressions of those comparisons,
4328 and constants, we can simplify it. There are only three cases
4329 to check: the two values can either be equal, the first can be
4330 greater, or the second can be greater. Fold the expression for
4331 those three values. Since each value must be 0 or 1, we have
4332 eight possibilities, each of which corresponds to the constant 0
4333 or 1 or one of the six possible comparisons.
4335 This handles common cases like (a > b) == 0 but also handles
4336 expressions like ((x > y) - (y > x)) > 0, which supposedly
4337 occur in macroized code. */
4339 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
4341 tree cval1 = 0, cval2 = 0;
4343 if (twoval_comparison_p (arg0, &cval1, &cval2)
4344 /* Don't handle degenerate cases here; they should already
4345 have been handled anyway. */
4346 && cval1 != 0 && cval2 != 0
4347 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
4348 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
4349 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
4350 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
4351 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
4353 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
4354 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
4356 /* We can't just pass T to eval_subst in case cval1 or cval2
4357 was the same as ARG1. */
4360 = fold (build (code, type,
4361 eval_subst (arg0, cval1, maxval, cval2, minval),
4364 = fold (build (code, type,
4365 eval_subst (arg0, cval1, maxval, cval2, maxval),
4368 = fold (build (code, type,
4369 eval_subst (arg0, cval1, minval, cval2, maxval),
4372 /* All three of these results should be 0 or 1. Confirm they
4373 are. Then use those values to select the proper code
4376 if ((integer_zerop (high_result)
4377 || integer_onep (high_result))
4378 && (integer_zerop (equal_result)
4379 || integer_onep (equal_result))
4380 && (integer_zerop (low_result)
4381 || integer_onep (low_result)))
4383 /* Make a 3-bit mask with the high-order bit being the
4384 value for `>', the next for '=', and the low for '<'. */
4385 switch ((integer_onep (high_result) * 4)
4386 + (integer_onep (equal_result) * 2)
4387 + integer_onep (low_result))
4391 return omit_one_operand (type, integer_zero_node, arg0);
4412 return omit_one_operand (type, integer_one_node, arg0);
4415 return fold (build (code, type, cval1, cval2));
4420 /* If this is a comparison of a field, we may be able to simplify it. */
4421 if ((TREE_CODE (arg0) == COMPONENT_REF
4422 || TREE_CODE (arg0) == BIT_FIELD_REF)
4423 && (code == EQ_EXPR || code == NE_EXPR)
4424 /* Handle the constant case even without -O
4425 to make sure the warnings are given. */
4426 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
4428 t1 = optimize_bit_field_compare (code, type, arg0, arg1);
4432 /* From here on, the only cases we handle are when the result is
4433 known to be a constant.
4435 To compute GT, swap the arguments and do LT.
4436 To compute GE, do LT and invert the result.
4437 To compute LE, swap the arguments, do LT and invert the result.
4438 To compute NE, do EQ and invert the result.
4440 Therefore, the code below must handle only EQ and LT. */
4442 if (code == LE_EXPR || code == GT_EXPR)
4444 tem = arg0, arg0 = arg1, arg1 = tem;
4445 code = swap_tree_comparison (code);
4448 /* Note that it is safe to invert for real values here because we
4449 will check below in the one case that it matters. */
4452 if (code == NE_EXPR || code == GE_EXPR)
4455 code = invert_tree_comparison (code);
4458 /* Compute a result for LT or EQ if args permit;
4459 otherwise return T. */
4460 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
4462 if (code == EQ_EXPR)
4463 t1 = build_int_2 ((TREE_INT_CST_LOW (arg0)
4464 == TREE_INT_CST_LOW (arg1))
4465 && (TREE_INT_CST_HIGH (arg0)
4466 == TREE_INT_CST_HIGH (arg1)),
4469 t1 = build_int_2 ((TREE_UNSIGNED (TREE_TYPE (arg0))
4470 ? INT_CST_LT_UNSIGNED (arg0, arg1)
4471 : INT_CST_LT (arg0, arg1)),
4475 /* Assume a nonexplicit constant cannot equal an explicit one,
4476 since such code would be undefined anyway.
4477 Exception: on sysvr4, using #pragma weak,
4478 a label can come out as 0. */
4479 else if (TREE_CODE (arg1) == INTEGER_CST
4480 && !integer_zerop (arg1)
4481 && TREE_CONSTANT (arg0)
4482 && TREE_CODE (arg0) == ADDR_EXPR
4484 t1 = build_int_2 (0, 0);
4486 /* Two real constants can be compared explicitly. */
4487 else if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
4489 /* If either operand is a NaN, the result is false with two
4490 exceptions: First, an NE_EXPR is true on NaNs, but that case
4491 is already handled correctly since we will be inverting the
4492 result for NE_EXPR. Second, if we had inverted a LE_EXPR
4493 or a GE_EXPR into a LT_EXPR, we must return true so that it
4494 will be inverted into false. */
4496 if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
4497 || REAL_VALUE_ISNAN (TREE_REAL_CST (arg1)))
4498 t1 = build_int_2 (invert && code == LT_EXPR, 0);
4500 else if (code == EQ_EXPR)
4501 t1 = build_int_2 (REAL_VALUES_EQUAL (TREE_REAL_CST (arg0),
4502 TREE_REAL_CST (arg1)),
4505 t1 = build_int_2 (REAL_VALUES_LESS (TREE_REAL_CST (arg0),
4506 TREE_REAL_CST (arg1)),
4510 if (t1 == NULL_TREE)
4514 TREE_INT_CST_LOW (t1) ^= 1;
4516 TREE_TYPE (t1) = type;
4520 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
4521 so all simple results must be passed through pedantic_non_lvalue. */
4522 if (TREE_CODE (arg0) == INTEGER_CST)
4523 return pedantic_non_lvalue
4524 (TREE_OPERAND (t, (integer_zerop (arg0) ? 2 : 1)));
4525 else if (operand_equal_p (arg1, TREE_OPERAND (expr, 2), 0))
4526 return pedantic_non_lvalue (omit_one_operand (type, arg1, arg0));
4528 /* If the second operand is zero, invert the comparison and swap
4529 the second and third operands. Likewise if the second operand
4530 is constant and the third is not or if the third operand is
4531 equivalent to the first operand of the comparison. */
4533 if (integer_zerop (arg1)
4534 || (TREE_CONSTANT (arg1) && ! TREE_CONSTANT (TREE_OPERAND (t, 2)))
4535 || (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
4536 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
4537 TREE_OPERAND (t, 2),
4538 TREE_OPERAND (arg0, 1))))
4540 /* See if this can be inverted. If it can't, possibly because
4541 it was a floating-point inequality comparison, don't do
4543 tem = invert_truthvalue (arg0);
4545 if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
4547 arg0 = TREE_OPERAND (t, 0) = tem;
4548 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 2);
4549 TREE_OPERAND (t, 2) = arg1;
4550 arg1 = TREE_OPERAND (t, 1);
4554 /* If we have A op B ? A : C, we may be able to convert this to a
4555 simpler expression, depending on the operation and the values
4556 of B and C. IEEE floating point prevents this though,
4557 because A or B might be -0.0 or a NaN. */
4559 if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
4560 && (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
4561 || ! FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 0))))
4562 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
4563 arg1, TREE_OPERAND (arg0, 1)))
4565 tree arg2 = TREE_OPERAND (t, 2);
4566 enum tree_code comp_code = TREE_CODE (arg0);
4568 /* If we have A op 0 ? A : -A, this is A, -A, abs (A), or abs (-A),
4569 depending on the comparison operation. */
4570 if (integer_zerop (TREE_OPERAND (arg0, 1))
4571 && TREE_CODE (arg2) == NEGATE_EXPR
4572 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
4576 return pedantic_non_lvalue
4577 (fold (build1 (NEGATE_EXPR, type, arg1)));
4579 return pedantic_non_lvalue (convert (type, arg1));
4582 return pedantic_non_lvalue
4583 (fold (build1 (ABS_EXPR, type, arg1)));
4586 return pedantic_non_lvalue
4587 (fold (build1 (NEGATE_EXPR, type,
4588 fold (build1 (ABS_EXPR, type, arg1)))));
4591 /* If this is A != 0 ? A : 0, this is simply A. For ==, it is
4594 if (integer_zerop (TREE_OPERAND (arg0, 1)) && integer_zerop (arg2))
4596 if (comp_code == NE_EXPR)
4597 return pedantic_non_lvalue (convert (type, arg1));
4598 else if (comp_code == EQ_EXPR)
4599 return pedantic_non_lvalue (convert (type, integer_zero_node));
4602 /* If this is A op B ? A : B, this is either A, B, min (A, B),
4603 or max (A, B), depending on the operation. */
4605 if (operand_equal_for_comparison_p (TREE_OPERAND (arg0, 1),
4606 arg2, TREE_OPERAND (arg0, 0)))
4610 return pedantic_non_lvalue (convert (type, arg2));
4612 return pedantic_non_lvalue (convert (type, arg1));
4615 return pedantic_non_lvalue
4616 (fold (build (MIN_EXPR, type, arg1, arg2)));
4619 return pedantic_non_lvalue
4620 (fold (build (MAX_EXPR, type, arg1, arg2)));
4623 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
4624 we might still be able to simplify this. For example,
4625 if C1 is one less or one more than C2, this might have started
4626 out as a MIN or MAX and been transformed by this function.
4627 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
4629 if (INTEGRAL_TYPE_P (type)
4630 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
4631 && TREE_CODE (arg2) == INTEGER_CST)
4635 /* We can replace A with C1 in this case. */
4636 arg1 = TREE_OPERAND (t, 1)
4637 = convert (type, TREE_OPERAND (arg0, 1));
4641 /* If C1 is C2 + 1, this is min(A, C2). */
4642 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type), 1)
4643 && operand_equal_p (TREE_OPERAND (arg0, 1),
4644 const_binop (PLUS_EXPR, arg2,
4645 integer_one_node, 0), 1))
4646 return pedantic_non_lvalue
4647 (fold (build (MIN_EXPR, type, arg1, arg2)));
4651 /* If C1 is C2 - 1, this is min(A, C2). */
4652 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type), 1)
4653 && operand_equal_p (TREE_OPERAND (arg0, 1),
4654 const_binop (MINUS_EXPR, arg2,
4655 integer_one_node, 0), 1))
4656 return pedantic_non_lvalue
4657 (fold (build (MIN_EXPR, type, arg1, arg2)));
4661 /* If C1 is C2 - 1, this is max(A, C2). */
4662 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type), 1)
4663 && operand_equal_p (TREE_OPERAND (arg0, 1),
4664 const_binop (MINUS_EXPR, arg2,
4665 integer_one_node, 0), 1))
4666 return pedantic_non_lvalue
4667 (fold (build (MAX_EXPR, type, arg1, arg2)));
4671 /* If C1 is C2 + 1, this is max(A, C2). */
4672 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type), 1)
4673 && operand_equal_p (TREE_OPERAND (arg0, 1),
4674 const_binop (PLUS_EXPR, arg2,
4675 integer_one_node, 0), 1))
4676 return pedantic_non_lvalue
4677 (fold (build (MAX_EXPR, type, arg1, arg2)));
4682 /* Convert A ? 1 : 0 to simply A. */
4683 if (integer_onep (TREE_OPERAND (t, 1))
4684 && integer_zerop (TREE_OPERAND (t, 2))
4685 /* If we try to convert TREE_OPERAND (t, 0) to our type, the
4686 call to fold will try to move the conversion inside
4687 a COND, which will recurse. In that case, the COND_EXPR
4688 is probably the best choice, so leave it alone. */
4689 && type == TREE_TYPE (arg0))
4690 return pedantic_non_lvalue (arg0);
4693 /* Look for expressions of the form A & 2 ? 2 : 0. The result of this
4694 operation is simply A & 2. */
4696 if (integer_zerop (TREE_OPERAND (t, 2))
4697 && TREE_CODE (arg0) == NE_EXPR
4698 && integer_zerop (TREE_OPERAND (arg0, 1))
4699 && integer_pow2p (arg1)
4700 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
4701 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
4703 return pedantic_non_lvalue (convert (type, TREE_OPERAND (arg0, 0)));
4708 /* When pedantic, a compound expression can be neither an lvalue
4709 nor an integer constant expression. */
4710 if (TREE_SIDE_EFFECTS (arg0) || pedantic)
4712 /* Don't let (0, 0) be null pointer constant. */
4713 if (integer_zerop (arg1))
4714 return non_lvalue (arg1);
4719 return build_complex (arg0, arg1);
4723 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
4725 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
4726 return omit_one_operand (type, TREE_OPERAND (arg0, 0),
4727 TREE_OPERAND (arg0, 1));
4728 else if (TREE_CODE (arg0) == COMPLEX_CST)
4729 return TREE_REALPART (arg0);
4730 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
4731 return fold (build (TREE_CODE (arg0), type,
4732 fold (build1 (REALPART_EXPR, type,
4733 TREE_OPERAND (arg0, 0))),
4734 fold (build1 (REALPART_EXPR,
4735 type, TREE_OPERAND (arg0, 1)))));
4739 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
4740 return convert (type, integer_zero_node);
4741 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
4742 return omit_one_operand (type, TREE_OPERAND (arg0, 1),
4743 TREE_OPERAND (arg0, 0));
4744 else if (TREE_CODE (arg0) == COMPLEX_CST)
4745 return TREE_IMAGPART (arg0);
4746 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
4747 return fold (build (TREE_CODE (arg0), type,
4748 fold (build1 (IMAGPART_EXPR, type,
4749 TREE_OPERAND (arg0, 0))),
4750 fold (build1 (IMAGPART_EXPR, type,
4751 TREE_OPERAND (arg0, 1)))));
4756 } /* switch (code) */