1 /* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987, 88, 92, 93, 94, 95, 1996 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 and size_binop.
32 fold takes a tree as argument and returns a simplified tree.
34 size_binop takes a tree code for an arithmetic operation
35 and two operands that are trees, and produces a tree for the
36 result, assuming the type comes from `sizetype'.
38 size_int takes an integer value, and creates a tree constant
39 with type from `sizetype'. */
47 /* Handle floating overflow for `const_binop'. */
48 static jmp_buf float_error;
50 static void encode PROTO((HOST_WIDE_INT *,
51 HOST_WIDE_INT, HOST_WIDE_INT));
52 static void decode PROTO((HOST_WIDE_INT *,
53 HOST_WIDE_INT *, HOST_WIDE_INT *));
54 int div_and_round_double PROTO((enum tree_code, int, HOST_WIDE_INT,
55 HOST_WIDE_INT, HOST_WIDE_INT,
56 HOST_WIDE_INT, HOST_WIDE_INT *,
57 HOST_WIDE_INT *, HOST_WIDE_INT *,
59 static int split_tree PROTO((tree, enum tree_code, tree *,
61 static tree const_binop PROTO((enum tree_code, tree, tree, int));
62 static tree fold_convert PROTO((tree, tree));
63 static enum tree_code invert_tree_comparison PROTO((enum tree_code));
64 static enum tree_code swap_tree_comparison PROTO((enum tree_code));
65 static int truth_value_p PROTO((enum tree_code));
66 static int operand_equal_for_comparison_p PROTO((tree, tree, tree));
67 static int twoval_comparison_p PROTO((tree, tree *, tree *, int *));
68 static tree eval_subst PROTO((tree, tree, tree, tree, tree));
69 static tree omit_one_operand PROTO((tree, tree, tree));
70 static tree pedantic_omit_one_operand PROTO((tree, tree, tree));
71 static tree distribute_bit_expr PROTO((enum tree_code, tree, tree, tree));
72 static tree make_bit_field_ref PROTO((tree, tree, int, int, int));
73 static tree optimize_bit_field_compare PROTO((enum tree_code, tree,
75 static tree decode_field_reference PROTO((tree, int *, int *,
76 enum machine_mode *, int *,
77 int *, tree *, tree *));
78 static int all_ones_mask_p PROTO((tree, int));
79 static int simple_operand_p PROTO((tree));
80 static tree range_binop PROTO((enum tree_code, tree, tree, int,
82 static tree make_range PROTO((tree, int *, tree *, tree *));
83 static tree build_range_check PROTO((tree, tree, int, tree, tree));
84 static int merge_ranges PROTO((int *, tree *, tree *, int, tree, tree,
86 static tree fold_range_test PROTO((tree));
87 static tree unextend PROTO((tree, int, int, tree));
88 static tree fold_truthop PROTO((enum tree_code, tree, tree, tree));
89 static tree strip_compound_expr PROTO((tree, tree));
95 /* Suppose A1 + B1 = SUM1, using 2's complement arithmetic ignoring overflow.
96 Suppose A, B and SUM have the same respective signs as A1, B1, and SUM1.
97 Then this yields nonzero if overflow occurred during the addition.
98 Overflow occurs if A and B have the same sign, but A and SUM differ in sign.
99 Use `^' to test whether signs differ, and `< 0' to isolate the sign. */
100 #define overflow_sum_sign(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
102 /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
103 We do that by representing the two-word integer in 4 words, with only
104 HOST_BITS_PER_WIDE_INT/2 bits stored in each word, as a positive number. */
107 ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT/2)) - 1))
108 #define HIGHPART(x) \
109 ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT/2)
110 #define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT/2)
112 /* Unpack a two-word integer into 4 words.
113 LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
114 WORDS points to the array of HOST_WIDE_INTs. */
117 encode (words, low, hi)
118 HOST_WIDE_INT *words;
119 HOST_WIDE_INT low, hi;
121 words[0] = LOWPART (low);
122 words[1] = HIGHPART (low);
123 words[2] = LOWPART (hi);
124 words[3] = HIGHPART (hi);
127 /* Pack an array of 4 words into a two-word integer.
128 WORDS points to the array of words.
129 The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces. */
132 decode (words, low, hi)
133 HOST_WIDE_INT *words;
134 HOST_WIDE_INT *low, *hi;
136 *low = words[0] | words[1] * BASE;
137 *hi = words[2] | words[3] * BASE;
140 /* Make the integer constant T valid for its type
141 by setting to 0 or 1 all the bits in the constant
142 that don't belong in the type.
143 Yield 1 if a signed overflow occurs, 0 otherwise.
144 If OVERFLOW is nonzero, a signed overflow has already occurred
145 in calculating T, so propagate it.
147 Make the real constant T valid for its type by calling CHECK_FLOAT_VALUE,
151 force_fit_type (t, overflow)
155 HOST_WIDE_INT low, high;
158 if (TREE_CODE (t) == REAL_CST)
160 #ifdef CHECK_FLOAT_VALUE
161 CHECK_FLOAT_VALUE (TYPE_MODE (TREE_TYPE (t)), TREE_REAL_CST (t),
167 else if (TREE_CODE (t) != INTEGER_CST)
170 low = TREE_INT_CST_LOW (t);
171 high = TREE_INT_CST_HIGH (t);
173 if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
176 prec = TYPE_PRECISION (TREE_TYPE (t));
178 /* First clear all bits that are beyond the type's precision. */
180 if (prec == 2 * HOST_BITS_PER_WIDE_INT)
182 else if (prec > HOST_BITS_PER_WIDE_INT)
184 TREE_INT_CST_HIGH (t)
185 &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
189 TREE_INT_CST_HIGH (t) = 0;
190 if (prec < HOST_BITS_PER_WIDE_INT)
191 TREE_INT_CST_LOW (t) &= ~((HOST_WIDE_INT) (-1) << prec);
194 /* Unsigned types do not suffer sign extension or overflow. */
195 if (TREE_UNSIGNED (TREE_TYPE (t)))
198 /* If the value's sign bit is set, extend the sign. */
199 if (prec != 2 * HOST_BITS_PER_WIDE_INT
200 && (prec > HOST_BITS_PER_WIDE_INT
201 ? (TREE_INT_CST_HIGH (t)
202 & ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)))
203 : TREE_INT_CST_LOW (t) & ((HOST_WIDE_INT) 1 << (prec - 1))))
205 /* Value is negative:
206 set to 1 all the bits that are outside this type's precision. */
207 if (prec > HOST_BITS_PER_WIDE_INT)
209 TREE_INT_CST_HIGH (t)
210 |= ((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
214 TREE_INT_CST_HIGH (t) = -1;
215 if (prec < HOST_BITS_PER_WIDE_INT)
216 TREE_INT_CST_LOW (t) |= ((HOST_WIDE_INT) (-1) << prec);
220 /* Yield nonzero if signed overflow occurred. */
222 ((overflow | (low ^ TREE_INT_CST_LOW (t)) | (high ^ TREE_INT_CST_HIGH (t)))
226 /* Add two doubleword integers with doubleword result.
227 Each argument is given as two `HOST_WIDE_INT' pieces.
228 One argument is L1 and H1; the other, L2 and H2.
229 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
232 add_double (l1, h1, l2, h2, lv, hv)
233 HOST_WIDE_INT l1, h1, l2, h2;
234 HOST_WIDE_INT *lv, *hv;
239 h = h1 + h2 + ((unsigned HOST_WIDE_INT) l < l1);
243 return overflow_sum_sign (h1, h2, h);
246 /* Negate a doubleword integer with doubleword result.
247 Return nonzero if the operation overflows, assuming it's signed.
248 The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
249 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
252 neg_double (l1, h1, lv, hv)
253 HOST_WIDE_INT l1, h1;
254 HOST_WIDE_INT *lv, *hv;
260 return (*hv & h1) < 0;
270 /* Multiply two doubleword integers with doubleword result.
271 Return nonzero if the operation overflows, assuming it's signed.
272 Each argument is given as two `HOST_WIDE_INT' pieces.
273 One argument is L1 and H1; the other, L2 and H2.
274 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
277 mul_double (l1, h1, l2, h2, lv, hv)
278 HOST_WIDE_INT l1, h1, l2, h2;
279 HOST_WIDE_INT *lv, *hv;
281 HOST_WIDE_INT arg1[4];
282 HOST_WIDE_INT arg2[4];
283 HOST_WIDE_INT prod[4 * 2];
284 register unsigned HOST_WIDE_INT carry;
285 register int i, j, k;
286 HOST_WIDE_INT toplow, tophigh, neglow, neghigh;
288 encode (arg1, l1, h1);
289 encode (arg2, l2, h2);
291 bzero ((char *) prod, sizeof prod);
293 for (i = 0; i < 4; i++)
296 for (j = 0; j < 4; j++)
299 /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000. */
300 carry += arg1[i] * arg2[j];
301 /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF. */
303 prod[k] = LOWPART (carry);
304 carry = HIGHPART (carry);
309 decode (prod, lv, hv); /* This ignores prod[4] through prod[4*2-1] */
311 /* Check for overflow by calculating the top half of the answer in full;
312 it should agree with the low half's sign bit. */
313 decode (prod+4, &toplow, &tophigh);
316 neg_double (l2, h2, &neglow, &neghigh);
317 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
321 neg_double (l1, h1, &neglow, &neghigh);
322 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
324 return (*hv < 0 ? ~(toplow & tophigh) : toplow | tophigh) != 0;
327 /* Shift the doubleword integer in L1, H1 left by COUNT places
328 keeping only PREC bits of result.
329 Shift right if COUNT is negative.
330 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
331 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
334 lshift_double (l1, h1, count, prec, lv, hv, arith)
335 HOST_WIDE_INT l1, h1, count;
337 HOST_WIDE_INT *lv, *hv;
342 rshift_double (l1, h1, - count, prec, lv, hv, arith);
346 #ifdef SHIFT_COUNT_TRUNCATED
347 if (SHIFT_COUNT_TRUNCATED)
351 if (count >= HOST_BITS_PER_WIDE_INT)
353 *hv = (unsigned HOST_WIDE_INT) l1 << count - HOST_BITS_PER_WIDE_INT;
358 *hv = (((unsigned HOST_WIDE_INT) h1 << count)
359 | ((unsigned HOST_WIDE_INT) l1 >> HOST_BITS_PER_WIDE_INT - count - 1 >> 1));
360 *lv = (unsigned HOST_WIDE_INT) l1 << count;
364 /* Shift the doubleword integer in L1, H1 right by COUNT places
365 keeping only PREC bits of result. COUNT must be positive.
366 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
367 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
370 rshift_double (l1, h1, count, prec, lv, hv, arith)
371 HOST_WIDE_INT l1, h1, count;
373 HOST_WIDE_INT *lv, *hv;
376 unsigned HOST_WIDE_INT signmask;
378 ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
381 #ifdef SHIFT_COUNT_TRUNCATED
382 if (SHIFT_COUNT_TRUNCATED)
386 if (count >= HOST_BITS_PER_WIDE_INT)
389 *lv = ((signmask << 2 * HOST_BITS_PER_WIDE_INT - count - 1 << 1)
390 | ((unsigned HOST_WIDE_INT) h1 >> count - HOST_BITS_PER_WIDE_INT));
394 *lv = (((unsigned HOST_WIDE_INT) l1 >> count)
395 | ((unsigned HOST_WIDE_INT) h1 << HOST_BITS_PER_WIDE_INT - count - 1 << 1));
396 *hv = ((signmask << HOST_BITS_PER_WIDE_INT - count)
397 | ((unsigned HOST_WIDE_INT) h1 >> count));
401 /* Rotate the doubleword integer in L1, H1 left by COUNT places
402 keeping only PREC bits of result.
403 Rotate right if COUNT is negative.
404 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
407 lrotate_double (l1, h1, count, prec, lv, hv)
408 HOST_WIDE_INT l1, h1, count;
410 HOST_WIDE_INT *lv, *hv;
412 HOST_WIDE_INT s1l, s1h, s2l, s2h;
418 lshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
419 rshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
424 /* Rotate the doubleword integer in L1, H1 left by COUNT places
425 keeping only PREC bits of result. COUNT must be positive.
426 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
429 rrotate_double (l1, h1, count, prec, lv, hv)
430 HOST_WIDE_INT l1, h1, count;
432 HOST_WIDE_INT *lv, *hv;
434 HOST_WIDE_INT s1l, s1h, s2l, s2h;
440 rshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
441 lshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
446 /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
447 for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
448 CODE is a tree code for a kind of division, one of
449 TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
451 It controls how the quotient is rounded to a integer.
452 Return nonzero if the operation overflows.
453 UNS nonzero says do unsigned division. */
456 div_and_round_double (code, uns,
457 lnum_orig, hnum_orig, lden_orig, hden_orig,
458 lquo, hquo, lrem, hrem)
461 HOST_WIDE_INT lnum_orig, hnum_orig; /* num == numerator == dividend */
462 HOST_WIDE_INT lden_orig, hden_orig; /* den == denominator == divisor */
463 HOST_WIDE_INT *lquo, *hquo, *lrem, *hrem;
466 HOST_WIDE_INT num[4 + 1]; /* extra element for scaling. */
467 HOST_WIDE_INT den[4], quo[4];
469 unsigned HOST_WIDE_INT work;
470 register unsigned HOST_WIDE_INT carry = 0;
471 HOST_WIDE_INT lnum = lnum_orig;
472 HOST_WIDE_INT hnum = hnum_orig;
473 HOST_WIDE_INT lden = lden_orig;
474 HOST_WIDE_INT hden = hden_orig;
477 if ((hden == 0) && (lden == 0))
480 /* calculate quotient sign and convert operands to unsigned. */
486 /* (minimum integer) / (-1) is the only overflow case. */
487 if (neg_double (lnum, hnum, &lnum, &hnum) && (lden & hden) == -1)
493 neg_double (lden, hden, &lden, &hden);
497 if (hnum == 0 && hden == 0)
498 { /* single precision */
500 /* This unsigned division rounds toward zero. */
501 *lquo = lnum / (unsigned HOST_WIDE_INT) lden;
506 { /* trivial case: dividend < divisor */
507 /* hden != 0 already checked. */
514 bzero ((char *) quo, sizeof quo);
516 bzero ((char *) num, sizeof num); /* to zero 9th element */
517 bzero ((char *) den, sizeof den);
519 encode (num, lnum, hnum);
520 encode (den, lden, hden);
522 /* Special code for when the divisor < BASE. */
523 if (hden == 0 && lden < BASE)
525 /* hnum != 0 already checked. */
526 for (i = 4 - 1; i >= 0; i--)
528 work = num[i] + carry * BASE;
529 quo[i] = work / (unsigned HOST_WIDE_INT) lden;
530 carry = work % (unsigned HOST_WIDE_INT) lden;
535 /* Full double precision division,
536 with thanks to Don Knuth's "Seminumerical Algorithms". */
537 int num_hi_sig, den_hi_sig;
538 unsigned HOST_WIDE_INT quo_est, scale;
540 /* Find the highest non-zero divisor digit. */
541 for (i = 4 - 1; ; i--)
547 /* Insure that the first digit of the divisor is at least BASE/2.
548 This is required by the quotient digit estimation algorithm. */
550 scale = BASE / (den[den_hi_sig] + 1);
551 if (scale > 1) { /* scale divisor and dividend */
553 for (i = 0; i <= 4 - 1; i++) {
554 work = (num[i] * scale) + carry;
555 num[i] = LOWPART (work);
556 carry = HIGHPART (work);
559 for (i = 0; i <= 4 - 1; i++) {
560 work = (den[i] * scale) + carry;
561 den[i] = LOWPART (work);
562 carry = HIGHPART (work);
563 if (den[i] != 0) den_hi_sig = i;
570 for (i = num_hi_sig - den_hi_sig - 1; i >= 0; i--) {
571 /* guess the next quotient digit, quo_est, by dividing the first
572 two remaining dividend digits by the high order quotient digit.
573 quo_est is never low and is at most 2 high. */
574 unsigned HOST_WIDE_INT tmp;
576 num_hi_sig = i + den_hi_sig + 1;
577 work = num[num_hi_sig] * BASE + num[num_hi_sig - 1];
578 if (num[num_hi_sig] != den[den_hi_sig])
579 quo_est = work / den[den_hi_sig];
583 /* refine quo_est so it's usually correct, and at most one high. */
584 tmp = work - quo_est * den[den_hi_sig];
586 && den[den_hi_sig - 1] * quo_est > (tmp * BASE + num[num_hi_sig - 2]))
589 /* Try QUO_EST as the quotient digit, by multiplying the
590 divisor by QUO_EST and subtracting from the remaining dividend.
591 Keep in mind that QUO_EST is the I - 1st digit. */
594 for (j = 0; j <= den_hi_sig; j++)
596 work = quo_est * den[j] + carry;
597 carry = HIGHPART (work);
598 work = num[i + j] - LOWPART (work);
599 num[i + j] = LOWPART (work);
600 carry += HIGHPART (work) != 0;
603 /* if quo_est was high by one, then num[i] went negative and
604 we need to correct things. */
606 if (num[num_hi_sig] < carry)
609 carry = 0; /* add divisor back in */
610 for (j = 0; j <= den_hi_sig; j++)
612 work = num[i + j] + den[j] + carry;
613 carry = HIGHPART (work);
614 num[i + j] = LOWPART (work);
616 num [num_hi_sig] += carry;
619 /* store the quotient digit. */
624 decode (quo, lquo, hquo);
627 /* if result is negative, make it so. */
629 neg_double (*lquo, *hquo, lquo, hquo);
631 /* compute trial remainder: rem = num - (quo * den) */
632 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
633 neg_double (*lrem, *hrem, lrem, hrem);
634 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
639 case TRUNC_MOD_EXPR: /* round toward zero */
640 case EXACT_DIV_EXPR: /* for this one, it shouldn't matter */
644 case FLOOR_MOD_EXPR: /* round toward negative infinity */
645 if (quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio < 0 && rem != 0 */
648 add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1,
651 else return overflow;
655 case CEIL_MOD_EXPR: /* round toward positive infinity */
656 if (!quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio > 0 && rem != 0 */
658 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
661 else return overflow;
665 case ROUND_MOD_EXPR: /* round to closest integer */
667 HOST_WIDE_INT labs_rem = *lrem, habs_rem = *hrem;
668 HOST_WIDE_INT labs_den = lden, habs_den = hden, ltwice, htwice;
670 /* get absolute values */
671 if (*hrem < 0) neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
672 if (hden < 0) neg_double (lden, hden, &labs_den, &habs_den);
674 /* if (2 * abs (lrem) >= abs (lden)) */
675 mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0,
676 labs_rem, habs_rem, <wice, &htwice);
677 if (((unsigned HOST_WIDE_INT) habs_den
678 < (unsigned HOST_WIDE_INT) htwice)
679 || (((unsigned HOST_WIDE_INT) habs_den
680 == (unsigned HOST_WIDE_INT) htwice)
681 && ((HOST_WIDE_INT unsigned) labs_den
682 < (unsigned HOST_WIDE_INT) ltwice)))
686 add_double (*lquo, *hquo,
687 (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
690 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
693 else return overflow;
701 /* compute true remainder: rem = num - (quo * den) */
702 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
703 neg_double (*lrem, *hrem, lrem, hrem);
704 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
708 #ifndef REAL_ARITHMETIC
709 /* Effectively truncate a real value to represent the nearest possible value
710 in a narrower mode. The result is actually represented in the same data
711 type as the argument, but its value is usually different.
713 A trap may occur during the FP operations and it is the responsibility
714 of the calling function to have a handler established. */
717 real_value_truncate (mode, arg)
718 enum machine_mode mode;
721 return REAL_VALUE_TRUNCATE (mode, arg);
724 #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
726 /* Check for infinity in an IEEE double precision number. */
732 /* The IEEE 64-bit double format. */
737 unsigned exponent : 11;
738 unsigned mantissa1 : 20;
743 unsigned mantissa1 : 20;
744 unsigned exponent : 11;
750 if (u.big_endian.sign == 1)
753 return (u.big_endian.exponent == 2047
754 && u.big_endian.mantissa1 == 0
755 && u.big_endian.mantissa2 == 0);
760 return (u.little_endian.exponent == 2047
761 && u.little_endian.mantissa1 == 0
762 && u.little_endian.mantissa2 == 0);
766 /* Check whether an IEEE double precision number is a NaN. */
772 /* The IEEE 64-bit double format. */
777 unsigned exponent : 11;
778 unsigned mantissa1 : 20;
783 unsigned mantissa1 : 20;
784 unsigned exponent : 11;
790 if (u.big_endian.sign == 1)
793 return (u.big_endian.exponent == 2047
794 && (u.big_endian.mantissa1 != 0
795 || u.big_endian.mantissa2 != 0));
800 return (u.little_endian.exponent == 2047
801 && (u.little_endian.mantissa1 != 0
802 || u.little_endian.mantissa2 != 0));
806 /* Check for a negative IEEE double precision number. */
812 /* The IEEE 64-bit double format. */
817 unsigned exponent : 11;
818 unsigned mantissa1 : 20;
823 unsigned mantissa1 : 20;
824 unsigned exponent : 11;
830 if (u.big_endian.sign == 1)
833 return u.big_endian.sign;
838 return u.little_endian.sign;
841 #else /* Target not IEEE */
843 /* Let's assume other float formats don't have infinity.
844 (This can be overridden by redefining REAL_VALUE_ISINF.) */
852 /* Let's assume other float formats don't have NaNs.
853 (This can be overridden by redefining REAL_VALUE_ISNAN.) */
861 /* Let's assume other float formats don't have minus zero.
862 (This can be overridden by redefining REAL_VALUE_NEGATIVE.) */
869 #endif /* Target not IEEE */
871 /* Try to change R into its exact multiplicative inverse in machine mode
872 MODE. Return nonzero function value if successful. */
875 exact_real_inverse (mode, r)
876 enum machine_mode mode;
886 /* Usually disable if bounds checks are not reliable. */
887 if ((HOST_FLOAT_FORMAT != TARGET_FLOAT_FORMAT) && !flag_pretend_float)
890 /* Set array index to the less significant bits in the unions, depending
891 on the endian-ness of the host doubles.
892 Disable if insufficient information on the data structure. */
893 #if HOST_FLOAT_FORMAT == UNKNOWN_FLOAT_FORMAT
896 #if HOST_FLOAT_FORMAT == VAX_FLOAT_FORMAT
899 #if HOST_FLOAT_FORMAT == IBM_FLOAT_FORMAT
902 #define K (2 * HOST_FLOAT_WORDS_BIG_ENDIAN)
907 if (setjmp (float_error))
909 /* Don't do the optimization if there was an arithmetic error. */
911 set_float_handler (NULL_PTR);
914 set_float_handler (float_error);
916 /* Domain check the argument. */
922 if (REAL_VALUE_ISINF (x.d) || REAL_VALUE_ISNAN (x.d))
926 /* Compute the reciprocal and check for numerical exactness.
927 It is unnecessary to check all the significand bits to determine
928 whether X is a power of 2. If X is not, then it is impossible for
929 the bottom half significand of both X and 1/X to be all zero bits.
930 Hence we ignore the data structure of the top half and examine only
931 the low order bits of the two significands. */
933 if (x.i[K] != 0 || x.i[K + 1] != 0 || t.i[K] != 0 || t.i[K + 1] != 0)
936 /* Truncate to the required mode and range-check the result. */
937 y.d = REAL_VALUE_TRUNCATE (mode, t.d);
938 #ifdef CHECK_FLOAT_VALUE
940 if (CHECK_FLOAT_VALUE (mode, y.d, i))
944 /* Fail if truncation changed the value. */
945 if (y.d != t.d || y.d == 0.0)
949 if (REAL_VALUE_ISINF (y.d) || REAL_VALUE_ISNAN (y.d))
953 /* Output the reciprocal and return success flag. */
954 set_float_handler (NULL_PTR);
958 #endif /* no REAL_ARITHMETIC */
960 /* Split a tree IN into a constant and a variable part
961 that could be combined with CODE to make IN.
962 CODE must be a commutative arithmetic operation.
963 Store the constant part into *CONP and the variable in &VARP.
964 Return 1 if this was done; zero means the tree IN did not decompose
967 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR.
968 Therefore, we must tell the caller whether the variable part
969 was subtracted. We do this by storing 1 or -1 into *VARSIGNP.
970 The value stored is the coefficient for the variable term.
971 The constant term we return should always be added;
972 we negate it if necessary. */
975 split_tree (in, code, varp, conp, varsignp)
981 register tree outtype = TREE_TYPE (in);
985 /* Strip any conversions that don't change the machine mode. */
986 while ((TREE_CODE (in) == NOP_EXPR
987 || TREE_CODE (in) == CONVERT_EXPR)
988 && (TYPE_MODE (TREE_TYPE (in))
989 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (in, 0)))))
990 in = TREE_OPERAND (in, 0);
992 if (TREE_CODE (in) == code
993 || (! FLOAT_TYPE_P (TREE_TYPE (in))
994 /* We can associate addition and subtraction together
995 (even though the C standard doesn't say so)
996 for integers because the value is not affected.
997 For reals, the value might be affected, so we can't. */
998 && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
999 || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
1001 enum tree_code code = TREE_CODE (TREE_OPERAND (in, 0));
1002 if (code == INTEGER_CST)
1004 *conp = TREE_OPERAND (in, 0);
1005 *varp = TREE_OPERAND (in, 1);
1006 if (TYPE_MODE (TREE_TYPE (*varp)) != TYPE_MODE (outtype)
1007 && TREE_TYPE (*varp) != outtype)
1008 *varp = convert (outtype, *varp);
1009 *varsignp = (TREE_CODE (in) == MINUS_EXPR) ? -1 : 1;
1012 if (TREE_CONSTANT (TREE_OPERAND (in, 1)))
1014 *conp = TREE_OPERAND (in, 1);
1015 *varp = TREE_OPERAND (in, 0);
1017 if (TYPE_MODE (TREE_TYPE (*varp)) != TYPE_MODE (outtype)
1018 && TREE_TYPE (*varp) != outtype)
1019 *varp = convert (outtype, *varp);
1020 if (TREE_CODE (in) == MINUS_EXPR)
1022 /* If operation is subtraction and constant is second,
1023 must negate it to get an additive constant.
1024 And this cannot be done unless it is a manifest constant.
1025 It could also be the address of a static variable.
1026 We cannot negate that, so give up. */
1027 if (TREE_CODE (*conp) == INTEGER_CST)
1028 /* Subtracting from integer_zero_node loses for long long. */
1029 *conp = fold (build1 (NEGATE_EXPR, TREE_TYPE (*conp), *conp));
1035 if (TREE_CONSTANT (TREE_OPERAND (in, 0)))
1037 *conp = TREE_OPERAND (in, 0);
1038 *varp = TREE_OPERAND (in, 1);
1039 if (TYPE_MODE (TREE_TYPE (*varp)) != TYPE_MODE (outtype)
1040 && TREE_TYPE (*varp) != outtype)
1041 *varp = convert (outtype, *varp);
1042 *varsignp = (TREE_CODE (in) == MINUS_EXPR) ? -1 : 1;
1049 /* Combine two constants ARG1 and ARG2 under operation CODE
1050 to produce a new constant.
1051 We assume ARG1 and ARG2 have the same data type,
1052 or at least are the same kind of constant and the same machine mode.
1054 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
1057 const_binop (code, arg1, arg2, notrunc)
1058 enum tree_code code;
1059 register tree arg1, arg2;
1062 STRIP_NOPS (arg1); STRIP_NOPS (arg2);
1064 if (TREE_CODE (arg1) == INTEGER_CST)
1066 register HOST_WIDE_INT int1l = TREE_INT_CST_LOW (arg1);
1067 register HOST_WIDE_INT int1h = TREE_INT_CST_HIGH (arg1);
1068 HOST_WIDE_INT int2l = TREE_INT_CST_LOW (arg2);
1069 HOST_WIDE_INT int2h = TREE_INT_CST_HIGH (arg2);
1070 HOST_WIDE_INT low, hi;
1071 HOST_WIDE_INT garbagel, garbageh;
1073 int uns = TREE_UNSIGNED (TREE_TYPE (arg1));
1079 t = build_int_2 (int1l | int2l, int1h | int2h);
1083 t = build_int_2 (int1l ^ int2l, int1h ^ int2h);
1087 t = build_int_2 (int1l & int2l, int1h & int2h);
1090 case BIT_ANDTC_EXPR:
1091 t = build_int_2 (int1l & ~int2l, int1h & ~int2h);
1097 /* It's unclear from the C standard whether shifts can overflow.
1098 The following code ignores overflow; perhaps a C standard
1099 interpretation ruling is needed. */
1100 lshift_double (int1l, int1h, int2l,
1101 TYPE_PRECISION (TREE_TYPE (arg1)),
1104 t = build_int_2 (low, hi);
1105 TREE_TYPE (t) = TREE_TYPE (arg1);
1107 force_fit_type (t, 0);
1108 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1109 TREE_CONSTANT_OVERFLOW (t)
1110 = TREE_CONSTANT_OVERFLOW (arg1) | TREE_CONSTANT_OVERFLOW (arg2);
1116 lrotate_double (int1l, int1h, int2l,
1117 TYPE_PRECISION (TREE_TYPE (arg1)),
1119 t = build_int_2 (low, hi);
1126 if ((unsigned HOST_WIDE_INT) int2l < int1l)
1129 overflow = int2h < hi;
1131 t = build_int_2 (int2l, int2h);
1137 if ((unsigned HOST_WIDE_INT) int1l < int2l)
1140 overflow = int1h < hi;
1142 t = build_int_2 (int1l, int1h);
1145 overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
1146 t = build_int_2 (low, hi);
1150 if (int2h == 0 && int2l == 0)
1152 t = build_int_2 (int1l, int1h);
1155 neg_double (int2l, int2h, &low, &hi);
1156 add_double (int1l, int1h, low, hi, &low, &hi);
1157 overflow = overflow_sum_sign (hi, int2h, int1h);
1158 t = build_int_2 (low, hi);
1162 overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
1163 t = build_int_2 (low, hi);
1166 case TRUNC_DIV_EXPR:
1167 case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1168 case EXACT_DIV_EXPR:
1169 /* This is a shortcut for a common special case.
1170 It reduces the number of tree nodes generated
1172 if (int2h == 0 && int2l > 0
1173 && TREE_TYPE (arg1) == sizetype
1174 && ! TREE_CONSTANT_OVERFLOW (arg1)
1175 && ! TREE_CONSTANT_OVERFLOW (arg2)
1176 && int1h == 0 && int1l >= 0)
1178 if (code == CEIL_DIV_EXPR)
1180 return size_int (int1l / int2l);
1182 case ROUND_DIV_EXPR:
1183 if (int2h == 0 && int2l == 1)
1185 t = build_int_2 (int1l, int1h);
1188 if (int1l == int2l && int1h == int2h)
1190 if ((int1l | int1h) == 0)
1192 t = build_int_2 (1, 0);
1195 overflow = div_and_round_double (code, uns,
1196 int1l, int1h, int2l, int2h,
1197 &low, &hi, &garbagel, &garbageh);
1198 t = build_int_2 (low, hi);
1201 case TRUNC_MOD_EXPR: case ROUND_MOD_EXPR:
1202 case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1203 overflow = div_and_round_double (code, uns,
1204 int1l, int1h, int2l, int2h,
1205 &garbagel, &garbageh, &low, &hi);
1206 t = build_int_2 (low, hi);
1213 low = (((unsigned HOST_WIDE_INT) int1h
1214 < (unsigned HOST_WIDE_INT) int2h)
1215 || (((unsigned HOST_WIDE_INT) int1h
1216 == (unsigned HOST_WIDE_INT) int2h)
1217 && ((unsigned HOST_WIDE_INT) int1l
1218 < (unsigned HOST_WIDE_INT) int2l)));
1222 low = ((int1h < int2h)
1223 || ((int1h == int2h)
1224 && ((unsigned HOST_WIDE_INT) int1l
1225 < (unsigned HOST_WIDE_INT) int2l)));
1227 if (low == (code == MIN_EXPR))
1228 t = build_int_2 (int1l, int1h);
1230 t = build_int_2 (int2l, int2h);
1237 TREE_TYPE (t) = TREE_TYPE (arg1);
1239 = ((notrunc ? !uns && overflow : force_fit_type (t, overflow && !uns))
1240 | TREE_OVERFLOW (arg1)
1241 | TREE_OVERFLOW (arg2));
1242 TREE_CONSTANT_OVERFLOW (t) = (TREE_OVERFLOW (t)
1243 | TREE_CONSTANT_OVERFLOW (arg1)
1244 | TREE_CONSTANT_OVERFLOW (arg2));
1247 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1248 if (TREE_CODE (arg1) == REAL_CST)
1253 REAL_VALUE_TYPE value;
1256 d1 = TREE_REAL_CST (arg1);
1257 d2 = TREE_REAL_CST (arg2);
1259 /* If either operand is a NaN, just return it. Otherwise, set up
1260 for floating-point trap; we return an overflow. */
1261 if (REAL_VALUE_ISNAN (d1))
1263 else if (REAL_VALUE_ISNAN (d2))
1265 else if (setjmp (float_error))
1267 t = copy_node (arg1);
1272 set_float_handler (float_error);
1274 #ifdef REAL_ARITHMETIC
1275 REAL_ARITHMETIC (value, code, d1, d2);
1292 #ifndef REAL_INFINITY
1301 value = MIN (d1, d2);
1305 value = MAX (d1, d2);
1311 #endif /* no REAL_ARITHMETIC */
1312 t = build_real (TREE_TYPE (arg1),
1313 real_value_truncate (TYPE_MODE (TREE_TYPE (arg1)), value));
1315 set_float_handler (NULL_PTR);
1318 = (force_fit_type (t, overflow)
1319 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1320 TREE_CONSTANT_OVERFLOW (t)
1322 | TREE_CONSTANT_OVERFLOW (arg1)
1323 | TREE_CONSTANT_OVERFLOW (arg2);
1326 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
1327 if (TREE_CODE (arg1) == COMPLEX_CST)
1329 register tree type = TREE_TYPE (arg1);
1330 register tree r1 = TREE_REALPART (arg1);
1331 register tree i1 = TREE_IMAGPART (arg1);
1332 register tree r2 = TREE_REALPART (arg2);
1333 register tree i2 = TREE_IMAGPART (arg2);
1339 t = build_complex (type,
1340 const_binop (PLUS_EXPR, r1, r2, notrunc),
1341 const_binop (PLUS_EXPR, i1, i2, notrunc));
1345 t = build_complex (type,
1346 const_binop (MINUS_EXPR, r1, r2, notrunc),
1347 const_binop (MINUS_EXPR, i1, i2, notrunc));
1351 t = build_complex (type,
1352 const_binop (MINUS_EXPR,
1353 const_binop (MULT_EXPR,
1355 const_binop (MULT_EXPR,
1358 const_binop (PLUS_EXPR,
1359 const_binop (MULT_EXPR,
1361 const_binop (MULT_EXPR,
1368 register tree magsquared
1369 = const_binop (PLUS_EXPR,
1370 const_binop (MULT_EXPR, r2, r2, notrunc),
1371 const_binop (MULT_EXPR, i2, i2, notrunc),
1374 t = build_complex (type,
1376 (INTEGRAL_TYPE_P (TREE_TYPE (r1))
1377 ? TRUNC_DIV_EXPR : RDIV_EXPR,
1378 const_binop (PLUS_EXPR,
1379 const_binop (MULT_EXPR, r1, r2,
1381 const_binop (MULT_EXPR, i1, i2,
1384 magsquared, notrunc),
1386 (INTEGRAL_TYPE_P (TREE_TYPE (r1))
1387 ? TRUNC_DIV_EXPR : RDIV_EXPR,
1388 const_binop (MINUS_EXPR,
1389 const_binop (MULT_EXPR, i1, r2,
1391 const_binop (MULT_EXPR, r1, i2,
1394 magsquared, notrunc));
1406 /* Return an INTEGER_CST with value V and type from `sizetype'. */
1410 unsigned HOST_WIDE_INT number;
1413 /* Type-size nodes already made for small sizes. */
1414 static tree size_table[2*HOST_BITS_PER_WIDE_INT + 1];
1416 if (number < 2*HOST_BITS_PER_WIDE_INT + 1
1417 && size_table[number] != 0)
1418 return size_table[number];
1419 if (number < 2*HOST_BITS_PER_WIDE_INT + 1)
1421 push_obstacks_nochange ();
1422 /* Make this a permanent node. */
1423 end_temporary_allocation ();
1424 t = build_int_2 (number, 0);
1425 TREE_TYPE (t) = sizetype;
1426 size_table[number] = t;
1431 t = build_int_2 (number, 0);
1432 TREE_TYPE (t) = sizetype;
1437 /* Combine operands OP1 and OP2 with arithmetic operation CODE.
1438 CODE is a tree code. Data type is taken from `sizetype',
1439 If the operands are constant, so is the result. */
1442 size_binop (code, arg0, arg1)
1443 enum tree_code code;
1446 /* Handle the special case of two integer constants faster. */
1447 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1449 /* And some specific cases even faster than that. */
1450 if (code == PLUS_EXPR && integer_zerop (arg0))
1452 else if ((code == MINUS_EXPR || code == PLUS_EXPR)
1453 && integer_zerop (arg1))
1455 else if (code == MULT_EXPR && integer_onep (arg0))
1458 /* Handle general case of two integer constants. */
1459 return const_binop (code, arg0, arg1, 0);
1462 if (arg0 == error_mark_node || arg1 == error_mark_node)
1463 return error_mark_node;
1465 return fold (build (code, sizetype, arg0, arg1));
1468 /* Given T, a tree representing type conversion of ARG1, a constant,
1469 return a constant tree representing the result of conversion. */
1472 fold_convert (t, arg1)
1476 register tree type = TREE_TYPE (t);
1479 if (TREE_CODE (type) == POINTER_TYPE || INTEGRAL_TYPE_P (type))
1481 if (TREE_CODE (arg1) == INTEGER_CST)
1483 /* If we would build a constant wider than GCC supports,
1484 leave the conversion unfolded. */
1485 if (TYPE_PRECISION (type) > 2 * HOST_BITS_PER_WIDE_INT)
1488 /* Given an integer constant, make new constant with new type,
1489 appropriately sign-extended or truncated. */
1490 t = build_int_2 (TREE_INT_CST_LOW (arg1),
1491 TREE_INT_CST_HIGH (arg1));
1492 TREE_TYPE (t) = type;
1493 /* Indicate an overflow if (1) ARG1 already overflowed,
1494 or (2) force_fit_type indicates an overflow.
1495 Tell force_fit_type that an overflow has already occurred
1496 if ARG1 is a too-large unsigned value and T is signed. */
1498 = (TREE_OVERFLOW (arg1)
1499 | force_fit_type (t,
1500 (TREE_INT_CST_HIGH (arg1) < 0
1501 & (TREE_UNSIGNED (type)
1502 < TREE_UNSIGNED (TREE_TYPE (arg1))))));
1503 TREE_CONSTANT_OVERFLOW (t)
1504 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
1506 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1507 else if (TREE_CODE (arg1) == REAL_CST)
1509 /* Don't initialize these, use assignments.
1510 Initialized local aggregates don't work on old compilers. */
1514 tree type1 = TREE_TYPE (arg1);
1516 x = TREE_REAL_CST (arg1);
1517 l = real_value_from_int_cst (type1, TYPE_MIN_VALUE (type));
1518 u = real_value_from_int_cst (type1, TYPE_MAX_VALUE (type));
1519 /* See if X will be in range after truncation towards 0.
1520 To compensate for truncation, move the bounds away from 0,
1521 but reject if X exactly equals the adjusted bounds. */
1522 #ifdef REAL_ARITHMETIC
1523 REAL_ARITHMETIC (l, MINUS_EXPR, l, dconst1);
1524 REAL_ARITHMETIC (u, PLUS_EXPR, u, dconst1);
1529 /* If X is a NaN, use zero instead and show we have an overflow.
1530 Otherwise, range check. */
1531 if (REAL_VALUE_ISNAN (x))
1532 overflow = 1, x = dconst0;
1533 else if (! (REAL_VALUES_LESS (l, x) && REAL_VALUES_LESS (x, u)))
1536 #ifndef REAL_ARITHMETIC
1538 HOST_WIDE_INT low, high;
1539 HOST_WIDE_INT half_word
1540 = (HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2);
1545 high = (HOST_WIDE_INT) (x / half_word / half_word);
1546 x -= (REAL_VALUE_TYPE) high * half_word * half_word;
1547 if (x >= (REAL_VALUE_TYPE) half_word * half_word / 2)
1549 low = x - (REAL_VALUE_TYPE) half_word * half_word / 2;
1550 low |= (HOST_WIDE_INT) -1 << (HOST_BITS_PER_WIDE_INT - 1);
1553 low = (HOST_WIDE_INT) x;
1554 if (TREE_REAL_CST (arg1) < 0)
1555 neg_double (low, high, &low, &high);
1556 t = build_int_2 (low, high);
1560 HOST_WIDE_INT low, high;
1561 REAL_VALUE_TO_INT (&low, &high, x);
1562 t = build_int_2 (low, high);
1565 TREE_TYPE (t) = type;
1567 = TREE_OVERFLOW (arg1) | force_fit_type (t, overflow);
1568 TREE_CONSTANT_OVERFLOW (t)
1569 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
1571 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
1572 TREE_TYPE (t) = type;
1574 else if (TREE_CODE (type) == REAL_TYPE)
1576 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1577 if (TREE_CODE (arg1) == INTEGER_CST)
1578 return build_real_from_int_cst (type, arg1);
1579 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
1580 if (TREE_CODE (arg1) == REAL_CST)
1582 if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1)))
1585 TREE_TYPE (arg1) = type;
1588 else if (setjmp (float_error))
1591 t = copy_node (arg1);
1594 set_float_handler (float_error);
1596 t = build_real (type, real_value_truncate (TYPE_MODE (type),
1597 TREE_REAL_CST (arg1)));
1598 set_float_handler (NULL_PTR);
1602 = TREE_OVERFLOW (arg1) | force_fit_type (t, overflow);
1603 TREE_CONSTANT_OVERFLOW (t)
1604 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
1608 TREE_CONSTANT (t) = 1;
1612 /* Return an expr equal to X but certainly not valid as an lvalue.
1613 Also make sure it is not valid as an null pointer constant. */
1621 /* These things are certainly not lvalues. */
1622 if (TREE_CODE (x) == NON_LVALUE_EXPR
1623 || TREE_CODE (x) == INTEGER_CST
1624 || TREE_CODE (x) == REAL_CST
1625 || TREE_CODE (x) == STRING_CST
1626 || TREE_CODE (x) == ADDR_EXPR)
1628 if (TREE_CODE (x) == INTEGER_CST && integer_zerop (x))
1630 /* Use NOP_EXPR instead of NON_LVALUE_EXPR
1631 so convert_for_assignment won't strip it.
1632 This is so this 0 won't be treated as a null pointer constant. */
1633 result = build1 (NOP_EXPR, TREE_TYPE (x), x);
1634 TREE_CONSTANT (result) = TREE_CONSTANT (x);
1640 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
1641 TREE_CONSTANT (result) = TREE_CONSTANT (x);
1645 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
1646 Zero means allow extended lvalues. */
1648 int pedantic_lvalues;
1650 /* When pedantic, return an expr equal to X but certainly not valid as a
1651 pedantic lvalue. Otherwise, return X. */
1654 pedantic_non_lvalue (x)
1657 if (pedantic_lvalues)
1658 return non_lvalue (x);
1663 /* Given a tree comparison code, return the code that is the logical inverse
1664 of the given code. It is not safe to do this for floating-point
1665 comparisons, except for NE_EXPR and EQ_EXPR. */
1667 static enum tree_code
1668 invert_tree_comparison (code)
1669 enum tree_code code;
1690 /* Similar, but return the comparison that results if the operands are
1691 swapped. This is safe for floating-point. */
1693 static enum tree_code
1694 swap_tree_comparison (code)
1695 enum tree_code code;
1715 /* Return nonzero if CODE is a tree code that represents a truth value. */
1718 truth_value_p (code)
1719 enum tree_code code;
1721 return (TREE_CODE_CLASS (code) == '<'
1722 || code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
1723 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
1724 || code == TRUTH_XOR_EXPR || code == TRUTH_NOT_EXPR);
1727 /* Return nonzero if two operands are necessarily equal.
1728 If ONLY_CONST is non-zero, only return non-zero for constants.
1729 This function tests whether the operands are indistinguishable;
1730 it does not test whether they are equal using C's == operation.
1731 The distinction is important for IEEE floating point, because
1732 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
1733 (2) two NaNs may be indistinguishable, but NaN!=NaN. */
1736 operand_equal_p (arg0, arg1, only_const)
1740 /* If both types don't have the same signedness, then we can't consider
1741 them equal. We must check this before the STRIP_NOPS calls
1742 because they may change the signedness of the arguments. */
1743 if (TREE_UNSIGNED (TREE_TYPE (arg0)) != TREE_UNSIGNED (TREE_TYPE (arg1)))
1749 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
1750 We don't care about side effects in that case because the SAVE_EXPR
1751 takes care of that for us. */
1752 if (TREE_CODE (arg0) == SAVE_EXPR && arg0 == arg1)
1753 return ! only_const;
1755 if (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1))
1758 if (TREE_CODE (arg0) == TREE_CODE (arg1)
1759 && TREE_CODE (arg0) == ADDR_EXPR
1760 && TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0))
1763 if (TREE_CODE (arg0) == TREE_CODE (arg1)
1764 && TREE_CODE (arg0) == INTEGER_CST
1765 && TREE_INT_CST_LOW (arg0) == TREE_INT_CST_LOW (arg1)
1766 && TREE_INT_CST_HIGH (arg0) == TREE_INT_CST_HIGH (arg1))
1769 /* Detect when real constants are equal. */
1770 if (TREE_CODE (arg0) == TREE_CODE (arg1)
1771 && TREE_CODE (arg0) == REAL_CST)
1772 return !bcmp ((char *) &TREE_REAL_CST (arg0),
1773 (char *) &TREE_REAL_CST (arg1),
1774 sizeof (REAL_VALUE_TYPE));
1782 if (TREE_CODE (arg0) != TREE_CODE (arg1))
1784 /* This is needed for conversions and for COMPONENT_REF.
1785 Might as well play it safe and always test this. */
1786 if (TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
1789 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
1792 /* Two conversions are equal only if signedness and modes match. */
1793 if ((TREE_CODE (arg0) == NOP_EXPR || TREE_CODE (arg0) == CONVERT_EXPR)
1794 && (TREE_UNSIGNED (TREE_TYPE (arg0))
1795 != TREE_UNSIGNED (TREE_TYPE (arg1))))
1798 return operand_equal_p (TREE_OPERAND (arg0, 0),
1799 TREE_OPERAND (arg1, 0), 0);
1803 return (operand_equal_p (TREE_OPERAND (arg0, 0),
1804 TREE_OPERAND (arg1, 0), 0)
1805 && operand_equal_p (TREE_OPERAND (arg0, 1),
1806 TREE_OPERAND (arg1, 1), 0));
1809 switch (TREE_CODE (arg0))
1812 return operand_equal_p (TREE_OPERAND (arg0, 0),
1813 TREE_OPERAND (arg1, 0), 0);
1817 return (operand_equal_p (TREE_OPERAND (arg0, 0),
1818 TREE_OPERAND (arg1, 0), 0)
1819 && operand_equal_p (TREE_OPERAND (arg0, 1),
1820 TREE_OPERAND (arg1, 1), 0));
1823 return (operand_equal_p (TREE_OPERAND (arg0, 0),
1824 TREE_OPERAND (arg1, 0), 0)
1825 && operand_equal_p (TREE_OPERAND (arg0, 1),
1826 TREE_OPERAND (arg1, 1), 0)
1827 && operand_equal_p (TREE_OPERAND (arg0, 2),
1828 TREE_OPERAND (arg1, 2), 0));
1836 /* Similar to operand_equal_p, but see if ARG0 might have been made by
1837 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
1839 When in doubt, return 0. */
1842 operand_equal_for_comparison_p (arg0, arg1, other)
1846 int unsignedp1, unsignedpo;
1847 tree primarg1, primother;
1848 unsigned correct_width;
1850 if (operand_equal_p (arg0, arg1, 0))
1853 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
1854 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
1857 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
1858 actual comparison operand, ARG0.
1860 First throw away any conversions to wider types
1861 already present in the operands. */
1863 primarg1 = get_narrower (arg1, &unsignedp1);
1864 primother = get_narrower (other, &unsignedpo);
1866 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
1867 if (unsignedp1 == unsignedpo
1868 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
1869 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
1871 tree type = TREE_TYPE (arg0);
1873 /* Make sure shorter operand is extended the right way
1874 to match the longer operand. */
1875 primarg1 = convert (signed_or_unsigned_type (unsignedp1,
1876 TREE_TYPE (primarg1)),
1879 if (operand_equal_p (arg0, convert (type, primarg1), 0))
1886 /* See if ARG is an expression that is either a comparison or is performing
1887 arithmetic on comparisons. The comparisons must only be comparing
1888 two different values, which will be stored in *CVAL1 and *CVAL2; if
1889 they are non-zero it means that some operands have already been found.
1890 No variables may be used anywhere else in the expression except in the
1891 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
1892 the expression and save_expr needs to be called with CVAL1 and CVAL2.
1894 If this is true, return 1. Otherwise, return zero. */
1897 twoval_comparison_p (arg, cval1, cval2, save_p)
1899 tree *cval1, *cval2;
1902 enum tree_code code = TREE_CODE (arg);
1903 char class = TREE_CODE_CLASS (code);
1905 /* We can handle some of the 'e' cases here. */
1906 if (class == 'e' && code == TRUTH_NOT_EXPR)
1908 else if (class == 'e'
1909 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
1910 || code == COMPOUND_EXPR))
1913 /* ??? Disable this since the SAVE_EXPR might already be in use outside
1914 the expression. There may be no way to make this work, but it needs
1915 to be looked at again for 2.6. */
1917 else if (class == 'e' && code == SAVE_EXPR && SAVE_EXPR_RTL (arg) == 0)
1919 /* If we've already found a CVAL1 or CVAL2, this expression is
1920 two complex to handle. */
1921 if (*cval1 || *cval2)
1932 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
1935 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
1936 && twoval_comparison_p (TREE_OPERAND (arg, 1),
1937 cval1, cval2, save_p));
1943 if (code == COND_EXPR)
1944 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
1945 cval1, cval2, save_p)
1946 && twoval_comparison_p (TREE_OPERAND (arg, 1),
1947 cval1, cval2, save_p)
1948 && twoval_comparison_p (TREE_OPERAND (arg, 2),
1949 cval1, cval2, save_p));
1953 /* First see if we can handle the first operand, then the second. For
1954 the second operand, we know *CVAL1 can't be zero. It must be that
1955 one side of the comparison is each of the values; test for the
1956 case where this isn't true by failing if the two operands
1959 if (operand_equal_p (TREE_OPERAND (arg, 0),
1960 TREE_OPERAND (arg, 1), 0))
1964 *cval1 = TREE_OPERAND (arg, 0);
1965 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
1967 else if (*cval2 == 0)
1968 *cval2 = TREE_OPERAND (arg, 0);
1969 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
1974 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
1976 else if (*cval2 == 0)
1977 *cval2 = TREE_OPERAND (arg, 1);
1978 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
1989 /* ARG is a tree that is known to contain just arithmetic operations and
1990 comparisons. Evaluate the operations in the tree substituting NEW0 for
1991 any occurrence of OLD0 as an operand of a comparison and likewise for
1995 eval_subst (arg, old0, new0, old1, new1)
1997 tree old0, new0, old1, new1;
1999 tree type = TREE_TYPE (arg);
2000 enum tree_code code = TREE_CODE (arg);
2001 char class = TREE_CODE_CLASS (code);
2003 /* We can handle some of the 'e' cases here. */
2004 if (class == 'e' && code == TRUTH_NOT_EXPR)
2006 else if (class == 'e'
2007 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2013 return fold (build1 (code, type,
2014 eval_subst (TREE_OPERAND (arg, 0),
2015 old0, new0, old1, new1)));
2018 return fold (build (code, type,
2019 eval_subst (TREE_OPERAND (arg, 0),
2020 old0, new0, old1, new1),
2021 eval_subst (TREE_OPERAND (arg, 1),
2022 old0, new0, old1, new1)));
2028 return eval_subst (TREE_OPERAND (arg, 0), old0, new0, old1, new1);
2031 return eval_subst (TREE_OPERAND (arg, 1), old0, new0, old1, new1);
2034 return fold (build (code, type,
2035 eval_subst (TREE_OPERAND (arg, 0),
2036 old0, new0, old1, new1),
2037 eval_subst (TREE_OPERAND (arg, 1),
2038 old0, new0, old1, new1),
2039 eval_subst (TREE_OPERAND (arg, 2),
2040 old0, new0, old1, new1)));
2045 tree arg0 = TREE_OPERAND (arg, 0);
2046 tree arg1 = TREE_OPERAND (arg, 1);
2048 /* We need to check both for exact equality and tree equality. The
2049 former will be true if the operand has a side-effect. In that
2050 case, we know the operand occurred exactly once. */
2052 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
2054 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
2057 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
2059 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
2062 return fold (build (code, type, arg0, arg1));
2069 /* Return a tree for the case when the result of an expression is RESULT
2070 converted to TYPE and OMITTED was previously an operand of the expression
2071 but is now not needed (e.g., we folded OMITTED * 0).
2073 If OMITTED has side effects, we must evaluate it. Otherwise, just do
2074 the conversion of RESULT to TYPE. */
2077 omit_one_operand (type, result, omitted)
2078 tree type, result, omitted;
2080 tree t = convert (type, result);
2082 if (TREE_SIDE_EFFECTS (omitted))
2083 return build (COMPOUND_EXPR, type, omitted, t);
2085 return non_lvalue (t);
2088 /* Similar, but call pedantic_non_lvalue instead of non_lvalue. */
2091 pedantic_omit_one_operand (type, result, omitted)
2092 tree type, result, omitted;
2094 tree t = convert (type, result);
2096 if (TREE_SIDE_EFFECTS (omitted))
2097 return build (COMPOUND_EXPR, type, omitted, t);
2099 return pedantic_non_lvalue (t);
2104 /* Return a simplified tree node for the truth-negation of ARG. This
2105 never alters ARG itself. We assume that ARG is an operation that
2106 returns a truth value (0 or 1). */
2109 invert_truthvalue (arg)
2112 tree type = TREE_TYPE (arg);
2113 enum tree_code code = TREE_CODE (arg);
2115 if (code == ERROR_MARK)
2118 /* If this is a comparison, we can simply invert it, except for
2119 floating-point non-equality comparisons, in which case we just
2120 enclose a TRUTH_NOT_EXPR around what we have. */
2122 if (TREE_CODE_CLASS (code) == '<')
2124 if (FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg, 0)))
2125 && code != NE_EXPR && code != EQ_EXPR)
2126 return build1 (TRUTH_NOT_EXPR, type, arg);
2128 return build (invert_tree_comparison (code), type,
2129 TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
2135 return convert (type, build_int_2 (TREE_INT_CST_LOW (arg) == 0
2136 && TREE_INT_CST_HIGH (arg) == 0, 0));
2138 case TRUTH_AND_EXPR:
2139 return build (TRUTH_OR_EXPR, type,
2140 invert_truthvalue (TREE_OPERAND (arg, 0)),
2141 invert_truthvalue (TREE_OPERAND (arg, 1)));
2144 return build (TRUTH_AND_EXPR, type,
2145 invert_truthvalue (TREE_OPERAND (arg, 0)),
2146 invert_truthvalue (TREE_OPERAND (arg, 1)));
2148 case TRUTH_XOR_EXPR:
2149 /* Here we can invert either operand. We invert the first operand
2150 unless the second operand is a TRUTH_NOT_EXPR in which case our
2151 result is the XOR of the first operand with the inside of the
2152 negation of the second operand. */
2154 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
2155 return build (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
2156 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
2158 return build (TRUTH_XOR_EXPR, type,
2159 invert_truthvalue (TREE_OPERAND (arg, 0)),
2160 TREE_OPERAND (arg, 1));
2162 case TRUTH_ANDIF_EXPR:
2163 return build (TRUTH_ORIF_EXPR, type,
2164 invert_truthvalue (TREE_OPERAND (arg, 0)),
2165 invert_truthvalue (TREE_OPERAND (arg, 1)));
2167 case TRUTH_ORIF_EXPR:
2168 return build (TRUTH_ANDIF_EXPR, type,
2169 invert_truthvalue (TREE_OPERAND (arg, 0)),
2170 invert_truthvalue (TREE_OPERAND (arg, 1)));
2172 case TRUTH_NOT_EXPR:
2173 return TREE_OPERAND (arg, 0);
2176 return build (COND_EXPR, type, TREE_OPERAND (arg, 0),
2177 invert_truthvalue (TREE_OPERAND (arg, 1)),
2178 invert_truthvalue (TREE_OPERAND (arg, 2)));
2181 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0),
2182 invert_truthvalue (TREE_OPERAND (arg, 1)));
2184 case NON_LVALUE_EXPR:
2185 return invert_truthvalue (TREE_OPERAND (arg, 0));
2190 return build1 (TREE_CODE (arg), type,
2191 invert_truthvalue (TREE_OPERAND (arg, 0)));
2194 if (!integer_onep (TREE_OPERAND (arg, 1)))
2196 return build (EQ_EXPR, type, arg, convert (type, integer_zero_node));
2199 return build1 (TRUTH_NOT_EXPR, type, arg);
2201 case CLEANUP_POINT_EXPR:
2202 return build1 (CLEANUP_POINT_EXPR, type,
2203 invert_truthvalue (TREE_OPERAND (arg, 0)));
2205 if (TREE_CODE (TREE_TYPE (arg)) != BOOLEAN_TYPE)
2207 return build1 (TRUTH_NOT_EXPR, type, arg);
2210 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
2211 operands are another bit-wise operation with a common input. If so,
2212 distribute the bit operations to save an operation and possibly two if
2213 constants are involved. For example, convert
2214 (A | B) & (A | C) into A | (B & C)
2215 Further simplification will occur if B and C are constants.
2217 If this optimization cannot be done, 0 will be returned. */
2220 distribute_bit_expr (code, type, arg0, arg1)
2221 enum tree_code code;
2228 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2229 || TREE_CODE (arg0) == code
2230 || (TREE_CODE (arg0) != BIT_AND_EXPR
2231 && TREE_CODE (arg0) != BIT_IOR_EXPR))
2234 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
2236 common = TREE_OPERAND (arg0, 0);
2237 left = TREE_OPERAND (arg0, 1);
2238 right = TREE_OPERAND (arg1, 1);
2240 else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
2242 common = TREE_OPERAND (arg0, 0);
2243 left = TREE_OPERAND (arg0, 1);
2244 right = TREE_OPERAND (arg1, 0);
2246 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
2248 common = TREE_OPERAND (arg0, 1);
2249 left = TREE_OPERAND (arg0, 0);
2250 right = TREE_OPERAND (arg1, 1);
2252 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
2254 common = TREE_OPERAND (arg0, 1);
2255 left = TREE_OPERAND (arg0, 0);
2256 right = TREE_OPERAND (arg1, 0);
2261 return fold (build (TREE_CODE (arg0), type, common,
2262 fold (build (code, type, left, right))));
2265 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
2266 starting at BITPOS. The field is unsigned if UNSIGNEDP is non-zero. */
2269 make_bit_field_ref (inner, type, bitsize, bitpos, unsignedp)
2272 int bitsize, bitpos;
2275 tree result = build (BIT_FIELD_REF, type, inner,
2276 size_int (bitsize), size_int (bitpos));
2278 TREE_UNSIGNED (result) = unsignedp;
2283 /* Optimize a bit-field compare.
2285 There are two cases: First is a compare against a constant and the
2286 second is a comparison of two items where the fields are at the same
2287 bit position relative to the start of a chunk (byte, halfword, word)
2288 large enough to contain it. In these cases we can avoid the shift
2289 implicit in bitfield extractions.
2291 For constants, we emit a compare of the shifted constant with the
2292 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
2293 compared. For two fields at the same position, we do the ANDs with the
2294 similar mask and compare the result of the ANDs.
2296 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
2297 COMPARE_TYPE is the type of the comparison, and LHS and RHS
2298 are the left and right operands of the comparison, respectively.
2300 If the optimization described above can be done, we return the resulting
2301 tree. Otherwise we return zero. */
2304 optimize_bit_field_compare (code, compare_type, lhs, rhs)
2305 enum tree_code code;
2309 int lbitpos, lbitsize, rbitpos, rbitsize;
2310 int lnbitpos, lnbitsize, rnbitpos, rnbitsize;
2311 tree type = TREE_TYPE (lhs);
2312 tree signed_type, unsigned_type;
2313 int const_p = TREE_CODE (rhs) == INTEGER_CST;
2314 enum machine_mode lmode, rmode, lnmode, rnmode;
2315 int lunsignedp, runsignedp;
2316 int lvolatilep = 0, rvolatilep = 0;
2317 tree linner, rinner;
2321 /* Get all the information about the extractions being done. If the bit size
2322 if the same as the size of the underlying object, we aren't doing an
2323 extraction at all and so can do nothing. */
2324 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
2325 &lunsignedp, &lvolatilep);
2326 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
2332 /* If this is not a constant, we can only do something if bit positions,
2333 sizes, and signedness are the same. */
2334 rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset,
2335 &rmode, &runsignedp, &rvolatilep);
2337 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
2338 || lunsignedp != runsignedp || offset != 0)
2342 /* See if we can find a mode to refer to this field. We should be able to,
2343 but fail if we can't. */
2344 lnmode = get_best_mode (lbitsize, lbitpos,
2345 TYPE_ALIGN (TREE_TYPE (linner)), word_mode,
2347 if (lnmode == VOIDmode)
2350 /* Set signed and unsigned types of the precision of this mode for the
2352 signed_type = type_for_mode (lnmode, 0);
2353 unsigned_type = type_for_mode (lnmode, 1);
2357 rnmode = get_best_mode (rbitsize, rbitpos,
2358 TYPE_ALIGN (TREE_TYPE (rinner)), word_mode,
2360 if (rnmode == VOIDmode)
2364 /* Compute the bit position and size for the new reference and our offset
2365 within it. If the new reference is the same size as the original, we
2366 won't optimize anything, so return zero. */
2367 lnbitsize = GET_MODE_BITSIZE (lnmode);
2368 lnbitpos = lbitpos & ~ (lnbitsize - 1);
2369 lbitpos -= lnbitpos;
2370 if (lnbitsize == lbitsize)
2375 rnbitsize = GET_MODE_BITSIZE (rnmode);
2376 rnbitpos = rbitpos & ~ (rnbitsize - 1);
2377 rbitpos -= rnbitpos;
2378 if (rnbitsize == rbitsize)
2382 if (BYTES_BIG_ENDIAN)
2383 lbitpos = lnbitsize - lbitsize - lbitpos;
2385 /* Make the mask to be used against the extracted field. */
2386 mask = build_int_2 (~0, ~0);
2387 TREE_TYPE (mask) = unsigned_type;
2388 force_fit_type (mask, 0);
2389 mask = convert (unsigned_type, mask);
2390 mask = const_binop (LSHIFT_EXPR, mask, size_int (lnbitsize - lbitsize), 0);
2391 mask = const_binop (RSHIFT_EXPR, mask,
2392 size_int (lnbitsize - lbitsize - lbitpos), 0);
2395 /* If not comparing with constant, just rework the comparison
2397 return build (code, compare_type,
2398 build (BIT_AND_EXPR, unsigned_type,
2399 make_bit_field_ref (linner, unsigned_type,
2400 lnbitsize, lnbitpos, 1),
2402 build (BIT_AND_EXPR, unsigned_type,
2403 make_bit_field_ref (rinner, unsigned_type,
2404 rnbitsize, rnbitpos, 1),
2407 /* Otherwise, we are handling the constant case. See if the constant is too
2408 big for the field. Warn and return a tree of for 0 (false) if so. We do
2409 this not only for its own sake, but to avoid having to test for this
2410 error case below. If we didn't, we might generate wrong code.
2412 For unsigned fields, the constant shifted right by the field length should
2413 be all zero. For signed fields, the high-order bits should agree with
2418 if (! integer_zerop (const_binop (RSHIFT_EXPR,
2419 convert (unsigned_type, rhs),
2420 size_int (lbitsize), 0)))
2422 warning ("comparison is always %s due to width of bitfield",
2423 code == NE_EXPR ? "one" : "zero");
2424 return convert (compare_type,
2426 ? integer_one_node : integer_zero_node));
2431 tree tem = const_binop (RSHIFT_EXPR, convert (signed_type, rhs),
2432 size_int (lbitsize - 1), 0);
2433 if (! integer_zerop (tem) && ! integer_all_onesp (tem))
2435 warning ("comparison is always %s due to width of bitfield",
2436 code == NE_EXPR ? "one" : "zero");
2437 return convert (compare_type,
2439 ? integer_one_node : integer_zero_node));
2443 /* Single-bit compares should always be against zero. */
2444 if (lbitsize == 1 && ! integer_zerop (rhs))
2446 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
2447 rhs = convert (type, integer_zero_node);
2450 /* Make a new bitfield reference, shift the constant over the
2451 appropriate number of bits and mask it with the computed mask
2452 (in case this was a signed field). If we changed it, make a new one. */
2453 lhs = make_bit_field_ref (linner, unsigned_type, lnbitsize, lnbitpos, 1);
2456 TREE_SIDE_EFFECTS (lhs) = 1;
2457 TREE_THIS_VOLATILE (lhs) = 1;
2460 rhs = fold (const_binop (BIT_AND_EXPR,
2461 const_binop (LSHIFT_EXPR,
2462 convert (unsigned_type, rhs),
2463 size_int (lbitpos), 0),
2466 return build (code, compare_type,
2467 build (BIT_AND_EXPR, unsigned_type, lhs, mask),
2471 /* Subroutine for fold_truthop: decode a field reference.
2473 If EXP is a comparison reference, we return the innermost reference.
2475 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
2476 set to the starting bit number.
2478 If the innermost field can be completely contained in a mode-sized
2479 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
2481 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
2482 otherwise it is not changed.
2484 *PUNSIGNEDP is set to the signedness of the field.
2486 *PMASK is set to the mask used. This is either contained in a
2487 BIT_AND_EXPR or derived from the width of the field.
2489 *PAND_MASK is set the the mask found in a BIT_AND_EXPR, if any.
2491 Return 0 if this is not a component reference or is one that we can't
2492 do anything with. */
2495 decode_field_reference (exp, pbitsize, pbitpos, pmode, punsignedp,
2496 pvolatilep, pmask, pand_mask)
2498 int *pbitsize, *pbitpos;
2499 enum machine_mode *pmode;
2500 int *punsignedp, *pvolatilep;
2505 tree mask, inner, offset;
2509 /* All the optimizations using this function assume integer fields.
2510 There are problems with FP fields since the type_for_size call
2511 below can fail for, e.g., XFmode. */
2512 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
2517 if (TREE_CODE (exp) == BIT_AND_EXPR)
2519 and_mask = TREE_OPERAND (exp, 1);
2520 exp = TREE_OPERAND (exp, 0);
2521 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
2522 if (TREE_CODE (and_mask) != INTEGER_CST)
2527 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
2528 punsignedp, pvolatilep);
2529 if ((inner == exp && and_mask == 0)
2530 || *pbitsize < 0 || offset != 0)
2533 /* Compute the mask to access the bitfield. */
2534 unsigned_type = type_for_size (*pbitsize, 1);
2535 precision = TYPE_PRECISION (unsigned_type);
2537 mask = build_int_2 (~0, ~0);
2538 TREE_TYPE (mask) = unsigned_type;
2539 force_fit_type (mask, 0);
2540 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
2541 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
2543 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
2545 mask = fold (build (BIT_AND_EXPR, unsigned_type,
2546 convert (unsigned_type, and_mask), mask));
2549 *pand_mask = and_mask;
2553 /* Return non-zero if MASK represents a mask of SIZE ones in the low-order
2557 all_ones_mask_p (mask, size)
2561 tree type = TREE_TYPE (mask);
2562 int precision = TYPE_PRECISION (type);
2565 tmask = build_int_2 (~0, ~0);
2566 TREE_TYPE (tmask) = signed_type (type);
2567 force_fit_type (tmask, 0);
2569 tree_int_cst_equal (mask,
2570 const_binop (RSHIFT_EXPR,
2571 const_binop (LSHIFT_EXPR, tmask,
2572 size_int (precision - size),
2574 size_int (precision - size), 0));
2577 /* Subroutine for fold_truthop: determine if an operand is simple enough
2578 to be evaluated unconditionally. */
2581 simple_operand_p (exp)
2584 /* Strip any conversions that don't change the machine mode. */
2585 while ((TREE_CODE (exp) == NOP_EXPR
2586 || TREE_CODE (exp) == CONVERT_EXPR)
2587 && (TYPE_MODE (TREE_TYPE (exp))
2588 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)))))
2589 exp = TREE_OPERAND (exp, 0);
2591 return (TREE_CODE_CLASS (TREE_CODE (exp)) == 'c'
2592 || (TREE_CODE_CLASS (TREE_CODE (exp)) == 'd'
2593 && ! TREE_ADDRESSABLE (exp)
2594 && ! TREE_THIS_VOLATILE (exp)
2595 && ! DECL_NONLOCAL (exp)
2596 /* Don't regard global variables as simple. They may be
2597 allocated in ways unknown to the compiler (shared memory,
2598 #pragma weak, etc). */
2599 && ! TREE_PUBLIC (exp)
2600 && ! DECL_EXTERNAL (exp)
2601 /* Loading a static variable is unduly expensive, but global
2602 registers aren't expensive. */
2603 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
2606 /* The following functions are subroutines to fold_range_test and allow it to
2607 try to change a logical combination of comparisons into a range test.
2610 X == 2 && X == 3 && X == 4 && X == 5
2614 (unsigned) (X - 2) <= 3
2616 We decribe each set of comparisons as being either inside or outside
2617 a range, using a variable named like IN_P, and then describe the
2618 range with a lower and upper bound. If one of the bounds is omitted,
2619 it represents either the highest or lowest value of the type.
2621 In the comments below, we represent a range by two numbers in brackets
2622 preceeded by a "+" to designate being inside that range, or a "-" to
2623 designate being outside that range, so the condition can be inverted by
2624 flipping the prefix. An omitted bound is represented by a "-". For
2625 example, "- [-, 10]" means being outside the range starting at the lowest
2626 possible value and ending at 10, in other words, being greater than 10.
2627 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
2630 We set up things so that the missing bounds are handled in a consistent
2631 manner so neither a missing bound nor "true" and "false" need to be
2632 handled using a special case. */
2634 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
2635 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
2636 and UPPER1_P are nonzero if the respective argument is an upper bound
2637 and zero for a lower. TYPE, if nonzero, is the type of the result; it
2638 must be specified for a comparison. ARG1 will be converted to ARG0's
2639 type if both are specified. */
2642 range_binop (code, type, arg0, upper0_p, arg1, upper1_p)
2643 enum tree_code code;
2646 int upper0_p, upper1_p;
2652 /* If neither arg represents infinity, do the normal operation.
2653 Else, if not a comparison, return infinity. Else handle the special
2654 comparison rules. Note that most of the cases below won't occur, but
2655 are handled for consistency. */
2657 if (arg0 != 0 && arg1 != 0)
2659 tem = fold (build (code, type != 0 ? type : TREE_TYPE (arg0),
2660 arg0, convert (TREE_TYPE (arg0), arg1)));
2662 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
2665 if (TREE_CODE_CLASS (code) != '<')
2668 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
2669 for neither. Then compute our result treating them as never equal
2670 and comparing bounds to non-bounds as above. */
2671 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
2672 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
2675 case EQ_EXPR: case NE_EXPR:
2676 result = (code == NE_EXPR);
2678 case LT_EXPR: case LE_EXPR:
2679 result = sgn0 < sgn1;
2681 case GT_EXPR: case GE_EXPR:
2682 result = sgn0 > sgn1;
2686 return convert (type, result ? integer_one_node : integer_zero_node);
2689 /* Given EXP, a logical expression, set the range it is testing into
2690 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
2691 actually being tested. *PLOW and *PHIGH will have be made the same type
2692 as the returned expression. If EXP is not a comparison, we will most
2693 likely not be returning a useful value and range. */
2696 make_range (exp, pin_p, plow, phigh)
2701 enum tree_code code;
2702 tree arg0, arg1, type;
2704 tree low, high, n_low, n_high;
2706 /* Start with simply saying "EXP != 0" and then look at the code of EXP
2707 and see if we can refine the range. Some of the cases below may not
2708 happen, but it doesn't seem worth worrying about this. We "continue"
2709 the outer loop when we've changed something; otherwise we "break"
2710 the switch, which will "break" the while. */
2712 in_p = 0, low = high = convert (TREE_TYPE (exp), integer_zero_node);
2716 code = TREE_CODE (exp);
2717 arg0 = TREE_OPERAND (exp, 0), arg1 = TREE_OPERAND (exp, 1);
2718 if (arg0 != 0 && tree_code_length[(int) code] > 0)
2719 type = TREE_TYPE (arg0);
2723 case TRUTH_NOT_EXPR:
2724 in_p = ! in_p, exp = arg0;
2727 case EQ_EXPR: case NE_EXPR:
2728 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
2729 /* We can only do something if the range is testing for zero
2730 and if the second operand is an integer constant. Note that
2731 saying something is "in" the range we make is done by
2732 complementing IN_P since it will set in the initial case of
2733 being not equal to zero; "out" is leaving it alone. */
2734 if (low == 0 || high == 0
2735 || ! integer_zerop (low) || ! integer_zerop (high)
2736 || TREE_CODE (arg1) != INTEGER_CST)
2741 case NE_EXPR: /* - [c, c] */
2744 case EQ_EXPR: /* + [c, c] */
2745 in_p = ! in_p, low = high = arg1;
2747 case GT_EXPR: /* - [-, c] */
2748 low = 0, high = arg1;
2750 case GE_EXPR: /* + [c, -] */
2751 in_p = ! in_p, low = arg1, high = 0;
2753 case LT_EXPR: /* - [c, -] */
2754 low = arg1, high = 0;
2756 case LE_EXPR: /* + [-, c] */
2757 in_p = ! in_p, low = 0, high = arg1;
2763 /* If this is an unsigned comparison, we also know that EXP is
2764 greater than or equal to zero. We base the range tests we make
2765 on that fact, so we record it here so we can parse existing
2767 if (TREE_UNSIGNED (type) && (low == 0 || high == 0))
2769 if (! merge_ranges (&n_in_p, &n_low, &n_high, in_p, low, high,
2770 1, convert (type, integer_zero_node),
2774 in_p = n_in_p, low = n_low, high = n_high;
2776 /* If the high bound is missing, reverse the range so it
2777 goes from zero to the low bound minus 1. */
2781 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
2782 integer_one_node, 0);
2783 low = convert (type, integer_zero_node);
2789 /* (-x) IN [a,b] -> x in [-b, -a] */
2790 n_low = range_binop (MINUS_EXPR, type,
2791 convert (type, integer_zero_node), 0, high, 1);
2792 n_high = range_binop (MINUS_EXPR, type,
2793 convert (type, integer_zero_node), 0, low, 0);
2794 low = n_low, high = n_high;
2800 exp = build (MINUS_EXPR, type, build1 (NEGATE_EXPR, type, arg0),
2801 convert (type, integer_one_node));
2804 case PLUS_EXPR: case MINUS_EXPR:
2805 if (TREE_CODE (arg1) != INTEGER_CST)
2808 /* If EXP is signed, any overflow in the computation is undefined,
2809 so we don't worry about it so long as our computations on
2810 the bounds don't overflow. For unsigned, overflow is defined
2811 and this is exactly the right thing. */
2812 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
2813 type, low, 0, arg1, 0);
2814 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
2815 type, high, 1, arg1, 0);
2816 if ((n_low != 0 && TREE_OVERFLOW (n_low))
2817 || (n_high != 0 && TREE_OVERFLOW (n_high)))
2820 /* Check for an unsigned range which has wrapped around the maximum
2821 value thus making n_high < n_low, and normalize it. */
2822 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
2824 low = range_binop (PLUS_EXPR, type, n_high, 0,
2825 integer_one_node, 0);
2826 high = range_binop (MINUS_EXPR, type, n_low, 0,
2827 integer_one_node, 0);
2831 low = n_low, high = n_high;
2836 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
2837 if (! INTEGRAL_TYPE_P (type)
2838 || (low != 0 && ! int_fits_type_p (low, type))
2839 || (high != 0 && ! int_fits_type_p (high, type)))
2843 low = convert (type, low);
2846 high = convert (type, high);
2855 *pin_p = in_p, *plow = low, *phigh = high;
2859 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
2860 type, TYPE, return an expression to test if EXP is in (or out of, depending
2861 on IN_P) the range. */
2864 build_range_check (type, exp, in_p, low, high)
2870 tree etype = TREE_TYPE (exp);
2874 && (0 != (value = build_range_check (type, exp, 1, low, high))))
2875 return invert_truthvalue (value);
2877 else if (low == 0 && high == 0)
2878 return convert (type, integer_one_node);
2881 return fold (build (LE_EXPR, type, exp, high));
2884 return fold (build (GE_EXPR, type, exp, low));
2886 else if (operand_equal_p (low, high, 0))
2887 return fold (build (EQ_EXPR, type, exp, low));
2889 else if (TREE_UNSIGNED (etype) && integer_zerop (low))
2890 return build_range_check (type, exp, 1, 0, high);
2892 else if (integer_zerop (low))
2894 utype = unsigned_type (etype);
2895 return build_range_check (type, convert (utype, exp), 1, 0,
2896 convert (utype, high));
2899 else if (0 != (value = const_binop (MINUS_EXPR, high, low, 0))
2900 && ! TREE_OVERFLOW (value))
2901 return build_range_check (type,
2902 fold (build (MINUS_EXPR, etype, exp, low)),
2903 1, convert (etype, integer_zero_node), value);
2908 /* Given two ranges, see if we can merge them into one. Return 1 if we
2909 can, 0 if we can't. Set the output range into the specified parameters. */
2912 merge_ranges (pin_p, plow, phigh, in0_p, low0, high0, in1_p, low1, high1)
2916 tree low0, high0, low1, high1;
2925 /* Make range 0 be the range that starts first. Swap them if it isn't. */
2926 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
2928 || (((low0 == 0 && low1 == 0)
2929 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
2931 && integer_onep (range_binop (GT_EXPR, integer_type_node,
2932 high0, 1, high1, 1))))
2934 temp = in0_p, in0_p = in1_p, in1_p = temp;
2935 tem = low0, low0 = low1, low1 = tem;
2936 tem = high0, high0 = high1, high1 = tem;
2939 /* Now flag two cases, whether the ranges are disjoint or whether the
2940 second range is totally subsumed in the first. Note that the tests
2941 below are simplified by the ones above. */
2942 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
2943 high0, 1, low1, 0));
2944 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
2945 high1, 1, high0, 1));
2947 /* We now have four cases, depending on whether we are including or
2948 excluding the two ranges. */
2951 /* If they don't overlap, the result is false. If the second range
2952 is a subset it is the result. Otherwise, the range is from the start
2953 of the second to the end of the first. */
2955 in_p = 0, low = high = 0;
2957 in_p = 1, low = low1, high = high1;
2959 in_p = 1, low = low1, high = high0;
2962 else if (in0_p && ! in1_p)
2964 /* If they don't overlap, the result is the first range. If the
2965 second range is a subset of the first, we can't describe this as
2966 a single range unless both ranges end at the same place. If both
2967 ranges also start in the same place, then the result is false.
2968 Otherwise, we go from the start of the first range to just before
2969 the start of the second. */
2971 in_p = 1, low = low0, high = high0;
2973 && integer_zerop (range_binop (EQ_EXPR, integer_type_node,
2974 high0, 1, high1, 0)))
2977 && integer_onep (range_binop (EQ_EXPR, integer_type_node,
2979 in_p = 0, low = high = 0;
2982 in_p = 1, low = low0;
2983 high = range_binop (MINUS_EXPR, NULL_TREE, low1, 0,
2984 integer_one_node, 0);
2988 else if (! in0_p && in1_p)
2990 /* If they don't overlap, the result is the second range. If the second
2991 is a subset of the first, the result is false. Otherwise,
2992 the range starts just after the first range and ends at the
2993 end of the second. */
2995 in_p = 1, low = low1, high = high1;
2997 in_p = 0, low = high = 0;
3000 in_p = 1, high = high1;
3001 low = range_binop (PLUS_EXPR, NULL_TREE, high0, 1,
3002 integer_one_node, 0);
3008 /* The case where we are excluding both ranges. Here the complex case
3009 is if they don't overlap. In that case, the only time we have a
3010 range is if they are adjacent. If the second is a subset of the
3011 first, the result is the first. Otherwise, the range to exclude
3012 starts at the beginning of the first range and ends at the end of the
3016 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
3017 range_binop (PLUS_EXPR, NULL_TREE,
3019 integer_one_node, 1),
3021 in_p = 0, low = low0, high = high1;
3026 in_p = 0, low = low0, high = high0;
3028 in_p = 0, low = low0, high = high1;
3031 *pin_p = in_p, *plow = low, *phigh = high;
3035 /* EXP is some logical combination of boolean tests. See if we can
3036 merge it into some range test. Return the new tree if so. */
3039 fold_range_test (exp)
3042 int or_op = (TREE_CODE (exp) == TRUTH_ORIF_EXPR
3043 || TREE_CODE (exp) == TRUTH_OR_EXPR);
3044 int in0_p, in1_p, in_p;
3045 tree low0, low1, low, high0, high1, high;
3046 tree lhs = make_range (TREE_OPERAND (exp, 0), &in0_p, &low0, &high0);
3047 tree rhs = make_range (TREE_OPERAND (exp, 1), &in1_p, &low1, &high1);
3050 /* If this is an OR operation, invert both sides; we will invert
3051 again at the end. */
3053 in0_p = ! in0_p, in1_p = ! in1_p;
3055 /* If both expressions are the same, if we can merge the ranges, and we
3056 can build the range test, return it or it inverted. */
3057 if (operand_equal_p (lhs, rhs, 0)
3058 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
3060 && 0 != (tem = (build_range_check (TREE_TYPE (exp), lhs,
3062 return or_op ? invert_truthvalue (tem) : tem;
3064 /* On machines where the branch cost is expensive, if this is a
3065 short-circuited branch and the underlying object on both sides
3066 is the same, make a non-short-circuit operation. */
3067 else if (BRANCH_COST >= 2
3068 && (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
3069 || TREE_CODE (exp) == TRUTH_ORIF_EXPR)
3070 && operand_equal_p (lhs, rhs, 0))
3072 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR. */
3073 if (simple_operand_p (lhs))
3074 return build (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
3075 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
3076 TREE_TYPE (exp), TREE_OPERAND (exp, 0),
3077 TREE_OPERAND (exp, 1));
3080 tree common = save_expr (lhs);
3082 if (0 != (lhs = build_range_check (TREE_TYPE (exp), common,
3083 or_op ? ! in0_p : in0_p,
3085 && (0 != (rhs = build_range_check (TREE_TYPE (exp), common,
3086 or_op ? ! in1_p : in1_p,
3088 return build (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
3089 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
3090 TREE_TYPE (exp), lhs, rhs);
3097 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
3098 bit value. Arrange things so the extra bits will be set to zero if and
3099 only if C is signed-extended to its full width. If MASK is nonzero,
3100 it is an INTEGER_CST that should be AND'ed with the extra bits. */
3103 unextend (c, p, unsignedp, mask)
3109 tree type = TREE_TYPE (c);
3110 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
3113 if (p == modesize || unsignedp)
3116 if (TREE_UNSIGNED (type))
3117 c = convert (signed_type (type), c);
3119 /* We work by getting just the sign bit into the low-order bit, then
3120 into the high-order bit, then sign-extend. We then XOR that value
3122 temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
3123 temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
3124 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
3125 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
3127 temp = const_binop (BIT_AND_EXPR, temp, convert (TREE_TYPE (c), mask), 0);
3129 return convert (type, const_binop (BIT_XOR_EXPR, c, temp, 0));
3132 /* Find ways of folding logical expressions of LHS and RHS:
3133 Try to merge two comparisons to the same innermost item.
3134 Look for range tests like "ch >= '0' && ch <= '9'".
3135 Look for combinations of simple terms on machines with expensive branches
3136 and evaluate the RHS unconditionally.
3138 For example, if we have p->a == 2 && p->b == 4 and we can make an
3139 object large enough to span both A and B, we can do this with a comparison
3140 against the object ANDed with the a mask.
3142 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
3143 operations to do this with one comparison.
3145 We check for both normal comparisons and the BIT_AND_EXPRs made this by
3146 function and the one above.
3148 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
3149 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
3151 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
3154 We return the simplified tree or 0 if no optimization is possible. */
3157 fold_truthop (code, truth_type, lhs, rhs)
3158 enum tree_code code;
3159 tree truth_type, lhs, rhs;
3161 /* If this is the "or" of two comparisons, we can do something if we
3162 the comparisons are NE_EXPR. If this is the "and", we can do something
3163 if the comparisons are EQ_EXPR. I.e.,
3164 (a->b == 2 && a->c == 4) can become (a->new == NEW).
3166 WANTED_CODE is this operation code. For single bit fields, we can
3167 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
3168 comparison for one-bit fields. */
3170 enum tree_code wanted_code;
3171 enum tree_code lcode, rcode;
3172 tree ll_arg, lr_arg, rl_arg, rr_arg;
3173 tree ll_inner, lr_inner, rl_inner, rr_inner;
3174 int ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
3175 int rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
3176 int xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
3177 int lnbitsize, lnbitpos, rnbitsize, rnbitpos;
3178 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
3179 enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
3180 enum machine_mode lnmode, rnmode;
3181 tree ll_mask, lr_mask, rl_mask, rr_mask;
3182 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
3183 tree l_const, r_const;
3185 int first_bit, end_bit;
3188 /* Start by getting the comparison codes. Fail if anything is volatile.
3189 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
3190 it were surrounded with a NE_EXPR. */
3192 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
3195 lcode = TREE_CODE (lhs);
3196 rcode = TREE_CODE (rhs);
3198 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
3199 lcode = NE_EXPR, lhs = build (NE_EXPR, truth_type, lhs, integer_zero_node);
3201 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
3202 rcode = NE_EXPR, rhs = build (NE_EXPR, truth_type, rhs, integer_zero_node);
3204 if (TREE_CODE_CLASS (lcode) != '<' || TREE_CODE_CLASS (rcode) != '<')
3207 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
3208 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
3210 ll_arg = TREE_OPERAND (lhs, 0);
3211 lr_arg = TREE_OPERAND (lhs, 1);
3212 rl_arg = TREE_OPERAND (rhs, 0);
3213 rr_arg = TREE_OPERAND (rhs, 1);
3215 /* If the RHS can be evaluated unconditionally and its operands are
3216 simple, it wins to evaluate the RHS unconditionally on machines
3217 with expensive branches. In this case, this isn't a comparison
3218 that can be merged. */
3220 /* @@ I'm not sure it wins on the m88110 to do this if the comparisons
3221 are with zero (tmw). */
3223 if (BRANCH_COST >= 2
3224 && INTEGRAL_TYPE_P (TREE_TYPE (rhs))
3225 && simple_operand_p (rl_arg)
3226 && simple_operand_p (rr_arg))
3227 return build (code, truth_type, lhs, rhs);
3229 /* See if the comparisons can be merged. Then get all the parameters for
3232 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
3233 || (rcode != EQ_EXPR && rcode != NE_EXPR))
3237 ll_inner = decode_field_reference (ll_arg,
3238 &ll_bitsize, &ll_bitpos, &ll_mode,
3239 &ll_unsignedp, &volatilep, &ll_mask,
3241 lr_inner = decode_field_reference (lr_arg,
3242 &lr_bitsize, &lr_bitpos, &lr_mode,
3243 &lr_unsignedp, &volatilep, &lr_mask,
3245 rl_inner = decode_field_reference (rl_arg,
3246 &rl_bitsize, &rl_bitpos, &rl_mode,
3247 &rl_unsignedp, &volatilep, &rl_mask,
3249 rr_inner = decode_field_reference (rr_arg,
3250 &rr_bitsize, &rr_bitpos, &rr_mode,
3251 &rr_unsignedp, &volatilep, &rr_mask,
3254 /* It must be true that the inner operation on the lhs of each
3255 comparison must be the same if we are to be able to do anything.
3256 Then see if we have constants. If not, the same must be true for
3258 if (volatilep || ll_inner == 0 || rl_inner == 0
3259 || ! operand_equal_p (ll_inner, rl_inner, 0))
3262 if (TREE_CODE (lr_arg) == INTEGER_CST
3263 && TREE_CODE (rr_arg) == INTEGER_CST)
3264 l_const = lr_arg, r_const = rr_arg;
3265 else if (lr_inner == 0 || rr_inner == 0
3266 || ! operand_equal_p (lr_inner, rr_inner, 0))
3269 l_const = r_const = 0;
3271 /* If either comparison code is not correct for our logical operation,
3272 fail. However, we can convert a one-bit comparison against zero into
3273 the opposite comparison against that bit being set in the field. */
3275 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
3276 if (lcode != wanted_code)
3278 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
3284 if (rcode != wanted_code)
3286 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
3292 /* See if we can find a mode that contains both fields being compared on
3293 the left. If we can't, fail. Otherwise, update all constants and masks
3294 to be relative to a field of that size. */
3295 first_bit = MIN (ll_bitpos, rl_bitpos);
3296 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
3297 lnmode = get_best_mode (end_bit - first_bit, first_bit,
3298 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
3300 if (lnmode == VOIDmode)
3303 lnbitsize = GET_MODE_BITSIZE (lnmode);
3304 lnbitpos = first_bit & ~ (lnbitsize - 1);
3305 type = type_for_size (lnbitsize, 1);
3306 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
3308 if (BYTES_BIG_ENDIAN)
3310 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
3311 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
3314 ll_mask = const_binop (LSHIFT_EXPR, convert (type, ll_mask),
3315 size_int (xll_bitpos), 0);
3316 rl_mask = const_binop (LSHIFT_EXPR, convert (type, rl_mask),
3317 size_int (xrl_bitpos), 0);
3321 l_const = convert (type, l_const);
3322 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
3323 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
3324 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
3325 fold (build1 (BIT_NOT_EXPR,
3329 warning ("comparison is always %s",
3330 wanted_code == NE_EXPR ? "one" : "zero");
3332 return convert (truth_type,
3333 wanted_code == NE_EXPR
3334 ? integer_one_node : integer_zero_node);
3339 r_const = convert (type, r_const);
3340 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
3341 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
3342 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
3343 fold (build1 (BIT_NOT_EXPR,
3347 warning ("comparison is always %s",
3348 wanted_code == NE_EXPR ? "one" : "zero");
3350 return convert (truth_type,
3351 wanted_code == NE_EXPR
3352 ? integer_one_node : integer_zero_node);
3356 /* If the right sides are not constant, do the same for it. Also,
3357 disallow this optimization if a size or signedness mismatch occurs
3358 between the left and right sides. */
3361 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
3362 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
3363 /* Make sure the two fields on the right
3364 correspond to the left without being swapped. */
3365 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
3368 first_bit = MIN (lr_bitpos, rr_bitpos);
3369 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
3370 rnmode = get_best_mode (end_bit - first_bit, first_bit,
3371 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
3373 if (rnmode == VOIDmode)
3376 rnbitsize = GET_MODE_BITSIZE (rnmode);
3377 rnbitpos = first_bit & ~ (rnbitsize - 1);
3378 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
3380 if (BYTES_BIG_ENDIAN)
3382 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
3383 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
3386 lr_mask = const_binop (LSHIFT_EXPR, convert (type, lr_mask),
3387 size_int (xlr_bitpos), 0);
3388 rr_mask = const_binop (LSHIFT_EXPR, convert (type, rr_mask),
3389 size_int (xrr_bitpos), 0);
3391 /* Make a mask that corresponds to both fields being compared.
3392 Do this for both items being compared. If the masks agree,
3393 we can do this by masking both and comparing the masked
3395 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
3396 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0);
3397 if (operand_equal_p (ll_mask, lr_mask, 0) && lnbitsize == rnbitsize)
3399 lhs = make_bit_field_ref (ll_inner, type, lnbitsize, lnbitpos,
3400 ll_unsignedp || rl_unsignedp);
3401 rhs = make_bit_field_ref (lr_inner, type, rnbitsize, rnbitpos,
3402 lr_unsignedp || rr_unsignedp);
3403 if (! all_ones_mask_p (ll_mask, lnbitsize))
3405 lhs = build (BIT_AND_EXPR, type, lhs, ll_mask);
3406 rhs = build (BIT_AND_EXPR, type, rhs, ll_mask);
3408 return build (wanted_code, truth_type, lhs, rhs);
3411 /* There is still another way we can do something: If both pairs of
3412 fields being compared are adjacent, we may be able to make a wider
3413 field containing them both. */
3414 if ((ll_bitsize + ll_bitpos == rl_bitpos
3415 && lr_bitsize + lr_bitpos == rr_bitpos)
3416 || (ll_bitpos == rl_bitpos + rl_bitsize
3417 && lr_bitpos == rr_bitpos + rr_bitsize))
3418 return build (wanted_code, truth_type,
3419 make_bit_field_ref (ll_inner, type,
3420 ll_bitsize + rl_bitsize,
3421 MIN (ll_bitpos, rl_bitpos),
3423 make_bit_field_ref (lr_inner, type,
3424 lr_bitsize + rr_bitsize,
3425 MIN (lr_bitpos, rr_bitpos),
3431 /* Handle the case of comparisons with constants. If there is something in
3432 common between the masks, those bits of the constants must be the same.
3433 If not, the condition is always false. Test for this to avoid generating
3434 incorrect code below. */
3435 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
3436 if (! integer_zerop (result)
3437 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
3438 const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
3440 if (wanted_code == NE_EXPR)
3442 warning ("`or' of unmatched not-equal tests is always 1");
3443 return convert (truth_type, integer_one_node);
3447 warning ("`and' of mutually exclusive equal-tests is always zero");
3448 return convert (truth_type, integer_zero_node);
3452 /* Construct the expression we will return. First get the component
3453 reference we will make. Unless the mask is all ones the width of
3454 that field, perform the mask operation. Then compare with the
3456 result = make_bit_field_ref (ll_inner, type, lnbitsize, lnbitpos,
3457 ll_unsignedp || rl_unsignedp);
3459 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
3460 if (! all_ones_mask_p (ll_mask, lnbitsize))
3461 result = build (BIT_AND_EXPR, type, result, ll_mask);
3463 return build (wanted_code, truth_type, result,
3464 const_binop (BIT_IOR_EXPR, l_const, r_const, 0));
3467 /* If T contains a COMPOUND_EXPR which was inserted merely to evaluate
3468 S, a SAVE_EXPR, return the expression actually being evaluated. Note
3469 that we may sometimes modify the tree. */
3472 strip_compound_expr (t, s)
3476 tree type = TREE_TYPE (t);
3477 enum tree_code code = TREE_CODE (t);
3479 /* See if this is the COMPOUND_EXPR we want to eliminate. */
3480 if (code == COMPOUND_EXPR && TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR
3481 && TREE_OPERAND (TREE_OPERAND (t, 0), 0) == s)
3482 return TREE_OPERAND (t, 1);
3484 /* See if this is a COND_EXPR or a simple arithmetic operator. We
3485 don't bother handling any other types. */
3486 else if (code == COND_EXPR)
3488 TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s);
3489 TREE_OPERAND (t, 1) = strip_compound_expr (TREE_OPERAND (t, 1), s);
3490 TREE_OPERAND (t, 2) = strip_compound_expr (TREE_OPERAND (t, 2), s);
3492 else if (TREE_CODE_CLASS (code) == '1')
3493 TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s);
3494 else if (TREE_CODE_CLASS (code) == '<'
3495 || TREE_CODE_CLASS (code) == '2')
3497 TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s);
3498 TREE_OPERAND (t, 1) = strip_compound_expr (TREE_OPERAND (t, 1), s);
3504 /* Perform constant folding and related simplification of EXPR.
3505 The related simplifications include x*1 => x, x*0 => 0, etc.,
3506 and application of the associative law.
3507 NOP_EXPR conversions may be removed freely (as long as we
3508 are careful not to change the C type of the overall expression)
3509 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
3510 but we can constant-fold them if they have constant operands. */
3516 register tree t = expr;
3517 tree t1 = NULL_TREE;
3519 tree type = TREE_TYPE (expr);
3520 register tree arg0, arg1;
3521 register enum tree_code code = TREE_CODE (t);
3525 /* WINS will be nonzero when the switch is done
3526 if all operands are constant. */
3530 /* Don't try to process an RTL_EXPR since its operands aren't trees.
3531 Likewise for a SAVE_EXPR that's already been evaluated. */
3532 if (code == RTL_EXPR || (code == SAVE_EXPR && SAVE_EXPR_RTL (t)) != 0)
3535 /* Return right away if already constant. */
3536 if (TREE_CONSTANT (t))
3538 if (code == CONST_DECL)
3539 return DECL_INITIAL (t);
3543 kind = TREE_CODE_CLASS (code);
3544 if (code == NOP_EXPR || code == FLOAT_EXPR || code == CONVERT_EXPR)
3548 /* Special case for conversion ops that can have fixed point args. */
3549 arg0 = TREE_OPERAND (t, 0);
3551 /* Don't use STRIP_NOPS, because signedness of argument type matters. */
3553 STRIP_TYPE_NOPS (arg0);
3555 if (arg0 != 0 && TREE_CODE (arg0) == COMPLEX_CST)
3556 subop = TREE_REALPART (arg0);
3560 if (subop != 0 && TREE_CODE (subop) != INTEGER_CST
3561 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
3562 && TREE_CODE (subop) != REAL_CST
3563 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
3565 /* Note that TREE_CONSTANT isn't enough:
3566 static var addresses are constant but we can't
3567 do arithmetic on them. */
3570 else if (kind == 'e' || kind == '<'
3571 || kind == '1' || kind == '2' || kind == 'r')
3573 register int len = tree_code_length[(int) code];
3575 for (i = 0; i < len; i++)
3577 tree op = TREE_OPERAND (t, i);
3581 continue; /* Valid for CALL_EXPR, at least. */
3583 if (kind == '<' || code == RSHIFT_EXPR)
3585 /* Signedness matters here. Perhaps we can refine this
3587 STRIP_TYPE_NOPS (op);
3591 /* Strip any conversions that don't change the mode. */
3595 if (TREE_CODE (op) == COMPLEX_CST)
3596 subop = TREE_REALPART (op);
3600 if (TREE_CODE (subop) != INTEGER_CST
3601 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
3602 && TREE_CODE (subop) != REAL_CST
3603 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
3605 /* Note that TREE_CONSTANT isn't enough:
3606 static var addresses are constant but we can't
3607 do arithmetic on them. */
3617 /* If this is a commutative operation, and ARG0 is a constant, move it
3618 to ARG1 to reduce the number of tests below. */
3619 if ((code == PLUS_EXPR || code == MULT_EXPR || code == MIN_EXPR
3620 || code == MAX_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR
3621 || code == BIT_AND_EXPR)
3622 && (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST))
3624 tem = arg0; arg0 = arg1; arg1 = tem;
3626 tem = TREE_OPERAND (t, 0); TREE_OPERAND (t, 0) = TREE_OPERAND (t, 1);
3627 TREE_OPERAND (t, 1) = tem;
3630 /* Now WINS is set as described above,
3631 ARG0 is the first operand of EXPR,
3632 and ARG1 is the second operand (if it has more than one operand).
3634 First check for cases where an arithmetic operation is applied to a
3635 compound, conditional, or comparison operation. Push the arithmetic
3636 operation inside the compound or conditional to see if any folding
3637 can then be done. Convert comparison to conditional for this purpose.
3638 The also optimizes non-constant cases that used to be done in
3641 Before we do that, see if this is a BIT_AND_EXPR or a BIT_OR_EXPR,
3642 one of the operands is a comparison and the other is a comparison, a
3643 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
3644 code below would make the expression more complex. Change it to a
3645 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
3646 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
3648 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
3649 || code == EQ_EXPR || code == NE_EXPR)
3650 && ((truth_value_p (TREE_CODE (arg0))
3651 && (truth_value_p (TREE_CODE (arg1))
3652 || (TREE_CODE (arg1) == BIT_AND_EXPR
3653 && integer_onep (TREE_OPERAND (arg1, 1)))))
3654 || (truth_value_p (TREE_CODE (arg1))
3655 && (truth_value_p (TREE_CODE (arg0))
3656 || (TREE_CODE (arg0) == BIT_AND_EXPR
3657 && integer_onep (TREE_OPERAND (arg0, 1)))))))
3659 t = fold (build (code == BIT_AND_EXPR ? TRUTH_AND_EXPR
3660 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
3664 if (code == EQ_EXPR)
3665 t = invert_truthvalue (t);
3670 if (TREE_CODE_CLASS (code) == '1')
3672 if (TREE_CODE (arg0) == COMPOUND_EXPR)
3673 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
3674 fold (build1 (code, type, TREE_OPERAND (arg0, 1))));
3675 else if (TREE_CODE (arg0) == COND_EXPR)
3677 t = fold (build (COND_EXPR, type, TREE_OPERAND (arg0, 0),
3678 fold (build1 (code, type, TREE_OPERAND (arg0, 1))),
3679 fold (build1 (code, type, TREE_OPERAND (arg0, 2)))));
3681 /* If this was a conversion, and all we did was to move into
3682 inside the COND_EXPR, bring it back out. But leave it if
3683 it is a conversion from integer to integer and the
3684 result precision is no wider than a word since such a
3685 conversion is cheap and may be optimized away by combine,
3686 while it couldn't if it were outside the COND_EXPR. Then return
3687 so we don't get into an infinite recursion loop taking the
3688 conversion out and then back in. */
3690 if ((code == NOP_EXPR || code == CONVERT_EXPR
3691 || code == NON_LVALUE_EXPR)
3692 && TREE_CODE (t) == COND_EXPR
3693 && TREE_CODE (TREE_OPERAND (t, 1)) == code
3694 && TREE_CODE (TREE_OPERAND (t, 2)) == code
3695 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0))
3696 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 2), 0)))
3697 && ! (INTEGRAL_TYPE_P (TREE_TYPE (t))
3698 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0)))
3699 && TYPE_PRECISION (TREE_TYPE (t)) <= BITS_PER_WORD))
3700 t = build1 (code, type,
3702 TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0)),
3703 TREE_OPERAND (t, 0),
3704 TREE_OPERAND (TREE_OPERAND (t, 1), 0),
3705 TREE_OPERAND (TREE_OPERAND (t, 2), 0)));
3708 else if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<')
3709 return fold (build (COND_EXPR, type, arg0,
3710 fold (build1 (code, type, integer_one_node)),
3711 fold (build1 (code, type, integer_zero_node))));
3713 else if (TREE_CODE_CLASS (code) == '2'
3714 || TREE_CODE_CLASS (code) == '<')
3716 if (TREE_CODE (arg1) == COMPOUND_EXPR)
3717 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
3718 fold (build (code, type,
3719 arg0, TREE_OPERAND (arg1, 1))));
3720 else if (TREE_CODE (arg1) == COND_EXPR
3721 || TREE_CODE_CLASS (TREE_CODE (arg1)) == '<')
3723 tree test, true_value, false_value;
3725 if (TREE_CODE (arg1) == COND_EXPR)
3727 test = TREE_OPERAND (arg1, 0);
3728 true_value = TREE_OPERAND (arg1, 1);
3729 false_value = TREE_OPERAND (arg1, 2);
3733 tree testtype = TREE_TYPE (arg1);
3735 true_value = convert (testtype, integer_one_node);
3736 false_value = convert (testtype, integer_zero_node);
3739 /* If ARG0 is complex we want to make sure we only evaluate
3740 it once. Though this is only required if it is volatile, it
3741 might be more efficient even if it is not. However, if we
3742 succeed in folding one part to a constant, we do not need
3743 to make this SAVE_EXPR. Since we do this optimization
3744 primarily to see if we do end up with constant and this
3745 SAVE_EXPR interferes with later optimizations, suppressing
3746 it when we can is important. */
3748 if (TREE_CODE (arg0) != SAVE_EXPR
3749 && ((TREE_CODE (arg0) != VAR_DECL
3750 && TREE_CODE (arg0) != PARM_DECL)
3751 || TREE_SIDE_EFFECTS (arg0)))
3753 tree lhs = fold (build (code, type, arg0, true_value));
3754 tree rhs = fold (build (code, type, arg0, false_value));
3756 if (TREE_CONSTANT (lhs) || TREE_CONSTANT (rhs))
3757 return fold (build (COND_EXPR, type, test, lhs, rhs));
3759 arg0 = save_expr (arg0);
3762 test = fold (build (COND_EXPR, type, test,
3763 fold (build (code, type, arg0, true_value)),
3764 fold (build (code, type, arg0, false_value))));
3765 if (TREE_CODE (arg0) == SAVE_EXPR)
3766 return build (COMPOUND_EXPR, type,
3767 convert (void_type_node, arg0),
3768 strip_compound_expr (test, arg0));
3770 return convert (type, test);
3773 else if (TREE_CODE (arg0) == COMPOUND_EXPR)
3774 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
3775 fold (build (code, type, TREE_OPERAND (arg0, 1), arg1)));
3776 else if (TREE_CODE (arg0) == COND_EXPR
3777 || TREE_CODE_CLASS (TREE_CODE (arg0)) == '<')
3779 tree test, true_value, false_value;
3781 if (TREE_CODE (arg0) == COND_EXPR)
3783 test = TREE_OPERAND (arg0, 0);
3784 true_value = TREE_OPERAND (arg0, 1);
3785 false_value = TREE_OPERAND (arg0, 2);
3789 tree testtype = TREE_TYPE (arg0);
3791 true_value = convert (testtype, integer_one_node);
3792 false_value = convert (testtype, integer_zero_node);
3795 if (TREE_CODE (arg1) != SAVE_EXPR
3796 && ((TREE_CODE (arg1) != VAR_DECL
3797 && TREE_CODE (arg1) != PARM_DECL)
3798 || TREE_SIDE_EFFECTS (arg1)))
3800 tree lhs = fold (build (code, type, true_value, arg1));
3801 tree rhs = fold (build (code, type, false_value, arg1));
3803 if (TREE_CONSTANT (lhs) || TREE_CONSTANT (rhs)
3804 || TREE_CONSTANT (arg1))
3805 return fold (build (COND_EXPR, type, test, lhs, rhs));
3807 arg1 = save_expr (arg1);
3810 test = fold (build (COND_EXPR, type, test,
3811 fold (build (code, type, true_value, arg1)),
3812 fold (build (code, type, false_value, arg1))));
3813 if (TREE_CODE (arg1) == SAVE_EXPR)
3814 return build (COMPOUND_EXPR, type,
3815 convert (void_type_node, arg1),
3816 strip_compound_expr (test, arg1));
3818 return convert (type, test);
3821 else if (TREE_CODE_CLASS (code) == '<'
3822 && TREE_CODE (arg0) == COMPOUND_EXPR)
3823 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
3824 fold (build (code, type, TREE_OPERAND (arg0, 1), arg1)));
3825 else if (TREE_CODE_CLASS (code) == '<'
3826 && TREE_CODE (arg1) == COMPOUND_EXPR)
3827 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
3828 fold (build (code, type, arg0, TREE_OPERAND (arg1, 1))));
3840 return fold (DECL_INITIAL (t));
3845 case FIX_TRUNC_EXPR:
3846 /* Other kinds of FIX are not handled properly by fold_convert. */
3848 if (TREE_TYPE (TREE_OPERAND (t, 0)) == TREE_TYPE (t))
3849 return TREE_OPERAND (t, 0);
3851 /* Handle cases of two conversions in a row. */
3852 if (TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
3853 || TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR)
3855 tree inside_type = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
3856 tree inter_type = TREE_TYPE (TREE_OPERAND (t, 0));
3857 tree final_type = TREE_TYPE (t);
3858 int inside_int = INTEGRAL_TYPE_P (inside_type);
3859 int inside_ptr = POINTER_TYPE_P (inside_type);
3860 int inside_float = FLOAT_TYPE_P (inside_type);
3861 int inside_prec = TYPE_PRECISION (inside_type);
3862 int inside_unsignedp = TREE_UNSIGNED (inside_type);
3863 int inter_int = INTEGRAL_TYPE_P (inter_type);
3864 int inter_ptr = POINTER_TYPE_P (inter_type);
3865 int inter_float = FLOAT_TYPE_P (inter_type);
3866 int inter_prec = TYPE_PRECISION (inter_type);
3867 int inter_unsignedp = TREE_UNSIGNED (inter_type);
3868 int final_int = INTEGRAL_TYPE_P (final_type);
3869 int final_ptr = POINTER_TYPE_P (final_type);
3870 int final_float = FLOAT_TYPE_P (final_type);
3871 int final_prec = TYPE_PRECISION (final_type);
3872 int final_unsignedp = TREE_UNSIGNED (final_type);
3874 /* In addition to the cases of two conversions in a row
3875 handled below, if we are converting something to its own
3876 type via an object of identical or wider precision, neither
3877 conversion is needed. */
3878 if (inside_type == final_type
3879 && ((inter_int && final_int) || (inter_float && final_float))
3880 && inter_prec >= final_prec)
3881 return TREE_OPERAND (TREE_OPERAND (t, 0), 0);
3883 /* Likewise, if the intermediate and final types are either both
3884 float or both integer, we don't need the middle conversion if
3885 it is wider than the final type and doesn't change the signedness
3886 (for integers). Avoid this if the final type is a pointer
3887 since then we sometimes need the inner conversion. Likewise if
3888 the outer has a precision not equal to the size of its mode. */
3889 if ((((inter_int || inter_ptr) && (inside_int || inside_ptr))
3890 || (inter_float && inside_float))
3891 && inter_prec >= inside_prec
3892 && (inter_float || inter_unsignedp == inside_unsignedp)
3893 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (final_type))
3894 && TYPE_MODE (final_type) == TYPE_MODE (inter_type))
3896 return convert (final_type, TREE_OPERAND (TREE_OPERAND (t, 0), 0));
3898 /* Two conversions in a row are not needed unless:
3899 - some conversion is floating-point (overstrict for now), or
3900 - the intermediate type is narrower than both initial and
3902 - the intermediate type and innermost type differ in signedness,
3903 and the outermost type is wider than the intermediate, or
3904 - the initial type is a pointer type and the precisions of the
3905 intermediate and final types differ, or
3906 - the final type is a pointer type and the precisions of the
3907 initial and intermediate types differ. */
3908 if (! inside_float && ! inter_float && ! final_float
3909 && (inter_prec > inside_prec || inter_prec > final_prec)
3910 && ! (inside_int && inter_int
3911 && inter_unsignedp != inside_unsignedp
3912 && inter_prec < final_prec)
3913 && ((inter_unsignedp && inter_prec > inside_prec)
3914 == (final_unsignedp && final_prec > inter_prec))
3915 && ! (inside_ptr && inter_prec != final_prec)
3916 && ! (final_ptr && inside_prec != inter_prec)
3917 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (final_type))
3918 && TYPE_MODE (final_type) == TYPE_MODE (inter_type))
3920 return convert (final_type, TREE_OPERAND (TREE_OPERAND (t, 0), 0));
3923 if (TREE_CODE (TREE_OPERAND (t, 0)) == MODIFY_EXPR
3924 && TREE_CONSTANT (TREE_OPERAND (TREE_OPERAND (t, 0), 1))
3925 /* Detect assigning a bitfield. */
3926 && !(TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) == COMPONENT_REF
3927 && DECL_BIT_FIELD (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 1))))
3929 /* Don't leave an assignment inside a conversion
3930 unless assigning a bitfield. */
3931 tree prev = TREE_OPERAND (t, 0);
3932 TREE_OPERAND (t, 0) = TREE_OPERAND (prev, 1);
3933 /* First do the assignment, then return converted constant. */
3934 t = build (COMPOUND_EXPR, TREE_TYPE (t), prev, fold (t));
3940 TREE_CONSTANT (t) = TREE_CONSTANT (arg0);
3943 return fold_convert (t, arg0);
3945 #if 0 /* This loses on &"foo"[0]. */
3950 /* Fold an expression like: "foo"[2] */
3951 if (TREE_CODE (arg0) == STRING_CST
3952 && TREE_CODE (arg1) == INTEGER_CST
3953 && !TREE_INT_CST_HIGH (arg1)
3954 && (i = TREE_INT_CST_LOW (arg1)) < TREE_STRING_LENGTH (arg0))
3956 t = build_int_2 (TREE_STRING_POINTER (arg0)[i], 0);
3957 TREE_TYPE (t) = TREE_TYPE (TREE_TYPE (arg0));
3958 force_fit_type (t, 0);
3965 if (TREE_CODE (arg0) == CONSTRUCTOR)
3967 tree m = purpose_member (arg1, CONSTRUCTOR_ELTS (arg0));
3974 TREE_CONSTANT (t) = wins;
3980 if (TREE_CODE (arg0) == INTEGER_CST)
3982 HOST_WIDE_INT low, high;
3983 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
3984 TREE_INT_CST_HIGH (arg0),
3986 t = build_int_2 (low, high);
3987 TREE_TYPE (t) = type;
3989 = (TREE_OVERFLOW (arg0)
3990 | force_fit_type (t, overflow && !TREE_UNSIGNED (type)));
3991 TREE_CONSTANT_OVERFLOW (t)
3992 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg0);
3994 else if (TREE_CODE (arg0) == REAL_CST)
3995 t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
3996 TREE_TYPE (t) = type;
3998 else if (TREE_CODE (arg0) == NEGATE_EXPR)
3999 return TREE_OPERAND (arg0, 0);
4001 /* Convert - (a - b) to (b - a) for non-floating-point. */
4002 else if (TREE_CODE (arg0) == MINUS_EXPR && ! FLOAT_TYPE_P (type))
4003 return build (MINUS_EXPR, type, TREE_OPERAND (arg0, 1),
4004 TREE_OPERAND (arg0, 0));
4011 if (TREE_CODE (arg0) == INTEGER_CST)
4013 if (! TREE_UNSIGNED (type)
4014 && TREE_INT_CST_HIGH (arg0) < 0)
4016 HOST_WIDE_INT low, high;
4017 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
4018 TREE_INT_CST_HIGH (arg0),
4020 t = build_int_2 (low, high);
4021 TREE_TYPE (t) = type;
4023 = (TREE_OVERFLOW (arg0)
4024 | force_fit_type (t, overflow));
4025 TREE_CONSTANT_OVERFLOW (t)
4026 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg0);
4029 else if (TREE_CODE (arg0) == REAL_CST)
4031 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
4032 t = build_real (type,
4033 REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
4035 TREE_TYPE (t) = type;
4037 else if (TREE_CODE (arg0) == ABS_EXPR || TREE_CODE (arg0) == NEGATE_EXPR)
4038 return build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0));
4042 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
4044 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
4045 return build (COMPLEX_EXPR, TREE_TYPE (arg0),
4046 TREE_OPERAND (arg0, 0),
4047 fold (build1 (NEGATE_EXPR,
4048 TREE_TYPE (TREE_TYPE (arg0)),
4049 TREE_OPERAND (arg0, 1))));
4050 else if (TREE_CODE (arg0) == COMPLEX_CST)
4051 return build_complex (type, TREE_OPERAND (arg0, 0),
4052 fold (build1 (NEGATE_EXPR,
4053 TREE_TYPE (TREE_TYPE (arg0)),
4054 TREE_OPERAND (arg0, 1))));
4055 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
4056 return fold (build (TREE_CODE (arg0), type,
4057 fold (build1 (CONJ_EXPR, type,
4058 TREE_OPERAND (arg0, 0))),
4059 fold (build1 (CONJ_EXPR,
4060 type, TREE_OPERAND (arg0, 1)))));
4061 else if (TREE_CODE (arg0) == CONJ_EXPR)
4062 return TREE_OPERAND (arg0, 0);
4068 if (TREE_CODE (arg0) == INTEGER_CST)
4069 t = build_int_2 (~ TREE_INT_CST_LOW (arg0),
4070 ~ TREE_INT_CST_HIGH (arg0));
4071 TREE_TYPE (t) = type;
4072 force_fit_type (t, 0);
4073 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg0);
4074 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (arg0);
4076 else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
4077 return TREE_OPERAND (arg0, 0);
4081 /* A + (-B) -> A - B */
4082 if (TREE_CODE (arg1) == NEGATE_EXPR)
4083 return fold (build (MINUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
4084 else if (! FLOAT_TYPE_P (type))
4086 if (integer_zerop (arg1))
4087 return non_lvalue (convert (type, arg0));
4089 /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
4090 with a constant, and the two constants have no bits in common,
4091 we should treat this as a BIT_IOR_EXPR since this may produce more
4093 if (TREE_CODE (arg0) == BIT_AND_EXPR
4094 && TREE_CODE (arg1) == BIT_AND_EXPR
4095 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
4096 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
4097 && integer_zerop (const_binop (BIT_AND_EXPR,
4098 TREE_OPERAND (arg0, 1),
4099 TREE_OPERAND (arg1, 1), 0)))
4101 code = BIT_IOR_EXPR;
4105 /* (A * C) + (B * C) -> (A+B) * C. Since we are most concerned
4106 about the case where C is a constant, just try one of the
4107 four possibilities. */
4109 if (TREE_CODE (arg0) == MULT_EXPR && TREE_CODE (arg1) == MULT_EXPR
4110 && operand_equal_p (TREE_OPERAND (arg0, 1),
4111 TREE_OPERAND (arg1, 1), 0))
4112 return fold (build (MULT_EXPR, type,
4113 fold (build (PLUS_EXPR, type,
4114 TREE_OPERAND (arg0, 0),
4115 TREE_OPERAND (arg1, 0))),
4116 TREE_OPERAND (arg0, 1)));
4118 /* In IEEE floating point, x+0 may not equal x. */
4119 else if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
4121 && real_zerop (arg1))
4122 return non_lvalue (convert (type, arg0));
4124 /* In most languages, can't associate operations on floats
4125 through parentheses. Rather than remember where the parentheses
4126 were, we don't associate floats at all. It shouldn't matter much.
4127 However, associating multiplications is only very slightly
4128 inaccurate, so do that if -ffast-math is specified. */
4129 if (FLOAT_TYPE_P (type)
4130 && ! (flag_fast_math && code == MULT_EXPR))
4133 /* The varsign == -1 cases happen only for addition and subtraction.
4134 It says that the arg that was split was really CON minus VAR.
4135 The rest of the code applies to all associative operations. */
4141 if (split_tree (arg0, code, &var, &con, &varsign))
4145 /* EXPR is (CON-VAR) +- ARG1. */
4146 /* If it is + and VAR==ARG1, return just CONST. */
4147 if (code == PLUS_EXPR && operand_equal_p (var, arg1, 0))
4148 return convert (TREE_TYPE (t), con);
4150 /* If ARG0 is a constant, don't change things around;
4151 instead keep all the constant computations together. */
4153 if (TREE_CONSTANT (arg0))
4156 /* Otherwise return (CON +- ARG1) - VAR. */
4157 t = build (MINUS_EXPR, type,
4158 fold (build (code, type, con, arg1)), var);
4162 /* EXPR is (VAR+CON) +- ARG1. */
4163 /* If it is - and VAR==ARG1, return just CONST. */
4164 if (code == MINUS_EXPR && operand_equal_p (var, arg1, 0))
4165 return convert (TREE_TYPE (t), con);
4167 /* If ARG0 is a constant, don't change things around;
4168 instead keep all the constant computations together. */
4170 if (TREE_CONSTANT (arg0))
4173 /* Otherwise return VAR +- (ARG1 +- CON). */
4174 tem = fold (build (code, type, arg1, con));
4175 t = build (code, type, var, tem);
4177 if (integer_zerop (tem)
4178 && (code == PLUS_EXPR || code == MINUS_EXPR))
4179 return convert (type, var);
4180 /* If we have x +/- (c - d) [c an explicit integer]
4181 change it to x -/+ (d - c) since if d is relocatable
4182 then the latter can be a single immediate insn
4183 and the former cannot. */
4184 if (TREE_CODE (tem) == MINUS_EXPR
4185 && TREE_CODE (TREE_OPERAND (tem, 0)) == INTEGER_CST)
4187 tree tem1 = TREE_OPERAND (tem, 1);
4188 TREE_OPERAND (tem, 1) = TREE_OPERAND (tem, 0);
4189 TREE_OPERAND (tem, 0) = tem1;
4191 (code == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR));
4197 if (split_tree (arg1, code, &var, &con, &varsign))
4199 if (TREE_CONSTANT (arg1))
4204 (code == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR));
4206 /* EXPR is ARG0 +- (CON +- VAR). */
4207 if (TREE_CODE (t) == MINUS_EXPR
4208 && operand_equal_p (var, arg0, 0))
4210 /* If VAR and ARG0 cancel, return just CON or -CON. */
4211 if (code == PLUS_EXPR)
4212 return convert (TREE_TYPE (t), con);
4213 return fold (build1 (NEGATE_EXPR, TREE_TYPE (t),
4214 convert (TREE_TYPE (t), con)));
4217 t = build (TREE_CODE (t), type,
4218 fold (build (code, TREE_TYPE (t), arg0, con)), var);
4220 if (integer_zerop (TREE_OPERAND (t, 0))
4221 && TREE_CODE (t) == PLUS_EXPR)
4222 return convert (TREE_TYPE (t), var);
4227 #if defined (REAL_IS_NOT_DOUBLE) && ! defined (REAL_ARITHMETIC)
4228 if (TREE_CODE (arg1) == REAL_CST)
4230 #endif /* REAL_IS_NOT_DOUBLE, and no REAL_ARITHMETIC */
4232 t1 = const_binop (code, arg0, arg1, 0);
4233 if (t1 != NULL_TREE)
4235 /* The return value should always have
4236 the same type as the original expression. */
4237 TREE_TYPE (t1) = TREE_TYPE (t);
4243 if (! FLOAT_TYPE_P (type))
4245 if (! wins && integer_zerop (arg0))
4246 return build1 (NEGATE_EXPR, type, arg1);
4247 if (integer_zerop (arg1))
4248 return non_lvalue (convert (type, arg0));
4250 /* (A * C) - (B * C) -> (A-B) * C. Since we are most concerned
4251 about the case where C is a constant, just try one of the
4252 four possibilities. */
4254 if (TREE_CODE (arg0) == MULT_EXPR && TREE_CODE (arg1) == MULT_EXPR
4255 && operand_equal_p (TREE_OPERAND (arg0, 1),
4256 TREE_OPERAND (arg1, 1), 0))
4257 return fold (build (MULT_EXPR, type,
4258 fold (build (MINUS_EXPR, type,
4259 TREE_OPERAND (arg0, 0),
4260 TREE_OPERAND (arg1, 0))),
4261 TREE_OPERAND (arg0, 1)));
4263 /* Convert A - (-B) to A + B. */
4264 else if (TREE_CODE (arg1) == NEGATE_EXPR)
4265 return fold (build (PLUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
4267 else if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
4270 /* Except with IEEE floating point, 0-x equals -x. */
4271 if (! wins && real_zerop (arg0))
4272 return build1 (NEGATE_EXPR, type, arg1);
4273 /* Except with IEEE floating point, x-0 equals x. */
4274 if (real_zerop (arg1))
4275 return non_lvalue (convert (type, arg0));
4278 /* Fold &x - &x. This can happen from &x.foo - &x.
4279 This is unsafe for certain floats even in non-IEEE formats.
4280 In IEEE, it is unsafe because it does wrong for NaNs.
4281 Also note that operand_equal_p is always false if an operand
4284 if ((! FLOAT_TYPE_P (type) || flag_fast_math)
4285 && operand_equal_p (arg0, arg1, 0))
4286 return convert (type, integer_zero_node);
4291 if (! FLOAT_TYPE_P (type))
4293 if (integer_zerop (arg1))
4294 return omit_one_operand (type, arg1, arg0);
4295 if (integer_onep (arg1))
4296 return non_lvalue (convert (type, arg0));
4298 /* ((A / C) * C) is A if the division is an
4299 EXACT_DIV_EXPR. Since C is normally a constant,
4300 just check for one of the four possibilities. */
4302 if (TREE_CODE (arg0) == EXACT_DIV_EXPR
4303 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
4304 return TREE_OPERAND (arg0, 0);
4306 /* (a * (1 << b)) is (a << b) */
4307 if (TREE_CODE (arg1) == LSHIFT_EXPR
4308 && integer_onep (TREE_OPERAND (arg1, 0)))
4309 return fold (build (LSHIFT_EXPR, type, arg0,
4310 TREE_OPERAND (arg1, 1)));
4311 if (TREE_CODE (arg0) == LSHIFT_EXPR
4312 && integer_onep (TREE_OPERAND (arg0, 0)))
4313 return fold (build (LSHIFT_EXPR, type, arg1,
4314 TREE_OPERAND (arg0, 1)));
4318 /* x*0 is 0, except for IEEE floating point. */
4319 if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
4321 && real_zerop (arg1))
4322 return omit_one_operand (type, arg1, arg0);
4323 /* In IEEE floating point, x*1 is not equivalent to x for snans.
4324 However, ANSI says we can drop signals,
4325 so we can do this anyway. */
4326 if (real_onep (arg1))
4327 return non_lvalue (convert (type, arg0));
4329 if (! wins && real_twop (arg1))
4331 tree arg = save_expr (arg0);
4332 return build (PLUS_EXPR, type, arg, arg);
4340 register enum tree_code code0, code1;
4342 if (integer_all_onesp (arg1))
4343 return omit_one_operand (type, arg1, arg0);
4344 if (integer_zerop (arg1))
4345 return non_lvalue (convert (type, arg0));
4346 t1 = distribute_bit_expr (code, type, arg0, arg1);
4347 if (t1 != NULL_TREE)
4350 /* (A << C1) | (A >> C2) if A is unsigned and C1+C2 is the size of A
4351 is a rotate of A by C1 bits. */
4352 /* (A << B) | (A >> (Z - B)) if A is unsigned and Z is the size of A
4353 is a rotate of A by B bits. */
4355 code0 = TREE_CODE (arg0);
4356 code1 = TREE_CODE (arg1);
4357 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
4358 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
4359 && operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1,0), 0)
4360 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
4362 register tree tree01, tree11;
4363 register enum tree_code code01, code11;
4365 tree01 = TREE_OPERAND (arg0, 1);
4366 tree11 = TREE_OPERAND (arg1, 1);
4367 code01 = TREE_CODE (tree01);
4368 code11 = TREE_CODE (tree11);
4369 if (code01 == INTEGER_CST
4370 && code11 == INTEGER_CST
4371 && TREE_INT_CST_HIGH (tree01) == 0
4372 && TREE_INT_CST_HIGH (tree11) == 0
4373 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
4374 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
4375 return build (LROTATE_EXPR, type, TREE_OPERAND (arg0, 0),
4376 code0 == LSHIFT_EXPR ? tree01 : tree11);
4377 else if (code11 == MINUS_EXPR
4378 && TREE_CODE (TREE_OPERAND (tree11, 0)) == INTEGER_CST
4379 && TREE_INT_CST_HIGH (TREE_OPERAND (tree11, 0)) == 0
4380 && TREE_INT_CST_LOW (TREE_OPERAND (tree11, 0))
4381 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))
4382 && operand_equal_p (tree01, TREE_OPERAND (tree11, 1), 0))
4383 return build (code0 == LSHIFT_EXPR ? LROTATE_EXPR : RROTATE_EXPR,
4384 type, TREE_OPERAND (arg0, 0), tree01);
4385 else if (code01 == MINUS_EXPR
4386 && TREE_CODE (TREE_OPERAND (tree01, 0)) == INTEGER_CST
4387 && TREE_INT_CST_HIGH (TREE_OPERAND (tree01, 0)) == 0
4388 && TREE_INT_CST_LOW (TREE_OPERAND (tree01, 0))
4389 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))
4390 && operand_equal_p (tree11, TREE_OPERAND (tree01, 1), 0))
4391 return build (code0 != LSHIFT_EXPR ? LROTATE_EXPR : RROTATE_EXPR,
4392 type, TREE_OPERAND (arg0, 0), tree11);
4399 if (integer_zerop (arg1))
4400 return non_lvalue (convert (type, arg0));
4401 if (integer_all_onesp (arg1))
4402 return fold (build1 (BIT_NOT_EXPR, type, arg0));
4407 if (integer_all_onesp (arg1))
4408 return non_lvalue (convert (type, arg0));
4409 if (integer_zerop (arg1))
4410 return omit_one_operand (type, arg1, arg0);
4411 t1 = distribute_bit_expr (code, type, arg0, arg1);
4412 if (t1 != NULL_TREE)
4414 /* Simplify ((int)c & 0x377) into (int)c, if c is unsigned char. */
4415 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == NOP_EXPR
4416 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0))))
4418 int prec = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)));
4419 if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
4420 && (~TREE_INT_CST_LOW (arg0)
4421 & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
4422 return build1 (NOP_EXPR, type, TREE_OPERAND (arg1, 0));
4424 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
4425 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
4427 int prec = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
4428 if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
4429 && (~TREE_INT_CST_LOW (arg1)
4430 & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
4431 return build1 (NOP_EXPR, type, TREE_OPERAND (arg0, 0));
4435 case BIT_ANDTC_EXPR:
4436 if (integer_all_onesp (arg0))
4437 return non_lvalue (convert (type, arg1));
4438 if (integer_zerop (arg0))
4439 return omit_one_operand (type, arg0, arg1);
4440 if (TREE_CODE (arg1) == INTEGER_CST)
4442 arg1 = fold (build1 (BIT_NOT_EXPR, type, arg1));
4443 code = BIT_AND_EXPR;
4449 /* In most cases, do nothing with a divide by zero. */
4450 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
4451 #ifndef REAL_INFINITY
4452 if (TREE_CODE (arg1) == REAL_CST && real_zerop (arg1))
4455 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
4457 /* In IEEE floating point, x/1 is not equivalent to x for snans.
4458 However, ANSI says we can drop signals, so we can do this anyway. */
4459 if (real_onep (arg1))
4460 return non_lvalue (convert (type, arg0));
4462 /* If ARG1 is a constant, we can convert this to a multiply by the
4463 reciprocal. This does not have the same rounding properties,
4464 so only do this if -ffast-math. We can actually always safely
4465 do it if ARG1 is a power of two, but it's hard to tell if it is
4466 or not in a portable manner. */
4467 if (TREE_CODE (arg1) == REAL_CST)
4470 && 0 != (tem = const_binop (code, build_real (type, dconst1),
4472 return fold (build (MULT_EXPR, type, arg0, tem));
4473 /* Find the reciprocal if optimizing and the result is exact. */
4477 r = TREE_REAL_CST (arg1);
4478 if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
4480 tem = build_real (type, r);
4481 return fold (build (MULT_EXPR, type, arg0, tem));
4487 case TRUNC_DIV_EXPR:
4488 case ROUND_DIV_EXPR:
4489 case FLOOR_DIV_EXPR:
4491 case EXACT_DIV_EXPR:
4492 if (integer_onep (arg1))
4493 return non_lvalue (convert (type, arg0));
4494 if (integer_zerop (arg1))
4497 /* If we have ((a / C1) / C2) where both division are the same type, try
4498 to simplify. First see if C1 * C2 overflows or not. */
4499 if (TREE_CODE (arg0) == code && TREE_CODE (arg1) == INTEGER_CST
4500 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
4504 new_divisor = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 1), arg1, 0);
4505 tem = const_binop (FLOOR_DIV_EXPR, new_divisor, arg1, 0);
4507 if (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) == TREE_INT_CST_LOW (tem)
4508 && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == TREE_INT_CST_HIGH (tem))
4510 /* If no overflow, divide by C1*C2. */
4511 return fold (build (code, type, TREE_OPERAND (arg0, 0), new_divisor));
4515 /* Look for ((a * C1) / C3) or (((a * C1) + C2) / C3),
4516 where C1 % C3 == 0 or C3 % C1 == 0. We can simplify these
4517 expressions, which often appear in the offsets or sizes of
4518 objects with a varying size. Only deal with positive divisors
4519 and multiplicands. If C2 is negative, we must have C2 % C3 == 0.
4521 Look for NOPs and SAVE_EXPRs inside. */
4523 if (TREE_CODE (arg1) == INTEGER_CST
4524 && tree_int_cst_sgn (arg1) >= 0)
4526 int have_save_expr = 0;
4527 tree c2 = integer_zero_node;
4530 if (TREE_CODE (xarg0) == SAVE_EXPR && SAVE_EXPR_RTL (xarg0) == 0)
4531 have_save_expr = 1, xarg0 = TREE_OPERAND (xarg0, 0);
4535 if (TREE_CODE (xarg0) == PLUS_EXPR
4536 && TREE_CODE (TREE_OPERAND (xarg0, 1)) == INTEGER_CST)
4537 c2 = TREE_OPERAND (xarg0, 1), xarg0 = TREE_OPERAND (xarg0, 0);
4538 else if (TREE_CODE (xarg0) == MINUS_EXPR
4539 && TREE_CODE (TREE_OPERAND (xarg0, 1)) == INTEGER_CST
4540 /* If we are doing this computation unsigned, the negate
4542 && ! TREE_UNSIGNED (type))
4544 c2 = fold (build1 (NEGATE_EXPR, type, TREE_OPERAND (xarg0, 1)));
4545 xarg0 = TREE_OPERAND (xarg0, 0);
4548 if (TREE_CODE (xarg0) == SAVE_EXPR && SAVE_EXPR_RTL (xarg0) == 0)
4549 have_save_expr = 1, xarg0 = TREE_OPERAND (xarg0, 0);
4553 if (TREE_CODE (xarg0) == MULT_EXPR
4554 && TREE_CODE (TREE_OPERAND (xarg0, 1)) == INTEGER_CST
4555 && tree_int_cst_sgn (TREE_OPERAND (xarg0, 1)) >= 0
4556 && (integer_zerop (const_binop (TRUNC_MOD_EXPR,
4557 TREE_OPERAND (xarg0, 1), arg1, 1))
4558 || integer_zerop (const_binop (TRUNC_MOD_EXPR, arg1,
4559 TREE_OPERAND (xarg0, 1), 1)))
4560 && (tree_int_cst_sgn (c2) >= 0
4561 || integer_zerop (const_binop (TRUNC_MOD_EXPR, c2,
4564 tree outer_div = integer_one_node;
4565 tree c1 = TREE_OPERAND (xarg0, 1);
4568 /* If C3 > C1, set them equal and do a divide by
4569 C3/C1 at the end of the operation. */
4570 if (tree_int_cst_lt (c1, c3))
4571 outer_div = const_binop (code, c3, c1, 0), c3 = c1;
4573 /* The result is A * (C1/C3) + (C2/C3). */
4574 t = fold (build (PLUS_EXPR, type,
4575 fold (build (MULT_EXPR, type,
4576 TREE_OPERAND (xarg0, 0),
4577 const_binop (code, c1, c3, 1))),
4578 const_binop (code, c2, c3, 1)));
4580 if (! integer_onep (outer_div))
4581 t = fold (build (code, type, t, convert (type, outer_div)));
4593 case FLOOR_MOD_EXPR:
4594 case ROUND_MOD_EXPR:
4595 case TRUNC_MOD_EXPR:
4596 if (integer_onep (arg1))
4597 return omit_one_operand (type, integer_zero_node, arg0);
4598 if (integer_zerop (arg1))
4601 /* Look for ((a * C1) % C3) or (((a * C1) + C2) % C3),
4602 where C1 % C3 == 0. Handle similarly to the division case,
4603 but don't bother with SAVE_EXPRs. */
4605 if (TREE_CODE (arg1) == INTEGER_CST
4606 && ! integer_zerop (arg1))
4608 tree c2 = integer_zero_node;
4611 if (TREE_CODE (xarg0) == PLUS_EXPR
4612 && TREE_CODE (TREE_OPERAND (xarg0, 1)) == INTEGER_CST)
4613 c2 = TREE_OPERAND (xarg0, 1), xarg0 = TREE_OPERAND (xarg0, 0);
4614 else if (TREE_CODE (xarg0) == MINUS_EXPR
4615 && TREE_CODE (TREE_OPERAND (xarg0, 1)) == INTEGER_CST
4616 && ! TREE_UNSIGNED (type))
4618 c2 = fold (build1 (NEGATE_EXPR, type, TREE_OPERAND (xarg0, 1)));
4619 xarg0 = TREE_OPERAND (xarg0, 0);
4624 if (TREE_CODE (xarg0) == MULT_EXPR
4625 && TREE_CODE (TREE_OPERAND (xarg0, 1)) == INTEGER_CST
4626 && integer_zerop (const_binop (TRUNC_MOD_EXPR,
4627 TREE_OPERAND (xarg0, 1),
4629 && tree_int_cst_sgn (c2) >= 0)
4630 /* The result is (C2%C3). */
4631 return omit_one_operand (type, const_binop (code, c2, arg1, 1),
4632 TREE_OPERAND (xarg0, 0));
4641 if (integer_zerop (arg1))
4642 return non_lvalue (convert (type, arg0));
4643 /* Since negative shift count is not well-defined,
4644 don't try to compute it in the compiler. */
4645 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
4647 /* Rewrite an LROTATE_EXPR by a constant into an
4648 RROTATE_EXPR by a new constant. */
4649 if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
4651 TREE_SET_CODE (t, RROTATE_EXPR);
4652 code = RROTATE_EXPR;
4653 TREE_OPERAND (t, 1) = arg1
4656 convert (TREE_TYPE (arg1),
4657 build_int_2 (GET_MODE_BITSIZE (TYPE_MODE (type)), 0)),
4659 if (tree_int_cst_sgn (arg1) < 0)
4663 /* If we have a rotate of a bit operation with the rotate count and
4664 the second operand of the bit operation both constant,
4665 permute the two operations. */
4666 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
4667 && (TREE_CODE (arg0) == BIT_AND_EXPR
4668 || TREE_CODE (arg0) == BIT_ANDTC_EXPR
4669 || TREE_CODE (arg0) == BIT_IOR_EXPR
4670 || TREE_CODE (arg0) == BIT_XOR_EXPR)
4671 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
4672 return fold (build (TREE_CODE (arg0), type,
4673 fold (build (code, type,
4674 TREE_OPERAND (arg0, 0), arg1)),
4675 fold (build (code, type,
4676 TREE_OPERAND (arg0, 1), arg1))));
4678 /* Two consecutive rotates adding up to the width of the mode can
4680 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
4681 && TREE_CODE (arg0) == RROTATE_EXPR
4682 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
4683 && TREE_INT_CST_HIGH (arg1) == 0
4684 && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
4685 && ((TREE_INT_CST_LOW (arg1)
4686 + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
4687 == GET_MODE_BITSIZE (TYPE_MODE (type))))
4688 return TREE_OPERAND (arg0, 0);
4693 if (operand_equal_p (arg0, arg1, 0))
4695 if (INTEGRAL_TYPE_P (type)
4696 && operand_equal_p (arg1, TYPE_MIN_VALUE (type), 1))
4697 return omit_one_operand (type, arg1, arg0);
4701 if (operand_equal_p (arg0, arg1, 0))
4703 if (INTEGRAL_TYPE_P (type)
4704 && operand_equal_p (arg1, TYPE_MAX_VALUE (type), 1))
4705 return omit_one_operand (type, arg1, arg0);
4708 case TRUTH_NOT_EXPR:
4709 /* Note that the operand of this must be an int
4710 and its values must be 0 or 1.
4711 ("true" is a fixed value perhaps depending on the language,
4712 but we don't handle values other than 1 correctly yet.) */
4713 tem = invert_truthvalue (arg0);
4714 /* Avoid infinite recursion. */
4715 if (TREE_CODE (tem) == TRUTH_NOT_EXPR)
4717 return convert (type, tem);
4719 case TRUTH_ANDIF_EXPR:
4720 /* Note that the operands of this must be ints
4721 and their values must be 0 or 1.
4722 ("true" is a fixed value perhaps depending on the language.) */
4723 /* If first arg is constant zero, return it. */
4724 if (integer_zerop (arg0))
4726 case TRUTH_AND_EXPR:
4727 /* If either arg is constant true, drop it. */
4728 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
4729 return non_lvalue (arg1);
4730 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
4731 return non_lvalue (arg0);
4732 /* If second arg is constant zero, result is zero, but first arg
4733 must be evaluated. */
4734 if (integer_zerop (arg1))
4735 return omit_one_operand (type, arg1, arg0);
4738 /* We only do these simplifications if we are optimizing. */
4742 /* Check for things like (A || B) && (A || C). We can convert this
4743 to A || (B && C). Note that either operator can be any of the four
4744 truth and/or operations and the transformation will still be
4745 valid. Also note that we only care about order for the
4746 ANDIF and ORIF operators. */
4747 if (TREE_CODE (arg0) == TREE_CODE (arg1)
4748 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
4749 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
4750 || TREE_CODE (arg0) == TRUTH_AND_EXPR
4751 || TREE_CODE (arg0) == TRUTH_OR_EXPR))
4753 tree a00 = TREE_OPERAND (arg0, 0);
4754 tree a01 = TREE_OPERAND (arg0, 1);
4755 tree a10 = TREE_OPERAND (arg1, 0);
4756 tree a11 = TREE_OPERAND (arg1, 1);
4757 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
4758 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
4759 && (code == TRUTH_AND_EXPR
4760 || code == TRUTH_OR_EXPR));
4762 if (operand_equal_p (a00, a10, 0))
4763 return fold (build (TREE_CODE (arg0), type, a00,
4764 fold (build (code, type, a01, a11))));
4765 else if (commutative && operand_equal_p (a00, a11, 0))
4766 return fold (build (TREE_CODE (arg0), type, a00,
4767 fold (build (code, type, a01, a10))));
4768 else if (commutative && operand_equal_p (a01, a10, 0))
4769 return fold (build (TREE_CODE (arg0), type, a01,
4770 fold (build (code, type, a00, a11))));
4772 /* This case if tricky because we must either have commutative
4773 operators or else A10 must not have side-effects. */
4775 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
4776 && operand_equal_p (a01, a11, 0))
4777 return fold (build (TREE_CODE (arg0), type,
4778 fold (build (code, type, a00, a10)),
4782 /* See if we can build a range comparison. */
4783 if (0 != (tem = fold_range_test (t)))
4786 /* Check for the possibility of merging component references. If our
4787 lhs is another similar operation, try to merge its rhs with our
4788 rhs. Then try to merge our lhs and rhs. */
4789 if (TREE_CODE (arg0) == code
4790 && 0 != (tem = fold_truthop (code, type,
4791 TREE_OPERAND (arg0, 1), arg1)))
4792 return fold (build (code, type, TREE_OPERAND (arg0, 0), tem));
4794 if ((tem = fold_truthop (code, type, arg0, arg1)) != 0)
4799 case TRUTH_ORIF_EXPR:
4800 /* Note that the operands of this must be ints
4801 and their values must be 0 or true.
4802 ("true" is a fixed value perhaps depending on the language.) */
4803 /* If first arg is constant true, return it. */
4804 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
4807 /* If either arg is constant zero, drop it. */
4808 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
4809 return non_lvalue (arg1);
4810 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1))
4811 return non_lvalue (arg0);
4812 /* If second arg is constant true, result is true, but we must
4813 evaluate first arg. */
4814 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
4815 return omit_one_operand (type, arg1, arg0);
4818 case TRUTH_XOR_EXPR:
4819 /* If either arg is constant zero, drop it. */
4820 if (integer_zerop (arg0))
4821 return non_lvalue (arg1);
4822 if (integer_zerop (arg1))
4823 return non_lvalue (arg0);
4824 /* If either arg is constant true, this is a logical inversion. */
4825 if (integer_onep (arg0))
4826 return non_lvalue (invert_truthvalue (arg1));
4827 if (integer_onep (arg1))
4828 return non_lvalue (invert_truthvalue (arg0));
4837 /* If one arg is a constant integer, put it last. */
4838 if (TREE_CODE (arg0) == INTEGER_CST
4839 && TREE_CODE (arg1) != INTEGER_CST)
4841 TREE_OPERAND (t, 0) = arg1;
4842 TREE_OPERAND (t, 1) = arg0;
4843 arg0 = TREE_OPERAND (t, 0);
4844 arg1 = TREE_OPERAND (t, 1);
4845 code = swap_tree_comparison (code);
4846 TREE_SET_CODE (t, code);
4849 /* Convert foo++ == CONST into ++foo == CONST + INCR.
4850 First, see if one arg is constant; find the constant arg
4851 and the other one. */
4853 tree constop = 0, varop;
4854 int constopnum = -1;
4856 if (TREE_CONSTANT (arg1))
4857 constopnum = 1, constop = arg1, varop = arg0;
4858 if (TREE_CONSTANT (arg0))
4859 constopnum = 0, constop = arg0, varop = arg1;
4861 if (constop && TREE_CODE (varop) == POSTINCREMENT_EXPR)
4863 /* This optimization is invalid for ordered comparisons
4864 if CONST+INCR overflows or if foo+incr might overflow.
4865 This optimization is invalid for floating point due to rounding.
4866 For pointer types we assume overflow doesn't happen. */
4867 if (TREE_CODE (TREE_TYPE (varop)) == POINTER_TYPE
4868 || (! FLOAT_TYPE_P (TREE_TYPE (varop))
4869 && (code == EQ_EXPR || code == NE_EXPR)))
4872 = fold (build (PLUS_EXPR, TREE_TYPE (varop),
4873 constop, TREE_OPERAND (varop, 1)));
4874 TREE_SET_CODE (varop, PREINCREMENT_EXPR);
4876 /* If VAROP is a reference to a bitfield, we must mask
4877 the constant by the width of the field. */
4878 if (TREE_CODE (TREE_OPERAND (varop, 0)) == COMPONENT_REF
4879 && DECL_BIT_FIELD(TREE_OPERAND
4880 (TREE_OPERAND (varop, 0), 1)))
4883 = TREE_INT_CST_LOW (DECL_SIZE
4885 (TREE_OPERAND (varop, 0), 1)));
4887 newconst = fold (build (BIT_AND_EXPR,
4888 TREE_TYPE (varop), newconst,
4889 convert (TREE_TYPE (varop),
4890 build_int_2 (size, 0))));
4894 t = build (code, type, TREE_OPERAND (t, 0),
4895 TREE_OPERAND (t, 1));
4896 TREE_OPERAND (t, constopnum) = newconst;
4900 else if (constop && TREE_CODE (varop) == POSTDECREMENT_EXPR)
4902 if (TREE_CODE (TREE_TYPE (varop)) == POINTER_TYPE
4903 || (! FLOAT_TYPE_P (TREE_TYPE (varop))
4904 && (code == EQ_EXPR || code == NE_EXPR)))
4907 = fold (build (MINUS_EXPR, TREE_TYPE (varop),
4908 constop, TREE_OPERAND (varop, 1)));
4909 TREE_SET_CODE (varop, PREDECREMENT_EXPR);
4911 if (TREE_CODE (TREE_OPERAND (varop, 0)) == COMPONENT_REF
4912 && DECL_BIT_FIELD(TREE_OPERAND
4913 (TREE_OPERAND (varop, 0), 1)))
4916 = TREE_INT_CST_LOW (DECL_SIZE
4918 (TREE_OPERAND (varop, 0), 1)));
4920 newconst = fold (build (BIT_AND_EXPR,
4921 TREE_TYPE (varop), newconst,
4922 convert (TREE_TYPE (varop),
4923 build_int_2 (size, 0))));
4927 t = build (code, type, TREE_OPERAND (t, 0),
4928 TREE_OPERAND (t, 1));
4929 TREE_OPERAND (t, constopnum) = newconst;
4935 /* Change X >= CST to X > (CST - 1) if CST is positive. */
4936 if (TREE_CODE (arg1) == INTEGER_CST
4937 && TREE_CODE (arg0) != INTEGER_CST
4938 && tree_int_cst_sgn (arg1) > 0)
4940 switch (TREE_CODE (t))
4944 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
4945 t = build (code, type, TREE_OPERAND (t, 0), arg1);
4950 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
4951 t = build (code, type, TREE_OPERAND (t, 0), arg1);
4956 /* If this is an EQ or NE comparison with zero and ARG0 is
4957 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
4958 two operations, but the latter can be done in one less insn
4959 one machine that have only two-operand insns or on which a
4960 constant cannot be the first operand. */
4961 if (integer_zerop (arg1) && (code == EQ_EXPR || code == NE_EXPR)
4962 && TREE_CODE (arg0) == BIT_AND_EXPR)
4964 if (TREE_CODE (TREE_OPERAND (arg0, 0)) == LSHIFT_EXPR
4965 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 0), 0)))
4967 fold (build (code, type,
4968 build (BIT_AND_EXPR, TREE_TYPE (arg0),
4970 TREE_TYPE (TREE_OPERAND (arg0, 0)),
4971 TREE_OPERAND (arg0, 1),
4972 TREE_OPERAND (TREE_OPERAND (arg0, 0), 1)),
4973 convert (TREE_TYPE (arg0),
4976 else if (TREE_CODE (TREE_OPERAND (arg0, 1)) == LSHIFT_EXPR
4977 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 1), 0)))
4979 fold (build (code, type,
4980 build (BIT_AND_EXPR, TREE_TYPE (arg0),
4982 TREE_TYPE (TREE_OPERAND (arg0, 1)),
4983 TREE_OPERAND (arg0, 0),
4984 TREE_OPERAND (TREE_OPERAND (arg0, 1), 1)),
4985 convert (TREE_TYPE (arg0),
4990 /* If this is an NE or EQ comparison of zero against the result of a
4991 signed MOD operation whose second operand is a power of 2, make
4992 the MOD operation unsigned since it is simpler and equivalent. */
4993 if ((code == NE_EXPR || code == EQ_EXPR)
4994 && integer_zerop (arg1)
4995 && ! TREE_UNSIGNED (TREE_TYPE (arg0))
4996 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
4997 || TREE_CODE (arg0) == CEIL_MOD_EXPR
4998 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
4999 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
5000 && integer_pow2p (TREE_OPERAND (arg0, 1)))
5002 tree newtype = unsigned_type (TREE_TYPE (arg0));
5003 tree newmod = build (TREE_CODE (arg0), newtype,
5004 convert (newtype, TREE_OPERAND (arg0, 0)),
5005 convert (newtype, TREE_OPERAND (arg0, 1)));
5007 return build (code, type, newmod, convert (newtype, arg1));
5010 /* If this is an NE comparison of zero with an AND of one, remove the
5011 comparison since the AND will give the correct value. */
5012 if (code == NE_EXPR && integer_zerop (arg1)
5013 && TREE_CODE (arg0) == BIT_AND_EXPR
5014 && integer_onep (TREE_OPERAND (arg0, 1)))
5015 return convert (type, arg0);
5017 /* If we have (A & C) == C where C is a power of 2, convert this into
5018 (A & C) != 0. Similarly for NE_EXPR. */
5019 if ((code == EQ_EXPR || code == NE_EXPR)
5020 && TREE_CODE (arg0) == BIT_AND_EXPR
5021 && integer_pow2p (TREE_OPERAND (arg0, 1))
5022 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
5023 return build (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
5024 arg0, integer_zero_node);
5026 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
5027 and similarly for >= into !=. */
5028 if ((code == LT_EXPR || code == GE_EXPR)
5029 && TREE_UNSIGNED (TREE_TYPE (arg0))
5030 && TREE_CODE (arg1) == LSHIFT_EXPR
5031 && integer_onep (TREE_OPERAND (arg1, 0)))
5032 return build (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
5033 build (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
5034 TREE_OPERAND (arg1, 1)),
5035 convert (TREE_TYPE (arg0), integer_zero_node));
5037 else if ((code == LT_EXPR || code == GE_EXPR)
5038 && TREE_UNSIGNED (TREE_TYPE (arg0))
5039 && (TREE_CODE (arg1) == NOP_EXPR
5040 || TREE_CODE (arg1) == CONVERT_EXPR)
5041 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
5042 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
5044 build (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
5045 convert (TREE_TYPE (arg0),
5046 build (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
5047 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1))),
5048 convert (TREE_TYPE (arg0), integer_zero_node));
5050 /* Simplify comparison of something with itself. (For IEEE
5051 floating-point, we can only do some of these simplifications.) */
5052 if (operand_equal_p (arg0, arg1, 0))
5059 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
5061 t = build_int_2 (1, 0);
5062 TREE_TYPE (t) = type;
5066 TREE_SET_CODE (t, code);
5070 /* For NE, we can only do this simplification if integer. */
5071 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
5073 /* ... fall through ... */
5076 t = build_int_2 (0, 0);
5077 TREE_TYPE (t) = type;
5082 /* An unsigned comparison against 0 can be simplified. */
5083 if (integer_zerop (arg1)
5084 && (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
5085 || TREE_CODE (TREE_TYPE (arg1)) == POINTER_TYPE)
5086 && TREE_UNSIGNED (TREE_TYPE (arg1)))
5088 switch (TREE_CODE (t))
5092 TREE_SET_CODE (t, NE_EXPR);
5096 TREE_SET_CODE (t, EQ_EXPR);
5099 return omit_one_operand (type,
5100 convert (type, integer_one_node),
5103 return omit_one_operand (type,
5104 convert (type, integer_zero_node),
5109 /* If we are comparing an expression that just has comparisons
5110 of two integer values, arithmetic expressions of those comparisons,
5111 and constants, we can simplify it. There are only three cases
5112 to check: the two values can either be equal, the first can be
5113 greater, or the second can be greater. Fold the expression for
5114 those three values. Since each value must be 0 or 1, we have
5115 eight possibilities, each of which corresponds to the constant 0
5116 or 1 or one of the six possible comparisons.
5118 This handles common cases like (a > b) == 0 but also handles
5119 expressions like ((x > y) - (y > x)) > 0, which supposedly
5120 occur in macroized code. */
5122 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
5124 tree cval1 = 0, cval2 = 0;
5127 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
5128 /* Don't handle degenerate cases here; they should already
5129 have been handled anyway. */
5130 && cval1 != 0 && cval2 != 0
5131 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
5132 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
5133 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
5134 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
5135 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
5137 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
5138 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
5140 /* We can't just pass T to eval_subst in case cval1 or cval2
5141 was the same as ARG1. */
5144 = fold (build (code, type,
5145 eval_subst (arg0, cval1, maxval, cval2, minval),
5148 = fold (build (code, type,
5149 eval_subst (arg0, cval1, maxval, cval2, maxval),
5152 = fold (build (code, type,
5153 eval_subst (arg0, cval1, minval, cval2, maxval),
5156 /* All three of these results should be 0 or 1. Confirm they
5157 are. Then use those values to select the proper code
5160 if ((integer_zerop (high_result)
5161 || integer_onep (high_result))
5162 && (integer_zerop (equal_result)
5163 || integer_onep (equal_result))
5164 && (integer_zerop (low_result)
5165 || integer_onep (low_result)))
5167 /* Make a 3-bit mask with the high-order bit being the
5168 value for `>', the next for '=', and the low for '<'. */
5169 switch ((integer_onep (high_result) * 4)
5170 + (integer_onep (equal_result) * 2)
5171 + integer_onep (low_result))
5175 return omit_one_operand (type, integer_zero_node, arg0);
5196 return omit_one_operand (type, integer_one_node, arg0);
5199 t = build (code, type, cval1, cval2);
5201 return save_expr (t);
5208 /* If this is a comparison of a field, we may be able to simplify it. */
5209 if ((TREE_CODE (arg0) == COMPONENT_REF
5210 || TREE_CODE (arg0) == BIT_FIELD_REF)
5211 && (code == EQ_EXPR || code == NE_EXPR)
5212 /* Handle the constant case even without -O
5213 to make sure the warnings are given. */
5214 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
5216 t1 = optimize_bit_field_compare (code, type, arg0, arg1);
5220 /* If this is a comparison of complex values and either or both
5221 sizes are a COMPLEX_EXPR, it is best to split up the comparisons
5222 and join them with a TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR. This
5223 may prevent needless evaluations. */
5224 if ((code == EQ_EXPR || code == NE_EXPR)
5225 && TREE_CODE (TREE_TYPE (arg0)) == COMPLEX_TYPE
5226 && (TREE_CODE (arg0) == COMPLEX_EXPR
5227 || TREE_CODE (arg1) == COMPLEX_EXPR))
5229 tree subtype = TREE_TYPE (TREE_TYPE (arg0));
5230 tree real0 = fold (build1 (REALPART_EXPR, subtype, arg0));
5231 tree imag0 = fold (build1 (IMAGPART_EXPR, subtype, arg0));
5232 tree real1 = fold (build1 (REALPART_EXPR, subtype, arg1));
5233 tree imag1 = fold (build1 (IMAGPART_EXPR, subtype, arg1));
5235 return fold (build ((code == EQ_EXPR ? TRUTH_ANDIF_EXPR
5238 fold (build (code, type, real0, real1)),
5239 fold (build (code, type, imag0, imag1))));
5242 /* From here on, the only cases we handle are when the result is
5243 known to be a constant.
5245 To compute GT, swap the arguments and do LT.
5246 To compute GE, do LT and invert the result.
5247 To compute LE, swap the arguments, do LT and invert the result.
5248 To compute NE, do EQ and invert the result.
5250 Therefore, the code below must handle only EQ and LT. */
5252 if (code == LE_EXPR || code == GT_EXPR)
5254 tem = arg0, arg0 = arg1, arg1 = tem;
5255 code = swap_tree_comparison (code);
5258 /* Note that it is safe to invert for real values here because we
5259 will check below in the one case that it matters. */
5262 if (code == NE_EXPR || code == GE_EXPR)
5265 code = invert_tree_comparison (code);
5268 /* Compute a result for LT or EQ if args permit;
5269 otherwise return T. */
5270 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
5272 if (code == EQ_EXPR)
5273 t1 = build_int_2 ((TREE_INT_CST_LOW (arg0)
5274 == TREE_INT_CST_LOW (arg1))
5275 && (TREE_INT_CST_HIGH (arg0)
5276 == TREE_INT_CST_HIGH (arg1)),
5279 t1 = build_int_2 ((TREE_UNSIGNED (TREE_TYPE (arg0))
5280 ? INT_CST_LT_UNSIGNED (arg0, arg1)
5281 : INT_CST_LT (arg0, arg1)),
5285 /* Assume a nonexplicit constant cannot equal an explicit one,
5286 since such code would be undefined anyway.
5287 Exception: on sysvr4, using #pragma weak,
5288 a label can come out as 0. */
5289 else if (TREE_CODE (arg1) == INTEGER_CST
5290 && !integer_zerop (arg1)
5291 && TREE_CONSTANT (arg0)
5292 && TREE_CODE (arg0) == ADDR_EXPR
5294 t1 = build_int_2 (0, 0);
5296 /* Two real constants can be compared explicitly. */
5297 else if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
5299 /* If either operand is a NaN, the result is false with two
5300 exceptions: First, an NE_EXPR is true on NaNs, but that case
5301 is already handled correctly since we will be inverting the
5302 result for NE_EXPR. Second, if we had inverted a LE_EXPR
5303 or a GE_EXPR into a LT_EXPR, we must return true so that it
5304 will be inverted into false. */
5306 if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
5307 || REAL_VALUE_ISNAN (TREE_REAL_CST (arg1)))
5308 t1 = build_int_2 (invert && code == LT_EXPR, 0);
5310 else if (code == EQ_EXPR)
5311 t1 = build_int_2 (REAL_VALUES_EQUAL (TREE_REAL_CST (arg0),
5312 TREE_REAL_CST (arg1)),
5315 t1 = build_int_2 (REAL_VALUES_LESS (TREE_REAL_CST (arg0),
5316 TREE_REAL_CST (arg1)),
5320 if (t1 == NULL_TREE)
5324 TREE_INT_CST_LOW (t1) ^= 1;
5326 TREE_TYPE (t1) = type;
5330 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
5331 so all simple results must be passed through pedantic_non_lvalue. */
5332 if (TREE_CODE (arg0) == INTEGER_CST)
5333 return pedantic_non_lvalue
5334 (TREE_OPERAND (t, (integer_zerop (arg0) ? 2 : 1)));
5335 else if (operand_equal_p (arg1, TREE_OPERAND (expr, 2), 0))
5336 return pedantic_omit_one_operand (type, arg1, arg0);
5338 /* If the second operand is zero, invert the comparison and swap
5339 the second and third operands. Likewise if the second operand
5340 is constant and the third is not or if the third operand is
5341 equivalent to the first operand of the comparison. */
5343 if (integer_zerop (arg1)
5344 || (TREE_CONSTANT (arg1) && ! TREE_CONSTANT (TREE_OPERAND (t, 2)))
5345 || (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
5346 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
5347 TREE_OPERAND (t, 2),
5348 TREE_OPERAND (arg0, 1))))
5350 /* See if this can be inverted. If it can't, possibly because
5351 it was a floating-point inequality comparison, don't do
5353 tem = invert_truthvalue (arg0);
5355 if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
5357 t = build (code, type, tem,
5358 TREE_OPERAND (t, 2), TREE_OPERAND (t, 1));
5360 arg1 = TREE_OPERAND (t, 2);
5365 /* If we have A op B ? A : C, we may be able to convert this to a
5366 simpler expression, depending on the operation and the values
5367 of B and C. IEEE floating point prevents this though,
5368 because A or B might be -0.0 or a NaN. */
5370 if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
5371 && (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
5372 || ! FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 0)))
5374 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
5375 arg1, TREE_OPERAND (arg0, 1)))
5377 tree arg2 = TREE_OPERAND (t, 2);
5378 enum tree_code comp_code = TREE_CODE (arg0);
5382 /* If we have A op 0 ? A : -A, this is A, -A, abs (A), or abs (-A),
5383 depending on the comparison operation. */
5384 if ((FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 1)))
5385 ? real_zerop (TREE_OPERAND (arg0, 1))
5386 : integer_zerop (TREE_OPERAND (arg0, 1)))
5387 && TREE_CODE (arg2) == NEGATE_EXPR
5388 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
5392 return pedantic_non_lvalue
5393 (fold (build1 (NEGATE_EXPR, type, arg1)));
5395 return pedantic_non_lvalue (convert (type, arg1));
5398 return pedantic_non_lvalue
5399 (convert (type, fold (build1 (ABS_EXPR,
5400 TREE_TYPE (arg1), arg1))));
5403 return pedantic_non_lvalue
5404 (fold (build1 (NEGATE_EXPR, type,
5406 fold (build1 (ABS_EXPR,
5411 /* If this is A != 0 ? A : 0, this is simply A. For ==, it is
5414 if (integer_zerop (TREE_OPERAND (arg0, 1)) && integer_zerop (arg2))
5416 if (comp_code == NE_EXPR)
5417 return pedantic_non_lvalue (convert (type, arg1));
5418 else if (comp_code == EQ_EXPR)
5419 return pedantic_non_lvalue (convert (type, integer_zero_node));
5422 /* If this is A op B ? A : B, this is either A, B, min (A, B),
5423 or max (A, B), depending on the operation. */
5425 if (operand_equal_for_comparison_p (TREE_OPERAND (arg0, 1),
5426 arg2, TREE_OPERAND (arg0, 0)))
5428 tree comp_op0 = TREE_OPERAND (arg0, 0);
5429 tree comp_op1 = TREE_OPERAND (arg0, 1);
5430 tree comp_type = TREE_TYPE (comp_op0);
5435 return pedantic_non_lvalue (convert (type, arg2));
5437 return pedantic_non_lvalue (convert (type, arg1));
5440 return pedantic_non_lvalue
5441 (convert (type, (fold (build (MIN_EXPR, comp_type,
5442 comp_op0, comp_op1)))));
5445 return pedantic_non_lvalue
5446 (convert (type, fold (build (MAX_EXPR, comp_type,
5447 comp_op0, comp_op1))));
5451 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
5452 we might still be able to simplify this. For example,
5453 if C1 is one less or one more than C2, this might have started
5454 out as a MIN or MAX and been transformed by this function.
5455 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
5457 if (INTEGRAL_TYPE_P (type)
5458 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
5459 && TREE_CODE (arg2) == INTEGER_CST)
5463 /* We can replace A with C1 in this case. */
5464 arg1 = convert (type, TREE_OPERAND (arg0, 1));
5465 t = build (code, type, TREE_OPERAND (t, 0), arg1,
5466 TREE_OPERAND (t, 2));
5470 /* If C1 is C2 + 1, this is min(A, C2). */
5471 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type), 1)
5472 && operand_equal_p (TREE_OPERAND (arg0, 1),
5473 const_binop (PLUS_EXPR, arg2,
5474 integer_one_node, 0), 1))
5475 return pedantic_non_lvalue
5476 (fold (build (MIN_EXPR, type, arg1, arg2)));
5480 /* If C1 is C2 - 1, this is min(A, C2). */
5481 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type), 1)
5482 && operand_equal_p (TREE_OPERAND (arg0, 1),
5483 const_binop (MINUS_EXPR, arg2,
5484 integer_one_node, 0), 1))
5485 return pedantic_non_lvalue
5486 (fold (build (MIN_EXPR, type, arg1, arg2)));
5490 /* If C1 is C2 - 1, this is max(A, C2). */
5491 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type), 1)
5492 && operand_equal_p (TREE_OPERAND (arg0, 1),
5493 const_binop (MINUS_EXPR, arg2,
5494 integer_one_node, 0), 1))
5495 return pedantic_non_lvalue
5496 (fold (build (MAX_EXPR, type, arg1, arg2)));
5500 /* If C1 is C2 + 1, this is max(A, C2). */
5501 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type), 1)
5502 && operand_equal_p (TREE_OPERAND (arg0, 1),
5503 const_binop (PLUS_EXPR, arg2,
5504 integer_one_node, 0), 1))
5505 return pedantic_non_lvalue
5506 (fold (build (MAX_EXPR, type, arg1, arg2)));
5511 /* If the second operand is simpler than the third, swap them
5512 since that produces better jump optimization results. */
5513 if ((TREE_CONSTANT (arg1) || TREE_CODE_CLASS (TREE_CODE (arg1)) == 'd'
5514 || TREE_CODE (arg1) == SAVE_EXPR)
5515 && ! (TREE_CONSTANT (TREE_OPERAND (t, 2))
5516 || TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (t, 2))) == 'd'
5517 || TREE_CODE (TREE_OPERAND (t, 2)) == SAVE_EXPR))
5519 /* See if this can be inverted. If it can't, possibly because
5520 it was a floating-point inequality comparison, don't do
5522 tem = invert_truthvalue (arg0);
5524 if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
5526 t = build (code, type, tem,
5527 TREE_OPERAND (t, 2), TREE_OPERAND (t, 1));
5529 arg1 = TREE_OPERAND (t, 2);
5534 /* Convert A ? 1 : 0 to simply A. */
5535 if (integer_onep (TREE_OPERAND (t, 1))
5536 && integer_zerop (TREE_OPERAND (t, 2))
5537 /* If we try to convert TREE_OPERAND (t, 0) to our type, the
5538 call to fold will try to move the conversion inside
5539 a COND, which will recurse. In that case, the COND_EXPR
5540 is probably the best choice, so leave it alone. */
5541 && type == TREE_TYPE (arg0))
5542 return pedantic_non_lvalue (arg0);
5544 /* Look for expressions of the form A & 2 ? 2 : 0. The result of this
5545 operation is simply A & 2. */
5547 if (integer_zerop (TREE_OPERAND (t, 2))
5548 && TREE_CODE (arg0) == NE_EXPR
5549 && integer_zerop (TREE_OPERAND (arg0, 1))
5550 && integer_pow2p (arg1)
5551 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
5552 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
5554 return pedantic_non_lvalue (convert (type, TREE_OPERAND (arg0, 0)));
5559 /* When pedantic, a compound expression can be neither an lvalue
5560 nor an integer constant expression. */
5561 if (TREE_SIDE_EFFECTS (arg0) || pedantic)
5563 /* Don't let (0, 0) be null pointer constant. */
5564 if (integer_zerop (arg1))
5565 return non_lvalue (arg1);
5570 return build_complex (type, arg0, arg1);
5574 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
5576 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
5577 return omit_one_operand (type, TREE_OPERAND (arg0, 0),
5578 TREE_OPERAND (arg0, 1));
5579 else if (TREE_CODE (arg0) == COMPLEX_CST)
5580 return TREE_REALPART (arg0);
5581 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
5582 return fold (build (TREE_CODE (arg0), type,
5583 fold (build1 (REALPART_EXPR, type,
5584 TREE_OPERAND (arg0, 0))),
5585 fold (build1 (REALPART_EXPR,
5586 type, TREE_OPERAND (arg0, 1)))));
5590 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
5591 return convert (type, integer_zero_node);
5592 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
5593 return omit_one_operand (type, TREE_OPERAND (arg0, 1),
5594 TREE_OPERAND (arg0, 0));
5595 else if (TREE_CODE (arg0) == COMPLEX_CST)
5596 return TREE_IMAGPART (arg0);
5597 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
5598 return fold (build (TREE_CODE (arg0), type,
5599 fold (build1 (IMAGPART_EXPR, type,
5600 TREE_OPERAND (arg0, 0))),
5601 fold (build1 (IMAGPART_EXPR, type,
5602 TREE_OPERAND (arg0, 1)))));
5605 /* Pull arithmetic ops out of the CLEANUP_POINT_EXPR where
5607 case CLEANUP_POINT_EXPR:
5608 if (! TREE_SIDE_EFFECTS (arg0))
5609 return TREE_OPERAND (t, 0);
5612 enum tree_code code0 = TREE_CODE (arg0);
5613 int kind0 = TREE_CODE_CLASS (code0);
5614 tree arg00 = TREE_OPERAND (arg0, 0);
5617 if (kind0 == '1' || code0 == TRUTH_NOT_EXPR)
5618 return fold (build1 (code0, type,
5619 fold (build1 (CLEANUP_POINT_EXPR,
5620 TREE_TYPE (arg00), arg00))));
5622 if (kind0 == '<' || kind0 == '2'
5623 || code0 == TRUTH_ANDIF_EXPR || code0 == TRUTH_ORIF_EXPR
5624 || code0 == TRUTH_AND_EXPR || code0 == TRUTH_OR_EXPR
5625 || code0 == TRUTH_XOR_EXPR)
5627 arg01 = TREE_OPERAND (arg0, 1);
5629 if (! TREE_SIDE_EFFECTS (arg00))
5630 return fold (build (code0, type, arg00,
5631 fold (build1 (CLEANUP_POINT_EXPR,
5632 TREE_TYPE (arg01), arg01))));
5634 if (! TREE_SIDE_EFFECTS (arg01))
5635 return fold (build (code0, type,
5636 fold (build1 (CLEANUP_POINT_EXPR,
5637 TREE_TYPE (arg00), arg00)),
5646 } /* switch (code) */