[llvm-commits] [llvm] r60463 - in /llvm/trunk: include/llvm/Transforms/Utils/BasicBlockUtils.h lib/Transforms/Utils/BasicBlockUtils.cpp lib/Transforms/Utils/SimplifyCFG.cpp

Chris Lattner sabre at nondot.org
Tue Dec 2 22:37:44 PST 2008


Author: lattner
Date: Wed Dec  3 00:37:44 2008
New Revision: 60463

URL: http://llvm.org/viewvc/llvm-project?rev=60463&view=rev
Log:
Factor some code out of SimplifyCFG, forming a new 
DeleteBlockIfDead method.

Modified:
    llvm/trunk/include/llvm/Transforms/Utils/BasicBlockUtils.h
    llvm/trunk/lib/Transforms/Utils/BasicBlockUtils.cpp
    llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp

Modified: llvm/trunk/include/llvm/Transforms/Utils/BasicBlockUtils.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/Utils/BasicBlockUtils.h?rev=60463&r1=60462&r2=60463&view=diff

==============================================================================
--- llvm/trunk/include/llvm/Transforms/Utils/BasicBlockUtils.h (original)
+++ llvm/trunk/include/llvm/Transforms/Utils/BasicBlockUtils.h Wed Dec  3 00:37:44 2008
@@ -26,6 +26,11 @@
 class Pass;
 class AliasAnalysis;
 
+/// DeleteBlockIfDead - If the specified basic block is trivially dead (has no
+/// predecessors and not the entry block), delete it and return true.  Otherwise
+/// return false.
+bool DeleteBlockIfDead(BasicBlock *BB);
+  
 /// MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor,
 /// if possible.  The return value indicates success or failure.
 bool MergeBlockIntoPredecessor(BasicBlock* BB, Pass* P = 0);

Modified: llvm/trunk/lib/Transforms/Utils/BasicBlockUtils.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/BasicBlockUtils.cpp?rev=60463&r1=60462&r2=60463&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Utils/BasicBlockUtils.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/BasicBlockUtils.cpp Wed Dec  3 00:37:44 2008
@@ -24,6 +24,39 @@
 #include <algorithm>
 using namespace llvm;
 
+/// DeleteBlockIfDead - If the specified basic block is trivially dead (has no
+/// predecessors and not the entry block), delete it and return true.  Otherwise
+/// return false.
+bool llvm::DeleteBlockIfDead(BasicBlock *BB) {
+  if (pred_begin(BB) != pred_end(BB) ||
+      BB == &BB->getParent()->getEntryBlock())
+    return false;
+  
+  TerminatorInst *BBTerm = BB->getTerminator();
+  
+  // Loop through all of our successors and make sure they know that one
+  // of their predecessors is going away.
+  for (unsigned i = 0, e = BBTerm->getNumSuccessors(); i != e; ++i)
+    BBTerm->getSuccessor(i)->removePredecessor(BB);
+  
+  // Zap all the instructions in the block.
+  while (!BB->empty()) {
+    Instruction &I = BB->back();
+    // If this instruction is used, replace uses with an arbitrary value.
+    // Because control flow can't get here, we don't care what we replace the
+    // value with.  Note that since this block is unreachable, and all values
+    // contained within it must dominate their uses, that all uses will
+    // eventually be removed (they are themselves dead).
+    if (!I.use_empty())
+      I.replaceAllUsesWith(UndefValue::get(I.getType()));
+    BB->getInstList().pop_back();
+  }
+  
+  // Zap the block!
+  BB->eraseFromParent();
+  return true;
+}
+
 /// MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor,
 /// if possible.  The return value indicates success or failure.
 bool llvm::MergeBlockIntoPredecessor(BasicBlock* BB, Pass* P) {

Modified: llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp?rev=60463&r1=60462&r2=60463&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp Wed Dec  3 00:37:44 2008
@@ -1681,27 +1681,7 @@
   // as a predecessor.  These are unreachable.
   if (pred_begin(BB) == pred_end(BB) || BB->getSinglePredecessor() == BB) {
     DOUT << "Removing BB: \n" << *BB;
-
-    // Loop through all of our successors and make sure they know that one
-    // of their predecessors is going away.
-    for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
-      SI->removePredecessor(BB);
-
-    while (!BB->empty()) {
-      Instruction &I = BB->back();
-      // If this instruction is used, replace uses with an arbitrary
-      // value.  Because control flow can't get here, we don't care
-      // what we replace the value with.  Note that since this block is
-      // unreachable, and all values contained within it must dominate their
-      // uses, that all uses will eventually be removed.
-      if (!I.use_empty())
-        // Make all users of this instruction use undef instead
-        I.replaceAllUsesWith(UndefValue::get(I.getType()));
-
-      // Remove the instruction from the basic block
-      BB->getInstList().pop_back();
-    }
-    BB->eraseFromParent();
+    DeleteBlockIfDead(BB);
     return true;
   }
 





More information about the llvm-commits mailing list