OSDN Git Service

Update FSF address.
[pf3gnuchains/gcc-fork.git] / gcc / fold-const.c
index e5184c0..7238e7a 100644 (file)
@@ -1,5 +1,5 @@
 /* Fold a constant sub-tree into a single node for C-compiler
-   Copyright (C) 1987, 1988, 1992, 1993, 1994 Free Software Foundation, Inc.
+   Copyright (C) 1987, 88, 92, 93, 94, 1995 Free Software Foundation, Inc.
 
 This file is part of GNU CC.
 
@@ -15,7 +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.  */
+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".
@@ -63,6 +64,7 @@ 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,
@@ -74,6 +76,7 @@ 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));
 
@@ -185,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
@@ -335,8 +338,10 @@ lshift_double (l1, h1, count, prec, lv, hv, arith)
       return;
     }
   
-  if (count >= prec)
-    count = (unsigned HOST_WIDE_INT) count & prec;
+#ifdef SHIFT_COUNT_TRUNCATED
+  if (SHIFT_COUNT_TRUNCATED)
+    count %= prec;
+#endif
 
   if (count >= HOST_BITS_PER_WIDE_INT)
     {
@@ -368,8 +373,10 @@ rshift_double (l1, h1, count, prec, lv, hv, arith)
              ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
              : 0);
 
-  if (count >= prec)
-    count = (unsigned HOST_WIDE_INT) count % prec;
+#ifdef SHIFT_COUNT_TRUNCATED
+  if (SHIFT_COUNT_TRUNCATED)
+    count %= prec;
+#endif
 
   if (count >= HOST_BITS_PER_WIDE_INT)
     {
@@ -397,34 +404,16 @@ lrotate_double (l1, h1, count, prec, lv, hv)
      int prec;
      HOST_WIDE_INT *lv, *hv;
 {
-  HOST_WIDE_INT arg1[4];
-  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;
+    count += prec;
 
-  carry = arg1[4 - 1] >> 16 - 1;
-  while (count > 0)
-    {
-      for (i = 0; i < 4; i++)
-       {
-         carry += arg1[i] << 1;
-         arg1[i] = LOWPART (carry);
-         carry = HIGHPART (carry);
-       }
-      count--;
-    }
-
-  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
@@ -437,28 +426,16 @@ rrotate_double (l1, h1, count, prec, lv, hv)
      int prec;
      HOST_WIDE_INT *lv, *hv;
 {
-  HOST_WIDE_INT arg1[4];
-  register int i;
-  register int carry;
-
-  encode (arg1, l1, h1);
-
-  if (count > prec)
-    count = prec;
+  HOST_WIDE_INT s1l, s1h, s2l, s2h;
 
-  carry = arg1[0] & 1;
-  while (count > 0)
-    {
-      for (i = 4 - 1; i >= 0; i--)
-       {
-         carry *= BASE;
-         carry += arg1[i];
-         arg1[i] = LOWPART (carry >> 1);
-       }
-      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
@@ -1161,7 +1138,7 @@ const_binop (code, arg1, arg2, notrunc)
     got_it:
       TREE_TYPE (t) = TREE_TYPE (arg1);
       TREE_OVERFLOW (t)
-       = ((notrunc ? !uns && overflow : force_fit_type (t, overflow))
+       = ((notrunc ? !uns && overflow : force_fit_type (t, overflow && !uns))
           | TREE_OVERFLOW (arg1)
           | TREE_OVERFLOW (arg2));
       TREE_CONSTANT_OVERFLOW (t) = (TREE_OVERFLOW (t)
@@ -1327,7 +1304,7 @@ 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.  */
@@ -1561,6 +1538,11 @@ non_lvalue (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.  */
 
@@ -1568,7 +1550,7 @@ tree
 pedantic_non_lvalue (x)
      tree x;
 {
-  if (pedantic)
+  if (pedantic_lvalues)
     return non_lvalue (x);
   else
     return x;
@@ -1764,7 +1746,8 @@ operand_equal_for_comparison_p (arg0, arg1, other)
   if (operand_equal_p (arg0, arg1, 0))
     return 1;
 
-  if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
+  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
@@ -1997,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
@@ -2094,6 +2093,10 @@ invert_truthvalue (arg)
 
     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 ();
@@ -2413,13 +2416,11 @@ decode_field_reference (exp, pbitsize, pbitpos, pmode, punsignedp,
        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 (inner == exp || *pbitsize < 0 || offset != 0)
+  if ((inner == exp && and_mask == 0)
+      || *pbitsize < 0 || offset != 0)
     return 0;
   
   /* Compute the mask to access the bitfield.  */
@@ -2457,12 +2458,12 @@ all_ones_mask_p (mask, size)
   TREE_TYPE (tmask) = signed_type (type);
   force_fit_type (tmask, 0);
   return
-    operand_equal_p (mask, 
-                    const_binop (RSHIFT_EXPR,
-                                 const_binop (LSHIFT_EXPR, tmask,
-                                              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
@@ -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'".
@@ -2833,20 +2864,39 @@ fold_truthop (code, truth_type, lhs, rhs)
   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,
@@ -3225,9 +3275,10 @@ 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
@@ -3236,7 +3287,7 @@ fold (expr)
             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 interfers with later optimizations, suppressing
+            SAVE_EXPR interferes with later optimizations, suppressing
             it when we can is important.  */
 
          if (TREE_CODE (arg0) != SAVE_EXPR
@@ -3280,9 +3331,10 @@ 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
@@ -3341,59 +3393,71 @@ fold (expr)
       if (TREE_TYPE (TREE_OPERAND (t, 0)) == TREE_TYPE (t))
        return TREE_OPERAND (t, 0);
 
-      /* 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 ((TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
-          || TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR)
-         && TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) == TREE_TYPE (t)
-         && ((INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0)))
-              && INTEGRAL_TYPE_P (TREE_TYPE (t)))
-             || (FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0)))
-                 && FLOAT_TYPE_P (TREE_TYPE (t))))
-         && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0)))
-             >= TYPE_PRECISION (TREE_TYPE (t))))
-       return TREE_OPERAND (TREE_OPERAND (t, 0), 0);
-
-      /* 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));
+      /* 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))
@@ -3629,10 +3693,8 @@ fold (expr)
                    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
                {
@@ -3648,9 +3710,9 @@ fold (expr)
                    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);
@@ -3691,9 +3753,9 @@ fold (expr)
                                       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);
@@ -4078,8 +4140,51 @@ 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_sgn (arg1) < 0)
+      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:
@@ -4103,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
@@ -4236,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)
          {
@@ -4257,7 +4366,10 @@ fold (expr)
                  = 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;
              }
          }
@@ -4271,7 +4383,9 @@ fold (expr)
                  = 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;
              }
          }
@@ -4286,16 +4400,15 @@ fold (expr)
            {
            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;
            }
        }
 
@@ -4679,7 +4792,7 @@ fold (expr)
        return pedantic_non_lvalue
          (TREE_OPERAND (t, (integer_zerop (arg0) ? 2 : 1)));
       else if (operand_equal_p (arg1, TREE_OPERAND (expr, 2), 0))
-       return pedantic_non_lvalue (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
@@ -4700,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);
            }
        }
 
@@ -4722,9 +4836,13 @@ fold (expr)
          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)
@@ -4737,12 +4855,16 @@ fold (expr)
              case GE_EXPR:
              case GT_EXPR:
                return pedantic_non_lvalue
-                 (fold (build1 (ABS_EXPR, type, arg1)));
+                 (convert (type, fold (build1 (ABS_EXPR,
+                                               TREE_TYPE (arg1), arg1))));
              case LE_EXPR:
              case LT_EXPR:
                return pedantic_non_lvalue
                  (fold (build1 (NEGATE_EXPR, type,
-                                fold (build1 (ABS_EXPR, type, arg1)))));
+                                convert (type,
+                                         fold (build1 (ABS_EXPR,
+                                                       TREE_TYPE (arg1),
+                                                       arg1))))));
              }
 
          /* If this is A != 0 ? A : 0, this is simply A.  For ==, it is
@@ -4761,21 +4883,29 @@ fold (expr)
 
          if (operand_equal_for_comparison_p (TREE_OPERAND (arg0, 1),
                                              arg2, TREE_OPERAND (arg0, 0)))
-           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
-                 (fold (build (MIN_EXPR, type, arg1, arg2)));
-             case GE_EXPR:
-             case GT_EXPR:
-               return pedantic_non_lvalue
-                 (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,
@@ -4790,8 +4920,9 @@ fold (expr)
              {
              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:
@@ -4851,10 +4982,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);
            }
        }
 
@@ -4929,6 +5061,45 @@ fold (expr)
                                          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) */