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 @@ -200,25 +200,6 @@ enum struct TransferKind { TransferCopy, TransferSpill, TransferRestore }; - /// Keeps track of lexical scopes associated with a user value's source - /// location. - class UserValueScopes { - DebugLoc DL; - LexicalScopes &LS; - SmallPtrSet LBlocks; - - public: - UserValueScopes(DebugLoc D, LexicalScopes &L) : DL(std::move(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); - } - }; - using FragmentInfo = DIExpression::FragmentInfo; using OptFragmentInfo = Optional; @@ -247,7 +228,6 @@ /// is moved. const MachineInstr &MI; - mutable UserValueScopes UVS; enum VarLocKind { InvalidKind = 0, RegisterKind, @@ -272,7 +252,7 @@ VarLoc(const MachineInstr &MI, LexicalScopes &LS) : Var(MI.getDebugVariable(), MI.getDebugExpression(), MI.getDebugLoc()->getInlinedAt()), - Expr(MI.getDebugExpression()), MI(MI), UVS(MI.getDebugLoc(), LS) { + Expr(MI.getDebugExpression()), MI(MI) { static_assert((sizeof(Loc) == sizeof(uint64_t)), "hash does not cover all members of Loc"); assert(MI.isDebugValue() && "not a DBG_VALUE"); @@ -438,7 +418,9 @@ /// Determine whether the lexical scope of this value's debug location /// dominates MBB. - bool dominates(MachineBasicBlock &MBB) const { return UVS.dominates(&MBB); } + bool dominates(LexicalScopes &LS, MachineBasicBlock &MBB) const { + return LS.dominates(MI.getDebugLoc().get(), &MBB); + } #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) // TRI can be null. @@ -1615,7 +1597,7 @@ if (!IsArtificial) { for (uint64_t ID : InLocsT) { LocIndex Idx = LocIndex::fromRawInteger(ID); - if (!VarLocIDs[Idx].dominates(MBB)) { + if (!VarLocIDs[Idx].dominates(LS, MBB)) { KillSet.set(ID); LLVM_DEBUG({ auto Name = VarLocIDs[Idx].Var.getVariable()->getName();