OSDN Git Service

x
[pf3gnuchains/gcc-fork.git] / gcc / fold-const.c
index b5a3d39..84e098a 100644 (file)
@@ -1,5 +1,5 @@
 /* Fold a constant sub-tree into a single node for C-compiler
-   Copyright (C) 1987, 88, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
+   Copyright (C) 1987, 88, 92-96, 1997 Free Software Foundation, Inc.
 
 This file is part of GNU CC.
 
@@ -47,37 +47,45 @@ Boston, MA 02111-1307, USA.  */
 /* Handle floating overflow for `const_binop'.  */
 static jmp_buf float_error;
 
-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,
+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 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 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 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 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 *, 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, tree));
-static tree fold_truthop PROTO((enum tree_code, tree, tree, tree));
+static int all_ones_mask_p     PROTO((tree, int));
+static int simple_operand_p    PROTO((tree));
+static tree range_binop                PROTO((enum tree_code, tree, tree, int,
+                                      tree, int));
+static tree make_range         PROTO((tree, int *, tree *, tree *));
+static tree build_range_check  PROTO((tree, tree, int, tree, tree));
+static int merge_ranges                PROTO((int *, tree *, tree *, int, tree, tree,
+                                      int, tree, tree));
+static tree fold_range_test    PROTO((tree));
+static tree unextend           PROTO((tree, int, int, tree));
+static tree fold_truthop       PROTO((enum tree_code, tree, tree, tree));
 static tree strip_compound_expr PROTO((tree, tree));
 
 #ifndef BRANCH_COST
@@ -859,6 +867,94 @@ target_negative (x)
   return x < 0;
 }
 #endif /* Target not IEEE */
+
+/* Try to change R into its exact multiplicative inverse in machine mode
+   MODE.  Return nonzero function value if successful.  */
+
+int
+exact_real_inverse (mode, r)
+     enum machine_mode mode;
+     REAL_VALUE_TYPE *r;
+{
+  union
+    {
+      double d;
+      unsigned short i[4];
+    }x, t, y;
+  int i;
+
+  /* Usually disable if bounds checks are not reliable.  */
+  if ((HOST_FLOAT_FORMAT != TARGET_FLOAT_FORMAT) && !flag_pretend_float)
+    return 0;
+
+  /* Set array index to the less significant bits in the unions, depending
+     on the endian-ness of the host doubles.
+     Disable if insufficient information on the data structure.  */
+#if HOST_FLOAT_FORMAT == UNKNOWN_FLOAT_FORMAT
+  return 0;
+#else
+#if HOST_FLOAT_FORMAT == VAX_FLOAT_FORMAT
+#define K 2
+#else
+#if HOST_FLOAT_FORMAT == IBM_FLOAT_FORMAT
+#define K 2
+#else
+#define K (2 * HOST_FLOAT_WORDS_BIG_ENDIAN)
+#endif
+#endif
+#endif
+
+  if (setjmp (float_error))
+    {
+      /* Don't do the optimization if there was an arithmetic error.  */
+fail:
+      set_float_handler (NULL_PTR);
+      return 0;
+    }
+  set_float_handler (float_error);
+
+  /* Domain check the argument.  */
+  x.d = *r;
+  if (x.d == 0.0)
+    goto fail;
+
+#ifdef REAL_INFINITY
+  if (REAL_VALUE_ISINF (x.d) || REAL_VALUE_ISNAN (x.d))
+    goto fail;
+#endif
+
+  /* Compute the reciprocal and check for numerical exactness.
+     It is unnecessary to check all the significand bits to determine
+     whether X is a power of 2.  If X is not, then it is impossible for
+     the bottom half significand of both X and 1/X to be all zero bits.
+     Hence we ignore the data structure of the top half and examine only
+     the low order bits of the two significands.  */
+  t.d = 1.0 / x.d;
+  if (x.i[K] != 0 || x.i[K + 1] != 0 || t.i[K] != 0 || t.i[K + 1] != 0)
+    goto fail;
+
+  /* Truncate to the required mode and range-check the result.  */
+  y.d = REAL_VALUE_TRUNCATE (mode, t.d);
+#ifdef CHECK_FLOAT_VALUE
+  i = 0;
+  if (CHECK_FLOAT_VALUE (mode, y.d, i))
+    goto fail;
+#endif
+
+  /* Fail if truncation changed the value.  */
+  if (y.d != t.d || y.d == 0.0)
+    goto fail;
+
+#ifdef REAL_INFINITY
+  if (REAL_VALUE_ISINF (y.d) || REAL_VALUE_ISNAN (y.d))
+    goto fail;
+#endif
+
+  /* Output the reciprocal and return success flag.  */
+  set_float_handler (NULL_PTR);
+  *r = y.d;
+  return 1;
+}
 #endif /* no REAL_ARITHMETIC */
 \f
 /* Split a tree IN into a constant and a variable part
@@ -950,7 +1046,7 @@ split_tree (in, code, varp, conp, varsignp)
   return 0;
 }
 \f
-/* Combine two constants NUM and ARG2 under operation CODE
+/* Combine two constants ARG1 and ARG2 under operation CODE
    to produce a new constant.
    We assume ARG1 and ARG2 have the same data type,
    or at least are the same kind of constant and the same machine mode.
@@ -976,23 +1072,24 @@ const_binop (code, arg1, arg2, notrunc)
       register tree t;
       int uns = TREE_UNSIGNED (TREE_TYPE (arg1));
       int overflow = 0;
+      int no_overflow = 0;
 
       switch (code)
        {
        case BIT_IOR_EXPR:
-         t = build_int_2 (int1l | int2l, int1h | int2h);
+         low = int1l | int2l, hi = int1h | int2h;
          break;
 
        case BIT_XOR_EXPR:
-         t = build_int_2 (int1l ^ int2l, int1h ^ int2h);
+         low = int1l ^ int2l, hi = int1h ^ int2h;
          break;
 
        case BIT_AND_EXPR:
-         t = build_int_2 (int1l & int2l, int1h & int2h);
+         low = int1l & int2l, hi = int1h & int2h;
          break;
 
        case BIT_ANDTC_EXPR:
-         t = build_int_2 (int1l & ~int2l, int1h & ~int2h);
+         low = int1l & ~int2l, hi = int1h & ~int2h;
          break;
 
        case RSHIFT_EXPR:
@@ -1005,14 +1102,8 @@ const_binop (code, arg1, arg2, notrunc)
                         TYPE_PRECISION (TREE_TYPE (arg1)),
                         &low, &hi,
                         !uns);
-         t = build_int_2 (low, hi);
-         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;
+         no_overflow = 1;
+         break;
 
        case RROTATE_EXPR:
          int2l = - int2l;
@@ -1020,92 +1111,76 @@ const_binop (code, arg1, arg2, notrunc)
          lrotate_double (int1l, int1h, int2l,
                          TYPE_PRECISION (TREE_TYPE (arg1)),
                          &low, &hi);
-         t = build_int_2 (low, hi);
          break;
 
        case PLUS_EXPR:
-         if (int1h == 0)
-           {
-             int2l += int1l;
-             if ((unsigned HOST_WIDE_INT) int2l < int1l)
-               {
-                 hi = int2h++;
-                 overflow = int2h < hi;
-               }
-             t = build_int_2 (int2l, int2h);
-             break;
-           }
-         if (int2h == 0)
-           {
-             int1l += int2l;
-             if ((unsigned HOST_WIDE_INT) int1l < int2l)
-               {
-                 hi = int1h++;
-                 overflow = int1h < hi;
-               }
-             t = build_int_2 (int1l, int1h);
-             break;
-           }
          overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
-         t = build_int_2 (low, hi);
          break;
 
        case MINUS_EXPR:
-         if (int2h == 0 && int2l == 0)
-           {
-             t = build_int_2 (int1l, int1h);
-             break;
-           }
          neg_double (int2l, int2h, &low, &hi);
          add_double (int1l, int1h, low, hi, &low, &hi);
          overflow = overflow_sum_sign (hi, int2h, int1h);
-         t = build_int_2 (low, hi);
          break;
 
        case MULT_EXPR:
          overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
-         t = build_int_2 (low, hi);
          break;
 
        case TRUNC_DIV_EXPR:
        case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
        case EXACT_DIV_EXPR:
-         /* This is a shortcut for a common special case.
-            It reduces the number of tree nodes generated
-            and saves time.  */
+         /* This is a shortcut for a common special case.  */
          if (int2h == 0 && int2l > 0
-             && TREE_TYPE (arg1) == sizetype
+             && ! TREE_CONSTANT_OVERFLOW (arg1)
+             && ! TREE_CONSTANT_OVERFLOW (arg2)
              && int1h == 0 && int1l >= 0)
            {
              if (code == CEIL_DIV_EXPR)
-               int1l += int2l-1;
-             return size_int (int1l / int2l);
+               int1l += int2l - 1;
+             low = int1l / int2l, hi = 0;
+             break;
            }
+
+         /* ... fall through ... */
+
        case ROUND_DIV_EXPR: 
          if (int2h == 0 && int2l == 1)
            {
-             t = build_int_2 (int1l, int1h);
+             low = int1l, hi = int1h;
              break;
            }
-         if (int1l == int2l && int1h == int2h)
+         if (int1l == int2l && int1h == int2h
+             && ! (int1l == 0 && int1h == 0))
            {
-             if ((int1l | int1h) == 0)
-               abort ();
-             t = build_int_2 (1, 0);
+             low = 1, hi = 0;
              break;
            }
          overflow = div_and_round_double (code, uns,
                                           int1l, int1h, int2l, int2h,
                                           &low, &hi, &garbagel, &garbageh);
-         t = build_int_2 (low, hi);
          break;
 
-       case TRUNC_MOD_EXPR: case ROUND_MOD_EXPR: 
+       case TRUNC_MOD_EXPR:
        case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
+         /* This is a shortcut for a common special case.  */
+         if (int2h == 0 && int2l > 0
+             && ! TREE_CONSTANT_OVERFLOW (arg1)
+             && ! TREE_CONSTANT_OVERFLOW (arg2)
+             && int1h == 0 && int1l >= 0)
+           {
+             if (code == CEIL_MOD_EXPR)
+               int1l += int2l - 1;
+             low = int1l % int2l, hi = 0;
+             break;
+           }
+
+         /* ... fall through ... */
+
+       case ROUND_MOD_EXPR: 
          overflow = div_and_round_double (code, uns,
                                           int1l, int1h, int2l, int2h,
                                           &garbagel, &garbageh, &low, &hi);
-         t = build_int_2 (low, hi);
          break;
 
        case MIN_EXPR:
@@ -1127,18 +1202,29 @@ const_binop (code, arg1, arg2, notrunc)
                             < (unsigned HOST_WIDE_INT) int2l)));
            }
          if (low == (code == MIN_EXPR))
-           t = build_int_2 (int1l, int1h);
+           low = int1l, hi = int1h;
          else
-           t = build_int_2 (int2l, int2h);
+           low = int2l, hi = int2h;
          break;
 
        default:
          abort ();
        }
     got_it:
-      TREE_TYPE (t) = TREE_TYPE (arg1);
+      if (TREE_TYPE (arg1) == sizetype && hi == 0
+         && low >= 0 && low <= TREE_INT_CST_LOW (TYPE_MAX_VALUE (sizetype))
+         && ! overflow
+         && ! TREE_OVERFLOW (arg1) && ! TREE_OVERFLOW (arg2))
+       t = size_int (low);
+      else
+       {
+         t = build_int_2 (low, hi);
+         TREE_TYPE (t) = TREE_TYPE (arg1);
+       }
+
       TREE_OVERFLOW (t)
-       = ((notrunc ? !uns && overflow : force_fit_type (t, overflow && !uns))
+       = ((notrunc ? !uns && overflow
+           : force_fit_type (t, overflow && !uns) && ! no_overflow)
           | TREE_OVERFLOW (arg1)
           | TREE_OVERFLOW (arg2));
       TREE_CONSTANT_OVERFLOW (t) = (TREE_OVERFLOW (t)
@@ -1228,6 +1314,7 @@ const_binop (code, arg1, arg2, notrunc)
 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
   if (TREE_CODE (arg1) == COMPLEX_CST)
     {
+      register tree type = TREE_TYPE (arg1);
       register tree r1 = TREE_REALPART (arg1);
       register tree i1 = TREE_IMAGPART (arg1);
       register tree r2 = TREE_REALPART (arg2);
@@ -1237,17 +1324,20 @@ const_binop (code, arg1, arg2, notrunc)
       switch (code)
        {
        case PLUS_EXPR:
-         t = build_complex (const_binop (PLUS_EXPR, r1, r2, notrunc),
+         t = build_complex (type,
+                            const_binop (PLUS_EXPR, r1, r2, notrunc),
                             const_binop (PLUS_EXPR, i1, i2, notrunc));
          break;
 
        case MINUS_EXPR:
-         t = build_complex (const_binop (MINUS_EXPR, r1, r2, notrunc),
+         t = build_complex (type,
+                            const_binop (MINUS_EXPR, r1, r2, notrunc),
                             const_binop (MINUS_EXPR, i1, i2, notrunc));
          break;
 
        case MULT_EXPR:
-         t = build_complex (const_binop (MINUS_EXPR,
+         t = build_complex (type,
+                            const_binop (MINUS_EXPR,
                                          const_binop (MULT_EXPR,
                                                       r1, r2, notrunc),
                                          const_binop (MULT_EXPR,
@@ -1269,32 +1359,33 @@ const_binop (code, arg1, arg2, notrunc)
                             const_binop (MULT_EXPR, i2, i2, notrunc),
                             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));
+           t = build_complex (type,
+                              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;
 
        default:
          abort ();
        }
-      TREE_TYPE (t) = TREE_TYPE (arg1);
       return t;
     }
   return 0;
@@ -1327,6 +1418,7 @@ size_int (number)
     {
       t = build_int_2 (number, 0);
       TREE_TYPE (t) = sizetype;
+      TREE_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (t) = force_fit_type (t, 0);
     }
   return t;
 }
@@ -1344,18 +1436,14 @@ size_binop (code, arg0, arg1)
   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
     {
       /* And some specific cases even faster than that.  */
-      if (code == PLUS_EXPR
-         && TREE_INT_CST_LOW (arg0) == 0
-         && TREE_INT_CST_HIGH (arg0) == 0)
+      if (code == PLUS_EXPR && integer_zerop (arg0))
        return arg1;
-      if (code == MINUS_EXPR
-         && TREE_INT_CST_LOW (arg1) == 0
-         && TREE_INT_CST_HIGH (arg1) == 0)
+      else if ((code == MINUS_EXPR || code == PLUS_EXPR)
+              && integer_zerop (arg1))
        return arg0;
-      if (code == MULT_EXPR
-         && TREE_INT_CST_LOW (arg0) == 1
-         && TREE_INT_CST_HIGH (arg0) == 0)
+      else if (code == MULT_EXPR && integer_onep (arg0))
        return arg1;
+
       /* Handle general case of two integer constants.  */
       return const_binop (code, arg0, arg1, 0);
     }
@@ -1647,46 +1735,61 @@ operand_equal_p (arg0, arg1, only_const)
   STRIP_NOPS (arg0);
   STRIP_NOPS (arg1);
 
-  /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
-     We don't care about side effects in that case because the SAVE_EXPR
-     takes care of that for us.  */
-  if (TREE_CODE (arg0) == SAVE_EXPR && arg0 == arg1)
-    return ! only_const;
-
-  if (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1))
+  if (TREE_CODE (arg0) != TREE_CODE (arg1)
+      /* This is needed for conversions and for COMPONENT_REF.
+        Might as well play it safe and always test this.  */
+      || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
     return 0;
 
-  if (TREE_CODE (arg0) == TREE_CODE (arg1)
-      && TREE_CODE (arg0) == ADDR_EXPR
-      && TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0))
-    return 1;
-
-  if (TREE_CODE (arg0) == TREE_CODE (arg1)
-      && TREE_CODE (arg0) == INTEGER_CST
-      && TREE_INT_CST_LOW (arg0) == TREE_INT_CST_LOW (arg1)
-      && TREE_INT_CST_HIGH (arg0) == TREE_INT_CST_HIGH (arg1))
+  /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
+     We don't care about side effects in that case because the SAVE_EXPR
+     takes care of that for us. In all other cases, two expressions are
+     equal if they have no side effects.  If we have two identical
+     expressions with side effects that should be treated the same due
+     to the only side effects being identical SAVE_EXPR's, that will
+     be detected in the recursive calls below.  */
+  if (arg0 == arg1 && ! only_const
+      && (TREE_CODE (arg0) == SAVE_EXPR
+         || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
     return 1;
 
-  /* Detect when real constants are equal.  */
-  if (TREE_CODE (arg0) == TREE_CODE (arg1)
-      && TREE_CODE (arg0) == REAL_CST)
-    return !bcmp ((char *) &TREE_REAL_CST (arg0),
-                 (char *) &TREE_REAL_CST (arg1),
-                 sizeof (REAL_VALUE_TYPE));
+  /* Next handle constant cases, those for which we can return 1 even
+     if ONLY_CONST is set.  */
+  if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
+    switch (TREE_CODE (arg0))
+      {
+      case INTEGER_CST:
+       return (! TREE_CONSTANT_OVERFLOW (arg0)
+               && ! TREE_CONSTANT_OVERFLOW (arg1)
+               && TREE_INT_CST_LOW (arg0) == TREE_INT_CST_LOW (arg1)
+               && TREE_INT_CST_HIGH (arg0) == TREE_INT_CST_HIGH (arg1));
+
+      case REAL_CST:
+       return (! TREE_CONSTANT_OVERFLOW (arg0)
+               && ! TREE_CONSTANT_OVERFLOW (arg1)
+               && REAL_VALUES_EQUAL (TREE_REAL_CST (arg0),
+                                     TREE_REAL_CST (arg1)));
+
+      case COMPLEX_CST:
+       return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
+                                only_const)
+               && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
+                                   only_const));
+
+      case STRING_CST:
+       return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
+               && ! strncmp (TREE_STRING_POINTER (arg0),
+                             TREE_STRING_POINTER (arg1),
+                             TREE_STRING_LENGTH (arg0)));
+
+      case ADDR_EXPR:
+       return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
+                               0);
+      }
 
   if (only_const)
     return 0;
 
-  if (arg0 == arg1)
-    return 1;
-
-  if (TREE_CODE (arg0) != TREE_CODE (arg1))
-    return 0;
-  /* This is needed for conversions and for COMPONENT_REF.
-     Might as well play it safe and always test this.  */
-  if (TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
-    return 0;
-
   switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
     {
     case '1':
@@ -1701,10 +1804,22 @@ operand_equal_p (arg0, arg1, only_const)
 
     case '<':
     case '2':
-      return (operand_equal_p (TREE_OPERAND (arg0, 0),
-                              TREE_OPERAND (arg1, 0), 0)
+      if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0)
+         && operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1),
+                             0))
+       return 1;
+
+      /* For commutative ops, allow the other order.  */
+      return ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MULT_EXPR
+              || TREE_CODE (arg0) == MIN_EXPR || TREE_CODE (arg0) == MAX_EXPR
+              || TREE_CODE (arg0) == BIT_IOR_EXPR
+              || TREE_CODE (arg0) == BIT_XOR_EXPR
+              || TREE_CODE (arg0) == BIT_AND_EXPR
+              || TREE_CODE (arg0) == NE_EXPR || TREE_CODE (arg0) == EQ_EXPR)
+             && operand_equal_p (TREE_OPERAND (arg0, 0),
+                                 TREE_OPERAND (arg1, 1), 0)
              && operand_equal_p (TREE_OPERAND (arg0, 1),
-                                 TREE_OPERAND (arg1, 1), 0));
+                                 TREE_OPERAND (arg1, 0), 0));
 
     case 'r':
       switch (TREE_CODE (arg0))
@@ -2215,6 +2330,7 @@ optimize_bit_field_compare (code, compare_type, lhs, rhs)
   enum machine_mode lmode, rmode, lnmode, rnmode;
   int lunsignedp, runsignedp;
   int lvolatilep = 0, rvolatilep = 0;
+  int alignment;
   tree linner, rinner;
   tree mask;
   tree offset;
@@ -2223,7 +2339,7 @@ optimize_bit_field_compare (code, compare_type, lhs, rhs)
      if the same as the size of the underlying object, we aren't doing an
      extraction at all and so can do nothing.  */
   linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
-                               &lunsignedp, &lvolatilep);
+                               &lunsignedp, &lvolatilep, &alignment);
   if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
       || offset != 0)
     return 0;
@@ -2232,8 +2348,8 @@ optimize_bit_field_compare (code, compare_type, lhs, rhs)
    {
      /* If this is not a constant, we can only do something if bit positions,
        sizes, and signedness are the same.   */
-     rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset,
-                                  &rmode, &runsignedp, &rvolatilep);
+     rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
+                                  &runsignedp, &rvolatilep, &alignment);
 
      if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
         || lunsignedp != runsignedp || offset != 0)
@@ -2406,6 +2522,7 @@ decode_field_reference (exp, pbitsize, pbitpos, pmode, punsignedp,
   tree mask, inner, offset;
   tree unsigned_type;
   int precision;
+  int alignment;
 
   /* All the optimizations using this function assume integer fields.  
      There are problems with FP fields since the type_for_size call
@@ -2426,7 +2543,7 @@ decode_field_reference (exp, pbitsize, pbitpos, pmode, punsignedp,
 
 
   inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
-                              punsignedp, pvolatilep);
+                              punsignedp, pvolatilep, &alignment);
   if ((inner == exp && and_mask == 0)
       || *pbitsize < 0 || offset != 0)
     return 0;
@@ -2504,134 +2621,510 @@ simple_operand_p (exp)
              && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
 }
 \f
-/* Subroutine for fold_truthop: try to optimize a range test.
+/* The following functions are subroutines to fold_range_test and allow it to
+   try to change a logical combination of comparisons into a range test.
+
+   For example, both
+       X == 2 && X == 3 && X == 4 && X == 5
+   and
+       X >= 2 && X <= 5
+   are converted to
+       (unsigned) (X - 2) <= 3
+
+   We decribe each set of comparisons as being either inside or outside
+   a range, using a variable named like IN_P, and then describe the
+   range with a lower and upper bound.  If one of the bounds is omitted,
+   it represents either the highest or lowest value of the type.
+
+   In the comments below, we represent a range by two numbers in brackets
+   preceeded by a "+" to designate being inside that range, or a "-" to
+   designate being outside that range, so the condition can be inverted by
+   flipping the prefix.  An omitted bound is represented by a "-".  For
+   example, "- [-, 10]" means being outside the range starting at the lowest
+   possible value and ending at 10, in other words, being greater than 10.
+   The range "+ [-, -]" is always true and hence the range "- [-, -]" is
+   always false.
+
+   We set up things so that the missing bounds are handled in a consistent
+   manner so neither a missing bound nor "true" and "false" need to be
+   handled using a special case.  */
+
+/* Return the result of applying CODE to ARG0 and ARG1, but handle the case
+   of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
+   and UPPER1_P are nonzero if the respective argument is an upper bound
+   and zero for a lower.  TYPE, if nonzero, is the type of the result; it
+   must be specified for a comparison.  ARG1 will be converted to ARG0's
+   type if both are specified.  */
+
+static tree
+range_binop (code, type, arg0, upper0_p, arg1, upper1_p)
+     enum tree_code code;
+     tree type;
+     tree arg0, arg1;
+     int upper0_p, upper1_p;
+{
+  tree tem;
+  int result;
+  int sgn0, sgn1;
 
-   For example, "i >= 2 && i =< 9" can be done as "(unsigned) (i - 2) <= 7".
+  /* If neither arg represents infinity, do the normal operation.
+     Else, if not a comparison, return infinity.  Else handle the special
+     comparison rules. Note that most of the cases below won't occur, but
+     are handled for consistency.  */
 
-   JCODE is the logical combination of the two terms.  It is TRUTH_AND_EXPR
-   (representing TRUTH_ANDIF_EXPR and TRUTH_AND_EXPR) or TRUTH_OR_EXPR
-   (representing TRUTH_ORIF_EXPR and TRUTH_OR_EXPR).  TYPE is the type of
-   the result.
+  if (arg0 != 0 && arg1 != 0)
+    {
+      tem = fold (build (code, type != 0 ? type : TREE_TYPE (arg0),
+                        arg0, convert (TREE_TYPE (arg0), arg1)));
+      STRIP_NOPS (tem);
+      return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
+    }
 
-   VAR is the value being tested.  LO_CODE and HI_CODE are the comparison
-   operators comparing VAR to LO_CST and HI_CST.  LO_CST is known to be no
-   larger than HI_CST (they may be equal).
+  if (TREE_CODE_CLASS (code) != '<')
+    return 0;
 
-   We return the simplified tree or 0 if no optimization is possible.  */
+  /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
+     for neither.  Then compute our result treating them as never equal
+     and comparing bounds to non-bounds as above.  */
+  sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
+  sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
+  switch (code)
+    {
+    case EQ_EXPR:  case NE_EXPR:
+      result = (code == NE_EXPR);
+      break;
+    case LT_EXPR:  case LE_EXPR:
+      result = sgn0 < sgn1;
+      break;
+    case GT_EXPR:  case GE_EXPR:
+      result = sgn0 > sgn1;
+      break;
+    }
+
+  return convert (type, result ? integer_one_node : integer_zero_node);
+}
+\f      
+/* Given EXP, a logical expression, set the range it is testing into
+   variables denoted by PIN_P, PLOW, and PHIGH.  Return the expression
+   actually being tested.  *PLOW and *PHIGH will have be made the same type
+   as the returned expression.  If EXP is not a comparison, we will most
+   likely not be returning a useful value and range.  */
 
 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;
+make_range (exp, pin_p, plow, phigh)
+     tree exp;
+     int *pin_p;
+     tree *plow, *phigh;
 {
-  tree utype;
-  enum tree_code rcode;
+  enum tree_code code;
+  tree arg0, arg1, type;
+  int in_p, n_in_p;
+  tree low, high, n_low, n_high;
 
-  /* See if this is a range test and normalize the constant terms.  */
+  /* Start with simply saying "EXP != 0" and then look at the code of EXP
+     and see if we can refine the range.  Some of the cases below may not
+     happen, but it doesn't seem worth worrying about this.  We "continue"
+     the outer loop when we've changed something; otherwise we "break"
+     the switch, which will "break" the while.  */
 
-  if (jcode == TRUTH_AND_EXPR)
+  in_p = 0, low = high = convert (TREE_TYPE (exp), integer_zero_node);
+
+  while (1)
     {
-      switch (lo_code)
+      code = TREE_CODE (exp);
+      arg0 = TREE_OPERAND (exp, 0), arg1 = TREE_OPERAND (exp, 1);
+      if (TREE_CODE_CLASS (code) == '<' || TREE_CODE_CLASS (code) == '1'
+         || TREE_CODE_CLASS (code) == '2')
+       type = TREE_TYPE (arg0);
+
+      switch (code)
        {
-       case NE_EXPR:
-         /* See if we have VAR != CST && VAR != CST+1.  */
-         if (! (hi_code == NE_EXPR
-                && TREE_INT_CST_LOW (hi_cst) - TREE_INT_CST_LOW (lo_cst) == 1
-                && tree_int_cst_equal (integer_one_node,
-                                       const_binop (MINUS_EXPR,
-                                                    hi_cst, lo_cst, 0))))
-           return 0;
+       case TRUTH_NOT_EXPR:
+         in_p = ! in_p, exp = arg0;
+         continue;
+
+       case EQ_EXPR: case NE_EXPR:
+       case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
+         /* We can only do something if the range is testing for zero
+            and if the second operand is an integer constant.  Note that
+            saying something is "in" the range we make is done by
+            complementing IN_P since it will set in the initial case of
+            being not equal to zero; "out" is leaving it alone.  */
+         if (low == 0 || high == 0
+             || ! integer_zerop (low) || ! integer_zerop (high)
+             || TREE_CODE (arg1) != INTEGER_CST)
+           break;
 
-         rcode = GT_EXPR;
-         break;
+         switch (code)
+           {
+           case NE_EXPR:  /* - [c, c]  */
+             low = high = arg1;
+             break;
+           case EQ_EXPR:  /* + [c, c]  */
+             in_p = ! in_p, low = high = arg1;
+             break;
+           case GT_EXPR:  /* - [-, c] */
+             low = 0, high = arg1;
+             break;
+           case GE_EXPR:  /* + [c, -] */
+             in_p = ! in_p, low = arg1, high = 0;
+             break;
+           case LT_EXPR:  /* - [c, -] */
+             low = arg1, high = 0;
+             break;
+           case LE_EXPR:  /* + [-, c] */
+             in_p = ! in_p, low = 0, high = arg1;
+             break;
+           }
 
-       case GT_EXPR:
-       case GE_EXPR:
-         if (hi_code == LT_EXPR)
-           hi_cst = const_binop (MINUS_EXPR, hi_cst, integer_one_node, 0);
-         else if (hi_code != LE_EXPR)
-           return 0;
+         exp = arg0;
 
-         if (lo_code == GT_EXPR)
-           lo_cst = const_binop (PLUS_EXPR, lo_cst, integer_one_node, 0);
+         /* If this is an unsigned comparison, we also know that EXP is
+            greater than or equal to zero.  We base the range tests we make
+            on that fact, so we record it here so we can parse existing
+            range tests.  */
+         if (TREE_UNSIGNED (type) && (low == 0 || high == 0))
+           {
+             if (! merge_ranges (&n_in_p, &n_low, &n_high, in_p, low, high,
+                                 1, convert (type, integer_zero_node),
+                                 NULL_TREE))
+               break;
 
-         /* We now have VAR >= LO_CST && VAR <= HI_CST.  */
-         rcode = LE_EXPR;
-         break;
+             in_p = n_in_p, low = n_low, high = n_high;
 
-       default:
-         return 0;
+             /* If the high bound is missing, reverse the range so it
+                goes from zero to the low bound minus 1.  */
+             if (high == 0)
+               {
+                 in_p = ! in_p;
+                 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
+                                     integer_one_node, 0);
+                 low = convert (type, integer_zero_node);
+               }
+           }
+         continue;
+
+       case NEGATE_EXPR:
+         /* (-x) IN [a,b] -> x in [-b, -a]  */
+         n_low = range_binop (MINUS_EXPR, type,
+                              convert (type, integer_zero_node), 0, high, 1);
+         n_high = range_binop (MINUS_EXPR, type,
+                               convert (type, integer_zero_node), 0, low, 0);
+         low = n_low, high = n_high;
+         exp = arg0;
+         continue;
+
+       case BIT_NOT_EXPR:
+         /* ~ X -> -X - 1  */
+         exp = build (MINUS_EXPR, type, build1 (NEGATE_EXPR, type, arg0),
+                      convert (type, integer_one_node));
+         continue;
+
+       case PLUS_EXPR:  case MINUS_EXPR:
+         if (TREE_CODE (arg1) != INTEGER_CST)
+           break;
+
+         /* If EXP is signed, any overflow in the computation is undefined,
+            so we don't worry about it so long as our computations on
+            the bounds don't overflow.  For unsigned, overflow is defined
+            and this is exactly the right thing.  */
+         n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
+                              type, low, 0, arg1, 0);
+         n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
+                               type, high, 1, arg1, 0);
+         if ((n_low != 0 && TREE_OVERFLOW (n_low))
+             || (n_high != 0 && TREE_OVERFLOW (n_high)))
+           break;
+
+         /* Check for an unsigned range which has wrapped around the maximum
+            value thus making n_high < n_low, and normalize it.  */
+         if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
+           {
+             low = range_binop (PLUS_EXPR, type, n_high, 0,
+                                integer_one_node, 0);
+             high = range_binop (MINUS_EXPR, type, n_low, 0,
+                                integer_one_node, 0);
+             in_p = ! in_p;
+           }
+         else
+           low = n_low, high = n_high;
+
+         exp = arg0;
+         continue;
+
+       case NOP_EXPR:  case NON_LVALUE_EXPR:  case CONVERT_EXPR:
+         if (! INTEGRAL_TYPE_P (type)
+             || (low != 0 && ! int_fits_type_p (low, type))
+             || (high != 0 && ! int_fits_type_p (high, type)))
+           break;
+
+         if (low != 0)
+           low = convert (type, low);
+
+         if (high != 0)
+           high = convert (type, high);
+
+         exp = arg0;
+         continue;
        }
+
+      break;
     }
-  else
+
+  /* If EXP is a constant, we can evaluate whether this is true or false.  */
+  if (TREE_CODE (exp) == INTEGER_CST)
     {
-      switch (lo_code)
-       {
-       case EQ_EXPR:
-         /* See if we have VAR == CST || VAR == CST+1.  */
-         if (! (hi_code == EQ_EXPR
-                && TREE_INT_CST_LOW (hi_cst) - TREE_INT_CST_LOW (lo_cst) == 1
-                && tree_int_cst_equal (integer_one_node,
-                                       const_binop (MINUS_EXPR,
-                                                    hi_cst, lo_cst, 0))))
-           return 0;
+      in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
+                                                exp, 0, low, 0))
+                     && integer_onep (range_binop (LE_EXPR, integer_type_node,
+                                                   exp, 1, high, 1)));
+      low = high = 0;
+      exp = 0;
+    }
 
-         rcode = LE_EXPR;
-         break;
+  *pin_p = in_p, *plow = low, *phigh = high;
+  return exp;
+}
+\f
+/* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
+   type, TYPE, return an expression to test if EXP is in (or out of, depending
+   on IN_P) the range.  */
 
-       case LE_EXPR:
-       case LT_EXPR:
-         if (hi_code == GE_EXPR)
-           hi_cst = const_binop (MINUS_EXPR, hi_cst, integer_one_node, 0);
-         else if (hi_code != GT_EXPR)
-           return 0;
+static tree
+build_range_check (type, exp, in_p, low, high)
+     tree type;
+     tree exp;
+     int in_p;
+     tree low, high;
+{
+  tree etype = TREE_TYPE (exp);
+  tree utype, value;
 
-         if (lo_code == LE_EXPR)
-           lo_cst = const_binop (PLUS_EXPR, lo_cst, integer_one_node, 0);
+  if (! in_p
+      && (0 != (value = build_range_check (type, exp, 1, low, high))))
+    return invert_truthvalue (value);
 
-         /* We now have VAR < LO_CST || VAR > HI_CST.  */
-         rcode = GT_EXPR;
-         break;
+  else if (low == 0 && high == 0)
+    return convert (type, integer_one_node);
 
-       default:
-         return 0;
-       }
+  else if (low == 0)
+    return fold (build (LE_EXPR, type, exp, high));
+
+  else if (high == 0)
+    return fold (build (GE_EXPR, type, exp, low));
+
+  else if (operand_equal_p (low, high, 0))
+    return fold (build (EQ_EXPR, type, exp, low));
+
+  else if (TREE_UNSIGNED (etype) && integer_zerop (low))
+    return build_range_check (type, exp, 1, 0, high);
+
+  else if (integer_zerop (low))
+    {
+      utype = unsigned_type (etype);
+      return build_range_check (type, convert (utype, exp), 1, 0,
+                               convert (utype, high));
     }
 
-  /* When normalizing, it is possible to both increment the smaller constant
-     and decrement the larger constant.  See if they are still ordered.  */
-  if (tree_int_cst_lt (hi_cst, lo_cst))
+  else if (0 != (value = const_binop (MINUS_EXPR, high, low, 0))
+          && ! TREE_OVERFLOW (value))
+    return build_range_check (type,
+                             fold (build (MINUS_EXPR, etype, exp, low)),
+                             1, convert (etype, integer_zero_node), value);
+  else
     return 0;
+}
+\f
+/* Given two ranges, see if we can merge them into one.  Return 1 if we 
+   can, 0 if we can't.  Set the output range into the specified parameters.  */
 
-  /* Fail if VAR isn't an integer.  */
-  utype = TREE_TYPE (var);
-  if (! INTEGRAL_TYPE_P (utype))
-    return 0;
+static int
+merge_ranges (pin_p, plow, phigh, in0_p, low0, high0, in1_p, low1, high1)
+     int *pin_p;
+     tree *plow, *phigh;
+     int in0_p, in1_p;
+     tree low0, high0, low1, high1;
+{
+  int no_overlap;
+  int subset;
+  int temp;
+  tree tem;
+  int in_p;
+  tree low, high;
+
+  /* Make range 0 be the range that starts first.  Swap them if it isn't.  */
+  if (integer_onep (range_binop (GT_EXPR, integer_type_node, 
+                                low0, 0, low1, 0))
+      || (((low0 == 0 && low1 == 0)
+          || integer_onep (range_binop (EQ_EXPR, integer_type_node,
+                                        low0, 0, low1, 0)))
+         && integer_onep (range_binop (GT_EXPR, integer_type_node,
+                                       high0, 1, high1, 1))))
+    {
+      temp = in0_p, in0_p = in1_p, in1_p = temp;
+      tem = low0, low0 = low1, low1 = tem;
+      tem = high0, high0 = high1, high1 = tem;
+    }
 
-  /* The range test is invalid if subtracting the two constants results
-     in overflow.  This can happen in traditional mode.  */
-  if (! int_fits_type_p (hi_cst, TREE_TYPE (var))
-      || ! int_fits_type_p (lo_cst, TREE_TYPE (var)))
-    return 0;
+  /* Now flag two cases, whether the ranges are disjoint or whether the
+     second range is totally subsumed in the first.  Note that the tests
+     below are simplified by the ones above.  */
+  no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
+                                         high0, 1, low1, 0));
+  subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
+                                     high1, 1, high0, 1));
+
+  /* We now have four cases, depending on whether we are including or
+     excluding the two ranges.  */
+  if (in0_p && in1_p)
+    {
+      /* If they don't overlap, the result is false.  If the second range
+        is a subset it is the result.  Otherwise, the range is from the start
+        of the second to the end of the first.  */
+      if (no_overlap)
+       in_p = 0, low = high = 0;
+      else if (subset)
+       in_p = 1, low = low1, high = high1;
+      else
+       in_p = 1, low = low1, high = high0;
+    }
+
+  else if (in0_p && ! in1_p)
+    {
+      /* If they don't overlap, the result is the first range.  If the
+        second range is a subset of the first, we can't describe this as
+        a single range unless both ranges end at the same place.  If both
+        ranges start in the same place, then the result is false.
+        Otherwise, we go from the start of the first range to just before
+        the start of the second.  */
+      if (no_overlap)
+       in_p = 1, low = low0, high = high0;
+      else if (subset
+              && integer_zerop (range_binop (EQ_EXPR, integer_type_node,
+                                             high0, 1, high1, 0)))
+       return 0;
+      else if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
+                                         low0, 0, low1, 0)))
+       in_p = 0, low = high = 0;
+      else
+       {
+         in_p = 1, low = low0;
+         high = range_binop (MINUS_EXPR, NULL_TREE, low1, 0,
+                             integer_one_node, 0);
+       }
+    }
+
+  else if (! in0_p && in1_p)
+    {
+      /* If they don't overlap, the result is the second range.  If the second
+        is a subset of the first, the result is false.  Otherwise,
+        the range starts just after the first range and ends at the
+        end of the second.  */
+      if (no_overlap)
+       in_p = 1, low = low1, high = high1;
+      else if (subset)
+       in_p = 0, low = high = 0;
+      else
+       {
+         in_p = 1, high = high1;
+         low = range_binop (PLUS_EXPR, NULL_TREE, high0, 1,
+                            integer_one_node, 0);
+       }
+    }
 
-  if (! TREE_UNSIGNED (utype))
+  else
     {
-      utype = unsigned_type (utype);
-      return fold (convert (type,
-                           build (rcode, utype,
-                                  convert (utype,
-                                           build (MINUS_EXPR, TREE_TYPE (var),
-                                                  var, lo_cst)),
-                                  convert (utype,
-                                           const_binop (MINUS_EXPR, hi_cst,
-                                                        lo_cst, 0)))));
+      /* The case where we are excluding both ranges.  Here the complex case
+        is if they don't overlap.  In that case, the only time we have a
+        range is if they are adjacent.  If the second is a subset of the
+        first, the result is the first.  Otherwise, the range to exclude
+        starts at the beginning of the first range and ends at the end of the
+        second.  */
+      if (no_overlap)
+       {
+         if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
+                                        range_binop (PLUS_EXPR, NULL_TREE,
+                                                     high0, 1,
+                                                     integer_one_node, 1),
+                                        1, low1, 0)))
+           in_p = 0, low = low0, high = high1;
+         else
+           return 0;
+       }
+      else if (subset)
+       in_p = 0, low = low0, high = high0;
+      else
+       in_p = 0, low = low0, high = high1;
+    }
+
+  *pin_p = in_p, *plow = low, *phigh = high;
+  return 1;
+}
+\f
+/* EXP is some logical combination of boolean tests.  See if we can
+   merge it into some range test.  Return the new tree if so.  */
+
+static tree
+fold_range_test (exp)
+     tree exp;
+{
+  int or_op = (TREE_CODE (exp) == TRUTH_ORIF_EXPR
+              || TREE_CODE (exp) == TRUTH_OR_EXPR);
+  int in0_p, in1_p, in_p;
+  tree low0, low1, low, high0, high1, high;
+  tree lhs = make_range (TREE_OPERAND (exp, 0), &in0_p, &low0, &high0);
+  tree rhs = make_range (TREE_OPERAND (exp, 1), &in1_p, &low1, &high1);
+  tree tem;
+
+  /* If this is an OR operation, invert both sides; we will invert
+     again at the end.  */
+  if (or_op)
+    in0_p = ! in0_p, in1_p = ! in1_p;
+
+  /* If both expressions are the same, if we can merge the ranges, and we
+     can build the range test, return it or it inverted.  If one of the
+     ranges is always true or always false, consider it to be the same
+     expression as the other.  */
+  if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
+      && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
+                      in1_p, low1, high1)
+      && 0 != (tem = (build_range_check (TREE_TYPE (exp),
+                                        lhs != 0 ? lhs
+                                        : rhs != 0 ? rhs : integer_zero_node,
+                                        in_p, low, high))))
+    return or_op ? invert_truthvalue (tem) : tem;
+
+  /* On machines where the branch cost is expensive, if this is a
+     short-circuited branch and the underlying object on both sides
+     is the same, make a non-short-circuit operation.  */
+  else if (BRANCH_COST >= 2
+          && (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
+              || TREE_CODE (exp) == TRUTH_ORIF_EXPR)
+          && operand_equal_p (lhs, rhs, 0))
+    {
+      /* If simple enough, just rewrite.  Otherwise, make a SAVE_EXPR.  */
+      if (simple_operand_p (lhs))
+       return build (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
+                     ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
+                     TREE_TYPE (exp), TREE_OPERAND (exp, 0),
+                     TREE_OPERAND (exp, 1));
+      else
+       {
+         tree common = save_expr (lhs);
+
+         if (0 != (lhs = build_range_check (TREE_TYPE (exp), common,
+                                            or_op ? ! in0_p : in0_p,
+                                            low0, high0))
+             && (0 != (rhs = build_range_check (TREE_TYPE (exp), common,
+                                                or_op ? ! in1_p : in1_p,
+                                                low1, high1))))
+           return build (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
+                         ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
+                         TREE_TYPE (exp), lhs, rhs);
+       }
     }
   else
-      return fold (convert (type,
-                           build (rcode, utype,
-                                  build (MINUS_EXPR, utype, var, lo_cst),
-                                  const_binop (MINUS_EXPR, hi_cst,
-                                               lo_cst, 0))));
+    return 0;
 }
 \f
 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
@@ -2653,18 +3146,28 @@ unextend (c, p, unsignedp, mask)
   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);
+
+  /* We must use a signed type in order to get an arithmetic right shift.
+     However, we must also avoid introducing accidental overflows, so that
+     a subsequent call to integer_zerop will work.  Hence we must 
+     do the type conversion here.  At this point, the constant is either
+     zero or one, and the conversion to a signed type can never overflow.
+     We could get an overflow if this conversion is done anywhere else.  */
+  if (TREE_UNSIGNED (type))
+    temp = convert (signed_type (type), temp);
+
   temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
   temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
   if (mask != 0)
     temp = const_binop (BIT_AND_EXPR, temp, convert (TREE_TYPE (c), mask), 0);
+  /* If necessary, convert the type back to match the type of C.  */
+  if (TREE_UNSIGNED (type))
+    temp = convert (type, temp);
 
   return convert (type, const_binop (BIT_XOR_EXPR, c, temp, 0));
 }
@@ -2725,13 +3228,11 @@ fold_truthop (code, truth_type, lhs, rhs)
   int first_bit, end_bit;
   int volatilep;
 
-  /* Start by getting the comparison codes and seeing if this looks like
-     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.  */
+  /* Start by getting the comparison codes.  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))
+  if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
     return 0;
 
   lcode = TREE_CODE (lhs);
@@ -2743,8 +3244,7 @@ fold_truthop (code, truth_type, lhs, rhs)
   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) != '<')
+  if (TREE_CODE_CLASS (lcode) != '<' || TREE_CODE_CLASS (rcode) != '<')
     return 0;
 
   code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
@@ -2755,36 +3255,6 @@ fold_truthop (code, truth_type, lhs, rhs)
   rl_arg = TREE_OPERAND (rhs, 0);
   rr_arg = TREE_OPERAND (rhs, 1);
   
-  if (TREE_CODE (lr_arg) == INTEGER_CST
-      && TREE_CODE (rr_arg) == INTEGER_CST
-      && operand_equal_p (ll_arg, rl_arg, 0))
-    {
-      if (tree_int_cst_lt (lr_arg, rr_arg))
-       result = range_test (code, truth_type, lcode, rcode,
-                            ll_arg, lr_arg, rr_arg);
-      else
-       result = range_test (code, truth_type, rcode, lcode,
-                            ll_arg, rr_arg, lr_arg);
-
-      /* If this isn't a range test, it also isn't a comparison that
-        can be merged.  However, it wins to evaluate the RHS unconditionally
-        on machines with expensive branches.   */
-
-      if (result == 0 && BRANCH_COST >= 2)
-       {
-         if (TREE_CODE (ll_arg) != VAR_DECL
-             && TREE_CODE (ll_arg) != PARM_DECL)
-           {
-             /* Avoid evaluating the variable part twice.  */
-             ll_arg = save_expr (ll_arg);
-             lhs = build (lcode, TREE_TYPE (lhs), ll_arg, lr_arg);
-             rhs = build (rcode, TREE_TYPE (rhs), ll_arg, rr_arg);
-           }
-         return build (code, truth_type, lhs, rhs);
-       }
-      return result;
-    }
-
   /* If the RHS can be evaluated unconditionally and its operands are
      simple, it wins to evaluate the RHS unconditionally on machines
      with expensive branches.  In this case, this isn't a comparison
@@ -3291,7 +3761,8 @@ fold (expr)
                      fold (build (code, type,
                                   arg0, TREE_OPERAND (arg1, 1))));
       else if (TREE_CODE (arg1) == COND_EXPR
-              || TREE_CODE_CLASS (TREE_CODE (arg1)) == '<')
+              || (TREE_CODE_CLASS (TREE_CODE (arg1)) == '<'
+                  && TREE_CODE_CLASS (code) != '<'))
        {
          tree test, true_value, false_value;
 
@@ -3347,7 +3818,8 @@ fold (expr)
        return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
                      fold (build (code, type, TREE_OPERAND (arg0, 1), arg1)));
       else if (TREE_CODE (arg0) == COND_EXPR
-              || TREE_CODE_CLASS (TREE_CODE (arg0)) == '<')
+              || (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
+                  && TREE_CODE_CLASS (code) != '<'))
        {
          tree test, true_value, false_value;
 
@@ -3566,7 +4038,6 @@ fold (expr)
            }
          else if (TREE_CODE (arg0) == REAL_CST)
            t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
-         TREE_TYPE (t) = type;
        }
       else if (TREE_CODE (arg0) == NEGATE_EXPR)
        return TREE_OPERAND (arg0, 0);
@@ -3605,7 +4076,6 @@ fold (expr)
                t = build_real (type,
                                REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
            }
-         TREE_TYPE (t) = type;
        }
       else if (TREE_CODE (arg0) == ABS_EXPR || TREE_CODE (arg0) == NEGATE_EXPR)
        return build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0));
@@ -3621,7 +4091,7 @@ fold (expr)
                                    TREE_TYPE (TREE_TYPE (arg0)),
                                    TREE_OPERAND (arg0, 1))));
       else if (TREE_CODE (arg0) == COMPLEX_CST)
-       return build_complex (TREE_OPERAND (arg0, 0),
+       return build_complex (type, TREE_OPERAND (arg0, 0),
                              fold (build1 (NEGATE_EXPR,
                                            TREE_TYPE (TREE_TYPE (arg0)),
                                            TREE_OPERAND (arg0, 1))));
@@ -3638,9 +4108,8 @@ fold (expr)
     case BIT_NOT_EXPR:
       if (wins)
        {
-         if (TREE_CODE (arg0) == INTEGER_CST)
-           t = build_int_2 (~ TREE_INT_CST_LOW (arg0),
-                            ~ TREE_INT_CST_HIGH (arg0));
+         t = build_int_2 (~ TREE_INT_CST_LOW (arg0),
+                          ~ TREE_INT_CST_HIGH (arg0));
          TREE_TYPE (t) = type;
          force_fit_type (t, 0);
          TREE_OVERFLOW (t) = TREE_OVERFLOW (arg0);
@@ -3807,7 +4276,9 @@ fold (expr)
        {
          /* The return value should always have
             the same type as the original expression.  */
-         TREE_TYPE (t1) = TREE_TYPE (t);
+         if (TREE_TYPE (t1) != TREE_TYPE (t))
+           t1 = convert (TREE_TYPE (t), t1);
+
          return t1;
        }
       return t;
@@ -4037,11 +4508,24 @@ fold (expr)
         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));
-
+      if (TREE_CODE (arg1) == REAL_CST)
+       {
+         if (flag_fast_math
+             && 0 != (tem = const_binop (code, build_real (type, dconst1),
+                                         arg1, 0)))
+           return fold (build (MULT_EXPR, type, arg0, tem));
+         /* Find the reciprocal if optimizing and the result is exact. */
+         else if (optimize)
+           {
+             REAL_VALUE_TYPE r;
+             r = TREE_REAL_CST (arg1);
+             if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
+                 {
+                   tem = build_real (type, r);
+                   return fold (build (MULT_EXPR, type, arg0, tem));
+                 }
+           }
+       }
       goto binary;
 
     case TRUNC_DIV_EXPR:
@@ -4293,6 +4777,10 @@ fold (expr)
         must be evaluated.  */
       if (integer_zerop (arg1))
        return omit_one_operand (type, arg1, arg0);
+      /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
+        case will be handled here.  */
+      if (integer_zerop (arg0))
+       return omit_one_operand (type, arg0, arg1);
 
     truth_andor:
       /* We only do these simplifications if we are optimizing.  */
@@ -4339,6 +4827,10 @@ fold (expr)
                                a01));
        }
 
+      /* See if we can build a range comparison.  */
+      if (0 != (tem = fold_range_test (t)))
+       return tem;
+
       /* 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.  */
@@ -4369,6 +4861,10 @@ fold (expr)
         evaluate first arg.  */
       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
        return omit_one_operand (type, arg1, arg0);
+      /* Likewise for first arg, but note this only occurs here for
+        TRUTH_OR_EXPR.  */
+      if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
+       return omit_one_operand (type, arg0, arg1);
       goto truth_andor;
 
     case TRUTH_XOR_EXPR:
@@ -4614,6 +5110,9 @@ fold (expr)
            case LE_EXPR:
              if (INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
                {
+                 if (type == integer_type_node)
+                   return integer_one_node;
+
                  t = build_int_2 (1, 0);
                  TREE_TYPE (t) = type;
                  return t;
@@ -4626,9 +5125,12 @@ fold (expr)
              /* For NE, we can only do this simplification if integer.  */
              if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
                break;
-             /* ... fall through ... */
+             /* ... fall through ...  */
            case GT_EXPR:
            case LT_EXPR:
+             if (type == integer_type_node)
+               return integer_zero_node;
+
              t = build_int_2 (0, 0);
              TREE_TYPE (t) = type;
              return t;
@@ -4763,11 +5265,11 @@ fold (expr)
 
       /* If this is a comparison of a field, we may be able to simplify it.  */
       if ((TREE_CODE (arg0) == COMPONENT_REF
-               || TREE_CODE (arg0) == BIT_FIELD_REF)
-              && (code == EQ_EXPR || code == NE_EXPR)
-              /* Handle the constant case even without -O
-                 to make sure the warnings are given.  */
-              && (optimize || TREE_CODE (arg1) == INTEGER_CST))
+          || TREE_CODE (arg0) == BIT_FIELD_REF)
+         && (code == EQ_EXPR || code == NE_EXPR)
+         /* Handle the constant case even without -O
+            to make sure the warnings are given.  */
+         && (optimize || TREE_CODE (arg1) == INTEGER_CST))
        {
          t1 = optimize_bit_field_compare (code, type, arg0, arg1);
          return t1 ? t1 : t;
@@ -4838,6 +5340,7 @@ fold (expr)
                              0);
        }
 
+#if 0 /* This is no longer useful, but breaks some real code.  */
       /* Assume a nonexplicit constant cannot equal an explicit one,
         since such code would be undefined anyway.
         Exception: on sysvr4, using #pragma weak,
@@ -4848,7 +5351,7 @@ fold (expr)
               && TREE_CODE (arg0) == ADDR_EXPR
               && code == EQ_EXPR)
        t1 = build_int_2 (0, 0);
-
+#endif
       /* Two real constants can be compared explicitly.  */
       else if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
        {
@@ -5123,7 +5626,7 @@ fold (expr)
 
     case COMPLEX_EXPR:
       if (wins)
-       return build_complex (arg0, arg1);
+       return build_complex (type, arg0, arg1);
       return t;
 
     case REALPART_EXPR: