[llvm] [ValueTracking][NFC] move isKnownInversion to ValueTracking (PR #95321)

Zain Jaffal via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 12 23:13:40 PDT 2024


https://github.com/zjaffal updated https://github.com/llvm/llvm-project/pull/95321

>From a074981af986e9ef7b1c9767853792bb7b3c4b9f Mon Sep 17 00:00:00 2001
From: Zain Jaffal <zain at jjaffal.com>
Date: Wed, 12 Jun 2024 23:25:54 +0100
Subject: [PATCH 1/2] [ValueTracking][NFC] move isKnownInversion to
 ValueTracking

---
 llvm/include/llvm/Analysis/ValueTracking.h    |  7 +++++
 llvm/lib/Analysis/ValueTracking.cpp           | 22 +++++++++++++++
 .../InstCombine/InstCombineSelect.cpp         | 27 -------------------
 3 files changed, 29 insertions(+), 27 deletions(-)

diff --git a/llvm/include/llvm/Analysis/ValueTracking.h b/llvm/include/llvm/Analysis/ValueTracking.h
index 0584b7e29f67b..8f497e3f5db99 100644
--- a/llvm/include/llvm/Analysis/ValueTracking.h
+++ b/llvm/include/llvm/Analysis/ValueTracking.h
@@ -136,6 +136,13 @@ bool isKnownNonZero(const Value *V, const SimplifyQuery &Q, unsigned Depth = 0);
 bool isKnownNegation(const Value *X, const Value *Y, bool NeedNSW = false,
                      bool AllowPoison = true);
 
+/// Return true iff:
+/// 1. X is poison implies Y is poison.
+/// 2. X is true implies Y is false.
+/// 3. X is false implies Y is true.
+/// Otherwise, return false.
+bool isKnownInversion(Value *X, Value *Y);
+
 /// Returns true if the give value is known to be non-negative.
 bool isKnownNonNegative(const Value *V, const SimplifyQuery &SQ,
                         unsigned Depth = 0);
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 6a806d596efe7..5123287a8c951 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -8176,6 +8176,28 @@ bool llvm::isKnownNegation(const Value *X, const Value *Y, bool NeedNSW,
                        match(Y, m_NSWSub(m_Specific(B), m_Specific(A)))));
 }
 
+bool llvm::isKnownInversion(Value *X, Value *Y) {
+  // Handle X = icmp pred A, B, Y = icmp pred A, C.
+  Value *A, *B, *C;
+  ICmpInst::Predicate Pred1, Pred2;
+  if (!match(X, m_ICmp(Pred1, m_Value(A), m_Value(B))) ||
+      !match(Y, m_c_ICmp(Pred2, m_Specific(A), m_Value(C))))
+    return false;
+
+  if (B == C)
+    return Pred1 == ICmpInst::getInversePredicate(Pred2);
+
+  // Try to infer the relationship from constant ranges.
+  const APInt *RHSC1, *RHSC2;
+  if (!match(B, m_APInt(RHSC1)) || !match(C, m_APInt(RHSC2)))
+    return false;
+
+  const auto CR1 = ConstantRange::makeExactICmpRegion(Pred1, *RHSC1);
+  const auto CR2 = ConstantRange::makeExactICmpRegion(Pred2, *RHSC2);
+
+  return CR1.inverse() == CR2;
+}
+
 static SelectPatternResult matchSelectPattern(CmpInst::Predicate Pred,
                                               FastMathFlags FMF,
                                               Value *CmpLHS, Value *CmpRHS,
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
index 588607b3fea2f..7d26807544d7e 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -3520,33 +3520,6 @@ static bool matchFMulByZeroIfResultEqZero(InstCombinerImpl &IC, Value *Cmp0,
   return false;
 }
 
-/// Return true iff:
-/// 1. X is poison implies Y is poison.
-/// 2. X is true implies Y is false.
-/// 3. X is false implies Y is true.
-/// Otherwise, return false.
-static bool isKnownInversion(Value *X, Value *Y) {
-  // Handle X = icmp pred A, B, Y = icmp pred A, C.
-  Value *A, *B, *C;
-  ICmpInst::Predicate Pred1, Pred2;
-  if (!match(X, m_ICmp(Pred1, m_Value(A), m_Value(B))) ||
-      !match(Y, m_c_ICmp(Pred2, m_Specific(A), m_Value(C))))
-    return false;
-
-  if (B == C)
-    return Pred1 == ICmpInst::getInversePredicate(Pred2);
-
-  // Try to infer the relationship from constant ranges.
-  const APInt *RHSC1, *RHSC2;
-  if (!match(B, m_APInt(RHSC1)) || !match(C, m_APInt(RHSC2)))
-    return false;
-
-  const auto CR1 = ConstantRange::makeExactICmpRegion(Pred1, *RHSC1);
-  const auto CR2 = ConstantRange::makeExactICmpRegion(Pred2, *RHSC2);
-
-  return CR1.inverse() == CR2;
-}
-
 Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
   Value *CondVal = SI.getCondition();
   Value *TrueVal = SI.getTrueValue();

>From a90d6b33e440de4446b89ddb201159a739258ccc Mon Sep 17 00:00:00 2001
From: Zain Jaffal <zain at jjaffal.com>
Date: Thu, 13 Jun 2024 07:13:24 +0100
Subject: [PATCH 2/2] change function parameters to use const

---
 llvm/include/llvm/Analysis/ValueTracking.h | 2 +-
 llvm/lib/Analysis/ValueTracking.cpp        | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/include/llvm/Analysis/ValueTracking.h b/llvm/include/llvm/Analysis/ValueTracking.h
index 8f497e3f5db99..e577d0cc7ad41 100644
--- a/llvm/include/llvm/Analysis/ValueTracking.h
+++ b/llvm/include/llvm/Analysis/ValueTracking.h
@@ -141,7 +141,7 @@ bool isKnownNegation(const Value *X, const Value *Y, bool NeedNSW = false,
 /// 2. X is true implies Y is false.
 /// 3. X is false implies Y is true.
 /// Otherwise, return false.
-bool isKnownInversion(Value *X, Value *Y);
+bool isKnownInversion(const Value *X, const Value *Y);
 
 /// Returns true if the give value is known to be non-negative.
 bool isKnownNonNegative(const Value *V, const SimplifyQuery &SQ,
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 5123287a8c951..8126d2a1acc27 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -8176,7 +8176,7 @@ bool llvm::isKnownNegation(const Value *X, const Value *Y, bool NeedNSW,
                        match(Y, m_NSWSub(m_Specific(B), m_Specific(A)))));
 }
 
-bool llvm::isKnownInversion(Value *X, Value *Y) {
+bool llvm::isKnownInversion(const Value *X, const Value *Y) {
   // Handle X = icmp pred A, B, Y = icmp pred A, C.
   Value *A, *B, *C;
   ICmpInst::Predicate Pred1, Pred2;



More information about the llvm-commits mailing list