[COMMITTED] PR tree-optimization/101496 - Only call vrp_visit_cond_stmt if range_of_stmt doesn't resolve to a const.

Message ID f33b52fe-612b-ecbe-20d7-e4557279e7a7@redhat.com
State New
Headers show
Series
  • [COMMITTED] PR tree-optimization/101496 - Only call vrp_visit_cond_stmt if range_of_stmt doesn't resolve to a const.
Related show

Commit Message

Martin Sebor via Gcc-patches July 22, 2021, 12:53 p.m.
Resolve the issue of nonsensical conditions resolving differently than 
vrp_visit_cond_stmt expects  by resolving range_of_stmt first.

this resolves an issue where ranger was using equality between two 
values to choose a branch when the ranges specified a different branch.  
Its in unreachable code, which is why the condition is both true and 
false at the same time.

Ultimately we'll simply remove the call to vrp_visit_cond_stmt.

Bootstrapped on x86_64 & powerpc64-unknown-linux-gnu with no 
regressions.  Pushed.

Andrew

Patch

From e58093276a6e319c2a6d9f02e343fbf8400dab60 Mon Sep 17 00:00:00 2001
From: Andrew MacLeod <amacleod@redhat.com>
Date: Mon, 19 Jul 2021 14:02:57 -0400
Subject: [PATCH 1/3] Only call vrp_visit_cond_stmt if range_of_stmt doesn't
 resolve to a const.

Eevntually all functionality will be subsumed.  Until then, call it only
if needed.

	gcc/
	PR tree-optimization/101496
	* vr-values.c (simplify_using_ranges::fold_cond): Call range_of_stmt
	first, then vrp_visit_cond_Stmt.

	gcc/testsuite
	* gcc.dg/pr101496.c: New.
---
 gcc/testsuite/gcc.dg/pr101496.c | 22 ++++++++++++++++++++++
 gcc/vr-values.c                 | 30 +++++++++++++++++-------------
 2 files changed, 39 insertions(+), 13 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/pr101496.c

diff --git a/gcc/testsuite/gcc.dg/pr101496.c b/gcc/testsuite/gcc.dg/pr101496.c
new file mode 100644
index 00000000000..091d4ad8f26
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr101496.c
@@ -0,0 +1,22 @@ 
+/* PR tree-optimization/101496 */
+/* { dg-do compile } */
+/* { dg-options "-O2 " } */
+
+int c_1, li_2, us_3, func_14_s_5;
+
+void func_14() {
+  {
+    unsigned uli_8 = 0;
+  lbl1806324B:
+    if (uli_8 /= us_3 |= func_14_s_5 < 0 | func_14_s_5 != c_1) {
+      uli_8 += c_1 >= us_3;
+      if (uli_8)
+        ;
+      else
+        li_2 &&func_14_s_5 <= c_1 ?: 0;
+      unsigned *ptr_9 = &uli_8;
+    }
+  }
+  goto lbl1806324B;
+}
+
diff --git a/gcc/vr-values.c b/gcc/vr-values.c
index 1b3ec38d288..c999ca80f03 100644
--- a/gcc/vr-values.c
+++ b/gcc/vr-values.c
@@ -3460,11 +3460,6 @@  range_fits_type_p (const value_range *vr,
 bool
 simplify_using_ranges::fold_cond (gcond *cond)
 {
-  /* ?? vrp_folder::fold_predicate_in() is a superset of this.  At
-     some point we should merge all variants of this code.  */
-  edge taken_edge;
-  vrp_visit_cond_stmt (cond, &taken_edge);
-
   int_range_max r;
   if (query->range_of_stmt (r, cond) && r.singleton_p ())
     {
@@ -3475,17 +3470,13 @@  simplify_using_ranges::fold_cond (gcond *cond)
 
       if (r.zero_p ())
 	{
-	  gcc_checking_assert (!taken_edge
-			       || taken_edge->flags & EDGE_FALSE_VALUE);
-	  if (dump_file && (dump_flags & TDF_DETAILS) && !taken_edge)
+	  if (dump_file && (dump_flags & TDF_DETAILS))
 	    fprintf (dump_file, "\nPredicate evaluates to: 0\n");
 	  gimple_cond_make_false (cond);
 	}
       else
 	{
-	  gcc_checking_assert (!taken_edge
-			       || taken_edge->flags & EDGE_TRUE_VALUE);
-	  if (dump_file && (dump_flags & TDF_DETAILS) && !taken_edge)
+	  if (dump_file && (dump_flags & TDF_DETAILS))
 	    fprintf (dump_file, "\nPredicate evaluates to: 1\n");
 	  gimple_cond_make_true (cond);
 	}
@@ -3493,12 +3484,25 @@  simplify_using_ranges::fold_cond (gcond *cond)
       return true;
     }
 
+  /* ?? vrp_folder::fold_predicate_in() is a superset of this.  At
+     some point we should merge all variants of this code.  */
+  edge taken_edge;
+  vrp_visit_cond_stmt (cond, &taken_edge);
+
   if (taken_edge)
     {
       if (taken_edge->flags & EDGE_TRUE_VALUE)
-       gimple_cond_make_true (cond);
+	{
+	  if (dump_file && (dump_flags & TDF_DETAILS))
+	    fprintf (dump_file, "\nVRP Predicate evaluates to: 1\n");
+	  gimple_cond_make_true (cond);
+	}
       else if (taken_edge->flags & EDGE_FALSE_VALUE)
-       gimple_cond_make_false (cond);
+	{
+	  if (dump_file && (dump_flags & TDF_DETAILS))
+	    fprintf (dump_file, "\nVRP Predicate evaluates to: 0\n");
+	  gimple_cond_make_false (cond);
+	}
       else
        gcc_unreachable ();
       update_stmt (cond);
-- 
2.17.2