diff --git a/lib/Transforms/Utils/CodeExtractor.cpp b/lib/Transforms/Utils/CodeExtractor.cpp index 4e131b4a11a..9a47130c7b3 100644 --- a/lib/Transforms/Utils/CodeExtractor.cpp +++ b/lib/Transforms/Utils/CodeExtractor.cpp @@ -371,6 +371,19 @@ Function *CodeExtractor::ExtractCodeRegion(const std::vector &code) if (!BlocksToExtract.count(PN->getIncomingBlock(i))) PN->setIncomingBlock(i, newFuncRoot); + // Look at all successors of the codeReplacer block. If any of these blocks + // had PHI nodes in them, we need to update the "from" block to be the code + // replacer, not the original block in the extracted region. + std::vector Succs(succ_begin(codeReplacer), + succ_end(codeReplacer)); + for (unsigned i = 0, e = Succs.size(); i != e; ++i) + for (BasicBlock::iterator I = Succs[i]->begin(); + PHINode *PN = dyn_cast(I); ++I) + for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) + if (BlocksToExtract.count(PN->getIncomingBlock(i))) + PN->setIncomingBlock(i, codeReplacer); + + DEBUG(if (verifyFunction(*newFunction)) abort()); return newFunction; }