1 /* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987, 88, 92-98, 1999 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, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /*@@ This file should be rewritten to use an arbitrary precision
22 @@ representation for "struct tree_int_cst" and "struct tree_real_cst".
23 @@ Perhaps the routines could also be used for bc/dc, and made a lib.
24 @@ The routines that translate from the ap rep should
25 @@ warn if precision et. al. is lost.
26 @@ This would also make life easier when this technology is used
27 @@ for cross-compilers. */
30 /* The entry points in this file are fold, size_int_wide, size_binop
33 fold takes a tree as argument and returns a simplified tree.
35 size_binop takes a tree code for an arithmetic operation
36 and two operands that are trees, and produces a tree for the
37 result, assuming the type comes from `sizetype'.
39 size_int takes an integer value, and creates a tree constant
40 with type from `sizetype'.
42 force_fit_type takes a constant and prior overflow indicator, and
43 forces the value to fit the type. It returns an overflow indicator. */
53 /* Handle floating overflow for `const_binop'. */
54 static jmp_buf float_error;
56 static void encode PROTO((HOST_WIDE_INT *,
57 HOST_WIDE_INT, HOST_WIDE_INT));
58 static void decode PROTO((HOST_WIDE_INT *,
59 HOST_WIDE_INT *, HOST_WIDE_INT *));
60 int div_and_round_double PROTO((enum tree_code, int, HOST_WIDE_INT,
61 HOST_WIDE_INT, HOST_WIDE_INT,
62 HOST_WIDE_INT, HOST_WIDE_INT *,
63 HOST_WIDE_INT *, HOST_WIDE_INT *,
65 static int split_tree PROTO((tree, enum tree_code, tree *,
67 static tree int_const_binop PROTO((enum tree_code, tree, tree, int, int));
68 static tree const_binop PROTO((enum tree_code, tree, tree, int));
69 static tree fold_convert PROTO((tree, tree));
70 static enum tree_code invert_tree_comparison PROTO((enum tree_code));
71 static enum tree_code swap_tree_comparison PROTO((enum tree_code));
72 static int truth_value_p PROTO((enum tree_code));
73 static int operand_equal_for_comparison_p PROTO((tree, tree, tree));
74 static int twoval_comparison_p PROTO((tree, tree *, tree *, int *));
75 static tree eval_subst PROTO((tree, tree, tree, tree, tree));
76 static tree omit_one_operand PROTO((tree, tree, tree));
77 static tree pedantic_omit_one_operand PROTO((tree, tree, tree));
78 static tree distribute_bit_expr PROTO((enum tree_code, tree, tree, tree));
79 static tree make_bit_field_ref PROTO((tree, tree, int, int, int));
80 static tree optimize_bit_field_compare PROTO((enum tree_code, tree,
82 static tree decode_field_reference PROTO((tree, int *, int *,
83 enum machine_mode *, int *,
84 int *, tree *, tree *));
85 static int all_ones_mask_p PROTO((tree, int));
86 static int simple_operand_p PROTO((tree));
87 static tree range_binop PROTO((enum tree_code, tree, tree, int,
89 static tree make_range PROTO((tree, int *, tree *, tree *));
90 static tree build_range_check PROTO((tree, tree, int, tree, tree));
91 static int merge_ranges PROTO((int *, tree *, tree *, int, tree, tree,
93 static tree fold_range_test PROTO((tree));
94 static tree unextend PROTO((tree, int, int, tree));
95 static tree fold_truthop PROTO((enum tree_code, tree, tree, tree));
96 static tree strip_compound_expr PROTO((tree, tree));
97 static int multiple_of_p PROTO((tree, tree, tree));
98 static tree constant_boolean_node PROTO((int, tree));
99 static int count_cond PROTO((tree, int));
102 #define BRANCH_COST 1
105 /* Suppose A1 + B1 = SUM1, using 2's complement arithmetic ignoring overflow.
106 Suppose A, B and SUM have the same respective signs as A1, B1, and SUM1.
107 Then this yields nonzero if overflow occurred during the addition.
108 Overflow occurs if A and B have the same sign, but A and SUM differ in sign.
109 Use `^' to test whether signs differ, and `< 0' to isolate the sign. */
110 #define overflow_sum_sign(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
112 /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
113 We do that by representing the two-word integer in 4 words, with only
114 HOST_BITS_PER_WIDE_INT/2 bits stored in each word, as a positive number. */
117 ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT/2)) - 1))
118 #define HIGHPART(x) \
119 ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT/2)
120 #define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT/2)
122 /* Unpack a two-word integer into 4 words.
123 LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
124 WORDS points to the array of HOST_WIDE_INTs. */
127 encode (words, low, hi)
128 HOST_WIDE_INT *words;
129 HOST_WIDE_INT low, hi;
131 words[0] = LOWPART (low);
132 words[1] = HIGHPART (low);
133 words[2] = LOWPART (hi);
134 words[3] = HIGHPART (hi);
137 /* Pack an array of 4 words into a two-word integer.
138 WORDS points to the array of words.
139 The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces. */
142 decode (words, low, hi)
143 HOST_WIDE_INT *words;
144 HOST_WIDE_INT *low, *hi;
146 *low = words[0] | words[1] * BASE;
147 *hi = words[2] | words[3] * BASE;
150 /* Make the integer constant T valid for its type
151 by setting to 0 or 1 all the bits in the constant
152 that don't belong in the type.
153 Yield 1 if a signed overflow occurs, 0 otherwise.
154 If OVERFLOW is nonzero, a signed overflow has already occurred
155 in calculating T, so propagate it.
157 Make the real constant T valid for its type by calling CHECK_FLOAT_VALUE,
161 force_fit_type (t, overflow)
165 HOST_WIDE_INT low, high;
168 if (TREE_CODE (t) == REAL_CST)
170 #ifdef CHECK_FLOAT_VALUE
171 CHECK_FLOAT_VALUE (TYPE_MODE (TREE_TYPE (t)), TREE_REAL_CST (t),
177 else if (TREE_CODE (t) != INTEGER_CST)
180 low = TREE_INT_CST_LOW (t);
181 high = TREE_INT_CST_HIGH (t);
183 if (POINTER_TYPE_P (TREE_TYPE (t)))
186 prec = TYPE_PRECISION (TREE_TYPE (t));
188 /* First clear all bits that are beyond the type's precision. */
190 if (prec == 2 * HOST_BITS_PER_WIDE_INT)
192 else 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) = 0;
200 if (prec < HOST_BITS_PER_WIDE_INT)
201 TREE_INT_CST_LOW (t) &= ~((HOST_WIDE_INT) (-1) << prec);
204 /* Unsigned types do not suffer sign extension or overflow. */
205 if (TREE_UNSIGNED (TREE_TYPE (t)))
208 /* If the value's sign bit is set, extend the sign. */
209 if (prec != 2 * HOST_BITS_PER_WIDE_INT
210 && (prec > HOST_BITS_PER_WIDE_INT
211 ? (TREE_INT_CST_HIGH (t)
212 & ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)))
213 : TREE_INT_CST_LOW (t) & ((HOST_WIDE_INT) 1 << (prec - 1))))
215 /* Value is negative:
216 set to 1 all the bits that are outside this type's precision. */
217 if (prec > HOST_BITS_PER_WIDE_INT)
219 TREE_INT_CST_HIGH (t)
220 |= ((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
224 TREE_INT_CST_HIGH (t) = -1;
225 if (prec < HOST_BITS_PER_WIDE_INT)
226 TREE_INT_CST_LOW (t) |= ((HOST_WIDE_INT) (-1) << prec);
230 /* Yield nonzero if signed overflow occurred. */
232 ((overflow | (low ^ TREE_INT_CST_LOW (t)) | (high ^ TREE_INT_CST_HIGH (t)))
236 /* Add two doubleword integers with doubleword result.
237 Each argument is given as two `HOST_WIDE_INT' pieces.
238 One argument is L1 and H1; the other, L2 and H2.
239 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
242 add_double (l1, h1, l2, h2, lv, hv)
243 HOST_WIDE_INT l1, h1, l2, h2;
244 HOST_WIDE_INT *lv, *hv;
249 h = h1 + h2 + ((unsigned HOST_WIDE_INT) l < l1);
253 return overflow_sum_sign (h1, h2, h);
256 /* Negate a doubleword integer with doubleword result.
257 Return nonzero if the operation overflows, assuming it's signed.
258 The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
259 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
262 neg_double (l1, h1, lv, hv)
263 HOST_WIDE_INT l1, h1;
264 HOST_WIDE_INT *lv, *hv;
270 return (*hv & h1) < 0;
280 /* Multiply two doubleword integers with doubleword result.
281 Return nonzero if the operation overflows, assuming it's signed.
282 Each argument is given as two `HOST_WIDE_INT' pieces.
283 One argument is L1 and H1; the other, L2 and H2.
284 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
287 mul_double (l1, h1, l2, h2, lv, hv)
288 HOST_WIDE_INT l1, h1, l2, h2;
289 HOST_WIDE_INT *lv, *hv;
291 HOST_WIDE_INT arg1[4];
292 HOST_WIDE_INT arg2[4];
293 HOST_WIDE_INT prod[4 * 2];
294 register unsigned HOST_WIDE_INT carry;
295 register int i, j, k;
296 HOST_WIDE_INT toplow, tophigh, neglow, neghigh;
298 encode (arg1, l1, h1);
299 encode (arg2, l2, h2);
301 bzero ((char *) prod, sizeof prod);
303 for (i = 0; i < 4; i++)
306 for (j = 0; j < 4; j++)
309 /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000. */
310 carry += arg1[i] * arg2[j];
311 /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF. */
313 prod[k] = LOWPART (carry);
314 carry = HIGHPART (carry);
319 decode (prod, lv, hv); /* This ignores prod[4] through prod[4*2-1] */
321 /* Check for overflow by calculating the top half of the answer in full;
322 it should agree with the low half's sign bit. */
323 decode (prod+4, &toplow, &tophigh);
326 neg_double (l2, h2, &neglow, &neghigh);
327 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
331 neg_double (l1, h1, &neglow, &neghigh);
332 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
334 return (*hv < 0 ? ~(toplow & tophigh) : toplow | tophigh) != 0;
337 /* Shift the doubleword integer in L1, H1 left by COUNT places
338 keeping only PREC bits of result.
339 Shift right if COUNT is negative.
340 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
341 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
344 lshift_double (l1, h1, count, prec, lv, hv, arith)
345 HOST_WIDE_INT l1, h1, count;
347 HOST_WIDE_INT *lv, *hv;
352 rshift_double (l1, h1, - count, prec, lv, hv, arith);
356 #ifdef SHIFT_COUNT_TRUNCATED
357 if (SHIFT_COUNT_TRUNCATED)
361 if (count >= HOST_BITS_PER_WIDE_INT)
363 *hv = (unsigned HOST_WIDE_INT) l1 << (count - HOST_BITS_PER_WIDE_INT);
368 *hv = (((unsigned HOST_WIDE_INT) h1 << count)
369 | ((unsigned HOST_WIDE_INT) l1 >> (HOST_BITS_PER_WIDE_INT - count - 1) >> 1));
370 *lv = (unsigned HOST_WIDE_INT) l1 << count;
374 /* Shift the doubleword integer in L1, H1 right by COUNT places
375 keeping only PREC bits of result. COUNT must be positive.
376 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
377 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
380 rshift_double (l1, h1, count, prec, lv, hv, arith)
381 HOST_WIDE_INT l1, h1, count;
382 int prec ATTRIBUTE_UNUSED;
383 HOST_WIDE_INT *lv, *hv;
386 unsigned HOST_WIDE_INT signmask;
388 ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
391 #ifdef SHIFT_COUNT_TRUNCATED
392 if (SHIFT_COUNT_TRUNCATED)
396 if (count >= HOST_BITS_PER_WIDE_INT)
399 *lv = ((signmask << (2 * HOST_BITS_PER_WIDE_INT - count - 1) << 1)
400 | ((unsigned HOST_WIDE_INT) h1 >> (count - HOST_BITS_PER_WIDE_INT)));
404 *lv = (((unsigned HOST_WIDE_INT) l1 >> count)
405 | ((unsigned HOST_WIDE_INT) h1 << (HOST_BITS_PER_WIDE_INT - count - 1) << 1));
406 *hv = ((signmask << (HOST_BITS_PER_WIDE_INT - count))
407 | ((unsigned HOST_WIDE_INT) h1 >> count));
411 /* Rotate the doubleword integer in L1, H1 left by COUNT places
412 keeping only PREC bits of result.
413 Rotate right if COUNT is negative.
414 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
417 lrotate_double (l1, h1, count, prec, lv, hv)
418 HOST_WIDE_INT l1, h1, count;
420 HOST_WIDE_INT *lv, *hv;
422 HOST_WIDE_INT s1l, s1h, s2l, s2h;
428 lshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
429 rshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
434 /* Rotate the doubleword integer in L1, H1 left by COUNT places
435 keeping only PREC bits of result. COUNT must be positive.
436 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
439 rrotate_double (l1, h1, count, prec, lv, hv)
440 HOST_WIDE_INT l1, h1, count;
442 HOST_WIDE_INT *lv, *hv;
444 HOST_WIDE_INT s1l, s1h, s2l, s2h;
450 rshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
451 lshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
456 /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
457 for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
458 CODE is a tree code for a kind of division, one of
459 TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
461 It controls how the quotient is rounded to a integer.
462 Return nonzero if the operation overflows.
463 UNS nonzero says do unsigned division. */
466 div_and_round_double (code, uns,
467 lnum_orig, hnum_orig, lden_orig, hden_orig,
468 lquo, hquo, lrem, hrem)
471 HOST_WIDE_INT lnum_orig, hnum_orig; /* num == numerator == dividend */
472 HOST_WIDE_INT lden_orig, hden_orig; /* den == denominator == divisor */
473 HOST_WIDE_INT *lquo, *hquo, *lrem, *hrem;
476 HOST_WIDE_INT num[4 + 1]; /* extra element for scaling. */
477 HOST_WIDE_INT den[4], quo[4];
479 unsigned HOST_WIDE_INT work;
480 register unsigned HOST_WIDE_INT carry = 0;
481 HOST_WIDE_INT lnum = lnum_orig;
482 HOST_WIDE_INT hnum = hnum_orig;
483 HOST_WIDE_INT lden = lden_orig;
484 HOST_WIDE_INT hden = hden_orig;
487 if ((hden == 0) && (lden == 0))
488 overflow = 1, lden = 1;
490 /* calculate quotient sign and convert operands to unsigned. */
496 /* (minimum integer) / (-1) is the only overflow case. */
497 if (neg_double (lnum, hnum, &lnum, &hnum) && (lden & hden) == -1)
503 neg_double (lden, hden, &lden, &hden);
507 if (hnum == 0 && hden == 0)
508 { /* single precision */
510 /* This unsigned division rounds toward zero. */
511 *lquo = lnum / (unsigned HOST_WIDE_INT) lden;
516 { /* trivial case: dividend < divisor */
517 /* hden != 0 already checked. */
524 bzero ((char *) quo, sizeof quo);
526 bzero ((char *) num, sizeof num); /* to zero 9th element */
527 bzero ((char *) den, sizeof den);
529 encode (num, lnum, hnum);
530 encode (den, lden, hden);
532 /* Special code for when the divisor < BASE. */
533 if (hden == 0 && lden < (HOST_WIDE_INT) BASE)
535 /* hnum != 0 already checked. */
536 for (i = 4 - 1; i >= 0; i--)
538 work = num[i] + carry * BASE;
539 quo[i] = work / (unsigned HOST_WIDE_INT) lden;
540 carry = work % (unsigned HOST_WIDE_INT) lden;
545 /* Full double precision division,
546 with thanks to Don Knuth's "Seminumerical Algorithms". */
547 int num_hi_sig, den_hi_sig;
548 unsigned HOST_WIDE_INT quo_est, scale;
550 /* Find the highest non-zero divisor digit. */
551 for (i = 4 - 1; ; i--)
557 /* Insure that the first digit of the divisor is at least BASE/2.
558 This is required by the quotient digit estimation algorithm. */
560 scale = BASE / (den[den_hi_sig] + 1);
561 if (scale > 1) { /* scale divisor and dividend */
563 for (i = 0; i <= 4 - 1; i++) {
564 work = (num[i] * scale) + carry;
565 num[i] = LOWPART (work);
566 carry = HIGHPART (work);
569 for (i = 0; i <= 4 - 1; i++) {
570 work = (den[i] * scale) + carry;
571 den[i] = LOWPART (work);
572 carry = HIGHPART (work);
573 if (den[i] != 0) den_hi_sig = i;
580 for (i = num_hi_sig - den_hi_sig - 1; i >= 0; i--) {
581 /* guess the next quotient digit, quo_est, by dividing the first
582 two remaining dividend digits by the high order quotient digit.
583 quo_est is never low and is at most 2 high. */
584 unsigned HOST_WIDE_INT tmp;
586 num_hi_sig = i + den_hi_sig + 1;
587 work = num[num_hi_sig] * BASE + num[num_hi_sig - 1];
588 if (num[num_hi_sig] != den[den_hi_sig])
589 quo_est = work / den[den_hi_sig];
593 /* refine quo_est so it's usually correct, and at most one high. */
594 tmp = work - quo_est * den[den_hi_sig];
596 && den[den_hi_sig - 1] * quo_est > (tmp * BASE + num[num_hi_sig - 2]))
599 /* Try QUO_EST as the quotient digit, by multiplying the
600 divisor by QUO_EST and subtracting from the remaining dividend.
601 Keep in mind that QUO_EST is the I - 1st digit. */
604 for (j = 0; j <= den_hi_sig; j++)
606 work = quo_est * den[j] + carry;
607 carry = HIGHPART (work);
608 work = num[i + j] - LOWPART (work);
609 num[i + j] = LOWPART (work);
610 carry += HIGHPART (work) != 0;
613 /* if quo_est was high by one, then num[i] went negative and
614 we need to correct things. */
616 if (num[num_hi_sig] < carry)
619 carry = 0; /* add divisor back in */
620 for (j = 0; j <= den_hi_sig; j++)
622 work = num[i + j] + den[j] + carry;
623 carry = HIGHPART (work);
624 num[i + j] = LOWPART (work);
626 num [num_hi_sig] += carry;
629 /* store the quotient digit. */
634 decode (quo, lquo, hquo);
637 /* if result is negative, make it so. */
639 neg_double (*lquo, *hquo, lquo, hquo);
641 /* compute trial remainder: rem = num - (quo * den) */
642 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
643 neg_double (*lrem, *hrem, lrem, hrem);
644 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
649 case TRUNC_MOD_EXPR: /* round toward zero */
650 case EXACT_DIV_EXPR: /* for this one, it shouldn't matter */
654 case FLOOR_MOD_EXPR: /* round toward negative infinity */
655 if (quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio < 0 && rem != 0 */
658 add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1,
661 else return overflow;
665 case CEIL_MOD_EXPR: /* round toward positive infinity */
666 if (!quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio > 0 && rem != 0 */
668 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
671 else return overflow;
675 case ROUND_MOD_EXPR: /* round to closest integer */
677 HOST_WIDE_INT labs_rem = *lrem, habs_rem = *hrem;
678 HOST_WIDE_INT labs_den = lden, habs_den = hden, ltwice, htwice;
680 /* get absolute values */
681 if (*hrem < 0) neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
682 if (hden < 0) neg_double (lden, hden, &labs_den, &habs_den);
684 /* if (2 * abs (lrem) >= abs (lden)) */
685 mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0,
686 labs_rem, habs_rem, <wice, &htwice);
687 if (((unsigned HOST_WIDE_INT) habs_den
688 < (unsigned HOST_WIDE_INT) htwice)
689 || (((unsigned HOST_WIDE_INT) habs_den
690 == (unsigned HOST_WIDE_INT) htwice)
691 && ((HOST_WIDE_INT unsigned) labs_den
692 < (unsigned HOST_WIDE_INT) ltwice)))
696 add_double (*lquo, *hquo,
697 (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
700 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
703 else return overflow;
711 /* compute true remainder: rem = num - (quo * den) */
712 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
713 neg_double (*lrem, *hrem, lrem, hrem);
714 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
718 #ifndef REAL_ARITHMETIC
719 /* Effectively truncate a real value to represent the nearest possible value
720 in a narrower mode. The result is actually represented in the same data
721 type as the argument, but its value is usually different.
723 A trap may occur during the FP operations and it is the responsibility
724 of the calling function to have a handler established. */
727 real_value_truncate (mode, arg)
728 enum machine_mode mode;
731 return REAL_VALUE_TRUNCATE (mode, arg);
734 #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
736 /* Check for infinity in an IEEE double precision number. */
742 /* The IEEE 64-bit double format. */
747 unsigned exponent : 11;
748 unsigned mantissa1 : 20;
753 unsigned mantissa1 : 20;
754 unsigned exponent : 11;
760 if (u.big_endian.sign == 1)
763 return (u.big_endian.exponent == 2047
764 && u.big_endian.mantissa1 == 0
765 && u.big_endian.mantissa2 == 0);
770 return (u.little_endian.exponent == 2047
771 && u.little_endian.mantissa1 == 0
772 && u.little_endian.mantissa2 == 0);
776 /* Check whether an IEEE double precision number is a NaN. */
782 /* The IEEE 64-bit double format. */
787 unsigned exponent : 11;
788 unsigned mantissa1 : 20;
793 unsigned mantissa1 : 20;
794 unsigned exponent : 11;
800 if (u.big_endian.sign == 1)
803 return (u.big_endian.exponent == 2047
804 && (u.big_endian.mantissa1 != 0
805 || u.big_endian.mantissa2 != 0));
810 return (u.little_endian.exponent == 2047
811 && (u.little_endian.mantissa1 != 0
812 || u.little_endian.mantissa2 != 0));
816 /* Check for a negative IEEE double precision number. */
822 /* The IEEE 64-bit double format. */
827 unsigned exponent : 11;
828 unsigned mantissa1 : 20;
833 unsigned mantissa1 : 20;
834 unsigned exponent : 11;
840 if (u.big_endian.sign == 1)
843 return u.big_endian.sign;
848 return u.little_endian.sign;
851 #else /* Target not IEEE */
853 /* Let's assume other float formats don't have infinity.
854 (This can be overridden by redefining REAL_VALUE_ISINF.) */
862 /* Let's assume other float formats don't have NaNs.
863 (This can be overridden by redefining REAL_VALUE_ISNAN.) */
871 /* Let's assume other float formats don't have minus zero.
872 (This can be overridden by redefining REAL_VALUE_NEGATIVE.) */
879 #endif /* Target not IEEE */
881 /* Try to change R into its exact multiplicative inverse in machine mode
882 MODE. Return nonzero function value if successful. */
885 exact_real_inverse (mode, r)
886 enum machine_mode mode;
896 /* Usually disable if bounds checks are not reliable. */
897 if ((HOST_FLOAT_FORMAT != TARGET_FLOAT_FORMAT) && !flag_pretend_float)
900 /* Set array index to the less significant bits in the unions, depending
901 on the endian-ness of the host doubles.
902 Disable if insufficient information on the data structure. */
903 #if HOST_FLOAT_FORMAT == UNKNOWN_FLOAT_FORMAT
906 #if HOST_FLOAT_FORMAT == VAX_FLOAT_FORMAT
909 #if HOST_FLOAT_FORMAT == IBM_FLOAT_FORMAT
912 #define K (2 * HOST_FLOAT_WORDS_BIG_ENDIAN)
917 if (setjmp (float_error))
919 /* Don't do the optimization if there was an arithmetic error. */
921 set_float_handler (NULL_PTR);
924 set_float_handler (float_error);
926 /* Domain check the argument. */
932 if (REAL_VALUE_ISINF (x.d) || REAL_VALUE_ISNAN (x.d))
936 /* Compute the reciprocal and check for numerical exactness.
937 It is unnecessary to check all the significand bits to determine
938 whether X is a power of 2. If X is not, then it is impossible for
939 the bottom half significand of both X and 1/X to be all zero bits.
940 Hence we ignore the data structure of the top half and examine only
941 the low order bits of the two significands. */
943 if (x.i[K] != 0 || x.i[K + 1] != 0 || t.i[K] != 0 || t.i[K + 1] != 0)
946 /* Truncate to the required mode and range-check the result. */
947 y.d = REAL_VALUE_TRUNCATE (mode, t.d);
948 #ifdef CHECK_FLOAT_VALUE
950 if (CHECK_FLOAT_VALUE (mode, y.d, i))
954 /* Fail if truncation changed the value. */
955 if (y.d != t.d || y.d == 0.0)
959 if (REAL_VALUE_ISINF (y.d) || REAL_VALUE_ISNAN (y.d))
963 /* Output the reciprocal and return success flag. */
964 set_float_handler (NULL_PTR);
970 /* Convert C9X hexadecimal floating point string constant S. Return
971 real value type in mode MODE. This function uses the host computer's
972 fp arithmetic when there is no REAL_ARITHMETIC. */
975 real_hex_to_f (s, mode)
977 enum machine_mode mode;
981 unsigned HOST_WIDE_INT low, high;
982 int frexpon, expon, shcount, nrmcount, k;
983 int sign, expsign, decpt, isfloat, isldouble, gotp, lost;
993 while (*p == ' ' || *p == '\t')
996 /* Sign, if any, comes first. */
1004 /* The string is supposed to start with 0x or 0X . */
1008 if (*p == 'x' || *p == 'X')
1021 lost = 0; /* Nonzero low order bits shifted out and discarded. */
1022 frexpon = 0; /* Bits after the decimal point. */
1023 expon = 0; /* Value of exponent. */
1024 decpt = 0; /* How many decimal points. */
1025 gotp = 0; /* How many P's. */
1027 while ((c = *p) != '\0')
1029 if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F')
1030 || (c >= 'a' && c <= 'f'))
1040 if ((high & 0xf0000000) == 0)
1042 high = (high << 4) + ((low >> 28) & 15);
1043 low = (low << 4) + k;
1050 /* Record nonzero lost bits. */
1062 else if (c == 'p' || c == 'P')
1066 /* Sign of exponent. */
1072 /* Value of exponent.
1073 The exponent field is a decimal integer. */
1076 k = (*p++ & 0x7f) - '0';
1077 expon = 10 * expon + k;
1080 /* F suffix is ambiguous in the significand part
1081 so it must appear after the decimal exponent field. */
1082 if (*p == 'f' || *p == 'F')
1089 else if (c == 'l' || c == 'L')
1098 /* Abort if last character read was not legitimate. */
1100 if ((c != '\0' && c != ' ' && c != '\n' && c != '\r') || (decpt > 1))
1102 /* There must be either one decimal point or one p. */
1103 if (decpt == 0 && gotp == 0)
1106 if ((high == 0) && (low == 0))
1119 /* Leave a high guard bit for carry-out. */
1120 if ((high & 0x80000000) != 0)
1123 low = (low >> 1) | (high << 31);
1127 if ((high & 0xffff8000) == 0)
1129 high = (high << 16) + ((low >> 16) & 0xffff);
1133 while ((high & 0xc0000000) == 0)
1135 high = (high << 1) + ((low >> 31) & 1);
1139 if (isfloat || GET_MODE_SIZE(mode) == UNITS_PER_WORD)
1141 /* Keep 24 bits precision, bits 0x7fffff80.
1142 Rounding bit is 0x40. */
1143 lost = lost | low | (high & 0x3f);
1147 if ((high & 0x80) || lost)
1154 /* We need real.c to do long double formats, so here default
1155 to double precision. */
1156 #if HOST_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
1158 Keep 53 bits precision, bits 0x7fffffff fffffc00.
1159 Rounding bit is low word 0x200. */
1160 lost = lost | (low & 0x1ff);
1163 if ((low & 0x400) || lost)
1165 low = (low + 0x200) & 0xfffffc00;
1172 /* Assume it's a VAX with 56-bit significand,
1173 bits 0x7fffffff ffffff80. */
1174 lost = lost | (low & 0x7f);
1177 if ((low & 0x80) || lost)
1179 low = (low + 0x40) & 0xffffff80;
1188 ip = REAL_VALUE_LDEXP (ip, 32) + (double) low;
1189 /* Apply shifts and exponent value as power of 2. */
1190 ip = REAL_VALUE_LDEXP (ip, expon - (nrmcount + frexpon));
1197 #endif /* no REAL_ARITHMETIC */
1199 /* Split a tree IN into a constant and a variable part
1200 that could be combined with CODE to make IN.
1201 CODE must be a commutative arithmetic operation.
1202 Store the constant part into *CONP and the variable in &VARP.
1203 Return 1 if this was done; zero means the tree IN did not decompose
1206 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR.
1207 Therefore, we must tell the caller whether the variable part
1208 was subtracted. We do this by storing 1 or -1 into *VARSIGNP.
1209 The value stored is the coefficient for the variable term.
1210 The constant term we return should always be added;
1211 we negate it if necessary. */
1214 split_tree (in, code, varp, conp, varsignp)
1216 enum tree_code code;
1220 register tree outtype = TREE_TYPE (in);
1224 /* Strip any conversions that don't change the machine mode. */
1225 while ((TREE_CODE (in) == NOP_EXPR
1226 || TREE_CODE (in) == CONVERT_EXPR)
1227 && (TYPE_MODE (TREE_TYPE (in))
1228 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (in, 0)))))
1229 in = TREE_OPERAND (in, 0);
1231 if (TREE_CODE (in) == code
1232 || (! FLOAT_TYPE_P (TREE_TYPE (in))
1233 /* We can associate addition and subtraction together
1234 (even though the C standard doesn't say so)
1235 for integers because the value is not affected.
1236 For reals, the value might be affected, so we can't. */
1237 && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
1238 || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
1240 enum tree_code code = TREE_CODE (TREE_OPERAND (in, 0));
1241 if (code == INTEGER_CST)
1243 *conp = TREE_OPERAND (in, 0);
1244 *varp = TREE_OPERAND (in, 1);
1245 if (TYPE_MODE (TREE_TYPE (*varp)) != TYPE_MODE (outtype)
1246 && TREE_TYPE (*varp) != outtype)
1247 *varp = convert (outtype, *varp);
1248 *varsignp = (TREE_CODE (in) == MINUS_EXPR) ? -1 : 1;
1251 if (TREE_CONSTANT (TREE_OPERAND (in, 1)))
1253 *conp = TREE_OPERAND (in, 1);
1254 *varp = TREE_OPERAND (in, 0);
1256 if (TYPE_MODE (TREE_TYPE (*varp)) != TYPE_MODE (outtype)
1257 && TREE_TYPE (*varp) != outtype)
1258 *varp = convert (outtype, *varp);
1259 if (TREE_CODE (in) == MINUS_EXPR)
1261 /* If operation is subtraction and constant is second,
1262 must negate it to get an additive constant.
1263 And this cannot be done unless it is a manifest constant.
1264 It could also be the address of a static variable.
1265 We cannot negate that, so give up. */
1266 if (TREE_CODE (*conp) == INTEGER_CST)
1267 /* Subtracting from integer_zero_node loses for long long. */
1268 *conp = fold (build1 (NEGATE_EXPR, TREE_TYPE (*conp), *conp));
1274 if (TREE_CONSTANT (TREE_OPERAND (in, 0)))
1276 *conp = TREE_OPERAND (in, 0);
1277 *varp = TREE_OPERAND (in, 1);
1278 if (TYPE_MODE (TREE_TYPE (*varp)) != TYPE_MODE (outtype)
1279 && TREE_TYPE (*varp) != outtype)
1280 *varp = convert (outtype, *varp);
1281 *varsignp = (TREE_CODE (in) == MINUS_EXPR) ? -1 : 1;
1288 /* Combine two integer constants ARG1 and ARG2 under operation CODE
1289 to produce a new constant.
1291 If NOTRUNC is nonzero, do not truncate the result to fit the data type.
1292 If FORSIZE is nonzero, compute overflow for unsigned types. */
1295 int_const_binop (code, arg1, arg2, notrunc, forsize)
1296 enum tree_code code;
1297 register tree arg1, arg2;
1298 int notrunc, forsize;
1300 HOST_WIDE_INT int1l, int1h, int2l, int2h;
1301 HOST_WIDE_INT low, hi;
1302 HOST_WIDE_INT garbagel, garbageh;
1304 int uns = TREE_UNSIGNED (TREE_TYPE (arg1));
1306 int no_overflow = 0;
1308 int1l = TREE_INT_CST_LOW (arg1);
1309 int1h = TREE_INT_CST_HIGH (arg1);
1310 int2l = TREE_INT_CST_LOW (arg2);
1311 int2h = TREE_INT_CST_HIGH (arg2);
1316 low = int1l | int2l, hi = int1h | int2h;
1320 low = int1l ^ int2l, hi = int1h ^ int2h;
1324 low = int1l & int2l, hi = int1h & int2h;
1327 case BIT_ANDTC_EXPR:
1328 low = int1l & ~int2l, hi = int1h & ~int2h;
1334 /* It's unclear from the C standard whether shifts can overflow.
1335 The following code ignores overflow; perhaps a C standard
1336 interpretation ruling is needed. */
1337 lshift_double (int1l, int1h, int2l,
1338 TYPE_PRECISION (TREE_TYPE (arg1)),
1347 lrotate_double (int1l, int1h, int2l,
1348 TYPE_PRECISION (TREE_TYPE (arg1)),
1353 overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
1357 neg_double (int2l, int2h, &low, &hi);
1358 add_double (int1l, int1h, low, hi, &low, &hi);
1359 overflow = overflow_sum_sign (hi, int2h, int1h);
1363 overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
1366 case TRUNC_DIV_EXPR:
1367 case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1368 case EXACT_DIV_EXPR:
1369 /* This is a shortcut for a common special case. */
1370 if (int2h == 0 && int2l > 0
1371 && ! TREE_CONSTANT_OVERFLOW (arg1)
1372 && ! TREE_CONSTANT_OVERFLOW (arg2)
1373 && int1h == 0 && int1l >= 0)
1375 if (code == CEIL_DIV_EXPR)
1377 low = int1l / int2l, hi = 0;
1381 /* ... fall through ... */
1383 case ROUND_DIV_EXPR:
1384 if (int2h == 0 && int2l == 1)
1386 low = int1l, hi = int1h;
1389 if (int1l == int2l && int1h == int2h
1390 && ! (int1l == 0 && int1h == 0))
1395 overflow = div_and_round_double (code, uns,
1396 int1l, int1h, int2l, int2h,
1397 &low, &hi, &garbagel, &garbageh);
1400 case TRUNC_MOD_EXPR:
1401 case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1402 /* This is a shortcut for a common special case. */
1403 if (int2h == 0 && int2l > 0
1404 && ! TREE_CONSTANT_OVERFLOW (arg1)
1405 && ! TREE_CONSTANT_OVERFLOW (arg2)
1406 && int1h == 0 && int1l >= 0)
1408 if (code == CEIL_MOD_EXPR)
1410 low = int1l % int2l, hi = 0;
1414 /* ... fall through ... */
1416 case ROUND_MOD_EXPR:
1417 overflow = div_and_round_double (code, uns,
1418 int1l, int1h, int2l, int2h,
1419 &garbagel, &garbageh, &low, &hi);
1426 low = (((unsigned HOST_WIDE_INT) int1h
1427 < (unsigned HOST_WIDE_INT) int2h)
1428 || (((unsigned HOST_WIDE_INT) int1h
1429 == (unsigned HOST_WIDE_INT) int2h)
1430 && ((unsigned HOST_WIDE_INT) int1l
1431 < (unsigned HOST_WIDE_INT) int2l)));
1435 low = ((int1h < int2h)
1436 || ((int1h == int2h)
1437 && ((unsigned HOST_WIDE_INT) int1l
1438 < (unsigned HOST_WIDE_INT) int2l)));
1440 if (low == (code == MIN_EXPR))
1441 low = int1l, hi = int1h;
1443 low = int2l, hi = int2h;
1450 if (TREE_TYPE (arg1) == sizetype && hi == 0
1452 && (TYPE_MAX_VALUE (sizetype) == NULL
1453 || low <= TREE_INT_CST_LOW (TYPE_MAX_VALUE (sizetype)))
1455 && ! TREE_OVERFLOW (arg1) && ! TREE_OVERFLOW (arg2))
1459 t = build_int_2 (low, hi);
1460 TREE_TYPE (t) = TREE_TYPE (arg1);
1464 = ((notrunc ? (!uns || forsize) && overflow
1465 : force_fit_type (t, (!uns || forsize) && overflow) && ! no_overflow)
1466 | TREE_OVERFLOW (arg1)
1467 | TREE_OVERFLOW (arg2));
1468 /* If we're doing a size calculation, unsigned arithmetic does overflow.
1469 So check if force_fit_type truncated the value. */
1471 && ! TREE_OVERFLOW (t)
1472 && (TREE_INT_CST_HIGH (t) != hi
1473 || TREE_INT_CST_LOW (t) != low))
1474 TREE_OVERFLOW (t) = 1;
1475 TREE_CONSTANT_OVERFLOW (t) = (TREE_OVERFLOW (t)
1476 | TREE_CONSTANT_OVERFLOW (arg1)
1477 | TREE_CONSTANT_OVERFLOW (arg2));
1481 /* Combine two constants ARG1 and ARG2 under operation CODE
1482 to produce a new constant.
1483 We assume ARG1 and ARG2 have the same data type,
1484 or at least are the same kind of constant and the same machine mode.
1486 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
1489 const_binop (code, arg1, arg2, notrunc)
1490 enum tree_code code;
1491 register tree arg1, arg2;
1494 STRIP_NOPS (arg1); STRIP_NOPS (arg2);
1496 if (TREE_CODE (arg1) == INTEGER_CST)
1497 return int_const_binop (code, arg1, arg2, notrunc, 0);
1499 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1500 if (TREE_CODE (arg1) == REAL_CST)
1505 REAL_VALUE_TYPE value;
1508 d1 = TREE_REAL_CST (arg1);
1509 d2 = TREE_REAL_CST (arg2);
1511 /* If either operand is a NaN, just return it. Otherwise, set up
1512 for floating-point trap; we return an overflow. */
1513 if (REAL_VALUE_ISNAN (d1))
1515 else if (REAL_VALUE_ISNAN (d2))
1517 else if (setjmp (float_error))
1519 t = copy_node (arg1);
1524 set_float_handler (float_error);
1526 #ifdef REAL_ARITHMETIC
1527 REAL_ARITHMETIC (value, code, d1, d2);
1544 #ifndef REAL_INFINITY
1553 value = MIN (d1, d2);
1557 value = MAX (d1, d2);
1563 #endif /* no REAL_ARITHMETIC */
1564 t = build_real (TREE_TYPE (arg1),
1565 real_value_truncate (TYPE_MODE (TREE_TYPE (arg1)), value));
1567 set_float_handler (NULL_PTR);
1570 = (force_fit_type (t, overflow)
1571 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1572 TREE_CONSTANT_OVERFLOW (t)
1574 | TREE_CONSTANT_OVERFLOW (arg1)
1575 | TREE_CONSTANT_OVERFLOW (arg2);
1578 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
1579 if (TREE_CODE (arg1) == COMPLEX_CST)
1581 register tree type = TREE_TYPE (arg1);
1582 register tree r1 = TREE_REALPART (arg1);
1583 register tree i1 = TREE_IMAGPART (arg1);
1584 register tree r2 = TREE_REALPART (arg2);
1585 register tree i2 = TREE_IMAGPART (arg2);
1591 t = build_complex (type,
1592 const_binop (PLUS_EXPR, r1, r2, notrunc),
1593 const_binop (PLUS_EXPR, i1, i2, notrunc));
1597 t = build_complex (type,
1598 const_binop (MINUS_EXPR, r1, r2, notrunc),
1599 const_binop (MINUS_EXPR, i1, i2, notrunc));
1603 t = build_complex (type,
1604 const_binop (MINUS_EXPR,
1605 const_binop (MULT_EXPR,
1607 const_binop (MULT_EXPR,
1610 const_binop (PLUS_EXPR,
1611 const_binop (MULT_EXPR,
1613 const_binop (MULT_EXPR,
1620 register tree magsquared
1621 = const_binop (PLUS_EXPR,
1622 const_binop (MULT_EXPR, r2, r2, notrunc),
1623 const_binop (MULT_EXPR, i2, i2, notrunc),
1626 t = build_complex (type,
1628 (INTEGRAL_TYPE_P (TREE_TYPE (r1))
1629 ? TRUNC_DIV_EXPR : RDIV_EXPR,
1630 const_binop (PLUS_EXPR,
1631 const_binop (MULT_EXPR, r1, r2,
1633 const_binop (MULT_EXPR, i1, i2,
1636 magsquared, notrunc),
1638 (INTEGRAL_TYPE_P (TREE_TYPE (r1))
1639 ? TRUNC_DIV_EXPR : RDIV_EXPR,
1640 const_binop (MINUS_EXPR,
1641 const_binop (MULT_EXPR, i1, r2,
1643 const_binop (MULT_EXPR, r1, i2,
1646 magsquared, notrunc));
1658 /* Return an INTEGER_CST with value V . The type is determined by bit_p:
1659 if it is zero, the type is taken from sizetype; if it is one, the type
1660 is taken from bitsizetype. */
1663 size_int_wide (number, high, bit_p)
1664 unsigned HOST_WIDE_INT number, high;
1668 /* Type-size nodes already made for small sizes. */
1669 static tree size_table[2*HOST_BITS_PER_WIDE_INT + 1][2];
1671 if (number < 2*HOST_BITS_PER_WIDE_INT + 1 && ! high
1672 && size_table[number][bit_p] != 0)
1673 return size_table[number][bit_p];
1674 if (number < 2*HOST_BITS_PER_WIDE_INT + 1 && ! high)
1676 push_obstacks_nochange ();
1677 /* Make this a permanent node. */
1678 end_temporary_allocation ();
1679 t = build_int_2 (number, 0);
1680 TREE_TYPE (t) = bit_p ? bitsizetype : sizetype;
1681 size_table[number][bit_p] = t;
1686 t = build_int_2 (number, high);
1687 TREE_TYPE (t) = bit_p ? bitsizetype : sizetype;
1688 TREE_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (t) = force_fit_type (t, 0);
1693 /* Combine operands OP1 and OP2 with arithmetic operation CODE.
1694 CODE is a tree code. Data type is taken from `sizetype',
1695 If the operands are constant, so is the result. */
1698 size_binop (code, arg0, arg1)
1699 enum tree_code code;
1702 /* Handle the special case of two integer constants faster. */
1703 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1705 /* And some specific cases even faster than that. */
1706 if (code == PLUS_EXPR && integer_zerop (arg0))
1708 else if ((code == MINUS_EXPR || code == PLUS_EXPR)
1709 && integer_zerop (arg1))
1711 else if (code == MULT_EXPR && integer_onep (arg0))
1714 /* Handle general case of two integer constants. */
1715 return int_const_binop (code, arg0, arg1, 0, 1);
1718 if (arg0 == error_mark_node || arg1 == error_mark_node)
1719 return error_mark_node;
1721 return fold (build (code, sizetype, arg0, arg1));
1724 /* Combine operands OP1 and OP2 with arithmetic operation CODE.
1725 CODE is a tree code. Data type is taken from `ssizetype',
1726 If the operands are constant, so is the result. */
1729 ssize_binop (code, arg0, arg1)
1730 enum tree_code code;
1733 /* Handle the special case of two integer constants faster. */
1734 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1736 /* And some specific cases even faster than that. */
1737 if (code == PLUS_EXPR && integer_zerop (arg0))
1739 else if ((code == MINUS_EXPR || code == PLUS_EXPR)
1740 && integer_zerop (arg1))
1742 else if (code == MULT_EXPR && integer_onep (arg0))
1745 /* Handle general case of two integer constants. We convert
1746 arg0 to ssizetype because int_const_binop uses its type for the
1748 arg0 = convert (ssizetype, arg0);
1749 return int_const_binop (code, arg0, arg1, 0, 0);
1752 if (arg0 == error_mark_node || arg1 == error_mark_node)
1753 return error_mark_node;
1755 return fold (build (code, ssizetype, arg0, arg1));
1758 /* Given T, a tree representing type conversion of ARG1, a constant,
1759 return a constant tree representing the result of conversion. */
1762 fold_convert (t, arg1)
1766 register tree type = TREE_TYPE (t);
1769 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
1771 if (TREE_CODE (arg1) == INTEGER_CST)
1773 /* If we would build a constant wider than GCC supports,
1774 leave the conversion unfolded. */
1775 if (TYPE_PRECISION (type) > 2 * HOST_BITS_PER_WIDE_INT)
1778 /* Given an integer constant, make new constant with new type,
1779 appropriately sign-extended or truncated. */
1780 t = build_int_2 (TREE_INT_CST_LOW (arg1),
1781 TREE_INT_CST_HIGH (arg1));
1782 TREE_TYPE (t) = type;
1783 /* Indicate an overflow if (1) ARG1 already overflowed,
1784 or (2) force_fit_type indicates an overflow.
1785 Tell force_fit_type that an overflow has already occurred
1786 if ARG1 is a too-large unsigned value and T is signed.
1787 But don't indicate an overflow if converting a pointer. */
1789 = ((force_fit_type (t,
1790 (TREE_INT_CST_HIGH (arg1) < 0
1791 && (TREE_UNSIGNED (type)
1792 < TREE_UNSIGNED (TREE_TYPE (arg1)))))
1793 && ! POINTER_TYPE_P (TREE_TYPE (arg1)))
1794 || TREE_OVERFLOW (arg1));
1795 TREE_CONSTANT_OVERFLOW (t)
1796 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
1798 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1799 else if (TREE_CODE (arg1) == REAL_CST)
1801 /* Don't initialize these, use assignments.
1802 Initialized local aggregates don't work on old compilers. */
1806 tree type1 = TREE_TYPE (arg1);
1809 x = TREE_REAL_CST (arg1);
1810 l = real_value_from_int_cst (type1, TYPE_MIN_VALUE (type));
1812 no_upper_bound = (TYPE_MAX_VALUE (type) == NULL);
1813 if (!no_upper_bound)
1814 u = real_value_from_int_cst (type1, TYPE_MAX_VALUE (type));
1816 /* See if X will be in range after truncation towards 0.
1817 To compensate for truncation, move the bounds away from 0,
1818 but reject if X exactly equals the adjusted bounds. */
1819 #ifdef REAL_ARITHMETIC
1820 REAL_ARITHMETIC (l, MINUS_EXPR, l, dconst1);
1821 if (!no_upper_bound)
1822 REAL_ARITHMETIC (u, PLUS_EXPR, u, dconst1);
1825 if (!no_upper_bound)
1828 /* If X is a NaN, use zero instead and show we have an overflow.
1829 Otherwise, range check. */
1830 if (REAL_VALUE_ISNAN (x))
1831 overflow = 1, x = dconst0;
1832 else if (! (REAL_VALUES_LESS (l, x)
1834 && REAL_VALUES_LESS (x, u)))
1837 #ifndef REAL_ARITHMETIC
1839 HOST_WIDE_INT low, high;
1840 HOST_WIDE_INT half_word
1841 = (HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2);
1846 high = (HOST_WIDE_INT) (x / half_word / half_word);
1847 x -= (REAL_VALUE_TYPE) high * half_word * half_word;
1848 if (x >= (REAL_VALUE_TYPE) half_word * half_word / 2)
1850 low = x - (REAL_VALUE_TYPE) half_word * half_word / 2;
1851 low |= (HOST_WIDE_INT) -1 << (HOST_BITS_PER_WIDE_INT - 1);
1854 low = (HOST_WIDE_INT) x;
1855 if (TREE_REAL_CST (arg1) < 0)
1856 neg_double (low, high, &low, &high);
1857 t = build_int_2 (low, high);
1861 HOST_WIDE_INT low, high;
1862 REAL_VALUE_TO_INT (&low, &high, x);
1863 t = build_int_2 (low, high);
1866 TREE_TYPE (t) = type;
1868 = TREE_OVERFLOW (arg1) | force_fit_type (t, overflow);
1869 TREE_CONSTANT_OVERFLOW (t)
1870 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
1872 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
1873 TREE_TYPE (t) = type;
1875 else if (TREE_CODE (type) == REAL_TYPE)
1877 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1878 if (TREE_CODE (arg1) == INTEGER_CST)
1879 return build_real_from_int_cst (type, arg1);
1880 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
1881 if (TREE_CODE (arg1) == REAL_CST)
1883 if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1)))
1886 TREE_TYPE (arg1) = type;
1889 else if (setjmp (float_error))
1892 t = copy_node (arg1);
1895 set_float_handler (float_error);
1897 t = build_real (type, real_value_truncate (TYPE_MODE (type),
1898 TREE_REAL_CST (arg1)));
1899 set_float_handler (NULL_PTR);
1903 = TREE_OVERFLOW (arg1) | force_fit_type (t, overflow);
1904 TREE_CONSTANT_OVERFLOW (t)
1905 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
1909 TREE_CONSTANT (t) = 1;
1913 /* Return an expr equal to X but certainly not valid as an lvalue. */
1921 /* These things are certainly not lvalues. */
1922 if (TREE_CODE (x) == NON_LVALUE_EXPR
1923 || TREE_CODE (x) == INTEGER_CST
1924 || TREE_CODE (x) == REAL_CST
1925 || TREE_CODE (x) == STRING_CST
1926 || TREE_CODE (x) == ADDR_EXPR)
1929 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
1930 TREE_CONSTANT (result) = TREE_CONSTANT (x);
1934 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
1935 Zero means allow extended lvalues. */
1937 int pedantic_lvalues;
1939 /* When pedantic, return an expr equal to X but certainly not valid as a
1940 pedantic lvalue. Otherwise, return X. */
1943 pedantic_non_lvalue (x)
1946 if (pedantic_lvalues)
1947 return non_lvalue (x);
1952 /* Given a tree comparison code, return the code that is the logical inverse
1953 of the given code. It is not safe to do this for floating-point
1954 comparisons, except for NE_EXPR and EQ_EXPR. */
1956 static enum tree_code
1957 invert_tree_comparison (code)
1958 enum tree_code code;
1979 /* Similar, but return the comparison that results if the operands are
1980 swapped. This is safe for floating-point. */
1982 static enum tree_code
1983 swap_tree_comparison (code)
1984 enum tree_code code;
2004 /* Return nonzero if CODE is a tree code that represents a truth value. */
2007 truth_value_p (code)
2008 enum tree_code code;
2010 return (TREE_CODE_CLASS (code) == '<'
2011 || code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
2012 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
2013 || code == TRUTH_XOR_EXPR || code == TRUTH_NOT_EXPR);
2016 /* Return nonzero if two operands are necessarily equal.
2017 If ONLY_CONST is non-zero, only return non-zero for constants.
2018 This function tests whether the operands are indistinguishable;
2019 it does not test whether they are equal using C's == operation.
2020 The distinction is important for IEEE floating point, because
2021 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2022 (2) two NaNs may be indistinguishable, but NaN!=NaN. */
2025 operand_equal_p (arg0, arg1, only_const)
2029 /* If both types don't have the same signedness, then we can't consider
2030 them equal. We must check this before the STRIP_NOPS calls
2031 because they may change the signedness of the arguments. */
2032 if (TREE_UNSIGNED (TREE_TYPE (arg0)) != TREE_UNSIGNED (TREE_TYPE (arg1)))
2038 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2039 /* This is needed for conversions and for COMPONENT_REF.
2040 Might as well play it safe and always test this. */
2041 || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
2044 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2045 We don't care about side effects in that case because the SAVE_EXPR
2046 takes care of that for us. In all other cases, two expressions are
2047 equal if they have no side effects. If we have two identical
2048 expressions with side effects that should be treated the same due
2049 to the only side effects being identical SAVE_EXPR's, that will
2050 be detected in the recursive calls below. */
2051 if (arg0 == arg1 && ! only_const
2052 && (TREE_CODE (arg0) == SAVE_EXPR
2053 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2056 /* Next handle constant cases, those for which we can return 1 even
2057 if ONLY_CONST is set. */
2058 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2059 switch (TREE_CODE (arg0))
2062 return (! TREE_CONSTANT_OVERFLOW (arg0)
2063 && ! TREE_CONSTANT_OVERFLOW (arg1)
2064 && TREE_INT_CST_LOW (arg0) == TREE_INT_CST_LOW (arg1)
2065 && TREE_INT_CST_HIGH (arg0) == TREE_INT_CST_HIGH (arg1));
2068 return (! TREE_CONSTANT_OVERFLOW (arg0)
2069 && ! TREE_CONSTANT_OVERFLOW (arg1)
2070 && REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
2071 TREE_REAL_CST (arg1)));
2074 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2076 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2080 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2081 && ! strncmp (TREE_STRING_POINTER (arg0),
2082 TREE_STRING_POINTER (arg1),
2083 TREE_STRING_LENGTH (arg0)));
2086 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2095 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2098 /* Two conversions are equal only if signedness and modes match. */
2099 if ((TREE_CODE (arg0) == NOP_EXPR || TREE_CODE (arg0) == CONVERT_EXPR)
2100 && (TREE_UNSIGNED (TREE_TYPE (arg0))
2101 != TREE_UNSIGNED (TREE_TYPE (arg1))))
2104 return operand_equal_p (TREE_OPERAND (arg0, 0),
2105 TREE_OPERAND (arg1, 0), 0);
2109 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0)
2110 && operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1),
2114 /* For commutative ops, allow the other order. */
2115 return ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MULT_EXPR
2116 || TREE_CODE (arg0) == MIN_EXPR || TREE_CODE (arg0) == MAX_EXPR
2117 || TREE_CODE (arg0) == BIT_IOR_EXPR
2118 || TREE_CODE (arg0) == BIT_XOR_EXPR
2119 || TREE_CODE (arg0) == BIT_AND_EXPR
2120 || TREE_CODE (arg0) == NE_EXPR || TREE_CODE (arg0) == EQ_EXPR)
2121 && operand_equal_p (TREE_OPERAND (arg0, 0),
2122 TREE_OPERAND (arg1, 1), 0)
2123 && operand_equal_p (TREE_OPERAND (arg0, 1),
2124 TREE_OPERAND (arg1, 0), 0));
2127 switch (TREE_CODE (arg0))
2130 return operand_equal_p (TREE_OPERAND (arg0, 0),
2131 TREE_OPERAND (arg1, 0), 0);
2135 return (operand_equal_p (TREE_OPERAND (arg0, 0),
2136 TREE_OPERAND (arg1, 0), 0)
2137 && operand_equal_p (TREE_OPERAND (arg0, 1),
2138 TREE_OPERAND (arg1, 1), 0));
2141 return (operand_equal_p (TREE_OPERAND (arg0, 0),
2142 TREE_OPERAND (arg1, 0), 0)
2143 && operand_equal_p (TREE_OPERAND (arg0, 1),
2144 TREE_OPERAND (arg1, 1), 0)
2145 && operand_equal_p (TREE_OPERAND (arg0, 2),
2146 TREE_OPERAND (arg1, 2), 0));
2152 if (TREE_CODE (arg0) == RTL_EXPR)
2153 return rtx_equal_p (RTL_EXPR_RTL (arg0), RTL_EXPR_RTL (arg1));
2161 /* Similar to operand_equal_p, but see if ARG0 might have been made by
2162 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
2164 When in doubt, return 0. */
2167 operand_equal_for_comparison_p (arg0, arg1, other)
2171 int unsignedp1, unsignedpo;
2172 tree primarg0, primarg1, primother;
2173 unsigned correct_width;
2175 if (operand_equal_p (arg0, arg1, 0))
2178 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
2179 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
2182 /* Discard any conversions that don't change the modes of ARG0 and ARG1
2183 and see if the inner values are the same. This removes any
2184 signedness comparison, which doesn't matter here. */
2185 primarg0 = arg0, primarg1 = arg1;
2186 STRIP_NOPS (primarg0); STRIP_NOPS (primarg1);
2187 if (operand_equal_p (primarg0, primarg1, 0))
2190 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
2191 actual comparison operand, ARG0.
2193 First throw away any conversions to wider types
2194 already present in the operands. */
2196 primarg1 = get_narrower (arg1, &unsignedp1);
2197 primother = get_narrower (other, &unsignedpo);
2199 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
2200 if (unsignedp1 == unsignedpo
2201 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
2202 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
2204 tree type = TREE_TYPE (arg0);
2206 /* Make sure shorter operand is extended the right way
2207 to match the longer operand. */
2208 primarg1 = convert (signed_or_unsigned_type (unsignedp1,
2209 TREE_TYPE (primarg1)),
2212 if (operand_equal_p (arg0, convert (type, primarg1), 0))
2219 /* See if ARG is an expression that is either a comparison or is performing
2220 arithmetic on comparisons. The comparisons must only be comparing
2221 two different values, which will be stored in *CVAL1 and *CVAL2; if
2222 they are non-zero it means that some operands have already been found.
2223 No variables may be used anywhere else in the expression except in the
2224 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
2225 the expression and save_expr needs to be called with CVAL1 and CVAL2.
2227 If this is true, return 1. Otherwise, return zero. */
2230 twoval_comparison_p (arg, cval1, cval2, save_p)
2232 tree *cval1, *cval2;
2235 enum tree_code code = TREE_CODE (arg);
2236 char class = TREE_CODE_CLASS (code);
2238 /* We can handle some of the 'e' cases here. */
2239 if (class == 'e' && code == TRUTH_NOT_EXPR)
2241 else if (class == 'e'
2242 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
2243 || code == COMPOUND_EXPR))
2246 /* ??? Disable this since the SAVE_EXPR might already be in use outside
2247 the expression. There may be no way to make this work, but it needs
2248 to be looked at again for 2.6. */
2250 else if (class == 'e' && code == SAVE_EXPR && SAVE_EXPR_RTL (arg) == 0)
2252 /* If we've already found a CVAL1 or CVAL2, this expression is
2253 two complex to handle. */
2254 if (*cval1 || *cval2)
2265 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
2268 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
2269 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2270 cval1, cval2, save_p));
2276 if (code == COND_EXPR)
2277 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
2278 cval1, cval2, save_p)
2279 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2280 cval1, cval2, save_p)
2281 && twoval_comparison_p (TREE_OPERAND (arg, 2),
2282 cval1, cval2, save_p));
2286 /* First see if we can handle the first operand, then the second. For
2287 the second operand, we know *CVAL1 can't be zero. It must be that
2288 one side of the comparison is each of the values; test for the
2289 case where this isn't true by failing if the two operands
2292 if (operand_equal_p (TREE_OPERAND (arg, 0),
2293 TREE_OPERAND (arg, 1), 0))
2297 *cval1 = TREE_OPERAND (arg, 0);
2298 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
2300 else if (*cval2 == 0)
2301 *cval2 = TREE_OPERAND (arg, 0);
2302 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
2307 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
2309 else if (*cval2 == 0)
2310 *cval2 = TREE_OPERAND (arg, 1);
2311 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
2323 /* ARG is a tree that is known to contain just arithmetic operations and
2324 comparisons. Evaluate the operations in the tree substituting NEW0 for
2325 any occurrence of OLD0 as an operand of a comparison and likewise for
2329 eval_subst (arg, old0, new0, old1, new1)
2331 tree old0, new0, old1, new1;
2333 tree type = TREE_TYPE (arg);
2334 enum tree_code code = TREE_CODE (arg);
2335 char class = TREE_CODE_CLASS (code);
2337 /* We can handle some of the 'e' cases here. */
2338 if (class == 'e' && code == TRUTH_NOT_EXPR)
2340 else if (class == 'e'
2341 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2347 return fold (build1 (code, type,
2348 eval_subst (TREE_OPERAND (arg, 0),
2349 old0, new0, old1, new1)));
2352 return fold (build (code, type,
2353 eval_subst (TREE_OPERAND (arg, 0),
2354 old0, new0, old1, new1),
2355 eval_subst (TREE_OPERAND (arg, 1),
2356 old0, new0, old1, new1)));
2362 return eval_subst (TREE_OPERAND (arg, 0), old0, new0, old1, new1);
2365 return eval_subst (TREE_OPERAND (arg, 1), old0, new0, old1, new1);
2368 return fold (build (code, type,
2369 eval_subst (TREE_OPERAND (arg, 0),
2370 old0, new0, old1, new1),
2371 eval_subst (TREE_OPERAND (arg, 1),
2372 old0, new0, old1, new1),
2373 eval_subst (TREE_OPERAND (arg, 2),
2374 old0, new0, old1, new1)));
2378 /* fall through - ??? */
2382 tree arg0 = TREE_OPERAND (arg, 0);
2383 tree arg1 = TREE_OPERAND (arg, 1);
2385 /* We need to check both for exact equality and tree equality. The
2386 former will be true if the operand has a side-effect. In that
2387 case, we know the operand occurred exactly once. */
2389 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
2391 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
2394 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
2396 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
2399 return fold (build (code, type, arg0, arg1));
2407 /* Return a tree for the case when the result of an expression is RESULT
2408 converted to TYPE and OMITTED was previously an operand of the expression
2409 but is now not needed (e.g., we folded OMITTED * 0).
2411 If OMITTED has side effects, we must evaluate it. Otherwise, just do
2412 the conversion of RESULT to TYPE. */
2415 omit_one_operand (type, result, omitted)
2416 tree type, result, omitted;
2418 tree t = convert (type, result);
2420 if (TREE_SIDE_EFFECTS (omitted))
2421 return build (COMPOUND_EXPR, type, omitted, t);
2423 return non_lvalue (t);
2426 /* Similar, but call pedantic_non_lvalue instead of non_lvalue. */
2429 pedantic_omit_one_operand (type, result, omitted)
2430 tree type, result, omitted;
2432 tree t = convert (type, result);
2434 if (TREE_SIDE_EFFECTS (omitted))
2435 return build (COMPOUND_EXPR, type, omitted, t);
2437 return pedantic_non_lvalue (t);
2442 /* Return a simplified tree node for the truth-negation of ARG. This
2443 never alters ARG itself. We assume that ARG is an operation that
2444 returns a truth value (0 or 1). */
2447 invert_truthvalue (arg)
2450 tree type = TREE_TYPE (arg);
2451 enum tree_code code = TREE_CODE (arg);
2453 if (code == ERROR_MARK)
2456 /* If this is a comparison, we can simply invert it, except for
2457 floating-point non-equality comparisons, in which case we just
2458 enclose a TRUTH_NOT_EXPR around what we have. */
2460 if (TREE_CODE_CLASS (code) == '<')
2462 if (FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg, 0)))
2463 && !flag_fast_math && code != NE_EXPR && code != EQ_EXPR)
2464 return build1 (TRUTH_NOT_EXPR, type, arg);
2466 return build (invert_tree_comparison (code), type,
2467 TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
2473 return convert (type, build_int_2 (TREE_INT_CST_LOW (arg) == 0
2474 && TREE_INT_CST_HIGH (arg) == 0, 0));
2476 case TRUTH_AND_EXPR:
2477 return build (TRUTH_OR_EXPR, type,
2478 invert_truthvalue (TREE_OPERAND (arg, 0)),
2479 invert_truthvalue (TREE_OPERAND (arg, 1)));
2482 return build (TRUTH_AND_EXPR, type,
2483 invert_truthvalue (TREE_OPERAND (arg, 0)),
2484 invert_truthvalue (TREE_OPERAND (arg, 1)));
2486 case TRUTH_XOR_EXPR:
2487 /* Here we can invert either operand. We invert the first operand
2488 unless the second operand is a TRUTH_NOT_EXPR in which case our
2489 result is the XOR of the first operand with the inside of the
2490 negation of the second operand. */
2492 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
2493 return build (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
2494 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
2496 return build (TRUTH_XOR_EXPR, type,
2497 invert_truthvalue (TREE_OPERAND (arg, 0)),
2498 TREE_OPERAND (arg, 1));
2500 case TRUTH_ANDIF_EXPR:
2501 return build (TRUTH_ORIF_EXPR, type,
2502 invert_truthvalue (TREE_OPERAND (arg, 0)),
2503 invert_truthvalue (TREE_OPERAND (arg, 1)));
2505 case TRUTH_ORIF_EXPR:
2506 return build (TRUTH_ANDIF_EXPR, type,
2507 invert_truthvalue (TREE_OPERAND (arg, 0)),
2508 invert_truthvalue (TREE_OPERAND (arg, 1)));
2510 case TRUTH_NOT_EXPR:
2511 return TREE_OPERAND (arg, 0);
2514 return build (COND_EXPR, type, TREE_OPERAND (arg, 0),
2515 invert_truthvalue (TREE_OPERAND (arg, 1)),
2516 invert_truthvalue (TREE_OPERAND (arg, 2)));
2519 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0),
2520 invert_truthvalue (TREE_OPERAND (arg, 1)));
2522 case NON_LVALUE_EXPR:
2523 return invert_truthvalue (TREE_OPERAND (arg, 0));
2528 return build1 (TREE_CODE (arg), type,
2529 invert_truthvalue (TREE_OPERAND (arg, 0)));
2532 if (!integer_onep (TREE_OPERAND (arg, 1)))
2534 return build (EQ_EXPR, type, arg, convert (type, integer_zero_node));
2537 return build1 (TRUTH_NOT_EXPR, type, arg);
2539 case CLEANUP_POINT_EXPR:
2540 return build1 (CLEANUP_POINT_EXPR, type,
2541 invert_truthvalue (TREE_OPERAND (arg, 0)));
2546 if (TREE_CODE (TREE_TYPE (arg)) != BOOLEAN_TYPE)
2548 return build1 (TRUTH_NOT_EXPR, type, arg);
2551 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
2552 operands are another bit-wise operation with a common input. If so,
2553 distribute the bit operations to save an operation and possibly two if
2554 constants are involved. For example, convert
2555 (A | B) & (A | C) into A | (B & C)
2556 Further simplification will occur if B and C are constants.
2558 If this optimization cannot be done, 0 will be returned. */
2561 distribute_bit_expr (code, type, arg0, arg1)
2562 enum tree_code code;
2569 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2570 || TREE_CODE (arg0) == code
2571 || (TREE_CODE (arg0) != BIT_AND_EXPR
2572 && TREE_CODE (arg0) != BIT_IOR_EXPR))
2575 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
2577 common = TREE_OPERAND (arg0, 0);
2578 left = TREE_OPERAND (arg0, 1);
2579 right = TREE_OPERAND (arg1, 1);
2581 else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
2583 common = TREE_OPERAND (arg0, 0);
2584 left = TREE_OPERAND (arg0, 1);
2585 right = TREE_OPERAND (arg1, 0);
2587 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
2589 common = TREE_OPERAND (arg0, 1);
2590 left = TREE_OPERAND (arg0, 0);
2591 right = TREE_OPERAND (arg1, 1);
2593 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
2595 common = TREE_OPERAND (arg0, 1);
2596 left = TREE_OPERAND (arg0, 0);
2597 right = TREE_OPERAND (arg1, 0);
2602 return fold (build (TREE_CODE (arg0), type, common,
2603 fold (build (code, type, left, right))));
2606 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
2607 starting at BITPOS. The field is unsigned if UNSIGNEDP is non-zero. */
2610 make_bit_field_ref (inner, type, bitsize, bitpos, unsignedp)
2613 int bitsize, bitpos;
2616 tree result = build (BIT_FIELD_REF, type, inner,
2617 size_int (bitsize), bitsize_int (bitpos, 0L));
2619 TREE_UNSIGNED (result) = unsignedp;
2624 /* Optimize a bit-field compare.
2626 There are two cases: First is a compare against a constant and the
2627 second is a comparison of two items where the fields are at the same
2628 bit position relative to the start of a chunk (byte, halfword, word)
2629 large enough to contain it. In these cases we can avoid the shift
2630 implicit in bitfield extractions.
2632 For constants, we emit a compare of the shifted constant with the
2633 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
2634 compared. For two fields at the same position, we do the ANDs with the
2635 similar mask and compare the result of the ANDs.
2637 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
2638 COMPARE_TYPE is the type of the comparison, and LHS and RHS
2639 are the left and right operands of the comparison, respectively.
2641 If the optimization described above can be done, we return the resulting
2642 tree. Otherwise we return zero. */
2645 optimize_bit_field_compare (code, compare_type, lhs, rhs)
2646 enum tree_code code;
2650 int lbitpos, lbitsize, rbitpos, rbitsize;
2651 int lnbitpos, lnbitsize, rnbitpos = 0, rnbitsize = 0;
2652 tree type = TREE_TYPE (lhs);
2653 tree signed_type, unsigned_type;
2654 int const_p = TREE_CODE (rhs) == INTEGER_CST;
2655 enum machine_mode lmode, rmode, lnmode, rnmode = VOIDmode;
2656 int lunsignedp, runsignedp;
2657 int lvolatilep = 0, rvolatilep = 0;
2659 tree linner, rinner = NULL_TREE;
2663 /* Get all the information about the extractions being done. If the bit size
2664 if the same as the size of the underlying object, we aren't doing an
2665 extraction at all and so can do nothing. */
2666 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
2667 &lunsignedp, &lvolatilep, &alignment);
2668 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
2674 /* If this is not a constant, we can only do something if bit positions,
2675 sizes, and signedness are the same. */
2676 rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
2677 &runsignedp, &rvolatilep, &alignment);
2679 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
2680 || lunsignedp != runsignedp || offset != 0)
2684 /* See if we can find a mode to refer to this field. We should be able to,
2685 but fail if we can't. */
2686 lnmode = get_best_mode (lbitsize, lbitpos,
2687 TYPE_ALIGN (TREE_TYPE (linner)), word_mode,
2689 if (lnmode == VOIDmode)
2692 /* Set signed and unsigned types of the precision of this mode for the
2694 signed_type = type_for_mode (lnmode, 0);
2695 unsigned_type = type_for_mode (lnmode, 1);
2699 rnmode = get_best_mode (rbitsize, rbitpos,
2700 TYPE_ALIGN (TREE_TYPE (rinner)), word_mode,
2702 if (rnmode == VOIDmode)
2706 /* Compute the bit position and size for the new reference and our offset
2707 within it. If the new reference is the same size as the original, we
2708 won't optimize anything, so return zero. */
2709 lnbitsize = GET_MODE_BITSIZE (lnmode);
2710 lnbitpos = lbitpos & ~ (lnbitsize - 1);
2711 lbitpos -= lnbitpos;
2712 if (lnbitsize == lbitsize)
2717 rnbitsize = GET_MODE_BITSIZE (rnmode);
2718 rnbitpos = rbitpos & ~ (rnbitsize - 1);
2719 rbitpos -= rnbitpos;
2720 if (rnbitsize == rbitsize)
2724 if (BYTES_BIG_ENDIAN)
2725 lbitpos = lnbitsize - lbitsize - lbitpos;
2727 /* Make the mask to be used against the extracted field. */
2728 mask = build_int_2 (~0, ~0);
2729 TREE_TYPE (mask) = unsigned_type;
2730 force_fit_type (mask, 0);
2731 mask = convert (unsigned_type, mask);
2732 mask = const_binop (LSHIFT_EXPR, mask, size_int (lnbitsize - lbitsize), 0);
2733 mask = const_binop (RSHIFT_EXPR, mask,
2734 size_int (lnbitsize - lbitsize - lbitpos), 0);
2737 /* If not comparing with constant, just rework the comparison
2739 return build (code, compare_type,
2740 build (BIT_AND_EXPR, unsigned_type,
2741 make_bit_field_ref (linner, unsigned_type,
2742 lnbitsize, lnbitpos, 1),
2744 build (BIT_AND_EXPR, unsigned_type,
2745 make_bit_field_ref (rinner, unsigned_type,
2746 rnbitsize, rnbitpos, 1),
2749 /* Otherwise, we are handling the constant case. See if the constant is too
2750 big for the field. Warn and return a tree of for 0 (false) if so. We do
2751 this not only for its own sake, but to avoid having to test for this
2752 error case below. If we didn't, we might generate wrong code.
2754 For unsigned fields, the constant shifted right by the field length should
2755 be all zero. For signed fields, the high-order bits should agree with
2760 if (! integer_zerop (const_binop (RSHIFT_EXPR,
2761 convert (unsigned_type, rhs),
2762 size_int (lbitsize), 0)))
2764 warning ("comparison is always %d due to width of bitfield",
2766 return convert (compare_type,
2768 ? integer_one_node : integer_zero_node));
2773 tree tem = const_binop (RSHIFT_EXPR, convert (signed_type, rhs),
2774 size_int (lbitsize - 1), 0);
2775 if (! integer_zerop (tem) && ! integer_all_onesp (tem))
2777 warning ("comparison is always %d due to width of bitfield",
2779 return convert (compare_type,
2781 ? integer_one_node : integer_zero_node));
2785 /* Single-bit compares should always be against zero. */
2786 if (lbitsize == 1 && ! integer_zerop (rhs))
2788 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
2789 rhs = convert (type, integer_zero_node);
2792 /* Make a new bitfield reference, shift the constant over the
2793 appropriate number of bits and mask it with the computed mask
2794 (in case this was a signed field). If we changed it, make a new one. */
2795 lhs = make_bit_field_ref (linner, unsigned_type, lnbitsize, lnbitpos, 1);
2798 TREE_SIDE_EFFECTS (lhs) = 1;
2799 TREE_THIS_VOLATILE (lhs) = 1;
2802 rhs = fold (const_binop (BIT_AND_EXPR,
2803 const_binop (LSHIFT_EXPR,
2804 convert (unsigned_type, rhs),
2805 size_int (lbitpos), 0),
2808 return build (code, compare_type,
2809 build (BIT_AND_EXPR, unsigned_type, lhs, mask),
2813 /* Subroutine for fold_truthop: decode a field reference.
2815 If EXP is a comparison reference, we return the innermost reference.
2817 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
2818 set to the starting bit number.
2820 If the innermost field can be completely contained in a mode-sized
2821 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
2823 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
2824 otherwise it is not changed.
2826 *PUNSIGNEDP is set to the signedness of the field.
2828 *PMASK is set to the mask used. This is either contained in a
2829 BIT_AND_EXPR or derived from the width of the field.
2831 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
2833 Return 0 if this is not a component reference or is one that we can't
2834 do anything with. */
2837 decode_field_reference (exp, pbitsize, pbitpos, pmode, punsignedp,
2838 pvolatilep, pmask, pand_mask)
2840 int *pbitsize, *pbitpos;
2841 enum machine_mode *pmode;
2842 int *punsignedp, *pvolatilep;
2847 tree mask, inner, offset;
2852 /* All the optimizations using this function assume integer fields.
2853 There are problems with FP fields since the type_for_size call
2854 below can fail for, e.g., XFmode. */
2855 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
2860 if (TREE_CODE (exp) == BIT_AND_EXPR)
2862 and_mask = TREE_OPERAND (exp, 1);
2863 exp = TREE_OPERAND (exp, 0);
2864 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
2865 if (TREE_CODE (and_mask) != INTEGER_CST)
2870 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
2871 punsignedp, pvolatilep, &alignment);
2872 if ((inner == exp && and_mask == 0)
2873 || *pbitsize < 0 || offset != 0)
2876 /* Compute the mask to access the bitfield. */
2877 unsigned_type = type_for_size (*pbitsize, 1);
2878 precision = TYPE_PRECISION (unsigned_type);
2880 mask = build_int_2 (~0, ~0);
2881 TREE_TYPE (mask) = unsigned_type;
2882 force_fit_type (mask, 0);
2883 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
2884 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
2886 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
2888 mask = fold (build (BIT_AND_EXPR, unsigned_type,
2889 convert (unsigned_type, and_mask), mask));
2892 *pand_mask = and_mask;
2896 /* Return non-zero if MASK represents a mask of SIZE ones in the low-order
2900 all_ones_mask_p (mask, size)
2904 tree type = TREE_TYPE (mask);
2905 int precision = TYPE_PRECISION (type);
2908 tmask = build_int_2 (~0, ~0);
2909 TREE_TYPE (tmask) = signed_type (type);
2910 force_fit_type (tmask, 0);
2912 tree_int_cst_equal (mask,
2913 const_binop (RSHIFT_EXPR,
2914 const_binop (LSHIFT_EXPR, tmask,
2915 size_int (precision - size),
2917 size_int (precision - size), 0));
2920 /* Subroutine for fold_truthop: determine if an operand is simple enough
2921 to be evaluated unconditionally. */
2924 simple_operand_p (exp)
2927 /* Strip any conversions that don't change the machine mode. */
2928 while ((TREE_CODE (exp) == NOP_EXPR
2929 || TREE_CODE (exp) == CONVERT_EXPR)
2930 && (TYPE_MODE (TREE_TYPE (exp))
2931 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)))))
2932 exp = TREE_OPERAND (exp, 0);
2934 return (TREE_CODE_CLASS (TREE_CODE (exp)) == 'c'
2935 || (TREE_CODE_CLASS (TREE_CODE (exp)) == 'd'
2936 && ! TREE_ADDRESSABLE (exp)
2937 && ! TREE_THIS_VOLATILE (exp)
2938 && ! DECL_NONLOCAL (exp)
2939 /* Don't regard global variables as simple. They may be
2940 allocated in ways unknown to the compiler (shared memory,
2941 #pragma weak, etc). */
2942 && ! TREE_PUBLIC (exp)
2943 && ! DECL_EXTERNAL (exp)
2944 /* Loading a static variable is unduly expensive, but global
2945 registers aren't expensive. */
2946 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
2949 /* The following functions are subroutines to fold_range_test and allow it to
2950 try to change a logical combination of comparisons into a range test.
2953 X == 2 && X == 3 && X == 4 && X == 5
2957 (unsigned) (X - 2) <= 3
2959 We describe each set of comparisons as being either inside or outside
2960 a range, using a variable named like IN_P, and then describe the
2961 range with a lower and upper bound. If one of the bounds is omitted,
2962 it represents either the highest or lowest value of the type.
2964 In the comments below, we represent a range by two numbers in brackets
2965 preceded by a "+" to designate being inside that range, or a "-" to
2966 designate being outside that range, so the condition can be inverted by
2967 flipping the prefix. An omitted bound is represented by a "-". For
2968 example, "- [-, 10]" means being outside the range starting at the lowest
2969 possible value and ending at 10, in other words, being greater than 10.
2970 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
2973 We set up things so that the missing bounds are handled in a consistent
2974 manner so neither a missing bound nor "true" and "false" need to be
2975 handled using a special case. */
2977 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
2978 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
2979 and UPPER1_P are nonzero if the respective argument is an upper bound
2980 and zero for a lower. TYPE, if nonzero, is the type of the result; it
2981 must be specified for a comparison. ARG1 will be converted to ARG0's
2982 type if both are specified. */
2985 range_binop (code, type, arg0, upper0_p, arg1, upper1_p)
2986 enum tree_code code;
2989 int upper0_p, upper1_p;
2995 /* If neither arg represents infinity, do the normal operation.
2996 Else, if not a comparison, return infinity. Else handle the special
2997 comparison rules. Note that most of the cases below won't occur, but
2998 are handled for consistency. */
3000 if (arg0 != 0 && arg1 != 0)
3002 tem = fold (build (code, type != 0 ? type : TREE_TYPE (arg0),
3003 arg0, convert (TREE_TYPE (arg0), arg1)));
3005 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
3008 if (TREE_CODE_CLASS (code) != '<')
3011 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
3012 for neither. Then compute our result treating them as never equal
3013 and comparing bounds to non-bounds as above. */
3014 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
3015 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
3018 case EQ_EXPR: case NE_EXPR:
3019 result = (code == NE_EXPR);
3021 case LT_EXPR: case LE_EXPR:
3022 result = sgn0 < sgn1;
3024 case GT_EXPR: case GE_EXPR:
3025 result = sgn0 > sgn1;
3031 return convert (type, result ? integer_one_node : integer_zero_node);
3034 /* Given EXP, a logical expression, set the range it is testing into
3035 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
3036 actually being tested. *PLOW and *PHIGH will have be made the same type
3037 as the returned expression. If EXP is not a comparison, we will most
3038 likely not be returning a useful value and range. */
3041 make_range (exp, pin_p, plow, phigh)
3046 enum tree_code code;
3047 tree arg0, arg1, type = NULL_TREE;
3048 tree orig_type = NULL_TREE;
3050 tree low, high, n_low, n_high;
3052 /* Start with simply saying "EXP != 0" and then look at the code of EXP
3053 and see if we can refine the range. Some of the cases below may not
3054 happen, but it doesn't seem worth worrying about this. We "continue"
3055 the outer loop when we've changed something; otherwise we "break"
3056 the switch, which will "break" the while. */
3058 in_p = 0, low = high = convert (TREE_TYPE (exp), integer_zero_node);
3062 code = TREE_CODE (exp);
3064 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
3066 arg0 = TREE_OPERAND (exp, 0);
3067 if (TREE_CODE_CLASS (code) == '<'
3068 || TREE_CODE_CLASS (code) == '1'
3069 || TREE_CODE_CLASS (code) == '2')
3070 type = TREE_TYPE (arg0);
3071 if (TREE_CODE_CLASS (code) == '2'
3072 || TREE_CODE_CLASS (code) == '<'
3073 || (TREE_CODE_CLASS (code) == 'e'
3074 && tree_code_length[(int) code] > 1))
3075 arg1 = TREE_OPERAND (exp, 1);
3080 case TRUTH_NOT_EXPR:
3081 in_p = ! in_p, exp = arg0;
3084 case EQ_EXPR: case NE_EXPR:
3085 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
3086 /* We can only do something if the range is testing for zero
3087 and if the second operand is an integer constant. Note that
3088 saying something is "in" the range we make is done by
3089 complementing IN_P since it will set in the initial case of
3090 being not equal to zero; "out" is leaving it alone. */
3091 if (low == 0 || high == 0
3092 || ! integer_zerop (low) || ! integer_zerop (high)
3093 || TREE_CODE (arg1) != INTEGER_CST)
3098 case NE_EXPR: /* - [c, c] */
3101 case EQ_EXPR: /* + [c, c] */
3102 in_p = ! in_p, low = high = arg1;
3104 case GT_EXPR: /* - [-, c] */
3105 low = 0, high = arg1;
3107 case GE_EXPR: /* + [c, -] */
3108 in_p = ! in_p, low = arg1, high = 0;
3110 case LT_EXPR: /* - [c, -] */
3111 low = arg1, high = 0;
3113 case LE_EXPR: /* + [-, c] */
3114 in_p = ! in_p, low = 0, high = arg1;
3122 /* If this is an unsigned comparison, we also know that EXP is
3123 greater than or equal to zero. We base the range tests we make
3124 on that fact, so we record it here so we can parse existing
3126 if (TREE_UNSIGNED (type) && (low == 0 || high == 0))
3128 if (! merge_ranges (&n_in_p, &n_low, &n_high, in_p, low, high,
3129 1, convert (type, integer_zero_node),
3133 in_p = n_in_p, low = n_low, high = n_high;
3135 /* If the high bound is missing, reverse the range so it
3136 goes from zero to the low bound minus 1. */
3140 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
3141 integer_one_node, 0);
3142 low = convert (type, integer_zero_node);
3148 /* (-x) IN [a,b] -> x in [-b, -a] */
3149 n_low = range_binop (MINUS_EXPR, type,
3150 convert (type, integer_zero_node), 0, high, 1);
3151 n_high = range_binop (MINUS_EXPR, type,
3152 convert (type, integer_zero_node), 0, low, 0);
3153 low = n_low, high = n_high;
3159 exp = build (MINUS_EXPR, type, build1 (NEGATE_EXPR, type, arg0),
3160 convert (type, integer_one_node));
3163 case PLUS_EXPR: case MINUS_EXPR:
3164 if (TREE_CODE (arg1) != INTEGER_CST)
3167 /* If EXP is signed, any overflow in the computation is undefined,
3168 so we don't worry about it so long as our computations on
3169 the bounds don't overflow. For unsigned, overflow is defined
3170 and this is exactly the right thing. */
3171 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3172 type, low, 0, arg1, 0);
3173 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3174 type, high, 1, arg1, 0);
3175 if ((n_low != 0 && TREE_OVERFLOW (n_low))
3176 || (n_high != 0 && TREE_OVERFLOW (n_high)))
3179 /* Check for an unsigned range which has wrapped around the maximum
3180 value thus making n_high < n_low, and normalize it. */
3181 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
3183 low = range_binop (PLUS_EXPR, type, n_high, 0,
3184 integer_one_node, 0);
3185 high = range_binop (MINUS_EXPR, type, n_low, 0,
3186 integer_one_node, 0);
3190 low = n_low, high = n_high;
3195 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
3196 if (orig_type == NULL_TREE)
3198 if (TYPE_PRECISION (type) > TYPE_PRECISION (orig_type))
3201 if (! INTEGRAL_TYPE_P (type)
3202 || (low != 0 && ! int_fits_type_p (low, type))
3203 || (high != 0 && ! int_fits_type_p (high, type)))
3206 n_low = low, n_high = high;
3209 n_low = convert (type, n_low);
3212 n_high = convert (type, n_high);
3214 /* If we're converting from an unsigned to a signed type,
3215 we will be doing the comparison as unsigned. The tests above
3216 have already verified that LOW and HIGH are both positive.
3218 So we have to make sure that the original unsigned value will
3219 be interpreted as positive. */
3220 if (TREE_UNSIGNED (type) && ! TREE_UNSIGNED (TREE_TYPE (exp)))
3222 tree equiv_type = type_for_mode (TYPE_MODE (type), 1);
3225 /* A range without an upper bound is, naturally, unbounded.
3226 Since convert would have cropped a very large value, use
3227 the max value for the destination type. */
3229 high_positive = TYPE_MAX_VALUE (equiv_type);
3232 high_positive = TYPE_MAX_VALUE (type);
3236 high_positive = fold (build (RSHIFT_EXPR, type,
3237 convert (type, high_positive),
3238 convert (type, integer_one_node)));
3240 /* If the low bound is specified, "and" the range with the
3241 range for which the original unsigned value will be
3245 if (! merge_ranges (&n_in_p, &n_low, &n_high,
3247 1, convert (type, integer_zero_node),
3251 in_p = (n_in_p == in_p);
3255 /* Otherwise, "or" the range with the range of the input
3256 that will be interpreted as negative. */
3257 if (! merge_ranges (&n_in_p, &n_low, &n_high,
3259 1, convert (type, integer_zero_node),
3263 in_p = (in_p != n_in_p);
3268 low = n_low, high = n_high;
3278 /* If EXP is a constant, we can evaluate whether this is true or false. */
3279 if (TREE_CODE (exp) == INTEGER_CST)
3281 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
3283 && integer_onep (range_binop (LE_EXPR, integer_type_node,
3289 *pin_p = in_p, *plow = low, *phigh = high;
3293 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
3294 type, TYPE, return an expression to test if EXP is in (or out of, depending
3295 on IN_P) the range. */
3298 build_range_check (type, exp, in_p, low, high)
3304 tree etype = TREE_TYPE (exp);
3308 && (0 != (value = build_range_check (type, exp, 1, low, high))))
3309 return invert_truthvalue (value);
3311 else if (low == 0 && high == 0)
3312 return convert (type, integer_one_node);
3315 return fold (build (LE_EXPR, type, exp, high));
3318 return fold (build (GE_EXPR, type, exp, low));
3320 else if (operand_equal_p (low, high, 0))
3321 return fold (build (EQ_EXPR, type, exp, low));
3323 else if (TREE_UNSIGNED (etype) && integer_zerop (low))
3324 return build_range_check (type, exp, 1, 0, high);
3326 else if (integer_zerop (low))
3328 utype = unsigned_type (etype);
3329 return build_range_check (type, convert (utype, exp), 1, 0,
3330 convert (utype, high));
3333 else if (0 != (value = const_binop (MINUS_EXPR, high, low, 0))
3334 && ! TREE_OVERFLOW (value))
3335 return build_range_check (type,
3336 fold (build (MINUS_EXPR, etype, exp, low)),
3337 1, convert (etype, integer_zero_node), value);
3342 /* Given two ranges, see if we can merge them into one. Return 1 if we
3343 can, 0 if we can't. Set the output range into the specified parameters. */
3346 merge_ranges (pin_p, plow, phigh, in0_p, low0, high0, in1_p, low1, high1)
3350 tree low0, high0, low1, high1;
3358 int lowequal = ((low0 == 0 && low1 == 0)
3359 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
3360 low0, 0, low1, 0)));
3361 int highequal = ((high0 == 0 && high1 == 0)
3362 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
3363 high0, 1, high1, 1)));
3365 /* Make range 0 be the range that starts first, or ends last if they
3366 start at the same value. Swap them if it isn't. */
3367 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
3370 && integer_onep (range_binop (GT_EXPR, integer_type_node,
3371 high1, 1, high0, 1))))
3373 temp = in0_p, in0_p = in1_p, in1_p = temp;
3374 tem = low0, low0 = low1, low1 = tem;
3375 tem = high0, high0 = high1, high1 = tem;
3378 /* Now flag two cases, whether the ranges are disjoint or whether the
3379 second range is totally subsumed in the first. Note that the tests
3380 below are simplified by the ones above. */
3381 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
3382 high0, 1, low1, 0));
3383 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
3384 high1, 1, high0, 1));
3386 /* We now have four cases, depending on whether we are including or
3387 excluding the two ranges. */
3390 /* If they don't overlap, the result is false. If the second range
3391 is a subset it is the result. Otherwise, the range is from the start
3392 of the second to the end of the first. */
3394 in_p = 0, low = high = 0;
3396 in_p = 1, low = low1, high = high1;
3398 in_p = 1, low = low1, high = high0;
3401 else if (in0_p && ! in1_p)
3403 /* If they don't overlap, the result is the first range. If they are
3404 equal, the result is false. If the second range is a subset of the
3405 first, and the ranges begin at the same place, we go from just after
3406 the end of the first range to the end of the second. If the second
3407 range is not a subset of the first, or if it is a subset and both
3408 ranges end at the same place, the range starts at the start of the
3409 first range and ends just before the second range.
3410 Otherwise, we can't describe this as a single range. */
3412 in_p = 1, low = low0, high = high0;
3413 else if (lowequal && highequal)
3414 in_p = 0, low = high = 0;
3415 else if (subset && lowequal)
3417 in_p = 1, high = high0;
3418 low = range_binop (PLUS_EXPR, NULL_TREE, high1, 0,
3419 integer_one_node, 0);
3421 else if (! subset || highequal)
3423 in_p = 1, low = low0;
3424 high = range_binop (MINUS_EXPR, NULL_TREE, low1, 0,
3425 integer_one_node, 0);
3431 else if (! in0_p && in1_p)
3433 /* If they don't overlap, the result is the second range. If the second
3434 is a subset of the first, the result is false. Otherwise,
3435 the range starts just after the first range and ends at the
3436 end of the second. */
3438 in_p = 1, low = low1, high = high1;
3440 in_p = 0, low = high = 0;
3443 in_p = 1, high = high1;
3444 low = range_binop (PLUS_EXPR, NULL_TREE, high0, 1,
3445 integer_one_node, 0);
3451 /* The case where we are excluding both ranges. Here the complex case
3452 is if they don't overlap. In that case, the only time we have a
3453 range is if they are adjacent. If the second is a subset of the
3454 first, the result is the first. Otherwise, the range to exclude
3455 starts at the beginning of the first range and ends at the end of the
3459 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
3460 range_binop (PLUS_EXPR, NULL_TREE,
3462 integer_one_node, 1),
3464 in_p = 0, low = low0, high = high1;
3469 in_p = 0, low = low0, high = high0;
3471 in_p = 0, low = low0, high = high1;
3474 *pin_p = in_p, *plow = low, *phigh = high;
3478 /* EXP is some logical combination of boolean tests. See if we can
3479 merge it into some range test. Return the new tree if so. */
3482 fold_range_test (exp)
3485 int or_op = (TREE_CODE (exp) == TRUTH_ORIF_EXPR
3486 || TREE_CODE (exp) == TRUTH_OR_EXPR);
3487 int in0_p, in1_p, in_p;
3488 tree low0, low1, low, high0, high1, high;
3489 tree lhs = make_range (TREE_OPERAND (exp, 0), &in0_p, &low0, &high0);
3490 tree rhs = make_range (TREE_OPERAND (exp, 1), &in1_p, &low1, &high1);
3493 /* If this is an OR operation, invert both sides; we will invert
3494 again at the end. */
3496 in0_p = ! in0_p, in1_p = ! in1_p;
3498 /* If both expressions are the same, if we can merge the ranges, and we
3499 can build the range test, return it or it inverted. If one of the
3500 ranges is always true or always false, consider it to be the same
3501 expression as the other. */
3502 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
3503 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
3505 && 0 != (tem = (build_range_check (TREE_TYPE (exp),
3507 : rhs != 0 ? rhs : integer_zero_node,
3509 return or_op ? invert_truthvalue (tem) : tem;
3511 /* On machines where the branch cost is expensive, if this is a
3512 short-circuited branch and the underlying object on both sides
3513 is the same, make a non-short-circuit operation. */
3514 else if (BRANCH_COST >= 2
3515 && (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
3516 || TREE_CODE (exp) == TRUTH_ORIF_EXPR)
3517 && operand_equal_p (lhs, rhs, 0))
3519 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
3520 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
3521 which cases we can't do this. */
3522 if (simple_operand_p (lhs))
3523 return build (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
3524 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
3525 TREE_TYPE (exp), TREE_OPERAND (exp, 0),
3526 TREE_OPERAND (exp, 1));
3528 else if (current_function_decl != 0
3529 && ! contains_placeholder_p (lhs))
3531 tree common = save_expr (lhs);
3533 if (0 != (lhs = build_range_check (TREE_TYPE (exp), common,
3534 or_op ? ! in0_p : in0_p,
3536 && (0 != (rhs = build_range_check (TREE_TYPE (exp), common,
3537 or_op ? ! in1_p : in1_p,
3539 return build (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
3540 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
3541 TREE_TYPE (exp), lhs, rhs);
3548 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
3549 bit value. Arrange things so the extra bits will be set to zero if and
3550 only if C is signed-extended to its full width. If MASK is nonzero,
3551 it is an INTEGER_CST that should be AND'ed with the extra bits. */
3554 unextend (c, p, unsignedp, mask)
3560 tree type = TREE_TYPE (c);
3561 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
3564 if (p == modesize || unsignedp)
3567 /* We work by getting just the sign bit into the low-order bit, then
3568 into the high-order bit, then sign-extend. We then XOR that value
3570 temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
3571 temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
3573 /* We must use a signed type in order to get an arithmetic right shift.
3574 However, we must also avoid introducing accidental overflows, so that
3575 a subsequent call to integer_zerop will work. Hence we must
3576 do the type conversion here. At this point, the constant is either
3577 zero or one, and the conversion to a signed type can never overflow.
3578 We could get an overflow if this conversion is done anywhere else. */
3579 if (TREE_UNSIGNED (type))
3580 temp = convert (signed_type (type), temp);
3582 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
3583 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
3585 temp = const_binop (BIT_AND_EXPR, temp, convert (TREE_TYPE (c), mask), 0);
3586 /* If necessary, convert the type back to match the type of C. */
3587 if (TREE_UNSIGNED (type))
3588 temp = convert (type, temp);
3590 return convert (type, const_binop (BIT_XOR_EXPR, c, temp, 0));
3593 /* Find ways of folding logical expressions of LHS and RHS:
3594 Try to merge two comparisons to the same innermost item.
3595 Look for range tests like "ch >= '0' && ch <= '9'".
3596 Look for combinations of simple terms on machines with expensive branches
3597 and evaluate the RHS unconditionally.
3599 For example, if we have p->a == 2 && p->b == 4 and we can make an
3600 object large enough to span both A and B, we can do this with a comparison
3601 against the object ANDed with the a mask.
3603 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
3604 operations to do this with one comparison.
3606 We check for both normal comparisons and the BIT_AND_EXPRs made this by
3607 function and the one above.
3609 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
3610 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
3612 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
3615 We return the simplified tree or 0 if no optimization is possible. */
3618 fold_truthop (code, truth_type, lhs, rhs)
3619 enum tree_code code;
3620 tree truth_type, lhs, rhs;
3622 /* If this is the "or" of two comparisons, we can do something if we
3623 the comparisons are NE_EXPR. If this is the "and", we can do something
3624 if the comparisons are EQ_EXPR. I.e.,
3625 (a->b == 2 && a->c == 4) can become (a->new == NEW).
3627 WANTED_CODE is this operation code. For single bit fields, we can
3628 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
3629 comparison for one-bit fields. */
3631 enum tree_code wanted_code;
3632 enum tree_code lcode, rcode;
3633 tree ll_arg, lr_arg, rl_arg, rr_arg;
3634 tree ll_inner, lr_inner, rl_inner, rr_inner;
3635 int ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
3636 int rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
3637 int xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
3638 int lnbitsize, lnbitpos, rnbitsize, rnbitpos;
3639 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
3640 enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
3641 enum machine_mode lnmode, rnmode;
3642 tree ll_mask, lr_mask, rl_mask, rr_mask;
3643 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
3644 tree l_const, r_const;
3646 int first_bit, end_bit;
3649 /* Start by getting the comparison codes. Fail if anything is volatile.
3650 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
3651 it were surrounded with a NE_EXPR. */
3653 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
3656 lcode = TREE_CODE (lhs);
3657 rcode = TREE_CODE (rhs);
3659 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
3660 lcode = NE_EXPR, lhs = build (NE_EXPR, truth_type, lhs, integer_zero_node);
3662 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
3663 rcode = NE_EXPR, rhs = build (NE_EXPR, truth_type, rhs, integer_zero_node);
3665 if (TREE_CODE_CLASS (lcode) != '<' || TREE_CODE_CLASS (rcode) != '<')
3668 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
3669 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
3671 ll_arg = TREE_OPERAND (lhs, 0);
3672 lr_arg = TREE_OPERAND (lhs, 1);
3673 rl_arg = TREE_OPERAND (rhs, 0);
3674 rr_arg = TREE_OPERAND (rhs, 1);
3676 /* If the RHS can be evaluated unconditionally and its operands are
3677 simple, it wins to evaluate the RHS unconditionally on machines
3678 with expensive branches. In this case, this isn't a comparison
3679 that can be merged. */
3681 /* @@ I'm not sure it wins on the m88110 to do this if the comparisons
3682 are with zero (tmw). */
3684 if (BRANCH_COST >= 2
3685 && INTEGRAL_TYPE_P (TREE_TYPE (rhs))
3686 && simple_operand_p (rl_arg)
3687 && simple_operand_p (rr_arg))
3688 return build (code, truth_type, lhs, rhs);
3690 /* See if the comparisons can be merged. Then get all the parameters for
3693 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
3694 || (rcode != EQ_EXPR && rcode != NE_EXPR))
3698 ll_inner = decode_field_reference (ll_arg,
3699 &ll_bitsize, &ll_bitpos, &ll_mode,
3700 &ll_unsignedp, &volatilep, &ll_mask,
3702 lr_inner = decode_field_reference (lr_arg,
3703 &lr_bitsize, &lr_bitpos, &lr_mode,
3704 &lr_unsignedp, &volatilep, &lr_mask,
3706 rl_inner = decode_field_reference (rl_arg,
3707 &rl_bitsize, &rl_bitpos, &rl_mode,
3708 &rl_unsignedp, &volatilep, &rl_mask,
3710 rr_inner = decode_field_reference (rr_arg,
3711 &rr_bitsize, &rr_bitpos, &rr_mode,
3712 &rr_unsignedp, &volatilep, &rr_mask,
3715 /* It must be true that the inner operation on the lhs of each
3716 comparison must be the same if we are to be able to do anything.
3717 Then see if we have constants. If not, the same must be true for
3719 if (volatilep || ll_inner == 0 || rl_inner == 0
3720 || ! operand_equal_p (ll_inner, rl_inner, 0))
3723 if (TREE_CODE (lr_arg) == INTEGER_CST
3724 && TREE_CODE (rr_arg) == INTEGER_CST)
3725 l_const = lr_arg, r_const = rr_arg;
3726 else if (lr_inner == 0 || rr_inner == 0
3727 || ! operand_equal_p (lr_inner, rr_inner, 0))
3730 l_const = r_const = 0;
3732 /* If either comparison code is not correct for our logical operation,
3733 fail. However, we can convert a one-bit comparison against zero into
3734 the opposite comparison against that bit being set in the field. */
3736 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
3737 if (lcode != wanted_code)
3739 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
3741 if (ll_unsignedp || tree_log2 (ll_mask) + 1 < ll_bitsize)
3744 /* Since ll_arg is a single bit bit mask, we can sign extend
3745 it appropriately with a NEGATE_EXPR.
3746 l_const is made a signed value here, but since for l_const != NULL
3747 lr_unsignedp is not used, we don't need to clear the latter. */
3748 l_const = fold (build1 (NEGATE_EXPR, TREE_TYPE (ll_arg),
3749 convert (TREE_TYPE (ll_arg), ll_mask)));
3755 if (rcode != wanted_code)
3757 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
3759 if (rl_unsignedp || tree_log2 (rl_mask) + 1 < rl_bitsize)
3762 /* This is analogous to the code for l_const above. */
3763 r_const = fold (build1 (NEGATE_EXPR, TREE_TYPE (rl_arg),
3764 convert (TREE_TYPE (rl_arg), rl_mask)));
3770 /* See if we can find a mode that contains both fields being compared on
3771 the left. If we can't, fail. Otherwise, update all constants and masks
3772 to be relative to a field of that size. */
3773 first_bit = MIN (ll_bitpos, rl_bitpos);
3774 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
3775 lnmode = get_best_mode (end_bit - first_bit, first_bit,
3776 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
3778 if (lnmode == VOIDmode)
3781 lnbitsize = GET_MODE_BITSIZE (lnmode);
3782 lnbitpos = first_bit & ~ (lnbitsize - 1);
3783 type = type_for_size (lnbitsize, 1);
3784 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
3786 if (BYTES_BIG_ENDIAN)
3788 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
3789 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
3792 ll_mask = const_binop (LSHIFT_EXPR, convert (type, ll_mask),
3793 size_int (xll_bitpos), 0);
3794 rl_mask = const_binop (LSHIFT_EXPR, convert (type, rl_mask),
3795 size_int (xrl_bitpos), 0);
3799 l_const = convert (type, l_const);
3800 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
3801 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
3802 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
3803 fold (build1 (BIT_NOT_EXPR,
3807 warning ("comparison is always %d", wanted_code == NE_EXPR);
3809 return convert (truth_type,
3810 wanted_code == NE_EXPR
3811 ? integer_one_node : integer_zero_node);
3816 r_const = convert (type, r_const);
3817 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
3818 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
3819 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
3820 fold (build1 (BIT_NOT_EXPR,
3824 warning ("comparison is always %d", wanted_code == NE_EXPR);
3826 return convert (truth_type,
3827 wanted_code == NE_EXPR
3828 ? integer_one_node : integer_zero_node);
3832 /* If the right sides are not constant, do the same for it. Also,
3833 disallow this optimization if a size or signedness mismatch occurs
3834 between the left and right sides. */
3837 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
3838 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
3839 /* Make sure the two fields on the right
3840 correspond to the left without being swapped. */
3841 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
3844 first_bit = MIN (lr_bitpos, rr_bitpos);
3845 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
3846 rnmode = get_best_mode (end_bit - first_bit, first_bit,
3847 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
3849 if (rnmode == VOIDmode)
3852 rnbitsize = GET_MODE_BITSIZE (rnmode);
3853 rnbitpos = first_bit & ~ (rnbitsize - 1);
3854 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
3856 if (BYTES_BIG_ENDIAN)
3858 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
3859 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
3862 lr_mask = const_binop (LSHIFT_EXPR, convert (type, lr_mask),
3863 size_int (xlr_bitpos), 0);
3864 rr_mask = const_binop (LSHIFT_EXPR, convert (type, rr_mask),
3865 size_int (xrr_bitpos), 0);
3867 /* Make a mask that corresponds to both fields being compared.
3868 Do this for both items being compared. If the masks agree,
3869 we can do this by masking both and comparing the masked
3871 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
3872 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0);
3873 if (operand_equal_p (ll_mask, lr_mask, 0) && lnbitsize == rnbitsize)
3875 lhs = make_bit_field_ref (ll_inner, type, lnbitsize, lnbitpos,
3876 ll_unsignedp || rl_unsignedp);
3877 rhs = make_bit_field_ref (lr_inner, type, rnbitsize, rnbitpos,
3878 lr_unsignedp || rr_unsignedp);
3879 if (! all_ones_mask_p (ll_mask, lnbitsize))
3881 lhs = build (BIT_AND_EXPR, type, lhs, ll_mask);
3882 rhs = build (BIT_AND_EXPR, type, rhs, ll_mask);
3884 return build (wanted_code, truth_type, lhs, rhs);
3887 /* There is still another way we can do something: If both pairs of
3888 fields being compared are adjacent, we may be able to make a wider
3889 field containing them both. */
3890 if ((ll_bitsize + ll_bitpos == rl_bitpos
3891 && lr_bitsize + lr_bitpos == rr_bitpos)
3892 || (ll_bitpos == rl_bitpos + rl_bitsize
3893 && lr_bitpos == rr_bitpos + rr_bitsize))
3894 return build (wanted_code, truth_type,
3895 make_bit_field_ref (ll_inner, type,
3896 ll_bitsize + rl_bitsize,
3897 MIN (ll_bitpos, rl_bitpos),
3899 make_bit_field_ref (lr_inner, type,
3900 lr_bitsize + rr_bitsize,
3901 MIN (lr_bitpos, rr_bitpos),
3907 /* Handle the case of comparisons with constants. If there is something in
3908 common between the masks, those bits of the constants must be the same.
3909 If not, the condition is always false. Test for this to avoid generating
3910 incorrect code below. */
3911 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
3912 if (! integer_zerop (result)
3913 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
3914 const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
3916 if (wanted_code == NE_EXPR)
3918 warning ("`or' of unmatched not-equal tests is always 1");
3919 return convert (truth_type, integer_one_node);
3923 warning ("`and' of mutually exclusive equal-tests is always 0");
3924 return convert (truth_type, integer_zero_node);
3928 /* Construct the expression we will return. First get the component
3929 reference we will make. Unless the mask is all ones the width of
3930 that field, perform the mask operation. Then compare with the
3932 result = make_bit_field_ref (ll_inner, type, lnbitsize, lnbitpos,
3933 ll_unsignedp || rl_unsignedp);
3935 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
3936 if (! all_ones_mask_p (ll_mask, lnbitsize))
3937 result = build (BIT_AND_EXPR, type, result, ll_mask);
3939 return build (wanted_code, truth_type, result,
3940 const_binop (BIT_IOR_EXPR, l_const, r_const, 0));
3943 /* If T contains a COMPOUND_EXPR which was inserted merely to evaluate
3944 S, a SAVE_EXPR, return the expression actually being evaluated. Note
3945 that we may sometimes modify the tree. */
3948 strip_compound_expr (t, s)
3952 enum tree_code code = TREE_CODE (t);
3954 /* See if this is the COMPOUND_EXPR we want to eliminate. */
3955 if (code == COMPOUND_EXPR && TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR
3956 && TREE_OPERAND (TREE_OPERAND (t, 0), 0) == s)
3957 return TREE_OPERAND (t, 1);
3959 /* See if this is a COND_EXPR or a simple arithmetic operator. We
3960 don't bother handling any other types. */
3961 else if (code == COND_EXPR)
3963 TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s);
3964 TREE_OPERAND (t, 1) = strip_compound_expr (TREE_OPERAND (t, 1), s);
3965 TREE_OPERAND (t, 2) = strip_compound_expr (TREE_OPERAND (t, 2), s);
3967 else if (TREE_CODE_CLASS (code) == '1')
3968 TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s);
3969 else if (TREE_CODE_CLASS (code) == '<'
3970 || TREE_CODE_CLASS (code) == '2')
3972 TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s);
3973 TREE_OPERAND (t, 1) = strip_compound_expr (TREE_OPERAND (t, 1), s);
3979 /* Return a node which has the indicated constant VALUE (either 0 or
3980 1), and is of the indicated TYPE. */
3983 constant_boolean_node (value, type)
3987 if (type == integer_type_node)
3988 return value ? integer_one_node : integer_zero_node;
3989 else if (TREE_CODE (type) == BOOLEAN_TYPE)
3990 return truthvalue_conversion (value ? integer_one_node :
3994 tree t = build_int_2 (value, 0);
3995 TREE_TYPE (t) = type;
4000 /* Utility function for the following routine, to see how complex a nesting of
4001 COND_EXPRs can be. EXPR is the expression and LIMIT is a count beyond which
4002 we don't care (to avoid spending too much time on complex expressions.). */
4005 count_cond (expr, lim)
4011 if (TREE_CODE (expr) != COND_EXPR)
4016 true = count_cond (TREE_OPERAND (expr, 1), lim - 1);
4017 false = count_cond (TREE_OPERAND (expr, 2), lim - 1 - true);
4018 return MIN (lim, 1 + true + false);
4021 /* Perform constant folding and related simplification of EXPR.
4022 The related simplifications include x*1 => x, x*0 => 0, etc.,
4023 and application of the associative law.
4024 NOP_EXPR conversions may be removed freely (as long as we
4025 are careful not to change the C type of the overall expression)
4026 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
4027 but we can constant-fold them if they have constant operands. */
4033 register tree t = expr;
4034 tree t1 = NULL_TREE;
4036 tree type = TREE_TYPE (expr);
4037 register tree arg0 = NULL_TREE, arg1 = NULL_TREE;
4038 register enum tree_code code = TREE_CODE (t);
4042 /* WINS will be nonzero when the switch is done
4043 if all operands are constant. */
4047 /* Don't try to process an RTL_EXPR since its operands aren't trees.
4048 Likewise for a SAVE_EXPR that's already been evaluated. */
4049 if (code == RTL_EXPR || (code == SAVE_EXPR && SAVE_EXPR_RTL (t)) != 0)
4052 /* Return right away if already constant. */
4053 if (TREE_CONSTANT (t))
4055 if (code == CONST_DECL)
4056 return DECL_INITIAL (t);
4060 #ifdef MAX_INTEGER_COMPUTATION_MODE
4061 check_max_integer_computation_mode (expr);
4064 kind = TREE_CODE_CLASS (code);
4065 if (code == NOP_EXPR || code == FLOAT_EXPR || code == CONVERT_EXPR)
4069 /* Special case for conversion ops that can have fixed point args. */
4070 arg0 = TREE_OPERAND (t, 0);
4072 /* Don't use STRIP_NOPS, because signedness of argument type matters. */
4074 STRIP_TYPE_NOPS (arg0);
4076 if (arg0 != 0 && TREE_CODE (arg0) == COMPLEX_CST)
4077 subop = TREE_REALPART (arg0);
4081 if (subop != 0 && TREE_CODE (subop) != INTEGER_CST
4082 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
4083 && TREE_CODE (subop) != REAL_CST
4084 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
4086 /* Note that TREE_CONSTANT isn't enough:
4087 static var addresses are constant but we can't
4088 do arithmetic on them. */
4091 else if (kind == 'e' || kind == '<'
4092 || kind == '1' || kind == '2' || kind == 'r')
4094 register int len = tree_code_length[(int) code];
4096 for (i = 0; i < len; i++)
4098 tree op = TREE_OPERAND (t, i);
4102 continue; /* Valid for CALL_EXPR, at least. */
4104 if (kind == '<' || code == RSHIFT_EXPR)
4106 /* Signedness matters here. Perhaps we can refine this
4108 STRIP_TYPE_NOPS (op);
4112 /* Strip any conversions that don't change the mode. */
4116 if (TREE_CODE (op) == COMPLEX_CST)
4117 subop = TREE_REALPART (op);
4121 if (TREE_CODE (subop) != INTEGER_CST
4122 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
4123 && TREE_CODE (subop) != REAL_CST
4124 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
4126 /* Note that TREE_CONSTANT isn't enough:
4127 static var addresses are constant but we can't
4128 do arithmetic on them. */
4138 /* If this is a commutative operation, and ARG0 is a constant, move it
4139 to ARG1 to reduce the number of tests below. */
4140 if ((code == PLUS_EXPR || code == MULT_EXPR || code == MIN_EXPR
4141 || code == MAX_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR
4142 || code == BIT_AND_EXPR)
4143 && (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST))
4145 tem = arg0; arg0 = arg1; arg1 = tem;
4147 tem = TREE_OPERAND (t, 0); TREE_OPERAND (t, 0) = TREE_OPERAND (t, 1);
4148 TREE_OPERAND (t, 1) = tem;
4151 /* Now WINS is set as described above,
4152 ARG0 is the first operand of EXPR,
4153 and ARG1 is the second operand (if it has more than one operand).
4155 First check for cases where an arithmetic operation is applied to a
4156 compound, conditional, or comparison operation. Push the arithmetic
4157 operation inside the compound or conditional to see if any folding
4158 can then be done. Convert comparison to conditional for this purpose.
4159 The also optimizes non-constant cases that used to be done in
4162 Before we do that, see if this is a BIT_AND_EXPR or a BIT_OR_EXPR,
4163 one of the operands is a comparison and the other is a comparison, a
4164 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
4165 code below would make the expression more complex. Change it to a
4166 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
4167 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
4169 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
4170 || code == EQ_EXPR || code == NE_EXPR)
4171 && ((truth_value_p (TREE_CODE (arg0))
4172 && (truth_value_p (TREE_CODE (arg1))
4173 || (TREE_CODE (arg1) == BIT_AND_EXPR
4174 && integer_onep (TREE_OPERAND (arg1, 1)))))
4175 || (truth_value_p (TREE_CODE (arg1))
4176 && (truth_value_p (TREE_CODE (arg0))
4177 || (TREE_CODE (arg0) == BIT_AND_EXPR
4178 && integer_onep (TREE_OPERAND (arg0, 1)))))))
4180 t = fold (build (code == BIT_AND_EXPR ? TRUTH_AND_EXPR
4181 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
4185 if (code == EQ_EXPR)
4186 t = invert_truthvalue (t);
4191 if (TREE_CODE_CLASS (code) == '1')
4193 if (TREE_CODE (arg0) == COMPOUND_EXPR)
4194 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
4195 fold (build1 (code, type, TREE_OPERAND (arg0, 1))));
4196 else if (TREE_CODE (arg0) == COND_EXPR)
4198 t = fold (build (COND_EXPR, type, TREE_OPERAND (arg0, 0),
4199 fold (build1 (code, type, TREE_OPERAND (arg0, 1))),
4200 fold (build1 (code, type, TREE_OPERAND (arg0, 2)))));
4202 /* If this was a conversion, and all we did was to move into
4203 inside the COND_EXPR, bring it back out. But leave it if
4204 it is a conversion from integer to integer and the
4205 result precision is no wider than a word since such a
4206 conversion is cheap and may be optimized away by combine,
4207 while it couldn't if it were outside the COND_EXPR. Then return
4208 so we don't get into an infinite recursion loop taking the
4209 conversion out and then back in. */
4211 if ((code == NOP_EXPR || code == CONVERT_EXPR
4212 || code == NON_LVALUE_EXPR)
4213 && TREE_CODE (t) == COND_EXPR
4214 && TREE_CODE (TREE_OPERAND (t, 1)) == code
4215 && TREE_CODE (TREE_OPERAND (t, 2)) == code
4216 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0))
4217 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 2), 0)))
4218 && ! (INTEGRAL_TYPE_P (TREE_TYPE (t))
4219 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0)))
4220 && TYPE_PRECISION (TREE_TYPE (t)) <= BITS_PER_WORD))
4221 t = build1 (code, type,
4223 TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0)),
4224 TREE_OPERAND (t, 0),
4225 TREE_OPERAND (TREE_OPERAND (t, 1), 0),
4226 TREE_OPERAND (TREE_OPERAND (t, 2), 0)));
4229 else if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<')
4230 return fold (build (COND_EXPR, type, arg0,
4231 fold (build1 (code, type, integer_one_node)),
4232 fold (build1 (code, type, integer_zero_node))));
4234 else if (TREE_CODE_CLASS (code) == '2'
4235 || TREE_CODE_CLASS (code) == '<')
4237 if (TREE_CODE (arg1) == COMPOUND_EXPR)
4238 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
4239 fold (build (code, type,
4240 arg0, TREE_OPERAND (arg1, 1))));
4241 else if ((TREE_CODE (arg1) == COND_EXPR
4242 || (TREE_CODE_CLASS (TREE_CODE (arg1)) == '<'
4243 && TREE_CODE_CLASS (code) != '<'))
4244 && (TREE_CODE (arg0) != COND_EXPR
4245 || count_cond (arg0, 25) + count_cond (arg1, 25) <= 25)
4246 && (! TREE_SIDE_EFFECTS (arg0)
4247 || (current_function_decl != 0
4248 && ! contains_placeholder_p (arg0))))
4250 tree test, true_value, false_value;
4251 tree lhs = 0, rhs = 0;
4253 if (TREE_CODE (arg1) == COND_EXPR)
4255 test = TREE_OPERAND (arg1, 0);
4256 true_value = TREE_OPERAND (arg1, 1);
4257 false_value = TREE_OPERAND (arg1, 2);
4261 tree testtype = TREE_TYPE (arg1);
4263 true_value = convert (testtype, integer_one_node);
4264 false_value = convert (testtype, integer_zero_node);
4267 /* If ARG0 is complex we want to make sure we only evaluate
4268 it once. Though this is only required if it is volatile, it
4269 might be more efficient even if it is not. However, if we
4270 succeed in folding one part to a constant, we do not need
4271 to make this SAVE_EXPR. Since we do this optimization
4272 primarily to see if we do end up with constant and this
4273 SAVE_EXPR interferes with later optimizations, suppressing
4274 it when we can is important.
4276 If we are not in a function, we can't make a SAVE_EXPR, so don't
4277 try to do so. Don't try to see if the result is a constant
4278 if an arm is a COND_EXPR since we get exponential behavior
4281 if (TREE_CODE (arg0) != SAVE_EXPR && ! TREE_CONSTANT (arg0)
4282 && current_function_decl != 0
4283 && ((TREE_CODE (arg0) != VAR_DECL
4284 && TREE_CODE (arg0) != PARM_DECL)
4285 || TREE_SIDE_EFFECTS (arg0)))
4287 if (TREE_CODE (true_value) != COND_EXPR)
4288 lhs = fold (build (code, type, arg0, true_value));
4290 if (TREE_CODE (false_value) != COND_EXPR)
4291 rhs = fold (build (code, type, arg0, false_value));
4293 if ((lhs == 0 || ! TREE_CONSTANT (lhs))
4294 && (rhs == 0 || !TREE_CONSTANT (rhs)))
4295 arg0 = save_expr (arg0), lhs = rhs = 0;
4299 lhs = fold (build (code, type, arg0, true_value));
4301 rhs = fold (build (code, type, arg0, false_value));
4303 test = fold (build (COND_EXPR, type, test, lhs, rhs));
4305 if (TREE_CODE (arg0) == SAVE_EXPR)
4306 return build (COMPOUND_EXPR, type,
4307 convert (void_type_node, arg0),
4308 strip_compound_expr (test, arg0));
4310 return convert (type, test);
4313 else if (TREE_CODE (arg0) == COMPOUND_EXPR)
4314 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
4315 fold (build (code, type, TREE_OPERAND (arg0, 1), arg1)));
4316 else if ((TREE_CODE (arg0) == COND_EXPR
4317 || (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
4318 && TREE_CODE_CLASS (code) != '<'))
4319 && (TREE_CODE (arg1) != COND_EXPR
4320 || count_cond (arg0, 25) + count_cond (arg1, 25) <= 25)
4321 && (! TREE_SIDE_EFFECTS (arg1)
4322 || (current_function_decl != 0
4323 && ! contains_placeholder_p (arg1))))
4325 tree test, true_value, false_value;
4326 tree lhs = 0, rhs = 0;
4328 if (TREE_CODE (arg0) == COND_EXPR)
4330 test = TREE_OPERAND (arg0, 0);
4331 true_value = TREE_OPERAND (arg0, 1);
4332 false_value = TREE_OPERAND (arg0, 2);
4336 tree testtype = TREE_TYPE (arg0);
4338 true_value = convert (testtype, integer_one_node);
4339 false_value = convert (testtype, integer_zero_node);
4342 if (TREE_CODE (arg1) != SAVE_EXPR && ! TREE_CONSTANT (arg0)
4343 && current_function_decl != 0
4344 && ((TREE_CODE (arg1) != VAR_DECL
4345 && TREE_CODE (arg1) != PARM_DECL)
4346 || TREE_SIDE_EFFECTS (arg1)))
4348 if (TREE_CODE (true_value) != COND_EXPR)
4349 lhs = fold (build (code, type, true_value, arg1));
4351 if (TREE_CODE (false_value) != COND_EXPR)
4352 rhs = fold (build (code, type, false_value, arg1));
4354 if ((lhs == 0 || ! TREE_CONSTANT (lhs))
4355 && (rhs == 0 || !TREE_CONSTANT (rhs)))
4356 arg1 = save_expr (arg1), lhs = rhs = 0;
4360 lhs = fold (build (code, type, true_value, arg1));
4363 rhs = fold (build (code, type, false_value, arg1));
4365 test = fold (build (COND_EXPR, type, test, lhs, rhs));
4366 if (TREE_CODE (arg1) == SAVE_EXPR)
4367 return build (COMPOUND_EXPR, type,
4368 convert (void_type_node, arg1),
4369 strip_compound_expr (test, arg1));
4371 return convert (type, test);
4374 else if (TREE_CODE_CLASS (code) == '<'
4375 && TREE_CODE (arg0) == COMPOUND_EXPR)
4376 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
4377 fold (build (code, type, TREE_OPERAND (arg0, 1), arg1)));
4378 else if (TREE_CODE_CLASS (code) == '<'
4379 && TREE_CODE (arg1) == COMPOUND_EXPR)
4380 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
4381 fold (build (code, type, arg0, TREE_OPERAND (arg1, 1))));
4393 return fold (DECL_INITIAL (t));
4398 case FIX_TRUNC_EXPR:
4399 /* Other kinds of FIX are not handled properly by fold_convert. */
4401 if (TREE_TYPE (TREE_OPERAND (t, 0)) == TREE_TYPE (t))
4402 return TREE_OPERAND (t, 0);
4404 /* Handle cases of two conversions in a row. */
4405 if (TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
4406 || TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR)
4408 tree inside_type = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
4409 tree inter_type = TREE_TYPE (TREE_OPERAND (t, 0));
4410 tree final_type = TREE_TYPE (t);
4411 int inside_int = INTEGRAL_TYPE_P (inside_type);
4412 int inside_ptr = POINTER_TYPE_P (inside_type);
4413 int inside_float = FLOAT_TYPE_P (inside_type);
4414 int inside_prec = TYPE_PRECISION (inside_type);
4415 int inside_unsignedp = TREE_UNSIGNED (inside_type);
4416 int inter_int = INTEGRAL_TYPE_P (inter_type);
4417 int inter_ptr = POINTER_TYPE_P (inter_type);
4418 int inter_float = FLOAT_TYPE_P (inter_type);
4419 int inter_prec = TYPE_PRECISION (inter_type);
4420 int inter_unsignedp = TREE_UNSIGNED (inter_type);
4421 int final_int = INTEGRAL_TYPE_P (final_type);
4422 int final_ptr = POINTER_TYPE_P (final_type);
4423 int final_float = FLOAT_TYPE_P (final_type);
4424 int final_prec = TYPE_PRECISION (final_type);
4425 int final_unsignedp = TREE_UNSIGNED (final_type);
4427 /* In addition to the cases of two conversions in a row
4428 handled below, if we are converting something to its own
4429 type via an object of identical or wider precision, neither
4430 conversion is needed. */
4431 if (inside_type == final_type
4432 && ((inter_int && final_int) || (inter_float && final_float))
4433 && inter_prec >= final_prec)
4434 return TREE_OPERAND (TREE_OPERAND (t, 0), 0);
4436 /* Likewise, if the intermediate and final types are either both
4437 float or both integer, we don't need the middle conversion if
4438 it is wider than the final type and doesn't change the signedness
4439 (for integers). Avoid this if the final type is a pointer
4440 since then we sometimes need the inner conversion. Likewise if
4441 the outer has a precision not equal to the size of its mode. */
4442 if ((((inter_int || inter_ptr) && (inside_int || inside_ptr))
4443 || (inter_float && inside_float))
4444 && inter_prec >= inside_prec
4445 && (inter_float || inter_unsignedp == inside_unsignedp)
4446 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (final_type))
4447 && TYPE_MODE (final_type) == TYPE_MODE (inter_type))
4449 return convert (final_type, TREE_OPERAND (TREE_OPERAND (t, 0), 0));
4451 /* If we have a sign-extension of a zero-extended value, we can
4452 replace that by a single zero-extension. */
4453 if (inside_int && inter_int && final_int
4454 && inside_prec < inter_prec && inter_prec < final_prec
4455 && inside_unsignedp && !inter_unsignedp)
4456 return convert (final_type, TREE_OPERAND (TREE_OPERAND (t, 0), 0));
4458 /* Two conversions in a row are not needed unless:
4459 - some conversion is floating-point (overstrict for now), or
4460 - the intermediate type is narrower than both initial and
4462 - the intermediate type and innermost type differ in signedness,
4463 and the outermost type is wider than the intermediate, or
4464 - the initial type is a pointer type and the precisions of the
4465 intermediate and final types differ, or
4466 - the final type is a pointer type and the precisions of the
4467 initial and intermediate types differ. */
4468 if (! inside_float && ! inter_float && ! final_float
4469 && (inter_prec > inside_prec || inter_prec > final_prec)
4470 && ! (inside_int && inter_int
4471 && inter_unsignedp != inside_unsignedp
4472 && inter_prec < final_prec)
4473 && ((inter_unsignedp && inter_prec > inside_prec)
4474 == (final_unsignedp && final_prec > inter_prec))
4475 && ! (inside_ptr && inter_prec != final_prec)
4476 && ! (final_ptr && inside_prec != inter_prec)
4477 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (final_type))
4478 && TYPE_MODE (final_type) == TYPE_MODE (inter_type))
4480 return convert (final_type, TREE_OPERAND (TREE_OPERAND (t, 0), 0));
4483 if (TREE_CODE (TREE_OPERAND (t, 0)) == MODIFY_EXPR
4484 && TREE_CONSTANT (TREE_OPERAND (TREE_OPERAND (t, 0), 1))
4485 /* Detect assigning a bitfield. */
4486 && !(TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) == COMPONENT_REF
4487 && DECL_BIT_FIELD (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 1))))
4489 /* Don't leave an assignment inside a conversion
4490 unless assigning a bitfield. */
4491 tree prev = TREE_OPERAND (t, 0);
4492 TREE_OPERAND (t, 0) = TREE_OPERAND (prev, 1);
4493 /* First do the assignment, then return converted constant. */
4494 t = build (COMPOUND_EXPR, TREE_TYPE (t), prev, fold (t));
4500 TREE_CONSTANT (t) = TREE_CONSTANT (arg0);
4503 return fold_convert (t, arg0);
4505 #if 0 /* This loses on &"foo"[0]. */
4510 /* Fold an expression like: "foo"[2] */
4511 if (TREE_CODE (arg0) == STRING_CST
4512 && TREE_CODE (arg1) == INTEGER_CST
4513 && !TREE_INT_CST_HIGH (arg1)
4514 && (i = TREE_INT_CST_LOW (arg1)) < TREE_STRING_LENGTH (arg0))
4516 t = build_int_2 (TREE_STRING_POINTER (arg0)[i], 0);
4517 TREE_TYPE (t) = TREE_TYPE (TREE_TYPE (arg0));
4518 force_fit_type (t, 0);
4525 if (TREE_CODE (arg0) == CONSTRUCTOR)
4527 tree m = purpose_member (arg1, CONSTRUCTOR_ELTS (arg0));
4534 TREE_CONSTANT (t) = wins;
4540 if (TREE_CODE (arg0) == INTEGER_CST)
4542 HOST_WIDE_INT low, high;
4543 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
4544 TREE_INT_CST_HIGH (arg0),
4546 t = build_int_2 (low, high);
4547 TREE_TYPE (t) = type;
4549 = (TREE_OVERFLOW (arg0)
4550 | force_fit_type (t, overflow && !TREE_UNSIGNED (type)));
4551 TREE_CONSTANT_OVERFLOW (t)
4552 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg0);
4554 else if (TREE_CODE (arg0) == REAL_CST)
4555 t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
4557 else if (TREE_CODE (arg0) == NEGATE_EXPR)
4558 return TREE_OPERAND (arg0, 0);
4560 /* Convert - (a - b) to (b - a) for non-floating-point. */
4561 else if (TREE_CODE (arg0) == MINUS_EXPR && ! FLOAT_TYPE_P (type))
4562 return build (MINUS_EXPR, type, TREE_OPERAND (arg0, 1),
4563 TREE_OPERAND (arg0, 0));
4570 if (TREE_CODE (arg0) == INTEGER_CST)
4572 if (! TREE_UNSIGNED (type)
4573 && TREE_INT_CST_HIGH (arg0) < 0)
4575 HOST_WIDE_INT low, high;
4576 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
4577 TREE_INT_CST_HIGH (arg0),
4579 t = build_int_2 (low, high);
4580 TREE_TYPE (t) = type;
4582 = (TREE_OVERFLOW (arg0)
4583 | force_fit_type (t, overflow));
4584 TREE_CONSTANT_OVERFLOW (t)
4585 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg0);
4588 else if (TREE_CODE (arg0) == REAL_CST)
4590 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
4591 t = build_real (type,
4592 REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
4595 else if (TREE_CODE (arg0) == ABS_EXPR || TREE_CODE (arg0) == NEGATE_EXPR)
4596 return build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0));
4600 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
4602 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
4603 return build (COMPLEX_EXPR, TREE_TYPE (arg0),
4604 TREE_OPERAND (arg0, 0),
4605 fold (build1 (NEGATE_EXPR,
4606 TREE_TYPE (TREE_TYPE (arg0)),
4607 TREE_OPERAND (arg0, 1))));
4608 else if (TREE_CODE (arg0) == COMPLEX_CST)
4609 return build_complex (type, TREE_OPERAND (arg0, 0),
4610 fold (build1 (NEGATE_EXPR,
4611 TREE_TYPE (TREE_TYPE (arg0)),
4612 TREE_OPERAND (arg0, 1))));
4613 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
4614 return fold (build (TREE_CODE (arg0), type,
4615 fold (build1 (CONJ_EXPR, type,
4616 TREE_OPERAND (arg0, 0))),
4617 fold (build1 (CONJ_EXPR,
4618 type, TREE_OPERAND (arg0, 1)))));
4619 else if (TREE_CODE (arg0) == CONJ_EXPR)
4620 return TREE_OPERAND (arg0, 0);
4626 t = build_int_2 (~ TREE_INT_CST_LOW (arg0),
4627 ~ TREE_INT_CST_HIGH (arg0));
4628 TREE_TYPE (t) = type;
4629 force_fit_type (t, 0);
4630 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg0);
4631 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (arg0);
4633 else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
4634 return TREE_OPERAND (arg0, 0);
4638 /* A + (-B) -> A - B */
4639 if (TREE_CODE (arg1) == NEGATE_EXPR)
4640 return fold (build (MINUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
4641 else if (! FLOAT_TYPE_P (type))
4643 if (integer_zerop (arg1))
4644 return non_lvalue (convert (type, arg0));
4646 /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
4647 with a constant, and the two constants have no bits in common,
4648 we should treat this as a BIT_IOR_EXPR since this may produce more
4650 if (TREE_CODE (arg0) == BIT_AND_EXPR
4651 && TREE_CODE (arg1) == BIT_AND_EXPR
4652 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
4653 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
4654 && integer_zerop (const_binop (BIT_AND_EXPR,
4655 TREE_OPERAND (arg0, 1),
4656 TREE_OPERAND (arg1, 1), 0)))
4658 code = BIT_IOR_EXPR;
4662 if (TREE_CODE (arg0) == MULT_EXPR && TREE_CODE (arg1) == MULT_EXPR)
4664 tree arg00, arg01, arg10, arg11;
4665 tree alt0, alt1, same;
4667 /* (A * C) + (B * C) -> (A+B) * C.
4668 We are most concerned about the case where C is a constant,
4669 but other combinations show up during loop reduction. Since
4670 it is not difficult, try all four possibilities. */
4672 arg00 = TREE_OPERAND (arg0, 0);
4673 arg01 = TREE_OPERAND (arg0, 1);
4674 arg10 = TREE_OPERAND (arg1, 0);
4675 arg11 = TREE_OPERAND (arg1, 1);
4678 if (operand_equal_p (arg01, arg11, 0))
4679 same = arg01, alt0 = arg00, alt1 = arg10;
4680 else if (operand_equal_p (arg00, arg10, 0))
4681 same = arg00, alt0 = arg01, alt1 = arg11;
4682 else if (operand_equal_p (arg00, arg11, 0))
4683 same = arg00, alt0 = arg01, alt1 = arg10;
4684 else if (operand_equal_p (arg01, arg10, 0))
4685 same = arg01, alt0 = arg00, alt1 = arg11;
4688 return fold (build (MULT_EXPR, type,
4689 fold (build (PLUS_EXPR, type, alt0, alt1)),
4693 /* In IEEE floating point, x+0 may not equal x. */
4694 else if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
4696 && real_zerop (arg1))
4697 return non_lvalue (convert (type, arg0));
4699 /* In most languages, can't associate operations on floats
4700 through parentheses. Rather than remember where the parentheses
4701 were, we don't associate floats at all. It shouldn't matter much.
4702 However, associating multiplications is only very slightly
4703 inaccurate, so do that if -ffast-math is specified. */
4704 if (FLOAT_TYPE_P (type)
4705 && ! (flag_fast_math && code == MULT_EXPR))
4708 /* The varsign == -1 cases happen only for addition and subtraction.
4709 It says that the arg that was split was really CON minus VAR.
4710 The rest of the code applies to all associative operations. */
4716 if (split_tree (arg0, code, &var, &con, &varsign))
4720 /* EXPR is (CON-VAR) +- ARG1. */
4721 /* If it is + and VAR==ARG1, return just CONST. */
4722 if (code == PLUS_EXPR && operand_equal_p (var, arg1, 0))
4723 return convert (TREE_TYPE (t), con);
4725 /* If ARG0 is a constant, don't change things around;
4726 instead keep all the constant computations together. */
4728 if (TREE_CONSTANT (arg0))
4731 /* Otherwise return (CON +- ARG1) - VAR. */
4732 t = build (MINUS_EXPR, type,
4733 fold (build (code, type, con, arg1)), var);
4737 /* EXPR is (VAR+CON) +- ARG1. */
4738 /* If it is - and VAR==ARG1, return just CONST. */
4739 if (code == MINUS_EXPR && operand_equal_p (var, arg1, 0))
4740 return convert (TREE_TYPE (t), con);
4742 /* If ARG0 is a constant, don't change things around;
4743 instead keep all the constant computations together. */
4745 if (TREE_CONSTANT (arg0))
4748 /* Otherwise return VAR +- (ARG1 +- CON). */
4749 tem = fold (build (code, type, arg1, con));
4750 t = build (code, type, var, tem);
4752 if (integer_zerop (tem)
4753 && (code == PLUS_EXPR || code == MINUS_EXPR))
4754 return convert (type, var);
4755 /* If we have x +/- (c - d) [c an explicit integer]
4756 change it to x -/+ (d - c) since if d is relocatable
4757 then the latter can be a single immediate insn
4758 and the former cannot. */
4759 if (TREE_CODE (tem) == MINUS_EXPR
4760 && TREE_CODE (TREE_OPERAND (tem, 0)) == INTEGER_CST)
4762 tree tem1 = TREE_OPERAND (tem, 1);
4763 TREE_OPERAND (tem, 1) = TREE_OPERAND (tem, 0);
4764 TREE_OPERAND (tem, 0) = tem1;
4766 (code == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR));
4772 if (split_tree (arg1, code, &var, &con, &varsign))
4774 if (TREE_CONSTANT (arg1))
4779 (code == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR));
4781 /* EXPR is ARG0 +- (CON +- VAR). */
4782 if (TREE_CODE (t) == MINUS_EXPR
4783 && operand_equal_p (var, arg0, 0))
4785 /* If VAR and ARG0 cancel, return just CON or -CON. */
4786 if (code == PLUS_EXPR)
4787 return convert (TREE_TYPE (t), con);
4788 return fold (build1 (NEGATE_EXPR, TREE_TYPE (t),
4789 convert (TREE_TYPE (t), con)));
4792 t = build (TREE_CODE (t), type,
4793 fold (build (code, TREE_TYPE (t), arg0, con)), var);
4795 if (integer_zerop (TREE_OPERAND (t, 0))
4796 && TREE_CODE (t) == PLUS_EXPR)
4797 return convert (TREE_TYPE (t), var);
4802 #if defined (REAL_IS_NOT_DOUBLE) && ! defined (REAL_ARITHMETIC)
4803 if (TREE_CODE (arg1) == REAL_CST)
4805 #endif /* REAL_IS_NOT_DOUBLE, and no REAL_ARITHMETIC */
4807 t1 = const_binop (code, arg0, arg1, 0);
4808 if (t1 != NULL_TREE)
4810 /* The return value should always have
4811 the same type as the original expression. */
4812 if (TREE_TYPE (t1) != TREE_TYPE (t))
4813 t1 = convert (TREE_TYPE (t), t1);
4820 if (! FLOAT_TYPE_P (type))
4822 if (! wins && integer_zerop (arg0))
4823 return build1 (NEGATE_EXPR, type, arg1);
4824 if (integer_zerop (arg1))
4825 return non_lvalue (convert (type, arg0));
4827 /* (A * C) - (B * C) -> (A-B) * C. Since we are most concerned
4828 about the case where C is a constant, just try one of the
4829 four possibilities. */
4831 if (TREE_CODE (arg0) == MULT_EXPR && TREE_CODE (arg1) == MULT_EXPR
4832 && operand_equal_p (TREE_OPERAND (arg0, 1),
4833 TREE_OPERAND (arg1, 1), 0))
4834 return fold (build (MULT_EXPR, type,
4835 fold (build (MINUS_EXPR, type,
4836 TREE_OPERAND (arg0, 0),
4837 TREE_OPERAND (arg1, 0))),
4838 TREE_OPERAND (arg0, 1)));
4840 /* Convert A - (-B) to A + B. */
4841 else if (TREE_CODE (arg1) == NEGATE_EXPR)
4842 return fold (build (PLUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
4844 else if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
4847 /* Except with IEEE floating point, 0-x equals -x. */
4848 if (! wins && real_zerop (arg0))
4849 return build1 (NEGATE_EXPR, type, arg1);
4850 /* Except with IEEE floating point, x-0 equals x. */
4851 if (real_zerop (arg1))
4852 return non_lvalue (convert (type, arg0));
4855 /* Fold &x - &x. This can happen from &x.foo - &x.
4856 This is unsafe for certain floats even in non-IEEE formats.
4857 In IEEE, it is unsafe because it does wrong for NaNs.
4858 Also note that operand_equal_p is always false if an operand
4861 if ((! FLOAT_TYPE_P (type) || flag_fast_math)
4862 && operand_equal_p (arg0, arg1, 0))
4863 return convert (type, integer_zero_node);
4868 if (! FLOAT_TYPE_P (type))
4870 if (integer_zerop (arg1))
4871 return omit_one_operand (type, arg1, arg0);
4872 if (integer_onep (arg1))
4873 return non_lvalue (convert (type, arg0));
4875 /* ((A / C) * C) is A if the division is an
4876 EXACT_DIV_EXPR. Since C is normally a constant,
4877 just check for one of the four possibilities. */
4879 if (TREE_CODE (arg0) == EXACT_DIV_EXPR
4880 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
4881 return TREE_OPERAND (arg0, 0);
4883 /* (a * (1 << b)) is (a << b) */
4884 if (TREE_CODE (arg1) == LSHIFT_EXPR
4885 && integer_onep (TREE_OPERAND (arg1, 0)))
4886 return fold (build (LSHIFT_EXPR, type, arg0,
4887 TREE_OPERAND (arg1, 1)));
4888 if (TREE_CODE (arg0) == LSHIFT_EXPR
4889 && integer_onep (TREE_OPERAND (arg0, 0)))
4890 return fold (build (LSHIFT_EXPR, type, arg1,
4891 TREE_OPERAND (arg0, 1)));
4895 /* x*0 is 0, except for IEEE floating point. */
4896 if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
4898 && real_zerop (arg1))
4899 return omit_one_operand (type, arg1, arg0);
4900 /* In IEEE floating point, x*1 is not equivalent to x for snans.
4901 However, ANSI says we can drop signals,
4902 so we can do this anyway. */
4903 if (real_onep (arg1))
4904 return non_lvalue (convert (type, arg0));
4906 if (! wins && real_twop (arg1) && current_function_decl != 0
4907 && ! contains_placeholder_p (arg0))
4909 tree arg = save_expr (arg0);
4910 return build (PLUS_EXPR, type, arg, arg);
4918 register enum tree_code code0, code1;
4920 if (integer_all_onesp (arg1))
4921 return omit_one_operand (type, arg1, arg0);
4922 if (integer_zerop (arg1))
4923 return non_lvalue (convert (type, arg0));
4924 t1 = distribute_bit_expr (code, type, arg0, arg1);
4925 if (t1 != NULL_TREE)
4928 /* (A << C1) | (A >> C2) if A is unsigned and C1+C2 is the size of A
4929 is a rotate of A by C1 bits. */
4930 /* (A << B) | (A >> (Z - B)) if A is unsigned and Z is the size of A
4931 is a rotate of A by B bits. */
4933 code0 = TREE_CODE (arg0);
4934 code1 = TREE_CODE (arg1);
4935 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
4936 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
4937 && operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1,0), 0)
4938 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
4940 register tree tree01, tree11;
4941 register enum tree_code code01, code11;
4943 tree01 = TREE_OPERAND (arg0, 1);
4944 tree11 = TREE_OPERAND (arg1, 1);
4945 code01 = TREE_CODE (tree01);
4946 code11 = TREE_CODE (tree11);
4947 if (code01 == INTEGER_CST
4948 && code11 == INTEGER_CST
4949 && TREE_INT_CST_HIGH (tree01) == 0
4950 && TREE_INT_CST_HIGH (tree11) == 0
4951 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
4952 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
4953 return build (LROTATE_EXPR, type, TREE_OPERAND (arg0, 0),
4954 code0 == LSHIFT_EXPR ? tree01 : tree11);
4955 else if (code11 == MINUS_EXPR
4956 && TREE_CODE (TREE_OPERAND (tree11, 0)) == INTEGER_CST
4957 && TREE_INT_CST_HIGH (TREE_OPERAND (tree11, 0)) == 0
4958 && TREE_INT_CST_LOW (TREE_OPERAND (tree11, 0))
4959 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))
4960 && operand_equal_p (tree01, TREE_OPERAND (tree11, 1), 0))
4961 return build (code0 == LSHIFT_EXPR ? LROTATE_EXPR : RROTATE_EXPR,
4962 type, TREE_OPERAND (arg0, 0), tree01);
4963 else if (code01 == MINUS_EXPR
4964 && TREE_CODE (TREE_OPERAND (tree01, 0)) == INTEGER_CST
4965 && TREE_INT_CST_HIGH (TREE_OPERAND (tree01, 0)) == 0
4966 && TREE_INT_CST_LOW (TREE_OPERAND (tree01, 0))
4967 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))
4968 && operand_equal_p (tree11, TREE_OPERAND (tree01, 1), 0))
4969 return build (code0 != LSHIFT_EXPR ? LROTATE_EXPR : RROTATE_EXPR,
4970 type, TREE_OPERAND (arg0, 0), tree11);
4977 if (integer_zerop (arg1))
4978 return non_lvalue (convert (type, arg0));
4979 if (integer_all_onesp (arg1))
4980 return fold (build1 (BIT_NOT_EXPR, type, arg0));
4985 if (integer_all_onesp (arg1))
4986 return non_lvalue (convert (type, arg0));
4987 if (integer_zerop (arg1))
4988 return omit_one_operand (type, arg1, arg0);
4989 t1 = distribute_bit_expr (code, type, arg0, arg1);
4990 if (t1 != NULL_TREE)
4992 /* Simplify ((int)c & 0x377) into (int)c, if c is unsigned char. */
4993 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == NOP_EXPR
4994 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0))))
4996 int prec = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)));
4997 if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
4998 && (~TREE_INT_CST_LOW (arg0)
4999 & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
5000 return build1 (NOP_EXPR, type, TREE_OPERAND (arg1, 0));
5002 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
5003 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
5005 int prec = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
5006 if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
5007 && (~TREE_INT_CST_LOW (arg1)
5008 & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
5009 return build1 (NOP_EXPR, type, TREE_OPERAND (arg0, 0));
5013 case BIT_ANDTC_EXPR:
5014 if (integer_all_onesp (arg0))
5015 return non_lvalue (convert (type, arg1));
5016 if (integer_zerop (arg0))
5017 return omit_one_operand (type, arg0, arg1);
5018 if (TREE_CODE (arg1) == INTEGER_CST)
5020 arg1 = fold (build1 (BIT_NOT_EXPR, type, arg1));
5021 code = BIT_AND_EXPR;
5027 /* In most cases, do nothing with a divide by zero. */
5028 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
5029 #ifndef REAL_INFINITY
5030 if (TREE_CODE (arg1) == REAL_CST && real_zerop (arg1))
5033 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
5035 /* In IEEE floating point, x/1 is not equivalent to x for snans.
5036 However, ANSI says we can drop signals, so we can do this anyway. */
5037 if (real_onep (arg1))
5038 return non_lvalue (convert (type, arg0));
5040 /* If ARG1 is a constant, we can convert this to a multiply by the
5041 reciprocal. This does not have the same rounding properties,
5042 so only do this if -ffast-math. We can actually always safely
5043 do it if ARG1 is a power of two, but it's hard to tell if it is
5044 or not in a portable manner. */
5045 if (TREE_CODE (arg1) == REAL_CST)
5048 && 0 != (tem = const_binop (code, build_real (type, dconst1),
5050 return fold (build (MULT_EXPR, type, arg0, tem));
5051 /* Find the reciprocal if optimizing and the result is exact. */
5055 r = TREE_REAL_CST (arg1);
5056 if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
5058 tem = build_real (type, r);
5059 return fold (build (MULT_EXPR, type, arg0, tem));
5065 case TRUNC_DIV_EXPR:
5066 case ROUND_DIV_EXPR:
5067 case FLOOR_DIV_EXPR:
5069 case EXACT_DIV_EXPR:
5070 if (integer_onep (arg1))
5071 return non_lvalue (convert (type, arg0));
5072 if (integer_zerop (arg1))
5075 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
5076 operation, EXACT_DIV_EXPR.
5078 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
5079 At one time others generated faster code, it's not clear if they do
5080 after the last round to changes to the DIV code in expmed.c. */
5081 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
5082 && multiple_of_p (type, arg0, arg1))
5083 return fold (build (EXACT_DIV_EXPR, type, arg0, arg1));
5085 /* If we have ((a / C1) / C2) where both division are the same type, try
5086 to simplify. First see if C1 * C2 overflows or not. */
5087 if (TREE_CODE (arg0) == code && TREE_CODE (arg1) == INTEGER_CST
5088 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
5092 new_divisor = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 1), arg1, 0);
5093 tem = const_binop (FLOOR_DIV_EXPR, new_divisor, arg1, 0);
5095 if (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) == TREE_INT_CST_LOW (tem)
5096 && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == TREE_INT_CST_HIGH (tem))
5098 /* If no overflow, divide by C1*C2. */
5099 return fold (build (code, type, TREE_OPERAND (arg0, 0), new_divisor));
5103 /* Look for ((a * C1) / C3) or (((a * C1) + C2) / C3),
5104 where C1 % C3 == 0 or C3 % C1 == 0. We can simplify these
5105 expressions, which often appear in the offsets or sizes of
5106 objects with a varying size. Only deal with positive divisors
5107 and multiplicands. If C2 is negative, we must have C2 % C3 == 0.
5109 Look for NOPs and SAVE_EXPRs inside. */
5111 if (TREE_CODE (arg1) == INTEGER_CST
5112 && tree_int_cst_sgn (arg1) >= 0)
5114 int have_save_expr = 0;
5115 tree c2 = integer_zero_node;
5118 if (TREE_CODE (xarg0) == SAVE_EXPR && SAVE_EXPR_RTL (xarg0) == 0)
5119 have_save_expr = 1, xarg0 = TREE_OPERAND (xarg0, 0);
5123 /* Look inside the dividend and simplify using EXACT_DIV_EXPR
5125 if (TREE_CODE (xarg0) == MULT_EXPR
5126 && multiple_of_p (type, TREE_OPERAND (xarg0, 0), arg1))
5130 t = fold (build (MULT_EXPR, type,
5131 fold (build (EXACT_DIV_EXPR, type,
5132 TREE_OPERAND (xarg0, 0), arg1)),
5133 TREE_OPERAND (xarg0, 1)));
5140 if (TREE_CODE (xarg0) == MULT_EXPR
5141 && multiple_of_p (type, TREE_OPERAND (xarg0, 1), arg1))
5145 t = fold (build (MULT_EXPR, type,
5146 fold (build (EXACT_DIV_EXPR, type,
5147 TREE_OPERAND (xarg0, 1), arg1)),
5148 TREE_OPERAND (xarg0, 0)));
5154 if (TREE_CODE (xarg0) == PLUS_EXPR
5155 && TREE_CODE (TREE_OPERAND (xarg0, 1)) == INTEGER_CST)
5156 c2 = TREE_OPERAND (xarg0, 1), xarg0 = TREE_OPERAND (xarg0, 0);
5157 else if (TREE_CODE (xarg0) == MINUS_EXPR
5158 && TREE_CODE (TREE_OPERAND (xarg0, 1)) == INTEGER_CST
5159 /* If we are doing this computation unsigned, the negate
5161 && ! TREE_UNSIGNED (type))
5163 c2 = fold (build1 (NEGATE_EXPR, type, TREE_OPERAND (xarg0, 1)));
5164 xarg0 = TREE_OPERAND (xarg0, 0);
5167 if (TREE_CODE (xarg0) == SAVE_EXPR && SAVE_EXPR_RTL (xarg0) == 0)
5168 have_save_expr = 1, xarg0 = TREE_OPERAND (xarg0, 0);
5172 if (TREE_CODE (xarg0) == MULT_EXPR
5173 && TREE_CODE (TREE_OPERAND (xarg0, 1)) == INTEGER_CST
5174 && tree_int_cst_sgn (TREE_OPERAND (xarg0, 1)) >= 0
5175 && (integer_zerop (const_binop (TRUNC_MOD_EXPR,
5176 TREE_OPERAND (xarg0, 1), arg1, 1))
5177 || integer_zerop (const_binop (TRUNC_MOD_EXPR, arg1,
5178 TREE_OPERAND (xarg0, 1), 1)))
5179 && (tree_int_cst_sgn (c2) >= 0
5180 || integer_zerop (const_binop (TRUNC_MOD_EXPR, c2,
5183 tree outer_div = integer_one_node;
5184 tree c1 = TREE_OPERAND (xarg0, 1);
5187 /* If C3 > C1, set them equal and do a divide by
5188 C3/C1 at the end of the operation. */
5189 if (tree_int_cst_lt (c1, c3))
5190 outer_div = const_binop (code, c3, c1, 0), c3 = c1;
5192 /* The result is A * (C1/C3) + (C2/C3). */
5193 t = fold (build (PLUS_EXPR, type,
5194 fold (build (MULT_EXPR, type,
5195 TREE_OPERAND (xarg0, 0),
5196 const_binop (code, c1, c3, 1))),
5197 const_binop (code, c2, c3, 1)));
5199 if (! integer_onep (outer_div))
5200 t = fold (build (code, type, t, convert (type, outer_div)));
5212 case FLOOR_MOD_EXPR:
5213 case ROUND_MOD_EXPR:
5214 case TRUNC_MOD_EXPR:
5215 if (integer_onep (arg1))
5216 return omit_one_operand (type, integer_zero_node, arg0);
5217 if (integer_zerop (arg1))
5220 /* Look for ((a * C1) % C3) or (((a * C1) + C2) % C3),
5221 where C1 % C3 == 0. Handle similarly to the division case,
5222 but don't bother with SAVE_EXPRs. */
5224 if (TREE_CODE (arg1) == INTEGER_CST
5225 && ! integer_zerop (arg1))
5227 tree c2 = integer_zero_node;
5230 if (TREE_CODE (xarg0) == PLUS_EXPR
5231 && TREE_CODE (TREE_OPERAND (xarg0, 1)) == INTEGER_CST)
5232 c2 = TREE_OPERAND (xarg0, 1), xarg0 = TREE_OPERAND (xarg0, 0);
5233 else if (TREE_CODE (xarg0) == MINUS_EXPR
5234 && TREE_CODE (TREE_OPERAND (xarg0, 1)) == INTEGER_CST
5235 && ! TREE_UNSIGNED (type))
5237 c2 = fold (build1 (NEGATE_EXPR, type, TREE_OPERAND (xarg0, 1)));
5238 xarg0 = TREE_OPERAND (xarg0, 0);
5243 if (TREE_CODE (xarg0) == MULT_EXPR
5244 && TREE_CODE (TREE_OPERAND (xarg0, 1)) == INTEGER_CST
5245 && integer_zerop (const_binop (TRUNC_MOD_EXPR,
5246 TREE_OPERAND (xarg0, 1),
5248 && tree_int_cst_sgn (c2) >= 0)
5249 /* The result is (C2%C3). */
5250 return omit_one_operand (type, const_binop (code, c2, arg1, 1),
5251 TREE_OPERAND (xarg0, 0));
5260 if (integer_zerop (arg1))
5261 return non_lvalue (convert (type, arg0));
5262 /* Since negative shift count is not well-defined,
5263 don't try to compute it in the compiler. */
5264 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
5266 /* Rewrite an LROTATE_EXPR by a constant into an
5267 RROTATE_EXPR by a new constant. */
5268 if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
5270 TREE_SET_CODE (t, RROTATE_EXPR);
5271 code = RROTATE_EXPR;
5272 TREE_OPERAND (t, 1) = arg1
5275 convert (TREE_TYPE (arg1),
5276 build_int_2 (GET_MODE_BITSIZE (TYPE_MODE (type)), 0)),
5278 if (tree_int_cst_sgn (arg1) < 0)
5282 /* If we have a rotate of a bit operation with the rotate count and
5283 the second operand of the bit operation both constant,
5284 permute the two operations. */
5285 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
5286 && (TREE_CODE (arg0) == BIT_AND_EXPR
5287 || TREE_CODE (arg0) == BIT_ANDTC_EXPR
5288 || TREE_CODE (arg0) == BIT_IOR_EXPR
5289 || TREE_CODE (arg0) == BIT_XOR_EXPR)
5290 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
5291 return fold (build (TREE_CODE (arg0), type,
5292 fold (build (code, type,
5293 TREE_OPERAND (arg0, 0), arg1)),
5294 fold (build (code, type,
5295 TREE_OPERAND (arg0, 1), arg1))));
5297 /* Two consecutive rotates adding up to the width of the mode can
5299 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
5300 && TREE_CODE (arg0) == RROTATE_EXPR
5301 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
5302 && TREE_INT_CST_HIGH (arg1) == 0
5303 && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
5304 && ((TREE_INT_CST_LOW (arg1)
5305 + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
5306 == GET_MODE_BITSIZE (TYPE_MODE (type))))
5307 return TREE_OPERAND (arg0, 0);
5312 if (operand_equal_p (arg0, arg1, 0))
5314 if (INTEGRAL_TYPE_P (type)
5315 && operand_equal_p (arg1, TYPE_MIN_VALUE (type), 1))
5316 return omit_one_operand (type, arg1, arg0);
5320 if (operand_equal_p (arg0, arg1, 0))
5322 if (INTEGRAL_TYPE_P (type)
5323 && TYPE_MAX_VALUE (type)
5324 && operand_equal_p (arg1, TYPE_MAX_VALUE (type), 1))
5325 return omit_one_operand (type, arg1, arg0);
5328 case TRUTH_NOT_EXPR:
5329 /* Note that the operand of this must be an int
5330 and its values must be 0 or 1.
5331 ("true" is a fixed value perhaps depending on the language,
5332 but we don't handle values other than 1 correctly yet.) */
5333 tem = invert_truthvalue (arg0);
5334 /* Avoid infinite recursion. */
5335 if (TREE_CODE (tem) == TRUTH_NOT_EXPR)
5337 return convert (type, tem);
5339 case TRUTH_ANDIF_EXPR:
5340 /* Note that the operands of this must be ints
5341 and their values must be 0 or 1.
5342 ("true" is a fixed value perhaps depending on the language.) */
5343 /* If first arg is constant zero, return it. */
5344 if (integer_zerop (arg0))
5346 case TRUTH_AND_EXPR:
5347 /* If either arg is constant true, drop it. */
5348 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
5349 return non_lvalue (arg1);
5350 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
5351 return non_lvalue (arg0);
5352 /* If second arg is constant zero, result is zero, but first arg
5353 must be evaluated. */
5354 if (integer_zerop (arg1))
5355 return omit_one_operand (type, arg1, arg0);
5356 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
5357 case will be handled here. */
5358 if (integer_zerop (arg0))
5359 return omit_one_operand (type, arg0, arg1);
5362 /* We only do these simplifications if we are optimizing. */
5366 /* Check for things like (A || B) && (A || C). We can convert this
5367 to A || (B && C). Note that either operator can be any of the four
5368 truth and/or operations and the transformation will still be
5369 valid. Also note that we only care about order for the
5370 ANDIF and ORIF operators. If B contains side effects, this
5371 might change the truth-value of A. */
5372 if (TREE_CODE (arg0) == TREE_CODE (arg1)
5373 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
5374 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
5375 || TREE_CODE (arg0) == TRUTH_AND_EXPR
5376 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
5377 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
5379 tree a00 = TREE_OPERAND (arg0, 0);
5380 tree a01 = TREE_OPERAND (arg0, 1);
5381 tree a10 = TREE_OPERAND (arg1, 0);
5382 tree a11 = TREE_OPERAND (arg1, 1);
5383 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
5384 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
5385 && (code == TRUTH_AND_EXPR
5386 || code == TRUTH_OR_EXPR));
5388 if (operand_equal_p (a00, a10, 0))
5389 return fold (build (TREE_CODE (arg0), type, a00,
5390 fold (build (code, type, a01, a11))));
5391 else if (commutative && operand_equal_p (a00, a11, 0))
5392 return fold (build (TREE_CODE (arg0), type, a00,
5393 fold (build (code, type, a01, a10))));
5394 else if (commutative && operand_equal_p (a01, a10, 0))
5395 return fold (build (TREE_CODE (arg0), type, a01,
5396 fold (build (code, type, a00, a11))));
5398 /* This case if tricky because we must either have commutative
5399 operators or else A10 must not have side-effects. */
5401 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
5402 && operand_equal_p (a01, a11, 0))
5403 return fold (build (TREE_CODE (arg0), type,
5404 fold (build (code, type, a00, a10)),
5408 /* See if we can build a range comparison. */
5409 if (0 != (tem = fold_range_test (t)))
5412 /* Check for the possibility of merging component references. If our
5413 lhs is another similar operation, try to merge its rhs with our
5414 rhs. Then try to merge our lhs and rhs. */
5415 if (TREE_CODE (arg0) == code
5416 && 0 != (tem = fold_truthop (code, type,
5417 TREE_OPERAND (arg0, 1), arg1)))
5418 return fold (build (code, type, TREE_OPERAND (arg0, 0), tem));
5420 if ((tem = fold_truthop (code, type, arg0, arg1)) != 0)
5425 case TRUTH_ORIF_EXPR:
5426 /* Note that the operands of this must be ints
5427 and their values must be 0 or true.
5428 ("true" is a fixed value perhaps depending on the language.) */
5429 /* If first arg is constant true, return it. */
5430 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
5433 /* If either arg is constant zero, drop it. */
5434 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
5435 return non_lvalue (arg1);
5436 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1))
5437 return non_lvalue (arg0);
5438 /* If second arg is constant true, result is true, but we must
5439 evaluate first arg. */
5440 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
5441 return omit_one_operand (type, arg1, arg0);
5442 /* Likewise for first arg, but note this only occurs here for
5444 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
5445 return omit_one_operand (type, arg0, arg1);
5448 case TRUTH_XOR_EXPR:
5449 /* If either arg is constant zero, drop it. */
5450 if (integer_zerop (arg0))
5451 return non_lvalue (arg1);
5452 if (integer_zerop (arg1))
5453 return non_lvalue (arg0);
5454 /* If either arg is constant true, this is a logical inversion. */
5455 if (integer_onep (arg0))
5456 return non_lvalue (invert_truthvalue (arg1));
5457 if (integer_onep (arg1))
5458 return non_lvalue (invert_truthvalue (arg0));
5467 /* If one arg is a constant integer, put it last. */
5468 if (TREE_CODE (arg0) == INTEGER_CST
5469 && TREE_CODE (arg1) != INTEGER_CST)
5471 TREE_OPERAND (t, 0) = arg1;
5472 TREE_OPERAND (t, 1) = arg0;
5473 arg0 = TREE_OPERAND (t, 0);
5474 arg1 = TREE_OPERAND (t, 1);
5475 code = swap_tree_comparison (code);
5476 TREE_SET_CODE (t, code);
5479 /* Convert foo++ == CONST into ++foo == CONST + INCR.
5480 First, see if one arg is constant; find the constant arg
5481 and the other one. */
5483 tree constop = 0, varop = NULL_TREE;
5484 int constopnum = -1;
5486 if (TREE_CONSTANT (arg1))
5487 constopnum = 1, constop = arg1, varop = arg0;
5488 if (TREE_CONSTANT (arg0))
5489 constopnum = 0, constop = arg0, varop = arg1;
5491 if (constop && TREE_CODE (varop) == POSTINCREMENT_EXPR)
5493 /* This optimization is invalid for ordered comparisons
5494 if CONST+INCR overflows or if foo+incr might overflow.
5495 This optimization is invalid for floating point due to rounding.
5496 For pointer types we assume overflow doesn't happen. */
5497 if (POINTER_TYPE_P (TREE_TYPE (varop))
5498 || (! FLOAT_TYPE_P (TREE_TYPE (varop))
5499 && (code == EQ_EXPR || code == NE_EXPR)))
5502 = fold (build (PLUS_EXPR, TREE_TYPE (varop),
5503 constop, TREE_OPERAND (varop, 1)));
5504 TREE_SET_CODE (varop, PREINCREMENT_EXPR);
5506 /* If VAROP is a reference to a bitfield, we must mask
5507 the constant by the width of the field. */
5508 if (TREE_CODE (TREE_OPERAND (varop, 0)) == COMPONENT_REF
5509 && DECL_BIT_FIELD(TREE_OPERAND
5510 (TREE_OPERAND (varop, 0), 1)))
5513 = TREE_INT_CST_LOW (DECL_SIZE
5515 (TREE_OPERAND (varop, 0), 1)));
5516 tree mask, unsigned_type;
5518 tree folded_compare;
5520 /* First check whether the comparison would come out
5521 always the same. If we don't do that we would
5522 change the meaning with the masking. */
5523 if (constopnum == 0)
5524 folded_compare = fold (build (code, type, constop,
5525 TREE_OPERAND (varop, 0)));
5527 folded_compare = fold (build (code, type,
5528 TREE_OPERAND (varop, 0),
5530 if (integer_zerop (folded_compare)
5531 || integer_onep (folded_compare))
5532 return omit_one_operand (type, folded_compare, varop);
5534 unsigned_type = type_for_size (size, 1);
5535 precision = TYPE_PRECISION (unsigned_type);
5536 mask = build_int_2 (~0, ~0);
5537 TREE_TYPE (mask) = unsigned_type;
5538 force_fit_type (mask, 0);
5539 mask = const_binop (RSHIFT_EXPR, mask,
5540 size_int (precision - size), 0);
5541 newconst = fold (build (BIT_AND_EXPR,
5542 TREE_TYPE (varop), newconst,
5543 convert (TREE_TYPE (varop),
5548 t = build (code, type, TREE_OPERAND (t, 0),
5549 TREE_OPERAND (t, 1));
5550 TREE_OPERAND (t, constopnum) = newconst;
5554 else if (constop && TREE_CODE (varop) == POSTDECREMENT_EXPR)
5556 if (POINTER_TYPE_P (TREE_TYPE (varop))
5557 || (! FLOAT_TYPE_P (TREE_TYPE (varop))
5558 && (code == EQ_EXPR || code == NE_EXPR)))
5561 = fold (build (MINUS_EXPR, TREE_TYPE (varop),
5562 constop, TREE_OPERAND (varop, 1)));
5563 TREE_SET_CODE (varop, PREDECREMENT_EXPR);
5565 if (TREE_CODE (TREE_OPERAND (varop, 0)) == COMPONENT_REF
5566 && DECL_BIT_FIELD(TREE_OPERAND
5567 (TREE_OPERAND (varop, 0), 1)))
5570 = TREE_INT_CST_LOW (DECL_SIZE
5572 (TREE_OPERAND (varop, 0), 1)));
5573 tree mask, unsigned_type;
5575 tree folded_compare;
5577 if (constopnum == 0)
5578 folded_compare = fold (build (code, type, constop,
5579 TREE_OPERAND (varop, 0)));
5581 folded_compare = fold (build (code, type,
5582 TREE_OPERAND (varop, 0),
5584 if (integer_zerop (folded_compare)
5585 || integer_onep (folded_compare))
5586 return omit_one_operand (type, folded_compare, varop);
5588 unsigned_type = type_for_size (size, 1);
5589 precision = TYPE_PRECISION (unsigned_type);
5590 mask = build_int_2 (~0, ~0);
5591 TREE_TYPE (mask) = TREE_TYPE (varop);
5592 force_fit_type (mask, 0);
5593 mask = const_binop (RSHIFT_EXPR, mask,
5594 size_int (precision - size), 0);
5595 newconst = fold (build (BIT_AND_EXPR,
5596 TREE_TYPE (varop), newconst,
5597 convert (TREE_TYPE (varop),
5602 t = build (code, type, TREE_OPERAND (t, 0),
5603 TREE_OPERAND (t, 1));
5604 TREE_OPERAND (t, constopnum) = newconst;
5610 /* Change X >= CST to X > (CST - 1) if CST is positive. */
5611 if (TREE_CODE (arg1) == INTEGER_CST
5612 && TREE_CODE (arg0) != INTEGER_CST
5613 && tree_int_cst_sgn (arg1) > 0)
5615 switch (TREE_CODE (t))
5619 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
5620 t = build (code, type, TREE_OPERAND (t, 0), arg1);
5625 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
5626 t = build (code, type, TREE_OPERAND (t, 0), arg1);
5634 /* If this is an EQ or NE comparison with zero and ARG0 is
5635 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
5636 two operations, but the latter can be done in one less insn
5637 on machines that have only two-operand insns or on which a
5638 constant cannot be the first operand. */
5639 if (integer_zerop (arg1) && (code == EQ_EXPR || code == NE_EXPR)
5640 && TREE_CODE (arg0) == BIT_AND_EXPR)
5642 if (TREE_CODE (TREE_OPERAND (arg0, 0)) == LSHIFT_EXPR
5643 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 0), 0)))
5645 fold (build (code, type,
5646 build (BIT_AND_EXPR, TREE_TYPE (arg0),
5648 TREE_TYPE (TREE_OPERAND (arg0, 0)),
5649 TREE_OPERAND (arg0, 1),
5650 TREE_OPERAND (TREE_OPERAND (arg0, 0), 1)),
5651 convert (TREE_TYPE (arg0),
5654 else if (TREE_CODE (TREE_OPERAND (arg0, 1)) == LSHIFT_EXPR
5655 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 1), 0)))
5657 fold (build (code, type,
5658 build (BIT_AND_EXPR, TREE_TYPE (arg0),
5660 TREE_TYPE (TREE_OPERAND (arg0, 1)),
5661 TREE_OPERAND (arg0, 0),
5662 TREE_OPERAND (TREE_OPERAND (arg0, 1), 1)),
5663 convert (TREE_TYPE (arg0),
5668 /* If this is an NE or EQ comparison of zero against the result of a
5669 signed MOD operation whose second operand is a power of 2, make
5670 the MOD operation unsigned since it is simpler and equivalent. */
5671 if ((code == NE_EXPR || code == EQ_EXPR)
5672 && integer_zerop (arg1)
5673 && ! TREE_UNSIGNED (TREE_TYPE (arg0))
5674 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
5675 || TREE_CODE (arg0) == CEIL_MOD_EXPR
5676 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
5677 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
5678 && integer_pow2p (TREE_OPERAND (arg0, 1)))
5680 tree newtype = unsigned_type (TREE_TYPE (arg0));
5681 tree newmod = build (TREE_CODE (arg0), newtype,
5682 convert (newtype, TREE_OPERAND (arg0, 0)),
5683 convert (newtype, TREE_OPERAND (arg0, 1)));
5685 return build (code, type, newmod, convert (newtype, arg1));
5688 /* If this is an NE comparison of zero with an AND of one, remove the
5689 comparison since the AND will give the correct value. */
5690 if (code == NE_EXPR && integer_zerop (arg1)
5691 && TREE_CODE (arg0) == BIT_AND_EXPR
5692 && integer_onep (TREE_OPERAND (arg0, 1)))
5693 return convert (type, arg0);
5695 /* If we have (A & C) == C where C is a power of 2, convert this into
5696 (A & C) != 0. Similarly for NE_EXPR. */
5697 if ((code == EQ_EXPR || code == NE_EXPR)
5698 && TREE_CODE (arg0) == BIT_AND_EXPR
5699 && integer_pow2p (TREE_OPERAND (arg0, 1))
5700 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
5701 return build (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
5702 arg0, integer_zero_node);
5704 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
5705 and similarly for >= into !=. */
5706 if ((code == LT_EXPR || code == GE_EXPR)
5707 && TREE_UNSIGNED (TREE_TYPE (arg0))
5708 && TREE_CODE (arg1) == LSHIFT_EXPR
5709 && integer_onep (TREE_OPERAND (arg1, 0)))
5710 return build (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
5711 build (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
5712 TREE_OPERAND (arg1, 1)),
5713 convert (TREE_TYPE (arg0), integer_zero_node));
5715 else if ((code == LT_EXPR || code == GE_EXPR)
5716 && TREE_UNSIGNED (TREE_TYPE (arg0))
5717 && (TREE_CODE (arg1) == NOP_EXPR
5718 || TREE_CODE (arg1) == CONVERT_EXPR)
5719 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
5720 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
5722 build (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
5723 convert (TREE_TYPE (arg0),
5724 build (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
5725 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1))),
5726 convert (TREE_TYPE (arg0), integer_zero_node));
5728 /* Simplify comparison of something with itself. (For IEEE
5729 floating-point, we can only do some of these simplifications.) */
5730 if (operand_equal_p (arg0, arg1, 0))
5737 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
5738 return constant_boolean_node (1, type);
5740 TREE_SET_CODE (t, code);
5744 /* For NE, we can only do this simplification if integer. */
5745 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
5747 /* ... fall through ... */
5750 return constant_boolean_node (0, type);
5756 /* An unsigned comparison against 0 can be simplified. */
5757 if (integer_zerop (arg1)
5758 && (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
5759 || POINTER_TYPE_P (TREE_TYPE (arg1)))
5760 && TREE_UNSIGNED (TREE_TYPE (arg1)))
5762 switch (TREE_CODE (t))
5766 TREE_SET_CODE (t, NE_EXPR);
5770 TREE_SET_CODE (t, EQ_EXPR);
5773 return omit_one_operand (type,
5774 convert (type, integer_one_node),
5777 return omit_one_operand (type,
5778 convert (type, integer_zero_node),
5785 /* An unsigned <= 0x7fffffff can be simplified. */
5787 int width = TYPE_PRECISION (TREE_TYPE (arg1));
5788 if (TREE_CODE (arg1) == INTEGER_CST
5789 && ! TREE_CONSTANT_OVERFLOW (arg1)
5790 && width <= HOST_BITS_PER_WIDE_INT
5791 && TREE_INT_CST_LOW (arg1) == ((HOST_WIDE_INT) 1 << (width - 1)) - 1
5792 && TREE_INT_CST_HIGH (arg1) == 0
5793 && (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
5794 || POINTER_TYPE_P (TREE_TYPE (arg1)))
5795 && TREE_UNSIGNED (TREE_TYPE (arg1)))
5797 switch (TREE_CODE (t))
5800 return fold (build (GE_EXPR, type,
5801 convert (signed_type (TREE_TYPE (arg0)),
5803 convert (signed_type (TREE_TYPE (arg1)),
5804 integer_zero_node)));
5806 return fold (build (LT_EXPR, type,
5807 convert (signed_type (TREE_TYPE (arg0)),
5809 convert (signed_type (TREE_TYPE (arg1)),
5810 integer_zero_node)));
5817 /* If we are comparing an expression that just has comparisons
5818 of two integer values, arithmetic expressions of those comparisons,
5819 and constants, we can simplify it. There are only three cases
5820 to check: the two values can either be equal, the first can be
5821 greater, or the second can be greater. Fold the expression for
5822 those three values. Since each value must be 0 or 1, we have
5823 eight possibilities, each of which corresponds to the constant 0
5824 or 1 or one of the six possible comparisons.
5826 This handles common cases like (a > b) == 0 but also handles
5827 expressions like ((x > y) - (y > x)) > 0, which supposedly
5828 occur in macroized code. */
5830 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
5832 tree cval1 = 0, cval2 = 0;
5835 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
5836 /* Don't handle degenerate cases here; they should already
5837 have been handled anyway. */
5838 && cval1 != 0 && cval2 != 0
5839 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
5840 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
5841 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
5842 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
5843 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
5844 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
5845 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
5847 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
5848 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
5850 /* We can't just pass T to eval_subst in case cval1 or cval2
5851 was the same as ARG1. */
5854 = fold (build (code, type,
5855 eval_subst (arg0, cval1, maxval, cval2, minval),
5858 = fold (build (code, type,
5859 eval_subst (arg0, cval1, maxval, cval2, maxval),
5862 = fold (build (code, type,
5863 eval_subst (arg0, cval1, minval, cval2, maxval),
5866 /* All three of these results should be 0 or 1. Confirm they
5867 are. Then use those values to select the proper code
5870 if ((integer_zerop (high_result)
5871 || integer_onep (high_result))
5872 && (integer_zerop (equal_result)
5873 || integer_onep (equal_result))
5874 && (integer_zerop (low_result)
5875 || integer_onep (low_result)))
5877 /* Make a 3-bit mask with the high-order bit being the
5878 value for `>', the next for '=', and the low for '<'. */
5879 switch ((integer_onep (high_result) * 4)
5880 + (integer_onep (equal_result) * 2)
5881 + integer_onep (low_result))
5885 return omit_one_operand (type, integer_zero_node, arg0);
5906 return omit_one_operand (type, integer_one_node, arg0);
5909 t = build (code, type, cval1, cval2);
5911 return save_expr (t);
5918 /* If this is a comparison of a field, we may be able to simplify it. */
5919 if ((TREE_CODE (arg0) == COMPONENT_REF
5920 || TREE_CODE (arg0) == BIT_FIELD_REF)
5921 && (code == EQ_EXPR || code == NE_EXPR)
5922 /* Handle the constant case even without -O
5923 to make sure the warnings are given. */
5924 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
5926 t1 = optimize_bit_field_compare (code, type, arg0, arg1);
5930 /* If this is a comparison of complex values and either or both sides
5931 are a COMPLEX_EXPR or COMPLEX_CST, it is best to split up the
5932 comparisons and join them with a TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR.
5933 This may prevent needless evaluations. */
5934 if ((code == EQ_EXPR || code == NE_EXPR)
5935 && TREE_CODE (TREE_TYPE (arg0)) == COMPLEX_TYPE
5936 && (TREE_CODE (arg0) == COMPLEX_EXPR
5937 || TREE_CODE (arg1) == COMPLEX_EXPR
5938 || TREE_CODE (arg0) == COMPLEX_CST
5939 || TREE_CODE (arg1) == COMPLEX_CST))
5941 tree subtype = TREE_TYPE (TREE_TYPE (arg0));
5942 tree real0, imag0, real1, imag1;
5944 arg0 = save_expr (arg0);
5945 arg1 = save_expr (arg1);
5946 real0 = fold (build1 (REALPART_EXPR, subtype, arg0));
5947 imag0 = fold (build1 (IMAGPART_EXPR, subtype, arg0));
5948 real1 = fold (build1 (REALPART_EXPR, subtype, arg1));
5949 imag1 = fold (build1 (IMAGPART_EXPR, subtype, arg1));
5951 return fold (build ((code == EQ_EXPR ? TRUTH_ANDIF_EXPR
5954 fold (build (code, type, real0, real1)),
5955 fold (build (code, type, imag0, imag1))));
5958 /* From here on, the only cases we handle are when the result is
5959 known to be a constant.
5961 To compute GT, swap the arguments and do LT.
5962 To compute GE, do LT and invert the result.
5963 To compute LE, swap the arguments, do LT and invert the result.
5964 To compute NE, do EQ and invert the result.
5966 Therefore, the code below must handle only EQ and LT. */
5968 if (code == LE_EXPR || code == GT_EXPR)
5970 tem = arg0, arg0 = arg1, arg1 = tem;
5971 code = swap_tree_comparison (code);
5974 /* Note that it is safe to invert for real values here because we
5975 will check below in the one case that it matters. */
5978 if (code == NE_EXPR || code == GE_EXPR)
5981 code = invert_tree_comparison (code);
5984 /* Compute a result for LT or EQ if args permit;
5985 otherwise return T. */
5986 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
5988 if (code == EQ_EXPR)
5989 t1 = build_int_2 ((TREE_INT_CST_LOW (arg0)
5990 == TREE_INT_CST_LOW (arg1))
5991 && (TREE_INT_CST_HIGH (arg0)
5992 == TREE_INT_CST_HIGH (arg1)),
5995 t1 = build_int_2 ((TREE_UNSIGNED (TREE_TYPE (arg0))
5996 ? INT_CST_LT_UNSIGNED (arg0, arg1)
5997 : INT_CST_LT (arg0, arg1)),
6001 #if 0 /* This is no longer useful, but breaks some real code. */
6002 /* Assume a nonexplicit constant cannot equal an explicit one,
6003 since such code would be undefined anyway.
6004 Exception: on sysvr4, using #pragma weak,
6005 a label can come out as 0. */
6006 else if (TREE_CODE (arg1) == INTEGER_CST
6007 && !integer_zerop (arg1)
6008 && TREE_CONSTANT (arg0)
6009 && TREE_CODE (arg0) == ADDR_EXPR
6011 t1 = build_int_2 (0, 0);
6013 /* Two real constants can be compared explicitly. */
6014 else if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
6016 /* If either operand is a NaN, the result is false with two
6017 exceptions: First, an NE_EXPR is true on NaNs, but that case
6018 is already handled correctly since we will be inverting the
6019 result for NE_EXPR. Second, if we had inverted a LE_EXPR
6020 or a GE_EXPR into a LT_EXPR, we must return true so that it
6021 will be inverted into false. */
6023 if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
6024 || REAL_VALUE_ISNAN (TREE_REAL_CST (arg1)))
6025 t1 = build_int_2 (invert && code == LT_EXPR, 0);
6027 else if (code == EQ_EXPR)
6028 t1 = build_int_2 (REAL_VALUES_EQUAL (TREE_REAL_CST (arg0),
6029 TREE_REAL_CST (arg1)),
6032 t1 = build_int_2 (REAL_VALUES_LESS (TREE_REAL_CST (arg0),
6033 TREE_REAL_CST (arg1)),
6037 if (t1 == NULL_TREE)
6041 TREE_INT_CST_LOW (t1) ^= 1;
6043 TREE_TYPE (t1) = type;
6044 if (TREE_CODE (type) == BOOLEAN_TYPE)
6045 return truthvalue_conversion (t1);
6049 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
6050 so all simple results must be passed through pedantic_non_lvalue. */
6051 if (TREE_CODE (arg0) == INTEGER_CST)
6052 return pedantic_non_lvalue
6053 (TREE_OPERAND (t, (integer_zerop (arg0) ? 2 : 1)));
6054 else if (operand_equal_p (arg1, TREE_OPERAND (expr, 2), 0))
6055 return pedantic_omit_one_operand (type, arg1, arg0);
6057 /* If the second operand is zero, invert the comparison and swap
6058 the second and third operands. Likewise if the second operand
6059 is constant and the third is not or if the third operand is
6060 equivalent to the first operand of the comparison. */
6062 if (integer_zerop (arg1)
6063 || (TREE_CONSTANT (arg1) && ! TREE_CONSTANT (TREE_OPERAND (t, 2)))
6064 || (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
6065 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
6066 TREE_OPERAND (t, 2),
6067 TREE_OPERAND (arg0, 1))))
6069 /* See if this can be inverted. If it can't, possibly because
6070 it was a floating-point inequality comparison, don't do
6072 tem = invert_truthvalue (arg0);
6074 if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
6076 t = build (code, type, tem,
6077 TREE_OPERAND (t, 2), TREE_OPERAND (t, 1));
6079 /* arg1 should be the first argument of the new T. */
6080 arg1 = TREE_OPERAND (t, 1);
6085 /* If we have A op B ? A : C, we may be able to convert this to a
6086 simpler expression, depending on the operation and the values
6087 of B and C. IEEE floating point prevents this though,
6088 because A or B might be -0.0 or a NaN. */
6090 if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
6091 && (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
6092 || ! FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 0)))
6094 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
6095 arg1, TREE_OPERAND (arg0, 1)))
6097 tree arg2 = TREE_OPERAND (t, 2);
6098 enum tree_code comp_code = TREE_CODE (arg0);
6102 /* If we have A op 0 ? A : -A, this is A, -A, abs (A), or abs (-A),
6103 depending on the comparison operation. */
6104 if ((FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 1)))
6105 ? real_zerop (TREE_OPERAND (arg0, 1))
6106 : integer_zerop (TREE_OPERAND (arg0, 1)))
6107 && TREE_CODE (arg2) == NEGATE_EXPR
6108 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
6112 return pedantic_non_lvalue
6113 (fold (build1 (NEGATE_EXPR, type, arg1)));
6115 return pedantic_non_lvalue (convert (type, arg1));
6118 if (TREE_UNSIGNED (TREE_TYPE (arg1)))
6119 arg1 = convert (signed_type (TREE_TYPE (arg1)), arg1);
6120 return pedantic_non_lvalue
6121 (convert (type, fold (build1 (ABS_EXPR,
6122 TREE_TYPE (arg1), arg1))));
6125 if (TREE_UNSIGNED (TREE_TYPE (arg1)))
6126 arg1 = convert (signed_type (TREE_TYPE (arg1)), arg1);
6127 return pedantic_non_lvalue
6128 (fold (build1 (NEGATE_EXPR, type,
6130 fold (build1 (ABS_EXPR,
6137 /* If this is A != 0 ? A : 0, this is simply A. For ==, it is
6140 if (integer_zerop (TREE_OPERAND (arg0, 1)) && integer_zerop (arg2))
6142 if (comp_code == NE_EXPR)
6143 return pedantic_non_lvalue (convert (type, arg1));
6144 else if (comp_code == EQ_EXPR)
6145 return pedantic_non_lvalue (convert (type, integer_zero_node));
6148 /* If this is A op B ? A : B, this is either A, B, min (A, B),
6149 or max (A, B), depending on the operation. */
6151 if (operand_equal_for_comparison_p (TREE_OPERAND (arg0, 1),
6152 arg2, TREE_OPERAND (arg0, 0)))
6154 tree comp_op0 = TREE_OPERAND (arg0, 0);
6155 tree comp_op1 = TREE_OPERAND (arg0, 1);
6156 tree comp_type = TREE_TYPE (comp_op0);
6161 return pedantic_non_lvalue (convert (type, arg2));
6163 return pedantic_non_lvalue (convert (type, arg1));
6166 /* In C++ a ?: expression can be an lvalue, so put the
6167 operand which will be used if they are equal first
6168 so that we can convert this back to the
6169 corresponding COND_EXPR. */
6170 return pedantic_non_lvalue
6171 (convert (type, (fold (build (MIN_EXPR, comp_type,
6172 (comp_code == LE_EXPR
6173 ? comp_op0 : comp_op1),
6174 (comp_code == LE_EXPR
6175 ? comp_op1 : comp_op0))))));
6179 return pedantic_non_lvalue
6180 (convert (type, fold (build (MAX_EXPR, comp_type,
6181 (comp_code == GE_EXPR
6182 ? comp_op0 : comp_op1),
6183 (comp_code == GE_EXPR
6184 ? comp_op1 : comp_op0)))));
6191 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
6192 we might still be able to simplify this. For example,
6193 if C1 is one less or one more than C2, this might have started
6194 out as a MIN or MAX and been transformed by this function.
6195 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
6197 if (INTEGRAL_TYPE_P (type)
6198 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
6199 && TREE_CODE (arg2) == INTEGER_CST)
6203 /* We can replace A with C1 in this case. */
6204 arg1 = convert (type, TREE_OPERAND (arg0, 1));
6205 t = build (code, type, TREE_OPERAND (t, 0), arg1,
6206 TREE_OPERAND (t, 2));
6210 /* If C1 is C2 + 1, this is min(A, C2). */
6211 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type), 1)
6212 && operand_equal_p (TREE_OPERAND (arg0, 1),
6213 const_binop (PLUS_EXPR, arg2,
6214 integer_one_node, 0), 1))
6215 return pedantic_non_lvalue
6216 (fold (build (MIN_EXPR, type, arg1, arg2)));
6220 /* If C1 is C2 - 1, this is min(A, C2). */
6221 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type), 1)
6222 && operand_equal_p (TREE_OPERAND (arg0, 1),
6223 const_binop (MINUS_EXPR, arg2,
6224 integer_one_node, 0), 1))
6225 return pedantic_non_lvalue
6226 (fold (build (MIN_EXPR, type, arg1, arg2)));
6230 /* If C1 is C2 - 1, this is max(A, C2). */
6231 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type), 1)
6232 && operand_equal_p (TREE_OPERAND (arg0, 1),
6233 const_binop (MINUS_EXPR, arg2,
6234 integer_one_node, 0), 1))
6235 return pedantic_non_lvalue
6236 (fold (build (MAX_EXPR, type, arg1, arg2)));
6240 /* If C1 is C2 + 1, this is max(A, C2). */
6241 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type), 1)
6242 && operand_equal_p (TREE_OPERAND (arg0, 1),
6243 const_binop (PLUS_EXPR, arg2,
6244 integer_one_node, 0), 1))
6245 return pedantic_non_lvalue
6246 (fold (build (MAX_EXPR, type, arg1, arg2)));
6255 /* If the second operand is simpler than the third, swap them
6256 since that produces better jump optimization results. */
6257 if ((TREE_CONSTANT (arg1) || TREE_CODE_CLASS (TREE_CODE (arg1)) == 'd'
6258 || TREE_CODE (arg1) == SAVE_EXPR)
6259 && ! (TREE_CONSTANT (TREE_OPERAND (t, 2))
6260 || TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (t, 2))) == 'd'
6261 || TREE_CODE (TREE_OPERAND (t, 2)) == SAVE_EXPR))
6263 /* See if this can be inverted. If it can't, possibly because
6264 it was a floating-point inequality comparison, don't do
6266 tem = invert_truthvalue (arg0);
6268 if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
6270 t = build (code, type, tem,
6271 TREE_OPERAND (t, 2), TREE_OPERAND (t, 1));
6273 /* arg1 should be the first argument of the new T. */
6274 arg1 = TREE_OPERAND (t, 1);
6279 /* Convert A ? 1 : 0 to simply A. */
6280 if (integer_onep (TREE_OPERAND (t, 1))
6281 && integer_zerop (TREE_OPERAND (t, 2))
6282 /* If we try to convert TREE_OPERAND (t, 0) to our type, the
6283 call to fold will try to move the conversion inside
6284 a COND, which will recurse. In that case, the COND_EXPR
6285 is probably the best choice, so leave it alone. */
6286 && type == TREE_TYPE (arg0))
6287 return pedantic_non_lvalue (arg0);
6289 /* Look for expressions of the form A & 2 ? 2 : 0. The result of this
6290 operation is simply A & 2. */
6292 if (integer_zerop (TREE_OPERAND (t, 2))
6293 && TREE_CODE (arg0) == NE_EXPR
6294 && integer_zerop (TREE_OPERAND (arg0, 1))
6295 && integer_pow2p (arg1)
6296 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
6297 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
6299 return pedantic_non_lvalue (convert (type, TREE_OPERAND (arg0, 0)));
6304 /* When pedantic, a compound expression can be neither an lvalue
6305 nor an integer constant expression. */
6306 if (TREE_SIDE_EFFECTS (arg0) || pedantic)
6308 /* Don't let (0, 0) be null pointer constant. */
6309 if (integer_zerop (arg1))
6310 return build1 (NOP_EXPR, TREE_TYPE (arg1), arg1);
6315 return build_complex (type, arg0, arg1);
6319 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
6321 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
6322 return omit_one_operand (type, TREE_OPERAND (arg0, 0),
6323 TREE_OPERAND (arg0, 1));
6324 else if (TREE_CODE (arg0) == COMPLEX_CST)
6325 return TREE_REALPART (arg0);
6326 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
6327 return fold (build (TREE_CODE (arg0), type,
6328 fold (build1 (REALPART_EXPR, type,
6329 TREE_OPERAND (arg0, 0))),
6330 fold (build1 (REALPART_EXPR,
6331 type, TREE_OPERAND (arg0, 1)))));
6335 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
6336 return convert (type, integer_zero_node);
6337 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
6338 return omit_one_operand (type, TREE_OPERAND (arg0, 1),
6339 TREE_OPERAND (arg0, 0));
6340 else if (TREE_CODE (arg0) == COMPLEX_CST)
6341 return TREE_IMAGPART (arg0);
6342 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
6343 return fold (build (TREE_CODE (arg0), type,
6344 fold (build1 (IMAGPART_EXPR, type,
6345 TREE_OPERAND (arg0, 0))),
6346 fold (build1 (IMAGPART_EXPR, type,
6347 TREE_OPERAND (arg0, 1)))));
6350 /* Pull arithmetic ops out of the CLEANUP_POINT_EXPR where
6352 case CLEANUP_POINT_EXPR:
6353 if (! has_cleanups (arg0))
6354 return TREE_OPERAND (t, 0);
6357 enum tree_code code0 = TREE_CODE (arg0);
6358 int kind0 = TREE_CODE_CLASS (code0);
6359 tree arg00 = TREE_OPERAND (arg0, 0);
6362 if (kind0 == '1' || code0 == TRUTH_NOT_EXPR)
6363 return fold (build1 (code0, type,
6364 fold (build1 (CLEANUP_POINT_EXPR,
6365 TREE_TYPE (arg00), arg00))));
6367 if (kind0 == '<' || kind0 == '2'
6368 || code0 == TRUTH_ANDIF_EXPR || code0 == TRUTH_ORIF_EXPR
6369 || code0 == TRUTH_AND_EXPR || code0 == TRUTH_OR_EXPR
6370 || code0 == TRUTH_XOR_EXPR)
6372 arg01 = TREE_OPERAND (arg0, 1);
6374 if (TREE_CONSTANT (arg00)
6375 || ((code0 == TRUTH_ANDIF_EXPR || code0 == TRUTH_ORIF_EXPR)
6376 && ! has_cleanups (arg00)))
6377 return fold (build (code0, type, arg00,
6378 fold (build1 (CLEANUP_POINT_EXPR,
6379 TREE_TYPE (arg01), arg01))));
6381 if (TREE_CONSTANT (arg01))
6382 return fold (build (code0, type,
6383 fold (build1 (CLEANUP_POINT_EXPR,
6384 TREE_TYPE (arg00), arg00)),
6393 } /* switch (code) */
6396 /* Determine if first argument is a multiple of second argument.
6397 Return 0 if it is not, or is not easily determined to so be.
6399 An example of the sort of thing we care about (at this point --
6400 this routine could surely be made more general, and expanded
6401 to do what the *_DIV_EXPR's fold() cases do now) is discovering
6404 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
6410 when we know that the two `SAVE_EXPR (J * 8)' nodes are the
6411 same node (which means they will have the same value at run
6412 time, even though we don't know when they'll be assigned).
6414 This code also handles discovering that
6416 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
6422 (of course) so we don't have to worry about dealing with a
6425 Note that we _look_ inside a SAVE_EXPR only to determine
6426 how it was calculated; it is not safe for fold() to do much
6427 of anything else with the internals of a SAVE_EXPR, since
6428 fold() cannot know when it will be evaluated at run time.
6429 For example, the latter example above _cannot_ be implemented
6434 or any variant thereof, since the value of J at evaluation time
6435 of the original SAVE_EXPR is not necessarily the same at the time
6436 the new expression is evaluated. The only optimization of this
6437 sort that would be valid is changing
6439 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
6445 SAVE_EXPR (I) * SAVE_EXPR (J)
6447 (where the same SAVE_EXPR (J) is used in the original and the
6448 transformed version). */
6451 multiple_of_p (type, top, bottom)
6456 if (operand_equal_p (top, bottom, 0))
6459 if (TREE_CODE (type) != INTEGER_TYPE)
6462 switch (TREE_CODE (top))
6465 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
6466 || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
6470 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
6471 && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
6474 /* Punt if conversion from non-integral or wider integral type. */
6475 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
6476 || (TYPE_PRECISION (type)
6477 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
6481 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
6484 if ((TREE_CODE (bottom) != INTEGER_CST)
6485 || (tree_int_cst_sgn (top) < 0)
6486 || (tree_int_cst_sgn (bottom) < 0))
6488 return integer_zerop (const_binop (TRUNC_MOD_EXPR,