I have changed the use of the array_bounds_checker in VRP to use a
ranger in my local tree to make sure there are no regressions when using
either VRP or the ranger.  In doing so I noticed that the checker
does not pass context to get_value_range, which causes the ranger to miss a
few cases.  This patch fixes the oversight.

Tested on x86-64 Linux using the array bounds checker both with VRP and
the ranger.

gcc/ChangeLog:

        * gimple-array-bounds.cc (array_bounds_checker::get_value_range):
        Add gimple argument.
        (array_bounds_checker::check_array_ref): Same.
        (array_bounds_checker::check_addr_expr): Same.
        (array_bounds_checker::check_array_bounds): Pass statement to
        check_array_bounds and check_addr_expr.
        * gimple-array-bounds.h (check_array_bounds): Add gimple argument.
        (check_addr_expr): Same.
        (get_value_range): Same.
---
 gcc/gimple-array-bounds.cc | 17 +++++++++--------
 gcc/gimple-array-bounds.h  |  6 +++---
 2 files changed, 12 insertions(+), 11 deletions(-)

diff --git a/gcc/gimple-array-bounds.cc b/gcc/gimple-array-bounds.cc
index 8dfd6f9500a..598c76bf52e 100644
--- a/gcc/gimple-array-bounds.cc
+++ b/gcc/gimple-array-bounds.cc
@@ -43,9 +43,9 @@ along with GCC; see the file COPYING3.  If not see
 // break the dependency on equivalences for this pass.
 
 const value_range *
-array_bounds_checker::get_value_range (const_tree op)
+array_bounds_checker::get_value_range (const_tree op, gimple *stmt)
 {
-  return ranges->get_value_range (op);
+  return ranges->get_value_range (op, stmt);
 }
 
 /* Try to determine the DECL that REF refers to.  Return the DECL or
@@ -173,7 +173,7 @@ trailing_array (tree arg, tree *pref)
 
 bool
 array_bounds_checker::check_array_ref (location_t location, tree ref,
-                                      bool ignore_off_by_one)
+                                      gimple *stmt, bool ignore_off_by_one)
 {
   if (warning_suppressed_p (ref, OPT_Warray_bounds))
     /* Return true to have the caller prevent warnings for enclosing
@@ -287,7 +287,7 @@ array_bounds_checker::check_array_ref (location_t location, 
tree ref,
   const value_range *vr = NULL;
   if (TREE_CODE (low_sub) == SSA_NAME)
     {
-      vr = get_value_range (low_sub);
+      vr = get_value_range (low_sub, stmt);
       if (!vr->undefined_p () && !vr->varying_p ())
        {
          low_sub = vr->kind () == VR_RANGE ? vr->max () : vr->min ();
@@ -563,7 +563,8 @@ array_bounds_checker::check_mem_ref (location_t location, 
tree ref,
    address of an ARRAY_REF, and call check_array_ref on it.  */
 
 void
-array_bounds_checker::check_addr_expr (location_t location, tree t)
+array_bounds_checker::check_addr_expr (location_t location, tree t,
+                                      gimple *stmt)
 {
   /* For the most significant subscript only, accept taking the address
      of the just-past-the-end element.  */
@@ -575,7 +576,7 @@ array_bounds_checker::check_addr_expr (location_t location, 
tree t)
       bool warned = false;
       if (TREE_CODE (t) == ARRAY_REF)
        {
-         warned = check_array_ref (location, t, ignore_off_by_one);
+         warned = check_array_ref (location, t, stmt, ignore_off_by_one);
          ignore_off_by_one = false;
        }
       else if (TREE_CODE (t) == MEM_REF)
@@ -728,14 +729,14 @@ array_bounds_checker::check_array_bounds (tree *tp, int 
*walk_subtree,
   bool warned = false;
   array_bounds_checker *checker = (array_bounds_checker *) wi->info;
   if (TREE_CODE (t) == ARRAY_REF)
-    warned = checker->check_array_ref (location, t,
+    warned = checker->check_array_ref (location, t, wi->stmt,
                                       false/*ignore_off_by_one*/);
   else if (TREE_CODE (t) == MEM_REF)
     warned = checker->check_mem_ref (location, t,
                                     false /*ignore_off_by_one*/);
   else if (TREE_CODE (t) == ADDR_EXPR)
     {
-      checker->check_addr_expr (location, t);
+      checker->check_addr_expr (location, t, wi->stmt);
       *walk_subtree = false;
     }
   else if (inbounds_memaccess_p (t))
diff --git a/gcc/gimple-array-bounds.h b/gcc/gimple-array-bounds.h
index fa64262777d..d8f7ff7a89f 100644
--- a/gcc/gimple-array-bounds.h
+++ b/gcc/gimple-array-bounds.h
@@ -31,10 +31,10 @@ public:
 
 private:
   static tree check_array_bounds (tree *tp, int *walk_subtree, void *data);
-  bool check_array_ref (location_t, tree, bool ignore_off_by_one);
+  bool check_array_ref (location_t, tree, gimple *, bool ignore_off_by_one);
   bool check_mem_ref (location_t, tree, bool ignore_off_by_one);
-  void check_addr_expr (location_t, tree);
-  const value_range *get_value_range (const_tree op);
+  void check_addr_expr (location_t, tree, gimple *);
+  const value_range *get_value_range (const_tree op, gimple *);
 
   struct function *fun;
   range_query *ranges;
-- 
2.31.1

Reply via email to