[GCC13] PR tree-optimization/105834 - Choose better initial values for ranger.

Message ID bb7ee1e7-ffae-4aeb-9bc3-d2483d1c8394@redhat.com
State Unresolved
Headers
Series [GCC13] PR tree-optimization/105834 - Choose better initial values for ranger. |

Checks

Context Check Description
snail/gcc-patch-check warning Git am fail log

Commit Message

Andrew MacLeod Nov. 6, 2023, 6:15 p.m. UTC
  As requested porting this patch from trunk resolves this PR in GCC 13.

Bootstraps on x86_64-pc-linux-gnu with no regressions.  OK for the gcc 
13 branch?

Andrew
  

Patch

From 0182a25607fa353274c27ec57ca497c00f1d1b76 Mon Sep 17 00:00:00 2001
From: Andrew MacLeod <amacleod@redhat.com>
Date: Mon, 6 Nov 2023 11:33:32 -0500
Subject: [PATCH] Choose better initial values for ranger.

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

	PR tree-optimization/105834
	gcc/
	* gimple-range-cache.cc (ranger_cache::get_global_range): Call
	fold_range with global query to choose an initial value.

	gcc/testsuite/
	* gcc.dg/pr105834.c
---
 gcc/gimple-range-cache.cc       | 17 ++++++++++++++++-
 gcc/testsuite/gcc.dg/pr105834.c | 17 +++++++++++++++++
 2 files changed, 33 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gcc.dg/pr105834.c

diff --git a/gcc/gimple-range-cache.cc b/gcc/gimple-range-cache.cc
index e4e75943632..b09df6c81bf 100644
--- a/gcc/gimple-range-cache.cc
+++ b/gcc/gimple-range-cache.cc
@@ -846,7 +846,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_global_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
+      // do 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_global_range (name, r);
+    }
 
   // If the existing value was not current, mark it as always current.
   if (!current_p)
diff --git a/gcc/testsuite/gcc.dg/pr105834.c b/gcc/testsuite/gcc.dg/pr105834.c
new file mode 100644
index 00000000000..d0eda03ef8b
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr105834.c
@@ -0,0 +1,17 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+static int a, b;
+
+void foo();
+
+int main() {
+    for (int c = 0; c < 2; c = c + (unsigned)3)
+        if (a)
+            for (;;)
+                if (c > 0)
+                    b = 0;
+    if (b)
+        foo();
+}
+/* { dg-final { scan-tree-dump-not "foo" "optimized" } }  */
-- 
2.41.0