[PATCH] D60047: [CaptureTracking] Pointer comparisons cannot escape

Ayke via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Mar 31 07:11:25 PDT 2019


aykevl created this revision.
aykevl added a reviewer: sanjoy.
Herald added subscribers: llvm-commits, jdoerfert.
Herald added a project: LLVM.

I don't see a way in which a pointer comparison can result in capturing a pointer that is not undefined, although I'm not all that familiar with the C/C++ standards.

I can think of a few cases:

- a comparison against a global, in which case the global was already referenced
- converting to an int and checking that: not included in CaptureTracking
- doing a GEP with large indices until one hits another pointer: UB

In all these, it seems to me that a comparison does not capture a pointer.

This is the post I'm basing myself on:
https://kristerw.blogspot.com/2016/03/c-pointers-are-not-hardware-pointers.html

Tests haven't been added yet because I'm not all that familiar with optimization internals and I want to know whether this is even a valid optimization.


Repository:
  rL LLVM

https://reviews.llvm.org/D60047

Files:
  lib/Analysis/CaptureTracking.cpp


Index: lib/Analysis/CaptureTracking.cpp
===================================================================
--- lib/Analysis/CaptureTracking.cpp
+++ lib/Analysis/CaptureTracking.cpp
@@ -331,25 +331,10 @@
       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)))
-        if (CPN->getType()->getAddressSpace() == 0)
-          if (isNoAliasCall(V->stripPointerCasts()))
-            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));
-      if (LI && isa<GlobalVariable>(LI->getPointerOperand()))
-        break;
-      // Otherwise, be conservative. There are crazy ways to capture pointers
-      // using comparisons.
-      if (Tracker->captured(U))
-        return;
+      // Pointers might be captured with some crazy pointer arithmetic,
+      // but calculating the pointers for these comparisons is undefined
+      // behavior. Therefore, we may assume that an icmp cannot capture a
+      // pointer.
       break;
     }
     default:


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D60047.193009.patch
Type: text/x-patch
Size: 1508 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190331/d34fe85c/attachment.bin>


More information about the llvm-commits mailing list