diff mbox series

[COMMITTED] Add a merge_range to ssa_cache and use it.

Message ID 4378bf33-4718-ff59-4083-769d4485c352@redhat.com
State New
Headers show
Series [COMMITTED] Add a merge_range to ssa_cache and use it. | expand

Commit Message

Andrew MacLeod July 28, 2023, 8:38 p.m. UTC
This adds some tweaks to the ssa-range cache.

1)   Adds a new merge_range which works like set_range, except if there 
is already a value, the two values are merged via intersection and 
stored.   THis avpoids having to check if there is a value, load it, 
intersect it then store that in the client. There is one usage pattern 
(but more to come) in the code base.. change to use it.

2)  The range_of_expr() method in ssa_cache does not set the stmt to a 
default of NULL.  Correct that oversight.

3)  the method empty_p() is added to the ssa_lazy_cache class so we can 
detect if the lazy cache has any active elements in it or not.

Bootstrapped on 86_64-pc-linux-gnu with no regressions.   Pushed.

Andrew
diff mbox series

Patch

From 72fb44ca53fda15024e0c272052b74b1f32735b1 Mon Sep 17 00:00:00 2001
From: Andrew MacLeod <amacleod@redhat.com>
Date: Fri, 28 Jul 2023 11:00:57 -0400
Subject: [PATCH 3/3] Add a merge_range to ssa_cache and use it.  add empty_p
 and param tweaks.

	* gimple-range-cache.cc (ssa_cache::merge_range): New.
	(ssa_lazy_cache::merge_range): New.
	* gimple-range-cache.h (class ssa_cache): Adjust protoypes.
	(class ssa_lazy_cache): Ditto.
	* gimple-range.cc (assume_query::calculate_op): Use merge_range.
---
 gcc/gimple-range-cache.cc | 45 +++++++++++++++++++++++++++++++++++++++
 gcc/gimple-range-cache.h  |  6 ++++--
 gcc/gimple-range.cc       |  6 ++----
 3 files changed, 51 insertions(+), 6 deletions(-)

diff --git a/gcc/gimple-range-cache.cc b/gcc/gimple-range-cache.cc
index 52165d2405b..5b74681b61a 100644
--- a/gcc/gimple-range-cache.cc
+++ b/gcc/gimple-range-cache.cc
@@ -605,6 +605,32 @@  ssa_cache::set_range (tree name, const vrange &r)
   return m != NULL;
 }
 
+// If NAME has a range, intersect it with R, otherwise set it to R.
+// Return TRUE if there was already a range set, otherwise false.
+
+bool
+ssa_cache::merge_range (tree name, const vrange &r)
+{
+  unsigned v = SSA_NAME_VERSION (name);
+  if (v >= m_tab.length ())
+    m_tab.safe_grow_cleared (num_ssa_names + 1);
+
+  vrange_storage *m = m_tab[v];
+  if (m)
+    {
+      Value_Range curr (TREE_TYPE (name));
+      m->get_vrange (curr, TREE_TYPE (name));
+      curr.intersect (r);
+      if (m->fits_p (curr))
+	m->set_vrange (curr);
+      else
+	m_tab[v] = m_range_allocator->clone (curr);
+    }
+  else
+    m_tab[v] = m_range_allocator->clone (r);
+  return m != NULL;
+}
+
 // Set the range for NAME to R in the ssa cache.
 
 void
@@ -689,6 +715,25 @@  ssa_lazy_cache::set_range (tree name, const vrange &r)
   return false;
 }
 
+// If NAME has a range, intersect it with R, otherwise set it to R.
+// Return TRUE if there was already a range set, otherwise false.
+
+bool
+ssa_lazy_cache::merge_range (tree name, const vrange &r)
+{
+  unsigned v = SSA_NAME_VERSION (name);
+  if (!bitmap_set_bit (active_p, v))
+    {
+      // There is already an entry, simply merge it.
+      gcc_checking_assert (v < m_tab.length ());
+      return ssa_cache::merge_range (name, r);
+    }
+  if (v >= m_tab.length ())
+    m_tab.safe_grow (num_ssa_names + 1);
+  m_tab[v] = m_range_allocator->clone (r);
+  return false;
+}
+
 // Return TRUE if NAME has a range, and return it in R.
 
 bool
diff --git a/gcc/gimple-range-cache.h b/gcc/gimple-range-cache.h
index a0f436b5723..bbb9b18a10c 100644
--- a/gcc/gimple-range-cache.h
+++ b/gcc/gimple-range-cache.h
@@ -61,11 +61,11 @@  public:
   virtual bool has_range (tree name) const;
   virtual bool get_range (vrange &r, tree name) const;
   virtual bool set_range (tree name, const vrange &r);
+  virtual bool merge_range (tree name, const vrange &r);
   virtual void clear_range (tree name);
   virtual void clear ();
   void dump (FILE *f = stderr);
-  virtual bool range_of_expr (vrange &r, tree expr, gimple *stmt);
-
+  virtual bool range_of_expr (vrange &r, tree expr, gimple *stmt = NULL);
 protected:
   vec<vrange_storage *> m_tab;
   vrange_allocator *m_range_allocator;
@@ -80,8 +80,10 @@  class ssa_lazy_cache : public ssa_cache
 public:
   inline ssa_lazy_cache () { active_p = BITMAP_ALLOC (NULL); }
   inline ~ssa_lazy_cache () { BITMAP_FREE (active_p); }
+  inline bool empty_p () const { return bitmap_empty_p (active_p); }
   virtual bool has_range (tree name) const;
   virtual bool set_range (tree name, const vrange &r);
+  virtual bool merge_range (tree name, const vrange &r);
   virtual bool get_range (vrange &r, tree name) const;
   virtual void clear_range (tree name);
   virtual void clear ();
diff --git a/gcc/gimple-range.cc b/gcc/gimple-range.cc
index 01e62d3ff39..01173c58f02 100644
--- a/gcc/gimple-range.cc
+++ b/gcc/gimple-range.cc
@@ -809,10 +809,8 @@  assume_query::calculate_op (tree op, gimple *s, vrange &lhs, fur_source &src)
   if (m_gori.compute_operand_range (op_range, s, lhs, op, src)
       && !op_range.varying_p ())
     {
-      Value_Range range (TREE_TYPE (op));
-      if (global.get_range (range, op))
-	op_range.intersect (range);
-      global.set_range (op, op_range);
+      // Set the global range, merging if there is already a range.
+      global.merge_range (op, op_range);
       gimple *def_stmt = SSA_NAME_DEF_STMT (op);
       if (def_stmt && gimple_get_lhs (def_stmt) == op)
 	calculate_stmt (def_stmt, op_range, src);
-- 
2.40.1