Skip to content

Commit 0226a2c

Browse files
nikicSterling-Augustine
authored andcommitted
[CmpInstAnalysis] Further canonicalize the predicate (NFC)
Canonicalize le to lt predicate by adjusting the constant by one. As such, we only have to handle slt and ult. This will make it easier to handle additional cases.
1 parent 4d45bfe commit 0226a2c

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

llvm/lib/Analysis/CmpInstAnalysis.cpp

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ llvm::decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate Pred,
7878
bool LookThruTrunc) {
7979
using namespace PatternMatch;
8080

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

8585
bool Inverted = false;
@@ -88,36 +88,30 @@ llvm::decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate Pred,
8888
Pred = ICmpInst::getInversePredicate(Pred);
8989
}
9090

91+
APInt C = *OrigC;
92+
if (ICmpInst::isLE(Pred)) {
93+
if (ICmpInst::isSigned(Pred) ? C.isMaxSignedValue() : C.isMaxValue())
94+
return std::nullopt;
95+
++C;
96+
Pred = ICmpInst::getStrictPredicate(Pred);
97+
}
98+
9199
DecomposedBitTest Result;
92100
switch (Pred) {
93101
default:
94102
llvm_unreachable("Unexpected predicate");
95103
case ICmpInst::ICMP_SLT:
96104
// X < 0 is equivalent to (X & SignMask) != 0.
97-
if (!C->isZero())
105+
if (!C.isZero())
98106
return std::nullopt;
99-
Result.Mask = APInt::getSignMask(C->getBitWidth());
100-
Result.Pred = ICmpInst::ICMP_NE;
101-
break;
102-
case ICmpInst::ICMP_SLE:
103-
// X <= -1 is equivalent to (X & SignMask) != 0.
104-
if (!C->isAllOnes())
105-
return std::nullopt;
106-
Result.Mask = APInt::getSignMask(C->getBitWidth());
107+
Result.Mask = APInt::getSignMask(C.getBitWidth());
107108
Result.Pred = ICmpInst::ICMP_NE;
108109
break;
109110
case ICmpInst::ICMP_ULT:
110111
// X <u 2^n is equivalent to (X & ~(2^n-1)) == 0.
111-
if (!C->isPowerOf2())
112-
return std::nullopt;
113-
Result.Mask = -*C;
114-
Result.Pred = ICmpInst::ICMP_EQ;
115-
break;
116-
case ICmpInst::ICMP_ULE:
117-
// X <=u 2^n-1 is equivalent to (X & ~(2^n-1)) == 0.
118-
if (!(*C + 1).isPowerOf2())
112+
if (!C.isPowerOf2())
119113
return std::nullopt;
120-
Result.Mask = ~*C;
114+
Result.Mask = -C;
121115
Result.Pred = ICmpInst::ICMP_EQ;
122116
break;
123117
}

0 commit comments

Comments
 (0)