[1/3] Make pointer_query cache a private member.

Message ID 20220202233530.2568647-2-msebor@redhat.com
State Committed
Commit 68e9b7b69a1d36ff86c54d52fba034737d9433c3
Headers
Series Enable pointer_query caching throughout. |

Commit Message

Martin Sebor Feb. 2, 2022, 11:35 p.m. UTC
  The first patch in the series make the pointer_query cache a private
member of the class and removes the ability to create an object of it
without one, or one with an external cache.  It also simplifies existing
clients of the class that provide an external cache to avoid doing so.

gcc/ChangeLog:

	* gimple-ssa-warn-access.cc (pass_waccess::pass_waccess): Remove
	pointer_query cache.
	* pointer-query.cc (pointer_query::pointer_query): Remove cache
	argument.  Zero-initialize new cache member.
	(pointer_query::get_ref): Replace cache pointer with direct access.
	(pointer_query::put_ref): Same.
	(pointer_query::flush_cache): Same.
	(pointer_query::dump): Same.
	* pointer-query.h (class pointer_query): Remove cache argument from
	ctor.  Change cache pointer to cache subobject member.
	* tree-ssa-strlen.cc: Remove pointer_query cache.
---
 gcc/gimple-ssa-warn-access.cc |  8 ++--
 gcc/pointer-query.cc          | 74 +++++++++++++++--------------------
 gcc/pointer-query.h           | 12 +++---
 gcc/tree-ssa-strlen.cc        |  8 ++--
 4 files changed, 44 insertions(+), 58 deletions(-)
  

Patch

diff --git a/gcc/gimple-ssa-warn-access.cc b/gcc/gimple-ssa-warn-access.cc
index ad5e2f4458e..4b3d2c00b03 100644
--- a/gcc/gimple-ssa-warn-access.cc
+++ b/gcc/gimple-ssa-warn-access.cc
@@ -2137,10 +2137,9 @@  private:
   /* Return true if use follows an invalidating statement.  */
   bool use_after_inval_p (gimple *, gimple *, bool = false);
 
-  /* A pointer_query object and its cache to store information about
-     pointers and their targets in.  */
+  /* A pointer_query object to store information about pointers and
+     their targets in.  */
   pointer_query m_ptr_qry;
-  pointer_query::cache_type m_var_cache;
   /* Mapping from DECLs and their clobber statements in the function.  */
   hash_map<tree, gimple *> m_clobbers;
   /* A bit is set for each basic block whose statements have been assigned
@@ -2158,8 +2157,7 @@  private:
 
 pass_waccess::pass_waccess (gcc::context *ctxt)
   : gimple_opt_pass (pass_data_waccess, ctxt),
-    m_ptr_qry (NULL, &m_var_cache),
-    m_var_cache (),
+    m_ptr_qry (NULL),
     m_clobbers (),
     m_bb_uids_set (),
     m_func (),
diff --git a/gcc/pointer-query.cc b/gcc/pointer-query.cc
index b092ef4fbdc..afbcd0a15ea 100644
--- a/gcc/pointer-query.cc
+++ b/gcc/pointer-query.cc
@@ -1433,12 +1433,11 @@  ssa_name_limit_t::~ssa_name_limit_t ()
 }
 
 /* Default ctor.  Initialize object with pointers to the range_query
-   and cache_type instances to use or null.  */
+   instance to use or null.  */
 
-pointer_query::pointer_query (range_query *qry /* = NULL */,
-			      cache_type *cache /* = NULL */)
-: rvals (qry), var_cache (cache), hits (), misses (),
-  failures (), depth (), max_depth ()
+pointer_query::pointer_query (range_query *qry /* = NULL */)
+  : rvals (qry), hits (), misses (), failures (), depth (), max_depth (),
+    var_cache ()
 {
   /* No op.  */
 }
@@ -1449,28 +1448,22 @@  pointer_query::pointer_query (range_query *qry /* = NULL */,
 const access_ref *
 pointer_query::get_ref (tree ptr, int ostype /* = 1 */) const
 {
-  if (!var_cache)
-    {
-      ++misses;
-      return NULL;
-    }
-
   unsigned version = SSA_NAME_VERSION (ptr);
   unsigned idx = version << 1 | (ostype & 1);
-  if (var_cache->indices.length () <= idx)
+  if (var_cache.indices.length () <= idx)
     {
       ++misses;
       return NULL;
     }
 
-  unsigned cache_idx = var_cache->indices[idx];
-  if (var_cache->access_refs.length () <= cache_idx)
+  unsigned cache_idx = var_cache.indices[idx];
+  if (var_cache.access_refs.length () <= cache_idx)
     {
       ++misses;
       return NULL;
     }
 
-  access_ref &cache_ref = var_cache->access_refs[cache_idx];
+  const access_ref &cache_ref = var_cache.access_refs[cache_idx];
   if (cache_ref.ref)
     {
       ++hits;
@@ -1491,17 +1484,17 @@  pointer_query::get_ref (tree ptr, gimple *stmt, access_ref *pref,
   const unsigned version
     = TREE_CODE (ptr) == SSA_NAME ? SSA_NAME_VERSION (ptr) : 0;
 
-  if (var_cache && version)
+  if (version)
     {
       unsigned idx = version << 1 | (ostype & 1);
-      if (idx < var_cache->indices.length ())
+      if (idx < var_cache.indices.length ())
 	{
-	  unsigned cache_idx = var_cache->indices[idx] - 1;
-	  if (cache_idx < var_cache->access_refs.length ()
-	      && var_cache->access_refs[cache_idx].ref)
+	  unsigned cache_idx = var_cache.indices[idx] - 1;
+	  if (cache_idx < var_cache.access_refs.length ()
+	      && var_cache.access_refs[cache_idx].ref)
 	    {
 	      ++hits;
-	      *pref = var_cache->access_refs[cache_idx];
+	      *pref = var_cache.access_refs[cache_idx];
 	      return true;
 	    }
 	}
@@ -1525,7 +1518,7 @@  void
 pointer_query::put_ref (tree ptr, const access_ref &ref, int ostype /* = 1 */)
 {
   /* Only add populated/valid entries.  */
-  if (!var_cache || !ref.ref || ref.sizrng[0] < 0)
+  if (!ref.ref || ref.sizrng[0] < 0)
     return;
 
   /* Add REF to the two-level cache.  */
@@ -1535,20 +1528,20 @@  pointer_query::put_ref (tree ptr, const access_ref &ref, int ostype /* = 1 */)
   /* Grow INDICES if necessary.  An index is valid if it's nonzero.
      Its value minus one is the index into ACCESS_REFS.  Not all
      entries are valid.  */
-  if (var_cache->indices.length () <= idx)
-    var_cache->indices.safe_grow_cleared (idx + 1);
+  if (var_cache.indices.length () <= idx)
+    var_cache.indices.safe_grow_cleared (idx + 1);
 
-  if (!var_cache->indices[idx])
-    var_cache->indices[idx] = var_cache->access_refs.length () + 1;
+  if (!var_cache.indices[idx])
+    var_cache.indices[idx] = var_cache.access_refs.length () + 1;
 
   /* Grow ACCESS_REF cache if necessary.  An entry is valid if its
      REF member is nonnull.  All entries except for the last two
      are valid.  Once nonnull, the REF value must stay unchanged.  */
-  unsigned cache_idx = var_cache->indices[idx];
-  if (var_cache->access_refs.length () <= cache_idx)
-    var_cache->access_refs.safe_grow_cleared (cache_idx + 1);
+  unsigned cache_idx = var_cache.indices[idx];
+  if (var_cache.access_refs.length () <= cache_idx)
+    var_cache.access_refs.safe_grow_cleared (cache_idx + 1);
 
-  access_ref &cache_ref = var_cache->access_refs[cache_idx];
+  access_ref &cache_ref = var_cache.access_refs[cache_idx];
   if (cache_ref.ref)
   {
     gcc_checking_assert (cache_ref.ref == ref.ref);
@@ -1563,10 +1556,8 @@  pointer_query::put_ref (tree ptr, const access_ref &ref, int ostype /* = 1 */)
 void
 pointer_query::flush_cache ()
 {
-  if (!var_cache)
-    return;
-  var_cache->indices.release ();
-  var_cache->access_refs.release ();
+  var_cache.indices.release ();
+  var_cache.access_refs.release ();
 }
 
 /* Dump statistics and, optionally, cache contents to DUMP_FILE.  */
@@ -1574,20 +1565,17 @@  pointer_query::flush_cache ()
 void
 pointer_query::dump (FILE *dump_file, bool contents /* = false */)
 {
-  if (!var_cache)
-    return;
-
   unsigned nused = 0, nrefs = 0;
-  unsigned nidxs = var_cache->indices.length ();
+  unsigned nidxs = var_cache.indices.length ();
   for (unsigned i = 0; i != nidxs; ++i)
     {
-      unsigned ari = var_cache->indices[i];
+      unsigned ari = var_cache.indices[i];
       if (!ari)
 	continue;
 
       ++nused;
 
-      const access_ref &aref = var_cache->access_refs[ari];
+      const access_ref &aref = var_cache.access_refs[ari];
       if (!aref.ref)
 	continue;
 
@@ -1604,7 +1592,7 @@  pointer_query::dump (FILE *dump_file, bool contents /* = false */)
 	   "  failures:           %u\n"
 	   "  max_depth:          %u\n",
 	   nidxs, nused,
-	   var_cache->access_refs.length (), nrefs,
+	   var_cache.access_refs.length (), nrefs,
 	   hits, misses, failures, max_depth);
 
   if (!contents || !nidxs)
@@ -1614,11 +1602,11 @@  pointer_query::dump (FILE *dump_file, bool contents /* = false */)
 
   for (unsigned i = 0; i != nidxs; ++i)
     {
-      unsigned ari = var_cache->indices[i];
+      unsigned ari = var_cache.indices[i];
       if (!ari)
 	continue;
 
-      const access_ref &aref = var_cache->access_refs[ari];
+      const access_ref &aref = var_cache.access_refs[ari];
       if (!aref.ref)
 	continue;
 
diff --git a/gcc/pointer-query.h b/gcc/pointer-query.h
index dbdcd593b79..4c725eeaf34 100644
--- a/gcc/pointer-query.h
+++ b/gcc/pointer-query.h
@@ -159,7 +159,6 @@  class pointer_query
 {
   DISABLE_COPY_AND_ASSIGN (pointer_query);
 
-public:
   /* Type of the two-level cache object defined by clients of the class
      to have pointer SSA_NAMEs cached for speedy access.  */
   struct cache_type
@@ -170,8 +169,9 @@  public:
     vec<access_ref> access_refs;
   };
 
-  /* Construct an object with the given Ranger instance and cache.  */
-  explicit pointer_query (range_query * = nullptr, cache_type * = nullptr);
+public:
+  /* Construct an object with the given Ranger instance.  */
+  explicit pointer_query (range_query * = nullptr);
 
   /* Retrieve the access_ref for a variable from cache if it's there.  */
   const access_ref* get_ref (tree, int = 1) const;
@@ -190,8 +190,6 @@  public:
 
   /* A Ranger instance.  May be null to use global ranges.  */
   range_query *rvals;
-  /* Cache of SSA_NAMEs.  May be null to disable caching.  */
-  cache_type *var_cache;
 
   /* Cache performance counters.  */
   mutable unsigned hits;
@@ -199,6 +197,10 @@  public:
   mutable unsigned failures;
   mutable unsigned depth;
   mutable unsigned max_depth;
+
+private:
+  /* Cache of SSA_NAMEs.  May be null to disable caching.  */
+  cache_type var_cache;
 };
 
 /* Describes a pair of references used in an access by built-in
diff --git a/gcc/tree-ssa-strlen.cc b/gcc/tree-ssa-strlen.cc
index df97b86d5f8..d5360f0bc28 100644
--- a/gcc/tree-ssa-strlen.cc
+++ b/gcc/tree-ssa-strlen.cc
@@ -236,8 +236,7 @@  class strlen_pass : public dom_walker
 public:
   strlen_pass (cdi_direction direction)
     : dom_walker (direction),
-      ptr_qry (&m_ranger, &var_cache),
-      var_cache (),
+      ptr_qry (&m_ranger),
       m_cleanup_cfg (false)
   {
   }
@@ -301,10 +300,9 @@  public:
 
   gimple_ranger m_ranger;
 
-  /* A pointer_query object and its cache to store information about
-     pointers and their targets in.  */
+  /* A pointer_query object to store information about pointers and
+     their targets in.  */
   pointer_query ptr_qry;
-  pointer_query::cache_type var_cache;
 
   gimple_stmt_iterator m_gsi;