Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions llvm/include/llvm/IR/PatternMatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -2046,15 +2046,17 @@ struct m_SplatOrPoisonMask {
};

template <typename PointerOpTy, typename OffsetOpTy> struct PtrAdd_match {
const DataLayout &DL;
PointerOpTy PointerOp;
OffsetOpTy OffsetOp;

PtrAdd_match(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp)
: PointerOp(PointerOp), OffsetOp(OffsetOp) {}
PtrAdd_match(const DataLayout &DL, const PointerOpTy &PointerOp,
const OffsetOpTy &OffsetOp)
: DL(DL), PointerOp(PointerOp), OffsetOp(OffsetOp) {}

template <typename OpTy> bool match(OpTy *V) const {
auto *GEP = dyn_cast<GEPOperator>(V);
return GEP && GEP->getSourceElementType()->isIntegerTy(8) &&
return GEP && GEP->getSourceElementType()->isIntegerTy(DL.getByteWidth()) &&
PointerOp.match(GEP->getPointerOperand()) &&
OffsetOp.match(GEP->idx_begin()->get());
}
Expand Down Expand Up @@ -2096,8 +2098,9 @@ inline auto m_GEP(const 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);
m_PtrAdd(const DataLayout &DL, const PointerOpTy &PointerOp,
const OffsetOpTy &OffsetOp) {
return PtrAdd_match<PointerOpTy, OffsetOpTy>(DL, PointerOp, OffsetOp);
}

//===----------------------------------------------------------------------===//
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Analysis/InstructionSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5533,7 +5533,7 @@ static Value *simplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty,
Value *Ptr, *X;
if ((CastOpc == Instruction::PtrToInt || CastOpc == Instruction::PtrToAddr) &&
match(Op,
m_PtrAdd(m_Value(Ptr),
m_PtrAdd(Q.DL, m_Value(Ptr),
m_Sub(m_Value(X), m_PtrToIntOrAddr(m_Deferred(Ptr))))) &&
X->getType() == Ty && Ty == Q.DL.getIndexType(Ptr->getType()))
return X;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1052,9 +1052,10 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Instruction *I,
Value *InnerPtr;
uint64_t GEPIndex;
uint64_t PtrMaskImmediate;
if (match(I, m_Intrinsic<Intrinsic::ptrmask>(
m_PtrAdd(m_Value(InnerPtr), m_ConstantInt(GEPIndex)),
m_ConstantInt(PtrMaskImmediate)))) {
if (match(I,
m_Intrinsic<Intrinsic::ptrmask>(
m_PtrAdd(DL, m_Value(InnerPtr), m_ConstantInt(GEPIndex)),
m_ConstantInt(PtrMaskImmediate)))) {

LHSKnown = computeKnownBits(InnerPtr, I, Depth + 1);
if (!LHSKnown.isZero()) {
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2692,7 +2692,7 @@ static Instruction *canonicalizeGEPOfConstGEPI8(GetElementPtrInst &GEP,
auto &DL = IC.getDataLayout();
Value *Base;
const APInt *C1;
if (!match(Src, m_PtrAdd(m_Value(Base), m_APInt(C1))))
if (!match(Src, m_PtrAdd(DL, m_Value(Base), m_APInt(C1))))
return nullptr;
Value *VarIndex;
const APInt *C2;
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1055,7 +1055,7 @@ bool SeparateConstOffsetFromGEP::splitGEP(GetElementPtrInst *GEP) {
const APInt *BaseOffset;
const bool ExtractBase =
match(GEP->getPointerOperand(),
m_PtrAdd(m_Value(NewBase), m_APInt(BaseOffset)));
m_PtrAdd(*DL, m_Value(NewBase), m_APInt(BaseOffset)));

const int64_t BaseByteOffset = ExtractBase ? BaseOffset->getSExtValue() : 0;

Expand Down
26 changes: 20 additions & 6 deletions llvm/unittests/IR/PatternMatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2599,26 +2599,40 @@ TEST_F(PatternMatchTest, ConstExpr) {
EXPECT_TRUE(match(V, m_ConstantExpr()));
}

TEST_F(PatternMatchTest, PtrAdd) {
// PatternMatchTest parametrized by byte width.
class PatternMatchByteParamTest
: public PatternMatchTest,
public ::testing::WithParamInterface<unsigned> {
public:
PatternMatchByteParamTest() {
M->setDataLayout("b:" + std::to_string(GetParam()));
}
};

INSTANTIATE_TEST_SUITE_P(ByteWidths, PatternMatchByteParamTest,
::testing::Values(8, 16, 32));

TEST_P(PatternMatchByteParamTest, PtrAdd) {
const DataLayout &DL = M->getDataLayout();
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 *PtrAddConst = ConstantExpr::getGetElementPtr(
Type::getIntNTy(Ctx, DL.getByteWidth()), Null, Offset);

Value *A, *B;
EXPECT_TRUE(match(PtrAdd, m_PtrAdd(m_Value(A), m_Value(B))));
EXPECT_TRUE(match(PtrAdd, m_PtrAdd(DL, 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_TRUE(match(PtrAddConst, m_PtrAdd(DL, 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))));
EXPECT_FALSE(match(OtherGEP, m_PtrAdd(DL, m_Value(A), m_Value(B))));
}

TEST_F(PatternMatchTest, ShiftOrSelf) {
Expand Down
Loading