[llvm] 3ad6359 - [PatternMatch] Add m_PtrAdd() matcher (NFC)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Thu Feb 8 06:31:47 PST 2024
Author: Nikita Popov
Date: 2024-02-08T15:31:39+01:00
New Revision: 3ad63593dac390e320808f3de0e1906c5fa45c8a
URL: https://github.com/llvm/llvm-project/commit/3ad63593dac390e320808f3de0e1906c5fa45c8a
DIFF: https://github.com/llvm/llvm-project/commit/3ad63593dac390e320808f3de0e1906c5fa45c8a.diff
LOG: [PatternMatch] Add m_PtrAdd() matcher (NFC)
This matches a getelementptr i8 instruction or constant expression,
with a given pointer operand and index.
Added:
Modified:
llvm/include/llvm/IR/PatternMatch.h
llvm/unittests/IR/PatternMatch.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/IR/PatternMatch.h b/llvm/include/llvm/IR/PatternMatch.h
index 3155e7dc38b64a..fed552414298ad 100644
--- a/llvm/include/llvm/IR/PatternMatch.h
+++ b/llvm/include/llvm/IR/PatternMatch.h
@@ -1614,6 +1614,21 @@ struct m_SplatOrUndefMask {
}
};
+template <typename PointerOpTy, typename OffsetOpTy> struct PtrAdd_match {
+ PointerOpTy PointerOp;
+ OffsetOpTy OffsetOp;
+
+ PtrAdd_match(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp)
+ : PointerOp(PointerOp), OffsetOp(OffsetOp) {}
+
+ template <typename OpTy> bool match(OpTy *V) {
+ auto *GEP = dyn_cast<GEPOperator>(V);
+ return GEP && GEP->getSourceElementType()->isIntegerTy(8) &&
+ PointerOp.match(GEP->getPointerOperand()) &&
+ OffsetOp.match(GEP->idx_begin()->get());
+ }
+};
+
/// Matches ShuffleVectorInst independently of mask value.
template <typename V1_t, typename V2_t>
inline TwoOps_match<V1_t, V2_t, Instruction::ShuffleVector>
@@ -1647,6 +1662,13 @@ inline auto m_GEP(const OperandTypes &...Ops) {
return AnyOps_match<Instruction::GetElementPtr, OperandTypes...>(Ops...);
}
+/// Matches GEP with i8 source element type
+template <typename PointerOpTy, typename OffsetOpTy>
+inline PtrAdd_match<PointerOpTy, OffsetOpTy>
+m_PtrAdd(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp) {
+ return PtrAdd_match<PointerOpTy, OffsetOpTy>(PointerOp, OffsetOp);
+}
+
//===----------------------------------------------------------------------===//
// Matchers for CastInst classes
//
diff --git a/llvm/unittests/IR/PatternMatch.cpp b/llvm/unittests/IR/PatternMatch.cpp
index 885b1346cde1eb..883149c686b42a 100644
--- a/llvm/unittests/IR/PatternMatch.cpp
+++ b/llvm/unittests/IR/PatternMatch.cpp
@@ -1889,4 +1889,26 @@ TEST_F(PatternMatchTest, ConstExpr) {
EXPECT_TRUE(match(V, m_ConstantExpr()));
}
+TEST_F(PatternMatchTest, PtrAdd) {
+ Type *PtrTy = PointerType::getUnqual(Ctx);
+ Type *IdxTy = Type::getInt64Ty(Ctx);
+ Constant *Null = Constant::getNullValue(PtrTy);
+ Constant *Offset = ConstantInt::get(IdxTy, 42);
+ Value *PtrAdd = IRB.CreatePtrAdd(Null, Offset);
+ Value *OtherGEP = IRB.CreateGEP(IdxTy, Null, Offset);
+ Value *PtrAddConst =
+ ConstantExpr::getGetElementPtr(Type::getInt8Ty(Ctx), Null, Offset);
+
+ Value *A, *B;
+ EXPECT_TRUE(match(PtrAdd, m_PtrAdd(m_Value(A), m_Value(B))));
+ EXPECT_EQ(A, Null);
+ EXPECT_EQ(B, Offset);
+
+ EXPECT_TRUE(match(PtrAddConst, m_PtrAdd(m_Value(A), m_Value(B))));
+ EXPECT_EQ(A, Null);
+ EXPECT_EQ(B, Offset);
+
+ EXPECT_FALSE(match(OtherGEP, m_PtrAdd(m_Value(A), m_Value(B))));
+}
+
} // anonymous namespace.
More information about the llvm-commits
mailing list