Skip to content

Commit 222a3b7

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 4d3f0b6 commit 222a3b7

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
@@ -1845,15 +1845,17 @@ struct m_SplatOrPoisonMask {
18451845
};
18461846

18471847
template <typename PointerOpTy, typename OffsetOpTy> struct PtrAdd_match {
1848+
const DataLayout &DL;
18481849
PointerOpTy PointerOp;
18491850
OffsetOpTy OffsetOp;
18501851

1851-
PtrAdd_match(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp)
1852-
: PointerOp(PointerOp), OffsetOp(OffsetOp) {}
1852+
PtrAdd_match(const DataLayout &DL, const PointerOpTy &PointerOp,
1853+
const OffsetOpTy &OffsetOp)
1854+
: DL(DL), PointerOp(PointerOp), OffsetOp(OffsetOp) {}
18531855

18541856
template <typename OpTy> bool match(OpTy *V) {
18551857
auto *GEP = dyn_cast<GEPOperator>(V);
1856-
return GEP && GEP->getSourceElementType()->isIntegerTy(8) &&
1858+
return GEP && GEP->getSourceElementType()->isIntegerTy(DL.getByteWidth()) &&
18571859
PointerOp.match(GEP->getPointerOperand()) &&
18581860
OffsetOp.match(GEP->idx_begin()->get());
18591861
}
@@ -1895,8 +1897,9 @@ inline auto m_GEP(const OperandTypes &...Ops) {
18951897
/// Matches GEP with i8 source element type
18961898
template <typename PointerOpTy, typename OffsetOpTy>
18971899
inline PtrAdd_match<PointerOpTy, OffsetOpTy>
1898-
m_PtrAdd(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp) {
1899-
return PtrAdd_match<PointerOpTy, OffsetOpTy>(PointerOp, OffsetOp);
1900+
m_PtrAdd(const DataLayout &DL, const PointerOpTy &PointerOp,
1901+
const OffsetOpTy &OffsetOp) {
1902+
return PtrAdd_match<PointerOpTy, OffsetOpTy>(DL, PointerOp, OffsetOp);
19001903
}
19011904

19021905
//===----------------------------------------------------------------------===//

llvm/lib/Analysis/InstructionSimplify.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5368,7 +5368,7 @@ static Value *simplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty,
53685368
// ptrtoint (ptradd (Ptr, X - ptrtoint(Ptr))) -> X
53695369
Value *Ptr, *X;
53705370
if (CastOpc == Instruction::PtrToInt &&
5371-
match(Op, m_PtrAdd(m_Value(Ptr),
5371+
match(Op, m_PtrAdd(Q.DL, m_Value(Ptr),
53725372
m_Sub(m_Value(X), m_PtrToInt(m_Deferred(Ptr))))) &&
53735373
X->getType() == Ty && Ty == Q.DL.getIndexType(Ptr->getType()))
53745374
return X;

llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -975,9 +975,10 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Instruction *I,
975975
Value *InnerPtr;
976976
uint64_t GEPIndex;
977977
uint64_t PtrMaskImmediate;
978-
if (match(I, m_Intrinsic<Intrinsic::ptrmask>(
979-
m_PtrAdd(m_Value(InnerPtr), m_ConstantInt(GEPIndex)),
980-
m_ConstantInt(PtrMaskImmediate)))) {
978+
if (match(I,
979+
m_Intrinsic<Intrinsic::ptrmask>(
980+
m_PtrAdd(DL, m_Value(InnerPtr), m_ConstantInt(GEPIndex)),
981+
m_ConstantInt(PtrMaskImmediate)))) {
981982

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

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2389,7 +2389,7 @@ static Instruction *canonicalizeGEPOfConstGEPI8(GetElementPtrInst &GEP,
23892389
auto &DL = IC.getDataLayout();
23902390
Value *Base;
23912391
const APInt *C1;
2392-
if (!match(Src, m_PtrAdd(m_Value(Base), m_APInt(C1))))
2392+
if (!match(Src, m_PtrAdd(DL, m_Value(Base), m_APInt(C1))))
23932393
return nullptr;
23942394
Value *VarIndex;
23952395
const APInt *C2;

llvm/unittests/IR/PatternMatch.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2553,26 +2553,40 @@ TEST_F(PatternMatchTest, ConstExpr) {
25532553
EXPECT_TRUE(match(V, m_ConstantExpr()));
25542554
}
25552555

2556-
TEST_F(PatternMatchTest, PtrAdd) {
2556+
// PatternMatchTest parametrized by byte width.
2557+
class PatternMatchByteParamTest
2558+
: public PatternMatchTest,
2559+
public ::testing::WithParamInterface<unsigned> {
2560+
public:
2561+
PatternMatchByteParamTest() {
2562+
M->setDataLayout("b:" + std::to_string(GetParam()));
2563+
}
2564+
};
2565+
2566+
INSTANTIATE_TEST_SUITE_P(ByteWidths, PatternMatchByteParamTest,
2567+
::testing::Values(8, 16, 32));
2568+
2569+
TEST_P(PatternMatchByteParamTest, PtrAdd) {
2570+
const DataLayout &DL = M->getDataLayout();
25572571
Type *PtrTy = PointerType::getUnqual(Ctx);
25582572
Type *IdxTy = Type::getInt64Ty(Ctx);
25592573
Constant *Null = Constant::getNullValue(PtrTy);
25602574
Constant *Offset = ConstantInt::get(IdxTy, 42);
25612575
Value *PtrAdd = IRB.CreatePtrAdd(Null, Offset);
25622576
Value *OtherGEP = IRB.CreateGEP(IdxTy, Null, Offset);
2563-
Value *PtrAddConst =
2564-
ConstantExpr::getGetElementPtr(Type::getInt8Ty(Ctx), Null, Offset);
2577+
Value *PtrAddConst = ConstantExpr::getGetElementPtr(
2578+
Type::getIntNTy(Ctx, DL.getByteWidth()), Null, Offset);
25652579

25662580
Value *A, *B;
2567-
EXPECT_TRUE(match(PtrAdd, m_PtrAdd(m_Value(A), m_Value(B))));
2581+
EXPECT_TRUE(match(PtrAdd, m_PtrAdd(DL, m_Value(A), m_Value(B))));
25682582
EXPECT_EQ(A, Null);
25692583
EXPECT_EQ(B, Offset);
25702584

2571-
EXPECT_TRUE(match(PtrAddConst, m_PtrAdd(m_Value(A), m_Value(B))));
2585+
EXPECT_TRUE(match(PtrAddConst, m_PtrAdd(DL, m_Value(A), m_Value(B))));
25722586
EXPECT_EQ(A, Null);
25732587
EXPECT_EQ(B, Offset);
25742588

2575-
EXPECT_FALSE(match(OtherGEP, m_PtrAdd(m_Value(A), m_Value(B))));
2589+
EXPECT_FALSE(match(OtherGEP, m_PtrAdd(DL, m_Value(A), m_Value(B))));
25762590
}
25772591

25782592
} // anonymous namespace.

0 commit comments

Comments
 (0)