If we are extracting a block that has multiple successors that are the same
authorChris Lattner <sabre@nondot.org>
Fri, 13 Aug 2004 03:27:07 +0000 (03:27 +0000)
committerChris Lattner <sabre@nondot.org>
Fri, 13 Aug 2004 03:27:07 +0000 (03:27 +0000)
block (common in a switch), make sure to remove extra edges in successor
blocks.  This fixes CodeExtractor/2004-08-12-BlockExtractPHI.ll and should
be pulled into LLVM 1.3 (though the regression test need not be, as that
would require pulling in the LoopExtract.cpp changes).

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

lib/Transforms/Utils/CodeExtractor.cpp

index 4521038dd660ce735fa0917917e3baaa6a65bbbf..aabc587b56d1c862ce01736c6bdf71544b86418b 100644 (file)
@@ -657,10 +657,19 @@ ExtractCodeRegion(const std::vector<BasicBlock*> &code) {
                                  succ_end(codeReplacer));
   for (unsigned i = 0, e = Succs.size(); i != e; ++i)
     for (BasicBlock::iterator I = Succs[i]->begin();
-         PHINode *PN = dyn_cast<PHINode>(I); ++I)
+         PHINode *PN = dyn_cast<PHINode>(I); ++I) {
+      std::set<BasicBlock*> ProcessedPreds;
       for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
         if (BlocksToExtract.count(PN->getIncomingBlock(i)))
-          PN->setIncomingBlock(i, codeReplacer);
+          if (ProcessedPreds.insert(PN->getIncomingBlock(i)).second)
+            PN->setIncomingBlock(i, codeReplacer);
+          else {
+            // There were multiple entries in the PHI for this block, now there
+            // is only one, so remove the duplicated entries.
+            PN->removeIncomingValue(i, false);
+            --i; --e;
+          }
+    }
   
   //std::cerr << "NEW FUNCTION: " << *newFunction;
   //  verifyFunction(*newFunction);