-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[DAG] Convert foldMaskedMerge to SDPatternMatch to match (m & x) | (~m & y) #143855
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
[DAG] Convert foldMaskedMerge to SDPatternMatch to match (m & x) | (~m & y) #143855
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-llvm-selectiondag Author: woruyu (woruyu) ChangesSummaryThis PR resolves #143363 Remove foldMaskedMergeImpl entirely to use SDPatternMatch Full diff: https://github.com/llvm/llvm-project/pull/143855.diff 1 Files Affected:
diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index e79a17e86bc87..19a02887ad315 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -8128,24 +8128,6 @@ static SDValue visitORCommutative(SelectionDAG &DAG, SDValue N0, SDValue N1,
return SDValue();
}
-static SDValue foldMaskedMergeImpl(SDValue AndL0, SDValue AndR0, SDValue AndL1,
- SDValue AndR1, const SDLoc &DL,
- SelectionDAG &DAG) {
- if (!isBitwiseNot(AndL0, true) || !AndL0->hasOneUse())
- return SDValue();
- SDValue NotOp = AndL0->getOperand(0);
- if (NotOp == AndR1)
- std::swap(AndR1, AndL1);
- if (NotOp != AndL1)
- return SDValue();
-
- EVT VT = AndL1.getValueType();
- SDValue Xor0 = DAG.getNode(ISD::XOR, DL, VT, AndR1, AndR0);
- SDValue And = DAG.getNode(ISD::AND, DL, VT, Xor0, NotOp);
- SDValue Xor1 = DAG.getNode(ISD::XOR, DL, VT, And, AndR0);
- return Xor1;
-}
-
/// Fold "masked merge" expressions like `(m & x) | (~m & y)` into the
/// equivalent `((x ^ y) & m) ^ y)` pattern.
/// This is typically a better representation for targets without a fused
@@ -8155,29 +8137,23 @@ static SDValue foldMaskedMerge(SDNode *Node, SelectionDAG &DAG,
// Note that masked-merge variants using XOR or ADD expressions are
// normalized to OR by InstCombine so we only check for OR.
assert(Node->getOpcode() == ISD::OR && "Must be called with ISD::OR node");
- SDValue N0 = Node->getOperand(0);
- if (N0->getOpcode() != ISD::AND || !N0->hasOneUse())
- return SDValue();
- SDValue N1 = Node->getOperand(1);
- if (N1->getOpcode() != ISD::AND || !N1->hasOneUse())
- return SDValue();
// If the target supports and-not, don't fold this.
if (TLI.hasAndNot(SDValue(Node, 0)))
return SDValue();
- SDValue N00 = N0->getOperand(0);
- SDValue N01 = N0->getOperand(1);
- SDValue N10 = N1->getOperand(0);
- SDValue N11 = N1->getOperand(1);
- if (SDValue Result = foldMaskedMergeImpl(N00, N01, N10, N11, DL, DAG))
- return Result;
- if (SDValue Result = foldMaskedMergeImpl(N01, N00, N10, N11, DL, DAG))
- return Result;
- if (SDValue Result = foldMaskedMergeImpl(N10, N11, N00, N01, DL, DAG))
- return Result;
- if (SDValue Result = foldMaskedMergeImpl(N11, N10, N00, N01, DL, DAG))
- return Result;
+ SDValue M, X, Y;
+ if (sd_match(Node, m_Or(m_OneUse(m_And(m_Value(M), m_Value(X))),
+ m_OneUse(m_And(m_OneUse(m_Not(m_Deferred(M))),
+ m_Value(Y))))) ||
+ sd_match(Node,
+ m_Or(m_OneUse(m_And(m_OneUse(m_Not(m_Value(M))), m_Value(Y))),
+ m_OneUse(m_And(m_Deferred(M), m_Value(X)))))) {
+ EVT VT = M.getValueType();
+ SDValue Xor = DAG.getNode(ISD::XOR, DL, VT, X, Y);
+ SDValue And = DAG.getNode(ISD::AND, DL, VT, Xor, M);
+ return DAG.getNode(ISD::XOR, DL, VT, And, Y);
+ }
return SDValue();
}
|
@RKSimon, hello, I run check-llvm-codegen locally, it can work well! |
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 but please wait until the commutativity discussion is complete.
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 - cheers
@woruyu Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
…m & y) (llvm#143855) This PR resolves llvm#143363 Remove foldMaskedMergeImpl entirely to use SDPatternMatch
…m & y) (llvm#143855) This PR resolves llvm#143363 Remove foldMaskedMergeImpl entirely to use SDPatternMatch
Summary
This PR resolves #143363
Remove foldMaskedMergeImpl entirely to use SDPatternMatch