Instead of defaulting to an initial value of VARYING before resolving cycles, try folding the statement using available global values instead.  THis can give us a much better initial approximation, especially in cases where there are no dependencies, ie
   f_45 = 77

This implements suggestion 2) in comment 22 of the PR:

   2) The initial value we choose is simply VARYING. This is why 1)
   alone won't solve this problem.  when we push _1947 on the stack, we
   set it to VARYING..  then proceed down along chain of other
   dependencies Driven by _1011 which are resolved first. When we get
   back to _1947 finally, we see:
      _1947 = 77;
   which evaluated to [77, 77], and is this different than VARYING, and
   thus would cause a new timestamp to be created even if (1) were
   implemented.

   TODO: When setting the initial value in the cache, rather than being
   lazy and using varying, we should invoke fold_stmt using
   get_global_range_query ().   This will fold the stmt and produce a
   result which resolved any ssa-names just using known global values.
   THis should not be expensive, and gives us a reasonable first
   approximation.  And for cases like _1947, the final result as well.

I stop doing this after inlining because there are some statements which change their evaluation (ie, BUILTIN_IN_CONSTANT)  which causes headaches... and then we  just default to VARYING again or anything which doesn't have a  global SSA range set..

There is a 2.7% hit to VRP to evaluate each statement this additional time, but only 0.09% to overall compile time. Besides, we get it back later in the patch set.. :-)

Bootstraps on x86_64-pc-linux-gnu with no regressions.   Pushed.

Andrew




From 3a20e1a33277bcb16d681b4f3633fcf8cce5a852 Mon Sep 17 00:00:00 2001
From: Andrew MacLeod <amacl...@redhat.com>
Date: Tue, 23 May 2023 15:11:44 -0400
Subject: [PATCH 1/3] Choose better initial values for ranger.

Instead of defaulting to VARYING, fold the stmt using just global ranges.

	PR tree-optimization/109695
	* gimple-range-cache.cc (ranger_cache::get_global_range): Call
	fold_range with global query to choose an initial value.
---
 gcc/gimple-range-cache.cc | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/gcc/gimple-range-cache.cc b/gcc/gimple-range-cache.cc
index 07c69ef858a..8ddfd9426c0 100644
--- a/gcc/gimple-range-cache.cc
+++ b/gcc/gimple-range-cache.cc
@@ -951,7 +951,22 @@ ranger_cache::get_global_range (vrange &r, tree name, bool &current_p)
 		|| m_temporal->current_p (name, m_gori.depend1 (name),
 					  m_gori.depend2 (name));
   else
-    m_globals.set_range (name, r);
+    {
+      // If no global value has been set and value is VARYING, fold the stmt
+      // using just global ranges to get a better initial value.
+      // After inlining we tend to decide some things are constant, so
+      // so not do this evaluation after inlining.
+      if (r.varying_p () && !cfun->after_inlining)
+	{
+	  gimple *s = SSA_NAME_DEF_STMT (name);
+	  if (gimple_get_lhs (s) == name)
+	    {
+	      if (!fold_range (r, s, get_global_range_query ()))
+		gimple_range_global (r, name);
+	    }
+	}
+      m_globals.set_range (name, r);
+    }
 
   // If the existing value was not current, mark it as always current.
   if (!current_p)
-- 
2.40.1

Reply via email to