[PATCH] D60047: [CaptureTracking] Don't let comparisons against nil escape inbounds pointers

Ayke via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 25 05:45:18 PDT 2019


aykevl updated this revision to Diff 196616.
aykevl retitled this revision from "[CaptureTracking] Pointer comparisons cannot escape" to "[CaptureTracking] Don't let comparisons against nil escape inbounds pointers".
aykevl edited the summary of this revision.
aykevl added a comment.

Update CaptureTracking to be more conservative and add FunctionAttrs tests.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D60047/new/

https://reviews.llvm.org/D60047

Files:
  lib/Analysis/CaptureTracking.cpp
  test/Transforms/FunctionAttrs/nocapture.ll


Index: test/Transforms/FunctionAttrs/nocapture.ll
===================================================================
--- test/Transforms/FunctionAttrs/nocapture.ll
+++ test/Transforms/FunctionAttrs/nocapture.ll
@@ -253,5 +253,28 @@
   ret void
 }
 
+; CHECK: define i1 @captureICmp(i32* readnone %x)
+define i1 @captureICmp(i32* %x) {
+entry:
+  %0 = getelementptr i32, i32* %x, i32 5
+  %1 = icmp eq i32* %0, null
+  ret i1 %1
+}
+
+; CHECK: define i1 @nocaptureInboundsGEPICmp(i32* nocapture readnone %x)
+define i1 @nocaptureInboundsGEPICmp(i32* %x) {
+entry:
+  %0 = getelementptr inbounds i32, i32* %x, i32 5
+  %1 = icmp eq i32* %0, null
+  ret i1 %1
+}
+
+; CHECK: define i1 @nocaptureDereferenceableOrNullICmp(i32* nocapture readnone dereferenceable_or_null(4) %x)
+define i1 @nocaptureDereferenceableOrNullICmp(i32* dereferenceable_or_null(4) %x) {
+entry:
+  %0 = icmp eq i32* %x, null
+  ret i1 %0
+}
+
 declare i8* @llvm.launder.invariant.group.p0i8(i8*)
 declare i8* @llvm.strip.invariant.group.p0i8(i8*)
Index: lib/Analysis/CaptureTracking.cpp
===================================================================
--- lib/Analysis/CaptureTracking.cpp
+++ lib/Analysis/CaptureTracking.cpp
@@ -331,14 +331,28 @@
       AddUses(I);
       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 (ConstantPointerNull *CPN =
-          dyn_cast<ConstantPointerNull>(I->getOperand(1)))
+          dyn_cast<ConstantPointerNull>(I->getOperand(1))) {
+        // 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 (CPN->getType()->getAddressSpace() == 0)
           if (isNoAliasCall(V->stripPointerCasts()))
             break;
+        // A getelementptr inbounds is either null or an in-bounds GEP, which
+        // means that there is no way to let a pointer escape using clever GEP
+        // hacking.
+        if (GetElementPtrInst *GEP =
+            dyn_cast<GetElementPtrInst>(I->getOperand(0)))
+          if (GEP->isInBounds())
+            break;
+        // Comparing a dereferenceable_or_null argument against null cannot lead
+        // to pointer escapes, because if it is not null it must be a valid
+        // (in-bounds) pointer.
+        if (Argument *A = dyn_cast<Argument>(I->getOperand(0)))
+          if (A->getDereferenceableOrNullBytes())
+            break;
+      }
       // Comparison against value stored in global variable. Given the pointer
       // does not escape, its value cannot be guessed and stored separately in a
       // global variable.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D60047.196616.patch
Type: text/x-patch
Size: 2810 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190425/9f4f2096/attachment.bin>


More information about the llvm-commits mailing list