Skip to content

Commit 7cca421

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 2a28c47 commit 7cca421

File tree

5 files changed

+33
-16
lines changed

5 files changed

+33
-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
@@ -5373,7 +5373,7 @@ static Value *simplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty,
53735373
// ptrtoint (ptradd (Ptr, X - ptrtoint(Ptr))) -> X
53745374
Value *Ptr, *X;
53755375
if (CastOpc == Instruction::PtrToInt &&
5376-
match(Op, m_PtrAdd(m_Value(Ptr),
5376+
match(Op, m_PtrAdd(Q.DL, m_Value(Ptr),
53775377
m_Sub(m_Value(X), m_PtrToInt(m_Deferred(Ptr))))) &&
53785378
X->getType() == Ty && Ty == Q.DL.getIndexType(Ptr->getType()))
53795379
return X;

llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -987,9 +987,10 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Instruction *I,
987987
Value *InnerPtr;
988988
uint64_t GEPIndex;
989989
uint64_t PtrMaskImmediate;
990-
if (match(I, m_Intrinsic<Intrinsic::ptrmask>(
991-
m_PtrAdd(m_Value(InnerPtr), m_ConstantInt(GEPIndex)),
992-
m_ConstantInt(PtrMaskImmediate)))) {
990+
if (match(I,
991+
m_Intrinsic<Intrinsic::ptrmask>(
992+
m_PtrAdd(DL, m_Value(InnerPtr), m_ConstantInt(GEPIndex)),
993+
m_ConstantInt(PtrMaskImmediate)))) {
993994

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

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2350,7 +2350,7 @@ static Instruction *canonicalizeGEPOfConstGEPI8(GetElementPtrInst &GEP,
23502350
auto &DL = IC.getDataLayout();
23512351
Value *Base;
23522352
const APInt *C1;
2353-
if (!match(Src, m_PtrAdd(m_Value(Base), m_APInt(C1))))
2353+
if (!match(Src, m_PtrAdd(DL, m_Value(Base), m_APInt(C1))))
23542354
return nullptr;
23552355
Value *VarIndex;
23562356
const APInt *C2;

llvm/unittests/IR/PatternMatch.cpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2419,26 +2419,39 @@ TEST_F(PatternMatchTest, ConstExpr) {
24192419
EXPECT_TRUE(match(V, m_ConstantExpr()));
24202420
}
24212421

2422-
TEST_F(PatternMatchTest, PtrAdd) {
2422+
// PatternMatchTest parametrized by byte width.
2423+
class PatternMatchByteParamTest : public PatternMatchTest,
2424+
public ::testing::WithParamInterface<unsigned> {
2425+
public:
2426+
PatternMatchByteParamTest() {
2427+
M->setDataLayout("b:" + std::to_string(GetParam()));
2428+
}
2429+
};
2430+
2431+
INSTANTIATE_TEST_SUITE_P(ByteWidths, PatternMatchByteParamTest,
2432+
::testing::Values(8, 16, 32));
2433+
2434+
TEST_P(PatternMatchByteParamTest, PtrAdd) {
2435+
const DataLayout &DL = M->getDataLayout();
24232436
Type *PtrTy = PointerType::getUnqual(Ctx);
24242437
Type *IdxTy = Type::getInt64Ty(Ctx);
24252438
Constant *Null = Constant::getNullValue(PtrTy);
24262439
Constant *Offset = ConstantInt::get(IdxTy, 42);
24272440
Value *PtrAdd = IRB.CreatePtrAdd(Null, Offset);
24282441
Value *OtherGEP = IRB.CreateGEP(IdxTy, Null, Offset);
2429-
Value *PtrAddConst =
2430-
ConstantExpr::getGetElementPtr(Type::getInt8Ty(Ctx), Null, Offset);
2442+
Value *PtrAddConst = ConstantExpr::getGetElementPtr(
2443+
Type::getIntNTy(Ctx, DL.getByteWidth()), Null, Offset);
24312444

24322445
Value *A, *B;
2433-
EXPECT_TRUE(match(PtrAdd, m_PtrAdd(m_Value(A), m_Value(B))));
2446+
EXPECT_TRUE(match(PtrAdd, m_PtrAdd(DL, m_Value(A), m_Value(B))));
24342447
EXPECT_EQ(A, Null);
24352448
EXPECT_EQ(B, Offset);
24362449

2437-
EXPECT_TRUE(match(PtrAddConst, m_PtrAdd(m_Value(A), m_Value(B))));
2450+
EXPECT_TRUE(match(PtrAddConst, m_PtrAdd(DL, m_Value(A), m_Value(B))));
24382451
EXPECT_EQ(A, Null);
24392452
EXPECT_EQ(B, Offset);
24402453

2441-
EXPECT_FALSE(match(OtherGEP, m_PtrAdd(m_Value(A), m_Value(B))));
2454+
EXPECT_FALSE(match(OtherGEP, m_PtrAdd(DL, m_Value(A), m_Value(B))));
24422455
}
24432456

24442457
} // anonymous namespace.

0 commit comments

Comments
 (0)