Skip to content

Commit f701d9b

Browse files
dtcxzywgithub-actions[bot]
authored andcommitted
Automerge: [PatternMatch] Fix matching order for m_c_Intrinsic (#166047)
`Op0` should always be checked before `Op1`. Otherwise it may not work as expected when `Op1` contains `m_Deferred`.
2 parents 7f90827 + 509ee6b commit f701d9b

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

llvm/include/llvm/IR/PatternMatch.h

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3069,12 +3069,26 @@ m_c_MaxOrMin(const LHS &L, const RHS &R) {
30693069
m_CombineOr(m_c_UMax(L, R), m_c_UMin(L, R)));
30703070
}
30713071

3072+
template <Intrinsic::ID IntrID, typename LHS, typename RHS>
3073+
struct CommutativeBinaryIntrinsic_match {
3074+
LHS L;
3075+
RHS R;
3076+
3077+
CommutativeBinaryIntrinsic_match(const LHS &L, const RHS &R) : L(L), R(R) {}
3078+
3079+
template <typename OpTy> bool match(OpTy *V) const {
3080+
const auto *II = dyn_cast<IntrinsicInst>(V);
3081+
if (!II || II->getIntrinsicID() != IntrID)
3082+
return false;
3083+
return (L.match(II->getArgOperand(0)) && R.match(II->getArgOperand(1))) ||
3084+
(L.match(II->getArgOperand(1)) && R.match(II->getArgOperand(0)));
3085+
}
3086+
};
3087+
30723088
template <Intrinsic::ID IntrID, typename T0, typename T1>
3073-
inline match_combine_or<typename m_Intrinsic_Ty<T0, T1>::Ty,
3074-
typename m_Intrinsic_Ty<T1, T0>::Ty>
3089+
inline CommutativeBinaryIntrinsic_match<IntrID, T0, T1>
30753090
m_c_Intrinsic(const T0 &Op0, const T1 &Op1) {
3076-
return m_CombineOr(m_Intrinsic<IntrID>(Op0, Op1),
3077-
m_Intrinsic<IntrID>(Op1, Op0));
3091+
return CommutativeBinaryIntrinsic_match<IntrID, T0, T1>(Op0, Op1);
30783092
}
30793093

30803094
/// Matches FAdd with LHS and RHS in either order.

llvm/unittests/IR/PatternMatch.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2657,4 +2657,31 @@ TEST_F(PatternMatchTest, ShiftOrSelf) {
26572657
EXPECT_EQ(ShAmtC, 0U);
26582658
}
26592659

2660+
TEST_F(PatternMatchTest, CommutativeDeferredIntrinsicMatch) {
2661+
Value *X = ConstantFP::get(IRB.getDoubleTy(), 1.0);
2662+
Value *Y = ConstantFP::get(IRB.getDoubleTy(), 2.0);
2663+
2664+
auto CheckMatch = [X, Y](Value *Pattern) {
2665+
Value *tX = nullptr, *tY = nullptr;
2666+
EXPECT_TRUE(
2667+
match(Pattern, m_c_Intrinsic<Intrinsic::minimum>(
2668+
m_Value(tX), m_c_Intrinsic<Intrinsic::minimum>(
2669+
m_Deferred(tX), m_Value(tY)))));
2670+
EXPECT_EQ(tX, X);
2671+
EXPECT_EQ(tY, Y);
2672+
};
2673+
CheckMatch(IRB.CreateBinaryIntrinsic(
2674+
Intrinsic::minimum, X,
2675+
IRB.CreateBinaryIntrinsic(Intrinsic::minimum, X, Y)));
2676+
CheckMatch(IRB.CreateBinaryIntrinsic(
2677+
Intrinsic::minimum, X,
2678+
IRB.CreateBinaryIntrinsic(Intrinsic::minimum, Y, X)));
2679+
CheckMatch(IRB.CreateBinaryIntrinsic(
2680+
Intrinsic::minimum, IRB.CreateBinaryIntrinsic(Intrinsic::minimum, X, Y),
2681+
X));
2682+
CheckMatch(IRB.CreateBinaryIntrinsic(
2683+
Intrinsic::minimum, IRB.CreateBinaryIntrinsic(Intrinsic::minimum, Y, X),
2684+
X));
2685+
}
2686+
26602687
} // anonymous namespace.

0 commit comments

Comments
 (0)