[LowerSwitch] Skip dead blocks for processSwitchInst()
authorChen Li <meloli87@gmail.com>
Tue, 11 Aug 2015 20:16:17 +0000 (20:16 +0000)
committerChen Li <meloli87@gmail.com>
Tue, 11 Aug 2015 20:16:17 +0000 (20:16 +0000)
Summary: This patch adds check for dead blocks and skip them for processSwitchInst(). This will help reduce compilation time.

Reviewers: reames, hans

Subscribers: llvm-commits

Differential Revision: http://reviews.llvm.org/D11953

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@244656 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Utils/LowerSwitch.cpp

index 0e47626ce5313496805ad35c198a05ad724fb8dd..9cd85fa45c6b010d7bac3155b65eba2fd7f751da 100644 (file)
@@ -78,7 +78,7 @@ namespace {
     typedef std::vector<CaseRange> CaseVector;
     typedef std::vector<CaseRange>::iterator CaseItr;
   private:
-    void processSwitchInst(SwitchInst *SI, SmallVectorImpl<BasicBlock*> &DeleteList);
+    void processSwitchInst(SwitchInst *SI, SmallPtrSetImpl<BasicBlock*> &DeleteList);
 
     BasicBlock *switchConvert(CaseItr Begin, CaseItr End,
                               ConstantInt *LowerBound, ConstantInt *UpperBound,
@@ -116,11 +116,16 @@ FunctionPass *llvm::createLowerSwitchPass() {
 
 bool LowerSwitch::runOnFunction(Function &F) {
   bool Changed = false;
-  SmallVector<BasicBlock*, 8> DeleteList;
+  SmallPtrSet<BasicBlock*, 8> DeleteList;
 
   for (Function::iterator I = F.begin(), E = F.end(); I != E; ) {
     BasicBlock *Cur = I++; // Advance over block so we don't traverse new blocks
 
+    // If the block is a dead Default block that will be deleted later, don't
+    // waste time processing it.
+    if (DeleteList.count(Cur))
+      continue;
+
     if (SwitchInst *SI = dyn_cast<SwitchInst>(Cur->getTerminator())) {
       Changed = true;
       processSwitchInst(SI, DeleteList);
@@ -402,7 +407,8 @@ unsigned LowerSwitch::Clusterify(CaseVector& Cases, SwitchInst *SI) {
 // processSwitchInst - Replace the specified switch instruction with a sequence
 // of chained if-then insts in a balanced binary search.
 //
-void LowerSwitch::processSwitchInst(SwitchInst *SI, SmallVectorImpl<BasicBlock*> &DeleteList) {
+void LowerSwitch::processSwitchInst(SwitchInst *SI,
+                                    SmallPtrSetImpl<BasicBlock*> &DeleteList) {
   BasicBlock *CurBlock = SI->getParent();
   BasicBlock *OrigBlock = CurBlock;
   Function *F = CurBlock->getParent();
@@ -525,5 +531,5 @@ void LowerSwitch::processSwitchInst(SwitchInst *SI, SmallVectorImpl<BasicBlock*>
 
   // If the Default block has no more predecessors just add it to DeleteList.
   if (pred_begin(OldDefault) == pred_end(OldDefault))
-    DeleteList.push_back(OldDefault);
+    DeleteList.insert(OldDefault);
 }