Skip to content

Commit e75ad5f

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 b8c2041 commit e75ad5f

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
@@ -1924,15 +1924,17 @@ struct m_SplatOrPoisonMask {
19241924
};
19251925

19261926
template <typename PointerOpTy, typename OffsetOpTy> struct PtrAdd_match {
1927+
const DataLayout &DL;
19271928
PointerOpTy PointerOp;
19281929
OffsetOpTy OffsetOp;
19291930

1930-
PtrAdd_match(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp)
1931-
: PointerOp(PointerOp), OffsetOp(OffsetOp) {}
1931+
PtrAdd_match(const DataLayout &DL, const PointerOpTy &PointerOp,
1932+
const OffsetOpTy &OffsetOp)
1933+
: DL(DL), PointerOp(PointerOp), OffsetOp(OffsetOp) {}
19321934

19331935
template <typename OpTy> bool match(OpTy *V) {
19341936
auto *GEP = dyn_cast<GEPOperator>(V);
1935-
return GEP && GEP->getSourceElementType()->isIntegerTy(8) &&
1937+
return GEP && GEP->getSourceElementType()->isIntegerTy(DL.getByteWidth()) &&
19361938
PointerOp.match(GEP->getPointerOperand()) &&
19371939
OffsetOp.match(GEP->idx_begin()->get());
19381940
}
@@ -1974,8 +1976,9 @@ inline auto m_GEP(const OperandTypes &...Ops) {
19741976
/// Matches GEP with i8 source element type
19751977
template <typename PointerOpTy, typename OffsetOpTy>
19761978
inline PtrAdd_match<PointerOpTy, OffsetOpTy>
1977-
m_PtrAdd(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp) {
1978-
return PtrAdd_match<PointerOpTy, OffsetOpTy>(PointerOp, OffsetOp);
1979+
m_PtrAdd(const DataLayout &DL, const PointerOpTy &PointerOp,
1980+
const OffsetOpTy &OffsetOp) {
1981+
return PtrAdd_match<PointerOpTy, OffsetOpTy>(DL, PointerOp, OffsetOp);
19791982
}
19801983

19811984
//===----------------------------------------------------------------------===//

llvm/lib/Analysis/InstructionSimplify.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5389,7 +5389,7 @@ static Value *simplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty,
53895389
// ptrtoint (ptradd (Ptr, X - ptrtoint(Ptr))) -> X
53905390
Value *Ptr, *X;
53915391
if (CastOpc == Instruction::PtrToInt &&
5392-
match(Op, m_PtrAdd(m_Value(Ptr),
5392+
match(Op, m_PtrAdd(Q.DL, m_Value(Ptr),
53935393
m_Sub(m_Value(X), m_PtrToInt(m_Deferred(Ptr))))) &&
53945394
X->getType() == Ty && Ty == Q.DL.getIndexType(Ptr->getType()))
53955395
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
@@ -2419,7 +2419,7 @@ static Instruction *canonicalizeGEPOfConstGEPI8(GetElementPtrInst &GEP,
24192419
auto &DL = IC.getDataLayout();
24202420
Value *Base;
24212421
const APInt *C1;
2422-
if (!match(Src, m_PtrAdd(m_Value(Base), m_APInt(C1))))
2422+
if (!match(Src, m_PtrAdd(DL, m_Value(Base), m_APInt(C1))))
24232423
return nullptr;
24242424
Value *VarIndex;
24252425
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)