-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[DAG] Fold (and X, (add (not Y), Z)) -> (and X, (not (sub Y, Z))). #141476
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
Conversation
@llvm/pr-subscribers-backend-x86 @llvm/pr-subscribers-llvm-selectiondag Author: Xu Zhang (simonzgx) ChangesFull diff: https://github.com/llvm/llvm-project/pull/141476.diff 1 Files Affected:
diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index efaa8bd4a7950..31fbab23e4fcf 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -7528,6 +7528,13 @@ SDValue DAGCombiner::visitAND(SDNode *N) {
return DAG.getNode(ISD::AND, DL, VT, X,
DAG.getNOT(DL, DAG.getNode(Opc, DL, VT, Y, Z), VT));
+ // a bitwiseop (~b + c) -> a bitwiseop ~(b - c).
+ // Fold (and X, (add (not Y), Z)) -> (and X, (not (sub Y, Z)))
+ if (sd_match(N, m_And(m_Value(X), m_Add(m_Value(NotY), m_Value(Z)))) &&
+ sd_match(NotY, m_Not(m_Value(Y))) &&
+ (TLI.hasAndNot(SDValue(N, 0)) || NotY->hasOneUse())) {
+ return DAG.getNode(ISD::AND, DL, VT, X, DAG.getNOT(DL, DAG.getNode(ISD::SUB, DL, VT, Y, Z), VT));
+ }
// Fold (and (srl X, C), 1) -> (srl X, BW-1) for signbit extraction
// If we are shifting down an extended sign bit, see if we can simplify
// this to shifting the MSB directly to expose further simplifications.
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
As well as |
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.
Needs tests
Yes, there are some failed UT that need fixing and some new ones to add. I'm still working on both and will submit soon. |
pin |
|
||
if (sd_match(N, m_BitwiseLogic(m_Value(X), | ||
m_Sub(m_AllOf(m_Value(Y), m_Not(m_Value(Y)), | ||
m_OneUse(m_Not(m_Value(Y)))), |
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 m_OneUse isn't doing anything as the previous pattern is matching all m_Not cases
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.
Please can regenerate the failing test files:
LLVM :: CodeGen/LoongArch/ctlz-cttz-ctpop.ll
LLVM :: CodeGen/X86/vector-lzcnt-128.ll
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!
…lvm#141476) Fixes llvm#140639 --------- Co-authored-by: Simon Pilgrim <llvm-dev@redking.me.uk>
…lvm#141476) Fixes llvm#140639 --------- Co-authored-by: Simon Pilgrim <llvm-dev@redking.me.uk>
Fixes #140639