diff --git a/llvm/lib/Analysis/BranchProbabilityInfo.cpp b/llvm/lib/Analysis/BranchProbabilityInfo.cpp index cd3e3a4991327..50dcd5f45233f 100644 --- a/llvm/lib/Analysis/BranchProbabilityInfo.cpp +++ b/llvm/lib/Analysis/BranchProbabilityInfo.cpp @@ -810,6 +810,7 @@ void BranchProbabilityInfo::computeEestimateBlockWeight( const Function &F, DominatorTree *DT, PostDominatorTree *PDT) { SmallVector BlockWorkList; SmallVector LoopWorkList; + SmallDenseMap> LoopExitBlocks; // By doing RPO we make sure that all predecessors already have weights // calculated before visiting theirs successors. @@ -828,12 +829,14 @@ void BranchProbabilityInfo::computeEestimateBlockWeight( do { while (!LoopWorkList.empty()) { const LoopBlock LoopBB = LoopWorkList.pop_back_val(); - - if (EstimatedLoopWeight.count(LoopBB.getLoopData())) + const LoopData LD = LoopBB.getLoopData(); + if (EstimatedLoopWeight.count(LD)) continue; - SmallVector Exits; - getLoopExitBlocks(LoopBB, Exits); + auto Res = LoopExitBlocks.try_emplace(LD); + SmallVectorImpl &Exits = Res.first->second; + if (Res.second) + getLoopExitBlocks(LoopBB, Exits); auto LoopWeight = getMaxEstimatedEdgeWeight( LoopBB, make_range(Exits.begin(), Exits.end())); @@ -842,7 +845,7 @@ void BranchProbabilityInfo::computeEestimateBlockWeight( if (LoopWeight <= static_cast(BlockExecWeight::UNREACHABLE)) LoopWeight = static_cast(BlockExecWeight::LOWEST_NON_ZERO); - EstimatedLoopWeight.insert({LoopBB.getLoopData(), *LoopWeight}); + EstimatedLoopWeight.insert({LD, *LoopWeight}); // Add all blocks entering the loop into working list. getLoopEnterBlocks(LoopBB, BlockWorkList); }