[llvm] r267708 - NFC. Introduce Value::getPointerDerferecnceableBytes

David Blaikie via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 27 11:10:39 PDT 2016


It's generally good to not checkin entirely dead & untested code - could
you include a unit test to demonstrate correctness, at least?

On Wed, Apr 27, 2016 at 5:51 AM, Artur Pilipenko via llvm-commits <
llvm-commits at lists.llvm.org> wrote:

> Author: apilipenko
> Date: Wed Apr 27 07:51:01 2016
> New Revision: 267708
>
> URL: http://llvm.org/viewvc/llvm-project?rev=267708&view=rev
> Log:
> 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
>
> Modified:
>     llvm/trunk/include/llvm/IR/Value.h
>     llvm/trunk/lib/Analysis/Loads.cpp
>     llvm/trunk/lib/IR/Value.cpp
>
> Modified: llvm/trunk/include/llvm/IR/Value.h
> URL:
> http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/Value.h?rev=267708&r1=267707&r2=267708&view=diff
>
> ==============================================================================
> --- llvm/trunk/include/llvm/IR/Value.h (original)
> +++ llvm/trunk/include/llvm/IR/Value.h Wed Apr 27 07:51:01 2016
> @@ -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
>
> Modified: llvm/trunk/lib/Analysis/Loads.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/Loads.cpp?rev=267708&r1=267707&r2=267708&view=diff
>
> ==============================================================================
> --- llvm/trunk/lib/Analysis/Loads.cpp (original)
> +++ llvm/trunk/lib/Analysis/Loads.cpp Wed Apr 27 07:51:01 2016
> @@ -33,34 +33,9 @@ static bool isDereferenceableFromAttribu
>    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)))
>
> Modified: llvm/trunk/lib/IR/Value.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/Value.cpp?rev=267708&r1=267707&r2=267708&view=diff
>
> ==============================================================================
> --- llvm/trunk/lib/IR/Value.cpp (original)
> +++ llvm/trunk/lib/IR/Value.cpp Wed Apr 27 07:51:01 2016
> @@ -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");
>
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160427/0feae98f/attachment.html>


More information about the llvm-commits mailing list