[llvm] r369158 - [CaptureTracking] Allow null to be in either icmp operand

Johannes Doerfert via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 16 14:53:49 PDT 2019


Author: jdoerfert
Date: Fri Aug 16 14:53:49 2019
New Revision: 369158

URL: http://llvm.org/viewvc/llvm-project?rev=369158&view=rev
Log:
[CaptureTracking] Allow null to be in either icmp operand

Summary:
Before we required the comparison against null to be "canonical", hence
null to be operand #1. This patch allows null to be in either operand,
similar to the handling of loaded globals that follows.

Reviewers: sanjoy, hfinkel, aykevl, sstefan1, uenoku

Subscribers: hiraditya, bollu, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D66321

Modified:
    llvm/trunk/lib/Analysis/CaptureTracking.cpp
    llvm/trunk/test/Transforms/FunctionAttrs/nocapture.ll

Modified: llvm/trunk/lib/Analysis/CaptureTracking.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/CaptureTracking.cpp?rev=369158&r1=369157&r2=369158&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/CaptureTracking.cpp (original)
+++ llvm/trunk/lib/Analysis/CaptureTracking.cpp Fri Aug 16 14:53:49 2019
@@ -331,7 +331,9 @@ void llvm::PointerMayBeCaptured(const Va
       AddUses(I);
       break;
     case Instruction::ICmp: {
-      if (auto *CPN = dyn_cast<ConstantPointerNull>(I->getOperand(1))) {
+      unsigned Idx = (I->getOperand(0) == V) ? 0 : 1;
+      unsigned OtherIdx = 1 - Idx;
+      if (auto *CPN = dyn_cast<ConstantPointerNull>(I->getOperand(OtherIdx))) {
         // 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.
@@ -339,7 +341,7 @@ void llvm::PointerMayBeCaptured(const Va
           if (isNoAliasCall(V->stripPointerCasts()))
             break;
         if (!I->getFunction()->nullPointerIsDefined()) {
-          auto *O = I->getOperand(0)->stripPointerCastsSameRepresentation();
+          auto *O = I->getOperand(Idx)->stripPointerCastsSameRepresentation();
           // An inbounds GEP can either be a valid pointer (pointing into
           // or to the end of an allocation), or be null in the default
           // address space. So for an inbounds GEPs there is no way to let
@@ -353,15 +355,15 @@ void llvm::PointerMayBeCaptured(const Va
           // cannot lead to pointer escapes, because if it is not null it
           // must be a valid (in-bounds) pointer.
           bool CanBeNull;
-          if (O->getPointerDereferenceableBytes(I->getModule()->getDataLayout(), CanBeNull))
+          if (O->getPointerDereferenceableBytes(I->getModule()->getDataLayout(),
+                                                CanBeNull))
             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.
-      unsigned OtherIndex = (I->getOperand(0) == V) ? 1 : 0;
-      auto *LI = dyn_cast<LoadInst>(I->getOperand(OtherIndex));
+      auto *LI = dyn_cast<LoadInst>(I->getOperand(OtherIdx));
       if (LI && isa<GlobalVariable>(LI->getPointerOperand()))
         break;
       // Otherwise, be conservative. There are crazy ways to capture pointers

Modified: llvm/trunk/test/Transforms/FunctionAttrs/nocapture.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/FunctionAttrs/nocapture.ll?rev=369158&r1=369157&r2=369158&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/FunctionAttrs/nocapture.ll (original)
+++ llvm/trunk/test/Transforms/FunctionAttrs/nocapture.ll Fri Aug 16 14:53:49 2019
@@ -259,6 +259,12 @@ define i1 @captureICmp(i32* %x) {
   ret i1 %1
 }
 
+; CHECK: define i1 @captureICmpRev(i32* readnone %x)
+define i1 @captureICmpRev(i32* %x) {
+  %1 = icmp eq i32* null, %x
+  ret i1 %1
+}
+
 ; CHECK: define i1 @nocaptureInboundsGEPICmp(i32* nocapture readnone %x)
 define i1 @nocaptureInboundsGEPICmp(i32* %x) {
   %1 = getelementptr inbounds i32, i32* %x, i32 5
@@ -267,6 +273,14 @@ define i1 @nocaptureInboundsGEPICmp(i32*
   ret i1 %3
 }
 
+; CHECK: define i1 @nocaptureInboundsGEPICmpRev(i32* nocapture readnone %x)
+define i1 @nocaptureInboundsGEPICmpRev(i32* %x) {
+  %1 = getelementptr inbounds i32, i32* %x, i32 5
+  %2 = bitcast i32* %1 to i8*
+  %3 = icmp eq i8* null, %2
+  ret i1 %3
+}
+
 ; CHECK: define i1 @nocaptureDereferenceableOrNullICmp(i32* nocapture readnone dereferenceable_or_null(4) %x)
 define i1 @nocaptureDereferenceableOrNullICmp(i32* dereferenceable_or_null(4) %x) {
   %1 = bitcast i32* %x to i8*




More information about the llvm-commits mailing list