-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[NFC][ValueTracking] Hoist the matching of RHS constant #125818
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
Merged
Merged
Conversation
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
@llvm/pr-subscribers-llvm-analysis Author: Yingwei Zheng (dtcxzyw) ChangesFull diff: https://github.com/llvm/llvm-project/pull/125818.diff 1 Files Affected:
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index f4958162accfe3..6eba6c0f08c3f4 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -683,28 +683,30 @@ static void computeKnownBitsFromCmp(const Value *V, CmpInst::Predicate Pred,
Value *Y;
const APInt *Mask, *C;
+ if (!match(RHS, m_APInt(C)))
+ return;
+
uint64_t ShAmt;
switch (Pred) {
case ICmpInst::ICMP_EQ:
// assume(V = C)
- if (match(LHS, m_V) && match(RHS, m_APInt(C))) {
+ if (match(LHS, m_V)) {
Known = Known.unionWith(KnownBits::makeConstant(*C));
// assume(V & Mask = C)
- } else if (match(LHS, m_c_And(m_V, m_Value(Y))) &&
- match(RHS, m_APInt(C))) {
+ } else if (match(LHS, m_c_And(m_V, m_Value(Y)))) {
// For one bits in Mask, we can propagate bits from C to V.
Known.One |= *C;
if (match(Y, m_APInt(Mask)))
Known.Zero |= ~*C & *Mask;
// assume(V | Mask = C)
- } else if (match(LHS, m_c_Or(m_V, m_Value(Y))) && match(RHS, m_APInt(C))) {
+ } else if (match(LHS, m_c_Or(m_V, m_Value(Y)))) {
// For zero bits in Mask, we can propagate bits from C to V.
Known.Zero |= ~*C;
if (match(Y, m_APInt(Mask)))
Known.One |= *C & ~*Mask;
// assume(V << ShAmt = C)
} else if (match(LHS, m_Shl(m_V, m_ConstantInt(ShAmt))) &&
- match(RHS, m_APInt(C)) && ShAmt < BitWidth) {
+ ShAmt < BitWidth) {
// For those bits in C that are known, we can propagate them to known
// bits in V shifted to the right by ShAmt.
KnownBits RHSKnown = KnownBits::makeConstant(*C);
@@ -713,7 +715,7 @@ static void computeKnownBitsFromCmp(const Value *V, CmpInst::Predicate Pred,
Known = Known.unionWith(RHSKnown);
// assume(V >> ShAmt = C)
} else if (match(LHS, m_Shr(m_V, m_ConstantInt(ShAmt))) &&
- match(RHS, m_APInt(C)) && ShAmt < BitWidth) {
+ ShAmt < BitWidth) {
KnownBits RHSKnown = KnownBits::makeConstant(*C);
// For those bits in RHS that are known, we can propagate them to known
// bits in V shifted to the right by C.
@@ -724,38 +726,36 @@ static void computeKnownBitsFromCmp(const Value *V, CmpInst::Predicate Pred,
case ICmpInst::ICMP_NE: {
// assume (V & B != 0) where B is a power of 2
const APInt *BPow2;
- if (match(LHS, m_And(m_V, m_Power2(BPow2))) && match(RHS, m_Zero()))
+ if (C->isZero() && match(LHS, m_And(m_V, m_Power2(BPow2))))
Known.One |= *BPow2;
break;
}
- default:
- if (match(RHS, m_APInt(C))) {
- const APInt *Offset = nullptr;
- if (match(LHS, m_CombineOr(m_V, m_AddLike(m_V, m_APInt(Offset))))) {
- ConstantRange LHSRange = ConstantRange::makeAllowedICmpRegion(Pred, *C);
- if (Offset)
- LHSRange = LHSRange.sub(*Offset);
- Known = Known.unionWith(LHSRange.toKnownBits());
- }
- if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
- // X & Y u> C -> X u> C && Y u> C
- // X nuw- Y u> C -> X u> C
- if (match(LHS, m_c_And(m_V, m_Value())) ||
- match(LHS, m_NUWSub(m_V, m_Value())))
- Known.One.setHighBits(
- (*C + (Pred == ICmpInst::ICMP_UGT)).countLeadingOnes());
- }
- if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
- // X | Y u< C -> X u< C && Y u< C
- // X nuw+ Y u< C -> X u< C && Y u< C
- if (match(LHS, m_c_Or(m_V, m_Value())) ||
- match(LHS, m_c_NUWAdd(m_V, m_Value()))) {
- Known.Zero.setHighBits(
- (*C - (Pred == ICmpInst::ICMP_ULT)).countLeadingZeros());
- }
+ default: {
+ const APInt *Offset = nullptr;
+ if (match(LHS, m_CombineOr(m_V, m_AddLike(m_V, m_APInt(Offset))))) {
+ ConstantRange LHSRange = ConstantRange::makeAllowedICmpRegion(Pred, *C);
+ if (Offset)
+ LHSRange = LHSRange.sub(*Offset);
+ Known = Known.unionWith(LHSRange.toKnownBits());
+ }
+ if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
+ // X & Y u> C -> X u> C && Y u> C
+ // X nuw- Y u> C -> X u> C
+ if (match(LHS, m_c_And(m_V, m_Value())) ||
+ match(LHS, m_NUWSub(m_V, m_Value())))
+ Known.One.setHighBits(
+ (*C + (Pred == ICmpInst::ICMP_UGT)).countLeadingOnes());
+ }
+ if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
+ // X | Y u< C -> X u< C && Y u< C
+ // X nuw+ Y u< C -> X u< C && Y u< C
+ if (match(LHS, m_c_Or(m_V, m_Value())) ||
+ match(LHS, m_c_NUWAdd(m_V, m_Value()))) {
+ Known.Zero.setHighBits(
+ (*C - (Pred == ICmpInst::ICMP_ULT)).countLeadingZeros());
}
}
- break;
+ } break;
}
}
|
nikic
approved these changes
Feb 5, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Icohedron
pushed a commit
to Icohedron/llvm-project
that referenced
this pull request
Feb 11, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
No description provided.