diff --git a/llvm/include/llvm/CodeGen/LexicalScopes.h b/llvm/include/llvm/CodeGen/LexicalScopes.h --- a/llvm/include/llvm/CodeGen/LexicalScopes.h +++ b/llvm/include/llvm/CodeGen/LexicalScopes.h @@ -163,8 +163,8 @@ void getMachineBasicBlocks(const DILocation *DL, SmallPtrSetImpl &MBBs); - /// dominates - Return true if DebugLoc's lexical scope dominates at least one - /// machine instruction's lexical scope in a given machine basic block. + /// Return true if DebugLoc's lexical scope dominates at least one machine + /// instruction's lexical scope in a given machine basic block. bool dominates(const DILocation *DL, MachineBasicBlock *MBB); /// findLexicalScope - Find lexical scope, either regular or inlined, for the @@ -250,6 +250,11 @@ /// CurrentFnLexicalScope - Top level scope for the current function. /// LexicalScope *CurrentFnLexicalScope = nullptr; + + /// Map a location to the set of basic blocks it dominates. This is a cache + /// for \ref LexicalScopes::getMachineBasicBlocks results. + using BlockSetT = SmallPtrSet; + DenseMap> DominatedBlocks; }; } // end namespace llvm diff --git a/llvm/lib/CodeGen/LexicalScopes.cpp b/llvm/lib/CodeGen/LexicalScopes.cpp --- a/llvm/lib/CodeGen/LexicalScopes.cpp +++ b/llvm/lib/CodeGen/LexicalScopes.cpp @@ -44,6 +44,7 @@ AbstractScopeMap.clear(); InlinedLexicalScopeMap.clear(); AbstractScopesList.clear(); + DominatedBlocks.clear(); } /// initialize - Scan machine function and constuct lexical scope nest. @@ -302,8 +303,6 @@ MBBs.insert(&*CurMBBIt); } -/// dominates - Return true if DebugLoc's lexical scope dominates at least one -/// machine instruction's lexical scope in a given machine basic block. bool LexicalScopes::dominates(const DILocation *DL, MachineBasicBlock *MBB) { assert(MF && "Unexpected uninitialized LexicalScopes object!"); LexicalScope *Scope = getOrCreateLexicalScope(DL); @@ -315,11 +314,17 @@ return true; // Fetch all the blocks in DLs scope. Because the range / block list also - // contain any subscopes, any instruction that DL dominates can be found - // in the block set. - SmallPtrSet Set; - getMachineBasicBlocks(DL, Set); - return Set.count(MBB) != 0; + // contain any subscopes, any instruction that DL dominates can be found in + // the block set. + // + // Cache the set of fetched blocks to avoid repeatedly recomputing the set in + // the LiveDebugValues pass. + std::unique_ptr &Set = DominatedBlocks[DL]; + if (!Set) { + Set = std::make_unique(); + getMachineBasicBlocks(DL, *Set); + } + return Set->count(MBB) != 0; } #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) diff --git a/llvm/lib/CodeGen/LiveDebugValues.cpp b/llvm/lib/CodeGen/LiveDebugValues.cpp --- a/llvm/lib/CodeGen/LiveDebugValues.cpp +++ b/llvm/lib/CodeGen/LiveDebugValues.cpp @@ -203,20 +203,15 @@ /// Keeps track of lexical scopes associated with a user value's source /// location. class UserValueScopes { - DebugLoc DL; + const DebugLoc &DL; LexicalScopes &LS; - SmallPtrSet LBlocks; public: - UserValueScopes(DebugLoc D, LexicalScopes &L) : DL(std::move(D)), LS(L) {} + UserValueScopes(const DebugLoc &D, LexicalScopes &L) : DL(D), LS(L) {} /// Return true if current scope dominates at least one machine /// instruction in a given machine basic block. - bool dominates(MachineBasicBlock *MBB) { - if (LBlocks.empty()) - LS.getMachineBasicBlocks(DL, LBlocks); - return LBlocks.count(MBB) != 0 || LS.dominates(DL, MBB); - } + bool dominates(MachineBasicBlock *MBB) { return LS.dominates(DL, MBB); } }; using FragmentInfo = DIExpression::FragmentInfo;