[llvm] SCEV: cover a codepath in isImpliedCondBalancedTypes (PR #123070)

Ramkumar Ramachandra via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 15 07:38:37 PST 2025


https://github.com/artagnon created https://github.com/llvm/llvm-project/pull/123070

The code that checks a predicate against a swapped predicate in isImpliedCondBalancedTypes is not covered by any existing test, within any Analysis or Transform. Fix this by adding a test to SCEV.

-- 8< --
See both commits individually for proof.

>From 95621e6b1d6d82f5886e2c7418f2668961d82cc4 Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <ramkumar.ramachandra at codasip.com>
Date: Wed, 15 Jan 2025 15:13:22 +0000
Subject: [PATCH 1/2] SCEV: pre-commit test to test implied-swapped codepath

---
 llvm/lib/Analysis/ScalarEvolution.cpp         |  1 +
 .../ScalarEvolution/implied-via-division.ll   | 53 +++++++++++++++++++
 2 files changed, 54 insertions(+)

diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index 7673c354817579..ddf79f4cd3a84a 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -11872,6 +11872,7 @@ bool ScalarEvolution::isImpliedCondBalancedTypes(
   // FIXME: use CmpPredicate::getMatching here.
   if (ICmpInst::getSwappedCmpPredicate(FoundPred) ==
       static_cast<CmpInst::Predicate>(Pred)) {
+    return false;
     // We can write the implication
     // 0.  LHS Pred      RHS  <-   FoundLHS SwapPred  FoundRHS
     // using one of the following ways:
diff --git a/llvm/test/Analysis/ScalarEvolution/implied-via-division.ll b/llvm/test/Analysis/ScalarEvolution/implied-via-division.ll
index fbe69b4b188977..9e21ec2683af0c 100644
--- a/llvm/test/Analysis/ScalarEvolution/implied-via-division.ll
+++ b/llvm/test/Analysis/ScalarEvolution/implied-via-division.ll
@@ -411,3 +411,56 @@ header:
 exit:
   ret void
 }
+
+define void @swapped_predicate(i32 %n) nounwind {
+; Prove that (n >= 1) ===> (0 >= -n / 2).
+; CHECK-LABEL: 'swapped_predicate'
+; CHECK-NEXT:  Determining loop execution counts for: @swapped_predicate
+; CHECK-NEXT:  Loop %header: backedge-taken count is (-1 * (0 smin (-1 + (-1 * %n.div.2)<nsw>)<nsw>))<nsw>
+; CHECK-NEXT:  Loop %header: constant max backedge-taken count is i32 1073741824
+; CHECK-NEXT:  Loop %header: symbolic max backedge-taken count is (-1 * (0 smin (-1 + (-1 * %n.div.2)<nsw>)<nsw>))<nsw>
+; CHECK-NEXT:  Loop %header: Trip multiple is 1
+;
+entry:
+  %cmp1 = icmp sge i32 %n, 1
+  %n.div.2 = sdiv i32 %n, 2
+  call void(i1, ...) @llvm.experimental.guard(i1 %cmp1) [ "deopt"() ]
+  br label %header
+
+header:
+  %indvar = phi i32 [ %indvar.next, %header ], [ 0, %entry ]
+  %indvar.next = add i32 %indvar, 1
+  %minus.indvar = sub nsw i32 0, %indvar
+  %minus.n.div.2 = sub nsw i32 0, %n.div.2
+  %exitcond = icmp sge i32 %minus.indvar, %minus.n.div.2
+  br i1 %exitcond, label %header, label %exit
+
+exit:
+  ret void
+}
+
+define void @swapped_predicate_neg(i32 %n) nounwind {
+; Prove that (n >= 1) =\=> (-n / 2 >= 0).
+; CHECK-LABEL: 'swapped_predicate_neg'
+; CHECK-NEXT:  Determining loop execution counts for: @swapped_predicate_neg
+; CHECK-NEXT:  Loop %header: Unpredictable backedge-taken count.
+; CHECK-NEXT:  Loop %header: Unpredictable constant max backedge-taken count.
+; CHECK-NEXT:  Loop %header: Unpredictable symbolic max backedge-taken count.
+;
+entry:
+  %cmp1 = icmp sge i32 %n, 1
+  %n.div.2 = sdiv i32 %n, 2
+  call void(i1, ...) @llvm.experimental.guard(i1 %cmp1) [ "deopt"() ]
+  br label %header
+
+header:
+  %indvar = phi i32 [ %indvar.next, %header ], [ 0, %entry ]
+  %indvar.next = add i32 %indvar, 1
+  %minus.indvar = sub nsw i32 0, %indvar
+  %minus.n.div.2 = sub nsw i32 0, %n.div.2
+  %exitcond = icmp sge i32 %minus.n.div.2, %minus.indvar
+  br i1 %exitcond, label %header, label %exit
+
+exit:
+  ret void
+}

>From 9fa69eda63634a44a2096c342ea2b8b863b79185 Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <ramkumar.ramachandra at codasip.com>
Date: Wed, 15 Jan 2025 15:32:08 +0000
Subject: [PATCH 2/2] SCEV: cover a codepath in isImpliedCondBalancedTypes

The code that checks a predicate against a swapped predicate in
isImpliedCondBalancedTypes is not covered by any existing test, within
any Analysis or Transform. Fix this by adding a test to SCEV.
---
 llvm/lib/Analysis/ScalarEvolution.cpp                      | 1 -
 llvm/test/Analysis/ScalarEvolution/implied-via-division.ll | 4 ++--
 2 files changed, 2 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index ddf79f4cd3a84a..7673c354817579 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -11872,7 +11872,6 @@ bool ScalarEvolution::isImpliedCondBalancedTypes(
   // FIXME: use CmpPredicate::getMatching here.
   if (ICmpInst::getSwappedCmpPredicate(FoundPred) ==
       static_cast<CmpInst::Predicate>(Pred)) {
-    return false;
     // We can write the implication
     // 0.  LHS Pred      RHS  <-   FoundLHS SwapPred  FoundRHS
     // using one of the following ways:
diff --git a/llvm/test/Analysis/ScalarEvolution/implied-via-division.ll b/llvm/test/Analysis/ScalarEvolution/implied-via-division.ll
index 9e21ec2683af0c..3fd59299ed81c9 100644
--- a/llvm/test/Analysis/ScalarEvolution/implied-via-division.ll
+++ b/llvm/test/Analysis/ScalarEvolution/implied-via-division.ll
@@ -416,9 +416,9 @@ define void @swapped_predicate(i32 %n) nounwind {
 ; Prove that (n >= 1) ===> (0 >= -n / 2).
 ; CHECK-LABEL: 'swapped_predicate'
 ; CHECK-NEXT:  Determining loop execution counts for: @swapped_predicate
-; CHECK-NEXT:  Loop %header: backedge-taken count is (-1 * (0 smin (-1 + (-1 * %n.div.2)<nsw>)<nsw>))<nsw>
+; CHECK-NEXT:  Loop %header: backedge-taken count is (1 + %n.div.2)<nsw>
 ; CHECK-NEXT:  Loop %header: constant max backedge-taken count is i32 1073741824
-; CHECK-NEXT:  Loop %header: symbolic max backedge-taken count is (-1 * (0 smin (-1 + (-1 * %n.div.2)<nsw>)<nsw>))<nsw>
+; CHECK-NEXT:  Loop %header: symbolic max backedge-taken count is (1 + %n.div.2)<nsw>
 ; CHECK-NEXT:  Loop %header: Trip multiple is 1
 ;
 entry:



More information about the llvm-commits mailing list