Skip to content

Commit 5a2a51b

Browse files
committed
[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.
1 parent 68c04b0 commit 5a2a51b

File tree

1 file changed

+11
-30
lines changed

1 file changed

+11
-30
lines changed

llvm/lib/Analysis/CmpInstAnalysis.cpp

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,19 @@ llvm::decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate Pred,
7979
using namespace PatternMatch;
8080

8181
const APInt *C;
82-
if (!match(RHS, m_APIntAllowPoison(C)))
82+
if (!ICmpInst::isRelational(Pred) || !match(RHS, m_APIntAllowPoison(C)))
8383
return std::nullopt;
8484

85+
bool Inverted = false;
86+
if (ICmpInst::isGT(Pred) || ICmpInst::isGE(Pred)) {
87+
Inverted = true;
88+
Pred = ICmpInst::getInversePredicate(Pred);
89+
}
90+
8591
DecomposedBitTest Result;
8692
switch (Pred) {
8793
default:
88-
return std::nullopt;
94+
llvm_unreachable("Unexpected predicate");
8995
case ICmpInst::ICMP_SLT:
9096
// X < 0 is equivalent to (X & SignMask) != 0.
9197
if (!C->isZero())
@@ -100,20 +106,6 @@ llvm::decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate Pred,
100106
Result.Mask = APInt::getSignMask(C->getBitWidth());
101107
Result.Pred = ICmpInst::ICMP_NE;
102108
break;
103-
case ICmpInst::ICMP_SGT:
104-
// X > -1 is equivalent to (X & SignMask) == 0.
105-
if (!C->isAllOnes())
106-
return std::nullopt;
107-
Result.Mask = APInt::getSignMask(C->getBitWidth());
108-
Result.Pred = ICmpInst::ICMP_EQ;
109-
break;
110-
case ICmpInst::ICMP_SGE:
111-
// X >= 0 is equivalent to (X & SignMask) == 0.
112-
if (!C->isZero())
113-
return std::nullopt;
114-
Result.Mask = APInt::getSignMask(C->getBitWidth());
115-
Result.Pred = ICmpInst::ICMP_EQ;
116-
break;
117109
case ICmpInst::ICMP_ULT:
118110
// X <u 2^n is equivalent to (X & ~(2^n-1)) == 0.
119111
if (!C->isPowerOf2())
@@ -128,22 +120,11 @@ llvm::decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate Pred,
128120
Result.Mask = ~*C;
129121
Result.Pred = ICmpInst::ICMP_EQ;
130122
break;
131-
case ICmpInst::ICMP_UGT:
132-
// X >u 2^n-1 is equivalent to (X & ~(2^n-1)) != 0.
133-
if (!(*C + 1).isPowerOf2())
134-
return std::nullopt;
135-
Result.Mask = ~*C;
136-
Result.Pred = ICmpInst::ICMP_NE;
137-
break;
138-
case ICmpInst::ICMP_UGE:
139-
// X >=u 2^n is equivalent to (X & ~(2^n-1)) != 0.
140-
if (!C->isPowerOf2())
141-
return std::nullopt;
142-
Result.Mask = -*C;
143-
Result.Pred = ICmpInst::ICMP_NE;
144-
break;
145123
}
146124

125+
if (Inverted)
126+
Result.Pred = ICmpInst::getInversePredicate(Result.Pred);
127+
147128
Value *X;
148129
if (LookThruTrunc && match(LHS, m_Trunc(m_Value(X)))) {
149130
Result.X = X;

0 commit comments

Comments
 (0)