Skip to content

Commit 2f01a13

Browse files
goldsteinnyuxuanchen1997
authored andcommitted
Recommit "[PatternMatch] Fix issue of stale reference in new m_{I,F,}Cmp matchers" (3rd Try) (#99292)
The first fix forgot to fixup the commutative matchers...
1 parent ee50cdc commit 2f01a13

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

llvm/include/llvm/IR/PatternMatch.h

+12-13
Original file line numberDiff line numberDiff line change
@@ -1550,23 +1550,27 @@ template <typename T> inline Exact_match<T> m_Exact(const T &SubPattern) {
15501550
template <typename LHS_t, typename RHS_t, typename Class, typename PredicateTy,
15511551
bool Commutable = false>
15521552
struct CmpClass_match {
1553-
PredicateTy &Predicate;
1553+
PredicateTy *Predicate;
15541554
LHS_t L;
15551555
RHS_t R;
15561556

15571557
// The evaluation order is always stable, regardless of Commutability.
15581558
// The LHS is always matched first.
15591559
CmpClass_match(PredicateTy &Pred, const LHS_t &LHS, const RHS_t &RHS)
1560-
: Predicate(Pred), L(LHS), R(RHS) {}
1560+
: Predicate(&Pred), L(LHS), R(RHS) {}
1561+
CmpClass_match(const LHS_t &LHS, const RHS_t &RHS)
1562+
: Predicate(nullptr), L(LHS), R(RHS) {}
15611563

15621564
template <typename OpTy> bool match(OpTy *V) {
15631565
if (auto *I = dyn_cast<Class>(V)) {
15641566
if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) {
1565-
Predicate = I->getPredicate();
1567+
if (Predicate)
1568+
*Predicate = I->getPredicate();
15661569
return true;
15671570
} else if (Commutable && L.match(I->getOperand(1)) &&
15681571
R.match(I->getOperand(0))) {
1569-
Predicate = I->getSwappedPredicate();
1572+
if (Predicate)
1573+
*Predicate = I->getSwappedPredicate();
15701574
return true;
15711575
}
15721576
}
@@ -1595,22 +1599,19 @@ m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
15951599
template <typename LHS, typename RHS>
15961600
inline CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate>
15971601
m_Cmp(const LHS &L, const RHS &R) {
1598-
CmpInst::Predicate Unused;
1599-
return CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate>(Unused, L, R);
1602+
return CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate>(L, R);
16001603
}
16011604

16021605
template <typename LHS, typename RHS>
16031606
inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>
16041607
m_ICmp(const LHS &L, const RHS &R) {
1605-
ICmpInst::Predicate Unused;
1606-
return CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>(Unused, L, R);
1608+
return CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>(L, R);
16071609
}
16081610

16091611
template <typename LHS, typename RHS>
16101612
inline CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>
16111613
m_FCmp(const LHS &L, const RHS &R) {
1612-
FCmpInst::Predicate Unused;
1613-
return CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>(Unused, L, R);
1614+
return CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>(L, R);
16141615
}
16151616

16161617
// Same as CmpClass, but instead of saving Pred as out output variable, match a
@@ -2681,9 +2682,7 @@ m_c_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
26812682
template <typename LHS, typename RHS>
26822683
inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate, true>
26832684
m_c_ICmp(const LHS &L, const RHS &R) {
2684-
ICmpInst::Predicate Unused;
2685-
return CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate, true>(Unused,
2686-
L, R);
2685+
return CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate, true>(L, R);
26872686
}
26882687

26892688
/// Matches a specific opcode with LHS and RHS in either order.

llvm/unittests/IR/PatternMatch.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -2235,7 +2235,7 @@ typedef ::testing::Types<std::tuple<Value*, Instruction*>,
22352235
MutableConstTestTypes;
22362236
TYPED_TEST_SUITE(MutableConstTest, MutableConstTestTypes, );
22372237

2238-
TYPED_TEST(MutableConstTest, /* FIXME: UAR bug */ DISABLED_ICmp) {
2238+
TYPED_TEST(MutableConstTest, ICmp) {
22392239
auto &IRB = PatternMatchTest::IRB;
22402240

22412241
typedef std::tuple_element_t<0, TypeParam> ValueType;
@@ -2319,7 +2319,7 @@ TYPED_TEST(MutableConstTest, /* FIXME: UAR bug */ DISABLED_ICmp) {
23192319
.match((InstructionType)IRB.CreateICmp(Pred, L, R)));
23202320
}
23212321

2322-
TYPED_TEST(MutableConstTest, /* FIXME: UAR bug */ DISABLED_FCmp) {
2322+
TYPED_TEST(MutableConstTest, FCmp) {
23232323
auto &IRB = PatternMatchTest::IRB;
23242324

23252325
typedef std::tuple_element_t<0, TypeParam> ValueType;

0 commit comments

Comments
 (0)