[llvm] [PatternMatch] Fix matching order for `m_c_Intrinsic` (PR #166047)
via llvm-commits
llvm-commits at lists.llvm.org
Sun Nov 2 01:00:28 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-ir
Author: Yingwei Zheng (dtcxzyw)
<details>
<summary>Changes</summary>
`Op0` should always be checked before `Op1`. Otherwise it may not work as expected when `Op1` contains `m_Deferred`.
---
Full diff: https://github.com/llvm/llvm-project/pull/166047.diff
1 Files Affected:
- (modified) llvm/include/llvm/IR/PatternMatch.h (+20-4)
``````````diff
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.
``````````
</details>
https://github.com/llvm/llvm-project/pull/166047
More information about the llvm-commits
mailing list