Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit 25f648c

Browse files
committed
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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@267708 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent e8bbab4 commit 25f648c

File tree

3 files changed

+43
-27
lines changed

3 files changed

+43
-27
lines changed

include/llvm/IR/Value.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,13 @@ class Value {
504504
return const_cast<Value*>(this)->stripInBoundsOffsets();
505505
}
506506

507+
/// \brief Returns the number of bytes known to be dereferenceable for the
508+
/// pointer value.
509+
///
510+
/// If CanBeNull is set by this function the pointer can either be null or be
511+
/// dereferenceable up to the returned number of bytes.
512+
unsigned getPointerDereferenceableBytes(bool &CanBeNull) const;
513+
507514
/// \brief Returns an alignment of the pointer value.
508515
///
509516
/// Returns an alignment which is either specified explicitly, e.g. via

lib/Analysis/Loads.cpp

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -33,34 +33,9 @@ static bool isDereferenceableFromAttribute(const Value *BV, APInt Offset,
3333
assert(Offset.isNonNegative() && "offset can't be negative");
3434
assert(Ty->isSized() && "must be sized");
3535

36-
APInt DerefBytes(Offset.getBitWidth(), 0);
3736
bool CheckForNonNull = false;
38-
if (const Argument *A = dyn_cast<Argument>(BV)) {
39-
DerefBytes = A->getDereferenceableBytes();
40-
if (!DerefBytes.getBoolValue()) {
41-
DerefBytes = A->getDereferenceableOrNullBytes();
42-
CheckForNonNull = true;
43-
}
44-
} else if (auto CS = ImmutableCallSite(BV)) {
45-
DerefBytes = CS.getDereferenceableBytes(0);
46-
if (!DerefBytes.getBoolValue()) {
47-
DerefBytes = CS.getDereferenceableOrNullBytes(0);
48-
CheckForNonNull = true;
49-
}
50-
} else if (const LoadInst *LI = dyn_cast<LoadInst>(BV)) {
51-
if (MDNode *MD = LI->getMetadata(LLVMContext::MD_dereferenceable)) {
52-
ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
53-
DerefBytes = CI->getLimitedValue();
54-
}
55-
if (!DerefBytes.getBoolValue()) {
56-
if (MDNode *MD =
57-
LI->getMetadata(LLVMContext::MD_dereferenceable_or_null)) {
58-
ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
59-
DerefBytes = CI->getLimitedValue();
60-
}
61-
CheckForNonNull = true;
62-
}
63-
}
37+
APInt DerefBytes(Offset.getBitWidth(),
38+
BV->getPointerDereferenceableBytes(CheckForNonNull));
6439

6540
if (DerefBytes.getBoolValue())
6641
if (DerefBytes.uge(Offset + DL.getTypeStoreSize(Ty)))

lib/IR/Value.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,40 @@ Value *Value::stripInBoundsOffsets() {
525525
return stripPointerCastsAndOffsets<PSK_InBounds>(this);
526526
}
527527

528+
unsigned Value::getPointerDereferenceableBytes(bool &CanBeNull) const {
529+
assert(getType()->isPointerTy() && "must be pointer");
530+
531+
unsigned DerefBytes = 0;
532+
CanBeNull = false;
533+
if (const Argument *A = dyn_cast<Argument>(this)) {
534+
DerefBytes = A->getDereferenceableBytes();
535+
if (DerefBytes == 0) {
536+
DerefBytes = A->getDereferenceableOrNullBytes();
537+
CanBeNull = true;
538+
}
539+
} else if (auto CS = ImmutableCallSite(this)) {
540+
DerefBytes = CS.getDereferenceableBytes(0);
541+
if (DerefBytes == 0) {
542+
DerefBytes = CS.getDereferenceableOrNullBytes(0);
543+
CanBeNull = true;
544+
}
545+
} else if (const LoadInst *LI = dyn_cast<LoadInst>(this)) {
546+
if (MDNode *MD = LI->getMetadata(LLVMContext::MD_dereferenceable)) {
547+
ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
548+
DerefBytes = CI->getLimitedValue();
549+
}
550+
if (DerefBytes == 0) {
551+
if (MDNode *MD =
552+
LI->getMetadata(LLVMContext::MD_dereferenceable_or_null)) {
553+
ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
554+
DerefBytes = CI->getLimitedValue();
555+
}
556+
CanBeNull = true;
557+
}
558+
}
559+
return DerefBytes;
560+
}
561+
528562
unsigned Value::getPointerAlignment(const DataLayout &DL) const {
529563
assert(getType()->isPointerTy() && "must be pointer");
530564

0 commit comments

Comments
 (0)