Skip to content

Commit 26dc400

Browse files
committed
[IR] Account for byte width in m_PtrAdd
The method has few uses yet, so just pass DL argument to it. The change follows m_PtrToIntSameSize, and I don't see a better way of delivering the byte width to the method.
1 parent 6d95f98 commit 26dc400

File tree

5 files changed

+34
-16
lines changed

5 files changed

+34
-16
lines changed

llvm/include/llvm/IR/PatternMatch.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1891,15 +1891,17 @@ struct m_SplatOrPoisonMask {
18911891
};
18921892

18931893
template <typename PointerOpTy, typename OffsetOpTy> struct PtrAdd_match {
1894+
const DataLayout &DL;
18941895
PointerOpTy PointerOp;
18951896
OffsetOpTy OffsetOp;
18961897

1897-
PtrAdd_match(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp)
1898-
: PointerOp(PointerOp), OffsetOp(OffsetOp) {}
1898+
PtrAdd_match(const DataLayout &DL, const PointerOpTy &PointerOp,
1899+
const OffsetOpTy &OffsetOp)
1900+
: DL(DL), PointerOp(PointerOp), OffsetOp(OffsetOp) {}
18991901

19001902
template <typename OpTy> bool match(OpTy *V) {
19011903
auto *GEP = dyn_cast<GEPOperator>(V);
1902-
return GEP && GEP->getSourceElementType()->isIntegerTy(8) &&
1904+
return GEP && GEP->getSourceElementType()->isIntegerTy(DL.getByteWidth()) &&
19031905
PointerOp.match(GEP->getPointerOperand()) &&
19041906
OffsetOp.match(GEP->idx_begin()->get());
19051907
}
@@ -1941,8 +1943,9 @@ inline auto m_GEP(const OperandTypes &...Ops) {
19411943
/// Matches GEP with i8 source element type
19421944
template <typename PointerOpTy, typename OffsetOpTy>
19431945
inline PtrAdd_match<PointerOpTy, OffsetOpTy>
1944-
m_PtrAdd(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp) {
1945-
return PtrAdd_match<PointerOpTy, OffsetOpTy>(PointerOp, OffsetOp);
1946+
m_PtrAdd(const DataLayout &DL, const PointerOpTy &PointerOp,
1947+
const OffsetOpTy &OffsetOp) {
1948+
return PtrAdd_match<PointerOpTy, OffsetOpTy>(DL, PointerOp, OffsetOp);
19461949
}
19471950

19481951
//===----------------------------------------------------------------------===//

llvm/lib/Analysis/InstructionSimplify.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5390,7 +5390,7 @@ static Value *simplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty,
53905390
// ptrtoint (ptradd (Ptr, X - ptrtoint(Ptr))) -> X
53915391
Value *Ptr, *X;
53925392
if (CastOpc == Instruction::PtrToInt &&
5393-
match(Op, m_PtrAdd(m_Value(Ptr),
5393+
match(Op, m_PtrAdd(Q.DL, m_Value(Ptr),
53945394
m_Sub(m_Value(X), m_PtrToInt(m_Deferred(Ptr))))) &&
53955395
X->getType() == Ty && Ty == Q.DL.getIndexType(Ptr->getType()))
53965396
return X;

llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -990,9 +990,10 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Instruction *I,
990990
Value *InnerPtr;
991991
uint64_t GEPIndex;
992992
uint64_t PtrMaskImmediate;
993-
if (match(I, m_Intrinsic<Intrinsic::ptrmask>(
994-
m_PtrAdd(m_Value(InnerPtr), m_ConstantInt(GEPIndex)),
995-
m_ConstantInt(PtrMaskImmediate)))) {
993+
if (match(I,
994+
m_Intrinsic<Intrinsic::ptrmask>(
995+
m_PtrAdd(DL, m_Value(InnerPtr), m_ConstantInt(GEPIndex)),
996+
m_ConstantInt(PtrMaskImmediate)))) {
996997

997998
LHSKnown = computeKnownBits(InnerPtr, Depth + 1, I);
998999
if (!LHSKnown.isZero()) {

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2418,7 +2418,7 @@ static Instruction *canonicalizeGEPOfConstGEPI8(GetElementPtrInst &GEP,
24182418
auto &DL = IC.getDataLayout();
24192419
Value *Base;
24202420
const APInt *C1;
2421-
if (!match(Src, m_PtrAdd(m_Value(Base), m_APInt(C1))))
2421+
if (!match(Src, m_PtrAdd(DL, m_Value(Base), m_APInt(C1))))
24222422
return nullptr;
24232423
Value *VarIndex;
24242424
const APInt *C2;

llvm/unittests/IR/PatternMatch.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2618,26 +2618,40 @@ TEST_F(PatternMatchTest, ConstExpr) {
26182618
EXPECT_TRUE(match(V, m_ConstantExpr()));
26192619
}
26202620

2621-
TEST_F(PatternMatchTest, PtrAdd) {
2621+
// PatternMatchTest parametrized by byte width.
2622+
class PatternMatchByteParamTest
2623+
: public PatternMatchTest,
2624+
public ::testing::WithParamInterface<unsigned> {
2625+
public:
2626+
PatternMatchByteParamTest() {
2627+
M->setDataLayout("b:" + std::to_string(GetParam()));
2628+
}
2629+
};
2630+
2631+
INSTANTIATE_TEST_SUITE_P(ByteWidths, PatternMatchByteParamTest,
2632+
::testing::Values(8, 16, 32));
2633+
2634+
TEST_P(PatternMatchByteParamTest, PtrAdd) {
2635+
const DataLayout &DL = M->getDataLayout();
26222636
Type *PtrTy = PointerType::getUnqual(Ctx);
26232637
Type *IdxTy = Type::getInt64Ty(Ctx);
26242638
Constant *Null = Constant::getNullValue(PtrTy);
26252639
Constant *Offset = ConstantInt::get(IdxTy, 42);
26262640
Value *PtrAdd = IRB.CreatePtrAdd(Null, Offset);
26272641
Value *OtherGEP = IRB.CreateGEP(IdxTy, Null, Offset);
2628-
Value *PtrAddConst =
2629-
ConstantExpr::getGetElementPtr(Type::getInt8Ty(Ctx), Null, Offset);
2642+
Value *PtrAddConst = ConstantExpr::getGetElementPtr(
2643+
Type::getIntNTy(Ctx, DL.getByteWidth()), Null, Offset);
26302644

26312645
Value *A, *B;
2632-
EXPECT_TRUE(match(PtrAdd, m_PtrAdd(m_Value(A), m_Value(B))));
2646+
EXPECT_TRUE(match(PtrAdd, m_PtrAdd(DL, m_Value(A), m_Value(B))));
26332647
EXPECT_EQ(A, Null);
26342648
EXPECT_EQ(B, Offset);
26352649

2636-
EXPECT_TRUE(match(PtrAddConst, m_PtrAdd(m_Value(A), m_Value(B))));
2650+
EXPECT_TRUE(match(PtrAddConst, m_PtrAdd(DL, m_Value(A), m_Value(B))));
26372651
EXPECT_EQ(A, Null);
26382652
EXPECT_EQ(B, Offset);
26392653

2640-
EXPECT_FALSE(match(OtherGEP, m_PtrAdd(m_Value(A), m_Value(B))));
2654+
EXPECT_FALSE(match(OtherGEP, m_PtrAdd(DL, m_Value(A), m_Value(B))));
26412655
}
26422656

26432657
} // anonymous namespace.

0 commit comments

Comments
 (0)