Fix some bugs, straighten stuff out, more work needs to be done.
authorChris Lattner <sabre@nondot.org>
Fri, 10 May 2002 15:37:35 +0000 (15:37 +0000)
committerChris Lattner <sabre@nondot.org>
Fri, 10 May 2002 15:37:35 +0000 (15:37 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@2600 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Scalar/ADCE.cpp

index 11f84334c2536b1faeb84e9418efdc7b0d3e2af7..4c0169171b245f0c5ea571f31f5273b30491454b 100644 (file)
@@ -188,34 +188,30 @@ void ADCE::doADCE(DominanceFrontier &CDG) {
   //
   std::set<BasicBlock*> VisitedBlocks;
   BasicBlock *EntryBlock = fixupCFG(Func->front(), VisitedBlocks, AliveBlocks);
-  if (EntryBlock && EntryBlock != Func->front()) {
-    // We need to move the new entry block to be the first bb of the function
-    Function::iterator EBI = find(Func->begin(), Func->end(), EntryBlock);
-    std::swap(*EBI, *Func->begin()); // Exchange old location with start of fn
-
-    while (PHINode *PN = dyn_cast<PHINode>(EntryBlock->front())) {
-      assert(PN->getNumIncomingValues() == 1 &&
-             "Can only have a single incoming value at this point...");
-      // The incoming value must be outside of the scope of the function, a
-      // global variable, constant or parameter maybe...
-      //
-      PN->replaceAllUsesWith(PN->getIncomingValue(0));
-
-      // Nuke the phi node...
-      delete EntryBlock->getInstList().remove(EntryBlock->begin());
-    }
-  }
 
   // Now go through and tell dead blocks to drop all of their references so they
-  // can be safely deleted.
+  // can be safely deleted.  Also, as we are doing so, if the block has
+  // successors that are still live (and that have PHI nodes in them), remove
+  // the entry for this block from the phi nodes.
   //
   for (Function::iterator BI = Func->begin(), BE = Func->end(); BI != BE; ++BI){
     BasicBlock *BB = *BI;
     if (!AliveBlocks.count(BB)) {
+      // Remove entries from successors PHI nodes if they are still alive...
+      for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI)
+        if (AliveBlocks.count(*SI)) {  // Only if the successor is alive...
+          BasicBlock *Succ = *SI;
+          for (BasicBlock::iterator I = Succ->begin();// Loop over all PHI nodes
+               PHINode *PN = dyn_cast<PHINode>(*I); ++I)
+            PN->removeIncomingValue(BB);         // Remove value for this block
+        }
+
       BB->dropAllReferences();
     }
   }
 
+  cerr << "Before Deleting Blocks: " << Func;
+
   // Now loop through all of the blocks and delete them.  We can safely do this
   // now because we know that there are no references to dead blocks (because
   // they have dropped all of their references...
@@ -228,6 +224,24 @@ void ADCE::doADCE(DominanceFrontier &CDG) {
     }
     ++BI;                                           // Increment iterator...
   }
+
+  if (EntryBlock && EntryBlock != Func->front()) {
+    // We need to move the new entry block to be the first bb of the function
+    Function::iterator EBI = find(Func->begin(), Func->end(), EntryBlock);
+    std::swap(*EBI, *Func->begin()); // Exchange old location with start of fn
+  }
+
+  while (PHINode *PN = dyn_cast<PHINode>(EntryBlock->front())) {
+    assert(PN->getNumIncomingValues() == 1 &&
+           "Can only have a single incoming value at this point...");
+    // The incoming value must be outside of the scope of the function, a
+    // global variable, constant or parameter maybe...
+    //
+    PN->replaceAllUsesWith(PN->getIncomingValue(0));
+    
+    // Nuke the phi node...
+    delete EntryBlock->getInstList().remove(EntryBlock->begin());
+  }
 }
 
 
@@ -284,7 +298,7 @@ BasicBlock *ADCE::fixupCFG(BasicBlock *BB, std::set<BasicBlock*> &VisitedBlocks,
     for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI) {
       BasicBlock *RetBB = fixupCFG(*SI, VisitedBlocks, AliveBlocks);
       if (RetBB) {
-       assert(ReturnBB == 0 && "One one live child allowed!");
+       assert(ReturnBB == 0 && "At most one live child allowed!");
        ReturnBB = RetBB;
       }
     }