[llvm] aa440ba - [NFCI][ValueTracking] getUnderlyingObject(): gracefully handle cycles

Roman Lebedev via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 15 03:51:16 PDT 2021


Author: Roman Lebedev
Date: 2021-03-15T13:51:02+03:00
New Revision: aa440ba24dc25e4c95f6dcf8ff647024f3b12661

URL: https://github.com/llvm/llvm-project/commit/aa440ba24dc25e4c95f6dcf8ff647024f3b12661
DIFF: https://github.com/llvm/llvm-project/commit/aa440ba24dc25e4c95f6dcf8ff647024f3b12661.diff

LOG: [NFCI][ValueTracking] getUnderlyingObject(): gracefully handle cycles

Normally, this function just doesn't bother about cycles,
and hopes that the caller supplied small-enough depth
so that at worst it will take a potentially large,
but limited amount of time. But that obviously doesn't work
if there is no depth limit.

This reapples 36f1c3db66f7268ea3183bcf0bbf05b3e1c570b4,
but without asserting, just bailout once cycle is detected.

Added: 
    

Modified: 
    llvm/lib/Analysis/ValueTracking.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 79399cf058bd..5fe026915477 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -4165,7 +4165,13 @@ static bool isSameUnderlyingObjectInLoop(const PHINode *PN,
 const Value *llvm::getUnderlyingObject(const Value *V, unsigned MaxLookup) {
   if (!V->getType()->isPointerTy())
     return V;
+  // Keep track of all the values we have recursed through.
+  SmallPtrSet<const Value *, 8> Visited;
   for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) {
+    // Did we encounter this value already?
+    if (!Visited.insert(V).second)
+      return V; // Cycle detected, we must be in an unreachable code.
+    // Otherwise, recurse further.
     if (auto *GEP = dyn_cast<GEPOperator>(V)) {
       V = GEP->getPointerOperand();
     } else if (Operator::getOpcode(V) == Instruction::BitCast ||


        


More information about the llvm-commits mailing list