+ std::vector<const PassInfo*> CleanupPasses;
+ CleanupPasses.push_back(getPI(createFunctionResolvingPass()));
+ CleanupPasses.push_back(getPI(createGlobalDCEPass()));
+ CleanupPasses.push_back(getPI(createDeadTypeEliminationPass()));
+
+ if (MayModifySemantics)
+ CleanupPasses.push_back(getPI(createDeadArgHackingPass()));
+ else
+ CleanupPasses.push_back(getPI(createDeadArgEliminationPass()));
+
+ Module *New = runPassesOn(M, CleanupPasses);
+ if (New == 0) {
+ std::cerr << "Final cleanups failed. Sorry. :( Please report a bug!\n";
+ }
+ delete M;
+ return New;
+}
+
+
+/// ExtractLoop - Given a module, extract up to one loop from it into a new
+/// function. This returns null if there are no extractable loops in the
+/// program or if the loop extractor crashes.
+Module *BugDriver::ExtractLoop(Module *M) {
+ std::vector<const PassInfo*> LoopExtractPasses;
+ LoopExtractPasses.push_back(getPI(createSingleLoopExtractorPass()));
+
+ Module *NewM = runPassesOn(M, LoopExtractPasses);
+ if (NewM == 0) {
+ Module *Old = swapProgramIn(M);
+ std::cout << "*** Loop extraction failed: ";
+ EmitProgressBytecode("loopextraction", true);
+ std::cout << "*** Sorry. :( Please report a bug!\n";
+ swapProgramIn(Old);
+ return 0;
+ }
+
+ // Check to see if we created any new functions. If not, no loops were
+ // extracted and we should return null.
+ if (M->size() == NewM->size()) {
+ delete NewM;
+ return 0;
+ }
+
+ return NewM;
+}
+
+
+// DeleteFunctionBody - "Remove" the function by deleting all of its basic
+// blocks, making it external.
+//
+void llvm::DeleteFunctionBody(Function *F) {
+ // delete the body of the function...
+ F->deleteBody();
+ assert(F->isExternal() && "This didn't make the function external!");
+}
+
+/// SplitFunctionsOutOfModule - Given a module and a list of functions in the
+/// module, split the functions OUT of the specified module, and place them in
+/// the new module.
+///
+/// FIXME: this could be made DRAMATICALLY more efficient for large programs if
+/// we just MOVED functions from one module to the other, instead of cloning the
+/// whole module, then proceeding to delete an entire module's worth of stuff.
+///
+Module *llvm::SplitFunctionsOutOfModule(Module *M,
+ const std::vector<Function*> &F) {
+ // Make sure functions & globals are all external so that linkage
+ // between the two modules will work.
+ for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
+ I->setLinkage(GlobalValue::ExternalLinkage);
+ for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
+ I->setLinkage(GlobalValue::ExternalLinkage);
+
+ Module *New = CloneModule(M);
+
+ // Make sure global initializers exist only in the safe module (CBE->.so)
+ for (Module::giterator I = New->gbegin(), E = New->gend(); I != E; ++I)
+ I->setInitializer(0); // Delete the initializer to make it external
+
+ // Remove the Test functions from the Safe module
+ for (unsigned i = 0, e = F.size(); i != e; ++i) {
+ Function *TNOF = M->getFunction(F[i]->getName(), F[i]->getFunctionType());
+ DEBUG(std::cerr << "Removing function " << F[i]->getName() << "\n");
+ assert(TNOF && "Function doesn't exist in module!");
+ DeleteFunctionBody(TNOF); // Function is now external in this module!
+ }
+
+ // Remove the Safe functions from the Test module
+ for (Module::iterator I = New->begin(), E = New->end(); I != E; ++I) {
+ bool funcFound = false;
+ for (std::vector<Function*>::const_iterator FI = F.begin(), Fe = F.end();
+ FI != Fe; ++FI)
+ if (I->getName() == (*FI)->getName()) funcFound = true;
+
+ if (!funcFound)
+ DeleteFunctionBody(I);
+ }
+ return New;