From 0f4383faa75fdeaeebe0c5156f927e9f88d61d53 Mon Sep 17 00:00:00 2001 From: Hideto Ueno Date: Wed, 27 Nov 2019 14:41:12 +0000 Subject: [PATCH] [Attributor] Handle special case when offset equals zero in nonnull deduction --- llvm/lib/Transforms/IPO/Attributor.cpp | 24 ++++++++++++++++++------ llvm/test/Transforms/FunctionAttrs/align.ll | 6 ++---- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/llvm/lib/Transforms/IPO/Attributor.cpp b/llvm/lib/Transforms/IPO/Attributor.cpp index 366c347..faf0cdf 100644 --- a/llvm/lib/Transforms/IPO/Attributor.cpp +++ b/llvm/lib/Transforms/IPO/Attributor.cpp @@ -308,15 +308,16 @@ static const Value *getPointerOperand(const Instruction *I) { return nullptr; } -static const Value *getBasePointerOfAccessPointerOperand(const Instruction *I, - int64_t &BytesOffset, - const DataLayout &DL) { +static const Value * +getBasePointerOfAccessPointerOperand(const Instruction *I, int64_t &BytesOffset, + const DataLayout &DL, + bool AllowNonInbounds = false) { const Value *Ptr = getPointerOperand(I); if (!Ptr) return nullptr; return GetPointerBaseWithConstantOffset(Ptr, BytesOffset, DL, - /*AllowNonInbounds*/ false); + AllowNonInbounds); } ChangeStatus AbstractAttribute::update(Attributor &A) { @@ -1702,8 +1703,7 @@ static int64_t getKnownNonNullAndDerefBytesForUse( return 0; } if (auto *GEP = dyn_cast(I)) - if (GEP->hasAllZeroIndices() || - (GEP->isInBounds() && GEP->hasAllConstantIndices())) { + if (GEP->hasAllConstantIndices()) { TrackUse = true; return 0; } @@ -1718,6 +1718,18 @@ static int64_t getKnownNonNullAndDerefBytesForUse( return std::max(int64_t(0), DerefBytes); } } + + /// Corner case when an offset is 0. + if (const Value *Base = getBasePointerOfAccessPointerOperand( + I, Offset, DL, /*AllowNonInbounds*/ true)) { + if (Offset == 0 && Base == &AssociatedValue && + getPointerOperand(I) == UseV) { + int64_t DerefBytes = + (int64_t)DL.getTypeStoreSize(PtrTy->getPointerElementType()); + IsNonNull |= !NullPointerIsDefined; + return std::max(int64_t(0), DerefBytes); + } + } if (const Value *Base = GetPointerBaseWithConstantOffset(UseV, Offset, DL, /*AllowNonInbounds*/ false)) { diff --git a/llvm/test/Transforms/FunctionAttrs/align.ll b/llvm/test/Transforms/FunctionAttrs/align.ll index b8817a4..a5bf919 100644 --- a/llvm/test/Transforms/FunctionAttrs/align.ll +++ b/llvm/test/Transforms/FunctionAttrs/align.ll @@ -351,8 +351,7 @@ define i64 @test12-1(i32* align 4 %p) { ret i64 %ret } -; FXIME: %p should have nonnull -; ATTRIBUTOR: define i64 @test12-2(i32* nocapture nofree readonly align 16 %p) +; ATTRIBUTOR: define i64 @test12-2(i32* nocapture nofree nonnull readonly align 16 dereferenceable(8) %p) define i64 @test12-2(i32* align 4 %p) { %p-cast = bitcast i32* %p to i64* %arrayidx0 = getelementptr i64, i64* %p-cast, i64 0 @@ -370,8 +369,7 @@ define void @test12-3(i32* align 4 %p) { ret void } -; FXIME: %p should have nonnull -; ATTRIBUTOR: define void @test12-4(i32* nocapture nofree writeonly align 16 %p) +; ATTRIBUTOR: define void @test12-4(i32* nocapture nofree nonnull writeonly align 16 dereferenceable(8) %p) define void @test12-4(i32* align 4 %p) { %p-cast = bitcast i32* %p to i64* %arrayidx0 = getelementptr i64, i64* %p-cast, i64 0 -- 2.7.4