OSDN Git Service

PR ada/24533
[pf3gnuchains/gcc-fork.git] / gcc / tree-vrp.c
index f88b23e..c4a921c 100644 (file)
@@ -1,5 +1,5 @@
 /* Support routines for Value Range Propagation (VRP).
-   Copyright (C) 2005 Free Software Foundation, Inc.
+   Copyright (C) 2005, 2006 Free Software Foundation, Inc.
    Contributed by Diego Novillo <dnovillo@redhat.com>.
 
 This file is part of GCC.
@@ -528,12 +528,14 @@ compare_values (tree val1, tree val2)
   if (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2))
     return -2;
 
-  /* We cannot compare overflowed values.  */
-  if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
-    return -2;
-
   if (!POINTER_TYPE_P (TREE_TYPE (val1)))
-    return tree_int_cst_compare (val1, val2);
+    {
+      /* We cannot compare overflowed values.  */
+      if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
+       return -2;
+
+      return tree_int_cst_compare (val1, val2);
+    }
   else
     {
       tree t;
@@ -1046,6 +1048,96 @@ extract_range_from_assert (value_range_t *vr_p, tree expr)
       if (compare_values (var_vr->min, vr_p->min) == 0
          && compare_values (var_vr->max, vr_p->max) == 0)
        set_value_range_to_varying (vr_p);
+      else
+       {
+         tree min, max, anti_min, anti_max, real_min, real_max;
+
+         /* We want to compute the logical AND of the two ranges;
+            there are three cases to consider.
+
+
+            1. The VR_ANTI_RANGE range is competely within the 
+               VR_RANGE and the endpoints of the ranges are
+               different.  In that case the resulting range
+               should be the VR_ANTI_RANGE.
+
+            2. The VR_ANTI_RANGE is completely disjoint from
+               the VR_RANGE.  In this case the resulting range
+               should be the VR_RANGE.
+
+            3. There is some overlap between the VR_ANTI_RANGE
+               and the VR_RANGE.
+
+               3a. If the high limit of the VR_ANTI_RANGE resides
+                   within the VR_RANGE, then the result is a new
+                   VR_RANGE starting at the high limit of the
+                   the VR_ANTI_RANGE + 1 and extending to the
+                   high limit of the original VR_RANGE.
+
+               3b. If the low limit of the VR_ANTI_RANGE resides
+                   within the VR_RANGE, then the result is a new
+                   VR_RANGE starting at the low limit of the original
+                   VR_RANGE and extending to the low limit of the
+                   VR_ANTI_RANGE - 1.  */
+         if (vr_p->type == VR_ANTI_RANGE)
+           {
+             anti_min = vr_p->min;
+             anti_max = vr_p->max;
+             real_min = var_vr->min;
+             real_max = var_vr->max;
+           }
+         else
+           {
+             anti_min = var_vr->min;
+             anti_max = var_vr->max;
+             real_min = vr_p->min;
+             real_max = vr_p->max;
+           }
+
+
+         /* Case 1, VR_ANTI_RANGE completely within VR_RANGE,
+            not including any endpoints.  */
+         if (compare_values (anti_max, real_max) == -1
+             && compare_values (anti_min, real_min) == 1)
+           {
+             set_value_range (vr_p, VR_ANTI_RANGE, anti_min,
+                              anti_max, vr_p->equiv);
+           }
+         /* Case 2, VR_ANTI_RANGE completely disjoint from
+            VR_RANGE.  */
+         else if (compare_values (anti_min, real_max) == 1
+                  || compare_values (anti_max, real_min) == -1)
+           {
+             set_value_range (vr_p, VR_RANGE, real_min,
+                              real_max, vr_p->equiv);
+           }
+         /* Case 3a, the anti-range extends into the low
+            part of the real range.  Thus creating a new
+            low for the real reange.  */
+         else if ((compare_values (anti_max, real_min) == 1
+                   || compare_values (anti_max, real_min) == 0)
+                  && compare_values (anti_max, real_max) == -1)
+           {
+             min = fold_build2 (PLUS_EXPR, TREE_TYPE (var_vr->min),
+                                anti_max,
+                                build_int_cst (TREE_TYPE (var_vr->min), 1));
+             max = real_max;
+             set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
+           }
+         /* Case 3b, the anti-range extends into the high
+            part of the real range.  Thus creating a new
+            higher for the real reange.  */
+         else if (compare_values (anti_min, real_min) == 1
+                  && (compare_values (anti_min, real_max) == -1
+                      || compare_values (anti_min, real_max) == 0))
+           {
+             max = fold_build2 (MINUS_EXPR, TREE_TYPE (var_vr->min),
+                                anti_min,
+                                build_int_cst (TREE_TYPE (var_vr->min), 1));
+             min = real_min;
+             set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
+           }
+       }
     }
 
   /* Remove names from the equivalence set that have ranges
@@ -1165,6 +1257,7 @@ static void
 extract_range_from_binary_expr (value_range_t *vr, tree expr)
 {
   enum tree_code code = TREE_CODE (expr);
+  enum value_range_type type;
   tree op0, op1, min, max;
   int cmp;
   value_range_t vr0 = { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL };
@@ -1182,6 +1275,7 @@ extract_range_from_binary_expr (value_range_t *vr, tree expr)
       && code != ROUND_DIV_EXPR
       && code != MIN_EXPR
       && code != MAX_EXPR
+      && code != BIT_AND_EXPR
       && code != TRUTH_ANDIF_EXPR
       && code != TRUTH_ORIF_EXPR
       && code != TRUTH_AND_EXPR
@@ -1217,14 +1311,22 @@ extract_range_from_binary_expr (value_range_t *vr, tree expr)
       return;
     }
 
+  /* The type of the resulting value range defaults to VR0.TYPE.  */
+  type = vr0.type;
+
   /* Refuse to operate on VARYING ranges, ranges of different kinds
-     and symbolic ranges.  TODO, we may be able to derive anti-ranges
-     in some cases.  */
-  if (vr0.type == VR_VARYING
-      || vr1.type == VR_VARYING
-      || vr0.type != vr1.type
-      || symbolic_range_p (&vr0)
-      || symbolic_range_p (&vr1))
+     and symbolic ranges.  As an exception, we allow BIT_AND_EXPR
+     because we may be able to derive a useful range even if one of
+     the operands is VR_VARYING or symbolic range.  TODO, we may be
+     able to derive anti-ranges in some cases.  */
+  if (code != BIT_AND_EXPR
+      && code != TRUTH_AND_EXPR
+      && code != TRUTH_OR_EXPR
+      && (vr0.type == VR_VARYING
+         || vr1.type == VR_VARYING
+         || vr0.type != vr1.type
+         || symbolic_range_p (&vr0)
+         || symbolic_range_p (&vr1)))
     {
       set_value_range_to_varying (vr);
       return;
@@ -1267,9 +1369,47 @@ extract_range_from_binary_expr (value_range_t *vr, tree expr)
       || code == TRUTH_OR_EXPR
       || code == TRUTH_XOR_EXPR)
     {
-      /* Boolean expressions cannot be folded with int_const_binop.  */
-      min = fold_binary (code, TREE_TYPE (expr), vr0.min, vr1.min);
-      max = fold_binary (code, TREE_TYPE (expr), vr0.max, vr1.max);
+      /* If one of the operands is zero, we know that the whole
+        expression evaluates zero.  */
+      if (code == TRUTH_AND_EXPR
+         && ((vr0.type == VR_RANGE
+              && integer_zerop (vr0.min)
+              && integer_zerop (vr0.max))
+             || (vr1.type == VR_RANGE
+                 && integer_zerop (vr1.min)
+                 && integer_zerop (vr1.max))))
+       {
+         type = VR_RANGE;
+         min = max = build_int_cst (TREE_TYPE (expr), 0);
+       }
+      /* If one of the operands is one, we know that the whole
+        expression evaluates one.  */
+      else if (code == TRUTH_OR_EXPR
+              && ((vr0.type == VR_RANGE
+                   && integer_onep (vr0.min)
+                   && integer_onep (vr0.max))
+                  || (vr1.type == VR_RANGE
+                      && integer_onep (vr1.min)
+                      && integer_onep (vr1.max))))
+       {
+         type = VR_RANGE;
+         min = max = build_int_cst (TREE_TYPE (expr), 1);
+       }
+      else if (vr0.type != VR_VARYING
+              && vr1.type != VR_VARYING
+              && vr0.type == vr1.type
+              && !symbolic_range_p (&vr0)
+              && !symbolic_range_p (&vr1))
+       {
+         /* Boolean expressions cannot be folded with int_const_binop.  */
+         min = fold_binary (code, TREE_TYPE (expr), vr0.min, vr1.min);
+         max = fold_binary (code, TREE_TYPE (expr), vr0.max, vr1.max);
+       }
+      else
+       {
+         set_value_range_to_varying (vr);
+         return;
+       }
     }
   else if (code == PLUS_EXPR
           || code == MIN_EXPR
@@ -1362,12 +1502,13 @@ extract_range_from_binary_expr (value_range_t *vr, tree expr)
       max = val[0];
       for (i = 1; i < 4; i++)
        {
-         if (TREE_OVERFLOW (min) || TREE_OVERFLOW (max))
+         if (!is_gimple_min_invariant (min) || TREE_OVERFLOW (min)
+             || !is_gimple_min_invariant (max) || TREE_OVERFLOW (max))
            break;
 
          if (val[i])
            {
-             if (TREE_OVERFLOW (val[i]))
+             if (!is_gimple_min_invariant (val[i]) || TREE_OVERFLOW (val[i]))
                {
                  /* If we found an overflowed value, set MIN and MAX
                     to it so that we set the resulting range to
@@ -1404,12 +1545,38 @@ extract_range_from_binary_expr (value_range_t *vr, tree expr)
       min = vrp_int_const_binop (code, vr0.min, vr1.max);
       max = vrp_int_const_binop (code, vr0.max, vr1.min);
     }
+  else if (code == BIT_AND_EXPR)
+    {
+      if (vr0.type == VR_RANGE
+         && vr0.min == vr0.max
+         && tree_expr_nonnegative_p (vr0.max)
+         && TREE_CODE (vr0.max) == INTEGER_CST)
+       {
+         min = build_int_cst (TREE_TYPE (expr), 0);
+         max = vr0.max;
+       }
+      else if (vr1.type == VR_RANGE
+         && vr1.min == vr1.max
+         && tree_expr_nonnegative_p (vr1.max)
+         && TREE_CODE (vr1.max) == INTEGER_CST)
+       {
+         type = VR_RANGE;
+         min = build_int_cst (TREE_TYPE (expr), 0);
+         max = vr1.max;
+       }
+      else
+       {
+         set_value_range_to_varying (vr);
+         return;
+       }
+    }
   else
     gcc_unreachable ();
 
   /* If either MIN or MAX overflowed, then set the resulting range to
      VARYING.  */
-  if (TREE_OVERFLOW (min) || TREE_OVERFLOW (max))
+  if (!is_gimple_min_invariant (min) || TREE_OVERFLOW (min)
+      || !is_gimple_min_invariant (max) || TREE_OVERFLOW (max))
     {
       set_value_range_to_varying (vr);
       return;
@@ -1424,7 +1591,7 @@ extract_range_from_binary_expr (value_range_t *vr, tree expr)
       set_value_range_to_varying (vr);
     }
   else
-    set_value_range (vr, vr0.type, min, max, NULL);
+    set_value_range (vr, type, min, max, NULL);
 }
 
 
@@ -1878,6 +2045,10 @@ compare_ranges (enum tree_code comp, value_range_t *vr0, value_range_t *vr1)
          else if (cmp_min != -2 && cmp_max != -2)
            return boolean_false_node;
        }
+      /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1.  */
+      else if (compare_values (vr0->min, vr1->max) == 1
+              || compare_values (vr1->min, vr0->max) == 1)
+       return boolean_false_node;
 
       return NULL_TREE;
     }
@@ -2158,21 +2329,21 @@ build_assert_expr_for (tree cond, tree v)
 
   if (COMPARISON_CLASS_P (cond))
     {
-      tree a = build (ASSERT_EXPR, TREE_TYPE (v), v, cond); 
-      assertion = build (MODIFY_EXPR, TREE_TYPE (v), n, a);
+      tree a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond); 
+      assertion = build2 (MODIFY_EXPR, TREE_TYPE (v), n, a);
     }
   else if (TREE_CODE (cond) == TRUTH_NOT_EXPR)
     {
       /* Given !V, build the assignment N = false.  */
       tree op0 = TREE_OPERAND (cond, 0);
       gcc_assert (op0 == v);
-      assertion = build (MODIFY_EXPR, TREE_TYPE (v), n, boolean_false_node);
+      assertion = build2 (MODIFY_EXPR, TREE_TYPE (v), n, boolean_false_node);
     }
   else if (TREE_CODE (cond) == SSA_NAME)
     {
       /* Given V, build the assignment N = true.  */
       gcc_assert (v == cond);
-      assertion = build (MODIFY_EXPR, TREE_TYPE (v), n, boolean_true_node);
+      assertion = build2 (MODIFY_EXPR, TREE_TYPE (v), n, boolean_true_node);
     }
   else
     gcc_unreachable ();
@@ -2775,7 +2946,7 @@ process_assert_insertions_for (tree name, assert_locus_t loc)
   edge_iterator ei;
   edge e;
 
-  cond = build (loc->comp_code, boolean_type_node, name, loc->val);
+  cond = build2 (loc->comp_code, boolean_type_node, name, loc->val);
   assert_expr = build_assert_expr_for (cond, name);
 
   if (loc->e)
@@ -2987,7 +3158,7 @@ remove_range_assertions (void)
              }
 
            /* And finally, remove the copy, it is not needed.  */
-           bsi_remove (&si);
+           bsi_remove (&si, true);
          }
        else
          bsi_next (&si);
@@ -3889,7 +4060,7 @@ test_for_singularity (enum tree_code cond_code, tree op0,
       if (cond_code == GT_EXPR)
        {
          tree one = build_int_cst (TREE_TYPE (op0), 1);
-         max = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), max, one);
+         min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
        }
     }
 
@@ -3906,10 +4077,10 @@ test_for_singularity (enum tree_code cond_code, tree op0,
       else
        max = vr->max;
 
-      /* If the new min/max values have converged to a
-        single value, then there is only one value which
-        can satisfy the condition, return that value.  */
-      if (min == max && is_gimple_min_invariant (min))
+      /* If the new min/max values have converged to a single value,
+        then there is only one value which can satisfy the condition,
+        return that value.  */
+      if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
        return min;
     }
   return NULL;
@@ -3951,7 +4122,7 @@ simplify_cond_using_ranges (tree stmt)
                }
 
              COND_EXPR_COND (stmt)
-               = build (EQ_EXPR, boolean_type_node, op0, new);
+               = build2 (EQ_EXPR, boolean_type_node, op0, new);
              update_stmt (stmt);
 
              if (dump_file)
@@ -3979,7 +4150,7 @@ simplify_cond_using_ranges (tree stmt)
                }
 
              COND_EXPR_COND (stmt)
-               = build (NE_EXPR, boolean_type_node, op0, new);
+               = build2 (NE_EXPR, boolean_type_node, op0, new);
              update_stmt (stmt);
 
              if (dump_file)