OSDN Git Service

Update FSF address.
[pf3gnuchains/gcc-fork.git] / gcc / fold-const.c
index 5a7aa46..7238e7a 100644 (file)
@@ -1,5 +1,5 @@
 /* Fold a constant sub-tree into a single node for C-compiler
-   Copyright (C) 1987, 1988, 1992 Free Software Foundation, Inc.
+   Copyright (C) 1987, 88, 92, 93, 94, 1995 Free Software Foundation, Inc.
 
 This file is part of GNU CC.
 
@@ -15,9 +15,8 @@ GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with GNU CC; see the file COPYING.  If not, write to
-the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
-
-/*@@ Fix lossage on folding division of big integers.  */
+the Free Software Foundation, 59 Temple Place - Suite 330,
+Boston, MA 02111-1307, USA.  */
 
 /*@@ This file should be rewritten to use an arbitrary precision
   @@ representation for "struct tree_int_cst" and "struct tree_real_cst".
@@ -48,11 +47,38 @@ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
 /* Handle floating overflow for `const_binop'.  */
 static jmp_buf float_error;
 
-void lshift_double ();
-void rshift_double ();
-void lrotate_double ();
-void rrotate_double ();
-static tree const_binop ();
+static void encode     PROTO((HOST_WIDE_INT *, HOST_WIDE_INT, HOST_WIDE_INT));
+static void decode     PROTO((HOST_WIDE_INT *, HOST_WIDE_INT *, HOST_WIDE_INT *));
+int div_and_round_double PROTO((enum tree_code, int, HOST_WIDE_INT,
+                                      HOST_WIDE_INT, HOST_WIDE_INT,
+                                      HOST_WIDE_INT, HOST_WIDE_INT *,
+                                      HOST_WIDE_INT *, HOST_WIDE_INT *,
+                                      HOST_WIDE_INT *));
+static int split_tree  PROTO((tree, enum tree_code, tree *, tree *, int *));
+static tree const_binop PROTO((enum tree_code, tree, tree, int));
+static tree fold_convert PROTO((tree, tree));
+static enum tree_code invert_tree_comparison PROTO((enum tree_code));
+static enum tree_code swap_tree_comparison PROTO((enum tree_code));
+static int truth_value_p PROTO((enum tree_code));
+static int operand_equal_for_comparison_p PROTO((tree, tree, tree));
+static int twoval_comparison_p PROTO((tree, tree *, tree *, int *));
+static tree eval_subst PROTO((tree, tree, tree, tree, tree));
+static tree omit_one_operand PROTO((tree, tree, tree));
+static tree pedantic_omit_one_operand PROTO((tree, tree, tree));
+static tree distribute_bit_expr PROTO((enum tree_code, tree, tree, tree));
+static tree make_bit_field_ref PROTO((tree, tree, int, int, int));
+static tree optimize_bit_field_compare PROTO((enum tree_code, tree,
+                                             tree, tree));
+static tree decode_field_reference PROTO((tree, int *, int *,
+                                         enum machine_mode *, int *,
+                                         int *, tree *));
+static int all_ones_mask_p PROTO((tree, int));
+static int simple_operand_p PROTO((tree));
+static tree range_test PROTO((enum tree_code, tree, enum tree_code,
+                              enum tree_code, tree, tree, tree));
+static tree unextend   PROTO((tree, int, int));
+static tree fold_truthop PROTO((enum tree_code, tree, tree, tree));
+static tree strip_compound_expr PROTO((tree, tree));
 
 #ifndef BRANCH_COST
 #define BRANCH_COST 1
@@ -69,46 +95,41 @@ static tree const_binop ();
 #define overflow_sum_sign(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
 \f
 /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
-   We do that by representing the two-word integer as MAX_SHORTS shorts,
-   with only 8 bits stored in each short, as a positive number.  */
+   We do that by representing the two-word integer in 4 words, with only
+   HOST_BITS_PER_WIDE_INT/2 bits stored in each word, as a positive number.  */
+
+#define LOWPART(x) \
+  ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT/2)) - 1))
+#define HIGHPART(x) \
+  ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT/2)
+#define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT/2)
 
-/* Unpack a two-word integer into MAX_SHORTS shorts.
+/* Unpack a two-word integer into 4 words.
    LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
-   SHORTS points to the array of shorts.  */
+   WORDS points to the array of HOST_WIDE_INTs.  */
 
 static void
-encode (shorts, low, hi)
-     short *shorts;
+encode (words, low, hi)
+     HOST_WIDE_INT *words;
      HOST_WIDE_INT low, hi;
 {
-  register int i;
-
-  for (i = 0; i < MAX_SHORTS / 2; i++)
-    {
-      shorts[i] = (low >> (i * 8)) & 0xff;
-      shorts[i + MAX_SHORTS / 2] = (hi >> (i * 8) & 0xff);
-    }
+  words[0] = LOWPART (low);
+  words[1] = HIGHPART (low);
+  words[2] = LOWPART (hi);
+  words[3] = HIGHPART (hi);
 }
 
-/* Pack an array of MAX_SHORTS shorts into a two-word integer.
-   SHORTS points to the array of shorts.
+/* Pack an array of 4 words into a two-word integer.
+   WORDS points to the array of words.
    The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces.  */
 
 static void
-decode (shorts, low, hi)
-     short *shorts;
+decode (words, low, hi)
+     HOST_WIDE_INT *words;
      HOST_WIDE_INT *low, *hi;
 {
-  register int i;
-  HOST_WIDE_INT lv = 0, hv = 0;
-
-  for (i = 0; i < MAX_SHORTS / 2; i++)
-    {
-      lv |= (HOST_WIDE_INT) shorts[i] << (i * 8);
-      hv |= (HOST_WIDE_INT) shorts[i + MAX_SHORTS / 2] << (i * 8);
-    }
-
-  *low = lv, *hi = hv;
+  *low = words[0] | words[1] * BASE;
+  *hi = words[2] | words[3] * BASE;
 }
 \f
 /* Make the integer constant T valid for its type
@@ -116,7 +137,10 @@ decode (shorts, low, hi)
    that don't belong in the type.
    Yield 1 if a signed overflow occurs, 0 otherwise.
    If OVERFLOW is nonzero, a signed overflow has already occurred
-   in calculating T, so propagate it.  */
+   in calculating T, so propagate it.
+
+   Make the real constant T valid for its type by calling CHECK_FLOAT_VALUE,
+   if it exists.  */
 
 int
 force_fit_type (t, overflow)
@@ -126,7 +150,16 @@ force_fit_type (t, overflow)
   HOST_WIDE_INT low, high;
   register int prec;
 
-  if (TREE_CODE (t) != INTEGER_CST)
+  if (TREE_CODE (t) == REAL_CST)
+    {
+#ifdef CHECK_FLOAT_VALUE
+      CHECK_FLOAT_VALUE (TYPE_MODE (TREE_TYPE (t)), TREE_REAL_CST (t),
+                        overflow);
+#endif
+      return overflow;
+    }
+
+  else if (TREE_CODE (t) != INTEGER_CST)
     return overflow;
 
   low = TREE_INT_CST_LOW (t);
@@ -155,7 +188,7 @@ force_fit_type (t, overflow)
 
   /* Unsigned types do not suffer sign extension or overflow.  */
   if (TREE_UNSIGNED (TREE_TYPE (t)))
-    return 0;
+    return overflow;
 
   /* If the value's sign bit is set, extend the sign.  */
   if (prec != 2 * HOST_BITS_PER_WIDE_INT
@@ -188,38 +221,27 @@ force_fit_type (t, overflow)
 /* Add two doubleword integers with doubleword result.
    Each argument is given as two `HOST_WIDE_INT' pieces.
    One argument is L1 and H1; the other, L2 and H2.
-   The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.
-   We use the 8-shorts representation internally.  */
+   The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
 
 int
 add_double (l1, h1, l2, h2, lv, hv)
      HOST_WIDE_INT l1, h1, l2, h2;
      HOST_WIDE_INT *lv, *hv;
 {
-  short arg1[MAX_SHORTS];
-  short arg2[MAX_SHORTS];
-  register int carry = 0;
-  register int i;
-
-  encode (arg1, l1, h1);
-  encode (arg2, l2, h2);
+  HOST_WIDE_INT l, h;
 
-  for (i = 0; i < MAX_SHORTS; i++)
-    {
-      carry += arg1[i] + arg2[i];
-      arg1[i] = carry & 0xff;
-      carry >>= 8;
-    }
+  l = l1 + l2;
+  h = h1 + h2 + ((unsigned HOST_WIDE_INT) l < l1);
 
-  decode (arg1, lv, hv);
-  return overflow_sum_sign (h1, h2, *hv);
+  *lv = l;
+  *hv = h;
+  return overflow_sum_sign (h1, h2, h);
 }
 
 /* Negate a doubleword integer with doubleword result.
    Return nonzero if the operation overflows, assuming it's signed.
    The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
-   The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.
-   We use the 8-shorts representation internally.  */
+   The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
 
 int
 neg_double (l1, h1, lv, hv)
@@ -244,86 +266,46 @@ neg_double (l1, h1, lv, hv)
    Return nonzero if the operation overflows, assuming it's signed.
    Each argument is given as two `HOST_WIDE_INT' pieces.
    One argument is L1 and H1; the other, L2 and H2.
-   The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.
-   We use the 8-shorts representation internally.  */
+   The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
 
 int
 mul_double (l1, h1, l2, h2, lv, hv)
      HOST_WIDE_INT l1, h1, l2, h2;
      HOST_WIDE_INT *lv, *hv;
 {
-  short arg1[MAX_SHORTS];
-  short arg2[MAX_SHORTS];
-  short prod[MAX_SHORTS * 2];
-  register int carry = 0;
+  HOST_WIDE_INT arg1[4];
+  HOST_WIDE_INT arg2[4];
+  HOST_WIDE_INT prod[4 * 2];
+  register unsigned HOST_WIDE_INT carry;
   register int i, j, k;
   HOST_WIDE_INT toplow, tophigh, neglow, neghigh;
 
-  /* These cases are used extensively, arising from pointer combinations.  */
-  if (h2 == 0)
-    {
-      if (l2 == 2)
-       {
-         int overflow = left_shift_overflows (h1, 1);
-         unsigned HOST_WIDE_INT temp = l1 + l1;
-         *hv = (h1 << 1) + (temp < l1);
-         *lv = temp;
-         return overflow;
-       }
-      if (l2 == 4)
-       {
-         int overflow = left_shift_overflows (h1, 2);
-         unsigned HOST_WIDE_INT temp = l1 + l1;
-         h1 = (h1 << 2) + ((temp < l1) << 1);
-         l1 = temp;
-         temp += temp;
-         h1 += (temp < l1);
-         *lv = temp;
-         *hv = h1;
-         return overflow;
-       }
-      if (l2 == 8)
-       {
-         int overflow = left_shift_overflows (h1, 3);
-         unsigned HOST_WIDE_INT temp = l1 + l1;
-         h1 = (h1 << 3) + ((temp < l1) << 2);
-         l1 = temp;
-         temp += temp;
-         h1 += (temp < l1) << 1;
-         l1 = temp;
-         temp += temp;
-         h1 += (temp < l1);
-         *lv = temp;
-         *hv = h1;
-         return overflow;
-       }
-    }
-
   encode (arg1, l1, h1);
   encode (arg2, l2, h2);
 
-  bzero (prod, sizeof prod);
+  bzero ((char *) prod, sizeof prod);
 
-  for (i = 0; i < MAX_SHORTS; i++)
-    for (j = 0; j < MAX_SHORTS; j++)
-      {
-       k = i + j;
-       carry = arg1[i] * arg2[j];
-       while (carry)
-         {
-           carry += prod[k];
-           prod[k] = carry & 0xff;
-           carry >>= 8;
-           k++;
-         }
-      }
+  for (i = 0; i < 4; i++)
+    {
+      carry = 0;
+      for (j = 0; j < 4; j++)
+       {
+         k = i + j;
+         /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000.  */
+         carry += arg1[i] * arg2[j];
+         /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF.  */
+         carry += prod[k];
+         prod[k] = LOWPART (carry);
+         carry = HIGHPART (carry);
+       }
+      prod[i + 4] = carry;
+    }
 
-  decode (prod, lv, hv);       /* This ignores
-                                  prod[MAX_SHORTS] -> prod[MAX_SHORTS*2-1] */
+  decode (prod, lv, hv);       /* This ignores prod[4] through prod[4*2-1] */
 
   /* Check for overflow by calculating the top half of the answer in full;
      it should agree with the low half's sign bit.  */
-  decode (prod+MAX_SHORTS, &toplow, &tophigh);
+  decode (prod+4, &toplow, &tophigh);
   if (h1 < 0)
     {
       neg_double (l2, h2, &neglow, &neghigh);
@@ -345,39 +327,33 @@ mul_double (l1, h1, l2, h2, lv, hv)
 
 void
 lshift_double (l1, h1, count, prec, lv, hv, arith)
-     HOST_WIDE_INT l1, h1;
-     int count, prec;
+     HOST_WIDE_INT l1, h1, count;
+     int prec;
      HOST_WIDE_INT *lv, *hv;
      int arith;
 {
-  short arg1[MAX_SHORTS];
-  register int i;
-  register int carry;
-
   if (count < 0)
     {
       rshift_double (l1, h1, - count, prec, lv, hv, arith);
       return;
     }
+  
+#ifdef SHIFT_COUNT_TRUNCATED
+  if (SHIFT_COUNT_TRUNCATED)
+    count %= prec;
+#endif
 
-  encode (arg1, l1, h1);
-
-  if (count > prec)
-    count = prec;
-
-  while (count > 0)
+  if (count >= HOST_BITS_PER_WIDE_INT)
     {
-      carry = 0;
-      for (i = 0; i < MAX_SHORTS; i++)
-       {
-         carry += arg1[i] << 1;
-         arg1[i] = carry & 0xff;
-         carry >>= 8;
-       }
-      count--;
+      *hv = (unsigned HOST_WIDE_INT) l1 << count - HOST_BITS_PER_WIDE_INT;
+      *lv = 0;
+    }
+  else
+    {
+      *hv = (((unsigned HOST_WIDE_INT) h1 << count)
+            | ((unsigned HOST_WIDE_INT) l1 >> HOST_BITS_PER_WIDE_INT - count - 1 >> 1));
+      *lv = (unsigned HOST_WIDE_INT) l1 << count;
     }
-
-  decode (arg1, lv, hv);
 }
 
 /* Shift the doubleword integer in L1, H1 right by COUNT places
@@ -387,72 +363,57 @@ lshift_double (l1, h1, count, prec, lv, hv, arith)
 
 void
 rshift_double (l1, h1, count, prec, lv, hv, arith)
-     HOST_WIDE_INT l1, h1, count, prec;
+     HOST_WIDE_INT l1, h1, count;
+     int prec;
      HOST_WIDE_INT *lv, *hv;
      int arith;
 {
-  short arg1[MAX_SHORTS];
-  register int i;
-  register int carry;
-
-  encode (arg1, l1, h1);
-
-  if (count > prec)
-    count = prec;
+  unsigned HOST_WIDE_INT signmask;
+  signmask = (arith
+             ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
+             : 0);
+
+#ifdef SHIFT_COUNT_TRUNCATED
+  if (SHIFT_COUNT_TRUNCATED)
+    count %= prec;
+#endif
 
-  while (count > 0)
+  if (count >= HOST_BITS_PER_WIDE_INT)
     {
-      carry = arith && arg1[7] >> 7; 
-      for (i = MAX_SHORTS - 1; i >= 0; i--)
-       {
-         carry <<= 8;
-         carry += arg1[i];
-         arg1[i] = (carry >> 1) & 0xff;
-       }
-      count--;
+      *hv = signmask;
+      *lv = ((signmask << 2 * HOST_BITS_PER_WIDE_INT - count - 1 << 1)
+            | ((unsigned HOST_WIDE_INT) h1 >> count - HOST_BITS_PER_WIDE_INT));
+    }
+  else
+    {
+      *lv = (((unsigned HOST_WIDE_INT) l1 >> count)
+            | ((unsigned HOST_WIDE_INT) h1 << HOST_BITS_PER_WIDE_INT - count - 1 << 1));
+      *hv = ((signmask << HOST_BITS_PER_WIDE_INT - count)
+            | ((unsigned HOST_WIDE_INT) h1 >> count));
     }
-
-  decode (arg1, lv, hv);
 }
 \f
-/* Rotate the doubldword integer in L1, H1 left by COUNT places
+/* Rotate the doubleword integer in L1, H1 left by COUNT places
    keeping only PREC bits of result.
    Rotate right if COUNT is negative.
    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
 
 void
 lrotate_double (l1, h1, count, prec, lv, hv)
-     HOST_WIDE_INT l1, h1, count, prec;
+     HOST_WIDE_INT l1, h1, count;
+     int prec;
      HOST_WIDE_INT *lv, *hv;
 {
-  short arg1[MAX_SHORTS];
-  register int i;
-  register int carry;
+  HOST_WIDE_INT s1l, s1h, s2l, s2h;
 
+  count %= prec;
   if (count < 0)
-    {
-      rrotate_double (l1, h1, - count, prec, lv, hv);
-      return;
-    }
-
-  encode (arg1, l1, h1);
-
-  if (count > prec)
-    count = prec;
-
-  carry = arg1[MAX_SHORTS - 1] >> 7;
-  while (count > 0)
-    {
-      for (i = 0; i < MAX_SHORTS; i++)
-       {
-         carry += arg1[i] << 1;
-         arg1[i] = carry & 0xff;
-         carry >>= 8;
-       }
-      count--;
-    }
+    count += prec;
 
-  decode (arg1, lv, hv);
+  lshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
+  rshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
+  *lv = s1l | s2l;
+  *hv = s1h | s2h;
 }
 
 /* Rotate the doubleword integer in L1, H1 left by COUNT places
@@ -461,31 +422,20 @@ lrotate_double (l1, h1, count, prec, lv, hv)
 
 void
 rrotate_double (l1, h1, count, prec, lv, hv)
-     HOST_WIDE_INT l1, h1, count, prec;
+     HOST_WIDE_INT l1, h1, count;
+     int prec;
      HOST_WIDE_INT *lv, *hv;
 {
-  short arg1[MAX_SHORTS];
-  register int i;
-  register int carry;
+  HOST_WIDE_INT s1l, s1h, s2l, s2h;
 
-  encode (arg1, l1, h1);
-
-  if (count > prec)
-    count = prec;
-
-  carry = arg1[0] & 1;
-  while (count > 0)
-    {
-      for (i = MAX_SHORTS - 1; i >= 0; i--)
-       {
-         carry <<= 8;
-         carry += arg1[i];
-         arg1[i] = (carry >> 1) & 0xff;
-       }
-      count--;
-    }
+  count %= prec;
+  if (count < 0)
+    count += prec;
 
-  decode (arg1, lv, hv);
+  rshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
+  lshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
+  *lv = s1l | s2l;
+  *hv = s1h | s2h;
 }
 \f
 /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
@@ -497,7 +447,7 @@ rrotate_double (l1, h1, count, prec, lv, hv)
    Return nonzero if the operation overflows.
    UNS nonzero says do unsigned division.  */
 
-static int
+int
 div_and_round_double (code, uns,
                      lnum_orig, hnum_orig, lden_orig, hden_orig,
                      lquo, hquo, lrem, hrem)
@@ -508,13 +458,14 @@ div_and_round_double (code, uns,
      HOST_WIDE_INT *lquo, *hquo, *lrem, *hrem;
 {
   int quo_neg = 0;
-  short num[MAX_SHORTS + 1];   /* extra element for scaling.  */
-  short den[MAX_SHORTS], quo[MAX_SHORTS];
-  register int i, j, work;
+  HOST_WIDE_INT num[4 + 1];    /* extra element for scaling.  */
+  HOST_WIDE_INT den[4], quo[4];
+  register int i, j;
+  unsigned HOST_WIDE_INT work;
   register int carry = 0;
-  unsigned HOST_WIDE_INT lnum = lnum_orig;
+  HOST_WIDE_INT lnum = lnum_orig;
   HOST_WIDE_INT hnum = hnum_orig;
-  unsigned HOST_WIDE_INT lden = lden_orig;
+  HOST_WIDE_INT lden = lden_orig;
   HOST_WIDE_INT hden = hden_orig;
   int overflow = 0;
 
@@ -541,7 +492,8 @@ div_and_round_double (code, uns,
   if (hnum == 0 && hden == 0)
     {                          /* single precision */
       *hquo = *hrem = 0;
-      *lquo = lnum / lden;     /* rounds toward zero since positive args */
+      /* This unsigned division rounds toward zero.  */
+      *lquo = lnum / (unsigned HOST_WIDE_INT) lden;
       goto finish_up;
     }
 
@@ -554,46 +506,37 @@ div_and_round_double (code, uns,
       goto finish_up;
     }
 
-  bzero (quo, sizeof quo);
+  bzero ((char *) quo, sizeof quo);
 
-  bzero (num, sizeof num);     /* to zero 9th element */
-  bzero (den, sizeof den);
+  bzero ((char *) num, sizeof num);    /* to zero 9th element */
+  bzero ((char *) den, sizeof den);
 
   encode (num, lnum, hnum); 
   encode (den, lden, hden);
 
-  /* This code requires more than just hden == 0.
-     We also have to require that we don't need more than three bytes
-     to hold CARRY.  If we ever did need four bytes to hold it, we
-     would lose part of it when computing WORK on the next round.  */
-  if (hden == 0 && ((lden << 8) >> 8) == lden)
-    {                          /* simpler algorithm */
+  /* Special code for when the divisor < BASE.  */
+  if (hden == 0 && lden < BASE)
+    {
       /* hnum != 0 already checked.  */
-      for (i = MAX_SHORTS - 1; i >= 0; i--)
+      for (i = 4 - 1; i >= 0; i--)
        {
-         work = num[i] + (carry << 8);
-         quo[i] = work / lden;
-         carry = work % lden;
+         work = num[i] + carry * BASE;
+         quo[i] = work / (unsigned HOST_WIDE_INT) lden;
+         carry = work % (unsigned HOST_WIDE_INT) lden;
        }
     }
-  else {                       /* full double precision,
-                                  with thanks to Don Knuth's
-                                  "Seminumerical Algorithms".  */
-#define BASE 256
-    int quo_est, scale, num_hi_sig, den_hi_sig, quo_hi_sig;
+  else
+    {
+      /* Full double precision division,
+        with thanks to Don Knuth's "Seminumerical Algorithms".  */
+    int quo_est, scale, num_hi_sig, den_hi_sig;
 
     /* Find the highest non-zero divisor digit.  */
-    for (i = MAX_SHORTS - 1; ; i--)
+    for (i = 4 - 1; ; i--)
       if (den[i] != 0) {
        den_hi_sig = i;
        break;
       }
-    for (i = MAX_SHORTS - 1; ; i--)
-      if (num[i] != 0) {
-       num_hi_sig = i;
-       break;
-      }
-    quo_hi_sig = num_hi_sig - den_hi_sig + 1;
 
     /* Insure that the first digit of the divisor is at least BASE/2.
        This is required by the quotient digit estimation algorithm.  */
@@ -601,43 +544,40 @@ div_and_round_double (code, uns,
     scale = BASE / (den[den_hi_sig] + 1);
     if (scale > 1) {           /* scale divisor and dividend */
       carry = 0;
-      for (i = 0; i <= MAX_SHORTS - 1; i++) {
+      for (i = 0; i <= 4 - 1; i++) {
        work = (num[i] * scale) + carry;
-       num[i] = work & 0xff;
-       carry = work >> 8;
-       if (num[i] != 0) num_hi_sig = i;
-      }
+       num[i] = LOWPART (work);
+       carry = HIGHPART (work);
+      } num[4] = carry;
       carry = 0;
-      for (i = 0; i <= MAX_SHORTS - 1; i++) {
+      for (i = 0; i <= 4 - 1; i++) {
        work = (den[i] * scale) + carry;
-       den[i] = work & 0xff;
-       carry = work >> 8;
+       den[i] = LOWPART (work);
+       carry = HIGHPART (work);
        if (den[i] != 0) den_hi_sig = i;
       }
     }
 
+    num_hi_sig = 4;
+
     /* Main loop */
-    for (i = quo_hi_sig; i > 0; i--) {
+    for (i = num_hi_sig - den_hi_sig - 1; i >= 0; i--) {
       /* guess the next quotient digit, quo_est, by dividing the first
         two remaining dividend digits by the high order quotient digit.
         quo_est is never low and is at most 2 high.  */
+      unsigned HOST_WIDE_INT tmp;
 
-      int num_hi;              /* index of highest remaining dividend digit */
-
-      num_hi = i + den_hi_sig;
-
-      work = (num[num_hi] * BASE) + (num_hi > 0 ? num[num_hi - 1] : 0);
-      if (num[num_hi] != den[den_hi_sig]) {
+      num_hi_sig = i + den_hi_sig + 1;
+      work = num[num_hi_sig] * BASE + num[num_hi_sig - 1];
+      if (num[num_hi_sig] != den[den_hi_sig])
        quo_est = work / den[den_hi_sig];
-      }
-      else {
+      else
        quo_est = BASE - 1;
-      }
 
       /* refine quo_est so it's usually correct, and at most one high.   */
-      while ((den[den_hi_sig - 1] * quo_est)
-            > (((work - (quo_est * den[den_hi_sig])) * BASE)
-                + ((num_hi - 1) > 0 ? num[num_hi - 2] : 0)))
+      tmp = work - quo_est * den[den_hi_sig];
+      if (tmp < BASE
+         && den[den_hi_sig - 1] * quo_est > (tmp * BASE + num[num_hi_sig - 2]))
        quo_est--;
 
       /* Try QUO_EST as the quotient digit, by multiplying the
@@ -645,48 +585,33 @@ div_and_round_double (code, uns,
         Keep in mind that QUO_EST is the I - 1st digit.  */
 
       carry = 0;
-
       for (j = 0; j <= den_hi_sig; j++)
        {
-         int digit;
-
-         work = num[i + j - 1] - (quo_est * den[j]) + carry;
-         digit = work & 0xff;
-         carry = work >> 8;
-         if (digit < 0)
-           {
-             digit += BASE;
-             carry--;
-           }
-         num[i + j - 1] = digit;
+         work = quo_est * den[j] + carry;
+         carry = HIGHPART (work);
+         work = num[i + j] - LOWPART (work);
+         num[i + j] = LOWPART (work);
+         carry += HIGHPART (work) != 0;
        }
 
       /* if quo_est was high by one, then num[i] went negative and
         we need to correct things.  */
 
-      if (num[num_hi] < 0)
+      if (num[num_hi_sig] < carry)
        {
          quo_est--;
          carry = 0;            /* add divisor back in */
          for (j = 0; j <= den_hi_sig; j++)
            {
-             work = num[i + j - 1] + den[j] + carry;
-             if (work > BASE)
-               {
-                 work -= BASE;
-                 carry = 1;
-               }
-             else
-               {
-                 carry = 0;
-               }
-             num[i + j - 1] = work;
+             work = num[i + j] + den[j] + carry;
+             carry = HIGHPART (work);
+             num[i + j] = LOWPART (work);
            }
-         num [num_hi] += carry;
+         num [num_hi_sig] += carry;
        }
 
       /* store the quotient digit.  */
-      quo[i - 1] = quo_est;
+      quo[i] = quo_est;
     }
   }
 
@@ -774,34 +699,20 @@ div_and_round_double (code, uns,
   return overflow;
 }
 \f
-/* Effectively truncate a real value to represent
-   the nearest possible value in a narrower mode.
-   The result is actually represented in the same data type as the argument,
-   but its value is usually different.  */
+#ifndef REAL_ARITHMETIC
+/* Effectively truncate a real value to represent the nearest possible value
+   in a narrower mode.  The result is actually represented in the same data
+   type as the argument, but its value is usually different.
+
+   A trap may occur during the FP operations and it is the responsibility
+   of the calling function to have a handler established.  */
 
 REAL_VALUE_TYPE
 real_value_truncate (mode, arg)
      enum machine_mode mode;
      REAL_VALUE_TYPE arg;
 {
-#ifdef __STDC__
-  /* Make sure the value is actually stored in memory before we turn off
-     the handler.  */
-  volatile
-#endif
-    REAL_VALUE_TYPE value;
-  jmp_buf handler, old_handler;
-  int handled;
-
-  if (setjmp (handler))
-    {
-      error ("floating overflow");
-      return dconst0;
-    }
-  handled = push_float_handler (handler, old_handler);
-  value = REAL_VALUE_TRUNCATE (mode, arg);
-  pop_float_handler (handled, old_handler);
-  return value;
+  return REAL_VALUE_TRUNCATE (mode, arg);
 }
 
 #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
@@ -950,6 +861,7 @@ target_negative (x)
   return x < 0;
 }
 #endif /* Target not IEEE */
+#endif /* no REAL_ARITHMETIC */
 \f
 /* Split a tree IN into a constant and a variable part
    that could be combined with CODE to make IN.
@@ -984,14 +896,13 @@ split_tree (in, code, varp, conp, varsignp)
     in = TREE_OPERAND (in, 0);
 
   if (TREE_CODE (in) == code
-      || (TREE_CODE (TREE_TYPE (in)) != REAL_TYPE
+      || (! FLOAT_TYPE_P (TREE_TYPE (in))
          /* We can associate addition and subtraction together
             (even though the C standard doesn't say so)
             for integers because the value is not affected.
             For reals, the value might be affected, so we can't.  */
-         &&
-         ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
-          || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
+         && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
+             || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
     {
       enum tree_code code = TREE_CODE (TREE_OPERAND (in, 0));
       if (code == INTEGER_CST)
@@ -1095,7 +1006,13 @@ const_binop (code, arg1, arg2, notrunc)
                         &low, &hi,
                         !uns);
          t = build_int_2 (low, hi);
-         break;
+         TREE_TYPE (t) = TREE_TYPE (arg1);
+         if (!notrunc)
+           force_fit_type (t, 0);
+         TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
+         TREE_CONSTANT_OVERFLOW (t)
+           = TREE_CONSTANT_OVERFLOW (arg1) | TREE_CONSTANT_OVERFLOW (arg2);
+         return t;
 
        case RROTATE_EXPR:
          int2l = - int2l;
@@ -1146,72 +1063,6 @@ const_binop (code, arg1, arg2, notrunc)
          break;
 
        case MULT_EXPR:
-         /* Optimize simple cases.  */
-         if (int1h == 0)
-           {
-             unsigned HOST_WIDE_INT temp;
-
-             switch (int1l)
-               {
-               case 0:
-                 t = build_int_2 (0, 0);
-                 goto got_it;
-               case 1:
-                 t = build_int_2 (int2l, int2h);
-                 goto got_it;
-               case 2:
-                 overflow = left_shift_overflows (int2h, 1);
-                 temp = int2l + int2l;
-                 int2h = (int2h << 1) + (temp < int2l);
-                 t = build_int_2 (temp, int2h);
-                 goto got_it;
-#if 0 /* This code can lose carries.  */
-               case 3:
-                 temp = int2l + int2l + int2l;
-                 int2h = int2h * 3 + (temp < int2l);
-                 t = build_int_2 (temp, int2h);
-                 goto got_it;
-#endif
-               case 4:
-                 overflow = left_shift_overflows (int2h, 2);
-                 temp = int2l + int2l;
-                 int2h = (int2h << 2) + ((temp < int2l) << 1);
-                 int2l = temp;
-                 temp += temp;
-                 int2h += (temp < int2l);
-                 t = build_int_2 (temp, int2h);
-                 goto got_it;
-               case 8:
-                 overflow = left_shift_overflows (int2h, 3);
-                 temp = int2l + int2l;
-                 int2h = (int2h << 3) + ((temp < int2l) << 2);
-                 int2l = temp;
-                 temp += temp;
-                 int2h += (temp < int2l) << 1;
-                 int2l = temp;
-                 temp += temp;
-                 int2h += (temp < int2l);
-                 t = build_int_2 (temp, int2h);
-                 goto got_it;
-               default:
-                 break;
-               }
-           }
-
-         if (int2h == 0)
-           {
-             if (int2l == 0)
-               {
-                 t = build_int_2 (0, 0);
-                 break;
-               }
-             if (int2l == 1)
-               {
-                 t = build_int_2 (int1l, int1h);
-                 break;
-               }
-           }
-
          overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
          t = build_int_2 (low, hi);
          break;
@@ -1286,27 +1137,40 @@ const_binop (code, arg1, arg2, notrunc)
        }
     got_it:
       TREE_TYPE (t) = TREE_TYPE (arg1);
-      TREE_CONSTANT_OVERFLOW (t)
-       = ((notrunc ? !uns && overflow : force_fit_type (t, overflow))
-          | TREE_CONSTANT_OVERFLOW (arg1)
-          | TREE_CONSTANT_OVERFLOW (arg2));
+      TREE_OVERFLOW (t)
+       = ((notrunc ? !uns && overflow : force_fit_type (t, overflow && !uns))
+          | TREE_OVERFLOW (arg1)
+          | TREE_OVERFLOW (arg2));
+      TREE_CONSTANT_OVERFLOW (t) = (TREE_OVERFLOW (t)
+                                   | TREE_CONSTANT_OVERFLOW (arg1)
+                                   | TREE_CONSTANT_OVERFLOW (arg2));
       return t;
     }
 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
   if (TREE_CODE (arg1) == REAL_CST)
     {
-      register REAL_VALUE_TYPE d1;
-      register REAL_VALUE_TYPE d2;
-      register REAL_VALUE_TYPE value;
+      REAL_VALUE_TYPE d1;
+      REAL_VALUE_TYPE d2;
+      int overflow = 0;
+      REAL_VALUE_TYPE value;
       tree t;
 
       d1 = TREE_REAL_CST (arg1);
       d2 = TREE_REAL_CST (arg2);
-      if (setjmp (float_error))
+
+      /* If either operand is a NaN, just return it.  Otherwise, set up
+        for floating-point trap; we return an overflow.  */
+      if (REAL_VALUE_ISNAN (d1))
+       return arg1;
+      else if (REAL_VALUE_ISNAN (d2))
+       return arg2;
+      else if (setjmp (float_error))
        {
-         pedwarn ("floating overflow in constant expression");
-         return build (code, TREE_TYPE (arg1), arg1, arg2);
+         t = copy_node (arg1);
+         overflow = 1;
+         goto got_float;
        }
+
       set_float_handler (float_error);
 
 #ifdef REAL_ARITHMETIC
@@ -1349,7 +1213,16 @@ const_binop (code, arg1, arg2, notrunc)
 #endif /* no REAL_ARITHMETIC */
       t = build_real (TREE_TYPE (arg1),
                      real_value_truncate (TYPE_MODE (TREE_TYPE (arg1)), value));
+    got_float:
       set_float_handler (NULL_PTR);
+
+      TREE_OVERFLOW (t)
+       = (force_fit_type (t, overflow)
+          | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
+      TREE_CONSTANT_OVERFLOW (t)
+       = TREE_OVERFLOW (t)
+         | TREE_CONSTANT_OVERFLOW (arg1)
+         | TREE_CONSTANT_OVERFLOW (arg2);
       return t;
     }
 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
@@ -1395,18 +1268,26 @@ const_binop (code, arg1, arg2, notrunc)
                             const_binop (MULT_EXPR, r2, r2, notrunc),
                             const_binop (MULT_EXPR, i2, i2, notrunc),
                             notrunc);
-           t = build_complex (const_binop (RDIV_EXPR,
-                                           const_binop (PLUS_EXPR,
-                                                        const_binop (MULT_EXPR, r1, r2, notrunc),
-                                                        const_binop (MULT_EXPR, i1, i2, notrunc),
-                                                        notrunc),
-                                           magsquared, notrunc),
-                              const_binop (RDIV_EXPR,
-                                           const_binop (MINUS_EXPR,
-                                                        const_binop (MULT_EXPR, i1, r2, notrunc),
-                                                        const_binop (MULT_EXPR, r1, i2, notrunc),
-                                                        notrunc),
-                                           magsquared, notrunc));
+
+           t = build_complex
+             (const_binop (INTEGRAL_TYPE_P (TREE_TYPE (r1))
+                           ? TRUNC_DIV_EXPR : RDIV_EXPR,
+                           const_binop (PLUS_EXPR,
+                                        const_binop (MULT_EXPR, r1, r2,
+                                                     notrunc),
+                                        const_binop (MULT_EXPR, i1, i2,
+                                                     notrunc),
+                                        notrunc),
+                           magsquared, notrunc),
+              const_binop (INTEGRAL_TYPE_P (TREE_TYPE (r1))
+                           ? TRUNC_DIV_EXPR : RDIV_EXPR,
+                           const_binop (MINUS_EXPR,
+                                        const_binop (MULT_EXPR, i1, r2,
+                                                     notrunc),
+                                        const_binop (MULT_EXPR, r1, i2,
+                                                     notrunc),
+                                        notrunc),
+                           magsquared, notrunc));
          }
          break;
 
@@ -1423,16 +1304,16 @@ const_binop (code, arg1, arg2, notrunc)
 
 tree
 size_int (number)
-     unsigned int number;
+     unsigned HOST_WIDE_INT number;
 {
   register tree t;
   /* Type-size nodes already made for small sizes.  */
   static tree size_table[2*HOST_BITS_PER_WIDE_INT + 1];
 
-  if (number >= 0 && number < 2*HOST_BITS_PER_WIDE_INT + 1
+  if (number < 2*HOST_BITS_PER_WIDE_INT + 1
       && size_table[number] != 0)
     return size_table[number];
-  if (number >= 0 && number < 2*HOST_BITS_PER_WIDE_INT + 1)
+  if (number < 2*HOST_BITS_PER_WIDE_INT + 1)
     {
       push_obstacks_nochange ();
       /* Make this a permanent node.  */
@@ -1494,35 +1375,47 @@ fold_convert (t, arg1)
      register tree arg1;
 {
   register tree type = TREE_TYPE (t);
+  int overflow = 0;
 
-  if (TREE_CODE (type) == POINTER_TYPE
-      || TREE_CODE (type) == INTEGER_TYPE
-      || TREE_CODE (type) == ENUMERAL_TYPE)
+  if (TREE_CODE (type) == POINTER_TYPE || INTEGRAL_TYPE_P (type))
     {
       if (TREE_CODE (arg1) == INTEGER_CST)
        {
+         /* If we would build a constant wider than GCC supports,
+            leave the conversion unfolded.  */
+         if (TYPE_PRECISION (type) > 2 * HOST_BITS_PER_WIDE_INT)
+           return t;
+
          /* Given an integer constant, make new constant with new type,
             appropriately sign-extended or truncated.  */
          t = build_int_2 (TREE_INT_CST_LOW (arg1),
                           TREE_INT_CST_HIGH (arg1));
          TREE_TYPE (t) = type;
          /* Indicate an overflow if (1) ARG1 already overflowed,
-            or (2) ARG1 is a too-large unsigned value and T is signed,
-            or (3) force_fit_type indicates an overflow.
-            force_fit_type can't detect (2), since it sees only T's type.  */
-         TREE_CONSTANT_OVERFLOW (t) =
-           (TREE_CONSTANT_OVERFLOW (arg1)
-            | (TREE_INT_CST_HIGH (arg1) < 0
-               & TREE_UNSIGNED (type) < TREE_UNSIGNED (TREE_TYPE (arg1)))
-            | force_fit_type (t, 0));
+            or (2) force_fit_type indicates an overflow.
+            Tell force_fit_type that an overflow has already occurred
+            if ARG1 is a too-large unsigned value and T is signed.  */
+         TREE_OVERFLOW (t)
+           = (TREE_OVERFLOW (arg1)
+              | force_fit_type (t,
+                                (TREE_INT_CST_HIGH (arg1) < 0
+                                 & (TREE_UNSIGNED (type)
+                                    < TREE_UNSIGNED (TREE_TYPE (arg1))))));
+         TREE_CONSTANT_OVERFLOW (t)
+           = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
        }
 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
       else if (TREE_CODE (arg1) == REAL_CST)
        {
-         REAL_VALUE_TYPE
-           l = real_value_from_int_cst (TYPE_MIN_VALUE (type)),
-           x = TREE_REAL_CST (arg1),
-           u = real_value_from_int_cst (TYPE_MAX_VALUE (type));
+         /* Don't initialize these, use assignments.
+            Initialized local aggregates don't work on old compilers.  */
+         REAL_VALUE_TYPE x;
+         REAL_VALUE_TYPE l;
+         REAL_VALUE_TYPE u;
+
+         x = TREE_REAL_CST (arg1);
+         l = real_value_from_int_cst (TYPE_MIN_VALUE (type));
+         u = real_value_from_int_cst (TYPE_MAX_VALUE (type));
          /* See if X will be in range after truncation towards 0.
             To compensate for truncation, move the bounds away from 0,
             but reject if X exactly equals the adjusted bounds.  */
@@ -1533,31 +1426,31 @@ fold_convert (t, arg1)
          l--;
          u++;
 #endif
-         if (! (REAL_VALUES_LESS (l, x) && REAL_VALUES_LESS (x, u)))
-           {
-             pedwarn ("real constant out of range for integer conversion");
-             return t;
-           }
+         /* If X is a NaN, use zero instead and show we have an overflow.
+            Otherwise, range check.  */
+         if (REAL_VALUE_ISNAN (x))
+           overflow = 1, x = dconst0;
+         else if (! (REAL_VALUES_LESS (l, x) && REAL_VALUES_LESS (x, u)))
+           overflow = 1;
+
 #ifndef REAL_ARITHMETIC
          {
-           REAL_VALUE_TYPE d;
            HOST_WIDE_INT low, high;
            HOST_WIDE_INT half_word
              = (HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2);
 
-           d = TREE_REAL_CST (arg1);
-           if (d < 0)
-             d = -d;
+           if (x < 0)
+             x = -x;
 
-           high = (HOST_WIDE_INT) (d / half_word / half_word);
-           d -= (REAL_VALUE_TYPE) high * half_word * half_word;
-           if (d >= (REAL_VALUE_TYPE) half_word * half_word / 2)
+           high = (HOST_WIDE_INT) (x / half_word / half_word);
+           x -= (REAL_VALUE_TYPE) high * half_word * half_word;
+           if (x >= (REAL_VALUE_TYPE) half_word * half_word / 2)
              {
-               low = d - (REAL_VALUE_TYPE) half_word * half_word / 2;
+               low = x - (REAL_VALUE_TYPE) half_word * half_word / 2;
                low |= (HOST_WIDE_INT) -1 << (HOST_BITS_PER_WIDE_INT - 1);
              }
            else
-             low = (HOST_WIDE_INT) d;
+             low = (HOST_WIDE_INT) x;
            if (TREE_REAL_CST (arg1) < 0)
              neg_double (low, high, &low, &high);
            t = build_int_2 (low, high);
@@ -1565,12 +1458,15 @@ fold_convert (t, arg1)
 #else
          {
            HOST_WIDE_INT low, high;
-           REAL_VALUE_TO_INT (low, high, TREE_REAL_CST (arg1));
+           REAL_VALUE_TO_INT (&low, &high, x);
            t = build_int_2 (low, high);
          }
 #endif
          TREE_TYPE (t) = type;
-         force_fit_type (t, 0);
+         TREE_OVERFLOW (t)
+           = TREE_OVERFLOW (arg1) | force_fit_type (t, overflow);
+         TREE_CONSTANT_OVERFLOW (t)
+           = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
        }
 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
       TREE_TYPE (t) = type;
@@ -1583,16 +1479,25 @@ fold_convert (t, arg1)
 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
       if (TREE_CODE (arg1) == REAL_CST)
        {
-         if (setjmp (float_error))
+         if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1)))
+           return arg1;
+         else if (setjmp (float_error))
            {
-             pedwarn ("floating overflow in constant expression");
-             return t;
+             overflow = 1;
+             t = copy_node (arg1);
+             goto got_it;
            }
          set_float_handler (float_error);
 
          t = build_real (type, real_value_truncate (TYPE_MODE (type),
                                                     TREE_REAL_CST (arg1)));
          set_float_handler (NULL_PTR);
+
+       got_it:
+         TREE_OVERFLOW (t)
+           = TREE_OVERFLOW (arg1) | force_fit_type (t, overflow);
+         TREE_CONSTANT_OVERFLOW (t)
+           = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
          return t;
        }
     }
@@ -1618,6 +1523,9 @@ non_lvalue (x)
     {
       if (TREE_CODE (x) == INTEGER_CST && integer_zerop (x))
        {
+         /* Use NOP_EXPR instead of NON_LVALUE_EXPR
+            so convert_for_assignment won't strip it.
+            This is so this 0 won't be treated as a null pointer constant.  */
          result = build1 (NOP_EXPR, TREE_TYPE (x), x);
          TREE_CONSTANT (result) = TREE_CONSTANT (x);
          return result;
@@ -1629,6 +1537,24 @@ non_lvalue (x)
   TREE_CONSTANT (result) = TREE_CONSTANT (x);
   return result;
 }
+
+/* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
+   Zero means allow extended lvalues.  */
+
+int pedantic_lvalues;
+
+/* When pedantic, return an expr equal to X but certainly not valid as a
+   pedantic lvalue.  Otherwise, return X.  */
+
+tree
+pedantic_non_lvalue (x)
+     tree x;
+{
+  if (pedantic_lvalues)
+    return non_lvalue (x);
+  else
+    return x;
+}
 \f
 /* Given a tree comparison code, return the code that is the logical inverse
    of the given code.  It is not safe to do this for floating-point
@@ -1681,6 +1607,18 @@ swap_tree_comparison (code)
       abort ();
     }
 }
+
+/* Return nonzero if CODE is a tree code that represents a truth value.  */
+
+static int
+truth_value_p (code)
+     enum tree_code code;
+{
+  return (TREE_CODE_CLASS (code) == '<'
+         || code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
+         || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
+         || code == TRUTH_XOR_EXPR || code == TRUTH_NOT_EXPR);
+}
 \f
 /* Return nonzero if two operands are necessarily equal.
    If ONLY_CONST is non-zero, only return non-zero for constants.
@@ -1727,7 +1665,8 @@ operand_equal_p (arg0, arg1, only_const)
   /* Detect when real constants are equal.  */
   if (TREE_CODE (arg0) == TREE_CODE (arg1)
       && TREE_CODE (arg0) == REAL_CST)
-    return !bcmp (&TREE_REAL_CST (arg0), &TREE_REAL_CST (arg1),
+    return !bcmp ((char *) &TREE_REAL_CST (arg0),
+                 (char *) &TREE_REAL_CST (arg1),
                  sizeof (REAL_VALUE_TYPE));
 
   if (only_const)
@@ -1802,12 +1741,13 @@ operand_equal_for_comparison_p (arg0, arg1, other)
 {
   int unsignedp1, unsignedpo;
   tree primarg1, primother;
-  int correct_width;
+  unsigned correct_width;
 
   if (operand_equal_p (arg0, arg1, 0))
     return 1;
 
-  if (TREE_CODE (TREE_TYPE (arg0)) != INTEGER_TYPE)
+  if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
+      || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
     return 0;
 
   /* Duplicate what shorten_compare does to ARG1 and see if that gives the
@@ -1844,46 +1784,65 @@ operand_equal_for_comparison_p (arg0, arg1, other)
    two different values, which will be stored in *CVAL1 and *CVAL2; if
    they are non-zero it means that some operands have already been found.
    No variables may be used anywhere else in the expression except in the
-   comparisons.
+   comparisons.  If SAVE_P is true it means we removed a SAVE_EXPR around
+   the expression and save_expr needs to be called with CVAL1 and CVAL2.
 
    If this is true, return 1.  Otherwise, return zero.  */
 
 static int
-twoval_comparison_p (arg, cval1, cval2)
+twoval_comparison_p (arg, cval1, cval2, save_p)
      tree arg;
      tree *cval1, *cval2;
+     int *save_p;
 {
   enum tree_code code = TREE_CODE (arg);
   char class = TREE_CODE_CLASS (code);
 
   /* We can handle some of the 'e' cases here.  */
-  if (class == 'e'
-      && (code == TRUTH_NOT_EXPR
-         || (code == SAVE_EXPR && SAVE_EXPR_RTL (arg) == 0)))
+  if (class == 'e' && code == TRUTH_NOT_EXPR)
     class = '1';
   else if (class == 'e'
           && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
               || code == COMPOUND_EXPR))
     class = '2';
 
+  /* ??? Disable this since the SAVE_EXPR might already be in use outside
+     the expression.  There may be no way to make this work, but it needs
+     to be looked at again for 2.6.  */
+#if 0
+  else if (class == 'e' && code == SAVE_EXPR && SAVE_EXPR_RTL (arg) == 0)
+    {
+      /* If we've already found a CVAL1 or CVAL2, this expression is
+        two complex to handle.  */
+      if (*cval1 || *cval2)
+       return 0;
+
+      class = '1';
+      *save_p = 1;
+    }
+#endif
+
   switch (class)
     {
     case '1':
-      return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2);
+      return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
 
     case '2':
-      return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
-             && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2));
+      return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
+             && twoval_comparison_p (TREE_OPERAND (arg, 1),
+                                     cval1, cval2, save_p));
 
     case 'c':
       return 1;
 
     case 'e':
       if (code == COND_EXPR)
-       return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
-               && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2)
+       return (twoval_comparison_p (TREE_OPERAND (arg, 0),
+                                    cval1, cval2, save_p)
+               && twoval_comparison_p (TREE_OPERAND (arg, 1),
+                                       cval1, cval2, save_p)
                && twoval_comparison_p (TREE_OPERAND (arg, 2),
-                                       cval1, cval2));
+                                       cval1, cval2, save_p));
       return 0;
          
     case '<':
@@ -2021,6 +1980,22 @@ omit_one_operand (type, result, omitted)
 
   return non_lvalue (t);
 }
+
+/* Similar, but call pedantic_non_lvalue instead of non_lvalue.  */
+
+static tree
+pedantic_omit_one_operand (type, result, omitted)
+     tree type, result, omitted;
+{
+  tree t = convert (type, result);
+
+  if (TREE_SIDE_EFFECTS (omitted))
+    return build (COMPOUND_EXPR, type, omitted, t);
+
+  return pedantic_non_lvalue (t);
+}
+
+
 \f
 /* Return a simplified tree node for the truth-negation of ARG.  This
    never alters ARG itself.  We assume that ARG is an operation that
@@ -2033,13 +2008,16 @@ invert_truthvalue (arg)
   tree type = TREE_TYPE (arg);
   enum tree_code code = TREE_CODE (arg);
 
+  if (code == ERROR_MARK)
+    return arg;
+
   /* If this is a comparison, we can simply invert it, except for
      floating-point non-equality comparisons, in which case we just
      enclose a TRUTH_NOT_EXPR around what we have.  */
 
   if (TREE_CODE_CLASS (code) == '<')
     {
-      if (TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))) == REAL_TYPE
+      if (FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg, 0)))
          && code != NE_EXPR && code != EQ_EXPR)
        return build1 (TRUTH_NOT_EXPR, type, arg);
       else
@@ -2109,12 +2087,20 @@ invert_truthvalue (arg)
                     invert_truthvalue (TREE_OPERAND (arg, 0)));
 
     case BIT_AND_EXPR:
-      if (! integer_onep (TREE_OPERAND (arg, 1)))
-       abort ();
+      if (!integer_onep (TREE_OPERAND (arg, 1)))
+       break;
       return build (EQ_EXPR, type, arg, convert (type, integer_zero_node));
-    }
 
-  abort ();
+    case SAVE_EXPR:
+      return build1 (TRUTH_NOT_EXPR, type, arg);
+
+    case CLEANUP_POINT_EXPR:
+      return build1 (CLEANUP_POINT_EXPR, type,
+                    invert_truthvalue (TREE_OPERAND (arg, 0)));
+    }
+  if (TREE_CODE (TREE_TYPE (arg)) != BOOLEAN_TYPE)
+    abort ();
+  return build1 (TRUTH_NOT_EXPR, type, arg);
 }
 
 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
@@ -2233,7 +2219,7 @@ optimize_bit_field_compare (code, compare_type, lhs, rhs)
      extraction at all and so can do nothing.  */
   linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
                                &lunsignedp, &lvolatilep);
-  if (lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
+  if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
       || offset != 0)
     return 0;
 
@@ -2244,7 +2230,7 @@ optimize_bit_field_compare (code, compare_type, lhs, rhs)
      rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset,
                                   &rmode, &runsignedp, &rvolatilep);
 
-     if (lbitpos != rbitpos || lbitsize != rbitsize
+     if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
         || lunsignedp != runsignedp || offset != 0)
        return 0;
    }
@@ -2289,12 +2275,14 @@ optimize_bit_field_compare (code, compare_type, lhs, rhs)
        return 0;
     }
 
-#if BYTES_BIG_ENDIAN
-  lbitpos = lnbitsize - lbitsize - lbitpos;
-#endif
+  if (BYTES_BIG_ENDIAN)
+    lbitpos = lnbitsize - lbitsize - lbitpos;
 
   /* Make the mask to be used against the extracted field.  */
-  mask = convert (unsigned_type, build_int_2 (~0, ~0));
+  mask = build_int_2 (~0, ~0);
+  TREE_TYPE (mask) = unsigned_type;
+  force_fit_type (mask, 0);
+  mask = convert (unsigned_type, mask);
   mask = const_binop (LSHIFT_EXPR, mask, size_int (lnbitsize - lbitsize), 0);
   mask = const_binop (RSHIFT_EXPR, mask,
                      size_int (lnbitsize - lbitsize - lbitpos), 0);
@@ -2359,11 +2347,16 @@ optimize_bit_field_compare (code, compare_type, lhs, rhs)
      appropriate number of bits and mask it with the computed mask
      (in case this was a signed field).  If we changed it, make a new one.  */
   lhs = make_bit_field_ref (linner, unsigned_type, lnbitsize, lnbitpos, 1);
+  if (lvolatilep)
+    {
+      TREE_SIDE_EFFECTS (lhs) = 1;
+      TREE_THIS_VOLATILE (lhs) = 1;
+    }
 
   rhs = fold (const_binop (BIT_AND_EXPR,
                           const_binop (LSHIFT_EXPR,
                                        convert (unsigned_type, rhs),
-                                       size_int (lbitpos)),
+                                       size_int (lbitpos), 0),
                           mask, 0));
 
   return build (code, compare_type,
@@ -2401,39 +2394,49 @@ decode_field_reference (exp, pbitsize, pbitpos, pmode, punsignedp,
      int *punsignedp, *pvolatilep;
      tree *pmask;
 {
-  tree mask = 0;
-  tree inner;
-  tree offset;
+  tree and_mask = 0;
+  tree mask, inner, offset;
+  tree unsigned_type;
+  int precision;
+
+  /* All the optimizations using this function assume integer fields.  
+     There are problems with FP fields since the type_for_size call
+     below can fail for, e.g., XFmode.  */
+  if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
+    return 0;
 
   STRIP_NOPS (exp);
 
   if (TREE_CODE (exp) == BIT_AND_EXPR)
     {
-      mask = TREE_OPERAND (exp, 1);
+      and_mask = TREE_OPERAND (exp, 1);
       exp = TREE_OPERAND (exp, 0);
-      STRIP_NOPS (exp); STRIP_NOPS (mask);
-      if (TREE_CODE (mask) != INTEGER_CST)
+      STRIP_NOPS (exp); STRIP_NOPS (and_mask);
+      if (TREE_CODE (and_mask) != INTEGER_CST)
        return 0;
     }
 
-  if (TREE_CODE (exp) != COMPONENT_REF && TREE_CODE (exp) != ARRAY_REF
-      && TREE_CODE (exp) != BIT_FIELD_REF)
-    return 0;
 
   inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
                               punsignedp, pvolatilep);
-  if (*pbitsize < 0 || offset != 0)
+  if ((inner == exp && and_mask == 0)
+      || *pbitsize < 0 || offset != 0)
     return 0;
   
-  if (mask == 0)
-    {
-      tree unsigned_type = type_for_size (*pbitsize, 1);
-      int precision = TYPE_PRECISION (unsigned_type);
+  /* Compute the mask to access the bitfield.  */
+  unsigned_type = type_for_size (*pbitsize, 1);
+  precision = TYPE_PRECISION (unsigned_type);
 
-      mask = convert (unsigned_type, build_int_2 (~0, ~0));
-      mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
-      mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
-    }
+  mask = build_int_2 (~0, ~0);
+  TREE_TYPE (mask) = unsigned_type;
+  force_fit_type (mask, 0);
+  mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
+  mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
+
+  /* Merge it with the mask we found in the BIT_AND_EXPR, if any.  */
+  if (and_mask != 0)
+    mask = fold (build (BIT_AND_EXPR, unsigned_type,
+                       convert (unsigned_type, and_mask), mask));
 
   *pmask = mask;
   return inner;
@@ -2449,24 +2452,23 @@ all_ones_mask_p (mask, size)
 {
   tree type = TREE_TYPE (mask);
   int precision = TYPE_PRECISION (type);
+  tree tmask;
 
+  tmask = build_int_2 (~0, ~0);
+  TREE_TYPE (tmask) = signed_type (type);
+  force_fit_type (tmask, 0);
   return
-    operand_equal_p (mask, 
-                    const_binop (RSHIFT_EXPR,
-                                 const_binop (LSHIFT_EXPR,
-                                              convert (signed_type (type),
-                                                       build_int_2 (~0, ~0)),
-                                              size_int (precision - size), 0),
-                                 size_int (precision - size), 0),
-                    0);
+    tree_int_cst_equal (mask, 
+                       const_binop (RSHIFT_EXPR,
+                                    const_binop (LSHIFT_EXPR, tmask,
+                                                 size_int (precision - size),
+                                                 0),
+                                    size_int (precision - size), 0));
 }
 
 /* Subroutine for fold_truthop: determine if an operand is simple enough
    to be evaluated unconditionally.  */
 
-#ifdef __GNUC__
-__inline
-#endif
 static int 
 simple_operand_p (exp)
      tree exp;
@@ -2508,7 +2510,7 @@ simple_operand_p (exp)
 
    We return the simplified tree or 0 if no optimization is possible.  */
 
-tree
+static tree
 range_test (jcode, type, lo_code, hi_code, var, lo_cst, hi_cst)
      enum tree_code jcode, lo_code, hi_code;
      tree type, var, lo_cst, hi_cst;
@@ -2594,8 +2596,7 @@ range_test (jcode, type, lo_code, hi_code, var, lo_cst, hi_cst)
 
   /* Fail if VAR isn't an integer.  */
   utype = TREE_TYPE (var);
-  if (TREE_CODE (utype) != INTEGER_TYPE
-      && TREE_CODE (utype) != ENUMERAL_TYPE)
+  if (! INTEGRAL_TYPE_P (utype))
     return 0;
 
   /* The range test is invalid if subtracting the two constants results
@@ -2618,6 +2619,36 @@ range_test (jcode, type, lo_code, hi_code, var, lo_cst, hi_cst)
                               const_binop (MINUS_EXPR, hi_cst, lo_cst, 0))));
 }
 \f
+/* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
+   bit value.  Arrange things so the extra bits will be set to zero if and
+   only if C is signed-extended to its full width.  */
+
+static tree
+unextend (c, p, unsignedp)
+     tree c;
+     int p;
+     int unsignedp;
+{
+  tree type = TREE_TYPE (c);
+  int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
+  tree temp;
+
+  if (p == modesize || unsignedp)
+    return c;
+
+  if (TREE_UNSIGNED (type))
+    c = convert (signed_type (type), c);
+
+  /* We work by getting just the sign bit into the low-order bit, then
+     into the high-order bit, then sign-extend.  We then XOR that value
+     with C.  */
+  temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
+  temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
+  temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
+  temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
+  return convert (type, const_binop (BIT_XOR_EXPR, c, temp, 0));
+}
+\f
 /* Find ways of folding logical expressions of LHS and RHS:
    Try to merge two comparisons to the same innermost item.
    Look for range tests like "ch >= '0' && ch <= '9'".
@@ -2674,7 +2705,9 @@ fold_truthop (code, truth_type, lhs, rhs)
   int volatilep;
 
   /* Start by getting the comparison codes and seeing if this looks like
-     a range test.  Fail if anything is volatile.  */
+     a range test.  Fail if anything is volatile.  If one operand is a
+     BIT_AND_EXPR with the constant one, treat it as if it were surrounded
+     with a NE_EXPR.  */
 
   if (TREE_SIDE_EFFECTS (lhs)
       || TREE_SIDE_EFFECTS (rhs))
@@ -2683,6 +2716,12 @@ fold_truthop (code, truth_type, lhs, rhs)
   lcode = TREE_CODE (lhs);
   rcode = TREE_CODE (rhs);
 
+  if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
+    lcode = NE_EXPR, lhs = build (NE_EXPR, truth_type, lhs, integer_zero_node);
+
+  if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
+    rcode = NE_EXPR, rhs = build (NE_EXPR, truth_type, rhs, integer_zero_node);
+
   if (TREE_CODE_CLASS (lcode) != '<'
       || TREE_CODE_CLASS (rcode) != '<')
     return 0;
@@ -2734,7 +2773,7 @@ fold_truthop (code, truth_type, lhs, rhs)
      are with zero (tmw).  */
 
   if (BRANCH_COST >= 2
-      && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
+      && INTEGRAL_TYPE_P (TREE_TYPE (rhs))
       && simple_operand_p (rl_arg)
       && simple_operand_p (rr_arg))
     return build (code, truth_type, lhs, rhs);
@@ -2814,30 +2853,50 @@ fold_truthop (code, truth_type, lhs, rhs)
   type = type_for_size (lnbitsize, 1);
   xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
 
-#if BYTES_BIG_ENDIAN
-  xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
-  xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
-#endif
+  if (BYTES_BIG_ENDIAN)
+    {
+      xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
+      xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
+    }
 
   ll_mask = const_binop (LSHIFT_EXPR, convert (type, ll_mask),
                         size_int (xll_bitpos), 0);
   rl_mask = const_binop (LSHIFT_EXPR, convert (type, rl_mask),
                         size_int (xrl_bitpos), 0);
 
-  /* Make sure the constants are interpreted as unsigned, so we
-     don't have sign bits outside the range of their type.  */
-
   if (l_const)
     {
-      l_const = convert (unsigned_type (TREE_TYPE (l_const)), l_const);
-      l_const = const_binop (LSHIFT_EXPR, convert (type, l_const),
-                            size_int (xll_bitpos), 0);
+      l_const = convert (type, unextend (l_const, ll_bitsize, ll_unsignedp));
+      l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
+      if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
+                                       fold (build1 (BIT_NOT_EXPR,
+                                                     type, ll_mask)),
+                                       0)))
+       {
+         warning ("comparison is always %s",
+                  wanted_code == NE_EXPR ? "one" : "zero");
+         
+         return convert (truth_type,
+                         wanted_code == NE_EXPR
+                         ? integer_one_node : integer_zero_node);
+       }
     }
   if (r_const)
     {
-      r_const = convert (unsigned_type (TREE_TYPE (r_const)), r_const);
-      r_const = const_binop (LSHIFT_EXPR, convert (type, r_const),
-                            size_int (xrl_bitpos), 0);
+      r_const = convert (type, unextend (r_const, rl_bitsize, rl_unsignedp));
+      r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
+      if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
+                                       fold (build1 (BIT_NOT_EXPR,
+                                                     type, rl_mask)),
+                                       0)))
+       {
+         warning ("comparison is always %s",
+                  wanted_code == NE_EXPR ? "one" : "zero");
+         
+         return convert (truth_type,
+                         wanted_code == NE_EXPR
+                         ? integer_one_node : integer_zero_node);
+       }
     }
 
   /* If the right sides are not constant, do the same for it.  Also,
@@ -2864,10 +2923,11 @@ fold_truthop (code, truth_type, lhs, rhs)
       rnbitpos = first_bit & ~ (rnbitsize - 1);
       xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
 
-#if BYTES_BIG_ENDIAN
-      xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
-      xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
-#endif
+      if (BYTES_BIG_ENDIAN)
+       {
+         xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
+         xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
+       }
 
       lr_mask = const_binop (LSHIFT_EXPR, convert (type, lr_mask),
                             size_int (xlr_bitpos), 0);
@@ -2950,6 +3010,43 @@ fold_truthop (code, truth_type, lhs, rhs)
                const_binop (BIT_IOR_EXPR, l_const, r_const, 0));
 }
 \f
+/* If T contains a COMPOUND_EXPR which was inserted merely to evaluate
+   S, a SAVE_EXPR, return the expression actually being evaluated.   Note
+   that we may sometimes modify the tree.  */
+
+static tree
+strip_compound_expr (t, s)
+     tree t;
+     tree s;
+{
+  tree type = TREE_TYPE (t);
+  enum tree_code code = TREE_CODE (t);
+
+  /* See if this is the COMPOUND_EXPR we want to eliminate.  */
+  if (code == COMPOUND_EXPR && TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR
+      && TREE_OPERAND (TREE_OPERAND (t, 0), 0) == s)
+    return TREE_OPERAND (t, 1);
+
+  /* See if this is a COND_EXPR or a simple arithmetic operator.   We
+     don't bother handling any other types.  */
+  else if (code == COND_EXPR)
+    {
+      TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s);
+      TREE_OPERAND (t, 1) = strip_compound_expr (TREE_OPERAND (t, 1), s);
+      TREE_OPERAND (t, 2) = strip_compound_expr (TREE_OPERAND (t, 2), s);
+    }
+  else if (TREE_CODE_CLASS (code) == '1')
+    TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s);
+  else if (TREE_CODE_CLASS (code) == '<'
+          || TREE_CODE_CLASS (code) == '2')
+    {
+      TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s);
+      TREE_OPERAND (t, 1) = strip_compound_expr (TREE_OPERAND (t, 1), s);
+    }
+
+  return t;
+}
+\f
 /* Perform constant folding and related simplification of EXPR.
    The related simplifications include x*1 => x, x*0 => 0, etc.,
    and application of the associative law.
@@ -2976,6 +3073,10 @@ fold (expr)
 
   int wins = 1;
 
+  /* Don't try to process an RTL_EXPR since its operands aren't trees.  */
+  if (code == RTL_EXPR)
+    return t;
+
   /* Return right away if already constant.  */
   if (TREE_CONSTANT (t))
     {
@@ -2987,6 +3088,8 @@ fold (expr)
   kind = TREE_CODE_CLASS (code);
   if (code == NOP_EXPR || code == FLOAT_EXPR || code == CONVERT_EXPR)
     {
+      tree subop;
+
       /* Special case for conversion ops that can have fixed point args.  */
       arg0 = TREE_OPERAND (t, 0);
 
@@ -2994,9 +3097,14 @@ fold (expr)
       if (arg0 != 0)
        STRIP_TYPE_NOPS (arg0);
 
-      if (arg0 != 0 && TREE_CODE (arg0) != INTEGER_CST
+      if (arg0 != 0 && TREE_CODE (arg0) == COMPLEX_CST)
+       subop = TREE_REALPART (arg0);
+      else
+       subop = arg0;
+
+      if (subop != 0 && TREE_CODE (subop) != INTEGER_CST
 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
-         && TREE_CODE (arg0) != REAL_CST
+         && TREE_CODE (subop) != REAL_CST
 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
          )
        /* Note that TREE_CONSTANT isn't enough:
@@ -3012,16 +3120,31 @@ fold (expr)
       for (i = 0; i < len; i++)
        {
          tree op = TREE_OPERAND (t, i);
+         tree subop;
 
          if (op == 0)
            continue;           /* Valid for CALL_EXPR, at least.  */
 
-         /* Strip any conversions that don't change the mode.  */
-         STRIP_NOPS (op);
+         if (kind == '<' || code == RSHIFT_EXPR)
+           {
+             /* Signedness matters here.  Perhaps we can refine this
+                later.  */
+             STRIP_TYPE_NOPS (op);
+           }
+         else
+           {
+             /* Strip any conversions that don't change the mode.  */
+             STRIP_NOPS (op);
+           }
          
-         if (TREE_CODE (op) != INTEGER_CST
+         if (TREE_CODE (op) == COMPLEX_CST)
+           subop = TREE_REALPART (op);
+         else
+           subop = op;
+
+         if (TREE_CODE (subop) != INTEGER_CST
 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
-             && TREE_CODE (op) != REAL_CST
+             && TREE_CODE (subop) != REAL_CST
 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
              )
            /* Note that TREE_CONSTANT isn't enough:
@@ -3058,7 +3181,37 @@ fold (expr)
      operation inside the compound or conditional to see if any folding
      can then be done.  Convert comparison to conditional for this purpose.
      The also optimizes non-constant cases that used to be done in
-     expand_expr.  */
+     expand_expr.
+
+     Before we do that, see if this is a BIT_AND_EXPR or a BIT_OR_EXPR,
+     one of the operands is a comparison and the other is a comparison, a
+     BIT_AND_EXPR with the constant 1, or a truth value.  In that case, the
+     code below would make the expression more complex.  Change it to a
+     TRUTH_{AND,OR}_EXPR.  Likewise, convert a similar NE_EXPR to 
+     TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR.  */
+
+  if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
+       || code == EQ_EXPR || code == NE_EXPR)
+      && ((truth_value_p (TREE_CODE (arg0))
+          && (truth_value_p (TREE_CODE (arg1))
+              || (TREE_CODE (arg1) == BIT_AND_EXPR
+                  && integer_onep (TREE_OPERAND (arg1, 1)))))
+         || (truth_value_p (TREE_CODE (arg1))
+             && (truth_value_p (TREE_CODE (arg0))
+                 || (TREE_CODE (arg0) == BIT_AND_EXPR
+                     && integer_onep (TREE_OPERAND (arg0, 1)))))))
+    {
+      t = fold (build (code == BIT_AND_EXPR ? TRUTH_AND_EXPR
+                      : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
+                      : TRUTH_XOR_EXPR,
+                      type, arg0, arg1));
+
+      if (code == EQ_EXPR)
+       t = invert_truthvalue (t);
+
+      return t;
+    }
+
   if (TREE_CODE_CLASS (code) == '1')
     {
       if (TREE_CODE (arg0) == COMPOUND_EXPR)
@@ -3071,9 +3224,13 @@ fold (expr)
                           fold (build1 (code, type, TREE_OPERAND (arg0, 2)))));
 
          /* If this was a conversion, and all we did was to move into
-            inside the COND_EXPR, bring it back out.  Then return so we
-            don't get into an infinite recursion loop taking the conversion
-            out and then back in.  */
+            inside the COND_EXPR, bring it back out.  But leave it if
+            it is a conversion from integer to integer and the
+            result precision is no wider than a word since such a
+            conversion is cheap and may be optimized away by combine,
+            while it couldn't if it were outside the COND_EXPR.  Then return
+            so we don't get into an infinite recursion loop taking the
+            conversion out and then back in.  */
 
          if ((code == NOP_EXPR || code == CONVERT_EXPR
               || code == NON_LVALUE_EXPR)
@@ -3081,7 +3238,10 @@ fold (expr)
              && TREE_CODE (TREE_OPERAND (t, 1)) == code
              && TREE_CODE (TREE_OPERAND (t, 2)) == code
              && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0))
-                 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 2), 0))))
+                 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 2), 0)))
+             && ! (INTEGRAL_TYPE_P (TREE_TYPE (t))
+                   && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0)))
+                   && TYPE_PRECISION (TREE_TYPE (t)) <= BITS_PER_WORD))
            t = build1 (code, type,
                        build (COND_EXPR,
                               TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0)),
@@ -3095,11 +3255,13 @@ fold (expr)
                            fold (build1 (code, type, integer_one_node)),
                            fold (build1 (code, type, integer_zero_node))));
    }
-  else if (TREE_CODE_CLASS (code) == '2')
+  else if (TREE_CODE_CLASS (code) == '2'
+          || TREE_CODE_CLASS (code) == '<')
     {
       if (TREE_CODE (arg1) == COMPOUND_EXPR)
        return build (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
-                     fold (build (code, type, arg0, TREE_OPERAND (arg1, 1))));
+                     fold (build (code, type,
+                                  arg0, TREE_OPERAND (arg1, 1))));
       else if (TREE_CODE (arg1) == COND_EXPR
               || TREE_CODE_CLASS (TREE_CODE (arg1)) == '<')
        {
@@ -3113,19 +3275,42 @@ fold (expr)
            }
          else
            {
+             tree testtype = TREE_TYPE (arg1);
              test = arg1;
-             true_value = integer_one_node;
-             false_value = integer_zero_node;
+             true_value = convert (testtype, integer_one_node);
+             false_value = convert (testtype, integer_zero_node);
+           }
+
+         /* If ARG0 is complex we want to make sure we only evaluate
+            it once.  Though this is only required if it is volatile, it
+            might be more efficient even if it is not.  However, if we
+            succeed in folding one part to a constant, we do not need
+            to make this SAVE_EXPR.  Since we do this optimization
+            primarily to see if we do end up with constant and this
+            SAVE_EXPR interferes with later optimizations, suppressing
+            it when we can is important.  */
+
+         if (TREE_CODE (arg0) != SAVE_EXPR
+             && ((TREE_CODE (arg0) != VAR_DECL
+                  && TREE_CODE (arg0) != PARM_DECL)
+                 || TREE_SIDE_EFFECTS (arg0)))
+           {
+             tree lhs = fold (build (code, type, arg0, true_value));
+             tree rhs = fold (build (code, type, arg0, false_value));
+
+             if (TREE_CONSTANT (lhs) || TREE_CONSTANT (rhs))
+               return fold (build (COND_EXPR, type, test, lhs, rhs));
+
+             arg0 = save_expr (arg0);
            }
 
-         if (TREE_CODE (arg0) != VAR_DECL && TREE_CODE (arg0) != PARM_DECL)
-           arg0 = save_expr (arg0);
          test = fold (build (COND_EXPR, type, test,
                              fold (build (code, type, arg0, true_value)),
                              fold (build (code, type, arg0, false_value))));
          if (TREE_CODE (arg0) == SAVE_EXPR)
            return build (COMPOUND_EXPR, type,
-                         convert (void_type_node, arg0), test);
+                         convert (void_type_node, arg0),
+                         strip_compound_expr (test, arg0));
          else
            return convert (type, test);
        }
@@ -3146,19 +3331,34 @@ fold (expr)
            }
          else
            {
+             tree testtype = TREE_TYPE (arg0);
              test = arg0;
-             true_value = integer_one_node;
-             false_value = integer_zero_node;
+             true_value = convert (testtype, integer_one_node);
+             false_value = convert (testtype, integer_zero_node);
+           }
+
+         if (TREE_CODE (arg1) != SAVE_EXPR
+             && ((TREE_CODE (arg1) != VAR_DECL
+                  && TREE_CODE (arg1) != PARM_DECL)
+                 || TREE_SIDE_EFFECTS (arg1)))
+           {
+             tree lhs = fold (build (code, type, true_value, arg1));
+             tree rhs = fold (build (code, type, false_value, arg1));
+
+             if (TREE_CONSTANT (lhs) || TREE_CONSTANT (rhs)
+                 || TREE_CONSTANT (arg1))
+               return fold (build (COND_EXPR, type, test, lhs, rhs));
+
+             arg1 = save_expr (arg1);
            }
 
-         if (TREE_CODE (arg1) != VAR_DECL && TREE_CODE (arg1) != PARM_DECL)
-           arg1 = save_expr (arg1);
          test = fold (build (COND_EXPR, type, test,
                              fold (build (code, type, true_value, arg1)),
                              fold (build (code, type, false_value, arg1))));
          if (TREE_CODE (arg1) == SAVE_EXPR)
            return build (COMPOUND_EXPR, type,
-                         convert (void_type_node, arg1), test);
+                         convert (void_type_node, arg1),
+                         strip_compound_expr (test, arg1));
          else
            return convert (type, test);
        }
@@ -3189,44 +3389,75 @@ fold (expr)
     case CONVERT_EXPR:
     case FIX_TRUNC_EXPR:
       /* Other kinds of FIX are not handled properly by fold_convert.  */
-      /* Two conversions in a row are not needed unless:
-        - the intermediate type is narrower than both initial and final, or
-        - the intermediate type and innermost type differ in signedness,
-          and the outermost type is wider than the intermediate, or
-        - the initial type is a pointer type and the precisions of the
-          intermediate and final types differ, or
-        - the final type is a pointer type and the precisions of the 
-         initial and intermediate types differ.  */
-      if ((TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
-          || TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR)
-         && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0)))
-             > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
-             ||
-             TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0)))
-             > TYPE_PRECISION (TREE_TYPE (t)))
-         && ! ((TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
-                == INTEGER_TYPE)
-               && (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
-                   == INTEGER_TYPE)
-               && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t, 0)))
-                   != TREE_UNSIGNED (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
-               && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0)))
-                   < TYPE_PRECISION (TREE_TYPE (t))))
-         && ((TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t, 0)))
-              && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0)))
-                  > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))))
-             ==
-             (TREE_UNSIGNED (TREE_TYPE (t))
-              && (TYPE_PRECISION (TREE_TYPE (t))
-                  > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0))))))
-         && ! ((TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
-                == POINTER_TYPE)
-               && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0)))
-                   != TYPE_PRECISION (TREE_TYPE (t))))
-         && ! (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE
-               && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
-                   != TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0))))))
-       return convert (TREE_TYPE (t), TREE_OPERAND (TREE_OPERAND (t, 0), 0));
+
+      if (TREE_TYPE (TREE_OPERAND (t, 0)) == TREE_TYPE (t))
+       return TREE_OPERAND (t, 0);
+
+      /* Handle cases of two conversions in a row.  */
+      if (TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
+         || TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR)
+       {
+         tree inside_type = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
+         tree inter_type = TREE_TYPE (TREE_OPERAND (t, 0));
+         tree final_type = TREE_TYPE (t);
+         int inside_int = INTEGRAL_TYPE_P (inside_type);
+         int inside_ptr = POINTER_TYPE_P (inside_type);
+         int inside_float = FLOAT_TYPE_P (inside_type);
+         int inside_prec = TYPE_PRECISION (inside_type);
+         int inside_unsignedp = TREE_UNSIGNED (inside_type);
+         int inter_int = INTEGRAL_TYPE_P (inter_type);
+         int inter_ptr = POINTER_TYPE_P (inter_type);
+         int inter_float = FLOAT_TYPE_P (inter_type);
+         int inter_prec = TYPE_PRECISION (inter_type);
+         int inter_unsignedp = TREE_UNSIGNED (inter_type);
+         int final_int = INTEGRAL_TYPE_P (final_type);
+         int final_ptr = POINTER_TYPE_P (final_type);
+         int final_float = FLOAT_TYPE_P (final_type);
+         int final_prec = TYPE_PRECISION (final_type);
+         int final_unsignedp = TREE_UNSIGNED (final_type);
+
+         /* In addition to the cases of two conversions in a row 
+            handled below, if we are converting something to its own
+            type via an object of identical or wider precision, neither
+            conversion is needed.  */
+         if (inside_type == final_type
+             && ((inter_int && final_int) || (inter_float && final_float))
+             && inter_prec >= final_prec)
+           return TREE_OPERAND (TREE_OPERAND (t, 0), 0);
+
+         /* Likewise, if the intermediate and final types are either both
+            float or both integer, we don't need the middle conversion if
+            it is wider than the final type and doesn't change the signedness
+            (for integers).  Avoid this if the final type is a pointer
+            since then we sometimes need the inner conversion.  */
+         if ((((inter_int || inter_ptr) && (inside_int || inside_ptr))
+              || (inter_float && inside_float))
+             && inter_prec >= inside_prec
+             && (inter_float || inter_unsignedp == inside_unsignedp)
+             && ! final_ptr)
+           return convert (final_type, TREE_OPERAND (TREE_OPERAND (t, 0), 0));
+
+         /* Two conversions in a row are not needed unless:
+            - some conversion is floating-point (overstrict for now), or
+            - the intermediate type is narrower than both initial and
+              final, or
+            - the intermediate type and innermost type differ in signedness,
+              and the outermost type is wider than the intermediate, or
+            - the initial type is a pointer type and the precisions of the
+              intermediate and final types differ, or
+            - the final type is a pointer type and the precisions of the 
+              initial and intermediate types differ.  */
+         if (! inside_float && ! inter_float && ! final_float
+             && (inter_prec > inside_prec || inter_prec > final_prec)
+             && ! (inside_int && inter_int
+                   && inter_unsignedp != inside_unsignedp
+                   && inter_prec < final_prec)
+             && ((inter_unsignedp && inter_prec > inside_prec)
+                 == (final_unsignedp && final_prec > inter_prec))
+             && ! (inside_ptr && inter_prec != final_prec)
+             && ! (final_ptr && inside_prec != inter_prec))
+           return convert (final_type, TREE_OPERAND (TREE_OPERAND (t, 0), 0));
+       }
 
       if (TREE_CODE (TREE_OPERAND (t, 0)) == MODIFY_EXPR
          && TREE_CONSTANT (TREE_OPERAND (TREE_OPERAND (t, 0), 1))
@@ -3269,6 +3500,15 @@ fold (expr)
       return t;
 #endif /* 0 */
 
+    case COMPONENT_REF:
+      if (TREE_CODE (arg0) == CONSTRUCTOR)
+       {
+         tree m = purpose_member (arg1, CONSTRUCTOR_ELTS (arg0));
+         if (m)
+           t = TREE_VALUE (m);
+       }
+      return t;
+
     case RANGE_EXPR:
       TREE_CONSTANT (t) = wins;
       return t;
@@ -3284,9 +3524,11 @@ fold (expr)
                                         &low, &high);
              t = build_int_2 (low, high);
              TREE_TYPE (t) = type;
-             TREE_CONSTANT_OVERFLOW (t)
-               = (TREE_CONSTANT_OVERFLOW (arg0)
+             TREE_OVERFLOW (t)
+               = (TREE_OVERFLOW (arg0)
                   | force_fit_type (t, overflow));
+             TREE_CONSTANT_OVERFLOW (t)
+               = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg0);
            }
          else if (TREE_CODE (arg0) == REAL_CST)
            t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
@@ -3296,7 +3538,7 @@ fold (expr)
        return TREE_OPERAND (arg0, 0);
 
       /* Convert - (a - b) to (b - a) for non-floating-point.  */
-      else if (TREE_CODE (arg0) == MINUS_EXPR && TREE_CODE (type) != REAL_TYPE)
+      else if (TREE_CODE (arg0) == MINUS_EXPR && ! FLOAT_TYPE_P (type))
        return build (MINUS_EXPR, type, TREE_OPERAND (arg0, 1),
                      TREE_OPERAND (arg0, 0));
 
@@ -3316,9 +3558,11 @@ fold (expr)
                                             &low, &high);
                  t = build_int_2 (low, high);
                  TREE_TYPE (t) = type;
-                 TREE_CONSTANT_OVERFLOW (t)
-                   = (TREE_CONSTANT_OVERFLOW (arg0)
+                 TREE_OVERFLOW (t)
+                   = (TREE_OVERFLOW (arg0)
                       | force_fit_type (t, overflow));
+                 TREE_CONSTANT_OVERFLOW (t)
+                   = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg0);
                }
            }
          else if (TREE_CODE (arg0) == REAL_CST)
@@ -3333,6 +3577,30 @@ fold (expr)
        return build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0));
       return t;
 
+    case CONJ_EXPR:
+      if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
+       return arg0;
+      else if (TREE_CODE (arg0) == COMPLEX_EXPR)
+       return build (COMPLEX_EXPR, TREE_TYPE (arg0),
+                     TREE_OPERAND (arg0, 0),
+                     fold (build1 (NEGATE_EXPR,
+                                   TREE_TYPE (TREE_TYPE (arg0)),
+                                   TREE_OPERAND (arg0, 1))));
+      else if (TREE_CODE (arg0) == COMPLEX_CST)
+       return build_complex (TREE_OPERAND (arg0, 0),
+                             fold (build1 (NEGATE_EXPR,
+                                           TREE_TYPE (TREE_TYPE (arg0)),
+                                           TREE_OPERAND (arg0, 1))));
+      else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
+       return fold (build (TREE_CODE (arg0), type,
+                           fold (build1 (CONJ_EXPR, type,
+                                         TREE_OPERAND (arg0, 0))),
+                           fold (build1 (CONJ_EXPR,
+                                         type, TREE_OPERAND (arg0, 1)))));
+      else if (TREE_CODE (arg0) == CONJ_EXPR)
+       return TREE_OPERAND (arg0, 0);
+      return t;
+
     case BIT_NOT_EXPR:
       if (wins)
        {
@@ -3341,6 +3609,7 @@ fold (expr)
                             ~ TREE_INT_CST_HIGH (arg0));
          TREE_TYPE (t) = type;
          force_fit_type (t, 0);
+         TREE_OVERFLOW (t) = TREE_OVERFLOW (arg0);
          TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (arg0);
        }
       else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
@@ -3351,7 +3620,7 @@ fold (expr)
       /* A + (-B) -> A - B */
       if (TREE_CODE (arg1) == NEGATE_EXPR)
        return fold (build (MINUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
-      else if (TREE_CODE (type) != REAL_TYPE)
+      else if (! FLOAT_TYPE_P (type))
        {
          if (integer_zerop (arg1))
            return non_lvalue (convert (type, arg0));
@@ -3371,17 +3640,35 @@ fold (expr)
              code = BIT_IOR_EXPR;
              goto bit_ior;
            }
+
+         /* (A * C) + (B * C) -> (A+B) * C.  Since we are most concerned
+            about the case where C is a constant, just try one of the
+            four possibilities.  */
+
+         if (TREE_CODE (arg0) == MULT_EXPR && TREE_CODE (arg1) == MULT_EXPR
+             && operand_equal_p (TREE_OPERAND (arg0, 1),
+                                 TREE_OPERAND (arg1, 1), 0))
+           return fold (build (MULT_EXPR, type,
+                               fold (build (PLUS_EXPR, type,
+                                            TREE_OPERAND (arg0, 0),
+                                            TREE_OPERAND (arg1, 0))),
+                               TREE_OPERAND (arg0, 1)));
        }
       /* In IEEE floating point, x+0 may not equal x.  */
-      else if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
+      else if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
+               || flag_fast_math)
               && real_zerop (arg1))
        return non_lvalue (convert (type, arg0));
     associate:
       /* In most languages, can't associate operations on floats
         through parentheses.  Rather than remember where the parentheses
-        were, we don't associate floats at all.  It shouldn't matter much.  */
-      if (TREE_CODE (type) == REAL_TYPE)
+        were, we don't associate floats at all.  It shouldn't matter much.
+        However, associating multiplications is only very slightly
+        inaccurate, so do that if -ffast-math is specified.  */
+      if (FLOAT_TYPE_P (type)
+         && ! (flag_fast_math && code == MULT_EXPR))
        goto binary;
+
       /* The varsign == -1 cases happen only for addition and subtraction.
         It says that the arg that was split was really CON minus VAR.
         The rest of the code applies to all associative operations.  */
@@ -3399,11 +3686,15 @@ fold (expr)
                  if (code == PLUS_EXPR && operand_equal_p (var, arg1, 0))
                    return convert (TREE_TYPE (t), con);
                    
+                 /* If ARG0 is a constant, don't change things around;
+                    instead keep all the constant computations together.  */
+
+                 if (TREE_CONSTANT (arg0))
+                   return t;
+
                  /* Otherwise return (CON +- ARG1) - VAR.  */
-                 TREE_SET_CODE (t, MINUS_EXPR);
-                 TREE_OPERAND (t, 1) = var;
-                 TREE_OPERAND (t, 0)
-                   = fold (build (code, TREE_TYPE (t), con, arg1));
+                 t = build (MINUS_EXPR, type,
+                            fold (build (code, type, con, arg1)), var);
                }
              else
                {
@@ -3412,10 +3703,16 @@ fold (expr)
                  if (code == MINUS_EXPR && operand_equal_p (var, arg1, 0))
                    return convert (TREE_TYPE (t), con);
                    
+                 /* If ARG0 is a constant, don't change things around;
+                    instead keep all the constant computations together.  */
+
+                 if (TREE_CONSTANT (arg0))
+                   return t;
+
                  /* Otherwise return VAR +- (ARG1 +- CON).  */
-                 TREE_OPERAND (t, 1) = tem
-                   = fold (build (code, TREE_TYPE (t), arg1, con));
-                 TREE_OPERAND (t, 0) = var;
+                 tem = fold (build (code, type, arg1, con));
+                 t = build (code, type, var, tem);
+
                  if (integer_zerop (tem)
                      && (code == PLUS_EXPR || code == MINUS_EXPR))
                    return convert (type, var);
@@ -3438,10 +3735,14 @@ fold (expr)
 
          if (split_tree (arg1, code, &var, &con, &varsign))
            {
-             /* EXPR is ARG0 +- (CON +- VAR).  */
+             if (TREE_CONSTANT (arg1))
+               return t;
+
              if (varsign == -1)
                TREE_SET_CODE (t,
                               (code == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR));
+
+             /* EXPR is ARG0 +- (CON +- VAR).  */
              if (TREE_CODE (t) == MINUS_EXPR
                  && operand_equal_p (var, arg0, 0))
                {
@@ -3451,9 +3752,10 @@ fold (expr)
                  return fold (build1 (NEGATE_EXPR, TREE_TYPE (t),
                                       convert (TREE_TYPE (t), con)));
                }
-             TREE_OPERAND (t, 0)
-               = fold (build (code, TREE_TYPE (t), arg0, con));
-             TREE_OPERAND (t, 1) = var;
+
+             t = build (TREE_CODE (t), type,
+                        fold (build (code, TREE_TYPE (t), arg0, con)), var);
+
              if (integer_zerop (TREE_OPERAND (t, 0))
                  && TREE_CODE (t) == PLUS_EXPR)
                return convert (TREE_TYPE (t), var);
@@ -3477,17 +3779,32 @@ fold (expr)
       return t;
 
     case MINUS_EXPR:
-      if (TREE_CODE (type) != REAL_TYPE)
+      if (! FLOAT_TYPE_P (type))
        {
          if (! wins && integer_zerop (arg0))
            return build1 (NEGATE_EXPR, type, arg1);
          if (integer_zerop (arg1))
            return non_lvalue (convert (type, arg0));
+
+         /* (A * C) - (B * C) -> (A-B) * C.  Since we are most concerned
+            about the case where C is a constant, just try one of the
+            four possibilities.  */
+
+         if (TREE_CODE (arg0) == MULT_EXPR && TREE_CODE (arg1) == MULT_EXPR
+             && operand_equal_p (TREE_OPERAND (arg0, 1),
+                                 TREE_OPERAND (arg1, 1), 0))
+           return fold (build (MULT_EXPR, type,
+                               fold (build (MINUS_EXPR, type,
+                                            TREE_OPERAND (arg0, 0),
+                                            TREE_OPERAND (arg1, 0))),
+                               TREE_OPERAND (arg0, 1)));
        }
       /* Convert A - (-B) to A + B.  */
       else if (TREE_CODE (arg1) == NEGATE_EXPR)
        return fold (build (PLUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
-      else if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT)
+
+      else if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
+              || flag_fast_math)
        {
          /* Except with IEEE floating point, 0-x equals -x.  */
          if (! wins && real_zerop (arg0))
@@ -3495,27 +3812,36 @@ fold (expr)
          /* Except with IEEE floating point, x-0 equals x.  */
          if (real_zerop (arg1))
            return non_lvalue (convert (type, arg0));
+       }
 
-         /* Fold &x - &x.  This can happen from &x.foo - &x. 
-            This is unsafe for certain floats even in non-IEEE formats.
-            In IEEE, it is unsafe because it does wrong for NaNs.
-            Also note that operand_equal_p is always false if an operand
-            is volatile.  */
+      /* Fold &x - &x.  This can happen from &x.foo - &x. 
+        This is unsafe for certain floats even in non-IEEE formats.
+        In IEEE, it is unsafe because it does wrong for NaNs.
+        Also note that operand_equal_p is always false if an operand
+        is volatile.  */
+
+      if ((! FLOAT_TYPE_P (type) || flag_fast_math)
+         && operand_equal_p (arg0, arg1, 0))
+       return convert (type, integer_zero_node);
 
-         if (operand_equal_p (arg0, arg1,
-                              TREE_CODE (type) == REAL_TYPE))
-           return convert (type, integer_zero_node);
-       }
       goto associate;
 
     case MULT_EXPR:
-      if (TREE_CODE (type) != REAL_TYPE)
+      if (! FLOAT_TYPE_P (type))
        {
          if (integer_zerop (arg1))
            return omit_one_operand (type, arg1, arg0);
          if (integer_onep (arg1))
            return non_lvalue (convert (type, arg0));
 
+         /* ((A / C) * C) is A if the division is an
+            EXACT_DIV_EXPR.   Since C is normally a constant,
+            just check for one of the four possibilities.  */
+
+         if (TREE_CODE (arg0) == EXACT_DIV_EXPR
+             && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
+           return TREE_OPERAND (arg0, 0);
+
          /* (a * (1 << b)) is (a << b)  */
          if (TREE_CODE (arg1) == LSHIFT_EXPR
              && integer_onep (TREE_OPERAND (arg1, 0)))
@@ -3529,7 +3855,8 @@ fold (expr)
       else
        {
          /* x*0 is 0, except for IEEE floating point.  */
-         if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
+         if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
+              || flag_fast_math)
              && real_zerop (arg1))
            return omit_one_operand (type, arg1, arg0);
          /* In IEEE floating point, x*1 is not equivalent to x for snans.
@@ -3629,66 +3956,134 @@ fold (expr)
        }
       goto binary;
 
+    case RDIV_EXPR:
+      /* In most cases, do nothing with a divide by zero.  */
+#if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
+#ifndef REAL_INFINITY
+      if (TREE_CODE (arg1) == REAL_CST && real_zerop (arg1))
+       return t;
+#endif
+#endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
+
+      /* In IEEE floating point, x/1 is not equivalent to x for snans.
+        However, ANSI says we can drop signals, so we can do this anyway.  */
+      if (real_onep (arg1))
+       return non_lvalue (convert (type, arg0));
+
+      /* If ARG1 is a constant, we can convert this to a multiply by the
+        reciprocal.  This does not have the same rounding properties,
+        so only do this if -ffast-math.  We can actually always safely
+        do it if ARG1 is a power of two, but it's hard to tell if it is
+        or not in a portable manner.  */
+      if (TREE_CODE (arg1) == REAL_CST && flag_fast_math
+         && 0 != (tem = const_binop (code, build_real (type, dconst1),
+                                     arg1, 0)))
+       return fold (build (MULT_EXPR, type, arg0, tem));
+
+      goto binary;
+
     case TRUNC_DIV_EXPR:
     case ROUND_DIV_EXPR:
     case FLOOR_DIV_EXPR:
     case CEIL_DIV_EXPR:
     case EXACT_DIV_EXPR:
-    case RDIV_EXPR:
       if (integer_onep (arg1))
        return non_lvalue (convert (type, arg0));
       if (integer_zerop (arg1))
        return t;
 
-      /* If we have ((a * C1) / C2) and C1 % C2 == 0, we can replace this with
-        (a * (C1/C2).  Also look for when we have a SAVE_EXPR in
-        between.  */
-      if (TREE_CODE (arg1) == INTEGER_CST
-         && TREE_INT_CST_LOW (arg1) > 0 && TREE_INT_CST_HIGH (arg1) == 0
-         && TREE_CODE (arg0) == MULT_EXPR
-         && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
-         && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) > 0
-         && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
-         && 0 == (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
-                  % TREE_INT_CST_LOW (arg1)))
+      /* If we have ((a / C1) / C2) where both division are the same type, try
+        to simplify.  First see if C1 * C2 overflows or not.  */
+      if (TREE_CODE (arg0) == code && TREE_CODE (arg1) == INTEGER_CST
+         && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
        {
-         tree new_op
-           = build_int_2 (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
-                          / TREE_INT_CST_LOW (arg1), 0);
+         tree new_divisor;
+
+         new_divisor = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 1), arg1, 0);
+         tem = const_binop (FLOOR_DIV_EXPR, new_divisor, arg1, 0);
 
-         TREE_TYPE (new_op) = type;
-         return build (MULT_EXPR, type, TREE_OPERAND (arg0, 0), new_op);
+         if (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) == TREE_INT_CST_LOW (tem)
+             && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == TREE_INT_CST_HIGH (tem))
+           {
+             /* If no overflow, divide by C1*C2.  */
+             return fold (build (code, type, TREE_OPERAND (arg0, 0), new_divisor));
+           }
        }
 
-      else if (TREE_CODE (arg1) == INTEGER_CST
-              && TREE_INT_CST_LOW (arg1) > 0 && TREE_INT_CST_HIGH (arg1) == 0
-              && TREE_CODE (arg0) == SAVE_EXPR
-              && TREE_CODE (TREE_OPERAND (arg0, 0)) == MULT_EXPR
-              && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
-                  == INTEGER_CST)
-              && (TREE_INT_CST_LOW (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
-                  > 0)
-              && (TREE_INT_CST_HIGH (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
-                  == 0)
-              && (TREE_INT_CST_LOW (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
-                  % TREE_INT_CST_LOW (arg1)) == 0)
+      /* Look for ((a * C1) / C3) or (((a * C1) + C2) / C3),
+        where C1 % C3 == 0 or C3 % C1 == 0.  We can simplify these
+        expressions, which often appear in the offsets or sizes of
+        objects with a varying size.  Only deal with positive divisors
+        and multiplicands.   If C2 is negative, we must have C2 % C3 == 0.
+
+        Look for NOPs and SAVE_EXPRs inside.  */
+
+      if (TREE_CODE (arg1) == INTEGER_CST
+         && tree_int_cst_sgn (arg1) >= 0)
        {
-         tree new_op
-           = build_int_2 (TREE_INT_CST_LOW (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
-                          / TREE_INT_CST_LOW (arg1), 0);
-         
-         TREE_TYPE (new_op) = type;
-         return build (MULT_EXPR, type,
-                       TREE_OPERAND (TREE_OPERAND (arg0, 0), 0), new_op);
-       }
+         int have_save_expr = 0;
+         tree c2 = integer_zero_node;
+         tree xarg0 = arg0;
+
+         if (TREE_CODE (xarg0) == SAVE_EXPR)
+           have_save_expr = 1, xarg0 = TREE_OPERAND (xarg0, 0);
+
+         STRIP_NOPS (xarg0);
+
+         if (TREE_CODE (xarg0) == PLUS_EXPR
+             && TREE_CODE (TREE_OPERAND (xarg0, 1)) == INTEGER_CST)
+           c2 = TREE_OPERAND (xarg0, 1), xarg0 = TREE_OPERAND (xarg0, 0);
+         else if (TREE_CODE (xarg0) == MINUS_EXPR
+                  && TREE_CODE (TREE_OPERAND (xarg0, 1)) == INTEGER_CST
+                  /* If we are doing this computation unsigned, the negate
+                     is incorrect.  */
+                  && ! TREE_UNSIGNED (type))
+           {
+             c2 = fold (build1 (NEGATE_EXPR, type, TREE_OPERAND (xarg0, 1)));
+             xarg0 = TREE_OPERAND (xarg0, 0);
+           }
 
-#if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
-#ifndef REAL_INFINITY
-      if (TREE_CODE (arg1) == REAL_CST
-         && real_zerop (arg1))
-       return t;
-#endif
-#endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
+         if (TREE_CODE (xarg0) == SAVE_EXPR)
+           have_save_expr = 1, xarg0 = TREE_OPERAND (xarg0, 0);
+
+         STRIP_NOPS (xarg0);
+
+         if (TREE_CODE (xarg0) == MULT_EXPR
+             && TREE_CODE (TREE_OPERAND (xarg0, 1)) == INTEGER_CST
+             && tree_int_cst_sgn (TREE_OPERAND (xarg0, 1)) >= 0
+             && (integer_zerop (const_binop (TRUNC_MOD_EXPR,
+                                             TREE_OPERAND (xarg0, 1), arg1, 1))
+                 || integer_zerop (const_binop (TRUNC_MOD_EXPR, arg1,
+                                                TREE_OPERAND (xarg0, 1), 1)))
+             && (tree_int_cst_sgn (c2) >= 0
+                 || integer_zerop (const_binop (TRUNC_MOD_EXPR, c2,
+                                                arg1, 1))))
+           {
+             tree outer_div = integer_one_node;
+             tree c1 = TREE_OPERAND (xarg0, 1);
+             tree c3 = arg1;
+
+             /* If C3 > C1, set them equal and do a divide by
+                C3/C1 at the end of the operation.  */
+             if (tree_int_cst_lt (c1, c3))
+               outer_div = const_binop (code, c3, c1, 0), c3 = c1;
+               
+             /* The result is A * (C1/C3) + (C2/C3).  */
+             t = fold (build (PLUS_EXPR, type,
+                              fold (build (MULT_EXPR, type,
+                                           TREE_OPERAND (xarg0, 0),
+                                           const_binop (code, c1, c3, 1))),
+                              const_binop (code, c2, c3, 1)));
+
+             if (! integer_onep (outer_div))
+               t = fold (build (code, type, t, convert (type, outer_div)));
+
+             if (have_save_expr)
+               t = save_expr (t);
+
+             return t;
+           }
+       }
 
       goto binary;
 
@@ -3700,6 +4095,41 @@ fold (expr)
        return omit_one_operand (type, integer_zero_node, arg0);
       if (integer_zerop (arg1))
        return t;
+
+      /* Look for ((a * C1) % C3) or (((a * C1) + C2) % C3),
+        where C1 % C3 == 0.  Handle similarly to the division case,
+        but don't bother with SAVE_EXPRs.  */
+
+      if (TREE_CODE (arg1) == INTEGER_CST
+         && ! integer_zerop (arg1))
+       {
+         tree c2 = integer_zero_node;
+         tree xarg0 = arg0;
+
+         if (TREE_CODE (xarg0) == PLUS_EXPR
+             && TREE_CODE (TREE_OPERAND (xarg0, 1)) == INTEGER_CST)
+           c2 = TREE_OPERAND (xarg0, 1), xarg0 = TREE_OPERAND (xarg0, 0);
+         else if (TREE_CODE (xarg0) == MINUS_EXPR
+                  && TREE_CODE (TREE_OPERAND (xarg0, 1)) == INTEGER_CST
+                  && ! TREE_UNSIGNED (type))
+           {
+             c2 = fold (build1 (NEGATE_EXPR, type, TREE_OPERAND (xarg0, 1)));
+             xarg0 = TREE_OPERAND (xarg0, 0);
+           }
+
+         STRIP_NOPS (xarg0);
+
+         if (TREE_CODE (xarg0) == MULT_EXPR
+             && TREE_CODE (TREE_OPERAND (xarg0, 1)) == INTEGER_CST
+             && integer_zerop (const_binop (TRUNC_MOD_EXPR,
+                                            TREE_OPERAND (xarg0, 1),
+                                            arg1, 1))
+             && tree_int_cst_sgn (c2) >= 0)
+           /* The result is (C2%C3).  */
+           return omit_one_operand (type, const_binop (code, c2, arg1, 1),
+                                    TREE_OPERAND (xarg0, 0));
+       }
+
       goto binary;
 
     case LSHIFT_EXPR:
@@ -3710,14 +4140,57 @@ fold (expr)
        return non_lvalue (convert (type, arg0));
       /* Since negative shift count is not well-defined,
         don't try to compute it in the compiler.  */
-      if (tree_int_cst_lt (arg1, integer_zero_node))
+      if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
        return t;
+      /* Rewrite an LROTATE_EXPR by a constant into an
+        RROTATE_EXPR by a new constant.  */
+      if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
+       {
+         TREE_SET_CODE (t, RROTATE_EXPR);
+         code = RROTATE_EXPR;
+         TREE_OPERAND (t, 1) = arg1
+           = const_binop
+             (MINUS_EXPR,
+              convert (TREE_TYPE (arg1),
+                       build_int_2 (GET_MODE_BITSIZE (TYPE_MODE (type)), 0)),
+              arg1, 0);
+         if (tree_int_cst_sgn (arg1) < 0)
+           return t;
+       }
+
+      /* If we have a rotate of a bit operation with the rotate count and
+        the second operand of the bit operation both constant,
+        permute the two operations.  */
+      if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
+         && (TREE_CODE (arg0) == BIT_AND_EXPR
+             || TREE_CODE (arg0) == BIT_ANDTC_EXPR
+             || TREE_CODE (arg0) == BIT_IOR_EXPR
+             || TREE_CODE (arg0) == BIT_XOR_EXPR)
+         && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
+       return fold (build (TREE_CODE (arg0), type,
+                           fold (build (code, type,
+                                        TREE_OPERAND (arg0, 0), arg1)),
+                           fold (build (code, type,
+                                        TREE_OPERAND (arg0, 1), arg1))));
+
+      /* Two consecutive rotates adding up to the width of the mode can
+        be ignored.  */
+      if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
+         && TREE_CODE (arg0) == RROTATE_EXPR
+         && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
+         && TREE_INT_CST_HIGH (arg1) == 0
+         && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
+         && ((TREE_INT_CST_LOW (arg1)
+              + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
+             == GET_MODE_BITSIZE (TYPE_MODE (type))))
+       return TREE_OPERAND (arg0, 0);
+
       goto binary;
 
     case MIN_EXPR:
       if (operand_equal_p (arg0, arg1, 0))
        return arg0;
-      if (TREE_CODE (type) == INTEGER_TYPE
+      if (INTEGRAL_TYPE_P (type)
          && operand_equal_p (arg1, TYPE_MIN_VALUE (type), 1))
        return omit_one_operand (type, arg1, arg0);
       goto associate;
@@ -3725,7 +4198,7 @@ fold (expr)
     case MAX_EXPR:
       if (operand_equal_p (arg0, arg1, 0))
        return arg0;
-      if (TREE_CODE (type) == INTEGER_TYPE
+      if (INTEGRAL_TYPE_P (type)
          && operand_equal_p (arg1, TYPE_MAX_VALUE (type), 1))
        return omit_one_operand (type, arg1, arg0);
       goto associate;
@@ -3735,7 +4208,11 @@ fold (expr)
         and its values must be 0 or 1.
         ("true" is a fixed value perhaps depending on the language,
         but we don't handle values other than 1 correctly yet.)  */
-      return invert_truthvalue (arg0);
+      tem = invert_truthvalue (arg0);
+      /* Avoid infinite recursion.  */
+      if (TREE_CODE (tem) == TRUTH_NOT_EXPR)
+       return t;
+      return convert (type, tem);
 
     case TRUTH_ANDIF_EXPR:
       /* Note that the operands of this must be ints
@@ -3756,23 +4233,61 @@ fold (expr)
        return omit_one_operand (type, arg1, arg0);
 
     truth_andor:
+      /* We only do these simplifications if we are optimizing.  */
+      if (!optimize)
+       return t;
+
+      /* Check for things like (A || B) && (A || C).  We can convert this
+        to A || (B && C).  Note that either operator can be any of the four
+        truth and/or operations and the transformation will still be
+        valid.   Also note that we only care about order for the
+        ANDIF and ORIF operators.  */
+      if (TREE_CODE (arg0) == TREE_CODE (arg1)
+         && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
+             || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
+             || TREE_CODE (arg0) == TRUTH_AND_EXPR
+             || TREE_CODE (arg0) == TRUTH_OR_EXPR))
+       {
+         tree a00 = TREE_OPERAND (arg0, 0);
+         tree a01 = TREE_OPERAND (arg0, 1);
+         tree a10 = TREE_OPERAND (arg1, 0);
+         tree a11 = TREE_OPERAND (arg1, 1);
+         int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
+                             || TREE_CODE (arg0) == TRUTH_AND_EXPR)
+                            && (code == TRUTH_AND_EXPR
+                                || code == TRUTH_OR_EXPR));
+
+         if (operand_equal_p (a00, a10, 0))
+           return fold (build (TREE_CODE (arg0), type, a00,
+                               fold (build (code, type, a01, a11))));
+         else if (commutative && operand_equal_p (a00, a11, 0))
+           return fold (build (TREE_CODE (arg0), type, a00,
+                               fold (build (code, type, a01, a10))));
+         else if (commutative && operand_equal_p (a01, a10, 0))
+           return fold (build (TREE_CODE (arg0), type, a01,
+                               fold (build (code, type, a00, a11))));
+
+         /* This case if tricky because we must either have commutative
+            operators or else A10 must not have side-effects.  */
+
+         else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
+                  && operand_equal_p (a01, a11, 0))
+           return fold (build (TREE_CODE (arg0), type,
+                               fold (build (code, type, a00, a10)),
+                               a01));
+       }
+
       /* Check for the possibility of merging component references.  If our
         lhs is another similar operation, try to merge its rhs with our
         rhs.  Then try to merge our lhs and rhs.  */
-      if (optimize)
-       {
-         if (TREE_CODE (arg0) == code)
-           {
-             tem = fold_truthop (code, type,
-                                 TREE_OPERAND (arg0, 1), arg1);
-             if (tem)
-               return fold (build (code, type, TREE_OPERAND (arg0, 0), tem));
-           }
+      if (TREE_CODE (arg0) == code
+         && 0 != (tem = fold_truthop (code, type,
+                                      TREE_OPERAND (arg0, 1), arg1)))
+       return fold (build (code, type, TREE_OPERAND (arg0, 0), tem));
+
+      if ((tem = fold_truthop (code, type, arg0, arg1)) != 0)
+       return tem;
 
-         tem = fold_truthop (code, type, arg0, arg1);
-         if (tem)
-           return tem;
-       }
       return t;
 
     case TRUTH_ORIF_EXPR:
@@ -3805,7 +4320,7 @@ fold (expr)
        return non_lvalue (invert_truthvalue (arg1));
       if (integer_onep (arg1))
        return non_lvalue (invert_truthvalue (arg0));
-      break;
+      return t;
 
     case EQ_EXPR:
     case NE_EXPR:
@@ -3830,12 +4345,12 @@ fold (expr)
         and the other one.  */
       {
        tree constop = 0, varop;
-       tree *constoploc;
+       int constopnum = -1;
 
        if (TREE_CONSTANT (arg1))
-         constoploc = &TREE_OPERAND (t, 1), constop = arg1, varop = arg0;
+         constopnum = 1, constop = arg1, varop = arg0;
        if (TREE_CONSTANT (arg0))
-         constoploc = &TREE_OPERAND (t, 0), constop = arg0, varop = arg1;
+         constopnum = 0, constop = arg0, varop = arg1;
 
        if (constop && TREE_CODE (varop) == POSTINCREMENT_EXPR)
          {
@@ -3844,28 +4359,33 @@ fold (expr)
               This optimization is invalid for floating point due to rounding.
               For pointer types we assume overflow doesn't happen.  */
            if (TREE_CODE (TREE_TYPE (varop)) == POINTER_TYPE
-               || (TREE_CODE (TREE_TYPE (varop)) != REAL_TYPE
+               || (! FLOAT_TYPE_P (TREE_TYPE (varop))
                    && (code == EQ_EXPR || code == NE_EXPR)))
              {
                tree newconst
                  = fold (build (PLUS_EXPR, TREE_TYPE (varop),
                                 constop, TREE_OPERAND (varop, 1)));
                TREE_SET_CODE (varop, PREINCREMENT_EXPR);
-               *constoploc = newconst;
+
+               t = build (code, type, TREE_OPERAND (t, 0),
+                          TREE_OPERAND (t, 1));
+               TREE_OPERAND (t, constopnum) = newconst;
                return t;
              }
          }
        else if (constop && TREE_CODE (varop) == POSTDECREMENT_EXPR)
          {
            if (TREE_CODE (TREE_TYPE (varop)) == POINTER_TYPE
-               || (TREE_CODE (TREE_TYPE (varop)) != REAL_TYPE
+               || (! FLOAT_TYPE_P (TREE_TYPE (varop))
                    && (code == EQ_EXPR || code == NE_EXPR)))
              {
                tree newconst
                  = fold (build (MINUS_EXPR, TREE_TYPE (varop),
                                 constop, TREE_OPERAND (varop, 1)));
                TREE_SET_CODE (varop, PREDECREMENT_EXPR);
-               *constoploc = newconst;
+               t = build (code, type, TREE_OPERAND (t, 0),
+                          TREE_OPERAND (t, 1));
+               TREE_OPERAND (t, constopnum) = newconst;
                return t;
              }
          }
@@ -3874,22 +4394,21 @@ fold (expr)
       /* Change X >= CST to X > (CST - 1) if CST is positive.  */
       if (TREE_CODE (arg1) == INTEGER_CST
          && TREE_CODE (arg0) != INTEGER_CST
-         && ! tree_int_cst_lt (arg1, integer_one_node))
+         && tree_int_cst_sgn (arg1) > 0)
        {
          switch (TREE_CODE (t))
            {
            case GE_EXPR:
              code = GT_EXPR;
-             TREE_SET_CODE (t, code);
              arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
-             TREE_OPERAND (t, 1) = arg1;
+             t = build (code, type, TREE_OPERAND (t, 0), arg1);
              break;
 
            case LT_EXPR:
              code = LE_EXPR;
-             TREE_SET_CODE (t, code);
              arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
-             TREE_OPERAND (t, 1) = arg1;
+             t = build (code, type, TREE_OPERAND (t, 0), arg1);
+             break;
            }
        }
 
@@ -3927,6 +4446,26 @@ fold (expr)
                           arg1));
        }
 
+      /* If this is an NE or EQ comparison of zero against the result of a
+        signed MOD operation whose second operand is a power of 2, make
+        the MOD operation unsigned since it is simpler and equivalent.  */
+      if ((code == NE_EXPR || code == EQ_EXPR)
+         && integer_zerop (arg1)
+         && ! TREE_UNSIGNED (TREE_TYPE (arg0))
+         && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
+             || TREE_CODE (arg0) == CEIL_MOD_EXPR
+             || TREE_CODE (arg0) == FLOOR_MOD_EXPR
+             || TREE_CODE (arg0) == ROUND_MOD_EXPR)
+         && integer_pow2p (TREE_OPERAND (arg0, 1)))
+       {
+         tree newtype = unsigned_type (TREE_TYPE (arg0));
+         tree newmod = build (TREE_CODE (arg0), newtype,
+                              convert (newtype, TREE_OPERAND (arg0, 0)),
+                              convert (newtype, TREE_OPERAND (arg0, 1)));
+
+         return build (code, type, newmod, convert (newtype, arg1));
+       }
+
       /* If this is an NE comparison of zero with an AND of one, remove the
         comparison since the AND will give the correct value.  */
       if (code == NE_EXPR && integer_zerop (arg1)
@@ -3943,6 +4482,30 @@ fold (expr)
        return build (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
                      arg0, integer_zero_node);
 
+      /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
+        and similarly for >= into !=.  */
+      if ((code == LT_EXPR || code == GE_EXPR)
+         && TREE_UNSIGNED (TREE_TYPE (arg0))
+         && TREE_CODE (arg1) == LSHIFT_EXPR
+         && integer_onep (TREE_OPERAND (arg1, 0)))
+       return build (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type, 
+                     build (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
+                            TREE_OPERAND (arg1, 1)),
+                     convert (TREE_TYPE (arg0), integer_zero_node));
+
+      else if ((code == LT_EXPR || code == GE_EXPR)
+              && TREE_UNSIGNED (TREE_TYPE (arg0))
+              && (TREE_CODE (arg1) == NOP_EXPR
+                  || TREE_CODE (arg1) == CONVERT_EXPR)
+              && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
+              && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
+       return
+         build (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
+                convert (TREE_TYPE (arg0),
+                         build (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
+                                TREE_OPERAND (TREE_OPERAND (arg1, 0), 1))),
+                convert (TREE_TYPE (arg0), integer_zero_node));
+
       /* Simplify comparison of something with itself.  (For IEEE
         floating-point, we can only do some of these simplifications.)  */
       if (operand_equal_p (arg0, arg1, 0))
@@ -3952,7 +4515,7 @@ fold (expr)
            case EQ_EXPR:
            case GE_EXPR:
            case LE_EXPR:
-             if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE)
+             if (INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
                {
                  t = build_int_2 (1, 0);
                  TREE_TYPE (t) = type;
@@ -3964,7 +4527,7 @@ fold (expr)
 
            case NE_EXPR:
              /* For NE, we can only do this simplification if integer.  */
-             if (TREE_CODE (TREE_TYPE (arg0)) != INTEGER_TYPE)
+             if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
                break;
              /* ... fall through ... */
            case GT_EXPR:
@@ -3977,7 +4540,7 @@ fold (expr)
 
       /* An unsigned comparison against 0 can be simplified.  */
       if (integer_zerop (arg1)
-         && (TREE_CODE (TREE_TYPE (arg1)) == INTEGER_TYPE
+         && (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
              || TREE_CODE (TREE_TYPE (arg1)) == POINTER_TYPE)
          && TREE_UNSIGNED (TREE_TYPE (arg1)))
        {
@@ -3992,11 +4555,13 @@ fold (expr)
              TREE_SET_CODE (t, EQ_EXPR);
              break;
            case GE_EXPR:
-             return omit_one_operand (integer_type_node,
-                                      integer_one_node, arg0);
+             return omit_one_operand (type,
+                                      convert (type, integer_one_node),
+                                      arg0);
            case LT_EXPR:
-             return omit_one_operand (integer_type_node,
-                                      integer_zero_node, arg0);
+             return omit_one_operand (type,
+                                      convert (type, integer_zero_node),
+                                      arg0);
            }
        }
 
@@ -4016,14 +4581,15 @@ fold (expr)
       if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
        {
          tree cval1 = 0, cval2 = 0;
+         int save_p = 0;
 
-         if (twoval_comparison_p (arg0, &cval1, &cval2)
+         if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
              /* Don't handle degenerate cases here; they should already
                 have been handled anyway.  */
              && cval1 != 0 && cval2 != 0
              && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
              && TREE_TYPE (cval1) == TREE_TYPE (cval2)
-             && TREE_CODE (TREE_TYPE (cval1)) == INTEGER_TYPE
+             && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
              && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
                                    TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
            {
@@ -4089,7 +4655,11 @@ fold (expr)
                      return omit_one_operand (type, integer_one_node, arg0);
                    }
 
-                 return fold (build (code, type, cval1, cval2));
+                 t = build (code, type, cval1, cval2);
+                 if (save_p)
+                   return save_expr (t);
+                 else
+                   return fold (t);
                }
            }
        }
@@ -4106,6 +4676,28 @@ fold (expr)
          return t1 ? t1 : t;
        }
 
+      /* If this is a comparison of complex values and either or both
+        sizes are a COMPLEX_EXPR, it is best to split up the comparisons
+        and join them with a TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR.  This
+        may prevent needless evaluations.  */
+      if ((code == EQ_EXPR || code == NE_EXPR)
+         && TREE_CODE (TREE_TYPE (arg0)) == COMPLEX_TYPE
+         && (TREE_CODE (arg0) == COMPLEX_EXPR
+             || TREE_CODE (arg1) == COMPLEX_EXPR))
+       {
+         tree subtype = TREE_TYPE (TREE_TYPE (arg0));
+         tree real0 = fold (build1 (REALPART_EXPR, subtype, arg0));
+         tree imag0 = fold (build1 (IMAGPART_EXPR, subtype, arg0));
+         tree real1 = fold (build1 (REALPART_EXPR, subtype, arg1));
+         tree imag1 = fold (build1 (IMAGPART_EXPR, subtype, arg1));
+
+         return fold (build ((code == EQ_EXPR ? TRUTH_ANDIF_EXPR
+                              : TRUTH_ORIF_EXPR),
+                             type,
+                             fold (build (code, type, real0, real1)),
+                             fold (build (code, type, imag0, imag1))));
+       }
+
       /* From here on, the only cases we handle are when the result is
         known to be a constant.
 
@@ -4194,10 +4786,13 @@ fold (expr)
       return t1;
 
     case COND_EXPR:
+      /* Pedantic ANSI C says that a conditional expression is never an lvalue,
+        so all simple results must be passed through pedantic_non_lvalue.  */
       if (TREE_CODE (arg0) == INTEGER_CST)
-       return TREE_OPERAND (t, (integer_zerop (arg0) ? 2 : 1));
+       return pedantic_non_lvalue
+         (TREE_OPERAND (t, (integer_zerop (arg0) ? 2 : 1)));
       else if (operand_equal_p (arg1, TREE_OPERAND (expr, 2), 0))
-       return omit_one_operand (type, arg1, arg0);
+       return pedantic_omit_one_operand (type, arg1, arg0);
 
       /* If the second operand is zero, invert the comparison and swap
         the second and third operands.  Likewise if the second operand
@@ -4218,10 +4813,11 @@ fold (expr)
 
          if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
            {
-             arg0 = TREE_OPERAND (t, 0) = tem;
-             TREE_OPERAND (t, 1) = TREE_OPERAND (t, 2);
-             TREE_OPERAND (t, 2) = arg1;
-             arg1 = TREE_OPERAND (t, 1);
+             t = build (code, type, tem,
+                        TREE_OPERAND (t, 2), TREE_OPERAND (t, 1));
+             arg0 = tem;
+             arg1 = TREE_OPERAND (t, 2);
+             STRIP_NOPS (arg1);
            }
        }
 
@@ -4232,31 +4828,43 @@ fold (expr)
 
       if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
          && (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
-             || TREE_CODE (TREE_TYPE (TREE_OPERAND (arg0, 0))) != REAL_TYPE)
+             || ! FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 0)))
+             || flag_fast_math)
          && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
                                             arg1, TREE_OPERAND (arg0, 1)))
        {
          tree arg2 = TREE_OPERAND (t, 2);
          enum tree_code comp_code = TREE_CODE (arg0);
 
+         STRIP_NOPS (arg2);
+
          /* If we have A op 0 ? A : -A, this is A, -A, abs (A), or abs (-A),
             depending on the comparison operation.  */
-         if (integer_zerop (TREE_OPERAND (arg0, 1))
+         if ((FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 1)))
+              ? real_zerop (TREE_OPERAND (arg0, 1))
+              : integer_zerop (TREE_OPERAND (arg0, 1)))
              && TREE_CODE (arg2) == NEGATE_EXPR
              && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
            switch (comp_code)
              {
              case EQ_EXPR:
-               return fold (build1 (NEGATE_EXPR, type, arg1));
+               return pedantic_non_lvalue
+                 (fold (build1 (NEGATE_EXPR, type, arg1)));
              case NE_EXPR:
-               return convert (type, arg1);
+               return pedantic_non_lvalue (convert (type, arg1));
              case GE_EXPR:
              case GT_EXPR:
-               return fold (build1 (ABS_EXPR, type, arg1));
+               return pedantic_non_lvalue
+                 (convert (type, fold (build1 (ABS_EXPR,
+                                               TREE_TYPE (arg1), arg1))));
              case LE_EXPR:
              case LT_EXPR:
-               return fold (build1 (NEGATE_EXPR, type,
-                                    fold (build1 (ABS_EXPR, type, arg1))));
+               return pedantic_non_lvalue
+                 (fold (build1 (NEGATE_EXPR, type,
+                                convert (type,
+                                         fold (build1 (ABS_EXPR,
+                                                       TREE_TYPE (arg1),
+                                                       arg1))))));
              }
 
          /* If this is A != 0 ? A : 0, this is simply A.  For ==, it is
@@ -4265,9 +4873,9 @@ fold (expr)
          if (integer_zerop (TREE_OPERAND (arg0, 1)) && integer_zerop (arg2))
            {
              if (comp_code == NE_EXPR)
-               return convert (type, arg1);
+               return pedantic_non_lvalue (convert (type, arg1));
              else if (comp_code == EQ_EXPR)
-               return convert (type, integer_zero_node);
+               return pedantic_non_lvalue (convert (type, integer_zero_node));
            }
 
          /* If this is A op B ? A : B, this is either A, B, min (A, B),
@@ -4275,35 +4883,46 @@ fold (expr)
 
          if (operand_equal_for_comparison_p (TREE_OPERAND (arg0, 1),
                                              arg2, TREE_OPERAND (arg0, 0)))
-           switch (comp_code)
-             {
-             case EQ_EXPR:
-               return convert (type, arg2);
-             case NE_EXPR:
-               return convert (type, arg1);
-             case LE_EXPR:
-             case LT_EXPR:
-               return fold (build (MIN_EXPR, type, arg1, arg2));
-             case GE_EXPR:
-             case GT_EXPR:
-               return fold (build (MAX_EXPR, type, arg1, arg2));
-             }
+           {
+             tree comp_op0 = TREE_OPERAND (arg0, 0);
+             tree comp_op1 = TREE_OPERAND (arg0, 1);
+             tree comp_type = TREE_TYPE (comp_op0);
+
+             switch (comp_code)
+               {
+               case EQ_EXPR:
+                 return pedantic_non_lvalue (convert (type, arg2));
+               case NE_EXPR:
+                 return pedantic_non_lvalue (convert (type, arg1));
+               case LE_EXPR:
+               case LT_EXPR:
+                 return pedantic_non_lvalue
+                   (convert (type, (fold (build (MIN_EXPR, comp_type,
+                                                 comp_op0, comp_op1)))));
+               case GE_EXPR:
+               case GT_EXPR:
+                 return pedantic_non_lvalue
+                   (convert (type, fold (build (MAX_EXPR, comp_type,
+                                                comp_op0, comp_op1))));
+               }
+           }
 
          /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
             we might still be able to simplify this.  For example,
             if C1 is one less or one more than C2, this might have started
             out as a MIN or MAX and been transformed by this function.
-            Only good for INTEGER_TYPE, because we need TYPE_MAX_VALUE.  */
+            Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE.  */
 
-         if (TREE_CODE (type) == INTEGER_TYPE
+         if (INTEGRAL_TYPE_P (type)
              && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
              && TREE_CODE (arg2) == INTEGER_CST)
            switch (comp_code)
              {
              case EQ_EXPR:
                /* We can replace A with C1 in this case.  */
-               arg1 = TREE_OPERAND (t, 1)
-                 = convert (type, TREE_OPERAND (arg0, 1));
+               arg1 = convert (type, TREE_OPERAND (arg0, 1));
+               t = build (code, type, TREE_OPERAND (t, 0), arg1,
+                          TREE_OPERAND (t, 2));
                break;
 
              case LT_EXPR:
@@ -4312,7 +4931,8 @@ fold (expr)
                    && operand_equal_p (TREE_OPERAND (arg0, 1),
                                        const_binop (PLUS_EXPR, arg2,
                                                     integer_one_node, 0), 1))
-                 return fold (build (MIN_EXPR, type, arg1, arg2));
+                 return pedantic_non_lvalue
+                   (fold (build (MIN_EXPR, type, arg1, arg2)));
                break;
 
              case LE_EXPR:
@@ -4321,7 +4941,8 @@ fold (expr)
                    && operand_equal_p (TREE_OPERAND (arg0, 1),
                                        const_binop (MINUS_EXPR, arg2,
                                                     integer_one_node, 0), 1))
-                 return fold (build (MIN_EXPR, type, arg1, arg2));
+                 return pedantic_non_lvalue
+                   (fold (build (MIN_EXPR, type, arg1, arg2)));
                break;
 
              case GT_EXPR:
@@ -4330,7 +4951,8 @@ fold (expr)
                    && operand_equal_p (TREE_OPERAND (arg0, 1),
                                        const_binop (MINUS_EXPR, arg2,
                                                     integer_one_node, 0), 1))
-                 return fold (build (MAX_EXPR, type, arg1, arg2));
+                 return pedantic_non_lvalue
+                   (fold (build (MAX_EXPR, type, arg1, arg2)));
                break;
 
              case GE_EXPR:
@@ -4339,11 +4961,35 @@ fold (expr)
                    && operand_equal_p (TREE_OPERAND (arg0, 1),
                                        const_binop (PLUS_EXPR, arg2,
                                                     integer_one_node, 0), 1))
-                 return fold (build (MAX_EXPR, type, arg1, arg2));
+                 return pedantic_non_lvalue
+                   (fold (build (MAX_EXPR, type, arg1, arg2)));
                break;
              }
        }
 
+      /* If the second operand is simpler than the third, swap them
+        since that produces better jump optimization results.  */
+      if ((TREE_CONSTANT (arg1) || TREE_CODE_CLASS (TREE_CODE (arg1)) == 'd'
+          || TREE_CODE (arg1) == SAVE_EXPR)
+         && ! (TREE_CONSTANT (TREE_OPERAND (t, 2))
+               || TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (t, 2))) == 'd'
+               || TREE_CODE (TREE_OPERAND (t, 2)) == SAVE_EXPR))
+       {
+         /* See if this can be inverted.  If it can't, possibly because
+            it was a floating-point inequality comparison, don't do
+            anything.  */
+         tem = invert_truthvalue (arg0);
+
+         if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
+           {
+             t = build (code, type, tem,
+                        TREE_OPERAND (t, 2), TREE_OPERAND (t, 1));
+             arg0 = tem;
+             arg1 = TREE_OPERAND (t, 2);
+             STRIP_NOPS (arg1);
+           }
+       }
+
       /* Convert A ? 1 : 0 to simply A.  */
       if (integer_onep (TREE_OPERAND (t, 1))
          && integer_zerop (TREE_OPERAND (t, 2))
@@ -4352,8 +4998,7 @@ fold (expr)
             a COND, which will recurse.  In that case, the COND_EXPR
             is probably the best choice, so leave it alone.  */
          && type == TREE_TYPE (arg0))
-       return arg0;
-
+       return pedantic_non_lvalue (arg0);
 
       /* Look for expressions of the form A & 2 ? 2 : 0.  The result of this
         operation is simply A & 2.  */
@@ -4365,18 +5010,96 @@ fold (expr)
          && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
          && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
                              arg1, 1))
-       return convert (type, TREE_OPERAND (arg0, 0));
+       return pedantic_non_lvalue (convert (type, TREE_OPERAND (arg0, 0)));
 
       return t;
 
     case COMPOUND_EXPR:
-      if (TREE_SIDE_EFFECTS (arg0))
+      /* When pedantic, a compound expression can be neither an lvalue
+        nor an integer constant expression.  */
+      if (TREE_SIDE_EFFECTS (arg0) || pedantic)
        return t;
       /* Don't let (0, 0) be null pointer constant.  */
       if (integer_zerop (arg1))
        return non_lvalue (arg1);
       return arg1;
 
+    case COMPLEX_EXPR:
+      if (wins)
+       return build_complex (arg0, arg1);
+      return t;
+
+    case REALPART_EXPR:
+      if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
+       return t;
+      else if (TREE_CODE (arg0) == COMPLEX_EXPR)
+       return omit_one_operand (type, TREE_OPERAND (arg0, 0),
+                                TREE_OPERAND (arg0, 1));
+      else if (TREE_CODE (arg0) == COMPLEX_CST)
+       return TREE_REALPART (arg0);
+      else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
+       return fold (build (TREE_CODE (arg0), type,
+                           fold (build1 (REALPART_EXPR, type,
+                                         TREE_OPERAND (arg0, 0))),
+                           fold (build1 (REALPART_EXPR,
+                                         type, TREE_OPERAND (arg0, 1)))));
+      return t;
+
+    case IMAGPART_EXPR:
+      if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
+       return convert (type, integer_zero_node);
+      else if (TREE_CODE (arg0) == COMPLEX_EXPR)
+       return omit_one_operand (type, TREE_OPERAND (arg0, 1),
+                                TREE_OPERAND (arg0, 0));
+      else if (TREE_CODE (arg0) == COMPLEX_CST)
+       return TREE_IMAGPART (arg0);
+      else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
+       return fold (build (TREE_CODE (arg0), type,
+                           fold (build1 (IMAGPART_EXPR, type,
+                                         TREE_OPERAND (arg0, 0))),
+                           fold (build1 (IMAGPART_EXPR, type,
+                                         TREE_OPERAND (arg0, 1)))));
+      return t;
+
+      /* Pull arithmetic ops out of the CLEANUP_POINT_EXPR where
+         appropriate.  */
+    case CLEANUP_POINT_EXPR:
+      if (! TREE_SIDE_EFFECTS (arg0))
+       return convert (type, arg0);
+
+      {
+       enum tree_code code0 = TREE_CODE (arg0);
+       int kind0 = TREE_CODE_CLASS (code0);
+       tree arg00 = TREE_OPERAND (arg0, 0);
+       tree arg01;
+
+       if (kind0 == '1' || code0 == TRUTH_NOT_EXPR)
+         return fold (build1 (code0, type, 
+                              fold (build1 (CLEANUP_POINT_EXPR,
+                                            TREE_TYPE (arg00), arg00))));
+
+       if (kind0 == '<' || kind0 == '2'
+           || code0 == TRUTH_ANDIF_EXPR || code0 == TRUTH_ORIF_EXPR
+           || code0 == TRUTH_AND_EXPR   || code0 == TRUTH_OR_EXPR
+           || code0 == TRUTH_XOR_EXPR)
+         {
+           arg01 = TREE_OPERAND (arg0, 1);
+
+           if (! TREE_SIDE_EFFECTS (arg00))
+             return fold (build (code0, type, arg00,
+                                 fold (build1 (CLEANUP_POINT_EXPR,
+                                               TREE_TYPE (arg01), arg01))));
+
+           if (! TREE_SIDE_EFFECTS (arg01))
+             return fold (build (code0, type,
+                                 fold (build1 (CLEANUP_POINT_EXPR,
+                                               TREE_TYPE (arg00), arg00)),
+                                 arg01));
+         }
+
+       return t;
+      }
+
     default:
       return t;
     } /* switch (code) */