1 /* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /*@@ This file should be rewritten to use an arbitrary precision
23 @@ representation for "struct tree_int_cst" and "struct tree_real_cst".
24 @@ Perhaps the routines could also be used for bc/dc, and made a lib.
25 @@ The routines that translate from the ap rep should
26 @@ warn if precision et. al. is lost.
27 @@ This would also make life easier when this technology is used
28 @@ for cross-compilers. */
31 /* The entry points in this file are fold, size_int_wide, size_binop
34 fold takes a tree as argument and returns a simplified tree.
36 size_binop takes a tree code for an arithmetic operation
37 and two operands that are trees, and produces a tree for the
38 result, assuming the type comes from `sizetype'.
40 size_int takes an integer value, and creates a tree constant
41 with type from `sizetype'.
43 force_fit_type takes a constant and prior overflow indicator, and
44 forces the value to fit the type. It returns an overflow indicator. */
56 static void encode PARAMS ((HOST_WIDE_INT *,
57 unsigned HOST_WIDE_INT,
59 static void decode PARAMS ((HOST_WIDE_INT *,
60 unsigned HOST_WIDE_INT *,
62 static tree negate_expr PARAMS ((tree));
63 static tree split_tree PARAMS ((tree, enum tree_code, tree *, tree *,
65 static tree associate_trees PARAMS ((tree, tree, enum tree_code, tree));
66 static tree int_const_binop PARAMS ((enum tree_code, tree, tree, int, int));
67 static void const_binop_1 PARAMS ((PTR));
68 static tree const_binop PARAMS ((enum tree_code, tree, tree, int));
69 static void fold_convert_1 PARAMS ((PTR));
70 static tree fold_convert PARAMS ((tree, tree));
71 static enum tree_code invert_tree_comparison PARAMS ((enum tree_code));
72 static enum tree_code swap_tree_comparison PARAMS ((enum tree_code));
73 static int truth_value_p PARAMS ((enum tree_code));
74 static int operand_equal_for_comparison_p PARAMS ((tree, tree, tree));
75 static int twoval_comparison_p PARAMS ((tree, tree *, tree *, int *));
76 static tree eval_subst PARAMS ((tree, tree, tree, tree, tree));
77 static tree omit_one_operand PARAMS ((tree, tree, tree));
78 static tree pedantic_omit_one_operand PARAMS ((tree, tree, tree));
79 static tree distribute_bit_expr PARAMS ((enum tree_code, tree, tree, tree));
80 static tree make_bit_field_ref PARAMS ((tree, tree, int, int, int));
81 static tree optimize_bit_field_compare PARAMS ((enum tree_code, tree,
83 static tree decode_field_reference PARAMS ((tree, int *, int *,
84 enum machine_mode *, int *,
85 int *, tree *, tree *));
86 static int all_ones_mask_p PARAMS ((tree, int));
87 static int simple_operand_p PARAMS ((tree));
88 static tree range_binop PARAMS ((enum tree_code, tree, tree, int,
90 static tree make_range PARAMS ((tree, int *, tree *, tree *));
91 static tree build_range_check PARAMS ((tree, tree, int, tree, tree));
92 static int merge_ranges PARAMS ((int *, tree *, tree *, int, tree, tree,
94 static tree fold_range_test PARAMS ((tree));
95 static tree unextend PARAMS ((tree, int, int, tree));
96 static tree fold_truthop PARAMS ((enum tree_code, tree, tree, tree));
97 static tree optimize_minmax_comparison PARAMS ((tree));
98 static tree extract_muldiv PARAMS ((tree, tree, enum tree_code, tree));
99 static tree strip_compound_expr PARAMS ((tree, tree));
100 static int multiple_of_p PARAMS ((tree, tree, tree));
101 static tree constant_boolean_node PARAMS ((int, tree));
102 static int count_cond PARAMS ((tree, int));
105 #define BRANCH_COST 1
108 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
109 overflow. Suppose A, B and SUM have the same respective signs as A1, B1,
110 and SUM1. Then this yields nonzero if overflow occurred during the
113 Overflow occurs if A and B have the same sign, but A and SUM differ in
114 sign. Use `^' to test whether signs differ, and `< 0' to isolate the
116 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
118 /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
119 We do that by representing the two-word integer in 4 words, with only
120 HOST_BITS_PER_WIDE_INT / 2 bits stored in each word, as a positive
121 number. The value of the word is LOWPART + HIGHPART * BASE. */
124 ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)) - 1))
125 #define HIGHPART(x) \
126 ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT / 2)
127 #define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT / 2)
129 /* Unpack a two-word integer into 4 words.
130 LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
131 WORDS points to the array of HOST_WIDE_INTs. */
134 encode (words, low, hi)
135 HOST_WIDE_INT *words;
136 unsigned HOST_WIDE_INT low;
139 words[0] = LOWPART (low);
140 words[1] = HIGHPART (low);
141 words[2] = LOWPART (hi);
142 words[3] = HIGHPART (hi);
145 /* Pack an array of 4 words into a two-word integer.
146 WORDS points to the array of words.
147 The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces. */
150 decode (words, low, hi)
151 HOST_WIDE_INT *words;
152 unsigned HOST_WIDE_INT *low;
155 *low = words[0] + words[1] * BASE;
156 *hi = words[2] + words[3] * BASE;
159 /* Make the integer constant T valid for its type by setting to 0 or 1 all
160 the bits in the constant that don't belong in the type.
162 Return 1 if a signed overflow occurs, 0 otherwise. If OVERFLOW is
163 nonzero, a signed overflow has already occurred in calculating T, so
166 Make the real constant T valid for its type by calling CHECK_FLOAT_VALUE,
170 force_fit_type (t, overflow)
174 unsigned HOST_WIDE_INT low;
178 if (TREE_CODE (t) == REAL_CST)
180 #ifdef CHECK_FLOAT_VALUE
181 CHECK_FLOAT_VALUE (TYPE_MODE (TREE_TYPE (t)), TREE_REAL_CST (t),
187 else if (TREE_CODE (t) != INTEGER_CST)
190 low = TREE_INT_CST_LOW (t);
191 high = TREE_INT_CST_HIGH (t);
193 if (POINTER_TYPE_P (TREE_TYPE (t)))
196 prec = TYPE_PRECISION (TREE_TYPE (t));
198 /* First clear all bits that are beyond the type's precision. */
200 if (prec == 2 * HOST_BITS_PER_WIDE_INT)
202 else if (prec > HOST_BITS_PER_WIDE_INT)
203 TREE_INT_CST_HIGH (t)
204 &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
207 TREE_INT_CST_HIGH (t) = 0;
208 if (prec < HOST_BITS_PER_WIDE_INT)
209 TREE_INT_CST_LOW (t) &= ~((unsigned HOST_WIDE_INT) (-1) << prec);
212 /* Unsigned types do not suffer sign extension or overflow. */
213 if (TREE_UNSIGNED (TREE_TYPE (t)))
216 /* If the value's sign bit is set, extend the sign. */
217 if (prec != 2 * HOST_BITS_PER_WIDE_INT
218 && (prec > HOST_BITS_PER_WIDE_INT
219 ? 0 != (TREE_INT_CST_HIGH (t)
221 << (prec - HOST_BITS_PER_WIDE_INT - 1)))
222 : 0 != (TREE_INT_CST_LOW (t)
223 & ((unsigned HOST_WIDE_INT) 1 << (prec - 1)))))
225 /* Value is negative:
226 set to 1 all the bits that are outside this type's precision. */
227 if (prec > HOST_BITS_PER_WIDE_INT)
228 TREE_INT_CST_HIGH (t)
229 |= ((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
232 TREE_INT_CST_HIGH (t) = -1;
233 if (prec < HOST_BITS_PER_WIDE_INT)
234 TREE_INT_CST_LOW (t) |= ((unsigned HOST_WIDE_INT) (-1) << prec);
238 /* Return nonzero if signed overflow occurred. */
240 ((overflow | (low ^ TREE_INT_CST_LOW (t)) | (high ^ TREE_INT_CST_HIGH (t)))
244 /* Add two doubleword integers with doubleword result.
245 Each argument is given as two `HOST_WIDE_INT' pieces.
246 One argument is L1 and H1; the other, L2 and H2.
247 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
250 add_double (l1, h1, l2, h2, lv, hv)
251 unsigned HOST_WIDE_INT l1, l2;
252 HOST_WIDE_INT h1, h2;
253 unsigned HOST_WIDE_INT *lv;
256 unsigned HOST_WIDE_INT l;
260 h = h1 + h2 + (l < l1);
264 return OVERFLOW_SUM_SIGN (h1, h2, h);
267 /* Negate a doubleword integer with doubleword result.
268 Return nonzero if the operation overflows, assuming it's signed.
269 The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
270 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
273 neg_double (l1, h1, lv, hv)
274 unsigned HOST_WIDE_INT l1;
276 unsigned HOST_WIDE_INT *lv;
283 return (*hv & h1) < 0;
293 /* Multiply two doubleword integers with doubleword result.
294 Return nonzero if the operation overflows, assuming it's signed.
295 Each argument is given as two `HOST_WIDE_INT' pieces.
296 One argument is L1 and H1; the other, L2 and H2.
297 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
300 mul_double (l1, h1, l2, h2, lv, hv)
301 unsigned HOST_WIDE_INT l1, l2;
302 HOST_WIDE_INT h1, h2;
303 unsigned HOST_WIDE_INT *lv;
306 HOST_WIDE_INT arg1[4];
307 HOST_WIDE_INT arg2[4];
308 HOST_WIDE_INT prod[4 * 2];
309 register unsigned HOST_WIDE_INT carry;
310 register int i, j, k;
311 unsigned HOST_WIDE_INT toplow, neglow;
312 HOST_WIDE_INT tophigh, neghigh;
314 encode (arg1, l1, h1);
315 encode (arg2, l2, h2);
317 bzero ((char *) prod, sizeof prod);
319 for (i = 0; i < 4; i++)
322 for (j = 0; j < 4; j++)
325 /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000. */
326 carry += arg1[i] * arg2[j];
327 /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF. */
329 prod[k] = LOWPART (carry);
330 carry = HIGHPART (carry);
335 decode (prod, lv, hv); /* This ignores prod[4] through prod[4*2-1] */
337 /* Check for overflow by calculating the top half of the answer in full;
338 it should agree with the low half's sign bit. */
339 decode (prod+4, &toplow, &tophigh);
342 neg_double (l2, h2, &neglow, &neghigh);
343 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
347 neg_double (l1, h1, &neglow, &neghigh);
348 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
350 return (*hv < 0 ? ~(toplow & tophigh) : toplow | tophigh) != 0;
353 /* Shift the doubleword integer in L1, H1 left by COUNT places
354 keeping only PREC bits of result.
355 Shift right if COUNT is negative.
356 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
357 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
360 lshift_double (l1, h1, count, prec, lv, hv, arith)
361 unsigned HOST_WIDE_INT l1;
362 HOST_WIDE_INT h1, count;
364 unsigned HOST_WIDE_INT *lv;
370 rshift_double (l1, h1, - count, prec, lv, hv, arith);
374 #ifdef SHIFT_COUNT_TRUNCATED
375 if (SHIFT_COUNT_TRUNCATED)
379 if (count >= 2 * HOST_BITS_PER_WIDE_INT)
381 /* Shifting by the host word size is undefined according to the
382 ANSI standard, so we must handle this as a special case. */
386 else if (count >= HOST_BITS_PER_WIDE_INT)
388 *hv = l1 << (count - HOST_BITS_PER_WIDE_INT);
393 *hv = (((unsigned HOST_WIDE_INT) h1 << count)
394 | (l1 >> (HOST_BITS_PER_WIDE_INT - count - 1) >> 1));
399 /* Shift the doubleword integer in L1, H1 right by COUNT places
400 keeping only PREC bits of result. COUNT must be positive.
401 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
402 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
405 rshift_double (l1, h1, count, prec, lv, hv, arith)
406 unsigned HOST_WIDE_INT l1;
407 HOST_WIDE_INT h1, count;
408 unsigned int prec ATTRIBUTE_UNUSED;
409 unsigned HOST_WIDE_INT *lv;
413 unsigned HOST_WIDE_INT signmask;
416 ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
419 #ifdef SHIFT_COUNT_TRUNCATED
420 if (SHIFT_COUNT_TRUNCATED)
424 if (count >= 2 * HOST_BITS_PER_WIDE_INT)
426 /* Shifting by the host word size is undefined according to the
427 ANSI standard, so we must handle this as a special case. */
431 else if (count >= HOST_BITS_PER_WIDE_INT)
434 *lv = ((signmask << (2 * HOST_BITS_PER_WIDE_INT - count - 1) << 1)
435 | ((unsigned HOST_WIDE_INT) h1 >> (count - HOST_BITS_PER_WIDE_INT)));
440 | ((unsigned HOST_WIDE_INT) h1 << (HOST_BITS_PER_WIDE_INT - count - 1) << 1));
441 *hv = ((signmask << (HOST_BITS_PER_WIDE_INT - count))
442 | ((unsigned HOST_WIDE_INT) h1 >> count));
446 /* Rotate the doubleword integer in L1, H1 left by COUNT places
447 keeping only PREC bits of result.
448 Rotate right if COUNT is negative.
449 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
452 lrotate_double (l1, h1, count, prec, lv, hv)
453 unsigned HOST_WIDE_INT l1;
454 HOST_WIDE_INT h1, count;
456 unsigned HOST_WIDE_INT *lv;
459 unsigned HOST_WIDE_INT s1l, s2l;
460 HOST_WIDE_INT s1h, s2h;
466 lshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
467 rshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
472 /* Rotate the doubleword integer in L1, H1 left by COUNT places
473 keeping only PREC bits of result. COUNT must be positive.
474 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
477 rrotate_double (l1, h1, count, prec, lv, hv)
478 unsigned HOST_WIDE_INT l1;
479 HOST_WIDE_INT h1, count;
481 unsigned HOST_WIDE_INT *lv;
484 unsigned HOST_WIDE_INT s1l, s2l;
485 HOST_WIDE_INT s1h, s2h;
491 rshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
492 lshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
497 /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
498 for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
499 CODE is a tree code for a kind of division, one of
500 TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
502 It controls how the quotient is rounded to a integer.
503 Return nonzero if the operation overflows.
504 UNS nonzero says do unsigned division. */
507 div_and_round_double (code, uns,
508 lnum_orig, hnum_orig, lden_orig, hden_orig,
509 lquo, hquo, lrem, hrem)
512 unsigned HOST_WIDE_INT lnum_orig; /* num == numerator == dividend */
513 HOST_WIDE_INT hnum_orig;
514 unsigned HOST_WIDE_INT lden_orig; /* den == denominator == divisor */
515 HOST_WIDE_INT hden_orig;
516 unsigned HOST_WIDE_INT *lquo, *lrem;
517 HOST_WIDE_INT *hquo, *hrem;
520 HOST_WIDE_INT num[4 + 1]; /* extra element for scaling. */
521 HOST_WIDE_INT den[4], quo[4];
523 unsigned HOST_WIDE_INT work;
524 unsigned HOST_WIDE_INT carry = 0;
525 unsigned HOST_WIDE_INT lnum = lnum_orig;
526 HOST_WIDE_INT hnum = hnum_orig;
527 unsigned HOST_WIDE_INT lden = lden_orig;
528 HOST_WIDE_INT hden = hden_orig;
531 if (hden == 0 && lden == 0)
532 overflow = 1, lden = 1;
534 /* calculate quotient sign and convert operands to unsigned. */
540 /* (minimum integer) / (-1) is the only overflow case. */
541 if (neg_double (lnum, hnum, &lnum, &hnum)
542 && ((HOST_WIDE_INT) lden & hden) == -1)
548 neg_double (lden, hden, &lden, &hden);
552 if (hnum == 0 && hden == 0)
553 { /* single precision */
555 /* This unsigned division rounds toward zero. */
561 { /* trivial case: dividend < divisor */
562 /* hden != 0 already checked. */
569 bzero ((char *) quo, sizeof quo);
571 bzero ((char *) num, sizeof num); /* to zero 9th element */
572 bzero ((char *) den, sizeof den);
574 encode (num, lnum, hnum);
575 encode (den, lden, hden);
577 /* Special code for when the divisor < BASE. */
578 if (hden == 0 && lden < (unsigned HOST_WIDE_INT) BASE)
580 /* hnum != 0 already checked. */
581 for (i = 4 - 1; i >= 0; i--)
583 work = num[i] + carry * BASE;
584 quo[i] = work / lden;
590 /* Full double precision division,
591 with thanks to Don Knuth's "Seminumerical Algorithms". */
592 int num_hi_sig, den_hi_sig;
593 unsigned HOST_WIDE_INT quo_est, scale;
595 /* Find the highest non-zero divisor digit. */
596 for (i = 4 - 1; ; i--)
602 /* Insure that the first digit of the divisor is at least BASE/2.
603 This is required by the quotient digit estimation algorithm. */
605 scale = BASE / (den[den_hi_sig] + 1);
607 { /* scale divisor and dividend */
609 for (i = 0; i <= 4 - 1; i++)
611 work = (num[i] * scale) + carry;
612 num[i] = LOWPART (work);
613 carry = HIGHPART (work);
618 for (i = 0; i <= 4 - 1; i++)
620 work = (den[i] * scale) + carry;
621 den[i] = LOWPART (work);
622 carry = HIGHPART (work);
623 if (den[i] != 0) den_hi_sig = i;
630 for (i = num_hi_sig - den_hi_sig - 1; i >= 0; i--)
632 /* Guess the next quotient digit, quo_est, by dividing the first
633 two remaining dividend digits by the high order quotient digit.
634 quo_est is never low and is at most 2 high. */
635 unsigned HOST_WIDE_INT tmp;
637 num_hi_sig = i + den_hi_sig + 1;
638 work = num[num_hi_sig] * BASE + num[num_hi_sig - 1];
639 if (num[num_hi_sig] != den[den_hi_sig])
640 quo_est = work / den[den_hi_sig];
644 /* Refine quo_est so it's usually correct, and at most one high. */
645 tmp = work - quo_est * den[den_hi_sig];
647 && (den[den_hi_sig - 1] * quo_est
648 > (tmp * BASE + num[num_hi_sig - 2])))
651 /* Try QUO_EST as the quotient digit, by multiplying the
652 divisor by QUO_EST and subtracting from the remaining dividend.
653 Keep in mind that QUO_EST is the I - 1st digit. */
656 for (j = 0; j <= den_hi_sig; j++)
658 work = quo_est * den[j] + carry;
659 carry = HIGHPART (work);
660 work = num[i + j] - LOWPART (work);
661 num[i + j] = LOWPART (work);
662 carry += HIGHPART (work) != 0;
665 /* If quo_est was high by one, then num[i] went negative and
666 we need to correct things. */
667 if (num[num_hi_sig] < carry)
670 carry = 0; /* add divisor back in */
671 for (j = 0; j <= den_hi_sig; j++)
673 work = num[i + j] + den[j] + carry;
674 carry = HIGHPART (work);
675 num[i + j] = LOWPART (work);
678 num [num_hi_sig] += carry;
681 /* Store the quotient digit. */
686 decode (quo, lquo, hquo);
689 /* if result is negative, make it so. */
691 neg_double (*lquo, *hquo, lquo, hquo);
693 /* compute trial remainder: rem = num - (quo * den) */
694 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
695 neg_double (*lrem, *hrem, lrem, hrem);
696 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
701 case TRUNC_MOD_EXPR: /* round toward zero */
702 case EXACT_DIV_EXPR: /* for this one, it shouldn't matter */
706 case FLOOR_MOD_EXPR: /* round toward negative infinity */
707 if (quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio < 0 && rem != 0 */
710 add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1,
718 case CEIL_MOD_EXPR: /* round toward positive infinity */
719 if (!quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio > 0 && rem != 0 */
721 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
729 case ROUND_MOD_EXPR: /* round to closest integer */
731 unsigned HOST_WIDE_INT labs_rem = *lrem;
732 HOST_WIDE_INT habs_rem = *hrem;
733 unsigned HOST_WIDE_INT labs_den = lden, ltwice;
734 HOST_WIDE_INT habs_den = hden, htwice;
736 /* Get absolute values */
738 neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
740 neg_double (lden, hden, &labs_den, &habs_den);
742 /* If (2 * abs (lrem) >= abs (lden)) */
743 mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0,
744 labs_rem, habs_rem, <wice, &htwice);
746 if (((unsigned HOST_WIDE_INT) habs_den
747 < (unsigned HOST_WIDE_INT) htwice)
748 || (((unsigned HOST_WIDE_INT) habs_den
749 == (unsigned HOST_WIDE_INT) htwice)
750 && (labs_den < ltwice)))
754 add_double (*lquo, *hquo,
755 (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
758 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
770 /* compute true remainder: rem = num - (quo * den) */
771 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
772 neg_double (*lrem, *hrem, lrem, hrem);
773 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
777 #ifndef REAL_ARITHMETIC
778 /* Effectively truncate a real value to represent the nearest possible value
779 in a narrower mode. The result is actually represented in the same data
780 type as the argument, but its value is usually different.
782 A trap may occur during the FP operations and it is the responsibility
783 of the calling function to have a handler established. */
786 real_value_truncate (mode, arg)
787 enum machine_mode mode;
790 return REAL_VALUE_TRUNCATE (mode, arg);
793 #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
795 /* Check for infinity in an IEEE double precision number. */
801 /* The IEEE 64-bit double format. */
806 unsigned exponent : 11;
807 unsigned mantissa1 : 20;
812 unsigned mantissa1 : 20;
813 unsigned exponent : 11;
819 if (u.big_endian.sign == 1)
822 return (u.big_endian.exponent == 2047
823 && u.big_endian.mantissa1 == 0
824 && u.big_endian.mantissa2 == 0);
829 return (u.little_endian.exponent == 2047
830 && u.little_endian.mantissa1 == 0
831 && u.little_endian.mantissa2 == 0);
835 /* Check whether an IEEE double precision number is a NaN. */
841 /* The IEEE 64-bit double format. */
846 unsigned exponent : 11;
847 unsigned mantissa1 : 20;
852 unsigned mantissa1 : 20;
853 unsigned exponent : 11;
859 if (u.big_endian.sign == 1)
862 return (u.big_endian.exponent == 2047
863 && (u.big_endian.mantissa1 != 0
864 || u.big_endian.mantissa2 != 0));
869 return (u.little_endian.exponent == 2047
870 && (u.little_endian.mantissa1 != 0
871 || u.little_endian.mantissa2 != 0));
875 /* Check for a negative IEEE double precision number. */
881 /* The IEEE 64-bit double format. */
886 unsigned exponent : 11;
887 unsigned mantissa1 : 20;
892 unsigned mantissa1 : 20;
893 unsigned exponent : 11;
899 if (u.big_endian.sign == 1)
902 return u.big_endian.sign;
907 return u.little_endian.sign;
910 #else /* Target not IEEE */
912 /* Let's assume other float formats don't have infinity.
913 (This can be overridden by redefining REAL_VALUE_ISINF.) */
917 REAL_VALUE_TYPE x ATTRIBUTE_UNUSED;
922 /* Let's assume other float formats don't have NaNs.
923 (This can be overridden by redefining REAL_VALUE_ISNAN.) */
927 REAL_VALUE_TYPE x ATTRIBUTE_UNUSED;
932 /* Let's assume other float formats don't have minus zero.
933 (This can be overridden by redefining REAL_VALUE_NEGATIVE.) */
941 #endif /* Target not IEEE */
943 /* Try to change R into its exact multiplicative inverse in machine mode
944 MODE. Return nonzero function value if successful. */
947 exact_real_inverse (mode, r)
948 enum machine_mode mode;
957 #ifdef CHECK_FLOAT_VALUE
961 /* Usually disable if bounds checks are not reliable. */
962 if ((HOST_FLOAT_FORMAT != TARGET_FLOAT_FORMAT) && !flag_pretend_float)
965 /* Set array index to the less significant bits in the unions, depending
966 on the endian-ness of the host doubles.
967 Disable if insufficient information on the data structure. */
968 #if HOST_FLOAT_FORMAT == UNKNOWN_FLOAT_FORMAT
971 #if HOST_FLOAT_FORMAT == VAX_FLOAT_FORMAT
974 #if HOST_FLOAT_FORMAT == IBM_FLOAT_FORMAT
977 #define K (2 * HOST_FLOAT_WORDS_BIG_ENDIAN)
982 if (setjmp (float_error))
984 /* Don't do the optimization if there was an arithmetic error. */
986 set_float_handler (NULL_PTR);
989 set_float_handler (float_error);
991 /* Domain check the argument. */
997 if (REAL_VALUE_ISINF (x.d) || REAL_VALUE_ISNAN (x.d))
1001 /* Compute the reciprocal and check for numerical exactness.
1002 It is unnecessary to check all the significand bits to determine
1003 whether X is a power of 2. If X is not, then it is impossible for
1004 the bottom half significand of both X and 1/X to be all zero bits.
1005 Hence we ignore the data structure of the top half and examine only
1006 the low order bits of the two significands. */
1008 if (x.i[K] != 0 || x.i[K + 1] != 0 || t.i[K] != 0 || t.i[K + 1] != 0)
1011 /* Truncate to the required mode and range-check the result. */
1012 y.d = REAL_VALUE_TRUNCATE (mode, t.d);
1013 #ifdef CHECK_FLOAT_VALUE
1015 if (CHECK_FLOAT_VALUE (mode, y.d, i))
1019 /* Fail if truncation changed the value. */
1020 if (y.d != t.d || y.d == 0.0)
1023 #ifdef REAL_INFINITY
1024 if (REAL_VALUE_ISINF (y.d) || REAL_VALUE_ISNAN (y.d))
1028 /* Output the reciprocal and return success flag. */
1029 set_float_handler (NULL_PTR);
1034 /* Convert C9X hexadecimal floating point string constant S. Return
1035 real value type in mode MODE. This function uses the host computer's
1036 floating point arithmetic when there is no REAL_ARITHMETIC. */
1039 real_hex_to_f (s, mode)
1041 enum machine_mode mode;
1045 unsigned HOST_WIDE_INT low, high;
1046 int shcount, nrmcount, k;
1047 int sign, expsign, isfloat;
1048 int lost = 0;/* Nonzero low order bits shifted out and discarded. */
1049 int frexpon = 0; /* Bits after the decimal point. */
1050 int expon = 0; /* Value of exponent. */
1051 int decpt = 0; /* How many decimal points. */
1052 int gotp = 0; /* How many P's. */
1059 while (*p == ' ' || *p == '\t')
1062 /* Sign, if any, comes first. */
1070 /* The string is supposed to start with 0x or 0X . */
1074 if (*p == 'x' || *p == 'X')
1088 while ((c = *p) != '\0')
1090 if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F')
1091 || (c >= 'a' && c <= 'f'))
1101 if ((high & 0xf0000000) == 0)
1103 high = (high << 4) + ((low >> 28) & 15);
1104 low = (low << 4) + k;
1111 /* Record nonzero lost bits. */
1124 else if (c == 'p' || c == 'P')
1128 /* Sign of exponent. */
1135 /* Value of exponent.
1136 The exponent field is a decimal integer. */
1139 k = (*p++ & 0x7f) - '0';
1140 expon = 10 * expon + k;
1144 /* F suffix is ambiguous in the significand part
1145 so it must appear after the decimal exponent field. */
1146 if (*p == 'f' || *p == 'F')
1154 else if (c == 'l' || c == 'L')
1163 /* Abort if last character read was not legitimate. */
1165 if ((c != '\0' && c != ' ' && c != '\n' && c != '\r') || (decpt > 1))
1168 /* There must be either one decimal point or one p. */
1169 if (decpt == 0 && gotp == 0)
1173 if (high == 0 && low == 0)
1185 /* Leave a high guard bit for carry-out. */
1186 if ((high & 0x80000000) != 0)
1189 low = (low >> 1) | (high << 31);
1194 if ((high & 0xffff8000) == 0)
1196 high = (high << 16) + ((low >> 16) & 0xffff);
1201 while ((high & 0xc0000000) == 0)
1203 high = (high << 1) + ((low >> 31) & 1);
1208 if (isfloat || GET_MODE_SIZE(mode) == UNITS_PER_WORD)
1210 /* Keep 24 bits precision, bits 0x7fffff80.
1211 Rounding bit is 0x40. */
1212 lost = lost | low | (high & 0x3f);
1216 if ((high & 0x80) || lost)
1223 /* We need real.c to do long double formats, so here default
1224 to double precision. */
1225 #if HOST_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
1227 Keep 53 bits precision, bits 0x7fffffff fffffc00.
1228 Rounding bit is low word 0x200. */
1229 lost = lost | (low & 0x1ff);
1232 if ((low & 0x400) || lost)
1234 low = (low + 0x200) & 0xfffffc00;
1241 /* Assume it's a VAX with 56-bit significand,
1242 bits 0x7fffffff ffffff80. */
1243 lost = lost | (low & 0x7f);
1246 if ((low & 0x80) || lost)
1248 low = (low + 0x40) & 0xffffff80;
1258 ip = REAL_VALUE_LDEXP (ip, 32) + (double) low;
1259 /* Apply shifts and exponent value as power of 2. */
1260 ip = REAL_VALUE_LDEXP (ip, expon - (nrmcount + frexpon));
1267 #endif /* no REAL_ARITHMETIC */
1269 /* Given T, an expression, return the negation of T. Allow for T to be
1270 null, in which case return null. */
1282 type = TREE_TYPE (t);
1283 STRIP_SIGN_NOPS (t);
1285 switch (TREE_CODE (t))
1289 if (! TREE_UNSIGNED (type)
1290 && 0 != (tem = fold (build1 (NEGATE_EXPR, type, t)))
1291 && ! TREE_OVERFLOW (tem))
1296 return convert (type, TREE_OPERAND (t, 0));
1299 /* - (A - B) -> B - A */
1300 if (! FLOAT_TYPE_P (type) || flag_fast_math)
1301 return convert (type,
1302 fold (build (MINUS_EXPR, TREE_TYPE (t),
1303 TREE_OPERAND (t, 1),
1304 TREE_OPERAND (t, 0))));
1311 return convert (type, build1 (NEGATE_EXPR, TREE_TYPE (t), t));
1314 /* Split a tree IN into a constant, literal and variable parts that could be
1315 combined with CODE to make IN. "constant" means an expression with
1316 TREE_CONSTANT but that isn't an actual constant. CODE must be a
1317 commutative arithmetic operation. Store the constant part into *CONP,
1318 the literal in &LITP and return the variable part. If a part isn't
1319 present, set it to null. If the tree does not decompose in this way,
1320 return the entire tree as the variable part and the other parts as null.
1322 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
1323 case, we negate an operand that was subtracted. If NEGATE_P is true, we
1324 are negating all of IN.
1326 If IN is itself a literal or constant, return it as appropriate.
1328 Note that we do not guarantee that any of the three values will be the
1329 same type as IN, but they will have the same signedness and mode. */
1332 split_tree (in, code, conp, litp, negate_p)
1334 enum tree_code code;
1343 /* Strip any conversions that don't change the machine mode or signedness. */
1344 STRIP_SIGN_NOPS (in);
1346 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST)
1348 else if (TREE_CONSTANT (in))
1351 else if (TREE_CODE (in) == code
1352 || (! FLOAT_TYPE_P (TREE_TYPE (in))
1353 /* We can associate addition and subtraction together (even
1354 though the C standard doesn't say so) for integers because
1355 the value is not affected. For reals, the value might be
1356 affected, so we can't. */
1357 && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
1358 || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
1360 tree op0 = TREE_OPERAND (in, 0);
1361 tree op1 = TREE_OPERAND (in, 1);
1362 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
1363 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
1365 /* First see if either of the operands is a literal, then a constant. */
1366 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST)
1367 *litp = op0, op0 = 0;
1368 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST)
1369 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
1371 if (op0 != 0 && TREE_CONSTANT (op0))
1372 *conp = op0, op0 = 0;
1373 else if (op1 != 0 && TREE_CONSTANT (op1))
1374 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
1376 /* If we haven't dealt with either operand, this is not a case we can
1377 decompose. Otherwise, VAR is either of the ones remaining, if any. */
1378 if (op0 != 0 && op1 != 0)
1383 var = op1, neg_var_p = neg1_p;
1385 /* Now do any needed negations. */
1386 if (neg_litp_p) *litp = negate_expr (*litp);
1387 if (neg_conp_p) *conp = negate_expr (*conp);
1388 if (neg_var_p) var = negate_expr (var);
1395 var = negate_expr (var);
1396 *conp = negate_expr (*conp);
1397 *litp = negate_expr (*litp);
1403 /* Re-associate trees split by the above function. T1 and T2 are either
1404 expressions to associate or null. Return the new expression, if any. If
1405 we build an operation, do it in TYPE and with CODE, except if CODE is a
1406 MINUS_EXPR, in which case we use PLUS_EXPR since split_tree will already
1407 have taken care of the negations. */
1410 associate_trees (t1, t2, code, type)
1412 enum tree_code code;
1420 if (code == MINUS_EXPR)
1423 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
1424 try to fold this since we will have infinite recursion. But do
1425 deal with any NEGATE_EXPRs. */
1426 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
1427 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
1429 if (TREE_CODE (t1) == NEGATE_EXPR)
1430 return build (MINUS_EXPR, type, convert (type, t2),
1431 convert (type, TREE_OPERAND (t1, 0)));
1432 else if (TREE_CODE (t2) == NEGATE_EXPR)
1433 return build (MINUS_EXPR, type, convert (type, t1),
1434 convert (type, TREE_OPERAND (t2, 0)));
1436 return build (code, type, convert (type, t1), convert (type, t2));
1439 return fold (build (code, type, convert (type, t1), convert (type, t2)));
1442 /* Combine two integer constants ARG1 and ARG2 under operation CODE
1443 to produce a new constant.
1445 If NOTRUNC is nonzero, do not truncate the result to fit the data type.
1446 If FORSIZE is nonzero, compute overflow for unsigned types. */
1449 int_const_binop (code, arg1, arg2, notrunc, forsize)
1450 enum tree_code code;
1451 register tree arg1, arg2;
1452 int notrunc, forsize;
1454 unsigned HOST_WIDE_INT int1l, int2l;
1455 HOST_WIDE_INT int1h, int2h;
1456 unsigned HOST_WIDE_INT low;
1458 unsigned HOST_WIDE_INT garbagel;
1459 HOST_WIDE_INT garbageh;
1461 int uns = TREE_UNSIGNED (TREE_TYPE (arg1));
1463 int no_overflow = 0;
1465 int1l = TREE_INT_CST_LOW (arg1);
1466 int1h = TREE_INT_CST_HIGH (arg1);
1467 int2l = TREE_INT_CST_LOW (arg2);
1468 int2h = TREE_INT_CST_HIGH (arg2);
1473 low = int1l | int2l, hi = int1h | int2h;
1477 low = int1l ^ int2l, hi = int1h ^ int2h;
1481 low = int1l & int2l, hi = int1h & int2h;
1484 case BIT_ANDTC_EXPR:
1485 low = int1l & ~int2l, hi = int1h & ~int2h;
1491 /* It's unclear from the C standard whether shifts can overflow.
1492 The following code ignores overflow; perhaps a C standard
1493 interpretation ruling is needed. */
1494 lshift_double (int1l, int1h, int2l,
1495 TYPE_PRECISION (TREE_TYPE (arg1)),
1504 lrotate_double (int1l, int1h, int2l,
1505 TYPE_PRECISION (TREE_TYPE (arg1)),
1510 overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
1514 neg_double (int2l, int2h, &low, &hi);
1515 add_double (int1l, int1h, low, hi, &low, &hi);
1516 overflow = OVERFLOW_SUM_SIGN (hi, int2h, int1h);
1520 overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
1523 case TRUNC_DIV_EXPR:
1524 case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1525 case EXACT_DIV_EXPR:
1526 /* This is a shortcut for a common special case. */
1527 if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1528 && ! TREE_CONSTANT_OVERFLOW (arg1)
1529 && ! TREE_CONSTANT_OVERFLOW (arg2)
1530 && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1532 if (code == CEIL_DIV_EXPR)
1535 low = int1l / int2l, hi = 0;
1539 /* ... fall through ... */
1541 case ROUND_DIV_EXPR:
1542 if (int2h == 0 && int2l == 1)
1544 low = int1l, hi = int1h;
1547 if (int1l == int2l && int1h == int2h
1548 && ! (int1l == 0 && int1h == 0))
1553 overflow = div_and_round_double (code, uns,
1554 int1l, int1h, int2l, int2h,
1555 &low, &hi, &garbagel, &garbageh);
1558 case TRUNC_MOD_EXPR:
1559 case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1560 /* This is a shortcut for a common special case. */
1561 if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1562 && ! TREE_CONSTANT_OVERFLOW (arg1)
1563 && ! TREE_CONSTANT_OVERFLOW (arg2)
1564 && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1566 if (code == CEIL_MOD_EXPR)
1568 low = int1l % int2l, hi = 0;
1572 /* ... fall through ... */
1574 case ROUND_MOD_EXPR:
1575 overflow = div_and_round_double (code, uns,
1576 int1l, int1h, int2l, int2h,
1577 &garbagel, &garbageh, &low, &hi);
1583 low = (((unsigned HOST_WIDE_INT) int1h
1584 < (unsigned HOST_WIDE_INT) int2h)
1585 || (((unsigned HOST_WIDE_INT) int1h
1586 == (unsigned HOST_WIDE_INT) int2h)
1589 low = (int1h < int2h
1590 || (int1h == int2h && int1l < int2l));
1592 if (low == (code == MIN_EXPR))
1593 low = int1l, hi = int1h;
1595 low = int2l, hi = int2h;
1602 if (forsize && hi == 0 && low < 1000)
1603 return size_int_type_wide (low, TREE_TYPE (arg1));
1606 t = build_int_2 (low, hi);
1607 TREE_TYPE (t) = TREE_TYPE (arg1);
1611 = ((notrunc ? (!uns || forsize) && overflow
1612 : force_fit_type (t, (!uns || forsize) && overflow) && ! no_overflow)
1613 | TREE_OVERFLOW (arg1)
1614 | TREE_OVERFLOW (arg2));
1616 /* If we're doing a size calculation, unsigned arithmetic does overflow.
1617 So check if force_fit_type truncated the value. */
1619 && ! TREE_OVERFLOW (t)
1620 && (TREE_INT_CST_HIGH (t) != hi
1621 || TREE_INT_CST_LOW (t) != low))
1622 TREE_OVERFLOW (t) = 1;
1624 TREE_CONSTANT_OVERFLOW (t) = (TREE_OVERFLOW (t)
1625 | TREE_CONSTANT_OVERFLOW (arg1)
1626 | TREE_CONSTANT_OVERFLOW (arg2));
1630 /* Define input and output argument for const_binop_1. */
1633 enum tree_code code; /* Input: tree code for operation*/
1634 tree type; /* Input: tree type for operation. */
1635 REAL_VALUE_TYPE d1, d2; /* Input: floating point operands. */
1636 tree t; /* Output: constant for result. */
1639 /* Do the real arithmetic for const_binop while protected by a
1640 float overflow handler. */
1643 const_binop_1 (data)
1646 struct cb_args *args = (struct cb_args *) data;
1647 REAL_VALUE_TYPE value;
1649 #ifdef REAL_ARITHMETIC
1650 REAL_ARITHMETIC (value, args->code, args->d1, args->d2);
1655 value = args->d1 + args->d2;
1659 value = args->d1 - args->d2;
1663 value = args->d1 * args->d2;
1667 #ifndef REAL_INFINITY
1672 value = args->d1 / args->d2;
1676 value = MIN (args->d1, args->d2);
1680 value = MAX (args->d1, args->d2);
1686 #endif /* no REAL_ARITHMETIC */
1689 = build_real (args->type,
1690 real_value_truncate (TYPE_MODE (args->type), value));
1693 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1694 constant. We assume ARG1 and ARG2 have the same data type, or at least
1695 are the same kind of constant and the same machine mode.
1697 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
1700 const_binop (code, arg1, arg2, notrunc)
1701 enum tree_code code;
1702 register tree arg1, arg2;
1705 STRIP_NOPS (arg1); STRIP_NOPS (arg2);
1707 if (TREE_CODE (arg1) == INTEGER_CST)
1708 return int_const_binop (code, arg1, arg2, notrunc, 0);
1710 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1711 if (TREE_CODE (arg1) == REAL_CST)
1717 struct cb_args args;
1719 d1 = TREE_REAL_CST (arg1);
1720 d2 = TREE_REAL_CST (arg2);
1722 /* If either operand is a NaN, just return it. Otherwise, set up
1723 for floating-point trap; we return an overflow. */
1724 if (REAL_VALUE_ISNAN (d1))
1726 else if (REAL_VALUE_ISNAN (d2))
1729 /* Setup input for const_binop_1() */
1730 args.type = TREE_TYPE (arg1);
1735 if (do_float_handler (const_binop_1, (PTR) &args))
1736 /* Receive output from const_binop_1. */
1740 /* We got an exception from const_binop_1. */
1741 t = copy_node (arg1);
1746 = (force_fit_type (t, overflow)
1747 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1748 TREE_CONSTANT_OVERFLOW (t)
1750 | TREE_CONSTANT_OVERFLOW (arg1)
1751 | TREE_CONSTANT_OVERFLOW (arg2);
1754 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
1755 if (TREE_CODE (arg1) == COMPLEX_CST)
1757 register tree type = TREE_TYPE (arg1);
1758 register tree r1 = TREE_REALPART (arg1);
1759 register tree i1 = TREE_IMAGPART (arg1);
1760 register tree r2 = TREE_REALPART (arg2);
1761 register tree i2 = TREE_IMAGPART (arg2);
1767 t = build_complex (type,
1768 const_binop (PLUS_EXPR, r1, r2, notrunc),
1769 const_binop (PLUS_EXPR, i1, i2, notrunc));
1773 t = build_complex (type,
1774 const_binop (MINUS_EXPR, r1, r2, notrunc),
1775 const_binop (MINUS_EXPR, i1, i2, notrunc));
1779 t = build_complex (type,
1780 const_binop (MINUS_EXPR,
1781 const_binop (MULT_EXPR,
1783 const_binop (MULT_EXPR,
1786 const_binop (PLUS_EXPR,
1787 const_binop (MULT_EXPR,
1789 const_binop (MULT_EXPR,
1796 register tree magsquared
1797 = const_binop (PLUS_EXPR,
1798 const_binop (MULT_EXPR, r2, r2, notrunc),
1799 const_binop (MULT_EXPR, i2, i2, notrunc),
1802 t = build_complex (type,
1804 (INTEGRAL_TYPE_P (TREE_TYPE (r1))
1805 ? TRUNC_DIV_EXPR : RDIV_EXPR,
1806 const_binop (PLUS_EXPR,
1807 const_binop (MULT_EXPR, r1, r2,
1809 const_binop (MULT_EXPR, i1, i2,
1812 magsquared, notrunc),
1814 (INTEGRAL_TYPE_P (TREE_TYPE (r1))
1815 ? TRUNC_DIV_EXPR : RDIV_EXPR,
1816 const_binop (MINUS_EXPR,
1817 const_binop (MULT_EXPR, i1, r2,
1819 const_binop (MULT_EXPR, r1, i2,
1822 magsquared, notrunc));
1834 /* Return an INTEGER_CST with value whose low-order HOST_BITS_PER_WIDE_INT
1835 bits are given by NUMBER and of the sizetype represented by KIND. */
1838 size_int_wide (number, kind)
1839 HOST_WIDE_INT number;
1840 enum size_type_kind kind;
1842 return size_int_type_wide (number, sizetype_tab[(int) kind]);
1845 /* Likewise, but the desired type is specified explicitly. */
1848 size_int_type_wide (number, type)
1849 HOST_WIDE_INT number;
1852 /* Type-size nodes already made for small sizes. */
1853 static tree size_table[2 * HOST_BITS_PER_WIDE_INT + 1];
1854 static int init_p = 0;
1857 if (ggc_p && ! init_p)
1859 ggc_add_tree_root ((tree *) size_table,
1860 sizeof size_table / sizeof (tree));
1864 /* If this is a positive number that fits in the table we use to hold
1865 cached entries, see if it is already in the table and put it there
1868 && number < (int) (sizeof size_table / sizeof size_table[0]) / 2)
1870 if (size_table[number] != 0)
1871 for (t = size_table[number]; t != 0; t = TREE_CHAIN (t))
1872 if (TREE_TYPE (t) == type)
1877 /* Make this a permanent node. */
1878 push_obstacks_nochange ();
1879 end_temporary_allocation ();
1882 t = build_int_2 (number, 0);
1883 TREE_TYPE (t) = type;
1884 TREE_CHAIN (t) = size_table[number];
1885 size_table[number] = t;
1893 t = build_int_2 (number, number < 0 ? -1 : 0);
1894 TREE_TYPE (t) = type;
1895 TREE_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (t) = force_fit_type (t, 0);
1899 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1900 is a tree code. The type of the result is taken from the operands.
1901 Both must be the same type integer type and it must be a size type.
1902 If the operands are constant, so is the result. */
1905 size_binop (code, arg0, arg1)
1906 enum tree_code code;
1909 tree type = TREE_TYPE (arg0);
1911 if (TREE_CODE (type) != INTEGER_TYPE || ! TYPE_IS_SIZETYPE (type)
1912 || type != TREE_TYPE (arg1))
1915 /* Handle the special case of two integer constants faster. */
1916 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1918 /* And some specific cases even faster than that. */
1919 if (code == PLUS_EXPR && integer_zerop (arg0))
1921 else if ((code == MINUS_EXPR || code == PLUS_EXPR)
1922 && integer_zerop (arg1))
1924 else if (code == MULT_EXPR && integer_onep (arg0))
1927 /* Handle general case of two integer constants. */
1928 return int_const_binop (code, arg0, arg1, 0, 1);
1931 if (arg0 == error_mark_node || arg1 == error_mark_node)
1932 return error_mark_node;
1934 return fold (build (code, type, arg0, arg1));
1937 /* Given two values, either both of sizetype or both of bitsizetype,
1938 compute the difference between the two values. Return the value
1939 in signed type corresponding to the type of the operands. */
1942 size_diffop (arg0, arg1)
1945 tree type = TREE_TYPE (arg0);
1948 if (TREE_CODE (type) != INTEGER_TYPE || ! TYPE_IS_SIZETYPE (type)
1949 || type != TREE_TYPE (arg1))
1952 /* If the type is already signed, just do the simple thing. */
1953 if (! TREE_UNSIGNED (type))
1954 return size_binop (MINUS_EXPR, arg0, arg1);
1956 ctype = (type == bitsizetype || type == ubitsizetype
1957 ? sbitsizetype : ssizetype);
1959 /* If either operand is not a constant, do the conversions to the signed
1960 type and subtract. The hardware will do the right thing with any
1961 overflow in the subtraction. */
1962 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1963 return size_binop (MINUS_EXPR, convert (ctype, arg0),
1964 convert (ctype, arg1));
1966 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1967 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1968 overflow) and negate (which can't either). Special-case a result
1969 of zero while we're here. */
1970 if (tree_int_cst_equal (arg0, arg1))
1971 return convert (ctype, integer_zero_node);
1972 else if (tree_int_cst_lt (arg1, arg0))
1973 return convert (ctype, size_binop (MINUS_EXPR, arg0, arg1));
1975 return size_binop (MINUS_EXPR, convert (ctype, integer_zero_node),
1976 convert (ctype, size_binop (MINUS_EXPR, arg1, arg0)));
1979 /* This structure is used to communicate arguments to fold_convert_1. */
1982 tree arg1; /* Input: value to convert. */
1983 tree type; /* Input: type to convert value to. */
1984 tree t; /* Ouput: result of conversion. */
1987 /* Function to convert floating-point constants, protected by floating
1988 point exception handler. */
1991 fold_convert_1 (data)
1994 struct fc_args * args = (struct fc_args *) data;
1996 args->t = build_real (args->type,
1997 real_value_truncate (TYPE_MODE (args->type),
1998 TREE_REAL_CST (args->arg1)));
2001 /* Given T, a tree representing type conversion of ARG1, a constant,
2002 return a constant tree representing the result of conversion. */
2005 fold_convert (t, arg1)
2009 register tree type = TREE_TYPE (t);
2012 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
2014 if (TREE_CODE (arg1) == INTEGER_CST)
2016 /* If we would build a constant wider than GCC supports,
2017 leave the conversion unfolded. */
2018 if (TYPE_PRECISION (type) > 2 * HOST_BITS_PER_WIDE_INT)
2021 /* If we are trying to make a sizetype for a small integer, use
2022 size_int to pick up cached types to reduce duplicate nodes. */
2023 if (TREE_CODE (type) == INTEGER_CST && TYPE_IS_SIZETYPE (type)
2024 && compare_tree_int (arg1, 1000) < 0)
2025 return size_int_type_wide (TREE_INT_CST_LOW (arg1), type);
2027 /* Given an integer constant, make new constant with new type,
2028 appropriately sign-extended or truncated. */
2029 t = build_int_2 (TREE_INT_CST_LOW (arg1),
2030 TREE_INT_CST_HIGH (arg1));
2031 TREE_TYPE (t) = type;
2032 /* Indicate an overflow if (1) ARG1 already overflowed,
2033 or (2) force_fit_type indicates an overflow.
2034 Tell force_fit_type that an overflow has already occurred
2035 if ARG1 is a too-large unsigned value and T is signed.
2036 But don't indicate an overflow if converting a pointer. */
2038 = ((force_fit_type (t,
2039 (TREE_INT_CST_HIGH (arg1) < 0
2040 && (TREE_UNSIGNED (type)
2041 < TREE_UNSIGNED (TREE_TYPE (arg1)))))
2042 && ! POINTER_TYPE_P (TREE_TYPE (arg1)))
2043 || TREE_OVERFLOW (arg1));
2044 TREE_CONSTANT_OVERFLOW (t)
2045 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
2047 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
2048 else if (TREE_CODE (arg1) == REAL_CST)
2050 /* Don't initialize these, use assignments.
2051 Initialized local aggregates don't work on old compilers. */
2055 tree type1 = TREE_TYPE (arg1);
2058 x = TREE_REAL_CST (arg1);
2059 l = real_value_from_int_cst (type1, TYPE_MIN_VALUE (type));
2061 no_upper_bound = (TYPE_MAX_VALUE (type) == NULL);
2062 if (!no_upper_bound)
2063 u = real_value_from_int_cst (type1, TYPE_MAX_VALUE (type));
2065 /* See if X will be in range after truncation towards 0.
2066 To compensate for truncation, move the bounds away from 0,
2067 but reject if X exactly equals the adjusted bounds. */
2068 #ifdef REAL_ARITHMETIC
2069 REAL_ARITHMETIC (l, MINUS_EXPR, l, dconst1);
2070 if (!no_upper_bound)
2071 REAL_ARITHMETIC (u, PLUS_EXPR, u, dconst1);
2074 if (!no_upper_bound)
2077 /* If X is a NaN, use zero instead and show we have an overflow.
2078 Otherwise, range check. */
2079 if (REAL_VALUE_ISNAN (x))
2080 overflow = 1, x = dconst0;
2081 else if (! (REAL_VALUES_LESS (l, x)
2083 && REAL_VALUES_LESS (x, u)))
2086 #ifndef REAL_ARITHMETIC
2088 HOST_WIDE_INT low, high;
2089 HOST_WIDE_INT half_word
2090 = (HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2);
2095 high = (HOST_WIDE_INT) (x / half_word / half_word);
2096 x -= (REAL_VALUE_TYPE) high * half_word * half_word;
2097 if (x >= (REAL_VALUE_TYPE) half_word * half_word / 2)
2099 low = x - (REAL_VALUE_TYPE) half_word * half_word / 2;
2100 low |= (HOST_WIDE_INT) -1 << (HOST_BITS_PER_WIDE_INT - 1);
2103 low = (HOST_WIDE_INT) x;
2104 if (TREE_REAL_CST (arg1) < 0)
2105 neg_double (low, high, &low, &high);
2106 t = build_int_2 (low, high);
2110 HOST_WIDE_INT low, high;
2111 REAL_VALUE_TO_INT (&low, &high, x);
2112 t = build_int_2 (low, high);
2115 TREE_TYPE (t) = type;
2117 = TREE_OVERFLOW (arg1) | force_fit_type (t, overflow);
2118 TREE_CONSTANT_OVERFLOW (t)
2119 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
2121 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
2122 TREE_TYPE (t) = type;
2124 else if (TREE_CODE (type) == REAL_TYPE)
2126 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
2127 if (TREE_CODE (arg1) == INTEGER_CST)
2128 return build_real_from_int_cst (type, arg1);
2129 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
2130 if (TREE_CODE (arg1) == REAL_CST)
2132 struct fc_args args;
2134 if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1)))
2137 TREE_TYPE (arg1) = type;
2141 /* Setup input for fold_convert_1() */
2145 if (do_float_handler (fold_convert_1, (PTR) &args))
2147 /* Receive output from fold_convert_1() */
2152 /* We got an exception from fold_convert_1() */
2154 t = copy_node (arg1);
2158 = TREE_OVERFLOW (arg1) | force_fit_type (t, overflow);
2159 TREE_CONSTANT_OVERFLOW (t)
2160 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
2164 TREE_CONSTANT (t) = 1;
2168 /* Return an expr equal to X but certainly not valid as an lvalue. */
2176 /* These things are certainly not lvalues. */
2177 if (TREE_CODE (x) == NON_LVALUE_EXPR
2178 || TREE_CODE (x) == INTEGER_CST
2179 || TREE_CODE (x) == REAL_CST
2180 || TREE_CODE (x) == STRING_CST
2181 || TREE_CODE (x) == ADDR_EXPR)
2184 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
2185 TREE_CONSTANT (result) = TREE_CONSTANT (x);
2189 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2190 Zero means allow extended lvalues. */
2192 int pedantic_lvalues;
2194 /* When pedantic, return an expr equal to X but certainly not valid as a
2195 pedantic lvalue. Otherwise, return X. */
2198 pedantic_non_lvalue (x)
2201 if (pedantic_lvalues)
2202 return non_lvalue (x);
2207 /* Given a tree comparison code, return the code that is the logical inverse
2208 of the given code. It is not safe to do this for floating-point
2209 comparisons, except for NE_EXPR and EQ_EXPR. */
2211 static enum tree_code
2212 invert_tree_comparison (code)
2213 enum tree_code code;
2234 /* Similar, but return the comparison that results if the operands are
2235 swapped. This is safe for floating-point. */
2237 static enum tree_code
2238 swap_tree_comparison (code)
2239 enum tree_code code;
2259 /* Return nonzero if CODE is a tree code that represents a truth value. */
2262 truth_value_p (code)
2263 enum tree_code code;
2265 return (TREE_CODE_CLASS (code) == '<'
2266 || code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
2267 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
2268 || code == TRUTH_XOR_EXPR || code == TRUTH_NOT_EXPR);
2271 /* Return nonzero if two operands are necessarily equal.
2272 If ONLY_CONST is non-zero, only return non-zero for constants.
2273 This function tests whether the operands are indistinguishable;
2274 it does not test whether they are equal using C's == operation.
2275 The distinction is important for IEEE floating point, because
2276 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2277 (2) two NaNs may be indistinguishable, but NaN!=NaN. */
2280 operand_equal_p (arg0, arg1, only_const)
2284 /* If both types don't have the same signedness, then we can't consider
2285 them equal. We must check this before the STRIP_NOPS calls
2286 because they may change the signedness of the arguments. */
2287 if (TREE_UNSIGNED (TREE_TYPE (arg0)) != TREE_UNSIGNED (TREE_TYPE (arg1)))
2293 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2294 /* This is needed for conversions and for COMPONENT_REF.
2295 Might as well play it safe and always test this. */
2296 || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2297 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2298 || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
2301 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2302 We don't care about side effects in that case because the SAVE_EXPR
2303 takes care of that for us. In all other cases, two expressions are
2304 equal if they have no side effects. If we have two identical
2305 expressions with side effects that should be treated the same due
2306 to the only side effects being identical SAVE_EXPR's, that will
2307 be detected in the recursive calls below. */
2308 if (arg0 == arg1 && ! only_const
2309 && (TREE_CODE (arg0) == SAVE_EXPR
2310 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2313 /* Next handle constant cases, those for which we can return 1 even
2314 if ONLY_CONST is set. */
2315 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2316 switch (TREE_CODE (arg0))
2319 return (! TREE_CONSTANT_OVERFLOW (arg0)
2320 && ! TREE_CONSTANT_OVERFLOW (arg1)
2321 && tree_int_cst_equal (arg0, arg1));
2324 return (! TREE_CONSTANT_OVERFLOW (arg0)
2325 && ! TREE_CONSTANT_OVERFLOW (arg1)
2326 && REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
2327 TREE_REAL_CST (arg1)));
2330 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2332 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2336 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2337 && ! memcmp (TREE_STRING_POINTER (arg0),
2338 TREE_STRING_POINTER (arg1),
2339 TREE_STRING_LENGTH (arg0)));
2342 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2351 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2354 /* Two conversions are equal only if signedness and modes match. */
2355 if ((TREE_CODE (arg0) == NOP_EXPR || TREE_CODE (arg0) == CONVERT_EXPR)
2356 && (TREE_UNSIGNED (TREE_TYPE (arg0))
2357 != TREE_UNSIGNED (TREE_TYPE (arg1))))
2360 return operand_equal_p (TREE_OPERAND (arg0, 0),
2361 TREE_OPERAND (arg1, 0), 0);
2365 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0)
2366 && operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1),
2370 /* For commutative ops, allow the other order. */
2371 return ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MULT_EXPR
2372 || TREE_CODE (arg0) == MIN_EXPR || TREE_CODE (arg0) == MAX_EXPR
2373 || TREE_CODE (arg0) == BIT_IOR_EXPR
2374 || TREE_CODE (arg0) == BIT_XOR_EXPR
2375 || TREE_CODE (arg0) == BIT_AND_EXPR
2376 || TREE_CODE (arg0) == NE_EXPR || TREE_CODE (arg0) == EQ_EXPR)
2377 && operand_equal_p (TREE_OPERAND (arg0, 0),
2378 TREE_OPERAND (arg1, 1), 0)
2379 && operand_equal_p (TREE_OPERAND (arg0, 1),
2380 TREE_OPERAND (arg1, 0), 0));
2383 /* If either of the pointer (or reference) expressions we are dereferencing
2384 contain a side effect, these cannot be equal. */
2385 if (TREE_SIDE_EFFECTS (arg0)
2386 || TREE_SIDE_EFFECTS (arg1))
2389 switch (TREE_CODE (arg0))
2392 return operand_equal_p (TREE_OPERAND (arg0, 0),
2393 TREE_OPERAND (arg1, 0), 0);
2397 return (operand_equal_p (TREE_OPERAND (arg0, 0),
2398 TREE_OPERAND (arg1, 0), 0)
2399 && operand_equal_p (TREE_OPERAND (arg0, 1),
2400 TREE_OPERAND (arg1, 1), 0));
2403 return (operand_equal_p (TREE_OPERAND (arg0, 0),
2404 TREE_OPERAND (arg1, 0), 0)
2405 && operand_equal_p (TREE_OPERAND (arg0, 1),
2406 TREE_OPERAND (arg1, 1), 0)
2407 && operand_equal_p (TREE_OPERAND (arg0, 2),
2408 TREE_OPERAND (arg1, 2), 0));
2414 if (TREE_CODE (arg0) == RTL_EXPR)
2415 return rtx_equal_p (RTL_EXPR_RTL (arg0), RTL_EXPR_RTL (arg1));
2423 /* Similar to operand_equal_p, but see if ARG0 might have been made by
2424 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
2426 When in doubt, return 0. */
2429 operand_equal_for_comparison_p (arg0, arg1, other)
2433 int unsignedp1, unsignedpo;
2434 tree primarg0, primarg1, primother;
2435 unsigned correct_width;
2437 if (operand_equal_p (arg0, arg1, 0))
2440 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
2441 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
2444 /* Discard any conversions that don't change the modes of ARG0 and ARG1
2445 and see if the inner values are the same. This removes any
2446 signedness comparison, which doesn't matter here. */
2447 primarg0 = arg0, primarg1 = arg1;
2448 STRIP_NOPS (primarg0); STRIP_NOPS (primarg1);
2449 if (operand_equal_p (primarg0, primarg1, 0))
2452 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
2453 actual comparison operand, ARG0.
2455 First throw away any conversions to wider types
2456 already present in the operands. */
2458 primarg1 = get_narrower (arg1, &unsignedp1);
2459 primother = get_narrower (other, &unsignedpo);
2461 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
2462 if (unsignedp1 == unsignedpo
2463 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
2464 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
2466 tree type = TREE_TYPE (arg0);
2468 /* Make sure shorter operand is extended the right way
2469 to match the longer operand. */
2470 primarg1 = convert (signed_or_unsigned_type (unsignedp1,
2471 TREE_TYPE (primarg1)),
2474 if (operand_equal_p (arg0, convert (type, primarg1), 0))
2481 /* See if ARG is an expression that is either a comparison or is performing
2482 arithmetic on comparisons. The comparisons must only be comparing
2483 two different values, which will be stored in *CVAL1 and *CVAL2; if
2484 they are non-zero it means that some operands have already been found.
2485 No variables may be used anywhere else in the expression except in the
2486 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
2487 the expression and save_expr needs to be called with CVAL1 and CVAL2.
2489 If this is true, return 1. Otherwise, return zero. */
2492 twoval_comparison_p (arg, cval1, cval2, save_p)
2494 tree *cval1, *cval2;
2497 enum tree_code code = TREE_CODE (arg);
2498 char class = TREE_CODE_CLASS (code);
2500 /* We can handle some of the 'e' cases here. */
2501 if (class == 'e' && code == TRUTH_NOT_EXPR)
2503 else if (class == 'e'
2504 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
2505 || code == COMPOUND_EXPR))
2508 else if (class == 'e' && code == SAVE_EXPR && SAVE_EXPR_RTL (arg) == 0
2509 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
2511 /* If we've already found a CVAL1 or CVAL2, this expression is
2512 two complex to handle. */
2513 if (*cval1 || *cval2)
2523 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
2526 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
2527 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2528 cval1, cval2, save_p));
2534 if (code == COND_EXPR)
2535 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
2536 cval1, cval2, save_p)
2537 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2538 cval1, cval2, save_p)
2539 && twoval_comparison_p (TREE_OPERAND (arg, 2),
2540 cval1, cval2, save_p));
2544 /* First see if we can handle the first operand, then the second. For
2545 the second operand, we know *CVAL1 can't be zero. It must be that
2546 one side of the comparison is each of the values; test for the
2547 case where this isn't true by failing if the two operands
2550 if (operand_equal_p (TREE_OPERAND (arg, 0),
2551 TREE_OPERAND (arg, 1), 0))
2555 *cval1 = TREE_OPERAND (arg, 0);
2556 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
2558 else if (*cval2 == 0)
2559 *cval2 = TREE_OPERAND (arg, 0);
2560 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
2565 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
2567 else if (*cval2 == 0)
2568 *cval2 = TREE_OPERAND (arg, 1);
2569 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
2581 /* ARG is a tree that is known to contain just arithmetic operations and
2582 comparisons. Evaluate the operations in the tree substituting NEW0 for
2583 any occurrence of OLD0 as an operand of a comparison and likewise for
2587 eval_subst (arg, old0, new0, old1, new1)
2589 tree old0, new0, old1, new1;
2591 tree type = TREE_TYPE (arg);
2592 enum tree_code code = TREE_CODE (arg);
2593 char class = TREE_CODE_CLASS (code);
2595 /* We can handle some of the 'e' cases here. */
2596 if (class == 'e' && code == TRUTH_NOT_EXPR)
2598 else if (class == 'e'
2599 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2605 return fold (build1 (code, type,
2606 eval_subst (TREE_OPERAND (arg, 0),
2607 old0, new0, old1, new1)));
2610 return fold (build (code, type,
2611 eval_subst (TREE_OPERAND (arg, 0),
2612 old0, new0, old1, new1),
2613 eval_subst (TREE_OPERAND (arg, 1),
2614 old0, new0, old1, new1)));
2620 return eval_subst (TREE_OPERAND (arg, 0), old0, new0, old1, new1);
2623 return eval_subst (TREE_OPERAND (arg, 1), old0, new0, old1, new1);
2626 return fold (build (code, type,
2627 eval_subst (TREE_OPERAND (arg, 0),
2628 old0, new0, old1, new1),
2629 eval_subst (TREE_OPERAND (arg, 1),
2630 old0, new0, old1, new1),
2631 eval_subst (TREE_OPERAND (arg, 2),
2632 old0, new0, old1, new1)));
2636 /* fall through - ??? */
2640 tree arg0 = TREE_OPERAND (arg, 0);
2641 tree arg1 = TREE_OPERAND (arg, 1);
2643 /* We need to check both for exact equality and tree equality. The
2644 former will be true if the operand has a side-effect. In that
2645 case, we know the operand occurred exactly once. */
2647 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
2649 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
2652 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
2654 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
2657 return fold (build (code, type, arg0, arg1));
2665 /* Return a tree for the case when the result of an expression is RESULT
2666 converted to TYPE and OMITTED was previously an operand of the expression
2667 but is now not needed (e.g., we folded OMITTED * 0).
2669 If OMITTED has side effects, we must evaluate it. Otherwise, just do
2670 the conversion of RESULT to TYPE. */
2673 omit_one_operand (type, result, omitted)
2674 tree type, result, omitted;
2676 tree t = convert (type, result);
2678 if (TREE_SIDE_EFFECTS (omitted))
2679 return build (COMPOUND_EXPR, type, omitted, t);
2681 return non_lvalue (t);
2684 /* Similar, but call pedantic_non_lvalue instead of non_lvalue. */
2687 pedantic_omit_one_operand (type, result, omitted)
2688 tree type, result, omitted;
2690 tree t = convert (type, result);
2692 if (TREE_SIDE_EFFECTS (omitted))
2693 return build (COMPOUND_EXPR, type, omitted, t);
2695 return pedantic_non_lvalue (t);
2700 /* Return a simplified tree node for the truth-negation of ARG. This
2701 never alters ARG itself. We assume that ARG is an operation that
2702 returns a truth value (0 or 1). */
2705 invert_truthvalue (arg)
2708 tree type = TREE_TYPE (arg);
2709 enum tree_code code = TREE_CODE (arg);
2711 if (code == ERROR_MARK)
2714 /* If this is a comparison, we can simply invert it, except for
2715 floating-point non-equality comparisons, in which case we just
2716 enclose a TRUTH_NOT_EXPR around what we have. */
2718 if (TREE_CODE_CLASS (code) == '<')
2720 if (FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg, 0)))
2721 && !flag_fast_math && code != NE_EXPR && code != EQ_EXPR)
2722 return build1 (TRUTH_NOT_EXPR, type, arg);
2724 return build (invert_tree_comparison (code), type,
2725 TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
2731 return convert (type, build_int_2 (integer_zerop (arg), 0));
2733 case TRUTH_AND_EXPR:
2734 return build (TRUTH_OR_EXPR, type,
2735 invert_truthvalue (TREE_OPERAND (arg, 0)),
2736 invert_truthvalue (TREE_OPERAND (arg, 1)));
2739 return build (TRUTH_AND_EXPR, type,
2740 invert_truthvalue (TREE_OPERAND (arg, 0)),
2741 invert_truthvalue (TREE_OPERAND (arg, 1)));
2743 case TRUTH_XOR_EXPR:
2744 /* Here we can invert either operand. We invert the first operand
2745 unless the second operand is a TRUTH_NOT_EXPR in which case our
2746 result is the XOR of the first operand with the inside of the
2747 negation of the second operand. */
2749 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
2750 return build (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
2751 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
2753 return build (TRUTH_XOR_EXPR, type,
2754 invert_truthvalue (TREE_OPERAND (arg, 0)),
2755 TREE_OPERAND (arg, 1));
2757 case TRUTH_ANDIF_EXPR:
2758 return build (TRUTH_ORIF_EXPR, type,
2759 invert_truthvalue (TREE_OPERAND (arg, 0)),
2760 invert_truthvalue (TREE_OPERAND (arg, 1)));
2762 case TRUTH_ORIF_EXPR:
2763 return build (TRUTH_ANDIF_EXPR, type,
2764 invert_truthvalue (TREE_OPERAND (arg, 0)),
2765 invert_truthvalue (TREE_OPERAND (arg, 1)));
2767 case TRUTH_NOT_EXPR:
2768 return TREE_OPERAND (arg, 0);
2771 return build (COND_EXPR, type, TREE_OPERAND (arg, 0),
2772 invert_truthvalue (TREE_OPERAND (arg, 1)),
2773 invert_truthvalue (TREE_OPERAND (arg, 2)));
2776 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0),
2777 invert_truthvalue (TREE_OPERAND (arg, 1)));
2779 case WITH_RECORD_EXPR:
2780 return build (WITH_RECORD_EXPR, type,
2781 invert_truthvalue (TREE_OPERAND (arg, 0)),
2782 TREE_OPERAND (arg, 1));
2784 case NON_LVALUE_EXPR:
2785 return invert_truthvalue (TREE_OPERAND (arg, 0));
2790 return build1 (TREE_CODE (arg), type,
2791 invert_truthvalue (TREE_OPERAND (arg, 0)));
2794 if (!integer_onep (TREE_OPERAND (arg, 1)))
2796 return build (EQ_EXPR, type, arg, convert (type, integer_zero_node));
2799 return build1 (TRUTH_NOT_EXPR, type, arg);
2801 case CLEANUP_POINT_EXPR:
2802 return build1 (CLEANUP_POINT_EXPR, type,
2803 invert_truthvalue (TREE_OPERAND (arg, 0)));
2808 if (TREE_CODE (TREE_TYPE (arg)) != BOOLEAN_TYPE)
2810 return build1 (TRUTH_NOT_EXPR, type, arg);
2813 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
2814 operands are another bit-wise operation with a common input. If so,
2815 distribute the bit operations to save an operation and possibly two if
2816 constants are involved. For example, convert
2817 (A | B) & (A | C) into A | (B & C)
2818 Further simplification will occur if B and C are constants.
2820 If this optimization cannot be done, 0 will be returned. */
2823 distribute_bit_expr (code, type, arg0, arg1)
2824 enum tree_code code;
2831 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2832 || TREE_CODE (arg0) == code
2833 || (TREE_CODE (arg0) != BIT_AND_EXPR
2834 && TREE_CODE (arg0) != BIT_IOR_EXPR))
2837 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
2839 common = TREE_OPERAND (arg0, 0);
2840 left = TREE_OPERAND (arg0, 1);
2841 right = TREE_OPERAND (arg1, 1);
2843 else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
2845 common = TREE_OPERAND (arg0, 0);
2846 left = TREE_OPERAND (arg0, 1);
2847 right = TREE_OPERAND (arg1, 0);
2849 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
2851 common = TREE_OPERAND (arg0, 1);
2852 left = TREE_OPERAND (arg0, 0);
2853 right = TREE_OPERAND (arg1, 1);
2855 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
2857 common = TREE_OPERAND (arg0, 1);
2858 left = TREE_OPERAND (arg0, 0);
2859 right = TREE_OPERAND (arg1, 0);
2864 return fold (build (TREE_CODE (arg0), type, common,
2865 fold (build (code, type, left, right))));
2868 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
2869 starting at BITPOS. The field is unsigned if UNSIGNEDP is non-zero. */
2872 make_bit_field_ref (inner, type, bitsize, bitpos, unsignedp)
2875 int bitsize, bitpos;
2878 tree result = build (BIT_FIELD_REF, type, inner,
2879 size_int (bitsize), bitsize_int (bitpos));
2881 TREE_UNSIGNED (result) = unsignedp;
2886 /* Optimize a bit-field compare.
2888 There are two cases: First is a compare against a constant and the
2889 second is a comparison of two items where the fields are at the same
2890 bit position relative to the start of a chunk (byte, halfword, word)
2891 large enough to contain it. In these cases we can avoid the shift
2892 implicit in bitfield extractions.
2894 For constants, we emit a compare of the shifted constant with the
2895 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
2896 compared. For two fields at the same position, we do the ANDs with the
2897 similar mask and compare the result of the ANDs.
2899 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
2900 COMPARE_TYPE is the type of the comparison, and LHS and RHS
2901 are the left and right operands of the comparison, respectively.
2903 If the optimization described above can be done, we return the resulting
2904 tree. Otherwise we return zero. */
2907 optimize_bit_field_compare (code, compare_type, lhs, rhs)
2908 enum tree_code code;
2912 int lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
2913 tree type = TREE_TYPE (lhs);
2914 tree signed_type, unsigned_type;
2915 int const_p = TREE_CODE (rhs) == INTEGER_CST;
2916 enum machine_mode lmode, rmode, nmode;
2917 int lunsignedp, runsignedp;
2918 int lvolatilep = 0, rvolatilep = 0;
2920 tree linner, rinner = NULL_TREE;
2924 /* Get all the information about the extractions being done. If the bit size
2925 if the same as the size of the underlying object, we aren't doing an
2926 extraction at all and so can do nothing. We also don't want to
2927 do anything if the inner expression is a PLACEHOLDER_EXPR since we
2928 then will no longer be able to replace it. */
2929 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
2930 &lunsignedp, &lvolatilep, &alignment);
2931 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
2932 || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
2937 /* If this is not a constant, we can only do something if bit positions,
2938 sizes, and signedness are the same. */
2939 rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
2940 &runsignedp, &rvolatilep, &alignment);
2942 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
2943 || lunsignedp != runsignedp || offset != 0
2944 || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
2948 /* See if we can find a mode to refer to this field. We should be able to,
2949 but fail if we can't. */
2950 nmode = get_best_mode (lbitsize, lbitpos,
2951 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
2952 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
2953 TYPE_ALIGN (TREE_TYPE (rinner))),
2954 word_mode, lvolatilep || rvolatilep);
2955 if (nmode == VOIDmode)
2958 /* Set signed and unsigned types of the precision of this mode for the
2960 signed_type = type_for_mode (nmode, 0);
2961 unsigned_type = type_for_mode (nmode, 1);
2963 /* Compute the bit position and size for the new reference and our offset
2964 within it. If the new reference is the same size as the original, we
2965 won't optimize anything, so return zero. */
2966 nbitsize = GET_MODE_BITSIZE (nmode);
2967 nbitpos = lbitpos & ~ (nbitsize - 1);
2969 if (nbitsize == lbitsize)
2972 if (BYTES_BIG_ENDIAN)
2973 lbitpos = nbitsize - lbitsize - lbitpos;
2975 /* Make the mask to be used against the extracted field. */
2976 mask = build_int_2 (~0, ~0);
2977 TREE_TYPE (mask) = unsigned_type;
2978 force_fit_type (mask, 0);
2979 mask = convert (unsigned_type, mask);
2980 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize), 0);
2981 mask = const_binop (RSHIFT_EXPR, mask,
2982 size_int (nbitsize - lbitsize - lbitpos), 0);
2985 /* If not comparing with constant, just rework the comparison
2987 return build (code, compare_type,
2988 build (BIT_AND_EXPR, unsigned_type,
2989 make_bit_field_ref (linner, unsigned_type,
2990 nbitsize, nbitpos, 1),
2992 build (BIT_AND_EXPR, unsigned_type,
2993 make_bit_field_ref (rinner, unsigned_type,
2994 nbitsize, nbitpos, 1),
2997 /* Otherwise, we are handling the constant case. See if the constant is too
2998 big for the field. Warn and return a tree of for 0 (false) if so. We do
2999 this not only for its own sake, but to avoid having to test for this
3000 error case below. If we didn't, we might generate wrong code.
3002 For unsigned fields, the constant shifted right by the field length should
3003 be all zero. For signed fields, the high-order bits should agree with
3008 if (! integer_zerop (const_binop (RSHIFT_EXPR,
3009 convert (unsigned_type, rhs),
3010 size_int (lbitsize), 0)))
3012 warning ("comparison is always %d due to width of bitfield",
3014 return convert (compare_type,
3016 ? integer_one_node : integer_zero_node));
3021 tree tem = const_binop (RSHIFT_EXPR, convert (signed_type, rhs),
3022 size_int (lbitsize - 1), 0);
3023 if (! integer_zerop (tem) && ! integer_all_onesp (tem))
3025 warning ("comparison is always %d due to width of bitfield",
3027 return convert (compare_type,
3029 ? integer_one_node : integer_zero_node));
3033 /* Single-bit compares should always be against zero. */
3034 if (lbitsize == 1 && ! integer_zerop (rhs))
3036 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
3037 rhs = convert (type, integer_zero_node);
3040 /* Make a new bitfield reference, shift the constant over the
3041 appropriate number of bits and mask it with the computed mask
3042 (in case this was a signed field). If we changed it, make a new one. */
3043 lhs = make_bit_field_ref (linner, unsigned_type, nbitsize, nbitpos, 1);
3046 TREE_SIDE_EFFECTS (lhs) = 1;
3047 TREE_THIS_VOLATILE (lhs) = 1;
3050 rhs = fold (const_binop (BIT_AND_EXPR,
3051 const_binop (LSHIFT_EXPR,
3052 convert (unsigned_type, rhs),
3053 size_int (lbitpos), 0),
3056 return build (code, compare_type,
3057 build (BIT_AND_EXPR, unsigned_type, lhs, mask),
3061 /* Subroutine for fold_truthop: decode a field reference.
3063 If EXP is a comparison reference, we return the innermost reference.
3065 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3066 set to the starting bit number.
3068 If the innermost field can be completely contained in a mode-sized
3069 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
3071 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3072 otherwise it is not changed.
3074 *PUNSIGNEDP is set to the signedness of the field.
3076 *PMASK is set to the mask used. This is either contained in a
3077 BIT_AND_EXPR or derived from the width of the field.
3079 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
3081 Return 0 if this is not a component reference or is one that we can't
3082 do anything with. */
3085 decode_field_reference (exp, pbitsize, pbitpos, pmode, punsignedp,
3086 pvolatilep, pmask, pand_mask)
3088 int *pbitsize, *pbitpos;
3089 enum machine_mode *pmode;
3090 int *punsignedp, *pvolatilep;
3095 tree mask, inner, offset;
3100 /* All the optimizations using this function assume integer fields.
3101 There are problems with FP fields since the type_for_size call
3102 below can fail for, e.g., XFmode. */
3103 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
3108 if (TREE_CODE (exp) == BIT_AND_EXPR)
3110 and_mask = TREE_OPERAND (exp, 1);
3111 exp = TREE_OPERAND (exp, 0);
3112 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
3113 if (TREE_CODE (and_mask) != INTEGER_CST)
3118 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
3119 punsignedp, pvolatilep, &alignment);
3120 if ((inner == exp && and_mask == 0)
3121 || *pbitsize < 0 || offset != 0
3122 || TREE_CODE (inner) == PLACEHOLDER_EXPR)
3125 /* Compute the mask to access the bitfield. */
3126 unsigned_type = type_for_size (*pbitsize, 1);
3127 precision = TYPE_PRECISION (unsigned_type);
3129 mask = build_int_2 (~0, ~0);
3130 TREE_TYPE (mask) = unsigned_type;
3131 force_fit_type (mask, 0);
3132 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3133 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3135 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
3137 mask = fold (build (BIT_AND_EXPR, unsigned_type,
3138 convert (unsigned_type, and_mask), mask));
3141 *pand_mask = and_mask;
3145 /* Return non-zero if MASK represents a mask of SIZE ones in the low-order
3149 all_ones_mask_p (mask, size)
3153 tree type = TREE_TYPE (mask);
3154 int precision = TYPE_PRECISION (type);
3157 tmask = build_int_2 (~0, ~0);
3158 TREE_TYPE (tmask) = signed_type (type);
3159 force_fit_type (tmask, 0);
3161 tree_int_cst_equal (mask,
3162 const_binop (RSHIFT_EXPR,
3163 const_binop (LSHIFT_EXPR, tmask,
3164 size_int (precision - size),
3166 size_int (precision - size), 0));
3169 /* Subroutine for fold_truthop: determine if an operand is simple enough
3170 to be evaluated unconditionally. */
3173 simple_operand_p (exp)
3176 /* Strip any conversions that don't change the machine mode. */
3177 while ((TREE_CODE (exp) == NOP_EXPR
3178 || TREE_CODE (exp) == CONVERT_EXPR)
3179 && (TYPE_MODE (TREE_TYPE (exp))
3180 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)))))
3181 exp = TREE_OPERAND (exp, 0);
3183 return (TREE_CODE_CLASS (TREE_CODE (exp)) == 'c'
3184 || (TREE_CODE_CLASS (TREE_CODE (exp)) == 'd'
3185 && ! TREE_ADDRESSABLE (exp)
3186 && ! TREE_THIS_VOLATILE (exp)
3187 && ! DECL_NONLOCAL (exp)
3188 /* Don't regard global variables as simple. They may be
3189 allocated in ways unknown to the compiler (shared memory,
3190 #pragma weak, etc). */
3191 && ! TREE_PUBLIC (exp)
3192 && ! DECL_EXTERNAL (exp)
3193 /* Loading a static variable is unduly expensive, but global
3194 registers aren't expensive. */
3195 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
3198 /* The following functions are subroutines to fold_range_test and allow it to
3199 try to change a logical combination of comparisons into a range test.
3202 X == 2 && X == 3 && X == 4 && X == 5
3206 (unsigned) (X - 2) <= 3
3208 We describe each set of comparisons as being either inside or outside
3209 a range, using a variable named like IN_P, and then describe the
3210 range with a lower and upper bound. If one of the bounds is omitted,
3211 it represents either the highest or lowest value of the type.
3213 In the comments below, we represent a range by two numbers in brackets
3214 preceded by a "+" to designate being inside that range, or a "-" to
3215 designate being outside that range, so the condition can be inverted by
3216 flipping the prefix. An omitted bound is represented by a "-". For
3217 example, "- [-, 10]" means being outside the range starting at the lowest
3218 possible value and ending at 10, in other words, being greater than 10.
3219 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
3222 We set up things so that the missing bounds are handled in a consistent
3223 manner so neither a missing bound nor "true" and "false" need to be
3224 handled using a special case. */
3226 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
3227 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
3228 and UPPER1_P are nonzero if the respective argument is an upper bound
3229 and zero for a lower. TYPE, if nonzero, is the type of the result; it
3230 must be specified for a comparison. ARG1 will be converted to ARG0's
3231 type if both are specified. */
3234 range_binop (code, type, arg0, upper0_p, arg1, upper1_p)
3235 enum tree_code code;
3238 int upper0_p, upper1_p;
3244 /* If neither arg represents infinity, do the normal operation.
3245 Else, if not a comparison, return infinity. Else handle the special
3246 comparison rules. Note that most of the cases below won't occur, but
3247 are handled for consistency. */
3249 if (arg0 != 0 && arg1 != 0)
3251 tem = fold (build (code, type != 0 ? type : TREE_TYPE (arg0),
3252 arg0, convert (TREE_TYPE (arg0), arg1)));
3254 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
3257 if (TREE_CODE_CLASS (code) != '<')
3260 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
3261 for neither. In real maths, we cannot assume open ended ranges are
3262 the same. But, this is computer arithmetic, where numbers are finite.
3263 We can therefore make the transformation of any unbounded range with
3264 the value Z, Z being greater than any representable number. This permits
3265 us to treat unbounded ranges as equal. */
3266 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
3267 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
3271 result = sgn0 == sgn1;
3274 result = sgn0 != sgn1;
3277 result = sgn0 < sgn1;
3280 result = sgn0 <= sgn1;
3283 result = sgn0 > sgn1;
3286 result = sgn0 >= sgn1;
3292 return convert (type, result ? integer_one_node : integer_zero_node);
3295 /* Given EXP, a logical expression, set the range it is testing into
3296 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
3297 actually being tested. *PLOW and *PHIGH will have be made the same type
3298 as the returned expression. If EXP is not a comparison, we will most
3299 likely not be returning a useful value and range. */
3302 make_range (exp, pin_p, plow, phigh)
3307 enum tree_code code;
3308 tree arg0 = NULL_TREE, arg1 = NULL_TREE, type = NULL_TREE;
3309 tree orig_type = NULL_TREE;
3311 tree low, high, n_low, n_high;
3313 /* Start with simply saying "EXP != 0" and then look at the code of EXP
3314 and see if we can refine the range. Some of the cases below may not
3315 happen, but it doesn't seem worth worrying about this. We "continue"
3316 the outer loop when we've changed something; otherwise we "break"
3317 the switch, which will "break" the while. */
3319 in_p = 0, low = high = convert (TREE_TYPE (exp), integer_zero_node);
3323 code = TREE_CODE (exp);
3325 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
3327 arg0 = TREE_OPERAND (exp, 0);
3328 if (TREE_CODE_CLASS (code) == '<'
3329 || TREE_CODE_CLASS (code) == '1'
3330 || TREE_CODE_CLASS (code) == '2')
3331 type = TREE_TYPE (arg0);
3332 if (TREE_CODE_CLASS (code) == '2'
3333 || TREE_CODE_CLASS (code) == '<'
3334 || (TREE_CODE_CLASS (code) == 'e'
3335 && tree_code_length[(int) code] > 1))
3336 arg1 = TREE_OPERAND (exp, 1);
3339 /* Set ORIG_TYPE as soon as TYPE is non-null so that we do not
3340 lose a cast by accident. */
3341 if (type != NULL_TREE && orig_type == NULL_TREE)
3346 case TRUTH_NOT_EXPR:
3347 in_p = ! in_p, exp = arg0;
3350 case EQ_EXPR: case NE_EXPR:
3351 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
3352 /* We can only do something if the range is testing for zero
3353 and if the second operand is an integer constant. Note that
3354 saying something is "in" the range we make is done by
3355 complementing IN_P since it will set in the initial case of
3356 being not equal to zero; "out" is leaving it alone. */
3357 if (low == 0 || high == 0
3358 || ! integer_zerop (low) || ! integer_zerop (high)
3359 || TREE_CODE (arg1) != INTEGER_CST)
3364 case NE_EXPR: /* - [c, c] */
3367 case EQ_EXPR: /* + [c, c] */
3368 in_p = ! in_p, low = high = arg1;
3370 case GT_EXPR: /* - [-, c] */
3371 low = 0, high = arg1;
3373 case GE_EXPR: /* + [c, -] */
3374 in_p = ! in_p, low = arg1, high = 0;
3376 case LT_EXPR: /* - [c, -] */
3377 low = arg1, high = 0;
3379 case LE_EXPR: /* + [-, c] */
3380 in_p = ! in_p, low = 0, high = arg1;
3388 /* If this is an unsigned comparison, we also know that EXP is
3389 greater than or equal to zero. We base the range tests we make
3390 on that fact, so we record it here so we can parse existing
3392 if (TREE_UNSIGNED (type) && (low == 0 || high == 0))
3394 if (! merge_ranges (&n_in_p, &n_low, &n_high, in_p, low, high,
3395 1, convert (type, integer_zero_node),
3399 in_p = n_in_p, low = n_low, high = n_high;
3401 /* If the high bound is missing, but we
3402 have a low bound, reverse the range so
3403 it goes from zero to the low bound minus 1. */
3404 if (high == 0 && low)
3407 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
3408 integer_one_node, 0);
3409 low = convert (type, integer_zero_node);
3415 /* (-x) IN [a,b] -> x in [-b, -a] */
3416 n_low = range_binop (MINUS_EXPR, type,
3417 convert (type, integer_zero_node), 0, high, 1);
3418 n_high = range_binop (MINUS_EXPR, type,
3419 convert (type, integer_zero_node), 0, low, 0);
3420 low = n_low, high = n_high;
3426 exp = build (MINUS_EXPR, type, negate_expr (arg0),
3427 convert (type, integer_one_node));
3430 case PLUS_EXPR: case MINUS_EXPR:
3431 if (TREE_CODE (arg1) != INTEGER_CST)
3434 /* If EXP is signed, any overflow in the computation is undefined,
3435 so we don't worry about it so long as our computations on
3436 the bounds don't overflow. For unsigned, overflow is defined
3437 and this is exactly the right thing. */
3438 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3439 type, low, 0, arg1, 0);
3440 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3441 type, high, 1, arg1, 0);
3442 if ((n_low != 0 && TREE_OVERFLOW (n_low))
3443 || (n_high != 0 && TREE_OVERFLOW (n_high)))
3446 /* Check for an unsigned range which has wrapped around the maximum
3447 value thus making n_high < n_low, and normalize it. */
3448 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
3450 low = range_binop (PLUS_EXPR, type, n_high, 0,
3451 integer_one_node, 0);
3452 high = range_binop (MINUS_EXPR, type, n_low, 0,
3453 integer_one_node, 0);
3457 low = n_low, high = n_high;
3462 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
3463 if (TYPE_PRECISION (type) > TYPE_PRECISION (orig_type))
3466 if (! INTEGRAL_TYPE_P (type)
3467 || (low != 0 && ! int_fits_type_p (low, type))
3468 || (high != 0 && ! int_fits_type_p (high, type)))
3471 n_low = low, n_high = high;
3474 n_low = convert (type, n_low);
3477 n_high = convert (type, n_high);
3479 /* If we're converting from an unsigned to a signed type,
3480 we will be doing the comparison as unsigned. The tests above
3481 have already verified that LOW and HIGH are both positive.
3483 So we have to make sure that the original unsigned value will
3484 be interpreted as positive. */
3485 if (TREE_UNSIGNED (type) && ! TREE_UNSIGNED (TREE_TYPE (exp)))
3487 tree equiv_type = type_for_mode (TYPE_MODE (type), 1);
3490 /* A range without an upper bound is, naturally, unbounded.
3491 Since convert would have cropped a very large value, use
3492 the max value for the destination type. */
3494 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
3495 : TYPE_MAX_VALUE (type);
3497 high_positive = fold (build (RSHIFT_EXPR, type,
3498 convert (type, high_positive),
3499 convert (type, integer_one_node)));
3501 /* If the low bound is specified, "and" the range with the
3502 range for which the original unsigned value will be
3506 if (! merge_ranges (&n_in_p, &n_low, &n_high,
3508 1, convert (type, integer_zero_node),
3512 in_p = (n_in_p == in_p);
3516 /* Otherwise, "or" the range with the range of the input
3517 that will be interpreted as negative. */
3518 if (! merge_ranges (&n_in_p, &n_low, &n_high,
3520 1, convert (type, integer_zero_node),
3524 in_p = (in_p != n_in_p);
3529 low = n_low, high = n_high;
3539 /* If EXP is a constant, we can evaluate whether this is true or false. */
3540 if (TREE_CODE (exp) == INTEGER_CST)
3542 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
3544 && integer_onep (range_binop (LE_EXPR, integer_type_node,
3550 *pin_p = in_p, *plow = low, *phigh = high;
3554 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
3555 type, TYPE, return an expression to test if EXP is in (or out of, depending
3556 on IN_P) the range. */
3559 build_range_check (type, exp, in_p, low, high)
3565 tree etype = TREE_TYPE (exp);
3569 && (0 != (value = build_range_check (type, exp, 1, low, high))))
3570 return invert_truthvalue (value);
3572 else if (low == 0 && high == 0)
3573 return convert (type, integer_one_node);
3576 return fold (build (LE_EXPR, type, exp, high));
3579 return fold (build (GE_EXPR, type, exp, low));
3581 else if (operand_equal_p (low, high, 0))
3582 return fold (build (EQ_EXPR, type, exp, low));
3584 else if (TREE_UNSIGNED (etype) && integer_zerop (low))
3585 return build_range_check (type, exp, 1, 0, high);
3587 else if (integer_zerop (low))
3589 utype = unsigned_type (etype);
3590 return build_range_check (type, convert (utype, exp), 1, 0,
3591 convert (utype, high));
3594 else if (0 != (value = const_binop (MINUS_EXPR, high, low, 0))
3595 && ! TREE_OVERFLOW (value))
3596 return build_range_check (type,
3597 fold (build (MINUS_EXPR, etype, exp, low)),
3598 1, convert (etype, integer_zero_node), value);
3603 /* Given two ranges, see if we can merge them into one. Return 1 if we
3604 can, 0 if we can't. Set the output range into the specified parameters. */
3607 merge_ranges (pin_p, plow, phigh, in0_p, low0, high0, in1_p, low1, high1)
3611 tree low0, high0, low1, high1;
3619 int lowequal = ((low0 == 0 && low1 == 0)
3620 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
3621 low0, 0, low1, 0)));
3622 int highequal = ((high0 == 0 && high1 == 0)
3623 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
3624 high0, 1, high1, 1)));
3626 /* Make range 0 be the range that starts first, or ends last if they
3627 start at the same value. Swap them if it isn't. */
3628 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
3631 && integer_onep (range_binop (GT_EXPR, integer_type_node,
3632 high1, 1, high0, 1))))
3634 temp = in0_p, in0_p = in1_p, in1_p = temp;
3635 tem = low0, low0 = low1, low1 = tem;
3636 tem = high0, high0 = high1, high1 = tem;
3639 /* Now flag two cases, whether the ranges are disjoint or whether the
3640 second range is totally subsumed in the first. Note that the tests
3641 below are simplified by the ones above. */
3642 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
3643 high0, 1, low1, 0));
3644 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
3645 high1, 1, high0, 1));
3647 /* We now have four cases, depending on whether we are including or
3648 excluding the two ranges. */
3651 /* If they don't overlap, the result is false. If the second range
3652 is a subset it is the result. Otherwise, the range is from the start
3653 of the second to the end of the first. */
3655 in_p = 0, low = high = 0;
3657 in_p = 1, low = low1, high = high1;
3659 in_p = 1, low = low1, high = high0;
3662 else if (in0_p && ! in1_p)
3664 /* If they don't overlap, the result is the first range. If they are
3665 equal, the result is false. If the second range is a subset of the
3666 first, and the ranges begin at the same place, we go from just after
3667 the end of the first range to the end of the second. If the second
3668 range is not a subset of the first, or if it is a subset and both
3669 ranges end at the same place, the range starts at the start of the
3670 first range and ends just before the second range.
3671 Otherwise, we can't describe this as a single range. */
3673 in_p = 1, low = low0, high = high0;
3674 else if (lowequal && highequal)
3675 in_p = 0, low = high = 0;
3676 else if (subset && lowequal)
3678 in_p = 1, high = high0;
3679 low = range_binop (PLUS_EXPR, NULL_TREE, high1, 0,
3680 integer_one_node, 0);
3682 else if (! subset || highequal)
3684 in_p = 1, low = low0;
3685 high = range_binop (MINUS_EXPR, NULL_TREE, low1, 0,
3686 integer_one_node, 0);
3692 else if (! in0_p && in1_p)
3694 /* If they don't overlap, the result is the second range. If the second
3695 is a subset of the first, the result is false. Otherwise,
3696 the range starts just after the first range and ends at the
3697 end of the second. */
3699 in_p = 1, low = low1, high = high1;
3700 else if (subset || highequal)
3701 in_p = 0, low = high = 0;
3704 in_p = 1, high = high1;
3705 low = range_binop (PLUS_EXPR, NULL_TREE, high0, 1,
3706 integer_one_node, 0);
3712 /* The case where we are excluding both ranges. Here the complex case
3713 is if they don't overlap. In that case, the only time we have a
3714 range is if they are adjacent. If the second is a subset of the
3715 first, the result is the first. Otherwise, the range to exclude
3716 starts at the beginning of the first range and ends at the end of the
3720 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
3721 range_binop (PLUS_EXPR, NULL_TREE,
3723 integer_one_node, 1),
3725 in_p = 0, low = low0, high = high1;
3730 in_p = 0, low = low0, high = high0;
3732 in_p = 0, low = low0, high = high1;
3735 *pin_p = in_p, *plow = low, *phigh = high;
3739 /* EXP is some logical combination of boolean tests. See if we can
3740 merge it into some range test. Return the new tree if so. */
3743 fold_range_test (exp)
3746 int or_op = (TREE_CODE (exp) == TRUTH_ORIF_EXPR
3747 || TREE_CODE (exp) == TRUTH_OR_EXPR);
3748 int in0_p, in1_p, in_p;
3749 tree low0, low1, low, high0, high1, high;
3750 tree lhs = make_range (TREE_OPERAND (exp, 0), &in0_p, &low0, &high0);
3751 tree rhs = make_range (TREE_OPERAND (exp, 1), &in1_p, &low1, &high1);
3754 /* If this is an OR operation, invert both sides; we will invert
3755 again at the end. */
3757 in0_p = ! in0_p, in1_p = ! in1_p;
3759 /* If both expressions are the same, if we can merge the ranges, and we
3760 can build the range test, return it or it inverted. If one of the
3761 ranges is always true or always false, consider it to be the same
3762 expression as the other. */
3763 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
3764 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
3766 && 0 != (tem = (build_range_check (TREE_TYPE (exp),
3768 : rhs != 0 ? rhs : integer_zero_node,
3770 return or_op ? invert_truthvalue (tem) : tem;
3772 /* On machines where the branch cost is expensive, if this is a
3773 short-circuited branch and the underlying object on both sides
3774 is the same, make a non-short-circuit operation. */
3775 else if (BRANCH_COST >= 2
3776 && (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
3777 || TREE_CODE (exp) == TRUTH_ORIF_EXPR)
3778 && operand_equal_p (lhs, rhs, 0))
3780 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
3781 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
3782 which cases we can't do this. */
3783 if (simple_operand_p (lhs))
3784 return build (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
3785 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
3786 TREE_TYPE (exp), TREE_OPERAND (exp, 0),
3787 TREE_OPERAND (exp, 1));
3789 else if (global_bindings_p () == 0
3790 && ! contains_placeholder_p (lhs))
3792 tree common = save_expr (lhs);
3794 if (0 != (lhs = build_range_check (TREE_TYPE (exp), common,
3795 or_op ? ! in0_p : in0_p,
3797 && (0 != (rhs = build_range_check (TREE_TYPE (exp), common,
3798 or_op ? ! in1_p : in1_p,
3800 return build (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
3801 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
3802 TREE_TYPE (exp), lhs, rhs);
3809 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
3810 bit value. Arrange things so the extra bits will be set to zero if and
3811 only if C is signed-extended to its full width. If MASK is nonzero,
3812 it is an INTEGER_CST that should be AND'ed with the extra bits. */
3815 unextend (c, p, unsignedp, mask)
3821 tree type = TREE_TYPE (c);
3822 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
3825 if (p == modesize || unsignedp)
3828 /* We work by getting just the sign bit into the low-order bit, then
3829 into the high-order bit, then sign-extend. We then XOR that value
3831 temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
3832 temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
3834 /* We must use a signed type in order to get an arithmetic right shift.
3835 However, we must also avoid introducing accidental overflows, so that
3836 a subsequent call to integer_zerop will work. Hence we must
3837 do the type conversion here. At this point, the constant is either
3838 zero or one, and the conversion to a signed type can never overflow.
3839 We could get an overflow if this conversion is done anywhere else. */
3840 if (TREE_UNSIGNED (type))
3841 temp = convert (signed_type (type), temp);
3843 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
3844 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
3846 temp = const_binop (BIT_AND_EXPR, temp, convert (TREE_TYPE (c), mask), 0);
3847 /* If necessary, convert the type back to match the type of C. */
3848 if (TREE_UNSIGNED (type))
3849 temp = convert (type, temp);
3851 return convert (type, const_binop (BIT_XOR_EXPR, c, temp, 0));
3854 /* Find ways of folding logical expressions of LHS and RHS:
3855 Try to merge two comparisons to the same innermost item.
3856 Look for range tests like "ch >= '0' && ch <= '9'".
3857 Look for combinations of simple terms on machines with expensive branches
3858 and evaluate the RHS unconditionally.
3860 For example, if we have p->a == 2 && p->b == 4 and we can make an
3861 object large enough to span both A and B, we can do this with a comparison
3862 against the object ANDed with the a mask.
3864 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
3865 operations to do this with one comparison.
3867 We check for both normal comparisons and the BIT_AND_EXPRs made this by
3868 function and the one above.
3870 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
3871 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
3873 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
3876 We return the simplified tree or 0 if no optimization is possible. */
3879 fold_truthop (code, truth_type, lhs, rhs)
3880 enum tree_code code;
3881 tree truth_type, lhs, rhs;
3883 /* If this is the "or" of two comparisons, we can do something if we
3884 the comparisons are NE_EXPR. If this is the "and", we can do something
3885 if the comparisons are EQ_EXPR. I.e.,
3886 (a->b == 2 && a->c == 4) can become (a->new == NEW).
3888 WANTED_CODE is this operation code. For single bit fields, we can
3889 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
3890 comparison for one-bit fields. */
3892 enum tree_code wanted_code;
3893 enum tree_code lcode, rcode;
3894 tree ll_arg, lr_arg, rl_arg, rr_arg;
3895 tree ll_inner, lr_inner, rl_inner, rr_inner;
3896 int ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
3897 int rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
3898 int xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
3899 int lnbitsize, lnbitpos, rnbitsize, rnbitpos;
3900 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
3901 enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
3902 enum machine_mode lnmode, rnmode;
3903 tree ll_mask, lr_mask, rl_mask, rr_mask;
3904 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
3905 tree l_const, r_const;
3906 tree lntype, rntype, result;
3907 int first_bit, end_bit;
3910 /* Start by getting the comparison codes. Fail if anything is volatile.
3911 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
3912 it were surrounded with a NE_EXPR. */
3914 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
3917 lcode = TREE_CODE (lhs);
3918 rcode = TREE_CODE (rhs);
3920 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
3921 lcode = NE_EXPR, lhs = build (NE_EXPR, truth_type, lhs, integer_zero_node);
3923 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
3924 rcode = NE_EXPR, rhs = build (NE_EXPR, truth_type, rhs, integer_zero_node);
3926 if (TREE_CODE_CLASS (lcode) != '<' || TREE_CODE_CLASS (rcode) != '<')
3929 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
3930 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
3932 ll_arg = TREE_OPERAND (lhs, 0);
3933 lr_arg = TREE_OPERAND (lhs, 1);
3934 rl_arg = TREE_OPERAND (rhs, 0);
3935 rr_arg = TREE_OPERAND (rhs, 1);
3937 /* If the RHS can be evaluated unconditionally and its operands are
3938 simple, it wins to evaluate the RHS unconditionally on machines
3939 with expensive branches. In this case, this isn't a comparison
3940 that can be merged. Avoid doing this if the RHS is a floating-point
3941 comparison since those can trap. */
3943 if (BRANCH_COST >= 2
3944 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
3945 && simple_operand_p (rl_arg)
3946 && simple_operand_p (rr_arg))
3947 return build (code, truth_type, lhs, rhs);
3949 /* See if the comparisons can be merged. Then get all the parameters for
3952 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
3953 || (rcode != EQ_EXPR && rcode != NE_EXPR))
3957 ll_inner = decode_field_reference (ll_arg,
3958 &ll_bitsize, &ll_bitpos, &ll_mode,
3959 &ll_unsignedp, &volatilep, &ll_mask,
3961 lr_inner = decode_field_reference (lr_arg,
3962 &lr_bitsize, &lr_bitpos, &lr_mode,
3963 &lr_unsignedp, &volatilep, &lr_mask,
3965 rl_inner = decode_field_reference (rl_arg,
3966 &rl_bitsize, &rl_bitpos, &rl_mode,
3967 &rl_unsignedp, &volatilep, &rl_mask,
3969 rr_inner = decode_field_reference (rr_arg,
3970 &rr_bitsize, &rr_bitpos, &rr_mode,
3971 &rr_unsignedp, &volatilep, &rr_mask,
3974 /* It must be true that the inner operation on the lhs of each
3975 comparison must be the same if we are to be able to do anything.
3976 Then see if we have constants. If not, the same must be true for
3978 if (volatilep || ll_inner == 0 || rl_inner == 0
3979 || ! operand_equal_p (ll_inner, rl_inner, 0))
3982 if (TREE_CODE (lr_arg) == INTEGER_CST
3983 && TREE_CODE (rr_arg) == INTEGER_CST)
3984 l_const = lr_arg, r_const = rr_arg;
3985 else if (lr_inner == 0 || rr_inner == 0
3986 || ! operand_equal_p (lr_inner, rr_inner, 0))
3989 l_const = r_const = 0;
3991 /* If either comparison code is not correct for our logical operation,
3992 fail. However, we can convert a one-bit comparison against zero into
3993 the opposite comparison against that bit being set in the field. */
3995 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
3996 if (lcode != wanted_code)
3998 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
4000 /* Make the left operand unsigned, since we are only interested
4001 in the value of one bit. Otherwise we are doing the wrong
4010 /* This is analogous to the code for l_const above. */
4011 if (rcode != wanted_code)
4013 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
4022 /* See if we can find a mode that contains both fields being compared on
4023 the left. If we can't, fail. Otherwise, update all constants and masks
4024 to be relative to a field of that size. */
4025 first_bit = MIN (ll_bitpos, rl_bitpos);
4026 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
4027 lnmode = get_best_mode (end_bit - first_bit, first_bit,
4028 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
4030 if (lnmode == VOIDmode)
4033 lnbitsize = GET_MODE_BITSIZE (lnmode);
4034 lnbitpos = first_bit & ~ (lnbitsize - 1);
4035 lntype = type_for_size (lnbitsize, 1);
4036 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
4038 if (BYTES_BIG_ENDIAN)
4040 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
4041 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
4044 ll_mask = const_binop (LSHIFT_EXPR, convert (lntype, ll_mask),
4045 size_int (xll_bitpos), 0);
4046 rl_mask = const_binop (LSHIFT_EXPR, convert (lntype, rl_mask),
4047 size_int (xrl_bitpos), 0);
4051 l_const = convert (lntype, l_const);
4052 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
4053 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
4054 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
4055 fold (build1 (BIT_NOT_EXPR,
4059 warning ("comparison is always %d", wanted_code == NE_EXPR);
4061 return convert (truth_type,
4062 wanted_code == NE_EXPR
4063 ? integer_one_node : integer_zero_node);
4068 r_const = convert (lntype, r_const);
4069 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
4070 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
4071 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
4072 fold (build1 (BIT_NOT_EXPR,
4076 warning ("comparison is always %d", wanted_code == NE_EXPR);
4078 return convert (truth_type,
4079 wanted_code == NE_EXPR
4080 ? integer_one_node : integer_zero_node);
4084 /* If the right sides are not constant, do the same for it. Also,
4085 disallow this optimization if a size or signedness mismatch occurs
4086 between the left and right sides. */
4089 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
4090 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
4091 /* Make sure the two fields on the right
4092 correspond to the left without being swapped. */
4093 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
4096 first_bit = MIN (lr_bitpos, rr_bitpos);
4097 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
4098 rnmode = get_best_mode (end_bit - first_bit, first_bit,
4099 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
4101 if (rnmode == VOIDmode)
4104 rnbitsize = GET_MODE_BITSIZE (rnmode);
4105 rnbitpos = first_bit & ~ (rnbitsize - 1);
4106 rntype = type_for_size (rnbitsize, 1);
4107 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
4109 if (BYTES_BIG_ENDIAN)
4111 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
4112 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
4115 lr_mask = const_binop (LSHIFT_EXPR, convert (rntype, lr_mask),
4116 size_int (xlr_bitpos), 0);
4117 rr_mask = const_binop (LSHIFT_EXPR, convert (rntype, rr_mask),
4118 size_int (xrr_bitpos), 0);
4120 /* Make a mask that corresponds to both fields being compared.
4121 Do this for both items being compared. If the operands are the
4122 same size and the bits being compared are in the same position
4123 then we can do this by masking both and comparing the masked
4125 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
4126 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0);
4127 if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
4129 lhs = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
4130 ll_unsignedp || rl_unsignedp);
4131 if (! all_ones_mask_p (ll_mask, lnbitsize))
4132 lhs = build (BIT_AND_EXPR, lntype, lhs, ll_mask);
4134 rhs = make_bit_field_ref (lr_inner, rntype, rnbitsize, rnbitpos,
4135 lr_unsignedp || rr_unsignedp);
4136 if (! all_ones_mask_p (lr_mask, rnbitsize))
4137 rhs = build (BIT_AND_EXPR, rntype, rhs, lr_mask);
4139 return build (wanted_code, truth_type, lhs, rhs);
4142 /* There is still another way we can do something: If both pairs of
4143 fields being compared are adjacent, we may be able to make a wider
4144 field containing them both.
4146 Note that we still must mask the lhs/rhs expressions. Furthermore,
4147 the mask must be shifted to account for the shift done by
4148 make_bit_field_ref. */
4149 if ((ll_bitsize + ll_bitpos == rl_bitpos
4150 && lr_bitsize + lr_bitpos == rr_bitpos)
4151 || (ll_bitpos == rl_bitpos + rl_bitsize
4152 && lr_bitpos == rr_bitpos + rr_bitsize))
4156 lhs = make_bit_field_ref (ll_inner, lntype, ll_bitsize + rl_bitsize,
4157 MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
4158 rhs = make_bit_field_ref (lr_inner, rntype, lr_bitsize + rr_bitsize,
4159 MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
4161 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
4162 size_int (MIN (xll_bitpos, xrl_bitpos)), 0);
4163 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
4164 size_int (MIN (xlr_bitpos, xrr_bitpos)), 0);
4166 /* Convert to the smaller type before masking out unwanted bits. */
4168 if (lntype != rntype)
4170 if (lnbitsize > rnbitsize)
4172 lhs = convert (rntype, lhs);
4173 ll_mask = convert (rntype, ll_mask);
4176 else if (lnbitsize < rnbitsize)
4178 rhs = convert (lntype, rhs);
4179 lr_mask = convert (lntype, lr_mask);
4184 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
4185 lhs = build (BIT_AND_EXPR, type, lhs, ll_mask);
4187 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
4188 rhs = build (BIT_AND_EXPR, type, rhs, lr_mask);
4190 return build (wanted_code, truth_type, lhs, rhs);
4196 /* Handle the case of comparisons with constants. If there is something in
4197 common between the masks, those bits of the constants must be the same.
4198 If not, the condition is always false. Test for this to avoid generating
4199 incorrect code below. */
4200 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
4201 if (! integer_zerop (result)
4202 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
4203 const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
4205 if (wanted_code == NE_EXPR)
4207 warning ("`or' of unmatched not-equal tests is always 1");
4208 return convert (truth_type, integer_one_node);
4212 warning ("`and' of mutually exclusive equal-tests is always 0");
4213 return convert (truth_type, integer_zero_node);
4217 /* Construct the expression we will return. First get the component
4218 reference we will make. Unless the mask is all ones the width of
4219 that field, perform the mask operation. Then compare with the
4221 result = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
4222 ll_unsignedp || rl_unsignedp);
4224 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
4225 if (! all_ones_mask_p (ll_mask, lnbitsize))
4226 result = build (BIT_AND_EXPR, lntype, result, ll_mask);
4228 return build (wanted_code, truth_type, result,
4229 const_binop (BIT_IOR_EXPR, l_const, r_const, 0));
4232 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
4236 optimize_minmax_comparison (t)
4239 tree type = TREE_TYPE (t);
4240 tree arg0 = TREE_OPERAND (t, 0);
4241 enum tree_code op_code;
4242 tree comp_const = TREE_OPERAND (t, 1);
4244 int consts_equal, consts_lt;
4247 STRIP_SIGN_NOPS (arg0);
4249 op_code = TREE_CODE (arg0);
4250 minmax_const = TREE_OPERAND (arg0, 1);
4251 consts_equal = tree_int_cst_equal (minmax_const, comp_const);
4252 consts_lt = tree_int_cst_lt (minmax_const, comp_const);
4253 inner = TREE_OPERAND (arg0, 0);
4255 /* If something does not permit us to optimize, return the original tree. */
4256 if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
4257 || TREE_CODE (comp_const) != INTEGER_CST
4258 || TREE_CONSTANT_OVERFLOW (comp_const)
4259 || TREE_CODE (minmax_const) != INTEGER_CST
4260 || TREE_CONSTANT_OVERFLOW (minmax_const))
4263 /* Now handle all the various comparison codes. We only handle EQ_EXPR
4264 and GT_EXPR, doing the rest with recursive calls using logical
4266 switch (TREE_CODE (t))
4268 case NE_EXPR: case LT_EXPR: case LE_EXPR:
4270 invert_truthvalue (optimize_minmax_comparison (invert_truthvalue (t)));
4274 fold (build (TRUTH_ORIF_EXPR, type,
4275 optimize_minmax_comparison
4276 (build (EQ_EXPR, type, arg0, comp_const)),
4277 optimize_minmax_comparison
4278 (build (GT_EXPR, type, arg0, comp_const))));
4281 if (op_code == MAX_EXPR && consts_equal)
4282 /* MAX (X, 0) == 0 -> X <= 0 */
4283 return fold (build (LE_EXPR, type, inner, comp_const));
4285 else if (op_code == MAX_EXPR && consts_lt)
4286 /* MAX (X, 0) == 5 -> X == 5 */
4287 return fold (build (EQ_EXPR, type, inner, comp_const));
4289 else if (op_code == MAX_EXPR)
4290 /* MAX (X, 0) == -1 -> false */
4291 return omit_one_operand (type, integer_zero_node, inner);
4293 else if (consts_equal)
4294 /* MIN (X, 0) == 0 -> X >= 0 */
4295 return fold (build (GE_EXPR, type, inner, comp_const));
4298 /* MIN (X, 0) == 5 -> false */
4299 return omit_one_operand (type, integer_zero_node, inner);
4302 /* MIN (X, 0) == -1 -> X == -1 */
4303 return fold (build (EQ_EXPR, type, inner, comp_const));
4306 if (op_code == MAX_EXPR && (consts_equal || consts_lt))
4307 /* MAX (X, 0) > 0 -> X > 0
4308 MAX (X, 0) > 5 -> X > 5 */
4309 return fold (build (GT_EXPR, type, inner, comp_const));
4311 else if (op_code == MAX_EXPR)
4312 /* MAX (X, 0) > -1 -> true */
4313 return omit_one_operand (type, integer_one_node, inner);
4315 else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
4316 /* MIN (X, 0) > 0 -> false
4317 MIN (X, 0) > 5 -> false */
4318 return omit_one_operand (type, integer_zero_node, inner);
4321 /* MIN (X, 0) > -1 -> X > -1 */
4322 return fold (build (GT_EXPR, type, inner, comp_const));
4329 /* T is an integer expression that is being multiplied, divided, or taken a
4330 modulus (CODE says which and what kind of divide or modulus) by a
4331 constant C. See if we can eliminate that operation by folding it with
4332 other operations already in T. WIDE_TYPE, if non-null, is a type that
4333 should be used for the computation if wider than our type.
4335 For example, if we are dividing (X * 8) + (Y + 16) by 4, we can return
4336 (X * 2) + (Y + 4). We also canonicalize (X + 7) * 4 into X * 4 + 28
4337 in the hope that either the machine has a multiply-accumulate insn
4338 or that this is part of an addressing calculation.
4340 If we return a non-null expression, it is an equivalent form of the
4341 original computation, but need not be in the original type. */
4344 extract_muldiv (t, c, code, wide_type)
4347 enum tree_code code;
4350 tree type = TREE_TYPE (t);
4351 enum tree_code tcode = TREE_CODE (t);
4352 tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
4353 > GET_MODE_SIZE (TYPE_MODE (type)))
4354 ? wide_type : type);
4356 int same_p = tcode == code;
4357 tree op0 = NULL_TREE, op1 = NULL_TREE;
4359 /* Don't deal with constants of zero here; they confuse the code below. */
4360 if (integer_zerop (c))
4363 if (TREE_CODE_CLASS (tcode) == '1')
4364 op0 = TREE_OPERAND (t, 0);
4366 if (TREE_CODE_CLASS (tcode) == '2')
4367 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
4369 /* Note that we need not handle conditional operations here since fold
4370 already handles those cases. So just do arithmetic here. */
4374 /* For a constant, we can always simplify if we are a multiply
4375 or (for divide and modulus) if it is a multiple of our constant. */
4376 if (code == MULT_EXPR
4377 || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c, 0)))
4378 return const_binop (code, convert (ctype, t), convert (ctype, c), 0);
4381 case CONVERT_EXPR: case NON_LVALUE_EXPR: case NOP_EXPR:
4383 /* Pass the constant down and see if we can make a simplification. If
4384 we can, replace this expression with the inner simplification for
4385 possible later conversion to our or some other type. */
4386 if (0 != (t1 = extract_muldiv (op0, convert (TREE_TYPE (op0), c), code,
4387 code == MULT_EXPR ? ctype : NULL_TREE)))
4391 case NEGATE_EXPR: case ABS_EXPR:
4392 if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
4393 return fold (build1 (tcode, ctype, convert (ctype, t1)));
4396 case MIN_EXPR: case MAX_EXPR:
4397 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
4398 if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0
4399 && (t2 = extract_muldiv (op1, c, code, wide_type)) != 0)
4401 if (tree_int_cst_sgn (c) < 0)
4402 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
4404 return fold (build (tcode, ctype, convert (ctype, t1),
4405 convert (ctype, t2)));
4409 case WITH_RECORD_EXPR:
4410 if ((t1 = extract_muldiv (TREE_OPERAND (t, 0), c, code, wide_type)) != 0)
4411 return build (WITH_RECORD_EXPR, TREE_TYPE (t1), t1,
4412 TREE_OPERAND (t, 1));
4416 /* If this has not been evaluated and the operand has no side effects,
4417 we can see if we can do something inside it and make a new one.
4418 Note that this test is overly conservative since we can do this
4419 if the only reason it had side effects is that it was another
4420 similar SAVE_EXPR, but that isn't worth bothering with. */
4421 if (SAVE_EXPR_RTL (t) == 0 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0))
4422 && 0 != (t1 = extract_muldiv (TREE_OPERAND (t, 0), c, code,
4424 return save_expr (t1);
4427 case LSHIFT_EXPR: case RSHIFT_EXPR:
4428 /* If the second operand is constant, this is a multiplication
4429 or floor division, by a power of two, so we can treat it that
4430 way unless the multiplier or divisor overflows. */
4431 if (TREE_CODE (op1) == INTEGER_CST
4432 && 0 != (t1 = convert (ctype,
4433 const_binop (LSHIFT_EXPR, size_one_node,
4435 && ! TREE_OVERFLOW (t1))
4436 return extract_muldiv (build (tcode == LSHIFT_EXPR
4437 ? MULT_EXPR : FLOOR_DIV_EXPR,
4438 ctype, convert (ctype, op0), t1),
4439 c, code, wide_type);
4442 case PLUS_EXPR: case MINUS_EXPR:
4443 /* See if we can eliminate the operation on both sides. If we can, we
4444 can return a new PLUS or MINUS. If we can't, the only remaining
4445 cases where we can do anything are if the second operand is a
4447 t1 = extract_muldiv (op0, c, code, wide_type);
4448 t2 = extract_muldiv (op1, c, code, wide_type);
4449 if (t1 != 0 && t2 != 0)
4450 return fold (build (tcode, ctype, convert (ctype, t1),
4451 convert (ctype, t2)));
4453 /* If this was a subtraction, negate OP1 and set it to be an addition.
4454 This simplifies the logic below. */
4455 if (tcode == MINUS_EXPR)
4456 tcode = PLUS_EXPR, op1 = negate_expr (op1);
4458 if (TREE_CODE (op1) != INTEGER_CST)
4461 /* If either OP1 or C are negative, this optimization is not safe for
4462 some of the division and remainder types while for others we need
4463 to change the code. */
4464 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
4466 if (code == CEIL_DIV_EXPR)
4467 code = FLOOR_DIV_EXPR;
4468 else if (code == CEIL_MOD_EXPR)
4469 code = FLOOR_MOD_EXPR;
4470 else if (code == FLOOR_DIV_EXPR)
4471 code = CEIL_DIV_EXPR;
4472 else if (code == FLOOR_MOD_EXPR)
4473 code = CEIL_MOD_EXPR;
4474 else if (code != MULT_EXPR)
4478 /* Now do the operation and verify it doesn't overflow. */
4479 op1 = const_binop (code, convert (ctype, op1), convert (ctype, c), 0);
4480 if (op1 == 0 || TREE_OVERFLOW (op1))
4483 /* If we were able to eliminate our operation from the first side,
4484 apply our operation to the second side and reform the PLUS. */
4485 if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
4486 return fold (build (tcode, ctype, convert (ctype, t1), op1));
4488 /* The last case is if we are a multiply. In that case, we can
4489 apply the distributive law to commute the multiply and addition
4490 if the multiplication of the constants doesn't overflow. */
4491 if (code == MULT_EXPR)
4492 return fold (build (tcode, ctype, fold (build (code, ctype,
4493 convert (ctype, op0),
4494 convert (ctype, c))),
4500 /* We have a special case here if we are doing something like
4501 (C * 8) % 4 since we know that's zero. */
4502 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
4503 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
4504 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
4505 && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
4506 return omit_one_operand (type, integer_zero_node, op0);
4508 /* ... fall through ... */
4510 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
4511 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
4512 /* If we can extract our operation from the LHS, do so and return a
4513 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
4514 do something only if the second operand is a constant. */
4516 && (t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
4517 return fold (build (tcode, ctype, convert (ctype, t1),
4518 convert (ctype, op1)));
4519 else if (tcode == MULT_EXPR && code == MULT_EXPR
4520 && (t1 = extract_muldiv (op1, c, code, wide_type)) != 0)
4521 return fold (build (tcode, ctype, convert (ctype, op0),
4522 convert (ctype, t1)));
4523 else if (TREE_CODE (op1) != INTEGER_CST)
4526 /* If these are the same operation types, we can associate them
4527 assuming no overflow. */
4529 && 0 != (t1 = const_binop (MULT_EXPR, convert (ctype, op1),
4530 convert (ctype, c), 0))
4531 && ! TREE_OVERFLOW (t1))
4532 return fold (build (tcode, ctype, convert (ctype, op0), t1));
4534 /* If these operations "cancel" each other, we have the main
4535 optimizations of this pass, which occur when either constant is a
4536 multiple of the other, in which case we replace this with either an
4537 operation or CODE or TCODE. If we have an unsigned type that is
4538 not a sizetype, we canot do this for division since it will change
4539 the result if the original computation overflowed. */
4540 if ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR
4541 && (! TREE_UNSIGNED (ctype)
4542 || (TREE_CODE (ctype) == INTEGER_TYPE
4543 && TYPE_IS_SIZETYPE (ctype))))
4544 || (tcode == MULT_EXPR
4545 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
4546 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR))
4548 if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
4549 return fold (build (tcode, ctype, convert (ctype, op0),
4551 const_binop (TRUNC_DIV_EXPR,
4553 else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1, 0)))
4554 return fold (build (code, ctype, convert (ctype, op0),
4556 const_binop (TRUNC_DIV_EXPR,
4568 /* If T contains a COMPOUND_EXPR which was inserted merely to evaluate
4569 S, a SAVE_EXPR, return the expression actually being evaluated. Note
4570 that we may sometimes modify the tree. */
4573 strip_compound_expr (t, s)
4577 enum tree_code code = TREE_CODE (t);
4579 /* See if this is the COMPOUND_EXPR we want to eliminate. */
4580 if (code == COMPOUND_EXPR && TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR
4581 && TREE_OPERAND (TREE_OPERAND (t, 0), 0) == s)
4582 return TREE_OPERAND (t, 1);
4584 /* See if this is a COND_EXPR or a simple arithmetic operator. We
4585 don't bother handling any other types. */
4586 else if (code == COND_EXPR)
4588 TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s);
4589 TREE_OPERAND (t, 1) = strip_compound_expr (TREE_OPERAND (t, 1), s);
4590 TREE_OPERAND (t, 2) = strip_compound_expr (TREE_OPERAND (t, 2), s);
4592 else if (TREE_CODE_CLASS (code) == '1')
4593 TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s);
4594 else if (TREE_CODE_CLASS (code) == '<'
4595 || TREE_CODE_CLASS (code) == '2')
4597 TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s);
4598 TREE_OPERAND (t, 1) = strip_compound_expr (TREE_OPERAND (t, 1), s);
4604 /* Return a node which has the indicated constant VALUE (either 0 or
4605 1), and is of the indicated TYPE. */
4608 constant_boolean_node (value, type)
4612 if (type == integer_type_node)
4613 return value ? integer_one_node : integer_zero_node;
4614 else if (TREE_CODE (type) == BOOLEAN_TYPE)
4615 return truthvalue_conversion (value ? integer_one_node :
4619 tree t = build_int_2 (value, 0);
4621 TREE_TYPE (t) = type;
4626 /* Utility function for the following routine, to see how complex a nesting of
4627 COND_EXPRs can be. EXPR is the expression and LIMIT is a count beyond which
4628 we don't care (to avoid spending too much time on complex expressions.). */
4631 count_cond (expr, lim)
4637 if (TREE_CODE (expr) != COND_EXPR)
4642 true = count_cond (TREE_OPERAND (expr, 1), lim - 1);
4643 false = count_cond (TREE_OPERAND (expr, 2), lim - 1 - true);
4644 return MIN (lim, 1 + true + false);
4647 /* Perform constant folding and related simplification of EXPR.
4648 The related simplifications include x*1 => x, x*0 => 0, etc.,
4649 and application of the associative law.
4650 NOP_EXPR conversions may be removed freely (as long as we
4651 are careful not to change the C type of the overall expression)
4652 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
4653 but we can constant-fold them if they have constant operands. */
4659 register tree t = expr;
4660 tree t1 = NULL_TREE;
4662 tree type = TREE_TYPE (expr);
4663 register tree arg0 = NULL_TREE, arg1 = NULL_TREE;
4664 register enum tree_code code = TREE_CODE (t);
4667 /* WINS will be nonzero when the switch is done
4668 if all operands are constant. */
4671 /* Don't try to process an RTL_EXPR since its operands aren't trees.
4672 Likewise for a SAVE_EXPR that's already been evaluated. */
4673 if (code == RTL_EXPR || (code == SAVE_EXPR && SAVE_EXPR_RTL (t)) != 0)
4676 /* Return right away if already constant. */
4677 if (TREE_CONSTANT (t))
4679 if (code == CONST_DECL)
4680 return DECL_INITIAL (t);
4684 #ifdef MAX_INTEGER_COMPUTATION_MODE
4685 check_max_integer_computation_mode (expr);
4688 kind = TREE_CODE_CLASS (code);
4689 if (code == NOP_EXPR || code == FLOAT_EXPR || code == CONVERT_EXPR)
4693 /* Special case for conversion ops that can have fixed point args. */
4694 arg0 = TREE_OPERAND (t, 0);
4696 /* Don't use STRIP_NOPS, because signedness of argument type matters. */
4698 STRIP_SIGN_NOPS (arg0);
4700 if (arg0 != 0 && TREE_CODE (arg0) == COMPLEX_CST)
4701 subop = TREE_REALPART (arg0);
4705 if (subop != 0 && TREE_CODE (subop) != INTEGER_CST
4706 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
4707 && TREE_CODE (subop) != REAL_CST
4708 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
4710 /* Note that TREE_CONSTANT isn't enough:
4711 static var addresses are constant but we can't
4712 do arithmetic on them. */
4715 else if (kind == 'e' || kind == '<'
4716 || kind == '1' || kind == '2' || kind == 'r')
4718 register int len = tree_code_length[(int) code];
4720 for (i = 0; i < len; i++)
4722 tree op = TREE_OPERAND (t, i);
4726 continue; /* Valid for CALL_EXPR, at least. */
4728 if (kind == '<' || code == RSHIFT_EXPR)
4730 /* Signedness matters here. Perhaps we can refine this
4732 STRIP_SIGN_NOPS (op);
4736 /* Strip any conversions that don't change the mode. */
4740 if (TREE_CODE (op) == COMPLEX_CST)
4741 subop = TREE_REALPART (op);
4745 if (TREE_CODE (subop) != INTEGER_CST
4746 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
4747 && TREE_CODE (subop) != REAL_CST
4748 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
4750 /* Note that TREE_CONSTANT isn't enough:
4751 static var addresses are constant but we can't
4752 do arithmetic on them. */
4762 /* If this is a commutative operation, and ARG0 is a constant, move it
4763 to ARG1 to reduce the number of tests below. */
4764 if ((code == PLUS_EXPR || code == MULT_EXPR || code == MIN_EXPR
4765 || code == MAX_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR
4766 || code == BIT_AND_EXPR)
4767 && (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST))
4769 tem = arg0; arg0 = arg1; arg1 = tem;
4771 tem = TREE_OPERAND (t, 0); TREE_OPERAND (t, 0) = TREE_OPERAND (t, 1);
4772 TREE_OPERAND (t, 1) = tem;
4775 /* Now WINS is set as described above,
4776 ARG0 is the first operand of EXPR,
4777 and ARG1 is the second operand (if it has more than one operand).
4779 First check for cases where an arithmetic operation is applied to a
4780 compound, conditional, or comparison operation. Push the arithmetic
4781 operation inside the compound or conditional to see if any folding
4782 can then be done. Convert comparison to conditional for this purpose.
4783 The also optimizes non-constant cases that used to be done in
4786 Before we do that, see if this is a BIT_AND_EXPR or a BIT_OR_EXPR,
4787 one of the operands is a comparison and the other is a comparison, a
4788 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
4789 code below would make the expression more complex. Change it to a
4790 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
4791 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
4793 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
4794 || code == EQ_EXPR || code == NE_EXPR)
4795 && ((truth_value_p (TREE_CODE (arg0))
4796 && (truth_value_p (TREE_CODE (arg1))
4797 || (TREE_CODE (arg1) == BIT_AND_EXPR
4798 && integer_onep (TREE_OPERAND (arg1, 1)))))
4799 || (truth_value_p (TREE_CODE (arg1))
4800 && (truth_value_p (TREE_CODE (arg0))
4801 || (TREE_CODE (arg0) == BIT_AND_EXPR
4802 && integer_onep (TREE_OPERAND (arg0, 1)))))))
4804 t = fold (build (code == BIT_AND_EXPR ? TRUTH_AND_EXPR
4805 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
4809 if (code == EQ_EXPR)
4810 t = invert_truthvalue (t);
4815 if (TREE_CODE_CLASS (code) == '1')
4817 if (TREE_CODE (arg0) == COMPOUND_EXPR)
4818 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
4819 fold (build1 (code, type, TREE_OPERAND (arg0, 1))));
4820 else if (TREE_CODE (arg0) == COND_EXPR)
4822 t = fold (build (COND_EXPR, type, TREE_OPERAND (arg0, 0),
4823 fold (build1 (code, type, TREE_OPERAND (arg0, 1))),
4824 fold (build1 (code, type, TREE_OPERAND (arg0, 2)))));
4826 /* If this was a conversion, and all we did was to move into
4827 inside the COND_EXPR, bring it back out. But leave it if
4828 it is a conversion from integer to integer and the
4829 result precision is no wider than a word since such a
4830 conversion is cheap and may be optimized away by combine,
4831 while it couldn't if it were outside the COND_EXPR. Then return
4832 so we don't get into an infinite recursion loop taking the
4833 conversion out and then back in. */
4835 if ((code == NOP_EXPR || code == CONVERT_EXPR
4836 || code == NON_LVALUE_EXPR)
4837 && TREE_CODE (t) == COND_EXPR
4838 && TREE_CODE (TREE_OPERAND (t, 1)) == code
4839 && TREE_CODE (TREE_OPERAND (t, 2)) == code
4840 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0))
4841 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 2), 0)))
4842 && ! (INTEGRAL_TYPE_P (TREE_TYPE (t))
4844 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0))))
4845 && TYPE_PRECISION (TREE_TYPE (t)) <= BITS_PER_WORD))
4846 t = build1 (code, type,
4848 TREE_TYPE (TREE_OPERAND
4849 (TREE_OPERAND (t, 1), 0)),
4850 TREE_OPERAND (t, 0),
4851 TREE_OPERAND (TREE_OPERAND (t, 1), 0),
4852 TREE_OPERAND (TREE_OPERAND (t, 2), 0)));
4855 else if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<')
4856 return fold (build (COND_EXPR, type, arg0,
4857 fold (build1 (code, type, integer_one_node)),
4858 fold (build1 (code, type, integer_zero_node))));
4860 else if (TREE_CODE_CLASS (code) == '2'
4861 || TREE_CODE_CLASS (code) == '<')
4863 if (TREE_CODE (arg1) == COMPOUND_EXPR)
4864 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
4865 fold (build (code, type,
4866 arg0, TREE_OPERAND (arg1, 1))));
4867 else if ((TREE_CODE (arg1) == COND_EXPR
4868 || (TREE_CODE_CLASS (TREE_CODE (arg1)) == '<'
4869 && TREE_CODE_CLASS (code) != '<'))
4870 && (TREE_CODE (arg0) != COND_EXPR
4871 || count_cond (arg0, 25) + count_cond (arg1, 25) <= 25)
4872 && (! TREE_SIDE_EFFECTS (arg0)
4873 || (global_bindings_p () == 0
4874 && ! contains_placeholder_p (arg0))))
4876 tree test, true_value, false_value;
4877 tree lhs = 0, rhs = 0;
4879 if (TREE_CODE (arg1) == COND_EXPR)
4881 test = TREE_OPERAND (arg1, 0);
4882 true_value = TREE_OPERAND (arg1, 1);
4883 false_value = TREE_OPERAND (arg1, 2);
4887 tree testtype = TREE_TYPE (arg1);
4889 true_value = convert (testtype, integer_one_node);
4890 false_value = convert (testtype, integer_zero_node);
4893 /* If ARG0 is complex we want to make sure we only evaluate
4894 it once. Though this is only required if it is volatile, it
4895 might be more efficient even if it is not. However, if we
4896 succeed in folding one part to a constant, we do not need
4897 to make this SAVE_EXPR. Since we do this optimization
4898 primarily to see if we do end up with constant and this
4899 SAVE_EXPR interferes with later optimizations, suppressing
4900 it when we can is important.
4902 If we are not in a function, we can't make a SAVE_EXPR, so don't
4903 try to do so. Don't try to see if the result is a constant
4904 if an arm is a COND_EXPR since we get exponential behavior
4907 if (TREE_CODE (arg0) != SAVE_EXPR && ! TREE_CONSTANT (arg0)
4908 && global_bindings_p () == 0
4909 && ((TREE_CODE (arg0) != VAR_DECL
4910 && TREE_CODE (arg0) != PARM_DECL)
4911 || TREE_SIDE_EFFECTS (arg0)))
4913 if (TREE_CODE (true_value) != COND_EXPR)
4914 lhs = fold (build (code, type, arg0, true_value));
4916 if (TREE_CODE (false_value) != COND_EXPR)
4917 rhs = fold (build (code, type, arg0, false_value));
4919 if ((lhs == 0 || ! TREE_CONSTANT (lhs))
4920 && (rhs == 0 || !TREE_CONSTANT (rhs)))
4921 arg0 = save_expr (arg0), lhs = rhs = 0;
4925 lhs = fold (build (code, type, arg0, true_value));
4927 rhs = fold (build (code, type, arg0, false_value));
4929 test = fold (build (COND_EXPR, type, test, lhs, rhs));
4931 if (TREE_CODE (arg0) == SAVE_EXPR)
4932 return build (COMPOUND_EXPR, type,
4933 convert (void_type_node, arg0),
4934 strip_compound_expr (test, arg0));
4936 return convert (type, test);
4939 else if (TREE_CODE (arg0) == COMPOUND_EXPR)
4940 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
4941 fold (build (code, type, TREE_OPERAND (arg0, 1), arg1)));
4942 else if ((TREE_CODE (arg0) == COND_EXPR
4943 || (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
4944 && TREE_CODE_CLASS (code) != '<'))
4945 && (TREE_CODE (arg1) != COND_EXPR
4946 || count_cond (arg0, 25) + count_cond (arg1, 25) <= 25)
4947 && (! TREE_SIDE_EFFECTS (arg1)
4948 || (global_bindings_p () == 0
4949 && ! contains_placeholder_p (arg1))))
4951 tree test, true_value, false_value;
4952 tree lhs = 0, rhs = 0;
4954 if (TREE_CODE (arg0) == COND_EXPR)
4956 test = TREE_OPERAND (arg0, 0);
4957 true_value = TREE_OPERAND (arg0, 1);
4958 false_value = TREE_OPERAND (arg0, 2);
4962 tree testtype = TREE_TYPE (arg0);
4964 true_value = convert (testtype, integer_one_node);
4965 false_value = convert (testtype, integer_zero_node);
4968 if (TREE_CODE (arg1) != SAVE_EXPR && ! TREE_CONSTANT (arg0)
4969 && global_bindings_p () == 0
4970 && ((TREE_CODE (arg1) != VAR_DECL
4971 && TREE_CODE (arg1) != PARM_DECL)
4972 || TREE_SIDE_EFFECTS (arg1)))
4974 if (TREE_CODE (true_value) != COND_EXPR)
4975 lhs = fold (build (code, type, true_value, arg1));
4977 if (TREE_CODE (false_value) != COND_EXPR)
4978 rhs = fold (build (code, type, false_value, arg1));
4980 if ((lhs == 0 || ! TREE_CONSTANT (lhs))
4981 && (rhs == 0 || !TREE_CONSTANT (rhs)))
4982 arg1 = save_expr (arg1), lhs = rhs = 0;
4986 lhs = fold (build (code, type, true_value, arg1));
4989 rhs = fold (build (code, type, false_value, arg1));
4991 test = fold (build (COND_EXPR, type, test, lhs, rhs));
4992 if (TREE_CODE (arg1) == SAVE_EXPR)
4993 return build (COMPOUND_EXPR, type,
4994 convert (void_type_node, arg1),
4995 strip_compound_expr (test, arg1));
4997 return convert (type, test);
5000 else if (TREE_CODE_CLASS (code) == '<'
5001 && TREE_CODE (arg0) == COMPOUND_EXPR)
5002 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
5003 fold (build (code, type, TREE_OPERAND (arg0, 1), arg1)));
5004 else if (TREE_CODE_CLASS (code) == '<'
5005 && TREE_CODE (arg1) == COMPOUND_EXPR)
5006 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
5007 fold (build (code, type, arg0, TREE_OPERAND (arg1, 1))));
5019 return fold (DECL_INITIAL (t));
5024 case FIX_TRUNC_EXPR:
5025 /* Other kinds of FIX are not handled properly by fold_convert. */
5027 if (TREE_TYPE (TREE_OPERAND (t, 0)) == TREE_TYPE (t))
5028 return TREE_OPERAND (t, 0);
5030 /* Handle cases of two conversions in a row. */
5031 if (TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
5032 || TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR)
5034 tree inside_type = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5035 tree inter_type = TREE_TYPE (TREE_OPERAND (t, 0));
5036 tree final_type = TREE_TYPE (t);
5037 int inside_int = INTEGRAL_TYPE_P (inside_type);
5038 int inside_ptr = POINTER_TYPE_P (inside_type);
5039 int inside_float = FLOAT_TYPE_P (inside_type);
5040 int inside_prec = TYPE_PRECISION (inside_type);
5041 int inside_unsignedp = TREE_UNSIGNED (inside_type);
5042 int inter_int = INTEGRAL_TYPE_P (inter_type);
5043 int inter_ptr = POINTER_TYPE_P (inter_type);
5044 int inter_float = FLOAT_TYPE_P (inter_type);
5045 int inter_prec = TYPE_PRECISION (inter_type);
5046 int inter_unsignedp = TREE_UNSIGNED (inter_type);
5047 int final_int = INTEGRAL_TYPE_P (final_type);
5048 int final_ptr = POINTER_TYPE_P (final_type);
5049 int final_float = FLOAT_TYPE_P (final_type);
5050 int final_prec = TYPE_PRECISION (final_type);
5051 int final_unsignedp = TREE_UNSIGNED (final_type);
5053 /* In addition to the cases of two conversions in a row
5054 handled below, if we are converting something to its own
5055 type via an object of identical or wider precision, neither
5056 conversion is needed. */
5057 if (inside_type == final_type
5058 && ((inter_int && final_int) || (inter_float && final_float))
5059 && inter_prec >= final_prec)
5060 return TREE_OPERAND (TREE_OPERAND (t, 0), 0);
5062 /* Likewise, if the intermediate and final types are either both
5063 float or both integer, we don't need the middle conversion if
5064 it is wider than the final type and doesn't change the signedness
5065 (for integers). Avoid this if the final type is a pointer
5066 since then we sometimes need the inner conversion. Likewise if
5067 the outer has a precision not equal to the size of its mode. */
5068 if ((((inter_int || inter_ptr) && (inside_int || inside_ptr))
5069 || (inter_float && inside_float))
5070 && inter_prec >= inside_prec
5071 && (inter_float || inter_unsignedp == inside_unsignedp)
5072 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (final_type))
5073 && TYPE_MODE (final_type) == TYPE_MODE (inter_type))
5075 return convert (final_type, TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5077 /* If we have a sign-extension of a zero-extended value, we can
5078 replace that by a single zero-extension. */
5079 if (inside_int && inter_int && final_int
5080 && inside_prec < inter_prec && inter_prec < final_prec
5081 && inside_unsignedp && !inter_unsignedp)
5082 return convert (final_type, TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5084 /* Two conversions in a row are not needed unless:
5085 - some conversion is floating-point (overstrict for now), or
5086 - the intermediate type is narrower than both initial and
5088 - the intermediate type and innermost type differ in signedness,
5089 and the outermost type is wider than the intermediate, or
5090 - the initial type is a pointer type and the precisions of the
5091 intermediate and final types differ, or
5092 - the final type is a pointer type and the precisions of the
5093 initial and intermediate types differ. */
5094 if (! inside_float && ! inter_float && ! final_float
5095 && (inter_prec > inside_prec || inter_prec > final_prec)
5096 && ! (inside_int && inter_int
5097 && inter_unsignedp != inside_unsignedp
5098 && inter_prec < final_prec)
5099 && ((inter_unsignedp && inter_prec > inside_prec)
5100 == (final_unsignedp && final_prec > inter_prec))
5101 && ! (inside_ptr && inter_prec != final_prec)
5102 && ! (final_ptr && inside_prec != inter_prec)
5103 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (final_type))
5104 && TYPE_MODE (final_type) == TYPE_MODE (inter_type))
5106 return convert (final_type, TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5109 if (TREE_CODE (TREE_OPERAND (t, 0)) == MODIFY_EXPR
5110 && TREE_CONSTANT (TREE_OPERAND (TREE_OPERAND (t, 0), 1))
5111 /* Detect assigning a bitfield. */
5112 && !(TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) == COMPONENT_REF
5113 && DECL_BIT_FIELD (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 1))))
5115 /* Don't leave an assignment inside a conversion
5116 unless assigning a bitfield. */
5117 tree prev = TREE_OPERAND (t, 0);
5118 TREE_OPERAND (t, 0) = TREE_OPERAND (prev, 1);
5119 /* First do the assignment, then return converted constant. */
5120 t = build (COMPOUND_EXPR, TREE_TYPE (t), prev, fold (t));
5126 TREE_CONSTANT (t) = TREE_CONSTANT (arg0);
5129 return fold_convert (t, arg0);
5131 #if 0 /* This loses on &"foo"[0]. */
5136 /* Fold an expression like: "foo"[2] */
5137 if (TREE_CODE (arg0) == STRING_CST
5138 && TREE_CODE (arg1) == INTEGER_CST
5139 && compare_tree_int (arg1, TREE_STRING_LENGTH (arg0)) < 0)
5141 t = build_int_2 (TREE_STRING_POINTER (arg0)[TREE_INT_CST_LOW (arg))], 0);
5142 TREE_TYPE (t) = TREE_TYPE (TREE_TYPE (arg0));
5143 force_fit_type (t, 0);
5150 if (TREE_CODE (arg0) == CONSTRUCTOR)
5152 tree m = purpose_member (arg1, CONSTRUCTOR_ELTS (arg0));
5159 TREE_CONSTANT (t) = wins;
5165 if (TREE_CODE (arg0) == INTEGER_CST)
5167 HOST_WIDE_INT low, high;
5168 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
5169 TREE_INT_CST_HIGH (arg0),
5171 t = build_int_2 (low, high);
5172 TREE_TYPE (t) = type;
5174 = (TREE_OVERFLOW (arg0)
5175 | force_fit_type (t, overflow && !TREE_UNSIGNED (type)));
5176 TREE_CONSTANT_OVERFLOW (t)
5177 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg0);
5179 else if (TREE_CODE (arg0) == REAL_CST)
5180 t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
5182 else if (TREE_CODE (arg0) == NEGATE_EXPR)
5183 return TREE_OPERAND (arg0, 0);
5185 /* Convert - (a - b) to (b - a) for non-floating-point. */
5186 else if (TREE_CODE (arg0) == MINUS_EXPR
5187 && (! FLOAT_TYPE_P (type) || flag_fast_math))
5188 return build (MINUS_EXPR, type, TREE_OPERAND (arg0, 1),
5189 TREE_OPERAND (arg0, 0));
5196 if (TREE_CODE (arg0) == INTEGER_CST)
5198 if (! TREE_UNSIGNED (type)
5199 && TREE_INT_CST_HIGH (arg0) < 0)
5201 HOST_WIDE_INT low, high;
5202 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
5203 TREE_INT_CST_HIGH (arg0),
5205 t = build_int_2 (low, high);
5206 TREE_TYPE (t) = type;
5208 = (TREE_OVERFLOW (arg0)
5209 | force_fit_type (t, overflow));
5210 TREE_CONSTANT_OVERFLOW (t)
5211 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg0);
5214 else if (TREE_CODE (arg0) == REAL_CST)
5216 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
5217 t = build_real (type,
5218 REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
5221 else if (TREE_CODE (arg0) == ABS_EXPR || TREE_CODE (arg0) == NEGATE_EXPR)
5222 return build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0));
5226 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
5228 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
5229 return build (COMPLEX_EXPR, TREE_TYPE (arg0),
5230 TREE_OPERAND (arg0, 0),
5231 negate_expr (TREE_OPERAND (arg0, 1)));
5232 else if (TREE_CODE (arg0) == COMPLEX_CST)
5233 return build_complex (type, TREE_OPERAND (arg0, 0),
5234 negate_expr (TREE_OPERAND (arg0, 1)));
5235 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
5236 return fold (build (TREE_CODE (arg0), type,
5237 fold (build1 (CONJ_EXPR, type,
5238 TREE_OPERAND (arg0, 0))),
5239 fold (build1 (CONJ_EXPR,
5240 type, TREE_OPERAND (arg0, 1)))));
5241 else if (TREE_CODE (arg0) == CONJ_EXPR)
5242 return TREE_OPERAND (arg0, 0);
5248 t = build_int_2 (~ TREE_INT_CST_LOW (arg0),
5249 ~ TREE_INT_CST_HIGH (arg0));
5250 TREE_TYPE (t) = type;
5251 force_fit_type (t, 0);
5252 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg0);
5253 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (arg0);
5255 else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
5256 return TREE_OPERAND (arg0, 0);
5260 /* A + (-B) -> A - B */
5261 if (TREE_CODE (arg1) == NEGATE_EXPR)
5262 return fold (build (MINUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
5263 /* (-A) + B -> B - A */
5264 if (TREE_CODE (arg0) == NEGATE_EXPR)
5265 return fold (build (MINUS_EXPR, type, arg1, TREE_OPERAND (arg0, 0)));
5266 else if (! FLOAT_TYPE_P (type))
5268 if (integer_zerop (arg1))
5269 return non_lvalue (convert (type, arg0));
5271 /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
5272 with a constant, and the two constants have no bits in common,
5273 we should treat this as a BIT_IOR_EXPR since this may produce more
5275 if (TREE_CODE (arg0) == BIT_AND_EXPR
5276 && TREE_CODE (arg1) == BIT_AND_EXPR
5277 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
5278 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
5279 && integer_zerop (const_binop (BIT_AND_EXPR,
5280 TREE_OPERAND (arg0, 1),
5281 TREE_OPERAND (arg1, 1), 0)))
5283 code = BIT_IOR_EXPR;
5287 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
5288 (plus (plus (mult) (mult)) (foo)) so that we can
5289 take advantage of the factoring cases below. */
5290 if ((TREE_CODE (arg0) == PLUS_EXPR
5291 && TREE_CODE (arg1) == MULT_EXPR)
5292 || (TREE_CODE (arg1) == PLUS_EXPR
5293 && TREE_CODE (arg0) == MULT_EXPR))
5295 tree parg0, parg1, parg, marg;
5297 if (TREE_CODE (arg0) == PLUS_EXPR)
5298 parg = arg0, marg = arg1;
5300 parg = arg1, marg = arg0;
5301 parg0 = TREE_OPERAND (parg, 0);
5302 parg1 = TREE_OPERAND (parg, 1);
5306 if (TREE_CODE (parg0) == MULT_EXPR
5307 && TREE_CODE (parg1) != MULT_EXPR)
5308 return fold (build (PLUS_EXPR, type,
5309 fold (build (PLUS_EXPR, type, parg0, marg)),
5311 if (TREE_CODE (parg0) != MULT_EXPR
5312 && TREE_CODE (parg1) == MULT_EXPR)
5313 return fold (build (PLUS_EXPR, type,
5314 fold (build (PLUS_EXPR, type, parg1, marg)),
5318 if (TREE_CODE (arg0) == MULT_EXPR && TREE_CODE (arg1) == MULT_EXPR)
5320 tree arg00, arg01, arg10, arg11;
5321 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
5323 /* (A * C) + (B * C) -> (A+B) * C.
5324 We are most concerned about the case where C is a constant,
5325 but other combinations show up during loop reduction. Since
5326 it is not difficult, try all four possibilities. */
5328 arg00 = TREE_OPERAND (arg0, 0);
5329 arg01 = TREE_OPERAND (arg0, 1);
5330 arg10 = TREE_OPERAND (arg1, 0);
5331 arg11 = TREE_OPERAND (arg1, 1);
5334 if (operand_equal_p (arg01, arg11, 0))
5335 same = arg01, alt0 = arg00, alt1 = arg10;
5336 else if (operand_equal_p (arg00, arg10, 0))
5337 same = arg00, alt0 = arg01, alt1 = arg11;
5338 else if (operand_equal_p (arg00, arg11, 0))
5339 same = arg00, alt0 = arg01, alt1 = arg10;
5340 else if (operand_equal_p (arg01, arg10, 0))
5341 same = arg01, alt0 = arg00, alt1 = arg11;
5343 /* No identical multiplicands; see if we can find a common
5344 power-of-two factor in non-power-of-two multiplies. This
5345 can help in multi-dimensional array access. */
5346 else if (TREE_CODE (arg01) == INTEGER_CST
5347 && TREE_CODE (arg11) == INTEGER_CST
5348 && TREE_INT_CST_HIGH (arg01) == 0
5349 && TREE_INT_CST_HIGH (arg11) == 0)
5351 HOST_WIDE_INT int01, int11, tmp;
5352 int01 = TREE_INT_CST_LOW (arg01);
5353 int11 = TREE_INT_CST_LOW (arg11);
5355 /* Move min of absolute values to int11. */
5356 if ((int01 >= 0 ? int01 : -int01)
5357 < (int11 >= 0 ? int11 : -int11))
5359 tmp = int01, int01 = int11, int11 = tmp;
5360 alt0 = arg00, arg00 = arg10, arg10 = alt0;
5361 alt0 = arg01, arg01 = arg11, arg11 = alt0;
5364 if (exact_log2 (int11) > 0 && int01 % int11 == 0)
5366 alt0 = fold (build (MULT_EXPR, type, arg00,
5367 build_int_2 (int01 / int11, 0)));
5374 return fold (build (MULT_EXPR, type,
5375 fold (build (PLUS_EXPR, type, alt0, alt1)),
5379 /* In IEEE floating point, x+0 may not equal x. */
5380 else if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
5382 && real_zerop (arg1))
5383 return non_lvalue (convert (type, arg0));
5384 /* x+(-0) equals x, even for IEEE. */
5385 else if (TREE_CODE (arg1) == REAL_CST
5386 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (arg1)))
5387 return non_lvalue (convert (type, arg0));
5390 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
5391 is a rotate of A by C1 bits. */
5392 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
5393 is a rotate of A by B bits. */
5395 register enum tree_code code0, code1;
5396 code0 = TREE_CODE (arg0);
5397 code1 = TREE_CODE (arg1);
5398 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
5399 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
5400 && operand_equal_p (TREE_OPERAND (arg0, 0),
5401 TREE_OPERAND (arg1,0), 0)
5402 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
5404 register tree tree01, tree11;
5405 register enum tree_code code01, code11;
5407 tree01 = TREE_OPERAND (arg0, 1);
5408 tree11 = TREE_OPERAND (arg1, 1);
5409 STRIP_NOPS (tree01);
5410 STRIP_NOPS (tree11);
5411 code01 = TREE_CODE (tree01);
5412 code11 = TREE_CODE (tree11);
5413 if (code01 == INTEGER_CST
5414 && code11 == INTEGER_CST
5415 && TREE_INT_CST_HIGH (tree01) == 0
5416 && TREE_INT_CST_HIGH (tree11) == 0
5417 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
5418 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
5419 return build (LROTATE_EXPR, type, TREE_OPERAND (arg0, 0),
5420 code0 == LSHIFT_EXPR ? tree01 : tree11);
5421 else if (code11 == MINUS_EXPR)
5423 tree tree110, tree111;
5424 tree110 = TREE_OPERAND (tree11, 0);
5425 tree111 = TREE_OPERAND (tree11, 1);
5426 STRIP_NOPS (tree110);
5427 STRIP_NOPS (tree111);
5428 if (TREE_CODE (tree110) == INTEGER_CST
5429 && 0 == compare_tree_int (tree110,
5431 (TREE_TYPE (TREE_OPERAND
5433 && operand_equal_p (tree01, tree111, 0))
5434 return build ((code0 == LSHIFT_EXPR
5437 type, TREE_OPERAND (arg0, 0), tree01);
5439 else if (code01 == MINUS_EXPR)
5441 tree tree010, tree011;
5442 tree010 = TREE_OPERAND (tree01, 0);
5443 tree011 = TREE_OPERAND (tree01, 1);
5444 STRIP_NOPS (tree010);
5445 STRIP_NOPS (tree011);
5446 if (TREE_CODE (tree010) == INTEGER_CST
5447 && 0 == compare_tree_int (tree010,
5449 (TREE_TYPE (TREE_OPERAND
5451 && operand_equal_p (tree11, tree011, 0))
5452 return build ((code0 != LSHIFT_EXPR
5455 type, TREE_OPERAND (arg0, 0), tree11);
5462 /* In most languages, can't associate operations on floats through
5463 parentheses. Rather than remember where the parentheses were, we
5464 don't associate floats at all. It shouldn't matter much. However,
5465 associating multiplications is only very slightly inaccurate, so do
5466 that if -ffast-math is specified. */
5469 && (! FLOAT_TYPE_P (type)
5470 || (flag_fast_math && code != MULT_EXPR)))
5472 tree var0, con0, lit0, var1, con1, lit1;
5474 /* Split both trees into variables, constants, and literals. Then
5475 associate each group together, the constants with literals,
5476 then the result with variables. This increases the chances of
5477 literals being recombined later and of generating relocatable
5478 expressions for the sum of a constant and literal. */
5479 var0 = split_tree (arg0, code, &con0, &lit0, 0);
5480 var1 = split_tree (arg1, code, &con1, &lit1, code == MINUS_EXPR);
5482 /* Only do something if we found more than two objects. Otherwise,
5483 nothing has changed and we risk infinite recursion. */
5484 if (2 < ((var0 != 0) + (var1 != 0) + (con0 != 0) + (con1 != 0)
5485 + (lit0 != 0) + (lit1 != 0)))
5487 var0 = associate_trees (var0, var1, code, type);
5488 con0 = associate_trees (con0, con1, code, type);
5489 lit0 = associate_trees (lit0, lit1, code, type);
5490 con0 = associate_trees (con0, lit0, code, type);
5491 return convert (type, associate_trees (var0, con0, code, type));
5496 #if defined (REAL_IS_NOT_DOUBLE) && ! defined (REAL_ARITHMETIC)
5497 if (TREE_CODE (arg1) == REAL_CST)
5499 #endif /* REAL_IS_NOT_DOUBLE, and no REAL_ARITHMETIC */
5501 t1 = const_binop (code, arg0, arg1, 0);
5502 if (t1 != NULL_TREE)
5504 /* The return value should always have
5505 the same type as the original expression. */
5506 if (TREE_TYPE (t1) != TREE_TYPE (t))
5507 t1 = convert (TREE_TYPE (t), t1);
5514 /* A - (-B) -> A + B */
5515 if (TREE_CODE (arg1) == NEGATE_EXPR)
5516 return fold (build (PLUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
5517 /* (-A) - CST -> (-CST) - A for floating point (what about ints ?) */
5518 if (TREE_CODE (arg0) == NEGATE_EXPR && TREE_CODE (arg1) == REAL_CST)
5520 fold (build (MINUS_EXPR, type,
5521 build_real (TREE_TYPE (arg1),
5522 REAL_VALUE_NEGATE (TREE_REAL_CST (arg1))),
5523 TREE_OPERAND (arg0, 0)));
5525 if (! FLOAT_TYPE_P (type))
5527 if (! wins && integer_zerop (arg0))
5528 return negate_expr (arg1);
5529 if (integer_zerop (arg1))
5530 return non_lvalue (convert (type, arg0));
5532 /* (A * C) - (B * C) -> (A-B) * C. Since we are most concerned
5533 about the case where C is a constant, just try one of the
5534 four possibilities. */
5536 if (TREE_CODE (arg0) == MULT_EXPR && TREE_CODE (arg1) == MULT_EXPR
5537 && operand_equal_p (TREE_OPERAND (arg0, 1),
5538 TREE_OPERAND (arg1, 1), 0))
5539 return fold (build (MULT_EXPR, type,
5540 fold (build (MINUS_EXPR, type,
5541 TREE_OPERAND (arg0, 0),
5542 TREE_OPERAND (arg1, 0))),
5543 TREE_OPERAND (arg0, 1)));
5546 else if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
5549 /* Except with IEEE floating point, 0-x equals -x. */
5550 if (! wins && real_zerop (arg0))
5551 return negate_expr (arg1);
5552 /* Except with IEEE floating point, x-0 equals x. */
5553 if (real_zerop (arg1))
5554 return non_lvalue (convert (type, arg0));
5557 /* Fold &x - &x. This can happen from &x.foo - &x.
5558 This is unsafe for certain floats even in non-IEEE formats.
5559 In IEEE, it is unsafe because it does wrong for NaNs.
5560 Also note that operand_equal_p is always false if an operand
5563 if ((! FLOAT_TYPE_P (type) || flag_fast_math)
5564 && operand_equal_p (arg0, arg1, 0))
5565 return convert (type, integer_zero_node);
5570 /* (-A) * (-B) -> A * B */
5571 if (TREE_CODE (arg0) == NEGATE_EXPR && TREE_CODE (arg1) == NEGATE_EXPR)
5572 return fold (build (MULT_EXPR, type, TREE_OPERAND (arg0, 0),
5573 TREE_OPERAND (arg1, 0)));
5575 if (! FLOAT_TYPE_P (type))
5577 if (integer_zerop (arg1))
5578 return omit_one_operand (type, arg1, arg0);
5579 if (integer_onep (arg1))
5580 return non_lvalue (convert (type, arg0));
5582 /* (a * (1 << b)) is (a << b) */
5583 if (TREE_CODE (arg1) == LSHIFT_EXPR
5584 && integer_onep (TREE_OPERAND (arg1, 0)))
5585 return fold (build (LSHIFT_EXPR, type, arg0,
5586 TREE_OPERAND (arg1, 1)));
5587 if (TREE_CODE (arg0) == LSHIFT_EXPR
5588 && integer_onep (TREE_OPERAND (arg0, 0)))
5589 return fold (build (LSHIFT_EXPR, type, arg1,
5590 TREE_OPERAND (arg0, 1)));
5592 if (TREE_CODE (arg1) == INTEGER_CST
5593 && 0 != (tem = extract_muldiv (TREE_OPERAND (t, 0), arg1,
5595 return convert (type, tem);
5600 /* x*0 is 0, except for IEEE floating point. */
5601 if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
5603 && real_zerop (arg1))
5604 return omit_one_operand (type, arg1, arg0);
5605 /* In IEEE floating point, x*1 is not equivalent to x for snans.
5606 However, ANSI says we can drop signals,
5607 so we can do this anyway. */
5608 if (real_onep (arg1))
5609 return non_lvalue (convert (type, arg0));
5611 if (! wins && real_twop (arg1) && global_bindings_p () == 0
5612 && ! contains_placeholder_p (arg0))
5614 tree arg = save_expr (arg0);
5615 return build (PLUS_EXPR, type, arg, arg);
5622 if (integer_all_onesp (arg1))
5623 return omit_one_operand (type, arg1, arg0);
5624 if (integer_zerop (arg1))
5625 return non_lvalue (convert (type, arg0));
5626 t1 = distribute_bit_expr (code, type, arg0, arg1);
5627 if (t1 != NULL_TREE)
5630 /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
5632 This results in more efficient code for machines without a NAND
5633 instruction. Combine will canonicalize to the first form
5634 which will allow use of NAND instructions provided by the
5635 backend if they exist. */
5636 if (TREE_CODE (arg0) == BIT_NOT_EXPR
5637 && TREE_CODE (arg1) == BIT_NOT_EXPR)
5639 return fold (build1 (BIT_NOT_EXPR, type,
5640 build (BIT_AND_EXPR, type,
5641 TREE_OPERAND (arg0, 0),
5642 TREE_OPERAND (arg1, 0))));
5645 /* See if this can be simplified into a rotate first. If that
5646 is unsuccessful continue in the association code. */
5650 if (integer_zerop (arg1))
5651 return non_lvalue (convert (type, arg0));
5652 if (integer_all_onesp (arg1))
5653 return fold (build1 (BIT_NOT_EXPR, type, arg0));
5655 /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
5656 with a constant, and the two constants have no bits in common,
5657 we should treat this as a BIT_IOR_EXPR since this may produce more
5659 if (TREE_CODE (arg0) == BIT_AND_EXPR
5660 && TREE_CODE (arg1) == BIT_AND_EXPR
5661 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
5662 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
5663 && integer_zerop (const_binop (BIT_AND_EXPR,
5664 TREE_OPERAND (arg0, 1),
5665 TREE_OPERAND (arg1, 1), 0)))
5667 code = BIT_IOR_EXPR;
5671 /* See if this can be simplified into a rotate first. If that
5672 is unsuccessful continue in the association code. */
5677 if (integer_all_onesp (arg1))
5678 return non_lvalue (convert (type, arg0));
5679 if (integer_zerop (arg1))
5680 return omit_one_operand (type, arg1, arg0);
5681 t1 = distribute_bit_expr (code, type, arg0, arg1);
5682 if (t1 != NULL_TREE)
5684 /* Simplify ((int)c & 0x377) into (int)c, if c is unsigned char. */
5685 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == NOP_EXPR
5686 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0))))
5688 int prec = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)));
5689 if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
5690 && (~TREE_INT_CST_LOW (arg0)
5691 & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
5692 return build1 (NOP_EXPR, type, TREE_OPERAND (arg1, 0));
5694 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
5695 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
5697 int prec = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
5698 if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
5699 && (~TREE_INT_CST_LOW (arg1)
5700 & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
5701 return build1 (NOP_EXPR, type, TREE_OPERAND (arg0, 0));
5704 /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
5706 This results in more efficient code for machines without a NOR
5707 instruction. Combine will canonicalize to the first form
5708 which will allow use of NOR instructions provided by the
5709 backend if they exist. */
5710 if (TREE_CODE (arg0) == BIT_NOT_EXPR
5711 && TREE_CODE (arg1) == BIT_NOT_EXPR)
5713 return fold (build1 (BIT_NOT_EXPR, type,
5714 build (BIT_IOR_EXPR, type,
5715 TREE_OPERAND (arg0, 0),
5716 TREE_OPERAND (arg1, 0))));
5721 case BIT_ANDTC_EXPR:
5722 if (integer_all_onesp (arg0))
5723 return non_lvalue (convert (type, arg1));
5724 if (integer_zerop (arg0))
5725 return omit_one_operand (type, arg0, arg1);
5726 if (TREE_CODE (arg1) == INTEGER_CST)
5728 arg1 = fold (build1 (BIT_NOT_EXPR, type, arg1));
5729 code = BIT_AND_EXPR;
5735 /* In most cases, do nothing with a divide by zero. */
5736 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
5737 #ifndef REAL_INFINITY
5738 if (TREE_CODE (arg1) == REAL_CST && real_zerop (arg1))
5741 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
5743 /* (-A) / (-B) -> A / B */
5744 if (TREE_CODE (arg0) == NEGATE_EXPR && TREE_CODE (arg1) == NEGATE_EXPR)
5745 return fold (build (RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
5746 TREE_OPERAND (arg1, 0)));
5748 /* In IEEE floating point, x/1 is not equivalent to x for snans.
5749 However, ANSI says we can drop signals, so we can do this anyway. */
5750 if (real_onep (arg1))
5751 return non_lvalue (convert (type, arg0));
5753 /* If ARG1 is a constant, we can convert this to a multiply by the
5754 reciprocal. This does not have the same rounding properties,
5755 so only do this if -ffast-math. We can actually always safely
5756 do it if ARG1 is a power of two, but it's hard to tell if it is
5757 or not in a portable manner. */
5758 if (TREE_CODE (arg1) == REAL_CST)
5761 && 0 != (tem = const_binop (code, build_real (type, dconst1),
5763 return fold (build (MULT_EXPR, type, arg0, tem));
5764 /* Find the reciprocal if optimizing and the result is exact. */
5768 r = TREE_REAL_CST (arg1);
5769 if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
5771 tem = build_real (type, r);
5772 return fold (build (MULT_EXPR, type, arg0, tem));
5778 case TRUNC_DIV_EXPR:
5779 case ROUND_DIV_EXPR:
5780 case FLOOR_DIV_EXPR:
5782 case EXACT_DIV_EXPR:
5783 if (integer_onep (arg1))
5784 return non_lvalue (convert (type, arg0));
5785 if (integer_zerop (arg1))
5788 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
5789 operation, EXACT_DIV_EXPR.
5791 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
5792 At one time others generated faster code, it's not clear if they do
5793 after the last round to changes to the DIV code in expmed.c. */
5794 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
5795 && multiple_of_p (type, arg0, arg1))
5796 return fold (build (EXACT_DIV_EXPR, type, arg0, arg1));
5798 if (TREE_CODE (arg1) == INTEGER_CST
5799 && 0 != (tem = extract_muldiv (TREE_OPERAND (t, 0), arg1,
5801 return convert (type, tem);
5806 case FLOOR_MOD_EXPR:
5807 case ROUND_MOD_EXPR:
5808 case TRUNC_MOD_EXPR:
5809 if (integer_onep (arg1))
5810 return omit_one_operand (type, integer_zero_node, arg0);
5811 if (integer_zerop (arg1))
5814 if (TREE_CODE (arg1) == INTEGER_CST
5815 && 0 != (tem = extract_muldiv (TREE_OPERAND (t, 0), arg1,
5817 return convert (type, tem);
5825 if (integer_zerop (arg1))
5826 return non_lvalue (convert (type, arg0));
5827 /* Since negative shift count is not well-defined,
5828 don't try to compute it in the compiler. */
5829 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
5831 /* Rewrite an LROTATE_EXPR by a constant into an
5832 RROTATE_EXPR by a new constant. */
5833 if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
5835 TREE_SET_CODE (t, RROTATE_EXPR);
5836 code = RROTATE_EXPR;
5837 TREE_OPERAND (t, 1) = arg1
5840 convert (TREE_TYPE (arg1),
5841 build_int_2 (GET_MODE_BITSIZE (TYPE_MODE (type)), 0)),
5843 if (tree_int_cst_sgn (arg1) < 0)
5847 /* If we have a rotate of a bit operation with the rotate count and
5848 the second operand of the bit operation both constant,
5849 permute the two operations. */
5850 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
5851 && (TREE_CODE (arg0) == BIT_AND_EXPR
5852 || TREE_CODE (arg0) == BIT_ANDTC_EXPR
5853 || TREE_CODE (arg0) == BIT_IOR_EXPR
5854 || TREE_CODE (arg0) == BIT_XOR_EXPR)
5855 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
5856 return fold (build (TREE_CODE (arg0), type,
5857 fold (build (code, type,
5858 TREE_OPERAND (arg0, 0), arg1)),
5859 fold (build (code, type,
5860 TREE_OPERAND (arg0, 1), arg1))));
5862 /* Two consecutive rotates adding up to the width of the mode can
5864 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
5865 && TREE_CODE (arg0) == RROTATE_EXPR
5866 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
5867 && TREE_INT_CST_HIGH (arg1) == 0
5868 && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
5869 && ((TREE_INT_CST_LOW (arg1)
5870 + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
5871 == (unsigned int) GET_MODE_BITSIZE (TYPE_MODE (type))))
5872 return TREE_OPERAND (arg0, 0);
5877 if (operand_equal_p (arg0, arg1, 0))
5879 if (INTEGRAL_TYPE_P (type)
5880 && operand_equal_p (arg1, TYPE_MIN_VALUE (type), 1))
5881 return omit_one_operand (type, arg1, arg0);
5885 if (operand_equal_p (arg0, arg1, 0))
5887 if (INTEGRAL_TYPE_P (type)
5888 && TYPE_MAX_VALUE (type)
5889 && operand_equal_p (arg1, TYPE_MAX_VALUE (type), 1))
5890 return omit_one_operand (type, arg1, arg0);
5893 case TRUTH_NOT_EXPR:
5894 /* Note that the operand of this must be an int
5895 and its values must be 0 or 1.
5896 ("true" is a fixed value perhaps depending on the language,
5897 but we don't handle values other than 1 correctly yet.) */
5898 tem = invert_truthvalue (arg0);
5899 /* Avoid infinite recursion. */
5900 if (TREE_CODE (tem) == TRUTH_NOT_EXPR)
5902 return convert (type, tem);
5904 case TRUTH_ANDIF_EXPR:
5905 /* Note that the operands of this must be ints
5906 and their values must be 0 or 1.
5907 ("true" is a fixed value perhaps depending on the language.) */
5908 /* If first arg is constant zero, return it. */
5909 if (integer_zerop (arg0))
5911 case TRUTH_AND_EXPR:
5912 /* If either arg is constant true, drop it. */
5913 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
5914 return non_lvalue (arg1);
5915 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
5916 return non_lvalue (arg0);
5917 /* If second arg is constant zero, result is zero, but first arg
5918 must be evaluated. */
5919 if (integer_zerop (arg1))
5920 return omit_one_operand (type, arg1, arg0);
5921 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
5922 case will be handled here. */
5923 if (integer_zerop (arg0))
5924 return omit_one_operand (type, arg0, arg1);
5927 /* We only do these simplifications if we are optimizing. */
5931 /* Check for things like (A || B) && (A || C). We can convert this
5932 to A || (B && C). Note that either operator can be any of the four
5933 truth and/or operations and the transformation will still be
5934 valid. Also note that we only care about order for the
5935 ANDIF and ORIF operators. If B contains side effects, this
5936 might change the truth-value of A. */
5937 if (TREE_CODE (arg0) == TREE_CODE (arg1)
5938 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
5939 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
5940 || TREE_CODE (arg0) == TRUTH_AND_EXPR
5941 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
5942 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
5944 tree a00 = TREE_OPERAND (arg0, 0);
5945 tree a01 = TREE_OPERAND (arg0, 1);
5946 tree a10 = TREE_OPERAND (arg1, 0);
5947 tree a11 = TREE_OPERAND (arg1, 1);
5948 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
5949 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
5950 && (code == TRUTH_AND_EXPR
5951 || code == TRUTH_OR_EXPR));
5953 if (operand_equal_p (a00, a10, 0))
5954 return fold (build (TREE_CODE (arg0), type, a00,
5955 fold (build (code, type, a01, a11))));
5956 else if (commutative && operand_equal_p (a00, a11, 0))
5957 return fold (build (TREE_CODE (arg0), type, a00,
5958 fold (build (code, type, a01, a10))));
5959 else if (commutative && operand_equal_p (a01, a10, 0))
5960 return fold (build (TREE_CODE (arg0), type, a01,
5961 fold (build (code, type, a00, a11))));
5963 /* This case if tricky because we must either have commutative
5964 operators or else A10 must not have side-effects. */
5966 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
5967 && operand_equal_p (a01, a11, 0))
5968 return fold (build (TREE_CODE (arg0), type,
5969 fold (build (code, type, a00, a10)),
5973 /* See if we can build a range comparison. */
5974 if (0 != (tem = fold_range_test (t)))
5977 /* Check for the possibility of merging component references. If our
5978 lhs is another similar operation, try to merge its rhs with our
5979 rhs. Then try to merge our lhs and rhs. */
5980 if (TREE_CODE (arg0) == code
5981 && 0 != (tem = fold_truthop (code, type,
5982 TREE_OPERAND (arg0, 1), arg1)))
5983 return fold (build (code, type, TREE_OPERAND (arg0, 0), tem));
5985 if ((tem = fold_truthop (code, type, arg0, arg1)) != 0)
5990 case TRUTH_ORIF_EXPR:
5991 /* Note that the operands of this must be ints
5992 and their values must be 0 or true.
5993 ("true" is a fixed value perhaps depending on the language.) */
5994 /* If first arg is constant true, return it. */
5995 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
5998 /* If either arg is constant zero, drop it. */
5999 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
6000 return non_lvalue (arg1);
6001 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1))
6002 return non_lvalue (arg0);
6003 /* If second arg is constant true, result is true, but we must
6004 evaluate first arg. */
6005 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
6006 return omit_one_operand (type, arg1, arg0);
6007 /* Likewise for first arg, but note this only occurs here for
6009 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
6010 return omit_one_operand (type, arg0, arg1);
6013 case TRUTH_XOR_EXPR:
6014 /* If either arg is constant zero, drop it. */
6015 if (integer_zerop (arg0))
6016 return non_lvalue (arg1);
6017 if (integer_zerop (arg1))
6018 return non_lvalue (arg0);
6019 /* If either arg is constant true, this is a logical inversion. */
6020 if (integer_onep (arg0))
6021 return non_lvalue (invert_truthvalue (arg1));
6022 if (integer_onep (arg1))
6023 return non_lvalue (invert_truthvalue (arg0));
6032 if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
6034 /* (-a) CMP (-b) -> b CMP a */
6035 if (TREE_CODE (arg0) == NEGATE_EXPR
6036 && TREE_CODE (arg1) == NEGATE_EXPR)
6037 return fold (build (code, type, TREE_OPERAND (arg1, 0),
6038 TREE_OPERAND (arg0, 0)));
6039 /* (-a) CMP CST -> a swap(CMP) (-CST) */
6040 if (TREE_CODE (arg0) == NEGATE_EXPR && TREE_CODE (arg1) == REAL_CST)
6043 (swap_tree_comparison (code), type,
6044 TREE_OPERAND (arg0, 0),
6045 build_real (TREE_TYPE (arg1),
6046 REAL_VALUE_NEGATE (TREE_REAL_CST (arg1)))));
6047 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
6048 /* a CMP (-0) -> a CMP 0 */
6049 if (TREE_CODE (arg1) == REAL_CST
6050 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (arg1)))
6051 return fold (build (code, type, arg0,
6052 build_real (TREE_TYPE (arg1), dconst0)));
6056 /* If one arg is a constant integer, put it last. */
6057 if (TREE_CODE (arg0) == INTEGER_CST
6058 && TREE_CODE (arg1) != INTEGER_CST)
6060 TREE_OPERAND (t, 0) = arg1;
6061 TREE_OPERAND (t, 1) = arg0;
6062 arg0 = TREE_OPERAND (t, 0);
6063 arg1 = TREE_OPERAND (t, 1);
6064 code = swap_tree_comparison (code);
6065 TREE_SET_CODE (t, code);
6068 /* Convert foo++ == CONST into ++foo == CONST + INCR.
6069 First, see if one arg is constant; find the constant arg
6070 and the other one. */
6072 tree constop = 0, varop = NULL_TREE;
6073 int constopnum = -1;
6075 if (TREE_CONSTANT (arg1))
6076 constopnum = 1, constop = arg1, varop = arg0;
6077 if (TREE_CONSTANT (arg0))
6078 constopnum = 0, constop = arg0, varop = arg1;
6080 if (constop && TREE_CODE (varop) == POSTINCREMENT_EXPR)
6082 /* This optimization is invalid for ordered comparisons
6083 if CONST+INCR overflows or if foo+incr might overflow.
6084 This optimization is invalid for floating point due to rounding.
6085 For pointer types we assume overflow doesn't happen. */
6086 if (POINTER_TYPE_P (TREE_TYPE (varop))
6087 || (! FLOAT_TYPE_P (TREE_TYPE (varop))
6088 && (code == EQ_EXPR || code == NE_EXPR)))
6091 = fold (build (PLUS_EXPR, TREE_TYPE (varop),
6092 constop, TREE_OPERAND (varop, 1)));
6093 TREE_SET_CODE (varop, PREINCREMENT_EXPR);
6095 /* If VAROP is a reference to a bitfield, we must mask
6096 the constant by the width of the field. */
6097 if (TREE_CODE (TREE_OPERAND (varop, 0)) == COMPONENT_REF
6098 && DECL_BIT_FIELD(TREE_OPERAND
6099 (TREE_OPERAND (varop, 0), 1)))
6102 = TREE_INT_CST_LOW (DECL_SIZE
6104 (TREE_OPERAND (varop, 0), 1)));
6105 tree mask, unsigned_type;
6107 tree folded_compare;
6109 /* First check whether the comparison would come out
6110 always the same. If we don't do that we would
6111 change the meaning with the masking. */
6112 if (constopnum == 0)
6113 folded_compare = fold (build (code, type, constop,
6114 TREE_OPERAND (varop, 0)));
6116 folded_compare = fold (build (code, type,
6117 TREE_OPERAND (varop, 0),
6119 if (integer_zerop (folded_compare)
6120 || integer_onep (folded_compare))
6121 return omit_one_operand (type, folded_compare, varop);
6123 unsigned_type = type_for_size (size, 1);
6124 precision = TYPE_PRECISION (unsigned_type);
6125 mask = build_int_2 (~0, ~0);
6126 TREE_TYPE (mask) = unsigned_type;
6127 force_fit_type (mask, 0);
6128 mask = const_binop (RSHIFT_EXPR, mask,
6129 size_int (precision - size), 0);
6130 newconst = fold (build (BIT_AND_EXPR,
6131 TREE_TYPE (varop), newconst,
6132 convert (TREE_TYPE (varop),
6137 t = build (code, type, TREE_OPERAND (t, 0),
6138 TREE_OPERAND (t, 1));
6139 TREE_OPERAND (t, constopnum) = newconst;
6143 else if (constop && TREE_CODE (varop) == POSTDECREMENT_EXPR)
6145 if (POINTER_TYPE_P (TREE_TYPE (varop))
6146 || (! FLOAT_TYPE_P (TREE_TYPE (varop))
6147 && (code == EQ_EXPR || code == NE_EXPR)))
6150 = fold (build (MINUS_EXPR, TREE_TYPE (varop),
6151 constop, TREE_OPERAND (varop, 1)));
6152 TREE_SET_CODE (varop, PREDECREMENT_EXPR);
6154 if (TREE_CODE (TREE_OPERAND (varop, 0)) == COMPONENT_REF
6155 && DECL_BIT_FIELD(TREE_OPERAND
6156 (TREE_OPERAND (varop, 0), 1)))
6159 = TREE_INT_CST_LOW (DECL_SIZE
6161 (TREE_OPERAND (varop, 0), 1)));
6162 tree mask, unsigned_type;
6164 tree folded_compare;
6166 if (constopnum == 0)
6167 folded_compare = fold (build (code, type, constop,
6168 TREE_OPERAND (varop, 0)));
6170 folded_compare = fold (build (code, type,
6171 TREE_OPERAND (varop, 0),
6173 if (integer_zerop (folded_compare)
6174 || integer_onep (folded_compare))
6175 return omit_one_operand (type, folded_compare, varop);
6177 unsigned_type = type_for_size (size, 1);
6178 precision = TYPE_PRECISION (unsigned_type);
6179 mask = build_int_2 (~0, ~0);
6180 TREE_TYPE (mask) = TREE_TYPE (varop);
6181 force_fit_type (mask, 0);
6182 mask = const_binop (RSHIFT_EXPR, mask,
6183 size_int (precision - size), 0);
6184 newconst = fold (build (BIT_AND_EXPR,
6185 TREE_TYPE (varop), newconst,
6186 convert (TREE_TYPE (varop),
6191 t = build (code, type, TREE_OPERAND (t, 0),
6192 TREE_OPERAND (t, 1));
6193 TREE_OPERAND (t, constopnum) = newconst;
6199 /* Change X >= CST to X > (CST - 1) if CST is positive. */
6200 if (TREE_CODE (arg1) == INTEGER_CST
6201 && TREE_CODE (arg0) != INTEGER_CST
6202 && tree_int_cst_sgn (arg1) > 0)
6204 switch (TREE_CODE (t))
6208 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
6209 t = build (code, type, TREE_OPERAND (t, 0), arg1);
6214 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
6215 t = build (code, type, TREE_OPERAND (t, 0), arg1);
6223 /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
6224 a MINUS_EXPR of a constant, we can convert it into a comparison with
6225 a revised constant as long as no overflow occurs. */
6226 if ((code == EQ_EXPR || code == NE_EXPR)
6227 && TREE_CODE (arg1) == INTEGER_CST
6228 && (TREE_CODE (arg0) == PLUS_EXPR
6229 || TREE_CODE (arg0) == MINUS_EXPR)
6230 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
6231 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
6232 ? MINUS_EXPR : PLUS_EXPR,
6233 arg1, TREE_OPERAND (arg0, 1), 0))
6234 && ! TREE_CONSTANT_OVERFLOW (tem))
6235 return fold (build (code, type, TREE_OPERAND (arg0, 0), tem));
6237 /* Similarly for a NEGATE_EXPR. */
6238 else if ((code == EQ_EXPR || code == NE_EXPR)
6239 && TREE_CODE (arg0) == NEGATE_EXPR
6240 && TREE_CODE (arg1) == INTEGER_CST
6241 && 0 != (tem = negate_expr (arg1))
6242 && TREE_CODE (tem) == INTEGER_CST
6243 && ! TREE_CONSTANT_OVERFLOW (tem))
6244 return fold (build (code, type, TREE_OPERAND (arg0, 0), tem));
6246 /* If we have X - Y == 0, we can convert that to X == Y and similarly
6247 for !=. Don't do this for ordered comparisons due to overflow. */
6248 else if ((code == NE_EXPR || code == EQ_EXPR)
6249 && integer_zerop (arg1) && TREE_CODE (arg0) == MINUS_EXPR)
6250 return fold (build (code, type,
6251 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1)));
6253 /* If we are widening one operand of an integer comparison,
6254 see if the other operand is similarly being widened. Perhaps we
6255 can do the comparison in the narrower type. */
6256 else if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
6257 && TREE_CODE (arg0) == NOP_EXPR
6258 && (tem = get_unwidened (arg0, NULL_TREE)) != arg0
6259 && (t1 = get_unwidened (arg1, TREE_TYPE (tem))) != 0
6260 && (TREE_TYPE (t1) == TREE_TYPE (tem)
6261 || (TREE_CODE (t1) == INTEGER_CST
6262 && int_fits_type_p (t1, TREE_TYPE (tem)))))
6263 return fold (build (code, type, tem, convert (TREE_TYPE (tem), t1)));
6265 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
6266 constant, we can simplify it. */
6267 else if (TREE_CODE (arg1) == INTEGER_CST
6268 && (TREE_CODE (arg0) == MIN_EXPR
6269 || TREE_CODE (arg0) == MAX_EXPR)
6270 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
6271 return optimize_minmax_comparison (t);
6273 /* If we are comparing an ABS_EXPR with a constant, we can
6274 convert all the cases into explicit comparisons, but they may
6275 well not be faster than doing the ABS and one comparison.
6276 But ABS (X) <= C is a range comparison, which becomes a subtraction
6277 and a comparison, and is probably faster. */
6278 else if (code == LE_EXPR && TREE_CODE (arg1) == INTEGER_CST
6279 && TREE_CODE (arg0) == ABS_EXPR
6280 && ! TREE_SIDE_EFFECTS (arg0)
6281 && (0 != (tem = negate_expr (arg1)))
6282 && TREE_CODE (tem) == INTEGER_CST
6283 && ! TREE_CONSTANT_OVERFLOW (tem))
6284 return fold (build (TRUTH_ANDIF_EXPR, type,
6285 build (GE_EXPR, type, TREE_OPERAND (arg0, 0), tem),
6286 build (LE_EXPR, type,
6287 TREE_OPERAND (arg0, 0), arg1)));
6289 /* If this is an EQ or NE comparison with zero and ARG0 is
6290 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
6291 two operations, but the latter can be done in one less insn
6292 on machines that have only two-operand insns or on which a
6293 constant cannot be the first operand. */
6294 if (integer_zerop (arg1) && (code == EQ_EXPR || code == NE_EXPR)
6295 && TREE_CODE (arg0) == BIT_AND_EXPR)
6297 if (TREE_CODE (TREE_OPERAND (arg0, 0)) == LSHIFT_EXPR
6298 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 0), 0)))
6300 fold (build (code, type,
6301 build (BIT_AND_EXPR, TREE_TYPE (arg0),
6303 TREE_TYPE (TREE_OPERAND (arg0, 0)),
6304 TREE_OPERAND (arg0, 1),
6305 TREE_OPERAND (TREE_OPERAND (arg0, 0), 1)),
6306 convert (TREE_TYPE (arg0),
6309 else if (TREE_CODE (TREE_OPERAND (arg0, 1)) == LSHIFT_EXPR
6310 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 1), 0)))
6312 fold (build (code, type,
6313 build (BIT_AND_EXPR, TREE_TYPE (arg0),
6315 TREE_TYPE (TREE_OPERAND (arg0, 1)),
6316 TREE_OPERAND (arg0, 0),
6317 TREE_OPERAND (TREE_OPERAND (arg0, 1), 1)),
6318 convert (TREE_TYPE (arg0),
6323 /* If this is an NE or EQ comparison of zero against the result of a
6324 signed MOD operation whose second operand is a power of 2, make
6325 the MOD operation unsigned since it is simpler and equivalent. */
6326 if ((code == NE_EXPR || code == EQ_EXPR)
6327 && integer_zerop (arg1)
6328 && ! TREE_UNSIGNED (TREE_TYPE (arg0))
6329 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
6330 || TREE_CODE (arg0) == CEIL_MOD_EXPR
6331 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
6332 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
6333 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6335 tree newtype = unsigned_type (TREE_TYPE (arg0));
6336 tree newmod = build (TREE_CODE (arg0), newtype,
6337 convert (newtype, TREE_OPERAND (arg0, 0)),
6338 convert (newtype, TREE_OPERAND (arg0, 1)));
6340 return build (code, type, newmod, convert (newtype, arg1));
6343 /* If this is an NE comparison of zero with an AND of one, remove the
6344 comparison since the AND will give the correct value. */
6345 if (code == NE_EXPR && integer_zerop (arg1)
6346 && TREE_CODE (arg0) == BIT_AND_EXPR
6347 && integer_onep (TREE_OPERAND (arg0, 1)))
6348 return convert (type, arg0);
6350 /* If we have (A & C) == C where C is a power of 2, convert this into
6351 (A & C) != 0. Similarly for NE_EXPR. */
6352 if ((code == EQ_EXPR || code == NE_EXPR)
6353 && TREE_CODE (arg0) == BIT_AND_EXPR
6354 && integer_pow2p (TREE_OPERAND (arg0, 1))
6355 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
6356 return build (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
6357 arg0, integer_zero_node);
6359 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
6360 and similarly for >= into !=. */
6361 if ((code == LT_EXPR || code == GE_EXPR)
6362 && TREE_UNSIGNED (TREE_TYPE (arg0))
6363 && TREE_CODE (arg1) == LSHIFT_EXPR
6364 && integer_onep (TREE_OPERAND (arg1, 0)))
6365 return build (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
6366 build (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
6367 TREE_OPERAND (arg1, 1)),
6368 convert (TREE_TYPE (arg0), integer_zero_node));
6370 else if ((code == LT_EXPR || code == GE_EXPR)
6371 && TREE_UNSIGNED (TREE_TYPE (arg0))
6372 && (TREE_CODE (arg1) == NOP_EXPR
6373 || TREE_CODE (arg1) == CONVERT_EXPR)
6374 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
6375 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
6377 build (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
6378 convert (TREE_TYPE (arg0),
6379 build (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
6380 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1))),
6381 convert (TREE_TYPE (arg0), integer_zero_node));
6383 /* Simplify comparison of something with itself. (For IEEE
6384 floating-point, we can only do some of these simplifications.) */
6385 if (operand_equal_p (arg0, arg1, 0))
6392 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
6393 return constant_boolean_node (1, type);
6395 TREE_SET_CODE (t, code);
6399 /* For NE, we can only do this simplification if integer. */
6400 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
6402 /* ... fall through ... */
6405 return constant_boolean_node (0, type);
6411 /* An unsigned comparison against 0 can be simplified. */
6412 if (integer_zerop (arg1)
6413 && (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
6414 || POINTER_TYPE_P (TREE_TYPE (arg1)))
6415 && TREE_UNSIGNED (TREE_TYPE (arg1)))
6417 switch (TREE_CODE (t))
6421 TREE_SET_CODE (t, NE_EXPR);
6425 TREE_SET_CODE (t, EQ_EXPR);
6428 return omit_one_operand (type,
6429 convert (type, integer_one_node),
6432 return omit_one_operand (type,
6433 convert (type, integer_zero_node),
6440 /* Comparisons with the highest or lowest possible integer of
6441 the specified size will have known values and an unsigned
6442 <= 0x7fffffff can be simplified. */
6444 int width = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg1)));
6446 if (TREE_CODE (arg1) == INTEGER_CST
6447 && ! TREE_CONSTANT_OVERFLOW (arg1)
6448 && width <= HOST_BITS_PER_WIDE_INT
6449 && (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
6450 || POINTER_TYPE_P (TREE_TYPE (arg1))))
6452 if (TREE_INT_CST_HIGH (arg1) == 0
6453 && (TREE_INT_CST_LOW (arg1)
6454 == ((unsigned HOST_WIDE_INT) 1 << (width - 1)) - 1)
6455 && ! TREE_UNSIGNED (TREE_TYPE (arg1)))
6456 switch (TREE_CODE (t))
6459 return omit_one_operand (type,
6460 convert (type, integer_zero_node),
6463 TREE_SET_CODE (t, EQ_EXPR);
6467 return omit_one_operand (type,
6468 convert (type, integer_one_node),
6471 TREE_SET_CODE (t, NE_EXPR);
6478 else if (TREE_INT_CST_HIGH (arg1) == -1
6479 && (- TREE_INT_CST_LOW (arg1)
6480 == ((unsigned HOST_WIDE_INT) 1 << (width - 1)))
6481 && ! TREE_UNSIGNED (TREE_TYPE (arg1)))
6482 switch (TREE_CODE (t))
6485 return omit_one_operand (type,
6486 convert (type, integer_zero_node),
6489 TREE_SET_CODE (t, EQ_EXPR);
6493 return omit_one_operand (type,
6494 convert (type, integer_one_node),
6497 TREE_SET_CODE (t, NE_EXPR);
6504 else if (TREE_INT_CST_HIGH (arg1) == 0
6505 && (TREE_INT_CST_LOW (arg1)
6506 == ((unsigned HOST_WIDE_INT) 1 << (width - 1)) - 1)
6507 && TREE_UNSIGNED (TREE_TYPE (arg1)))
6509 switch (TREE_CODE (t))
6512 return fold (build (GE_EXPR, type,
6513 convert (signed_type (TREE_TYPE (arg0)),
6515 convert (signed_type (TREE_TYPE (arg1)),
6516 integer_zero_node)));
6518 return fold (build (LT_EXPR, type,
6519 convert (signed_type (TREE_TYPE (arg0)),
6521 convert (signed_type (TREE_TYPE (arg1)),
6522 integer_zero_node)));
6530 /* If we are comparing an expression that just has comparisons
6531 of two integer values, arithmetic expressions of those comparisons,
6532 and constants, we can simplify it. There are only three cases
6533 to check: the two values can either be equal, the first can be
6534 greater, or the second can be greater. Fold the expression for
6535 those three values. Since each value must be 0 or 1, we have
6536 eight possibilities, each of which corresponds to the constant 0
6537 or 1 or one of the six possible comparisons.
6539 This handles common cases like (a > b) == 0 but also handles
6540 expressions like ((x > y) - (y > x)) > 0, which supposedly
6541 occur in macroized code. */
6543 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
6545 tree cval1 = 0, cval2 = 0;
6548 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
6549 /* Don't handle degenerate cases here; they should already
6550 have been handled anyway. */
6551 && cval1 != 0 && cval2 != 0
6552 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
6553 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
6554 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
6555 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
6556 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
6557 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
6558 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
6560 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
6561 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
6563 /* We can't just pass T to eval_subst in case cval1 or cval2
6564 was the same as ARG1. */
6567 = fold (build (code, type,
6568 eval_subst (arg0, cval1, maxval, cval2, minval),
6571 = fold (build (code, type,
6572 eval_subst (arg0, cval1, maxval, cval2, maxval),
6575 = fold (build (code, type,
6576 eval_subst (arg0, cval1, minval, cval2, maxval),
6579 /* All three of these results should be 0 or 1. Confirm they
6580 are. Then use those values to select the proper code
6583 if ((integer_zerop (high_result)
6584 || integer_onep (high_result))
6585 && (integer_zerop (equal_result)
6586 || integer_onep (equal_result))
6587 && (integer_zerop (low_result)
6588 || integer_onep (low_result)))
6590 /* Make a 3-bit mask with the high-order bit being the
6591 value for `>', the next for '=', and the low for '<'. */
6592 switch ((integer_onep (high_result) * 4)
6593 + (integer_onep (equal_result) * 2)
6594 + integer_onep (low_result))
6598 return omit_one_operand (type, integer_zero_node, arg0);
6619 return omit_one_operand (type, integer_one_node, arg0);
6622 t = build (code, type, cval1, cval2);
6624 return save_expr (t);
6631 /* If this is a comparison of a field, we may be able to simplify it. */
6632 if ((TREE_CODE (arg0) == COMPONENT_REF
6633 || TREE_CODE (arg0) == BIT_FIELD_REF)
6634 && (code == EQ_EXPR || code == NE_EXPR)
6635 /* Handle the constant case even without -O
6636 to make sure the warnings are given. */
6637 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
6639 t1 = optimize_bit_field_compare (code, type, arg0, arg1);
6643 /* If this is a comparison of complex values and either or both sides
6644 are a COMPLEX_EXPR or COMPLEX_CST, it is best to split up the
6645 comparisons and join them with a TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR.
6646 This may prevent needless evaluations. */
6647 if ((code == EQ_EXPR || code == NE_EXPR)
6648 && TREE_CODE (TREE_TYPE (arg0)) == COMPLEX_TYPE
6649 && (TREE_CODE (arg0) == COMPLEX_EXPR
6650 || TREE_CODE (arg1) == COMPLEX_EXPR
6651 || TREE_CODE (arg0) == COMPLEX_CST
6652 || TREE_CODE (arg1) == COMPLEX_CST))
6654 tree subtype = TREE_TYPE (TREE_TYPE (arg0));
6655 tree real0, imag0, real1, imag1;
6657 arg0 = save_expr (arg0);
6658 arg1 = save_expr (arg1);
6659 real0 = fold (build1 (REALPART_EXPR, subtype, arg0));
6660 imag0 = fold (build1 (IMAGPART_EXPR, subtype, arg0));
6661 real1 = fold (build1 (REALPART_EXPR, subtype, arg1));
6662 imag1 = fold (build1 (IMAGPART_EXPR, subtype, arg1));
6664 return fold (build ((code == EQ_EXPR ? TRUTH_ANDIF_EXPR
6667 fold (build (code, type, real0, real1)),
6668 fold (build (code, type, imag0, imag1))));
6671 /* From here on, the only cases we handle are when the result is
6672 known to be a constant.
6674 To compute GT, swap the arguments and do LT.
6675 To compute GE, do LT and invert the result.
6676 To compute LE, swap the arguments, do LT and invert the result.
6677 To compute NE, do EQ and invert the result.
6679 Therefore, the code below must handle only EQ and LT. */
6681 if (code == LE_EXPR || code == GT_EXPR)
6683 tem = arg0, arg0 = arg1, arg1 = tem;
6684 code = swap_tree_comparison (code);
6687 /* Note that it is safe to invert for real values here because we
6688 will check below in the one case that it matters. */
6692 if (code == NE_EXPR || code == GE_EXPR)
6695 code = invert_tree_comparison (code);
6698 /* Compute a result for LT or EQ if args permit;
6699 otherwise return T. */
6700 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
6702 if (code == EQ_EXPR)
6703 t1 = build_int_2 (tree_int_cst_equal (arg0, arg1), 0);
6705 t1 = build_int_2 ((TREE_UNSIGNED (TREE_TYPE (arg0))
6706 ? INT_CST_LT_UNSIGNED (arg0, arg1)
6707 : INT_CST_LT (arg0, arg1)),
6711 #if 0 /* This is no longer useful, but breaks some real code. */
6712 /* Assume a nonexplicit constant cannot equal an explicit one,
6713 since such code would be undefined anyway.
6714 Exception: on sysvr4, using #pragma weak,
6715 a label can come out as 0. */
6716 else if (TREE_CODE (arg1) == INTEGER_CST
6717 && !integer_zerop (arg1)
6718 && TREE_CONSTANT (arg0)
6719 && TREE_CODE (arg0) == ADDR_EXPR
6721 t1 = build_int_2 (0, 0);
6723 /* Two real constants can be compared explicitly. */
6724 else if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
6726 /* If either operand is a NaN, the result is false with two
6727 exceptions: First, an NE_EXPR is true on NaNs, but that case
6728 is already handled correctly since we will be inverting the
6729 result for NE_EXPR. Second, if we had inverted a LE_EXPR
6730 or a GE_EXPR into a LT_EXPR, we must return true so that it
6731 will be inverted into false. */
6733 if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
6734 || REAL_VALUE_ISNAN (TREE_REAL_CST (arg1)))
6735 t1 = build_int_2 (invert && code == LT_EXPR, 0);
6737 else if (code == EQ_EXPR)
6738 t1 = build_int_2 (REAL_VALUES_EQUAL (TREE_REAL_CST (arg0),
6739 TREE_REAL_CST (arg1)),
6742 t1 = build_int_2 (REAL_VALUES_LESS (TREE_REAL_CST (arg0),
6743 TREE_REAL_CST (arg1)),
6747 if (t1 == NULL_TREE)
6751 TREE_INT_CST_LOW (t1) ^= 1;
6753 TREE_TYPE (t1) = type;
6754 if (TREE_CODE (type) == BOOLEAN_TYPE)
6755 return truthvalue_conversion (t1);
6759 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
6760 so all simple results must be passed through pedantic_non_lvalue. */
6761 if (TREE_CODE (arg0) == INTEGER_CST)
6762 return pedantic_non_lvalue
6763 (TREE_OPERAND (t, (integer_zerop (arg0) ? 2 : 1)));
6764 else if (operand_equal_p (arg1, TREE_OPERAND (expr, 2), 0))
6765 return pedantic_omit_one_operand (type, arg1, arg0);
6767 /* If the second operand is zero, invert the comparison and swap
6768 the second and third operands. Likewise if the second operand
6769 is constant and the third is not or if the third operand is
6770 equivalent to the first operand of the comparison. */
6772 if (integer_zerop (arg1)
6773 || (TREE_CONSTANT (arg1) && ! TREE_CONSTANT (TREE_OPERAND (t, 2)))
6774 || (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
6775 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
6776 TREE_OPERAND (t, 2),
6777 TREE_OPERAND (arg0, 1))))
6779 /* See if this can be inverted. If it can't, possibly because
6780 it was a floating-point inequality comparison, don't do
6782 tem = invert_truthvalue (arg0);
6784 if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
6786 t = build (code, type, tem,
6787 TREE_OPERAND (t, 2), TREE_OPERAND (t, 1));
6789 /* arg1 should be the first argument of the new T. */
6790 arg1 = TREE_OPERAND (t, 1);
6795 /* If we have A op B ? A : C, we may be able to convert this to a
6796 simpler expression, depending on the operation and the values
6797 of B and C. IEEE floating point prevents this though,
6798 because A or B might be -0.0 or a NaN. */
6800 if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
6801 && (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
6802 || ! FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 0)))
6804 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
6805 arg1, TREE_OPERAND (arg0, 1)))
6807 tree arg2 = TREE_OPERAND (t, 2);
6808 enum tree_code comp_code = TREE_CODE (arg0);
6812 /* If we have A op 0 ? A : -A, this is A, -A, abs (A), or abs (-A),
6813 depending on the comparison operation. */
6814 if ((FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 1)))
6815 ? real_zerop (TREE_OPERAND (arg0, 1))
6816 : integer_zerop (TREE_OPERAND (arg0, 1)))
6817 && TREE_CODE (arg2) == NEGATE_EXPR
6818 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
6822 return pedantic_non_lvalue (negate_expr (arg1));
6824 return pedantic_non_lvalue (convert (type, arg1));
6827 if (TREE_UNSIGNED (TREE_TYPE (arg1)))
6828 arg1 = convert (signed_type (TREE_TYPE (arg1)), arg1);
6829 return pedantic_non_lvalue
6830 (convert (type, fold (build1 (ABS_EXPR,
6831 TREE_TYPE (arg1), arg1))));
6834 if (TREE_UNSIGNED (TREE_TYPE (arg1)))
6835 arg1 = convert (signed_type (TREE_TYPE (arg1)), arg1);
6836 return pedantic_non_lvalue
6837 (negate_expr (convert (type,
6838 fold (build1 (ABS_EXPR,
6845 /* If this is A != 0 ? A : 0, this is simply A. For ==, it is
6848 if (integer_zerop (TREE_OPERAND (arg0, 1)) && integer_zerop (arg2))
6850 if (comp_code == NE_EXPR)
6851 return pedantic_non_lvalue (convert (type, arg1));
6852 else if (comp_code == EQ_EXPR)
6853 return pedantic_non_lvalue (convert (type, integer_zero_node));
6856 /* If this is A op B ? A : B, this is either A, B, min (A, B),
6857 or max (A, B), depending on the operation. */
6859 if (operand_equal_for_comparison_p (TREE_OPERAND (arg0, 1),
6860 arg2, TREE_OPERAND (arg0, 0)))
6862 tree comp_op0 = TREE_OPERAND (arg0, 0);
6863 tree comp_op1 = TREE_OPERAND (arg0, 1);
6864 tree comp_type = TREE_TYPE (comp_op0);
6869 return pedantic_non_lvalue (convert (type, arg2));
6871 return pedantic_non_lvalue (convert (type, arg1));
6874 /* In C++ a ?: expression can be an lvalue, so put the
6875 operand which will be used if they are equal first
6876 so that we can convert this back to the
6877 corresponding COND_EXPR. */
6878 return pedantic_non_lvalue
6879 (convert (type, (fold (build (MIN_EXPR, comp_type,
6880 (comp_code == LE_EXPR
6881 ? comp_op0 : comp_op1),
6882 (comp_code == LE_EXPR
6883 ? comp_op1 : comp_op0))))));
6887 return pedantic_non_lvalue
6888 (convert (type, fold (build (MAX_EXPR, comp_type,
6889 (comp_code == GE_EXPR
6890 ? comp_op0 : comp_op1),
6891 (comp_code == GE_EXPR
6892 ? comp_op1 : comp_op0)))));
6899 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
6900 we might still be able to simplify this. For example,
6901 if C1 is one less or one more than C2, this might have started
6902 out as a MIN or MAX and been transformed by this function.
6903 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
6905 if (INTEGRAL_TYPE_P (type)
6906 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
6907 && TREE_CODE (arg2) == INTEGER_CST)
6911 /* We can replace A with C1 in this case. */
6912 arg1 = convert (type, TREE_OPERAND (arg0, 1));
6913 t = build (code, type, TREE_OPERAND (t, 0), arg1,
6914 TREE_OPERAND (t, 2));
6918 /* If C1 is C2 + 1, this is min(A, C2). */
6919 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type), 1)
6920 && operand_equal_p (TREE_OPERAND (arg0, 1),
6921 const_binop (PLUS_EXPR, arg2,
6922 integer_one_node, 0), 1))
6923 return pedantic_non_lvalue
6924 (fold (build (MIN_EXPR, type, arg1, arg2)));
6928 /* If C1 is C2 - 1, this is min(A, C2). */
6929 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type), 1)
6930 && operand_equal_p (TREE_OPERAND (arg0, 1),
6931 const_binop (MINUS_EXPR, arg2,
6932 integer_one_node, 0), 1))
6933 return pedantic_non_lvalue
6934 (fold (build (MIN_EXPR, type, arg1, arg2)));
6938 /* If C1 is C2 - 1, this is max(A, C2). */
6939 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type), 1)
6940 && operand_equal_p (TREE_OPERAND (arg0, 1),
6941 const_binop (MINUS_EXPR, arg2,
6942 integer_one_node, 0), 1))
6943 return pedantic_non_lvalue
6944 (fold (build (MAX_EXPR, type, arg1, arg2)));
6948 /* If C1 is C2 + 1, this is max(A, C2). */
6949 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type), 1)
6950 && operand_equal_p (TREE_OPERAND (arg0, 1),
6951 const_binop (PLUS_EXPR, arg2,
6952 integer_one_node, 0), 1))
6953 return pedantic_non_lvalue
6954 (fold (build (MAX_EXPR, type, arg1, arg2)));
6963 /* If the second operand is simpler than the third, swap them
6964 since that produces better jump optimization results. */
6965 if ((TREE_CONSTANT (arg1) || TREE_CODE_CLASS (TREE_CODE (arg1)) == 'd'
6966 || TREE_CODE (arg1) == SAVE_EXPR)
6967 && ! (TREE_CONSTANT (TREE_OPERAND (t, 2))
6968 || TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (t, 2))) == 'd'
6969 || TREE_CODE (TREE_OPERAND (t, 2)) == SAVE_EXPR))
6971 /* See if this can be inverted. If it can't, possibly because
6972 it was a floating-point inequality comparison, don't do
6974 tem = invert_truthvalue (arg0);
6976 if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
6978 t = build (code, type, tem,
6979 TREE_OPERAND (t, 2), TREE_OPERAND (t, 1));
6981 /* arg1 should be the first argument of the new T. */
6982 arg1 = TREE_OPERAND (t, 1);
6987 /* Convert A ? 1 : 0 to simply A. */
6988 if (integer_onep (TREE_OPERAND (t, 1))
6989 && integer_zerop (TREE_OPERAND (t, 2))
6990 /* If we try to convert TREE_OPERAND (t, 0) to our type, the
6991 call to fold will try to move the conversion inside
6992 a COND, which will recurse. In that case, the COND_EXPR
6993 is probably the best choice, so leave it alone. */
6994 && type == TREE_TYPE (arg0))
6995 return pedantic_non_lvalue (arg0);
6997 /* Look for expressions of the form A & 2 ? 2 : 0. The result of this
6998 operation is simply A & 2. */
7000 if (integer_zerop (TREE_OPERAND (t, 2))
7001 && TREE_CODE (arg0) == NE_EXPR
7002 && integer_zerop (TREE_OPERAND (arg0, 1))
7003 && integer_pow2p (arg1)
7004 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
7005 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
7007 return pedantic_non_lvalue (convert (type, TREE_OPERAND (arg0, 0)));
7012 /* When pedantic, a compound expression can be neither an lvalue
7013 nor an integer constant expression. */
7014 if (TREE_SIDE_EFFECTS (arg0) || pedantic)
7016 /* Don't let (0, 0) be null pointer constant. */
7017 if (integer_zerop (arg1))
7018 return build1 (NOP_EXPR, TREE_TYPE (arg1), arg1);
7023 return build_complex (type, arg0, arg1);
7027 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
7029 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
7030 return omit_one_operand (type, TREE_OPERAND (arg0, 0),
7031 TREE_OPERAND (arg0, 1));
7032 else if (TREE_CODE (arg0) == COMPLEX_CST)
7033 return TREE_REALPART (arg0);
7034 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
7035 return fold (build (TREE_CODE (arg0), type,
7036 fold (build1 (REALPART_EXPR, type,
7037 TREE_OPERAND (arg0, 0))),
7038 fold (build1 (REALPART_EXPR,
7039 type, TREE_OPERAND (arg0, 1)))));
7043 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
7044 return convert (type, integer_zero_node);
7045 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
7046 return omit_one_operand (type, TREE_OPERAND (arg0, 1),
7047 TREE_OPERAND (arg0, 0));
7048 else if (TREE_CODE (arg0) == COMPLEX_CST)
7049 return TREE_IMAGPART (arg0);
7050 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
7051 return fold (build (TREE_CODE (arg0), type,
7052 fold (build1 (IMAGPART_EXPR, type,
7053 TREE_OPERAND (arg0, 0))),
7054 fold (build1 (IMAGPART_EXPR, type,
7055 TREE_OPERAND (arg0, 1)))));
7058 /* Pull arithmetic ops out of the CLEANUP_POINT_EXPR where
7060 case CLEANUP_POINT_EXPR:
7061 if (! has_cleanups (arg0))
7062 return TREE_OPERAND (t, 0);
7065 enum tree_code code0 = TREE_CODE (arg0);
7066 int kind0 = TREE_CODE_CLASS (code0);
7067 tree arg00 = TREE_OPERAND (arg0, 0);
7070 if (kind0 == '1' || code0 == TRUTH_NOT_EXPR)
7071 return fold (build1 (code0, type,
7072 fold (build1 (CLEANUP_POINT_EXPR,
7073 TREE_TYPE (arg00), arg00))));
7075 if (kind0 == '<' || kind0 == '2'
7076 || code0 == TRUTH_ANDIF_EXPR || code0 == TRUTH_ORIF_EXPR
7077 || code0 == TRUTH_AND_EXPR || code0 == TRUTH_OR_EXPR
7078 || code0 == TRUTH_XOR_EXPR)
7080 arg01 = TREE_OPERAND (arg0, 1);
7082 if (TREE_CONSTANT (arg00)
7083 || ((code0 == TRUTH_ANDIF_EXPR || code0 == TRUTH_ORIF_EXPR)
7084 && ! has_cleanups (arg00)))
7085 return fold (build (code0, type, arg00,
7086 fold (build1 (CLEANUP_POINT_EXPR,
7087 TREE_TYPE (arg01), arg01))));
7089 if (TREE_CONSTANT (arg01))
7090 return fold (build (code0, type,
7091 fold (build1 (CLEANUP_POINT_EXPR,
7092 TREE_TYPE (arg00), arg00)),
7101 } /* switch (code) */
7104 /* Determine if first argument is a multiple of second argument. Return 0 if
7105 it is not, or we cannot easily determined it to be.
7107 An example of the sort of thing we care about (at this point; this routine
7108 could surely be made more general, and expanded to do what the *_DIV_EXPR's
7109 fold cases do now) is discovering that
7111 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
7117 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
7119 This code also handles discovering that
7121 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
7123 is a multiple of 8 so we don't have to worry about dealing with a
7126 Note that we *look* inside a SAVE_EXPR only to determine how it was
7127 calculated; it is not safe for fold to do much of anything else with the
7128 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
7129 at run time. For example, the latter example above *cannot* be implemented
7130 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
7131 evaluation time of the original SAVE_EXPR is not necessarily the same at
7132 the time the new expression is evaluated. The only optimization of this
7133 sort that would be valid is changing
7135 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
7139 SAVE_EXPR (I) * SAVE_EXPR (J)
7141 (where the same SAVE_EXPR (J) is used in the original and the
7142 transformed version). */
7145 multiple_of_p (type, top, bottom)
7150 if (operand_equal_p (top, bottom, 0))
7153 if (TREE_CODE (type) != INTEGER_TYPE)
7156 switch (TREE_CODE (top))
7159 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
7160 || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
7164 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
7165 && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
7168 /* Can't handle conversions from non-integral or wider integral type. */
7169 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
7170 || (TYPE_PRECISION (type)
7171 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
7174 /* .. fall through ... */
7177 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
7180 if ((TREE_CODE (bottom) != INTEGER_CST)
7181 || (tree_int_cst_sgn (top) < 0)
7182 || (tree_int_cst_sgn (bottom) < 0))
7184 return integer_zerop (const_binop (TRUNC_MOD_EXPR,