diff --git a/llvm/include/llvm/IR/DebugInfo.h b/llvm/include/llvm/IR/DebugInfo.h index 684bc7b026ff0..eb5728647a81d 100644 --- a/llvm/include/llvm/IR/DebugInfo.h +++ b/llvm/include/llvm/IR/DebugInfo.h @@ -40,8 +40,9 @@ class Module; /// Finds dbg.declare intrinsics declaring local variables as living in the /// memory that 'V' points to. -void findDbgDeclares(SmallVectorImpl &DbgUsers, Value *V, - SmallVectorImpl *DPValues = nullptr); +TinyPtrVector findDbgDeclares(Value *V); +/// As above, for DPVDeclares. +TinyPtrVector findDPVDeclares(Value *V); /// Finds the llvm.dbg.value intrinsics describing a value. void findDbgValues(SmallVectorImpl &DbgValues, diff --git a/llvm/lib/IR/DebugInfo.cpp b/llvm/lib/IR/DebugInfo.cpp index 316197aba727f..6b7dd74916517 100644 --- a/llvm/lib/IR/DebugInfo.cpp +++ b/llvm/lib/IR/DebugInfo.cpp @@ -44,6 +44,42 @@ using namespace llvm; using namespace llvm::at; using namespace llvm::dwarf; +TinyPtrVector llvm::findDbgDeclares(Value *V) { + // This function is hot. Check whether the value has any metadata to avoid a + // DenseMap lookup. + if (!V->isUsedByMetadata()) + return {}; + auto *L = LocalAsMetadata::getIfExists(V); + if (!L) + return {}; + auto *MDV = MetadataAsValue::getIfExists(V->getContext(), L); + if (!MDV) + return {}; + + TinyPtrVector Declares; + for (User *U : MDV->users()) + if (auto *DDI = dyn_cast(U)) + Declares.push_back(DDI); + + return Declares; +} +TinyPtrVector llvm::findDPVDeclares(Value *V) { + // This function is hot. Check whether the value has any metadata to avoid a + // DenseMap lookup. + if (!V->isUsedByMetadata()) + return {}; + auto *L = LocalAsMetadata::getIfExists(V); + if (!L) + return {}; + + TinyPtrVector Declares; + for (DPValue *DPV : L->getAllDPValueUsers()) + if (DPV->getType() == DPValue::LocationType::Declare) + Declares.push_back(DPV); + + return Declares; +} + template static void findDbgIntrinsics(SmallVectorImpl &Result, Value *V, @@ -97,12 +133,6 @@ static void findDbgIntrinsics(SmallVectorImpl &Result, Value *V, } } -void llvm::findDbgDeclares(SmallVectorImpl &DbgUsers, - Value *V, SmallVectorImpl *DPValues) { - findDbgIntrinsics(DbgUsers, V, - DPValues); -} - void llvm::findDbgValues(SmallVectorImpl &DbgValues, Value *V, SmallVectorImpl *DPValues) { findDbgIntrinsics(DbgValues, V, diff --git a/llvm/lib/Transforms/Coroutines/CoroFrame.cpp b/llvm/lib/Transforms/Coroutines/CoroFrame.cpp index 4d2b31fbbcedb..429344652a4c4 100644 --- a/llvm/lib/Transforms/Coroutines/CoroFrame.cpp +++ b/llvm/lib/Transforms/Coroutines/CoroFrame.cpp @@ -963,18 +963,15 @@ static void cacheDIVar(FrameDataInfo &FrameData, if (DIVarCache.contains(V)) continue; - SmallVector DDIs; - SmallVector DPVs; - findDbgDeclares(DDIs, V, &DPVs); - auto CacheIt = [&DIVarCache, V](auto &Container) { + auto CacheIt = [&DIVarCache, V](auto Container) { auto *I = llvm::find_if(Container, [](auto *DDI) { return DDI->getExpression()->getNumElements() == 0; }); if (I != Container.end()) DIVarCache.insert({V, (*I)->getVariable()}); }; - CacheIt(DDIs); - CacheIt(DPVs); + CacheIt(findDbgDeclares(V)); + CacheIt(findDPVDeclares(V)); } } @@ -1125,9 +1122,8 @@ static void buildFrameDebugInfo(Function &F, coro::Shape &Shape, assert(PromiseAlloca && "Coroutine with switch ABI should own Promise alloca"); - SmallVector DIs; - SmallVector DPVs; - findDbgDeclares(DIs, PromiseAlloca, &DPVs); + TinyPtrVector DIs = findDbgDeclares(PromiseAlloca); + TinyPtrVector DPVs = findDPVDeclares(PromiseAlloca); DILocalVariable *PromiseDIVariable = nullptr; DILocation *DILoc = nullptr; @@ -1865,9 +1861,8 @@ static void insertSpills(const FrameDataInfo &FrameData, coro::Shape &Shape) { FrameTy->getElementType(FrameData.getFieldIndex(E.first)), GEP, SpillAlignment, E.first->getName() + Twine(".reload")); - SmallVector DIs; - SmallVector DPVs; - findDbgDeclares(DIs, Def, &DPVs); + TinyPtrVector DIs = findDbgDeclares(Def); + TinyPtrVector DPVs = findDPVDeclares(Def); // Try best to find dbg.declare. If the spill is a temp, there may not // be a direct dbg.declare. Walk up the load chain to find one from an // alias. @@ -1881,9 +1876,8 @@ static void insertSpills(const FrameDataInfo &FrameData, coro::Shape &Shape) { CurDef = LdInst->getPointerOperand(); if (!isa(CurDef)) break; - DIs.clear(); - DPVs.clear(); - findDbgDeclares(DIs, CurDef, &DPVs); + DIs = findDbgDeclares(CurDef); + DPVs = findDPVDeclares(CurDef); } } diff --git a/llvm/lib/Transforms/Scalar/SROA.cpp b/llvm/lib/Transforms/Scalar/SROA.cpp index a40ca7fd6b7de..adf87ebb69f54 100644 --- a/llvm/lib/Transforms/Scalar/SROA.cpp +++ b/llvm/lib/Transforms/Scalar/SROA.cpp @@ -5021,9 +5021,6 @@ bool SROA::splitAlloca(AllocaInst &AI, AllocaSlices &AS) { // Remove any existing intrinsics on the new alloca describing // the variable fragment. - SmallVector FragDbgDeclares; - SmallVector FragDPVs; - findDbgDeclares(FragDbgDeclares, Fragment.Alloca, &FragDPVs); auto RemoveOne = [DbgVariable](auto *OldDII) { auto SameVariableFragment = [](const auto *LHS, const auto *RHS) { return LHS->getVariable() == RHS->getVariable() && @@ -5033,8 +5030,8 @@ bool SROA::splitAlloca(AllocaInst &AI, AllocaSlices &AS) { if (SameVariableFragment(OldDII, DbgVariable)) OldDII->eraseFromParent(); }; - for_each(FragDbgDeclares, RemoveOne); - for_each(FragDPVs, RemoveOne); + for_each(findDbgDeclares(Fragment.Alloca), RemoveOne); + for_each(findDPVDeclares(Fragment.Alloca), RemoveOne); insertNewDbgInst(DIB, DbgVariable, Fragment.Alloca, FragmentExpr, &AI); } @@ -5042,11 +5039,8 @@ bool SROA::splitAlloca(AllocaInst &AI, AllocaSlices &AS) { // Migrate debug information from the old alloca to the new alloca(s) // and the individual partitions. - SmallVector DbgDeclares; - SmallVector DPValues; - findDbgDeclares(DbgDeclares, &AI, &DPValues); - for_each(DbgDeclares, MigrateOne); - for_each(DPValues, MigrateOne); + for_each(findDbgDeclares(&AI), MigrateOne); + for_each(findDPVDeclares(&AI), MigrateOne); for_each(at::getAssignmentMarkers(&AI), MigrateOne); return Changed; @@ -5169,12 +5163,9 @@ bool SROA::deleteDeadInstructions( // not be able to find it. if (AllocaInst *AI = dyn_cast(I)) { DeletedAllocas.insert(AI); - SmallVector DbgDeclares; - SmallVector DPValues; - findDbgDeclares(DbgDeclares, AI, &DPValues); - for (DbgDeclareInst *OldDII : DbgDeclares) + for (DbgDeclareInst *OldDII : findDbgDeclares(AI)) OldDII->eraseFromParent(); - for (DPValue *OldDII : DPValues) + for (DPValue *OldDII : findDPVDeclares(AI)) OldDII->eraseFromParent(); } diff --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp index b9cad764aaef8..d1b42f28923f5 100644 --- a/llvm/lib/Transforms/Utils/Local.cpp +++ b/llvm/lib/Transforms/Utils/Local.cpp @@ -2130,9 +2130,8 @@ void llvm::insertDebugValuesForPHIs(BasicBlock *BB, bool llvm::replaceDbgDeclare(Value *Address, Value *NewAddress, DIBuilder &Builder, uint8_t DIExprFlags, int Offset) { - SmallVector DbgDeclares; - SmallVector DPValues; - findDbgDeclares(DbgDeclares, Address, &DPValues); + TinyPtrVector DbgDeclares = findDbgDeclares(Address); + TinyPtrVector DPVDeclares = findDPVDeclares(Address); auto ReplaceOne = [&](auto *DII) { assert(DII->getVariable() && "Missing variable"); @@ -2143,9 +2142,9 @@ bool llvm::replaceDbgDeclare(Value *Address, Value *NewAddress, }; for_each(DbgDeclares, ReplaceOne); - for_each(DPValues, ReplaceOne); + for_each(DPVDeclares, ReplaceOne); - return !DbgDeclares.empty() || !DPValues.empty(); + return !DbgDeclares.empty() || !DPVDeclares.empty(); } static void updateOneDbgValueForAlloca(const DebugLoc &Loc, diff --git a/llvm/lib/Transforms/Utils/MemoryOpRemark.cpp b/llvm/lib/Transforms/Utils/MemoryOpRemark.cpp index 47c6bcbaf26ec..d671a9373bf02 100644 --- a/llvm/lib/Transforms/Utils/MemoryOpRemark.cpp +++ b/llvm/lib/Transforms/Utils/MemoryOpRemark.cpp @@ -321,9 +321,6 @@ void MemoryOpRemark::visitVariable(const Value *V, bool FoundDI = false; // Try to get an llvm.dbg.declare, which has a DILocalVariable giving us the // real debug info name and size of the variable. - SmallVector DbgDeclares; - SmallVector DPValues; - findDbgDeclares(DbgDeclares, const_cast(V), &DPValues); auto FindDI = [&](const auto *DVI) { if (DILocalVariable *DILV = DVI->getVariable()) { std::optional DISize = getSizeInBytes(DILV->getSizeInBits()); @@ -334,8 +331,8 @@ void MemoryOpRemark::visitVariable(const Value *V, } } }; - for_each(DbgDeclares, FindDI); - for_each(DPValues, FindDI); + for_each(findDbgDeclares(const_cast(V)), FindDI); + for_each(findDPVDeclares(const_cast(V)), FindDI); if (FoundDI) { assert(!Result.empty());