[llvm] 5a2a51b - [CmpInstAnalysis] Canonicalize to lt predicate (NFC)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Wed Oct 2 03:03:03 PDT 2024
Author: Nikita Popov
Date: 2024-10-02T12:02:55+02:00
New Revision: 5a2a51bae4fb39d130ec671fecbe50e0adf3e330
URL: https://github.com/llvm/llvm-project/commit/5a2a51bae4fb39d130ec671fecbe50e0adf3e330
DIFF: https://github.com/llvm/llvm-project/commit/5a2a51bae4fb39d130ec671fecbe50e0adf3e330.diff
LOG: [CmpInstAnalysis] Canonicalize to lt predicate (NFC)
Reduce the number of cases we have to handle by a factor of two
by inverting the predicate such that we get an lt/le rather than
gt/ge predicate.
Added:
Modified:
llvm/lib/Analysis/CmpInstAnalysis.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Analysis/CmpInstAnalysis.cpp b/llvm/lib/Analysis/CmpInstAnalysis.cpp
index 36d7aa510545af..1d0a5e329a1407 100644
--- a/llvm/lib/Analysis/CmpInstAnalysis.cpp
+++ b/llvm/lib/Analysis/CmpInstAnalysis.cpp
@@ -79,13 +79,19 @@ llvm::decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate Pred,
using namespace PatternMatch;
const APInt *C;
- if (!match(RHS, m_APIntAllowPoison(C)))
+ if (!ICmpInst::isRelational(Pred) || !match(RHS, m_APIntAllowPoison(C)))
return std::nullopt;
+ bool Inverted = false;
+ if (ICmpInst::isGT(Pred) || ICmpInst::isGE(Pred)) {
+ Inverted = true;
+ Pred = ICmpInst::getInversePredicate(Pred);
+ }
+
DecomposedBitTest Result;
switch (Pred) {
default:
- return std::nullopt;
+ llvm_unreachable("Unexpected predicate");
case ICmpInst::ICMP_SLT:
// X < 0 is equivalent to (X & SignMask) != 0.
if (!C->isZero())
@@ -100,20 +106,6 @@ llvm::decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate Pred,
Result.Mask = APInt::getSignMask(C->getBitWidth());
Result.Pred = ICmpInst::ICMP_NE;
break;
- case ICmpInst::ICMP_SGT:
- // X > -1 is equivalent to (X & SignMask) == 0.
- if (!C->isAllOnes())
- return std::nullopt;
- Result.Mask = APInt::getSignMask(C->getBitWidth());
- Result.Pred = ICmpInst::ICMP_EQ;
- break;
- case ICmpInst::ICMP_SGE:
- // X >= 0 is equivalent to (X & SignMask) == 0.
- if (!C->isZero())
- return std::nullopt;
- Result.Mask = APInt::getSignMask(C->getBitWidth());
- Result.Pred = ICmpInst::ICMP_EQ;
- break;
case ICmpInst::ICMP_ULT:
// X <u 2^n is equivalent to (X & ~(2^n-1)) == 0.
if (!C->isPowerOf2())
@@ -128,22 +120,11 @@ llvm::decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate Pred,
Result.Mask = ~*C;
Result.Pred = ICmpInst::ICMP_EQ;
break;
- case ICmpInst::ICMP_UGT:
- // X >u 2^n-1 is equivalent to (X & ~(2^n-1)) != 0.
- if (!(*C + 1).isPowerOf2())
- return std::nullopt;
- Result.Mask = ~*C;
- Result.Pred = ICmpInst::ICMP_NE;
- break;
- case ICmpInst::ICMP_UGE:
- // X >=u 2^n is equivalent to (X & ~(2^n-1)) != 0.
- if (!C->isPowerOf2())
- return std::nullopt;
- Result.Mask = -*C;
- Result.Pred = ICmpInst::ICMP_NE;
- break;
}
+ if (Inverted)
+ Result.Pred = ICmpInst::getInversePredicate(Result.Pred);
+
Value *X;
if (LookThruTrunc && match(LHS, m_Trunc(m_Value(X)))) {
Result.X = X;
More information about the llvm-commits
mailing list