Index: include/llvm/IR/Value.h =================================================================== --- include/llvm/IR/Value.h +++ include/llvm/IR/Value.h @@ -501,6 +501,13 @@ return const_cast(this)->stripInBoundsOffsets(); } + /// \brief Returns the number of bytes known to be dereferenceable for the + /// pointer value. + /// + /// Sets CanBeNull to true if the pointer is either null or can 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 Index: lib/Analysis/Loads.cpp =================================================================== --- lib/Analysis/Loads.cpp +++ lib/Analysis/Loads.cpp @@ -33,34 +33,9 @@ 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(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(BV)) { - if (MDNode *MD = LI->getMetadata(LLVMContext::MD_dereferenceable)) { - ConstantInt *CI = mdconst::extract(MD->getOperand(0)); - DerefBytes = CI->getLimitedValue(); - } - if (!DerefBytes.getBoolValue()) { - if (MDNode *MD = - LI->getMetadata(LLVMContext::MD_dereferenceable_or_null)) { - ConstantInt *CI = mdconst::extract(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))) Index: lib/IR/Value.cpp =================================================================== --- lib/IR/Value.cpp +++ lib/IR/Value.cpp @@ -519,6 +519,38 @@ return stripPointerCastsAndOffsets(this); } +unsigned Value::getPointerDereferenceableBytes(bool &CanBeNull) const { + unsigned DerefBytes = 0; + CanBeNull = false; + if (const Argument *A = dyn_cast(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(this)) { + if (MDNode *MD = LI->getMetadata(LLVMContext::MD_dereferenceable)) { + ConstantInt *CI = mdconst::extract(MD->getOperand(0)); + DerefBytes = CI->getLimitedValue(); + } + if (DerefBytes == 0) { + if (MDNode *MD = + LI->getMetadata(LLVMContext::MD_dereferenceable_or_null)) { + ConstantInt *CI = mdconst::extract(MD->getOperand(0)); + DerefBytes = CI->getLimitedValue(); + } + CanBeNull = true; + } + } + return DerefBytes; +} + unsigned Value::getPointerAlignment(const DataLayout &DL) const { assert(getType()->isPointerTy() && "must be pointer");