Skip to content

Commit 5a2b928

Browse files
committed
Review comments
Change-Id: I5355b103c34cb5d49868f44e3f8c10b95764a531
1 parent 740a37e commit 5a2b928

File tree

5 files changed

+35
-31
lines changed

5 files changed

+35
-31
lines changed

llvm/include/llvm/Analysis/CmpInstAnalysis.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,28 +95,28 @@ namespace llvm {
9595
/// Represents the operation icmp (X & Mask) pred C, where pred can only be
9696
/// eq or ne.
9797
struct DecomposedBitTest {
98-
Value *X = nullptr;
98+
Value *X;
9999
CmpInst::Predicate Pred;
100100
APInt Mask;
101101
APInt C;
102102
};
103103

104104
/// Decompose an icmp into the form ((X & Mask) pred C) if possible.
105105
/// Unless \p AllowNonZeroC is true, C will always be 0. If \p
106-
/// DecomposeBitMask is specified, then, for equality predicates, this will
106+
/// DecomposeAnd is specified, then, for equality predicates, this will
107107
/// decompose bitmasking (e.g. implemented via `and`).
108108
std::optional<DecomposedBitTest>
109109
decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate Pred,
110110
bool LookThroughTrunc = true, bool AllowNonZeroC = false,
111-
bool DecomposeBitMask = false);
111+
bool DecomposeAnd = false);
112112

113113
/// Decompose an icmp into the form ((X & Mask) pred C) if
114114
/// possible. Unless \p AllowNonZeroC is true, C will always be 0.
115-
/// If \p DecomposeBitMask is specified, then, for equality predicates, this
115+
/// If \p DecomposeAnd is specified, then, for equality predicates, this
116116
/// will decompose bitmasking (e.g. implemented via `and`).
117117
std::optional<DecomposedBitTest>
118118
decomposeBitTest(Value *Cond, bool LookThroughTrunc = true,
119-
bool AllowNonZeroC = false, bool DecomposeBitMask = false);
119+
bool AllowNonZeroC = false, bool DecomposeAnd = false);
120120

121121
} // end namespace llvm
122122

llvm/lib/Analysis/CmpInstAnalysis.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ Constant *llvm::getPredForFCmpCode(unsigned Code, Type *OpTy,
7676
std::optional<DecomposedBitTest>
7777
llvm::decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate Pred,
7878
bool LookThruTrunc, bool AllowNonZeroC,
79-
bool DecomposeBitMask) {
79+
bool DecomposeAnd) {
8080
using namespace PatternMatch;
8181

8282
const APInt *OrigC;
83-
if ((ICmpInst::isEquality(Pred) && !DecomposeBitMask) ||
83+
if ((ICmpInst::isEquality(Pred) && !DecomposeAnd) ||
8484
!match(RHS, m_APIntAllowPoison(OrigC)))
8585
return std::nullopt;
8686

@@ -102,7 +102,7 @@ llvm::decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate Pred,
102102

103103
switch (Pred) {
104104
default:
105-
return std::nullopt;
105+
llvm_unreachable("Unexpected predicate");
106106
case ICmpInst::ICMP_SLT: {
107107
// X < 0 is equivalent to (X & SignMask) != 0.
108108
if (C.isZero()) {
@@ -152,11 +152,14 @@ llvm::decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate Pred,
152152
}
153153
case ICmpInst::ICMP_EQ:
154154
case ICmpInst::ICMP_NE: {
155-
assert(DecomposeBitMask);
155+
assert(DecomposeAnd);
156156
const APInt *AndC;
157157
Value *AndVal;
158158
if (match(LHS, m_And(m_Value(AndVal), m_APIntAllowPoison(AndC)))) {
159-
Result = {AndVal /*X*/, Pred /*Pred*/, *AndC /*Mask*/, *OrigC /*C*/};
159+
LHS = AndVal;
160+
Result.Mask = *AndC;
161+
Result.C = C;
162+
Result.Pred = Pred;
160163
break;
161164
}
162165

@@ -175,25 +178,24 @@ llvm::decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate Pred,
175178
Result.X = X;
176179
Result.Mask = Result.Mask.zext(X->getType()->getScalarSizeInBits());
177180
Result.C = Result.C.zext(X->getType()->getScalarSizeInBits());
178-
} else if (!Result.X) {
181+
} else
179182
Result.X = LHS;
180-
}
181183

182184
return Result;
183185
}
184186

185187
std::optional<DecomposedBitTest> llvm::decomposeBitTest(Value *Cond,
186188
bool LookThruTrunc,
187189
bool AllowNonZeroC,
188-
bool DecomposeBitMask) {
190+
bool DecomposeAnd) {
189191
using namespace PatternMatch;
190192
if (auto *ICmp = dyn_cast<ICmpInst>(Cond)) {
191193
// Don't allow pointers. Splat vectors are fine.
192194
if (!ICmp->getOperand(0)->getType()->isIntOrIntVectorTy())
193195
return std::nullopt;
194196
return decomposeBitTestICmp(ICmp->getOperand(0), ICmp->getOperand(1),
195197
ICmp->getPredicate(), LookThruTrunc,
196-
AllowNonZeroC, DecomposeBitMask);
198+
AllowNonZeroC, DecomposeAnd);
197199
}
198200
Value *X;
199201
if (Cond->getType()->isIntOrIntVectorTy(1) &&

llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,7 @@ static Value *foldSignedTruncationCheck(ICmpInst *ICmp0, ICmpInst *ICmp1,
878878
auto Res = llvm::decomposeBitTestICmp(
879879
ICmp->getOperand(0), ICmp->getOperand(1), Pred,
880880
/*LookThroughTrunc=*/true, /*AllowNonZeroC=*/false,
881-
/*DecomposeBitMask=*/true);
881+
/*DecomposeAnd=*/true);
882882
if (Res && Res->Pred == ICmpInst::ICMP_EQ) {
883883
X = Res->X;
884884
UnsetBitsMask = Res->Mask;

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3730,29 +3730,31 @@ static Value *foldSelectBitTest(SelectInst &Sel, Value *CondVal, Value *TrueVal,
37303730
Value *CmpLHS, *CmpRHS;
37313731

37323732
if (match(CondVal, m_ICmp(Pred, m_Value(CmpLHS), m_Value(CmpRHS)))) {
3733-
auto Res = decomposeBitTestICmp(
3734-
CmpLHS, CmpRHS, Pred, /*LookThroughTrunc=*/true,
3735-
/*AllowNonZeroC=*/false, /*DecomposeBitMask=*/true);
3736-
3737-
if (!Res)
3738-
return nullptr;
3733+
if (ICmpInst::isEquality(Pred)) {
3734+
if (!match(CmpRHS, m_Zero()))
3735+
return nullptr;
37393736

3740-
V = CmpLHS;
3741-
AndMask = Res->Mask;
3737+
V = CmpLHS;
3738+
const APInt *AndRHS;
3739+
if (!match(CmpLHS, m_And(m_Value(), m_Power2(AndRHS))))
3740+
return nullptr;
37423741

3743-
if (!ICmpInst::isEquality(Pred)) {
3742+
AndMask = *AndRHS;
3743+
} else if (auto Res = decomposeBitTestICmp(CmpLHS, CmpRHS, Pred)) {
3744+
assert(ICmpInst::isEquality(Res->Pred) && "Not equality test?");
3745+
AndMask = Res->Mask;
37443746
V = Res->X;
37453747
KnownBits Known =
37463748
computeKnownBits(V, /*Depth=*/0, SQ.getWithInstruction(&Sel));
37473749
AndMask &= Known.getMaxValue();
3748-
CreateAnd = true;
3749-
}
3750-
3751-
Pred = Res->Pred;
3750+
if (!AndMask.isPowerOf2())
3751+
return nullptr;
37523752

3753-
if (!AndMask.isPowerOf2())
3753+
Pred = Res->Pred;
3754+
CreateAnd = true;
3755+
} else {
37543756
return nullptr;
3755-
3757+
}
37563758
} else if (auto *Trunc = dyn_cast<TruncInst>(CondVal)) {
37573759
V = Trunc->getOperand(0);
37583760
AndMask = APInt(V->getType()->getScalarSizeInBits(), 1);

llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2765,7 +2765,7 @@ static bool detectShiftUntilBitTestIdiom(Loop *CurLoop, Value *&BaseX,
27652765
auto MatchDecomposableConstantBitMask = [&]() {
27662766
auto Res = llvm::decomposeBitTestICmp(
27672767
CmpLHS, CmpRHS, Pred, /*LookThroughTrunc=*/true,
2768-
/*AllowNonZeroC=*/false, /*DecomposeBitMask=*/true);
2768+
/*AllowNonZeroC=*/false, /*DecomposeAnd=*/true);
27692769
if (Res && Res->Mask.isPowerOf2()) {
27702770
assert(ICmpInst::isEquality(Res->Pred));
27712771
Pred = Res->Pred;

0 commit comments

Comments
 (0)