<div dir="ltr">It's generally good to not checkin entirely dead & untested code - could you include a unit test to demonstrate correctness, at least?</div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Apr 27, 2016 at 5:51 AM, Artur Pilipenko via llvm-commits <span dir="ltr"><<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: apilipenko<br>
Date: Wed Apr 27 07:51:01 2016<br>
New Revision: 267708<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=267708&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=267708&view=rev</a><br>
Log:<br>
NFC. Introduce Value::getPointerDerferecnceableBytes<br>
<br>
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).<br>
<br>
Reviewed By: reames<br>
<br>
Differential Revision: <a href="http://reviews.llvm.org/D17572" rel="noreferrer" target="_blank">http://reviews.llvm.org/D17572</a><br>
<br>
Modified:<br>
    llvm/trunk/include/llvm/IR/Value.h<br>
    llvm/trunk/lib/Analysis/Loads.cpp<br>
    llvm/trunk/lib/IR/Value.cpp<br>
<br>
Modified: llvm/trunk/include/llvm/IR/Value.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/Value.h?rev=267708&r1=267707&r2=267708&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/Value.h?rev=267708&r1=267707&r2=267708&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/IR/Value.h (original)<br>
+++ llvm/trunk/include/llvm/IR/Value.h Wed Apr 27 07:51:01 2016<br>
@@ -504,6 +504,13 @@ public:<br>
     return const_cast<Value*>(this)->stripInBoundsOffsets();<br>
   }<br>
<br>
+  /// \brief Returns the number of bytes known to be dereferenceable for the<br>
+  /// pointer value.<br>
+  ///<br>
+  /// If CanBeNull is set by this function the pointer can either be null or be<br>
+  /// dereferenceable up to the returned number of bytes.<br>
+  unsigned getPointerDereferenceableBytes(bool &CanBeNull) const;<br>
+<br>
   /// \brief Returns an alignment of the pointer value.<br>
   ///<br>
   /// Returns an alignment which is either specified explicitly, e.g. via<br>
<br>
Modified: llvm/trunk/lib/Analysis/Loads.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/Loads.cpp?rev=267708&r1=267707&r2=267708&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/Loads.cpp?rev=267708&r1=267707&r2=267708&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Analysis/Loads.cpp (original)<br>
+++ llvm/trunk/lib/Analysis/Loads.cpp Wed Apr 27 07:51:01 2016<br>
@@ -33,34 +33,9 @@ static bool isDereferenceableFromAttribu<br>
   assert(Offset.isNonNegative() && "offset can't be negative");<br>
   assert(Ty->isSized() && "must be sized");<br>
<br>
-  APInt DerefBytes(Offset.getBitWidth(), 0);<br>
   bool CheckForNonNull = false;<br>
-  if (const Argument *A = dyn_cast<Argument>(BV)) {<br>
-    DerefBytes = A->getDereferenceableBytes();<br>
-    if (!DerefBytes.getBoolValue()) {<br>
-      DerefBytes = A->getDereferenceableOrNullBytes();<br>
-      CheckForNonNull = true;<br>
-    }<br>
-  } else if (auto CS = ImmutableCallSite(BV)) {<br>
-    DerefBytes = CS.getDereferenceableBytes(0);<br>
-    if (!DerefBytes.getBoolValue()) {<br>
-      DerefBytes = CS.getDereferenceableOrNullBytes(0);<br>
-      CheckForNonNull = true;<br>
-    }<br>
-  } else if (const LoadInst *LI = dyn_cast<LoadInst>(BV)) {<br>
-    if (MDNode *MD = LI->getMetadata(LLVMContext::MD_dereferenceable)) {<br>
-      ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));<br>
-      DerefBytes = CI->getLimitedValue();<br>
-    }<br>
-    if (!DerefBytes.getBoolValue()) {<br>
-      if (MDNode *MD =<br>
-              LI->getMetadata(LLVMContext::MD_dereferenceable_or_null)) {<br>
-        ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));<br>
-        DerefBytes = CI->getLimitedValue();<br>
-      }<br>
-      CheckForNonNull = true;<br>
-    }<br>
-  }<br>
+  APInt DerefBytes(Offset.getBitWidth(),<br>
+                   BV->getPointerDereferenceableBytes(CheckForNonNull));<br>
<br>
   if (DerefBytes.getBoolValue())<br>
     if (DerefBytes.uge(Offset + DL.getTypeStoreSize(Ty)))<br>
<br>
Modified: llvm/trunk/lib/IR/Value.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/Value.cpp?rev=267708&r1=267707&r2=267708&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/Value.cpp?rev=267708&r1=267707&r2=267708&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/IR/Value.cpp (original)<br>
+++ llvm/trunk/lib/IR/Value.cpp Wed Apr 27 07:51:01 2016<br>
@@ -525,6 +525,40 @@ Value *Value::stripInBoundsOffsets() {<br>
   return stripPointerCastsAndOffsets<PSK_InBounds>(this);<br>
 }<br>
<br>
+unsigned Value::getPointerDereferenceableBytes(bool &CanBeNull) const {<br>
+  assert(getType()->isPointerTy() && "must be pointer");<br>
+<br>
+  unsigned DerefBytes = 0;<br>
+  CanBeNull = false;<br>
+  if (const Argument *A = dyn_cast<Argument>(this)) {<br>
+    DerefBytes = A->getDereferenceableBytes();<br>
+    if (DerefBytes == 0) {<br>
+      DerefBytes = A->getDereferenceableOrNullBytes();<br>
+      CanBeNull = true;<br>
+    }<br>
+  } else if (auto CS = ImmutableCallSite(this)) {<br>
+    DerefBytes = CS.getDereferenceableBytes(0);<br>
+    if (DerefBytes == 0) {<br>
+      DerefBytes = CS.getDereferenceableOrNullBytes(0);<br>
+      CanBeNull = true;<br>
+    }<br>
+  } else if (const LoadInst *LI = dyn_cast<LoadInst>(this)) {<br>
+    if (MDNode *MD = LI->getMetadata(LLVMContext::MD_dereferenceable)) {<br>
+      ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));<br>
+      DerefBytes = CI->getLimitedValue();<br>
+    }<br>
+    if (DerefBytes == 0) {<br>
+      if (MDNode *MD =<br>
+              LI->getMetadata(LLVMContext::MD_dereferenceable_or_null)) {<br>
+        ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));<br>
+        DerefBytes = CI->getLimitedValue();<br>
+      }<br>
+      CanBeNull = true;<br>
+    }<br>
+  }<br>
+  return DerefBytes;<br>
+}<br>
+<br>
 unsigned Value::getPointerAlignment(const DataLayout &DL) const {<br>
   assert(getType()->isPointerTy() && "must be pointer");<br>
<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div>