[PATCH] D107357: [getUnderlyingObject] support ptr_provenance

Jeroen Dobbelaere via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 3 08:09:55 PDT 2021


jeroen.dobbelaere created this revision.
jeroen.dobbelaere added reviewers: jdoerfert, nikic.
Herald added a subscriber: hiraditya.
jeroen.dobbelaere requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Allow to follow the pointer path through the computation operand 0 of llvm.experimental.ptr.provenance) or through the provenance (operand 1 of llvm.experimental.ptr.provenance).

Note: No further usage of this change is made at this moment. The BasicAliasAnalysis is mixing up the 'provenance' (as meant in ptr provenance and 'base' as where you end up when looking through the pointer computations. Any change there means choosing a policy (like explained in N2676 and others). At this moment, I am not sure what path to follow.

- For full restrict, the idea is that the ptr_provenance and the underlying object of the computation end up to be the same. For N2676 this is not necessarily the case. Also for fixing issues related to GVN optimizing pointers based on 'equality', but losing provenance by doing so, this will also change that assumption.
- Another potential issue is the handling of `ConstantPointerNull`:
  - for a ptr_provenance, this would indicate that it can originate from any object
  - for a pointer computation, this would indicate a 'null pointer'. The combination should still be interpreted as a '


https://reviews.llvm.org/D107357

Files:
  llvm/include/llvm/Analysis/ValueTracking.h
  llvm/lib/Analysis/ValueTracking.cpp


Index: llvm/lib/Analysis/ValueTracking.cpp
===================================================================
--- llvm/lib/Analysis/ValueTracking.cpp
+++ llvm/lib/Analysis/ValueTracking.cpp
@@ -4353,7 +4353,8 @@
   return true;
 }
 
-const Value *llvm::getUnderlyingObject(const Value *V, unsigned MaxLookup) {
+const Value *llvm::getUnderlyingObject(const Value *V, unsigned MaxLookup,
+                                       bool FollowProvenance) {
   if (!V->getType()->isPointerTy())
     return V;
   for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) {
@@ -4376,6 +4377,11 @@
           continue;
         }
       } else if (auto *Call = dyn_cast<CallBase>(V)) {
+        if (Call->getIntrinsicID() == Intrinsic::experimental_ptr_provenance) {
+          V = Call->getArgOperand(FollowProvenance ? 1 : 0);
+          continue;
+        }
+
         // CaptureTracking can know about special capturing properties of some
         // intrinsics like launder.invariant.group, that can't be expressed with
         // the attributes, but have properties like returning aliasing pointer.
@@ -4400,13 +4406,14 @@
 
 void llvm::getUnderlyingObjects(const Value *V,
                                 SmallVectorImpl<const Value *> &Objects,
-                                LoopInfo *LI, unsigned MaxLookup) {
+                                LoopInfo *LI, unsigned MaxLookup,
+                                bool FollowProvenance) {
   SmallPtrSet<const Value *, 4> Visited;
   SmallVector<const Value *, 4> Worklist;
   Worklist.push_back(V);
   do {
     const Value *P = Worklist.pop_back_val();
-    P = getUnderlyingObject(P, MaxLookup);
+    P = getUnderlyingObject(P, MaxLookup, FollowProvenance);
 
     if (!Visited.insert(P).second)
       continue;
Index: llvm/include/llvm/Analysis/ValueTracking.h
===================================================================
--- llvm/include/llvm/Analysis/ValueTracking.h
+++ llvm/include/llvm/Analysis/ValueTracking.h
@@ -369,9 +369,14 @@
   /// the specified value, returning the original object being addressed. Note
   /// that the returned value has pointer type if the specified value does. If
   /// the MaxLookup value is non-zero, it limits the number of instructions to
-  /// be stripped off.
-  const Value *getUnderlyingObject(const Value *V, unsigned MaxLookup = 6);
-  inline Value *getUnderlyingObject(Value *V, unsigned MaxLookup = 6) {
+  /// be stripped off. When FollowProvenance is set, the provenance side of
+  /// llvm.experimental.ptr.provenance is taken. Be aware that for provenance,
+  /// a ConstantPointerNull indicates that any valid object can be the
+  /// underlying object.
+  const Value *getUnderlyingObject(const Value *V, unsigned MaxLookup = 6,
+                                   bool FollowProvenance = false);
+  inline Value *getUnderlyingObject(Value *V, unsigned MaxLookup = 6,
+                                    bool FollowProvenance = false) {
     // Force const to avoid infinite recursion.
     const Value *VConst = V;
     return const_cast<Value *>(getUnderlyingObject(VConst, MaxLookup));
@@ -405,9 +410,12 @@
   /// Since A[i] and A[i-1] are independent pointers, getUnderlyingObjects
   /// should not assume that Curr and Prev share the same underlying object thus
   /// it shouldn't look through the phi above.
+  /// When FollowProvenance is set, the provenance side of
+  /// llvm.experimental.ptr.provenance is taken.
   void getUnderlyingObjects(const Value *V,
                             SmallVectorImpl<const Value *> &Objects,
-                            LoopInfo *LI = nullptr, unsigned MaxLookup = 6);
+                            LoopInfo *LI = nullptr, unsigned MaxLookup = 6,
+                            bool FollowProvenance = false);
 
   /// This is a wrapper around getUnderlyingObjects and adds support for basic
   /// ptrtoint+arithmetic+inttoptr sequences.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D107357.363745.patch
Type: text/x-patch
Size: 3918 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210803/5cc14148/attachment.bin>


More information about the llvm-commits mailing list