From 4def33398a961df10f9a6b54f0a9bc357f689229 Mon Sep 17 00:00:00 2001 From: Roman Lebedev Date: Thu, 25 Mar 2021 22:58:10 +0300 Subject: [PATCH] [NFCI][SimplifyCFG] Don't pay for a Small{Map,Set}Vector when plain SmallSet will suffice This *only* changes the cases where we *really* don't care about the iteration order of the underlying contained, namely when we will use the values from it to form DTU updates. --- lib/Transforms/Utils/BasicBlockUtils.cpp | 17 ++++++++--------- lib/Transforms/Utils/Local.cpp | 10 +++++----- lib/Transforms/Utils/SimplifyCFG.cpp | 18 ++++++++---------- 3 files changed, 21 insertions(+), 24 deletions(-) diff --git a/lib/Transforms/Utils/BasicBlockUtils.cpp b/lib/Transforms/Utils/BasicBlockUtils.cpp index d1a3ae5c0ed..2d5f1dd5e96 100644 --- a/lib/Transforms/Utils/BasicBlockUtils.cpp +++ b/lib/Transforms/Utils/BasicBlockUtils.cpp @@ -228,8 +228,7 @@ bool llvm::MergeBlockIntoPredecessor(BasicBlock *BB, DomTreeUpdater *DTU, // These dominator edges will be redirected from Pred. std::vector Updates; if (DTU) { - SmallSetVector UniqueSuccessors(succ_begin(BB), - succ_end(BB)); + SmallPtrSet UniqueSuccessors(succ_begin(BB), succ_end(BB)); Updates.reserve(1 + (2 * UniqueSuccessors.size())); // Add insert edges first. Experimentally, for the particular case of two // blocks that can be merged, with a single successor and single predecessor @@ -569,8 +568,8 @@ static BasicBlock *SplitBlockImpl(BasicBlock *Old, Instruction *SplitPt, if (DTU) { SmallVector Updates; // Old dominates New. New node dominates all other nodes dominated by Old. - SmallSetVector UniqueSuccessorsOfOld(succ_begin(New), - succ_end(New)); + SmallPtrSet UniqueSuccessorsOfOld(succ_begin(New), + succ_end(New)); Updates.push_back({DominatorTree::Insert, Old, New}); Updates.reserve(Updates.size() + 2 * UniqueSuccessorsOfOld.size()); for (BasicBlock *UniqueSuccessorOfOld : UniqueSuccessorsOfOld) { @@ -635,8 +634,8 @@ BasicBlock *llvm::splitBlockBefore(BasicBlock *Old, Instruction *SplitPt, SmallVector DTUpdates; // New dominates Old. The predecessor nodes of the Old node dominate // New node. - SmallSetVector UniquePredecessorsOfOld(pred_begin(New), - pred_end(New)); + SmallPtrSet UniquePredecessorsOfOld(pred_begin(New), + pred_end(New)); DTUpdates.push_back({DominatorTree::Insert, New, Old}); DTUpdates.reserve(DTUpdates.size() + 2 * UniquePredecessorsOfOld.size()); for (BasicBlock *UniquePredecessorOfOld : UniquePredecessorsOfOld) { @@ -675,7 +674,7 @@ static void UpdateAnalysisInformation(BasicBlock *OldBB, BasicBlock *NewBB, } else { // Split block expects NewBB to have a non-empty set of predecessors. SmallVector Updates; - SmallSetVector UniquePreds(Preds.begin(), Preds.end()); + SmallPtrSet UniquePreds(Preds.begin(), Preds.end()); Updates.push_back({DominatorTree::Insert, NewBB, OldBB}); Updates.reserve(Updates.size() + 2 * UniquePreds.size()); for (auto *UniquePred : UniquePreds) { @@ -1141,8 +1140,8 @@ SplitBlockAndInsertIfThenImpl(Value *Cond, Instruction *SplitBefore, BasicBlock *Head = SplitBefore->getParent(); BasicBlock *Tail = Head->splitBasicBlock(SplitBefore->getIterator()); if (DTU) { - SmallSetVector UniqueSuccessorsOfHead(succ_begin(Tail), - succ_end(Tail)); + SmallPtrSet UniqueSuccessorsOfHead(succ_begin(Tail), + succ_end(Tail)); Updates.push_back({DominatorTree::Insert, Head, Tail}); Updates.reserve(Updates.size() + 2 * UniqueSuccessorsOfHead.size()); for (BasicBlock *UniqueSuccessorOfHead : UniqueSuccessorsOfHead) { diff --git a/lib/Transforms/Utils/Local.cpp b/lib/Transforms/Utils/Local.cpp index 5b3472c7348..5eabb1056bc 100644 --- a/lib/Transforms/Utils/Local.cpp +++ b/lib/Transforms/Utils/Local.cpp @@ -258,7 +258,7 @@ bool llvm::ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions, Builder.CreateBr(TheOnlyDest); BasicBlock *BB = SI->getParent(); - SmallSetVector RemovedSuccessors; + SmallSet RemovedSuccessors; // Remove entries from PHI nodes which we no longer branch to... BasicBlock *SuccToKeep = TheOnlyDest; @@ -330,7 +330,7 @@ bool llvm::ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions, if (auto *BA = dyn_cast(IBI->getAddress()->stripPointerCasts())) { BasicBlock *TheOnlyDest = BA->getBasicBlock(); - SmallSetVector RemovedSuccessors; + SmallSet RemovedSuccessors; // Insert the new branch. Builder.CreateBr(TheOnlyDest); @@ -2132,7 +2132,7 @@ unsigned llvm::changeToUnreachable(Instruction *I, bool UseLLVMTrap, if (MSSAU) MSSAU->changeToUnreachable(I); - SmallSetVector UniqueSuccessors; + SmallSet UniqueSuccessors; // Loop over all of the successors, removing BB's entry from any PHI // nodes. @@ -2393,7 +2393,7 @@ static bool markAliveBlocks(Function &F, } }; - SmallMapVector NumPerSuccessorCases; + SmallDenseMap NumPerSuccessorCases; // Set of unique CatchPads. SmallDenseMap> @@ -2507,7 +2507,7 @@ bool llvm::removeUnreachableBlocks(Function &F, DomTreeUpdater *DTU, // their internal references. Update DTU if available. std::vector Updates; for (auto *BB : BlocksToRemove) { - SmallSetVector UniqueSuccessors; + SmallSet UniqueSuccessors; for (BasicBlock *Successor : successors(BB)) { // Only remove references to BB in reachable successors of BB. if (Reachable.count(Successor)) diff --git a/lib/Transforms/Utils/SimplifyCFG.cpp b/lib/Transforms/Utils/SimplifyCFG.cpp index c34ef3dec26..b5f14530875 100644 --- a/lib/Transforms/Utils/SimplifyCFG.cpp +++ b/lib/Transforms/Utils/SimplifyCFG.cpp @@ -910,7 +910,7 @@ bool SimplifyCFGOpt::SimplifyEqualityComparisonWithOnlyPredecessor( LLVM_DEBUG(dbgs() << "Threading pred instr: " << *Pred->getTerminator() << "Through successor TI: " << *TI); - SmallMapVector NumPerSuccessorCases; + SmallDenseMap NumPerSuccessorCases; for (SwitchInst::CaseIt i = SI->case_end(), e = SI->case_begin(); i != e;) { --i; auto *Successor = i->getCaseSuccessor(); @@ -961,7 +961,7 @@ bool SimplifyCFGOpt::SimplifyEqualityComparisonWithOnlyPredecessor( if (!TheRealDest) TheRealDest = ThisDef; - SmallSetVector RemovedSuccs; + SmallPtrSet RemovedSuccs; // Remove PHI node entries for dead edges. BasicBlock *CheckEdge = TheRealDest; @@ -3793,7 +3793,7 @@ bool SimplifyCFGOpt::SimplifyTerminatorOnSelect(Instruction *OldTerm, BasicBlock *KeepEdge1 = TrueBB; BasicBlock *KeepEdge2 = TrueBB != FalseBB ? FalseBB : nullptr; - SmallSetVector RemovedSuccessors; + SmallPtrSet RemovedSuccessors; // Then remove the rest. for (BasicBlock *Succ : successors(OldTerm)) { @@ -4913,7 +4913,7 @@ static bool eliminateDeadSwitchCases(SwitchInst *SI, DomTreeUpdater *DTU, // Gather dead cases. SmallVector DeadCases; - SmallMapVector NumPerSuccessorCases; + SmallDenseMap NumPerSuccessorCases; for (auto &Case : SI->cases()) { auto *Successor = Case.getCaseSuccessor(); if (DTU) @@ -5999,7 +5999,7 @@ static bool SwitchToLookupTable(SwitchInst *SI, IRBuilder<> &Builder, } // Remove the switch. - SmallSetVector RemovedSuccessors; + SmallPtrSet RemovedSuccessors; for (unsigned i = 0, e = SI->getNumSuccessors(); i < e; ++i) { BasicBlock *Succ = SI->getSuccessor(i); @@ -6181,7 +6181,7 @@ bool SimplifyCFGOpt::simplifyIndirectBr(IndirectBrInst *IBI) { // Eliminate redundant destinations. SmallPtrSet Succs; - SmallSetVector RemovedSuccs; + SmallPtrSet RemovedSuccs; for (unsigned i = 0, e = IBI->getNumDestinations(); i != e; ++i) { BasicBlock *Dest = IBI->getDestination(i); if (!Dest->hasAddressTaken() || !Succs.insert(Dest).second) { @@ -6271,8 +6271,7 @@ static bool TryToMergeLandingPad(LandingPadInst *LPad, BranchInst *BI, // We've found an identical block. Update our predecessors to take that // path instead and make ourselves dead. - SmallPtrSet Preds; - Preds.insert(pred_begin(BB), pred_end(BB)); + SmallPtrSet Preds(pred_begin(BB), pred_end(BB)); for (BasicBlock *Pred : Preds) { InvokeInst *II = cast(Pred->getTerminator()); assert(II->getNormalDest() != BB && II->getUnwindDest() == BB && @@ -6293,8 +6292,7 @@ static bool TryToMergeLandingPad(LandingPadInst *LPad, BranchInst *BI, Inst.eraseFromParent(); } - SmallPtrSet Succs; - Succs.insert(succ_begin(BB), succ_end(BB)); + SmallPtrSet Succs(succ_begin(BB), succ_end(BB)); for (BasicBlock *Succ : Succs) { Succ->removePredecessor(BB); if (DTU)