-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[PatternMatch] Fix matching order for m_c_Intrinsic
#166047
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-llvm-ir Author: Yingwei Zheng (dtcxzyw) Changes
Full diff: https://github.com/llvm/llvm-project/pull/166047.diff 1 Files Affected:
diff --git a/llvm/include/llvm/IR/PatternMatch.h b/llvm/include/llvm/IR/PatternMatch.h
index e3ec7e1764da7..54f9c28dcbf97 100644
--- a/llvm/include/llvm/IR/PatternMatch.h
+++ b/llvm/include/llvm/IR/PatternMatch.h
@@ -3069,12 +3069,28 @@ m_c_MaxOrMin(const LHS &L, const RHS &R) {
m_CombineOr(m_c_UMax(L, R), m_c_UMin(L, R)));
}
+template <Intrinsic::ID IntrID, typename LHS, typename RHS>
+struct CommutativeBinaryIntrinsic_match {
+ unsigned ID;
+ LHS L;
+ RHS R;
+
+ CommutativeBinaryIntrinsic_match(const LHS &L, const RHS &R)
+ : ID(IntrID), L(L), R(R) {}
+
+ template <typename OpTy> bool match(OpTy *V) const {
+ const auto *II = dyn_cast<IntrinsicInst>(V);
+ if (!II || II->getIntrinsicID() != ID)
+ return false;
+ return (L.match(II->getArgOperand(0)) && R.match(II->getArgOperand(1))) ||
+ (L.match(II->getArgOperand(1)) && R.match(II->getArgOperand(0)));
+ }
+};
+
template <Intrinsic::ID IntrID, typename T0, typename T1>
-inline match_combine_or<typename m_Intrinsic_Ty<T0, T1>::Ty,
- typename m_Intrinsic_Ty<T1, T0>::Ty>
+inline CommutativeBinaryIntrinsic_match<IntrID, T0, T1>
m_c_Intrinsic(const T0 &Op0, const T1 &Op1) {
- return m_CombineOr(m_Intrinsic<IntrID>(Op0, Op1),
- m_Intrinsic<IntrID>(Op1, Op0));
+ return CommutativeBinaryIntrinsic_match<IntrID, T0, T1>(Op0, Op1);
}
/// Matches FAdd with LHS and RHS in either order.
|
artagnon
left a comment
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.
I'm confused what the functional change is here: can you write a test in PatternMatchTest?
Done. |
antoniofrighetto
left a comment
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
artagnon
left a comment
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, thanks!
Op0should always be checked beforeOp1. Otherwise it may not work as expected whenOp1containsm_Deferred.