From d9a29a67527fbabd0e0388aefa8e6aa9b19f3f03 Mon Sep 17 00:00:00 2001 From: Philip Reames Date: Mon, 8 Mar 2021 11:32:31 -0800 Subject: [PATCH] constify getUnderlyingObject implementation [nfc] --- llvm/include/llvm/Analysis/ValueTracking.h | 9 +++++---- llvm/lib/Analysis/ValueTracking.cpp | 6 +++--- 2 files changed, 8 insertions(+), 7 deletions(-) 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 6437a4aa49b8..80f03d3379de 100644 --- a/llvm/lib/Analysis/ValueTracking.cpp +++ b/llvm/lib/Analysis/ValueTracking.cpp @@ -4162,18 +4162,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();