An ongoing issue  is the the order we evaluate things in can affect decisions along the way. As ranger isn't a fully iterative pass, we can sometimes come up with different results if back edges are processed in different orders.

One of the ways this can happen is when the cache is propagating on-entry values for an SSA_NAME. It calculates outgoing edge values and the gori-compute engine can flag ssa-names that were involved in a range calculation that have not yet been initialized.  When the propagation for the original name is done, it goes back and examines the "poor values" and tries to quickly calculate a better range, and if it comes up with one, immediately tries to go back  and update the location/range gori_compute flagged.   This produces better ranges earlier.

However, when we do this in different orders, we can get different results.  We were processing the uses on is_gimple_debug statements just like normal uses, and this would sometimes cause a difference in how things were resolved.

This patch adds a flag to enable/disable this attempt to look up new values, and when range_of_expr is processing the use on a debug statement, turns it off for the query.  This means the query will never cause a new lookup, and this should resolve all the -fcompare-debug issues.

Bootstrapped on x86_64-pc-linux-gnu, with no new regressions. Pushed.

Andrew

>From 715914d3f9e4e40af58d22103c7650cdd720ef92 Mon Sep 17 00:00:00 2001
From: Andrew MacLeod <amacl...@redhat.com>
Date: Mon, 31 May 2021 12:13:50 -0400
Subject: [PATCH 4/4] Do not calculate new values when evaluating a debug
 statement.

Add a flag to enable/disable immediately improving poor values found during
cache propagation. Then disable it when processing debug statements.

	gcc/
	PR tree-optimization/100781
	* gimple-range-cache.cc (ranger_cache::ranger_cache): Enable new
	value calculation by default.
	(ranger_cache::enable_new_values): New.
	(ranger_cache::disable_new_values): New.
	(ranger_cache::push_poor_value): Check if new values are allowed.
	* gimple-range-cache.h (class ranger_cache): New member/methods.
	* gimple-range.cc (gimple_ranger::range_of_expr): Check for debug
	statement, and disable/renable new value calculation.

	gcc/testsuite/
	PR tree-optimization/100781
	* gcc.dg/pr100781.c: New.
---
 gcc/gimple-range-cache.cc       | 20 ++++++++++++++++++++
 gcc/gimple-range-cache.h        |  3 +++
 gcc/gimple-range.cc             |  9 +++++++++
 gcc/testsuite/gcc.dg/pr100781.c | 25 +++++++++++++++++++++++++
 4 files changed, 57 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/pr100781.c

diff --git a/gcc/gimple-range-cache.cc b/gcc/gimple-range-cache.cc
index dc32841310a..cc27574b7b4 100644
--- a/gcc/gimple-range-cache.cc
+++ b/gcc/gimple-range-cache.cc
@@ -586,6 +586,7 @@ ranger_cache::ranger_cache (gimple_ranger &q) : query (q)
       if (bb)
 	m_gori.exports (bb);
     }
+  enable_new_values ();
 }
 
 ranger_cache::~ranger_cache ()
@@ -606,6 +607,23 @@ ranger_cache::dump (FILE *f)
   fprintf (f, "\n");
 }
 
+// Allow the cache to flag and query new values when propagation is forced
+// to use an unknown value.
+
+void
+ranger_cache::enable_new_values ()
+{
+  m_new_value_p = true;
+}
+
+// Disable new value querying.
+
+void
+ranger_cache::disable_new_values ()
+{
+  m_new_value_p = false;
+}
+
 // Dump the caches for basic block BB to file F.
 
 void
@@ -689,6 +707,8 @@ ranger_cache::set_global_range (tree name, const irange &r)
 bool
 ranger_cache::push_poor_value (basic_block bb, tree name)
 {
+  if (!m_new_value_p)
+    return false;
   if (m_poor_value_list.length ())
     {
       // Don't push anything else to the same block.  If there are multiple 
diff --git a/gcc/gimple-range-cache.h b/gcc/gimple-range-cache.h
index fee69bcc578..4af461d2aa3 100644
--- a/gcc/gimple-range-cache.h
+++ b/gcc/gimple-range-cache.h
@@ -100,6 +100,8 @@ public:
   bool get_non_stale_global_range (irange &r, tree name);
   void set_global_range (tree name, const irange &r);
 
+  void enable_new_values ();
+  void disable_new_values ();
   non_null_ref m_non_null;
   gori_compute m_gori;
 
@@ -131,6 +133,7 @@ private:
   bool push_poor_value (basic_block bb, tree name);
   vec<update_record> m_poor_value_list;
   class gimple_ranger &query;
+  bool m_new_value_p;
 };
 
 #endif // GCC_SSA_RANGE_CACHE_H
diff --git a/gcc/gimple-range.cc b/gcc/gimple-range.cc
index d58e151eb4e..ed0a0c9702b 100644
--- a/gcc/gimple-range.cc
+++ b/gcc/gimple-range.cc
@@ -971,6 +971,15 @@ gimple_ranger::range_of_expr (irange &r, tree expr, gimple *stmt)
       return true;
     }
 
+  // For a debug stmt, pick the best value currently available, do not
+  // trigger new value calculations.  PR 100781.
+  if (is_gimple_debug (stmt))
+    {
+      m_cache.disable_new_values ();
+      m_cache.range_of_expr (r, expr, stmt);
+      m_cache.enable_new_values ();
+      return true;
+    }
   basic_block bb = gimple_bb (stmt);
   gimple *def_stmt = SSA_NAME_DEF_STMT (expr);
 
diff --git a/gcc/testsuite/gcc.dg/pr100781.c b/gcc/testsuite/gcc.dg/pr100781.c
new file mode 100644
index 00000000000..c0e008a3ba5
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr100781.c
@@ -0,0 +1,25 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 --param=evrp-mode=ranger -fcompare-debug  " } */
+
+struct a {
+  int b;
+};
+long c(short d, long e, struct a f) {
+g:;
+  int h = f.b <= e, i = d, n = h >= d;
+  if (!n)
+    goto j;
+  goto k;
+j:;
+  long l = 5;
+  if (l)
+    goto m;
+  d = 0;
+m:
+  if (d)
+    return f.b;
+k:
+  goto g;
+}
+int main() { }
+
-- 
2.17.2

Reply via email to