[llvm-branch-commits] [llvm-branch] r89739 - in /llvm/branches/Apple/Zoidberg: include/llvm/Analysis/CaptureTracking.h lib/Analysis/BasicAliasAnalysis.cpp lib/Analysis/CaptureTracking.cpp lib/Transforms/IPO/FunctionAttrs.cpp

Bob Wilson bob.wilson at apple.com
Mon Nov 23 21:32:21 PST 2009


Author: bwilson
Date: Mon Nov 23 23:32:21 2009
New Revision: 89739

URL: http://llvm.org/viewvc/llvm-project?rev=89739&view=rev
Log:
Reapply Dan's changes 89389, 89398, 89436, and 89485.

Modified:
    llvm/branches/Apple/Zoidberg/include/llvm/Analysis/CaptureTracking.h
    llvm/branches/Apple/Zoidberg/lib/Analysis/BasicAliasAnalysis.cpp
    llvm/branches/Apple/Zoidberg/lib/Analysis/CaptureTracking.cpp
    llvm/branches/Apple/Zoidberg/lib/Transforms/IPO/FunctionAttrs.cpp

Modified: llvm/branches/Apple/Zoidberg/include/llvm/Analysis/CaptureTracking.h
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Zoidberg/include/llvm/Analysis/CaptureTracking.h?rev=89739&r1=89738&r2=89739&view=diff

==============================================================================
--- llvm/branches/Apple/Zoidberg/include/llvm/Analysis/CaptureTracking.h (original)
+++ llvm/branches/Apple/Zoidberg/include/llvm/Analysis/CaptureTracking.h Mon Nov 23 23:32:21 2009
@@ -21,8 +21,12 @@
   /// by the enclosing function (which is required to exist).  This routine can
   /// be expensive, so consider caching the results.  The boolean ReturnCaptures
   /// specifies whether returning the value (or part of it) from the function
+  /// counts as capturing it or not.  The boolean StoreCaptures specified whether
+  /// storing the value (or part of it) into memory anywhere automatically
   /// counts as capturing it or not.
-  bool PointerMayBeCaptured(const Value *V, bool ReturnCaptures);
+  bool PointerMayBeCaptured(const Value *V,
+                            bool ReturnCaptures,
+                            bool StoreCaptures);
 
 } // end namespace llvm
 

Modified: llvm/branches/Apple/Zoidberg/lib/Analysis/BasicAliasAnalysis.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Zoidberg/lib/Analysis/BasicAliasAnalysis.cpp?rev=89739&r1=89738&r2=89739&view=diff

==============================================================================
--- llvm/branches/Apple/Zoidberg/lib/Analysis/BasicAliasAnalysis.cpp (original)
+++ llvm/branches/Apple/Zoidberg/lib/Analysis/BasicAliasAnalysis.cpp Mon Nov 23 23:32:21 2009
@@ -79,7 +79,12 @@
 static bool isNonEscapingLocalObject(const Value *V) {
   // If this is a local allocation, check to see if it escapes.
   if (isa<AllocaInst>(V) || isNoAliasCall(V))
-    return !PointerMayBeCaptured(V, false);
+    // Set StoreCaptures to True so that we can assume in our callers that the
+    // pointer is not the result of a load instruction. Currently
+    // PointerMayBeCaptured doesn't have any special analysis for the
+    // StoreCaptures=false case; if it did, our callers could be refined to be
+    // more precise.
+    return !PointerMayBeCaptured(V, false, /*StoreCaptures=*/true);
 
   // If this is an argument that corresponds to a byval or noalias argument,
   // then it has not escaped before entering the function.  Check if it escapes
@@ -89,7 +94,7 @@
       // Don't bother analyzing arguments already known not to escape.
       if (A->hasNoCaptureAttr())
         return true;
-      return !PointerMayBeCaptured(V, false);
+      return !PointerMayBeCaptured(V, false, /*StoreCaptures=*/true);
     }
   return false;
 }
@@ -683,15 +688,19 @@
         (V2Size != ~0U && isObjectSmallerThan(O1, V2Size, *TD)))
       return NoAlias;
   
-  // If one pointer is the result of a call/invoke and the other is a
+  // If one pointer is the result of a call/invoke or load and the other is a
   // non-escaping local object, then we know the object couldn't escape to a
-  // point where the call could return it.
-  if ((isa<CallInst>(O1) || isa<InvokeInst>(O1)) &&
-      isNonEscapingLocalObject(O2) && O1 != O2)
-    return NoAlias;
-  if ((isa<CallInst>(O2) || isa<InvokeInst>(O2)) &&
-      isNonEscapingLocalObject(O1) && O1 != O2)
-    return NoAlias;
+  // point where the call could return it. The load case works because
+  // isNonEscapingLocalObject considers all stores to be escapes (it
+  // passes true for the StoreCaptures argument to PointerMayBeCaptured).
+  if (O1 != O2) {
+    if ((isa<CallInst>(O1) || isa<InvokeInst>(O1) || isa<LoadInst>(O1)) &&
+        isNonEscapingLocalObject(O2))
+      return NoAlias;
+    if ((isa<CallInst>(O2) || isa<InvokeInst>(O2) || isa<LoadInst>(O2)) &&
+        isNonEscapingLocalObject(O1))
+      return NoAlias;
+  }
 
   if (!isa<GEPOperator>(V1) && isa<GEPOperator>(V2)) {
     std::swap(V1, V2);

Modified: llvm/branches/Apple/Zoidberg/lib/Analysis/CaptureTracking.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Zoidberg/lib/Analysis/CaptureTracking.cpp?rev=89739&r1=89738&r2=89739&view=diff

==============================================================================
--- llvm/branches/Apple/Zoidberg/lib/Analysis/CaptureTracking.cpp (original)
+++ llvm/branches/Apple/Zoidberg/lib/Analysis/CaptureTracking.cpp Mon Nov 23 23:32:21 2009
@@ -19,6 +19,7 @@
 #include "llvm/Analysis/CaptureTracking.h"
 #include "llvm/Instructions.h"
 #include "llvm/Value.h"
+#include "llvm/Analysis/AliasAnalysis.h"
 #include "llvm/ADT/SmallSet.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/Support/CallSite.h"
@@ -28,8 +29,11 @@
 /// by the enclosing function (which is required to exist).  This routine can
 /// be expensive, so consider caching the results.  The boolean ReturnCaptures
 /// specifies whether returning the value (or part of it) from the function
+/// counts as capturing it or not.  The boolean StoreCaptures specified whether
+/// storing the value (or part of it) into memory anywhere automatically
 /// counts as capturing it or not.
-bool llvm::PointerMayBeCaptured(const Value *V, bool ReturnCaptures) {
+bool llvm::PointerMayBeCaptured(const Value *V,
+                                bool ReturnCaptures, bool StoreCaptures) {
   assert(isa<PointerType>(V->getType()) && "Capture is for pointers only!");
   SmallVector<Use*, 16> Worklist;
   SmallSet<Use*, 16> Visited;
@@ -53,8 +57,7 @@
       // Not captured if the callee is readonly, doesn't return a copy through
       // its return value and doesn't unwind (a readonly function can leak bits
       // by throwing an exception or not depending on the input value).
-      if (CS.onlyReadsMemory() && CS.doesNotThrow() &&
-          I->getType() == Type::getVoidTy(V->getContext()))
+      if (CS.onlyReadsMemory() && CS.doesNotThrow() && I->getType()->isVoidTy())
         break;
 
       // Not captured if only passed via 'nocapture' arguments.  Note that
@@ -82,7 +85,11 @@
       break;
     case Instruction::Store:
       if (V == I->getOperand(0))
-        // Stored the pointer - it may be captured.
+        // Stored the pointer - conservatively assume it may be captured.
+        // TODO: If StoreCaptures is not true, we could do Fancy analysis
+        // to determine whether this store is not actually an escape point.
+        // In that case, BasicAliasAnalysis should be updated as well to
+        // take advantage of this.
         return true;
       // Storing to the pointee does not cause the pointer to be captured.
       break;
@@ -98,6 +105,18 @@
           Worklist.push_back(U);
       }
       break;
+    case Instruction::ICmp:
+      // Don't count comparisons of a no-alias return value against null as
+      // captures. This allows us to ignore comparisons of malloc results
+      // with null, for example.
+      if (isNoAliasCall(V->stripPointerCasts()))
+        if (ConstantPointerNull *CPN =
+              dyn_cast<ConstantPointerNull>(I->getOperand(1)))
+          if (CPN->getType()->getAddressSpace() == 0)
+            break;
+      // Otherwise, be conservative. There are crazy ways to capture pointers
+      // using comparisons.
+      return true;
     default:
       // Something else - be conservative and say it is captured.
       return true;

Modified: llvm/branches/Apple/Zoidberg/lib/Transforms/IPO/FunctionAttrs.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Zoidberg/lib/Transforms/IPO/FunctionAttrs.cpp?rev=89739&r1=89738&r2=89739&view=diff

==============================================================================
--- llvm/branches/Apple/Zoidberg/lib/Transforms/IPO/FunctionAttrs.cpp (original)
+++ llvm/branches/Apple/Zoidberg/lib/Transforms/IPO/FunctionAttrs.cpp Mon Nov 23 23:32:21 2009
@@ -212,7 +212,7 @@
 
     for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A!=E; ++A)
       if (isa<PointerType>(A->getType()) && !A->hasNoCaptureAttr() &&
-          !PointerMayBeCaptured(A, true)) {
+          !PointerMayBeCaptured(A, true, /*StoreCaptures=*/false)) {
         A->addAttr(Attribute::NoCapture);
         ++NumNoCapture;
         Changed = true;
@@ -280,7 +280,7 @@
           return false;  // Did not come from an allocation.
       }
 
-    if (PointerMayBeCaptured(RetVal, false))
+    if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false))
       return false;
   }
 





More information about the llvm-branch-commits mailing list