[PATCH] D107357: [getUnderlyingObject] support ptr_provenance

Jeroen Dobbelaere via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 15 14:14:31 PST 2023


jeroen.dobbelaere updated this revision to Diff 497802.
jeroen.dobbelaere added a comment.

Rebased to: 8c7cfa357280dd93d33b10bbba0fe33797e27d63 <https://reviews.llvm.org/rG8c7cfa357280dd93d33b10bbba0fe33797e27d63>  (Feb 14, 2023)


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

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
@@ -4564,7 +4564,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) {
@@ -4587,6 +4588,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.
@@ -4611,13 +4617,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
@@ -359,12 +359,18 @@
 /// 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. For provenance,
+/// `UnknownProvenance` 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));
+  return const_cast<Value *>(
+      getUnderlyingObject(VConst, MaxLookup, FollowProvenance));
 }
 
 /// This method is similar to getUnderlyingObject except that it can
@@ -395,9 +401,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.497802.patch
Type: text/x-patch
Size: 4017 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230215/477c06af/attachment.bin>


More information about the llvm-commits mailing list