diff --git a/llvm/include/llvm/Analysis/ValueTracking.h b/llvm/include/llvm/Analysis/ValueTracking.h index fc5150c53de5..6020bbd97bae 100644 --- a/llvm/include/llvm/Analysis/ValueTracking.h +++ b/llvm/include/llvm/Analysis/ValueTracking.h @@ -370,10 +370,11 @@ constexpr unsigned MaxAnalysisRecursionDepth = 6; /// 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. - Value *getUnderlyingObject(Value *V, unsigned MaxLookup = 6); - inline const Value *getUnderlyingObject(const Value *V, - unsigned MaxLookup = 6) { - return getUnderlyingObject(const_cast(V), MaxLookup); + const Value *getUnderlyingObject(const Value *V, unsigned MaxLookup = 6); + inline Value *getUnderlyingObject(Value *V, unsigned MaxLookup = 6) { + // Force const to avoid infinite recursion. + const Value *VConst = V; + return const_cast(getUnderlyingObject(VConst, MaxLookup)); } /// This method is similar to getUnderlyingObject except that it can diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp index 22c080c8d3f8..d7d6c53a75bf 100644 --- a/llvm/lib/Analysis/ValueTracking.cpp +++ b/llvm/lib/Analysis/ValueTracking.cpp @@ -4299,18 +4299,18 @@ static bool isSameUnderlyingObjectInLoop(const PHINode *PN, return true; } -Value *llvm::getUnderlyingObject(Value *V, unsigned MaxLookup) { +const Value *llvm::getUnderlyingObject(const Value *V, unsigned MaxLookup) { if (!V->getType()->isPointerTy()) return V; for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) { - if (GEPOperator *GEP = dyn_cast(V)) { + if (auto *GEP = dyn_cast(V)) { V = GEP->getPointerOperand(); } else if (Operator::getOpcode(V) == Instruction::BitCast || Operator::getOpcode(V) == Instruction::AddrSpaceCast) { V = cast(V)->getOperand(0); if (!V->getType()->isPointerTy()) return V; - } else if (GlobalAlias *GA = dyn_cast(V)) { + } else if (auto *GA = dyn_cast(V)) { if (GA->isInterposable()) return V; V = GA->getAliasee();