[llvm] [SCEV] Use fast-check for equality implication (NFC) (PR #224289)

Ramkumar Ramachandra via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 17 06:08:26 PDT 2026


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

Introduce a isKnownNonEqual helper, and use it to speed up isImpliedCondBalancedTypes for equality predicates. This improves compile-time.

>From b96b99e5460ad4d5668206142f62475011e4cf3d Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <artagnon at tenstorrent.com>
Date: Thu, 17 Sep 2026 14:04:34 +0100
Subject: [PATCH] [SCEV] Use fast-check for equality implication (NFC)

Introduce a isKnownNonEqual helper, and use it to speed up
isImpliedCondBalancedTypes for equality predicates. This improves
compile-time.
---
 llvm/include/llvm/Analysis/ScalarEvolution.h |  3 +++
 llvm/lib/Analysis/ScalarEvolution.cpp        | 18 ++++++++++++++----
 2 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/llvm/include/llvm/Analysis/ScalarEvolution.h b/llvm/include/llvm/Analysis/ScalarEvolution.h
index 7fddd4ca4119f..22e7808a6f5a6 100644
--- a/llvm/include/llvm/Analysis/ScalarEvolution.h
+++ b/llvm/include/llvm/Analysis/ScalarEvolution.h
@@ -1258,6 +1258,9 @@ class ScalarEvolution {
   /// Test if the given expression is known to be non-zero.
   LLVM_ABI bool isKnownNonZero(const SCEV *S);
 
+  /// Test if the given expressions are known non to be equal.
+  LLVM_ABI bool isKnownNonEqual(const SCEV *LHS, const SCEV *RHS);
+
   /// Returns true if \p Op is guaranteed to not be poison.
   LLVM_ABI static bool isGuaranteedNotToBePoison(const SCEV *Op);
 
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index 30b5adfa266a2..56c95e54bdb8b 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -11269,6 +11269,13 @@ bool ScalarEvolution::isKnownNonZero(const SCEV *S) {
   return getUnsignedRangeMin(S) != 0;
 }
 
+bool ScalarEvolution::isKnownNonEqual(const SCEV *LHS, const SCEV *RHS) {
+  const SCEV *Minus = getMinusSCEV(LHS, LHS);
+  if (isa<SCEVCouldNotCompute>(Minus))
+    return false;
+  return isKnownNonZero(Minus);
+}
+
 bool ScalarEvolution::isKnownToBeAPowerOfTwo(const SCEV *S, bool OrZero,
                                              bool OrNegative) {
   auto NonRecursive = [OrNegative](const SCEV *S) {
@@ -12367,14 +12374,17 @@ bool ScalarEvolution::isImpliedCondBalancedTypes(
     }
   }
 
-  // Check whether the actual condition is beyond sufficient.
+  // x = y implies x <= y, x >= y, and x != y.
+  // x < y or x > y imply x != y.
   if (FoundPred == ICmpInst::ICMP_EQ)
     if (ICmpInst::isTrueWhenEqual(Pred))
-      if (isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS, CtxI))
+      if (FoundLHS == FoundRHS ||
+          isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS, CtxI))
         return true;
   if (Pred == ICmpInst::ICMP_NE)
-    if (!ICmpInst::isTrueWhenEqual(FoundPred))
-      if (isImpliedCondOperands(FoundPred, LHS, RHS, FoundLHS, FoundRHS, CtxI))
+    if (ICmpInst::isFalseWhenEqual(FoundPred))
+      if (isKnownNonEqual(LHS, RHS) ||
+          isImpliedCondOperands(FoundPred, LHS, RHS, FoundLHS, FoundRHS, CtxI))
         return true;
 
   if (isImpliedCondOperandsViaRanges(Pred, LHS, RHS, FoundPred, FoundLHS, FoundRHS))



More information about the llvm-commits mailing list