Skip to content

Commit 4d50f23

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 caa10b7 commit 4d50f23

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
@@ -1940,15 +1940,17 @@ struct m_SplatOrPoisonMask {
19401940
};
19411941

19421942
template <typename PointerOpTy, typename OffsetOpTy> struct PtrAdd_match {
1943+
const DataLayout &DL;
19431944
PointerOpTy PointerOp;
19441945
OffsetOpTy OffsetOp;
19451946

1946-
PtrAdd_match(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp)
1947-
: PointerOp(PointerOp), OffsetOp(OffsetOp) {}
1947+
PtrAdd_match(const DataLayout &DL, const PointerOpTy &PointerOp,
1948+
const OffsetOpTy &OffsetOp)
1949+
: DL(DL), PointerOp(PointerOp), OffsetOp(OffsetOp) {}
19481950

19491951
template <typename OpTy> bool match(OpTy *V) const {
19501952
auto *GEP = dyn_cast<GEPOperator>(V);
1951-
return GEP && GEP->getSourceElementType()->isIntegerTy(8) &&
1953+
return GEP && GEP->getSourceElementType()->isIntegerTy(DL.getByteWidth()) &&
19521954
PointerOp.match(GEP->getPointerOperand()) &&
19531955
OffsetOp.match(GEP->idx_begin()->get());
19541956
}
@@ -1990,8 +1992,9 @@ inline auto m_GEP(const OperandTypes &...Ops) {
19901992
/// Matches GEP with i8 source element type
19911993
template <typename PointerOpTy, typename OffsetOpTy>
19921994
inline PtrAdd_match<PointerOpTy, OffsetOpTy>
1993-
m_PtrAdd(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp) {
1994-
return PtrAdd_match<PointerOpTy, OffsetOpTy>(PointerOp, OffsetOp);
1995+
m_PtrAdd(const DataLayout &DL, const PointerOpTy &PointerOp,
1996+
const OffsetOpTy &OffsetOp) {
1997+
return PtrAdd_match<PointerOpTy, OffsetOpTy>(DL, PointerOp, OffsetOp);
19951998
}
19961999

19972000
//===----------------------------------------------------------------------===//

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
@@ -991,9 +991,10 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Instruction *I,
991991
Value *InnerPtr;
992992
uint64_t GEPIndex;
993993
uint64_t PtrMaskImmediate;
994-
if (match(I, m_Intrinsic<Intrinsic::ptrmask>(
995-
m_PtrAdd(m_Value(InnerPtr), m_ConstantInt(GEPIndex)),
996-
m_ConstantInt(PtrMaskImmediate)))) {
994+
if (match(I,
995+
m_Intrinsic<Intrinsic::ptrmask>(
996+
m_PtrAdd(DL, m_Value(InnerPtr), m_ConstantInt(GEPIndex)),
997+
m_ConstantInt(PtrMaskImmediate)))) {
997998

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

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2595,7 +2595,7 @@ static Instruction *canonicalizeGEPOfConstGEPI8(GetElementPtrInst &GEP,
25952595
auto &DL = IC.getDataLayout();
25962596
Value *Base;
25972597
const APInt *C1;
2598-
if (!match(Src, m_PtrAdd(m_Value(Base), m_APInt(C1))))
2598+
if (!match(Src, m_PtrAdd(DL, m_Value(Base), m_APInt(C1))))
25992599
return nullptr;
26002600
Value *VarIndex;
26012601
const APInt *C2;

llvm/unittests/IR/PatternMatch.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2599,26 +2599,40 @@ TEST_F(PatternMatchTest, ConstExpr) {
25992599
EXPECT_TRUE(match(V, m_ConstantExpr()));
26002600
}
26012601

2602-
TEST_F(PatternMatchTest, PtrAdd) {
2602+
// PatternMatchTest parametrized by byte width.
2603+
class PatternMatchByteParamTest
2604+
: public PatternMatchTest,
2605+
public ::testing::WithParamInterface<unsigned> {
2606+
public:
2607+
PatternMatchByteParamTest() {
2608+
M->setDataLayout("b:" + std::to_string(GetParam()));
2609+
}
2610+
};
2611+
2612+
INSTANTIATE_TEST_SUITE_P(ByteWidths, PatternMatchByteParamTest,
2613+
::testing::Values(8, 16, 32));
2614+
2615+
TEST_P(PatternMatchByteParamTest, PtrAdd) {
2616+
const DataLayout &DL = M->getDataLayout();
26032617
Type *PtrTy = PointerType::getUnqual(Ctx);
26042618
Type *IdxTy = Type::getInt64Ty(Ctx);
26052619
Constant *Null = Constant::getNullValue(PtrTy);
26062620
Constant *Offset = ConstantInt::get(IdxTy, 42);
26072621
Value *PtrAdd = IRB.CreatePtrAdd(Null, Offset);
26082622
Value *OtherGEP = IRB.CreateGEP(IdxTy, Null, Offset);
2609-
Value *PtrAddConst =
2610-
ConstantExpr::getGetElementPtr(Type::getInt8Ty(Ctx), Null, Offset);
2623+
Value *PtrAddConst = ConstantExpr::getGetElementPtr(
2624+
Type::getIntNTy(Ctx, DL.getByteWidth()), Null, Offset);
26112625

26122626
Value *A, *B;
2613-
EXPECT_TRUE(match(PtrAdd, m_PtrAdd(m_Value(A), m_Value(B))));
2627+
EXPECT_TRUE(match(PtrAdd, m_PtrAdd(DL, m_Value(A), m_Value(B))));
26142628
EXPECT_EQ(A, Null);
26152629
EXPECT_EQ(B, Offset);
26162630

2617-
EXPECT_TRUE(match(PtrAddConst, m_PtrAdd(m_Value(A), m_Value(B))));
2631+
EXPECT_TRUE(match(PtrAddConst, m_PtrAdd(DL, m_Value(A), m_Value(B))));
26182632
EXPECT_EQ(A, Null);
26192633
EXPECT_EQ(B, Offset);
26202634

2621-
EXPECT_FALSE(match(OtherGEP, m_PtrAdd(m_Value(A), m_Value(B))));
2635+
EXPECT_FALSE(match(OtherGEP, m_PtrAdd(DL, m_Value(A), m_Value(B))));
26222636
}
26232637

26242638
} // anonymous namespace.

0 commit comments

Comments
 (0)