From 05ff069ba937dc3970f2a757e426935fcf4c15fb Mon Sep 17 00:00:00 2001
From: Andrew MacLeod <amacleod@redhat.com>
Date: Wed, 22 May 2024 19:27:01 -0400
Subject: [PATCH 3/5] More tweaks from gimple_outgoing_range changes.

the dom_ranger used for fast vrp no longer needs a local
gimple_outgoing_range object as it is now always available from the
range_query parent class.

The builtin_unreachable code for adjusting globals and removing the
builtin calls during the final VRP pass can now function with just
a range_query object rather than a specific ranger.   This adjusts it to
use the extra methods in the range_query API.
This will now allow removal of builtin_unreachable calls even if there is no
active ranger with dependency info available.

	* gimple-range.cc (dom_ranger::dom_ranger): Do not initialize m_out.
	(dom_ranger::maybe_push_edge): Use gori () rather than m_out.
	* gimple-range.h (dom_ranger::m_out): Remove.
	* tree-vrp.cc (remove_unreachable::remove_unreachable): Use a
	range-query ranther than a gimple_ranger.
	(remove_unreachable::remove): New.
	(remove_unreachable::m_ranger): Change to a range_query.
	(remove_unreachable::handle_early): If there is no dependency
	information, do nothing.
	(remove_unreachable::remove_and_update_globals): Do not update
	globals if there is no dependecy info to use.
---
 gcc/gimple-range.cc |  4 ++--
 gcc/gimple-range.h  |  1 -
 gcc/tree-vrp.cc     | 47 +++++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 47 insertions(+), 5 deletions(-)

diff --git a/gcc/gimple-range.cc b/gcc/gimple-range.cc
index 0749c9fa215..711646abb67 100644
--- a/gcc/gimple-range.cc
+++ b/gcc/gimple-range.cc
@@ -922,7 +922,7 @@ assume_query::dump (FILE *f)
 
 // Create a DOM based ranger for use by a DOM walk pass.
 
-dom_ranger::dom_ranger () : m_global (), m_out ()
+dom_ranger::dom_ranger () : m_global ()
 {
   m_freelist.create (0);
   m_freelist.truncate (0);
@@ -1156,7 +1156,7 @@ dom_ranger::maybe_push_edge (edge e, bool edge_0)
     e_cache = m_freelist.pop ();
   else
     e_cache = new ssa_lazy_cache;
-  gori_on_edge (*e_cache, e, this, &m_out);
+  gori_on_edge (*e_cache, e, this, &gori ());
   if (e_cache->empty_p ())
     m_freelist.safe_push (e_cache);
   else
diff --git a/gcc/gimple-range.h b/gcc/gimple-range.h
index 1532951a449..180090bed15 100644
--- a/gcc/gimple-range.h
+++ b/gcc/gimple-range.h
@@ -121,7 +121,6 @@ protected:
   DISABLE_COPY_AND_ASSIGN (dom_ranger);
   void maybe_push_edge (edge e, bool edge_0);
   ssa_cache m_global;
-  gimple_outgoing_range m_out;
   vec<ssa_lazy_cache *> m_freelist;
   vec<ssa_lazy_cache *> m_e0;
   vec<ssa_lazy_cache *> m_e1;
diff --git a/gcc/tree-vrp.cc b/gcc/tree-vrp.cc
index 7d7f9fe2932..1c7b451d8fb 100644
--- a/gcc/tree-vrp.cc
+++ b/gcc/tree-vrp.cc
@@ -85,14 +85,15 @@ along with GCC; see the file COPYING3.  If not see
 
 class remove_unreachable {
 public:
-  remove_unreachable (gimple_ranger &r, bool all) : m_ranger (r), final_p (all)
+  remove_unreachable (range_query &r, bool all) : m_ranger (r), final_p (all)
     { m_list.create (30); }
   ~remove_unreachable () { m_list.release (); }
   void handle_early (gimple *s, edge e);
   void maybe_register (gimple *s);
+  bool remove ();
   bool remove_and_update_globals ();
   vec<std::pair<int, int> > m_list;
-  gimple_ranger &m_ranger;
+  range_query &m_ranger;
   bool final_p;
 };
 
@@ -195,6 +196,9 @@ fully_replaceable (tree name, basic_block bb)
 void
 remove_unreachable::handle_early (gimple *s, edge e)
 {
+  // If there is no gori_ssa, there is no early processsing.
+  if (!m_ranger.gori_ssa ())
+    return ;
   bool lhs_p = TREE_CODE (gimple_cond_lhs (s)) == SSA_NAME;
   bool rhs_p = TREE_CODE (gimple_cond_rhs (s)) == SSA_NAME;
   // Do not remove __builtin_unreachable if it confers a relation, or
@@ -253,6 +257,41 @@ remove_unreachable::handle_early (gimple *s, edge e)
     }
 }
 
+// Process the edges in the list, change the conditions and removing any
+// dead code feeding those conditions.   This removes the unreachables, but
+// makes no attempt to set globals values.
+
+bool
+remove_unreachable::remove ()
+{
+  if (!final_p || m_list.length () == 0)
+    return false;
+
+  bool change = false;
+  unsigned i;
+  for (i = 0; i < m_list.length (); i++)
+    {
+      auto eb = m_list[i];
+      basic_block src = BASIC_BLOCK_FOR_FN (cfun, eb.first);
+      basic_block dest = BASIC_BLOCK_FOR_FN (cfun, eb.second);
+      if (!src || !dest)
+	continue;
+      edge e = find_edge (src, dest);
+      gimple *s = gimple_outgoing_range_stmt_p (e->src);
+      gcc_checking_assert (gimple_code (s) == GIMPLE_COND);
+
+      change = true;
+      // Rewrite the condition.
+      if (e->flags & EDGE_TRUE_VALUE)
+	gimple_cond_make_true (as_a<gcond *> (s));
+      else
+	gimple_cond_make_false (as_a<gcond *> (s));
+      update_stmt (s);
+    }
+
+  return change;
+}
+
 
 // Process the edges in the list, change the conditions and removing any
 // dead code feeding those conditions.  Calculate the range of any
@@ -266,6 +305,10 @@ remove_unreachable::remove_and_update_globals ()
   if (m_list.length () == 0)
     return false;
 
+  // If there is no import/export info, just remove unreachables if necessary.
+  if (!m_ranger.gori_ssa ())
+    return remove ();
+
   // Ensure the cache in SCEV has been cleared before processing
   // globals to be removed.
   scev_reset ();
-- 
2.41.0

