-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[TargetLowering] Update to use SDPatternMatch (NFC) #146894
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
Open
AZero13
wants to merge
1
commit into
llvm:main
Choose a base branch
from
AZero13:sdpattern
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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-selectiondag Author: AZero13 (AZero13) ChangesFull diff: https://github.com/llvm/llvm-project/pull/146894.diff 1 Files Affected:
diff --git a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
index 000f8cc6786a5..cee2aabdf9874 100644
--- a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
@@ -4179,51 +4179,42 @@ SDValue TargetLowering::foldSetCCWithAnd(EVT VT, SDValue N0, SDValue N1,
}
}
- // Match these patterns in any of their permutations:
// (X & Y) == Y
// (X & Y) != Y
- SDValue X, Y;
- if (N0.getOperand(0) == N1) {
- X = N0.getOperand(1);
- Y = N0.getOperand(0);
- } else if (N0.getOperand(1) == N1) {
- X = N0.getOperand(0);
- Y = N0.getOperand(1);
- } else {
- return SDValue();
- }
-
- // TODO: We should invert (X & Y) eq/ne 0 -> (X & Y) ne/eq Y if
- // `isXAndYEqZeroPreferableToXAndYEqY` is false. This is a bit difficult as
- // its liable to create and infinite loop.
- SDValue Zero = DAG.getConstant(0, DL, OpVT);
- if (isXAndYEqZeroPreferableToXAndYEqY(Cond, OpVT) &&
- DAG.isKnownToBeAPowerOfTwo(Y)) {
- // Simplify X & Y == Y to X & Y != 0 if Y has exactly one bit set.
- // Note that where Y is variable and is known to have at most one bit set
- // (for example, if it is Z & 1) we cannot do this; the expressions are not
- // equivalent when Y == 0.
- assert(OpVT.isInteger());
- Cond = ISD::getSetCCInverse(Cond, OpVT);
- if (DCI.isBeforeLegalizeOps() ||
- isCondCodeLegal(Cond, N0.getSimpleValueType()))
- return DAG.getSetCC(DL, VT, N0, Zero, Cond);
- } else if (N0.hasOneUse() && hasAndNotCompare(Y)) {
- // If the target supports an 'and-not' or 'and-complement' logic operation,
- // try to use that to make a comparison operation more efficient.
- // But don't do this transform if the mask is a single bit because there are
- // more efficient ways to deal with that case (for example, 'bt' on x86 or
- // 'rlwinm' on PPC).
-
- // Bail out if the compare operand that we want to turn into a zero is
- // already a zero (otherwise, infinite loop).
- if (isNullConstant(Y))
- return SDValue();
-
- // Transform this into: ~X & Y == 0.
- SDValue NotX = DAG.getNOT(SDLoc(X), X, OpVT);
- SDValue NewAnd = DAG.getNode(ISD::AND, SDLoc(N0), OpVT, NotX, Y);
- return DAG.getSetCC(DL, VT, NewAnd, Zero, Cond);
+ SDValue X;
+ if (sd_match(N0, m_And(m_Value(X), m_Specific(N1))) && hasAndNotCompare(N1)) {
+ // TODO: We should invert (X & Y) eq/ne 0 -> (X & Y) ne/eq Y if
+ // `isXAndYEqZeroPreferableToXAndYEqY` is false. This is a bit difficult as
+ // its liable to create and infinite loop.
+ SDValue Zero = DAG.getConstant(0, DL, OpVT);
+ if (isXAndYEqZeroPreferableToXAndYEqY(Cond, OpVT) &&
+ DAG.isKnownToBeAPowerOfTwo(N1)) {
+ // Simplify X & Y == Y to X & Y != 0 if Y has exactly one bit set.
+ // Note that where Y is variable and is known to have at most one bit set
+ // (for example, if it is Z & 1) we cannot do this; the expressions are
+ // not equivalent when Y == 0.
+ assert(OpVT.isInteger());
+ Cond = ISD::getSetCCInverse(Cond, OpVT);
+ if (DCI.isBeforeLegalizeOps() ||
+ isCondCodeLegal(Cond, N0.getSimpleValueType()))
+ return DAG.getSetCC(DL, VT, N0, Zero, Cond);
+ } else if (N0.hasOneUse() && hasAndNotCompare(N1)) {
+ // If the target supports an 'and-not' or 'and-complement' logic
+ // operation, try to use that to make a comparison operation more
+ // efficient. But don't do this transform if the mask is a single bit
+ // because there are more efficient ways to deal with that case (for
+ // example, 'bt' on x86 or 'rlwinm' on PPC).
+
+ // Bail out if the compare operand that we want to turn into a zero is
+ // already a zero (otherwise, infinite loop).
+ if (isNullConstant(N1))
+ return SDValue();
+
+ // Transform this into: ~X & Y == 0.
+ SDValue NotX = DAG.getNOT(SDLoc(X), X, OpVT);
+ SDValue NewAnd = DAG.getNode(ISD::AND, SDLoc(N0), OpVT, NotX, N1);
+ return DAG.getSetCC(DL, VT, NewAnd, Zero, Cond);
+ }
}
return SDValue();
|
arsenm
reviewed
Jul 7, 2025
| SDValue NewAnd = DAG.getNode(ISD::AND, SDLoc(N0), OpVT, NotX, Y); | ||
| return DAG.getSetCC(DL, VT, NewAnd, Zero, Cond); | ||
| SDValue X; | ||
| if (sd_match(N0, m_And(m_Value(X), m_Specific(N1)))) { |
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.
The and check was already performed above; it's not a plus unless you eliminate the earlier check too?
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.