Skip to content

Commit 856e1f0

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 29393ec commit 856e1f0

File tree

6 files changed

+35
-17
lines changed

6 files changed

+35
-17
lines changed

llvm/include/llvm/IR/PatternMatch.h

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

20482048
template <typename PointerOpTy, typename OffsetOpTy> struct PtrAdd_match {
2049+
const DataLayout &DL;
20492050
PointerOpTy PointerOp;
20502051
OffsetOpTy OffsetOp;
20512052

2052-
PtrAdd_match(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp)
2053-
: PointerOp(PointerOp), OffsetOp(OffsetOp) {}
2053+
PtrAdd_match(const DataLayout &DL, const PointerOpTy &PointerOp,
2054+
const OffsetOpTy &OffsetOp)
2055+
: DL(DL), PointerOp(PointerOp), OffsetOp(OffsetOp) {}
20542056

20552057
template <typename OpTy> bool match(OpTy *V) const {
20562058
auto *GEP = dyn_cast<GEPOperator>(V);
2057-
return GEP && GEP->getSourceElementType()->isIntegerTy(8) &&
2059+
return GEP && GEP->getSourceElementType()->isIntegerTy(DL.getByteWidth()) &&
20582060
PointerOp.match(GEP->getPointerOperand()) &&
20592061
OffsetOp.match(GEP->idx_begin()->get());
20602062
}
@@ -2096,8 +2098,9 @@ inline auto m_GEP(const OperandTypes &...Ops) {
20962098
/// Matches GEP with i8 source element type
20972099
template <typename PointerOpTy, typename OffsetOpTy>
20982100
inline PtrAdd_match<PointerOpTy, OffsetOpTy>
2099-
m_PtrAdd(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp) {
2100-
return PtrAdd_match<PointerOpTy, OffsetOpTy>(PointerOp, OffsetOp);
2101+
m_PtrAdd(const DataLayout &DL, const PointerOpTy &PointerOp,
2102+
const OffsetOpTy &OffsetOp) {
2103+
return PtrAdd_match<PointerOpTy, OffsetOpTy>(DL, PointerOp, OffsetOp);
21012104
}
21022105

21032106
//===----------------------------------------------------------------------===//

llvm/lib/Analysis/InstructionSimplify.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5533,7 +5533,7 @@ static Value *simplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty,
55335533
Value *Ptr, *X;
55345534
if ((CastOpc == Instruction::PtrToInt || CastOpc == Instruction::PtrToAddr) &&
55355535
match(Op,
5536-
m_PtrAdd(m_Value(Ptr),
5536+
m_PtrAdd(Q.DL, m_Value(Ptr),
55375537
m_Sub(m_Value(X), m_PtrToIntOrAddr(m_Deferred(Ptr))))) &&
55385538
X->getType() == Ty && Ty == Q.DL.getIndexType(Ptr->getType()))
55395539
return X;

llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,9 +1052,10 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Instruction *I,
10521052
Value *InnerPtr;
10531053
uint64_t GEPIndex;
10541054
uint64_t PtrMaskImmediate;
1055-
if (match(I, m_Intrinsic<Intrinsic::ptrmask>(
1056-
m_PtrAdd(m_Value(InnerPtr), m_ConstantInt(GEPIndex)),
1057-
m_ConstantInt(PtrMaskImmediate)))) {
1055+
if (match(I,
1056+
m_Intrinsic<Intrinsic::ptrmask>(
1057+
m_PtrAdd(DL, m_Value(InnerPtr), m_ConstantInt(GEPIndex)),
1058+
m_ConstantInt(PtrMaskImmediate)))) {
10581059

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

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2692,7 +2692,7 @@ static Instruction *canonicalizeGEPOfConstGEPI8(GetElementPtrInst &GEP,
26922692
auto &DL = IC.getDataLayout();
26932693
Value *Base;
26942694
const APInt *C1;
2695-
if (!match(Src, m_PtrAdd(m_Value(Base), m_APInt(C1))))
2695+
if (!match(Src, m_PtrAdd(DL, m_Value(Base), m_APInt(C1))))
26962696
return nullptr;
26972697
Value *VarIndex;
26982698
const APInt *C2;

llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1055,7 +1055,7 @@ bool SeparateConstOffsetFromGEP::splitGEP(GetElementPtrInst *GEP) {
10551055
const APInt *BaseOffset;
10561056
const bool ExtractBase =
10571057
match(GEP->getPointerOperand(),
1058-
m_PtrAdd(m_Value(NewBase), m_APInt(BaseOffset)));
1058+
m_PtrAdd(*DL, m_Value(NewBase), m_APInt(BaseOffset)));
10591059

10601060
const int64_t BaseByteOffset = ExtractBase ? BaseOffset->getSExtValue() : 0;
10611061

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
TEST_F(PatternMatchTest, ShiftOrSelf) {

0 commit comments

Comments
 (0)