-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[InstCombine] Improve folding of icmp pred (and X, Mask/~Mask), Y)
#81562
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1a3fec9
[InstCombine] Add tests for expanding `foldICmpWithLowBitMaskedVal`; NFC
goldsteinn d98b93f
[InstCombine] Move `foldICmpWithLowBitMaskedVal` to `foldICmpCommutat…
goldsteinn 7f8c766
[InstCombine] Improve mask detection in `foldICmpWithLowBitMaskedVal`
goldsteinn ceb1d02
[InstCombine] Recognize `(icmp eq/ne (and X, ~Mask), 0)` pattern in `…
goldsteinn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4069,11 +4069,101 @@ Instruction *InstCombinerImpl::foldSelectICmp(ICmpInst::Predicate Pred, | |
return nullptr; | ||
} | ||
|
||
// Returns whether V is a Mask ((X + 1) & X == 0) or ~Mask (-Pow2OrZero) | ||
static bool isMaskOrZero(const Value *V, bool Not, const SimplifyQuery &Q, | ||
unsigned Depth = 0) { | ||
if (Not ? match(V, m_NegatedPower2OrZero()) : match(V, m_LowBitMaskOrZero())) | ||
return true; | ||
if (V->getType()->getScalarSizeInBits() == 1) | ||
return true; | ||
if (Depth++ >= MaxAnalysisRecursionDepth) | ||
dtcxzyw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return false; | ||
Value *X; | ||
const Instruction *I = dyn_cast<Instruction>(V); | ||
if (!I) | ||
return false; | ||
switch (I->getOpcode()) { | ||
dtcxzyw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
case Instruction::ZExt: | ||
// ZExt(Mask) is a Mask. | ||
return !Not && isMaskOrZero(I->getOperand(0), Not, Q, Depth); | ||
case Instruction::SExt: | ||
// SExt(Mask) is a Mask. | ||
// SExt(~Mask) is a ~Mask. | ||
return isMaskOrZero(I->getOperand(0), Not, Q, Depth); | ||
case Instruction::And: | ||
case Instruction::Or: | ||
// Mask0 | Mask1 is a Mask. | ||
// Mask0 & Mask1 is a Mask. | ||
// ~Mask0 | ~Mask1 is a ~Mask. | ||
// ~Mask0 & ~Mask1 is a ~Mask. | ||
return isMaskOrZero(I->getOperand(1), Not, Q, Depth) && | ||
isMaskOrZero(I->getOperand(0), Not, Q, Depth); | ||
case Instruction::Xor: | ||
if (match(V, m_Not(m_Value(X)))) | ||
return isMaskOrZero(X, !Not, Q, Depth); | ||
|
||
// (X ^ (X - 1)) is a Mask | ||
dtcxzyw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return !Not && | ||
match(V, m_c_Xor(m_Value(X), m_Add(m_Deferred(X), m_AllOnes()))); | ||
case Instruction::Select: | ||
// c ? Mask0 : Mask1 is a Mask. | ||
return isMaskOrZero(I->getOperand(1), Not, Q, Depth) && | ||
isMaskOrZero(I->getOperand(2), Not, Q, Depth); | ||
case Instruction::Shl: | ||
// (~Mask) << X is a ~Mask. | ||
return Not && isMaskOrZero(I->getOperand(0), Not, Q, Depth); | ||
case Instruction::LShr: | ||
// Mask >> X is a Mask. | ||
return !Not && isMaskOrZero(I->getOperand(0), Not, Q, Depth); | ||
case Instruction::AShr: | ||
// Mask s>> X is a Mask. | ||
// ~Mask s>> X is a ~Mask. | ||
return isMaskOrZero(I->getOperand(0), Not, Q, Depth); | ||
case Instruction::Add: | ||
// Pow2 - 1 is a Mask. | ||
if (!Not && match(I->getOperand(1), m_AllOnes())) | ||
return isKnownToBeAPowerOfTwo(I->getOperand(0), Q.DL, /*OrZero*/ true, | ||
Depth, Q.AC, Q.CxtI, Q.DT); | ||
break; | ||
case Instruction::Sub: | ||
// -Pow2 is a ~Mask. | ||
if (Not && match(I->getOperand(0), m_Zero())) | ||
return isKnownToBeAPowerOfTwo(I->getOperand(1), Q.DL, /*OrZero*/ true, | ||
Depth, Q.AC, Q.CxtI, Q.DT); | ||
break; | ||
case Instruction::Call: { | ||
if (auto *II = dyn_cast<IntrinsicInst>(I)) { | ||
switch (II->getIntrinsicID()) { | ||
// min/max(Mask0, Mask1) is a Mask. | ||
// min/max(~Mask0, ~Mask1) is a ~Mask. | ||
case Intrinsic::umax: | ||
case Intrinsic::smax: | ||
case Intrinsic::umin: | ||
case Intrinsic::smin: | ||
return isMaskOrZero(II->getArgOperand(1), Not, Q, Depth) && | ||
isMaskOrZero(II->getArgOperand(0), Not, Q, Depth); | ||
|
||
// In the context of masks, bitreverse(Mask) == ~Mask | ||
case Intrinsic::bitreverse: | ||
return isMaskOrZero(II->getArgOperand(0), !Not, Q, Depth); | ||
default: | ||
break; | ||
} | ||
} | ||
break; | ||
} | ||
default: | ||
break; | ||
} | ||
return false; | ||
} | ||
|
||
/// Some comparisons can be simplified. | ||
/// In this case, we are looking for comparisons that look like | ||
/// a check for a lossy truncation. | ||
/// Folds: | ||
/// icmp SrcPred (x & Mask), x to icmp DstPred x, Mask | ||
/// icmp eq/ne (x & ~Mask), 0 to icmp DstPred x, Mask | ||
/// Where Mask is some pattern that produces all-ones in low bits: | ||
/// (-1 >> y) | ||
/// ((-1 << y) >> y) <- non-canonical, has extra uses | ||
|
@@ -4082,23 +4172,35 @@ Instruction *InstCombinerImpl::foldSelectICmp(ICmpInst::Predicate Pred, | |
/// The Mask can be a constant, too. | ||
/// For some predicates, the operands are commutative. | ||
/// For others, x can only be on a specific side. | ||
static Value *foldICmpWithLowBitMaskedVal(ICmpInst &I, | ||
InstCombiner::BuilderTy &Builder) { | ||
ICmpInst::Predicate SrcPred; | ||
Value *X, *M, *Y; | ||
auto m_VariableMask = m_CombineOr( | ||
m_CombineOr(m_Not(m_Shl(m_AllOnes(), m_Value())), | ||
m_Add(m_Shl(m_One(), m_Value()), m_AllOnes())), | ||
m_CombineOr(m_LShr(m_AllOnes(), m_Value()), | ||
m_LShr(m_Shl(m_AllOnes(), m_Value(Y)), m_Deferred(Y)))); | ||
auto m_Mask = m_CombineOr(m_VariableMask, m_LowBitMask()); | ||
if (!match(&I, m_c_ICmp(SrcPred, | ||
m_c_And(m_CombineAnd(m_Mask, m_Value(M)), m_Value(X)), | ||
m_Deferred(X)))) | ||
static Value *foldICmpWithLowBitMaskedVal(ICmpInst::Predicate Pred, Value *Op0, | ||
Value *Op1, const SimplifyQuery &Q, | ||
InstCombiner &IC) { | ||
Value *X, *M; | ||
bool NeedsNot = false; | ||
|
||
auto CheckMask = [&](Value *V, bool Not) { | ||
if (ICmpInst::isSigned(Pred) && !match(V, m_ImmConstant())) | ||
return false; | ||
return isMaskOrZero(V, Not, Q); | ||
}; | ||
|
||
if (match(Op0, m_c_And(m_Specific(Op1), m_Value(M))) && | ||
CheckMask(M, /*Not*/ false)) { | ||
X = Op1; | ||
} else if (match(Op1, m_Zero()) && ICmpInst::isEquality(Pred) && | ||
match(Op0, m_OneUse(m_And(m_Value(X), m_Value(M))))) { | ||
NeedsNot = true; | ||
if (IC.isFreeToInvert(X, X->hasOneUse()) && CheckMask(X, /*Not*/ true)) | ||
std::swap(X, M); | ||
else if (!IC.isFreeToInvert(M, M->hasOneUse()) || | ||
!CheckMask(M, /*Not*/ true)) | ||
return nullptr; | ||
} else { | ||
return nullptr; | ||
} | ||
|
||
ICmpInst::Predicate DstPred; | ||
switch (SrcPred) { | ||
switch (Pred) { | ||
case ICmpInst::Predicate::ICMP_EQ: | ||
// x & (-1 >> y) == x -> x u<= (-1 >> y) | ||
DstPred = ICmpInst::Predicate::ICMP_ULE; | ||
|
@@ -4164,7 +4266,9 @@ static Value *foldICmpWithLowBitMaskedVal(ICmpInst &I, | |
M = Constant::replaceUndefsWith(VecC, SafeReplacementConstant); | ||
} | ||
|
||
return Builder.CreateICmp(DstPred, X, M); | ||
if (NeedsNot) | ||
M = IC.Builder.CreateNot(M); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If its freely invertable |
||
return IC.Builder.CreateICmp(DstPred, X, M); | ||
} | ||
|
||
/// Some comparisons can be simplified. | ||
|
@@ -5081,9 +5185,6 @@ Instruction *InstCombinerImpl::foldICmpBinOp(ICmpInst &I, | |
if (Value *V = foldMultiplicationOverflowCheck(I)) | ||
return replaceInstUsesWith(I, V); | ||
|
||
if (Value *V = foldICmpWithLowBitMaskedVal(I, Builder)) | ||
return replaceInstUsesWith(I, V); | ||
|
||
if (Instruction *R = foldICmpAndXX(I, Q, *this)) | ||
return R; | ||
|
||
|
@@ -6984,6 +7085,10 @@ Instruction *InstCombinerImpl::foldICmpCommutative(ICmpInst::Predicate Pred, | |
} | ||
} | ||
|
||
const SimplifyQuery Q = SQ.getWithInstruction(&CxtI); | ||
if (Value *V = foldICmpWithLowBitMaskedVal(Pred, Op0, Op1, Q, *this)) | ||
return replaceInstUsesWith(CxtI, V); | ||
|
||
return nullptr; | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.