diff options
author | Artur Pilipenko <apilipenko@azulsystems.com> | 2016-04-27 12:51:01 +0000 |
---|---|---|
committer | Artur Pilipenko <apilipenko@azulsystems.com> | 2016-04-27 12:51:01 +0000 |
commit | 345f01481ba3e7ea60fc0459e32f29240e00a7be (patch) | |
tree | be06f9ab94c95f11ea3bd4de128491cbb527fa2e /llvm | |
parent | 8ab2803b6323d081e29cc4f37cc90e48d4c09be3 (diff) | |
download | bcm5719-llvm-345f01481ba3e7ea60fc0459e32f29240e00a7be.tar.gz bcm5719-llvm-345f01481ba3e7ea60fc0459e32f29240e00a7be.zip |
NFC. Introduce Value::getPointerDerferecnceableBytes
Extract a part of isDereferenceableAndAlignedPointer functionality to Value::getPointerDerferecnceableBytes. Currently it's a NFC, but in future I'm going to accumulate all the logic about value dereferenceability in this function similarly to Value::getPointerAlignment function (D16144).
Reviewed By: reames
Differential Revision: http://reviews.llvm.org/D17572
llvm-svn: 267708
Diffstat (limited to 'llvm')
-rw-r--r-- | llvm/include/llvm/IR/Value.h | 7 | ||||
-rw-r--r-- | llvm/lib/Analysis/Loads.cpp | 29 | ||||
-rw-r--r-- | llvm/lib/IR/Value.cpp | 34 |
3 files changed, 43 insertions, 27 deletions
diff --git a/llvm/include/llvm/IR/Value.h b/llvm/include/llvm/IR/Value.h index cdbd4ecced2..39ad9d8145c 100644 --- a/llvm/include/llvm/IR/Value.h +++ b/llvm/include/llvm/IR/Value.h @@ -504,6 +504,13 @@ public: return const_cast<Value*>(this)->stripInBoundsOffsets(); } + /// \brief Returns the number of bytes known to be dereferenceable for the + /// pointer value. + /// + /// If CanBeNull is set by this function the pointer can either be null or be + /// dereferenceable up to the returned number of bytes. + unsigned getPointerDereferenceableBytes(bool &CanBeNull) const; + /// \brief Returns an alignment of the pointer value. /// /// Returns an alignment which is either specified explicitly, e.g. via diff --git a/llvm/lib/Analysis/Loads.cpp b/llvm/lib/Analysis/Loads.cpp index 445e577f805..58a8723626b 100644 --- a/llvm/lib/Analysis/Loads.cpp +++ b/llvm/lib/Analysis/Loads.cpp @@ -33,34 +33,9 @@ static bool isDereferenceableFromAttribute(const Value *BV, APInt Offset, assert(Offset.isNonNegative() && "offset can't be negative"); assert(Ty->isSized() && "must be sized"); - APInt DerefBytes(Offset.getBitWidth(), 0); bool CheckForNonNull = false; - if (const Argument *A = dyn_cast<Argument>(BV)) { - DerefBytes = A->getDereferenceableBytes(); - if (!DerefBytes.getBoolValue()) { - DerefBytes = A->getDereferenceableOrNullBytes(); - CheckForNonNull = true; - } - } else if (auto CS = ImmutableCallSite(BV)) { - DerefBytes = CS.getDereferenceableBytes(0); - if (!DerefBytes.getBoolValue()) { - DerefBytes = CS.getDereferenceableOrNullBytes(0); - CheckForNonNull = true; - } - } else if (const LoadInst *LI = dyn_cast<LoadInst>(BV)) { - if (MDNode *MD = LI->getMetadata(LLVMContext::MD_dereferenceable)) { - ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0)); - DerefBytes = CI->getLimitedValue(); - } - if (!DerefBytes.getBoolValue()) { - if (MDNode *MD = - LI->getMetadata(LLVMContext::MD_dereferenceable_or_null)) { - ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0)); - DerefBytes = CI->getLimitedValue(); - } - CheckForNonNull = true; - } - } + APInt DerefBytes(Offset.getBitWidth(), + BV->getPointerDereferenceableBytes(CheckForNonNull)); if (DerefBytes.getBoolValue()) if (DerefBytes.uge(Offset + DL.getTypeStoreSize(Ty))) diff --git a/llvm/lib/IR/Value.cpp b/llvm/lib/IR/Value.cpp index 08b4936e702..fb8d00f5657 100644 --- a/llvm/lib/IR/Value.cpp +++ b/llvm/lib/IR/Value.cpp @@ -525,6 +525,40 @@ Value *Value::stripInBoundsOffsets() { return stripPointerCastsAndOffsets<PSK_InBounds>(this); } +unsigned Value::getPointerDereferenceableBytes(bool &CanBeNull) const { + assert(getType()->isPointerTy() && "must be pointer"); + + unsigned DerefBytes = 0; + CanBeNull = false; + if (const Argument *A = dyn_cast<Argument>(this)) { + DerefBytes = A->getDereferenceableBytes(); + if (DerefBytes == 0) { + DerefBytes = A->getDereferenceableOrNullBytes(); + CanBeNull = true; + } + } else if (auto CS = ImmutableCallSite(this)) { + DerefBytes = CS.getDereferenceableBytes(0); + if (DerefBytes == 0) { + DerefBytes = CS.getDereferenceableOrNullBytes(0); + CanBeNull = true; + } + } else if (const LoadInst *LI = dyn_cast<LoadInst>(this)) { + if (MDNode *MD = LI->getMetadata(LLVMContext::MD_dereferenceable)) { + ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0)); + DerefBytes = CI->getLimitedValue(); + } + if (DerefBytes == 0) { + if (MDNode *MD = + LI->getMetadata(LLVMContext::MD_dereferenceable_or_null)) { + ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0)); + DerefBytes = CI->getLimitedValue(); + } + CanBeNull = true; + } + } + return DerefBytes; +} + unsigned Value::getPointerAlignment(const DataLayout &DL) const { assert(getType()->isPointerTy() && "must be pointer"); |