[PATCH] D17611: NFC. Introduce Value::isPointerDereferenceable
Artur Pilipenko via llvm-commits
llvm-commits at lists.llvm.org
Thu Feb 25 08:44:35 PST 2016
apilipenko created this revision.
apilipenko added reviewers: hfinkel, reames.
apilipenko added a subscriber: llvm-commits.
This is analogous to D17572 (and the diff is based upon it). Extract a part of isDereferenceableAndAlignedPointer functionality to Value::isPointerDereferenceable. The difference between those function is that isPointerDereferenceable can be used for opaque types.
http://reviews.llvm.org/D17611
Files:
include/llvm/IR/Value.h
lib/Analysis/Loads.cpp
lib/IR/Value.cpp
Index: lib/IR/Value.cpp
===================================================================
--- lib/IR/Value.cpp
+++ lib/IR/Value.cpp
@@ -553,6 +553,29 @@
return DerefBytes;
}
+bool Value::isPointerDereferenceable(bool &CanBeNull) const {
+ assert(getType()->isPointerTy() && "must be pointer");
+
+ CanBeNull = false;
+
+ // These are obviously ok.
+ if (isa<AllocaInst>(this))
+ return true;
+
+ // Global variables which can't collapse to null are ok.
+ // TODO: return true for those but set CanBeNull flag
+ if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
+ if (!GV->hasExternalWeakLinkage())
+ return true;
+
+ // byval arguments are okay.
+ if (const Argument *A = dyn_cast<Argument>(this))
+ if (A->hasByValAttr())
+ return true;
+
+ return false;
+}
+
unsigned Value::getPointerAlignment(const DataLayout &DL) const {
assert(getType()->isPointerTy() && "must be pointer");
Index: lib/Analysis/Loads.cpp
===================================================================
--- lib/Analysis/Loads.cpp
+++ lib/Analysis/Loads.cpp
@@ -91,9 +91,11 @@
// Note that it is not safe to speculate into a malloc'd region because
// malloc may return null.
- // These are obviously ok if aligned.
- if (isa<AllocaInst>(V))
+ bool CheckForNull;
+ if (V->isPointerDereferenceable(CheckForNull)) {
+ assert(!CheckForNull && "don't check for non-null currently");
return isAligned(V, Align, DL);
+ }
// It's not always safe to follow a bitcast, for example:
// bitcast i8* (alloca i8) to i32*
@@ -112,16 +114,6 @@
CtxI, DT, TLI, Visited);
}
- // Global variables which can't collapse to null are ok.
- if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
- if (!GV->hasExternalWeakLinkage())
- return isAligned(V, Align, DL);
-
- // byval arguments are okay.
- if (const Argument *A = dyn_cast<Argument>(V))
- if (A->hasByValAttr())
- return isAligned(V, Align, DL);
-
if (isDereferenceableFromAttribute(V, DL, CtxI, DT, TLI))
return isAligned(V, Align, DL);
Index: include/llvm/IR/Value.h
===================================================================
--- include/llvm/IR/Value.h
+++ include/llvm/IR/Value.h
@@ -508,6 +508,12 @@
/// dereferenceable up to the returned number of bytes.
unsigned getPointerDereferenceableBytes(bool &CanBeNull) const;
+ /// \brief Returns true if the pointer value is fully dereferenceable.
+ ///
+ /// Sets CanBeNull to true if the pointer is either null or can be fully
+ /// dereferenceable.
+ bool isPointerDereferenceable(bool &CanBeNull) const;
+
/// \brief Returns an alignment of the pointer value.
///
/// Returns an alignment which is either specified explicitly, e.g. via
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D17611.49073.patch
Type: text/x-patch
Size: 2811 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160225/b3373612/attachment.bin>
More information about the llvm-commits
mailing list