Index: /Users/rriddle/Desktop/llvm/llvm/include/llvm/Transforms/Utils/CodeExtractor.h =================================================================== --- /Users/rriddle/Desktop/llvm/llvm/include/llvm/Transforms/Utils/CodeExtractor.h +++ /Users/rriddle/Desktop/llvm/llvm/include/llvm/Transforms/Utils/CodeExtractor.h @@ -54,6 +54,12 @@ Type *RetTy; public: + + /// \brief Check to see if a block is valid for extraction. + /// + /// Blocks containing EHPads, allocas, invokes, or vastarts are not valid. + static bool CodeExtractor::isBlockValidForExtraction(const BasicBlock &BB); + /// \brief Create a code extractor for a single basic block. /// /// In this formation, we don't require a dominator tree. The given basic @@ -123,4 +129,4 @@ }; } -#endif +#endif \ No newline at end of file Index: /Users/rriddle/Desktop/llvm/llvm/lib/Transforms/Utils/CodeExtractor.cpp =================================================================== --- /Users/rriddle/Desktop/llvm/llvm/lib/Transforms/Utils/CodeExtractor.cpp +++ /Users/rriddle/Desktop/llvm/llvm/lib/Transforms/Utils/CodeExtractor.cpp @@ -49,7 +49,7 @@ cl::desc("Aggregate arguments to code-extracted functions")); /// \brief Test whether a block is valid for extraction. -static bool isBlockValidForExtraction(const BasicBlock &BB) { +bool CodeExtractor::isBlockValidForExtraction(const BasicBlock &BB) { // Landing pads must be in the function where they were inserted for cleanup. if (BB.isEHPad()) return false; @@ -81,7 +81,7 @@ if (!Result.insert(*BBBegin)) llvm_unreachable("Repeated basic blocks in extraction input"); - if (!isBlockValidForExtraction(**BBBegin)) { + if (!CodeExtractor::isBlockValidForExtraction(**BBBegin)) { Result.clear(); return Result; } @@ -339,7 +339,7 @@ // If the old function is no-throw, so is the new one. if (oldFunction->doesNotThrow()) newFunction->setDoesNotThrow(); - + newFunction->getBasicBlockList().push_back(newRootNode); // Create an iterator to name all of the arguments we inserted. @@ -413,7 +413,7 @@ // Emit a call to the new function, passing in: *pointer to struct (if // aggregating parameters), or plan inputs and allocated memory for outputs std::vector params, StructValues, ReloadOutputs, Reloads; - + LLVMContext &Context = newFunction->getContext(); // Add inputs as params, or to be filled into the struct @@ -630,7 +630,7 @@ } else { // Otherwise we must have code extracted an unwind or something, just // return whatever we want. - ReturnInst::Create(Context, + ReturnInst::Create(Context, Constant::getNullValue(OldFnRetTy), TheSwitch); } @@ -692,13 +692,13 @@ Function *oldFunction = header->getParent(); // This takes place of the original loop - BasicBlock *codeReplacer = BasicBlock::Create(header->getContext(), + BasicBlock *codeReplacer = BasicBlock::Create(header->getContext(), "codeRepl", oldFunction, header); // The new function needs a root node because other nodes can branch to the // head of the region, but the entry node of a function cannot have preds. - BasicBlock *newFuncRoot = BasicBlock::Create(header->getContext(), + BasicBlock *newFuncRoot = BasicBlock::Create(header->getContext(), "newFuncRoot"); newFuncRoot->getInstList().push_back(BranchInst::Create(header)); @@ -737,14 +737,18 @@ // 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 (unsigned i = 0, e = Succs.size(); i != e; ++i) { for (BasicBlock::iterator I = Succs[i]->begin(); isa(I); ++I) { PHINode *PN = cast(I); std::set ProcessedPreds; - for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) - if (Blocks.count(PN->getIncomingBlock(i))) { - if (ProcessedPreds.insert(PN->getIncomingBlock(i)).second) + for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { + BasicBlock *IncomingBlock = PN->getIncomingBlock(i); + if (Blocks.count(IncomingBlock)) { + if (ProcessedPreds.insert(IncomingBlock).second) { + assert(PN->getBasicBlockIndex(codeReplacer) < 0 + && "Code Extractor depends on critical edges being broken"); 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. @@ -752,7 +756,9 @@ --i; --e; } } + } } + } //cerr << "NEW FUNCTION: " << *newFunction; // verifyFunction(*newFunction); @@ -760,7 +766,7 @@ // cerr << "OLD FUNCTION: " << *oldFunction; // verifyFunction(*oldFunction); - DEBUG(if (verifyFunction(*newFunction)) + DEBUG(if (verifyFunction(*newFunction)) report_fatal_error("verifyFunction failed!")); return newFunction; } \ No newline at end of file