173a6bdd9SChris Lattner //===- ExtractFunction.cpp - Extract a function from Program --------------===// 273a6bdd9SChris Lattner // 309344dcfSJohn Criswell // The LLVM Compiler Infrastructure 409344dcfSJohn Criswell // 5345353d6SChris Lattner // This file is distributed under the University of Illinois Open Source 6345353d6SChris Lattner // License. See LICENSE.TXT for details. 709344dcfSJohn Criswell // 809344dcfSJohn Criswell //===----------------------------------------------------------------------===// 909344dcfSJohn Criswell // 10fd72bed3SChris Lattner // This file implements several methods that are used to extract functions, 11fd72bed3SChris Lattner // loops, or portions of a module from the rest of the module. 1273a6bdd9SChris Lattner // 1373a6bdd9SChris Lattner //===----------------------------------------------------------------------===// 1473a6bdd9SChris Lattner 1573a6bdd9SChris Lattner #include "BugDriver.h" 16fdb533a1SChris Lattner #include "llvm/Constants.h" 17fdb533a1SChris Lattner #include "llvm/DerivedTypes.h" 18b258410aSOwen Anderson #include "llvm/LLVMContext.h" 1973a6bdd9SChris Lattner #include "llvm/Module.h" 2073a6bdd9SChris Lattner #include "llvm/PassManager.h" 21ef432703SBrian Gaeke #include "llvm/Pass.h" 220c2305b1SMisha Brukman #include "llvm/Analysis/Verifier.h" 23e2d31aa1SDan Gohman #include "llvm/Assembly/Writer.h" 2473a6bdd9SChris Lattner #include "llvm/Transforms/IPO.h" 250eb5ce94SChris Lattner #include "llvm/Transforms/Scalar.h" 2673a6bdd9SChris Lattner #include "llvm/Transforms/Utils/Cloning.h" 27a060b10eSChris Lattner #include "llvm/Transforms/Utils/FunctionUtils.h" 28f3e8f97dSChris Lattner #include "llvm/Target/TargetData.h" 297c16caa3SReid Spencer #include "llvm/Support/CommandLine.h" 307c16caa3SReid Spencer #include "llvm/Support/Debug.h" 317c16caa3SReid Spencer #include "llvm/Support/FileUtilities.h" 32*c521f541SChris Lattner #include "llvm/Support/raw_ostream.h" 33c6243020SNick Lewycky #include "llvm/System/Path.h" 34c6243020SNick Lewycky #include "llvm/System/Signals.h" 353838243dSChris Lattner #include <set> 363f1ad878SChris Lattner using namespace llvm; 37960707c3SBrian Gaeke 38960707c3SBrian Gaeke namespace llvm { 39d1e2aaefSChris Lattner bool DisableSimplifyCFG = false; 40960707c3SBrian Gaeke } // End llvm namespace 41960707c3SBrian Gaeke 428d8de422SChris Lattner namespace { 438d8de422SChris Lattner cl::opt<bool> 448d8de422SChris Lattner NoDCE ("disable-dce", 458d8de422SChris Lattner cl::desc("Do not use the -dce pass to reduce testcases")); 46d1e2aaefSChris Lattner cl::opt<bool, true> 47d1e2aaefSChris Lattner NoSCFG("disable-simplifycfg", cl::location(DisableSimplifyCFG), 488d8de422SChris Lattner cl::desc("Do not use the -simplifycfg pass to reduce testcases")); 498d8de422SChris Lattner } 5073a6bdd9SChris Lattner 510eb5ce94SChris Lattner /// deleteInstructionFromProgram - This method clones the current Program and 520eb5ce94SChris Lattner /// deletes the specified instruction from the cloned module. It then runs a 530eb5ce94SChris Lattner /// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code which 540eb5ce94SChris Lattner /// depends on the value. The modified module is then returned. 550eb5ce94SChris Lattner /// 56b943b6b0SChris Lattner Module *BugDriver::deleteInstructionFromProgram(const Instruction *I, 570eb5ce94SChris Lattner unsigned Simplification) const { 580eb5ce94SChris Lattner Module *Result = CloneModule(Program); 590eb5ce94SChris Lattner 60b943b6b0SChris Lattner const BasicBlock *PBB = I->getParent(); 61b943b6b0SChris Lattner const Function *PF = PBB->getParent(); 620eb5ce94SChris Lattner 630eb5ce94SChris Lattner Module::iterator RFI = Result->begin(); // Get iterator to corresponding fn 64b943b6b0SChris Lattner std::advance(RFI, std::distance(PF->getParent()->begin(), 65b943b6b0SChris Lattner Module::const_iterator(PF))); 660eb5ce94SChris Lattner 670eb5ce94SChris Lattner Function::iterator RBI = RFI->begin(); // Get iterator to corresponding BB 68b943b6b0SChris Lattner std::advance(RBI, std::distance(PF->begin(), Function::const_iterator(PBB))); 690eb5ce94SChris Lattner 700eb5ce94SChris Lattner BasicBlock::iterator RI = RBI->begin(); // Get iterator to corresponding inst 71b943b6b0SChris Lattner std::advance(RI, std::distance(PBB->begin(), BasicBlock::const_iterator(I))); 72b943b6b0SChris Lattner Instruction *TheInst = RI; // Got the corresponding instruction! 730eb5ce94SChris Lattner 740eb5ce94SChris Lattner // If this instruction produces a value, replace any users with null values 757855a5cbSChris Lattner if (isa<StructType>(TheInst->getType())) 76b292b8ceSOwen Anderson TheInst->replaceAllUsesWith(UndefValue::get(TheInst->getType())); 7755f1c09eSOwen Anderson else if (TheInst->getType() != Type::getVoidTy(I->getContext())) 785a1acd99SOwen Anderson TheInst->replaceAllUsesWith(Constant::getNullValue(TheInst->getType())); 790eb5ce94SChris Lattner 800eb5ce94SChris Lattner // Remove the instruction from the program. 81b943b6b0SChris Lattner TheInst->getParent()->getInstList().erase(TheInst); 820eb5ce94SChris Lattner 83fdb533a1SChris Lattner 84fdb533a1SChris Lattner //writeProgramToFile("current.bc", Result); 85fdb533a1SChris Lattner 8610c0469bSChris Lattner // Spiff up the output a little bit. 870eb5ce94SChris Lattner PassManager Passes; 88f3e8f97dSChris Lattner // Make sure that the appropriate target data is always used... 8916cf8130SChris Lattner Passes.add(new TargetData(Result)); 90f3e8f97dSChris Lattner 91fd72bed3SChris Lattner /// FIXME: If this used runPasses() like the methods below, we could get rid 92fd72bed3SChris Lattner /// of the -disable-* options! 938d8de422SChris Lattner if (Simplification > 1 && !NoDCE) 940eb5ce94SChris Lattner Passes.add(createDeadCodeEliminationPass()); 95d1e2aaefSChris Lattner if (Simplification && !DisableSimplifyCFG) 960eb5ce94SChris Lattner Passes.add(createCFGSimplificationPass()); // Delete dead control flow 9744ffd7c3SChris Lattner 9844ffd7c3SChris Lattner Passes.add(createVerifierPass()); 990eb5ce94SChris Lattner Passes.run(*Result); 1000eb5ce94SChris Lattner return Result; 1010eb5ce94SChris Lattner } 102514c02ebSChris Lattner 1032920100eSChris Lattner static const PassInfo *getPI(Pass *P) { 1042920100eSChris Lattner const PassInfo *PI = P->getPassInfo(); 1052920100eSChris Lattner delete P; 1062920100eSChris Lattner return PI; 1072920100eSChris Lattner } 1082920100eSChris Lattner 109514c02ebSChris Lattner /// performFinalCleanups - This method clones the current Program and performs 110514c02ebSChris Lattner /// a series of cleanups intended to get rid of extra cruft on the module 1115e166a5fSChris Lattner /// before handing it to the user. 112514c02ebSChris Lattner /// 1132920100eSChris Lattner Module *BugDriver::performFinalCleanups(Module *M, bool MayModifySemantics) { 1142b3ab562SChris Lattner // Make all functions external, so GlobalDCE doesn't delete them... 1152b3ab562SChris Lattner for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) 1162b3ab562SChris Lattner I->setLinkage(GlobalValue::ExternalLinkage); 1172b3ab562SChris Lattner 1182920100eSChris Lattner std::vector<const PassInfo*> CleanupPasses; 1192920100eSChris Lattner CleanupPasses.push_back(getPI(createGlobalDCEPass())); 1202920100eSChris Lattner CleanupPasses.push_back(getPI(createDeadTypeEliminationPass())); 1213f1ad878SChris Lattner 1223f1ad878SChris Lattner if (MayModifySemantics) 1232920100eSChris Lattner CleanupPasses.push_back(getPI(createDeadArgHackingPass())); 1243f1ad878SChris Lattner else 1253f1ad878SChris Lattner CleanupPasses.push_back(getPI(createDeadArgEliminationPass())); 1262920100eSChris Lattner 1276ce2d033SChris Lattner Module *New = runPassesOn(M, CleanupPasses); 1286ce2d033SChris Lattner if (New == 0) { 129d8db3760SDan Gohman errs() << "Final cleanups failed. Sorry. :( Please report a bug!\n"; 1305e166a5fSChris Lattner return M; 1316ce2d033SChris Lattner } 1322920100eSChris Lattner delete M; 1336ce2d033SChris Lattner return New; 134514c02ebSChris Lattner } 135567543f0SChris Lattner 136567543f0SChris Lattner 1373fe96bc9SChris Lattner /// ExtractLoop - Given a module, extract up to one loop from it into a new 1383fe96bc9SChris Lattner /// function. This returns null if there are no extractable loops in the 1393fe96bc9SChris Lattner /// program or if the loop extractor crashes. 1403fe96bc9SChris Lattner Module *BugDriver::ExtractLoop(Module *M) { 1413fe96bc9SChris Lattner std::vector<const PassInfo*> LoopExtractPasses; 1423fe96bc9SChris Lattner LoopExtractPasses.push_back(getPI(createSingleLoopExtractorPass())); 1433fe96bc9SChris Lattner 1446ce2d033SChris Lattner Module *NewM = runPassesOn(M, LoopExtractPasses); 1456ce2d033SChris Lattner if (NewM == 0) { 146bcec7875SChris Lattner Module *Old = swapProgramIn(M); 147ee05152cSDan Gohman outs() << "*** Loop extraction failed: "; 1480e535c3cSGabor Greif EmitProgressBitcode("loopextraction", true); 149ee05152cSDan Gohman outs() << "*** Sorry. :( Please report a bug!\n"; 150bcec7875SChris Lattner swapProgramIn(Old); 1513fe96bc9SChris Lattner return 0; 1523fe96bc9SChris Lattner } 1533fe96bc9SChris Lattner 1543fe96bc9SChris Lattner // Check to see if we created any new functions. If not, no loops were 1558cb483b3SChris Lattner // extracted and we should return null. Limit the number of loops we extract 1568cb483b3SChris Lattner // to avoid taking forever. 1578cb483b3SChris Lattner static unsigned NumExtracted = 32; 158d308797cSChris Lattner if (M->size() == NewM->size() || --NumExtracted == 0) { 1593fe96bc9SChris Lattner delete NewM; 1603fe96bc9SChris Lattner return 0; 161d308797cSChris Lattner } else { 162d308797cSChris Lattner assert(M->size() < NewM->size() && "Loop extract removed functions?"); 163d308797cSChris Lattner Module::iterator MI = NewM->begin(); 164d308797cSChris Lattner for (unsigned i = 0, e = M->size(); i != e; ++i) 165d308797cSChris Lattner ++MI; 1663fe96bc9SChris Lattner } 1673fe96bc9SChris Lattner 1683fe96bc9SChris Lattner return NewM; 1693fe96bc9SChris Lattner } 1703fe96bc9SChris Lattner 1713fe96bc9SChris Lattner 172567543f0SChris Lattner // DeleteFunctionBody - "Remove" the function by deleting all of its basic 173567543f0SChris Lattner // blocks, making it external. 174567543f0SChris Lattner // 175567543f0SChris Lattner void llvm::DeleteFunctionBody(Function *F) { 176567543f0SChris Lattner // delete the body of the function... 177567543f0SChris Lattner F->deleteBody(); 1785301e7c6SReid Spencer assert(F->isDeclaration() && "This didn't make the function external!"); 179567543f0SChris Lattner } 180567543f0SChris Lattner 181fdb533a1SChris Lattner /// GetTorInit - Given a list of entries for static ctors/dtors, return them 182fdb533a1SChris Lattner /// as a constant array. 183fdb533a1SChris Lattner static Constant *GetTorInit(std::vector<std::pair<Function*, int> > &TorList) { 184fdb533a1SChris Lattner assert(!TorList.empty() && "Don't create empty tor list!"); 185fdb533a1SChris Lattner std::vector<Constant*> ArrayElts; 186fdb533a1SChris Lattner for (unsigned i = 0, e = TorList.size(); i != e; ++i) { 187fdb533a1SChris Lattner std::vector<Constant*> Elts; 18855f1c09eSOwen Anderson Elts.push_back(ConstantInt::get( 18955f1c09eSOwen Anderson Type::getInt32Ty(TorList[i].first->getContext()), TorList[i].second)); 190fdb533a1SChris Lattner Elts.push_back(TorList[i].first); 19103cb69fbSOwen Anderson ArrayElts.push_back(ConstantStruct::get( 19203cb69fbSOwen Anderson TorList[i].first->getContext(), Elts)); 193fdb533a1SChris Lattner } 1944056ca95SOwen Anderson return ConstantArray::get(ArrayType::get(ArrayElts[0]->getType(), 195fdb533a1SChris Lattner ArrayElts.size()), 196fdb533a1SChris Lattner ArrayElts); 197fdb533a1SChris Lattner } 198fdb533a1SChris Lattner 199fdb533a1SChris Lattner /// SplitStaticCtorDtor - A module was recently split into two parts, M1/M2, and 200fdb533a1SChris Lattner /// M1 has all of the global variables. If M2 contains any functions that are 201fdb533a1SChris Lattner /// static ctors/dtors, we need to add an llvm.global_[cd]tors global to M2, and 202fdb533a1SChris Lattner /// prune appropriate entries out of M1s list. 203956ae9dcSDan Gohman static void SplitStaticCtorDtor(const char *GlobalName, Module *M1, Module *M2, 204956ae9dcSDan Gohman DenseMap<const Value*, Value*> ValueMap) { 205fdb533a1SChris Lattner GlobalVariable *GV = M1->getNamedGlobal(GlobalName); 2066de96a1bSRafael Espindola if (!GV || GV->isDeclaration() || GV->hasLocalLinkage() || 207fdb533a1SChris Lattner !GV->use_empty()) return; 208fdb533a1SChris Lattner 209fdb533a1SChris Lattner std::vector<std::pair<Function*, int> > M1Tors, M2Tors; 210fdb533a1SChris Lattner ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer()); 211fdb533a1SChris Lattner if (!InitList) return; 212fdb533a1SChris Lattner 213fdb533a1SChris Lattner for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) { 214fdb533a1SChris Lattner if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){ 215fdb533a1SChris Lattner if (CS->getNumOperands() != 2) return; // Not array of 2-element structs. 216fdb533a1SChris Lattner 217fdb533a1SChris Lattner if (CS->getOperand(1)->isNullValue()) 218fdb533a1SChris Lattner break; // Found a null terminator, stop here. 219fdb533a1SChris Lattner 220e0fc4dfcSReid Spencer ConstantInt *CI = dyn_cast<ConstantInt>(CS->getOperand(0)); 221e0fc4dfcSReid Spencer int Priority = CI ? CI->getSExtValue() : 0; 222fdb533a1SChris Lattner 223fdb533a1SChris Lattner Constant *FP = CS->getOperand(1); 224fdb533a1SChris Lattner if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP)) 2256c38f0bbSReid Spencer if (CE->isCast()) 226fdb533a1SChris Lattner FP = CE->getOperand(0); 227fdb533a1SChris Lattner if (Function *F = dyn_cast<Function>(FP)) { 2285301e7c6SReid Spencer if (!F->isDeclaration()) 229fdb533a1SChris Lattner M1Tors.push_back(std::make_pair(F, Priority)); 230fdb533a1SChris Lattner else { 231fdb533a1SChris Lattner // Map to M2's version of the function. 232956ae9dcSDan Gohman F = cast<Function>(ValueMap[F]); 233fdb533a1SChris Lattner M2Tors.push_back(std::make_pair(F, Priority)); 234fdb533a1SChris Lattner } 235fdb533a1SChris Lattner } 236fdb533a1SChris Lattner } 237fdb533a1SChris Lattner } 238fdb533a1SChris Lattner 239fdb533a1SChris Lattner GV->eraseFromParent(); 240fdb533a1SChris Lattner if (!M1Tors.empty()) { 241fdb533a1SChris Lattner Constant *M1Init = GetTorInit(M1Tors); 242b17f3294SOwen Anderson new GlobalVariable(*M1, M1Init->getType(), false, 2435948fdf6SOwen Anderson GlobalValue::AppendingLinkage, 244b17f3294SOwen Anderson M1Init, GlobalName); 245fdb533a1SChris Lattner } 246fdb533a1SChris Lattner 247fdb533a1SChris Lattner GV = M2->getNamedGlobal(GlobalName); 248fdb533a1SChris Lattner assert(GV && "Not a clone of M1?"); 249fdb533a1SChris Lattner assert(GV->use_empty() && "llvm.ctors shouldn't have uses!"); 250fdb533a1SChris Lattner 251fdb533a1SChris Lattner GV->eraseFromParent(); 252fdb533a1SChris Lattner if (!M2Tors.empty()) { 253fdb533a1SChris Lattner Constant *M2Init = GetTorInit(M2Tors); 254b17f3294SOwen Anderson new GlobalVariable(*M2, M2Init->getType(), false, 2555948fdf6SOwen Anderson GlobalValue::AppendingLinkage, 256b17f3294SOwen Anderson M2Init, GlobalName); 257fdb533a1SChris Lattner } 258fdb533a1SChris Lattner } 259fdb533a1SChris Lattner 260b417a5c1SPatrick Jenkins 261567543f0SChris Lattner /// SplitFunctionsOutOfModule - Given a module and a list of functions in the 262567543f0SChris Lattner /// module, split the functions OUT of the specified module, and place them in 263567543f0SChris Lattner /// the new module. 264956ae9dcSDan Gohman Module * 265956ae9dcSDan Gohman llvm::SplitFunctionsOutOfModule(Module *M, 266956ae9dcSDan Gohman const std::vector<Function*> &F, 267956ae9dcSDan Gohman DenseMap<const Value*, Value*> &ValueMap) { 268567543f0SChris Lattner // Make sure functions & globals are all external so that linkage 269567543f0SChris Lattner // between the two modules will work. 270567543f0SChris Lattner for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) 271567543f0SChris Lattner I->setLinkage(GlobalValue::ExternalLinkage); 272fdb533a1SChris Lattner for (Module::global_iterator I = M->global_begin(), E = M->global_end(); 273c76bacbfSOwen Anderson I != E; ++I) { 274d786b51eSDaniel Dunbar if (I->hasName() && I->getName()[0] == '\01') 275d786b51eSDaniel Dunbar I->setName(I->getName().substr(1)); 276567543f0SChris Lattner I->setLinkage(GlobalValue::ExternalLinkage); 277c76bacbfSOwen Anderson } 278567543f0SChris Lattner 279956ae9dcSDan Gohman DenseMap<const Value*, Value*> NewValueMap; 280956ae9dcSDan Gohman Module *New = CloneModule(M, NewValueMap); 281567543f0SChris Lattner 2827275b02eSChris Lattner // Make sure global initializers exist only in the safe module (CBE->.so) 2837275b02eSChris Lattner for (Module::global_iterator I = New->global_begin(), E = New->global_end(); 2847275b02eSChris Lattner I != E; ++I) 2857275b02eSChris Lattner I->setInitializer(0); // Delete the initializer to make it external 286567543f0SChris Lattner 2877275b02eSChris Lattner // Remove the Test functions from the Safe module 288956ae9dcSDan Gohman std::set<Function *> TestFunctions; 289567543f0SChris Lattner for (unsigned i = 0, e = F.size(); i != e; ++i) { 290956ae9dcSDan Gohman Function *TNOF = cast<Function>(ValueMap[F[i]]); 291d8db3760SDan Gohman DEBUG(errs() << "Removing function "); 292d8db3760SDan Gohman DEBUG(WriteAsOperand(errs(), TNOF, false)); 293d8db3760SDan Gohman DEBUG(errs() << "\n"); 294956ae9dcSDan Gohman TestFunctions.insert(cast<Function>(NewValueMap[TNOF])); 2957275b02eSChris Lattner DeleteFunctionBody(TNOF); // Function is now external in this module! 296567543f0SChris Lattner } 297567543f0SChris Lattner 298b417a5c1SPatrick Jenkins 2997275b02eSChris Lattner // Remove the Safe functions from the Test module 3007275b02eSChris Lattner for (Module::iterator I = New->begin(), E = New->end(); I != E; ++I) 301956ae9dcSDan Gohman if (!TestFunctions.count(I)) 3027275b02eSChris Lattner DeleteFunctionBody(I); 303b417a5c1SPatrick Jenkins 304fdb533a1SChris Lattner 305fdb533a1SChris Lattner // Make sure that there is a global ctor/dtor array in both halves of the 306fdb533a1SChris Lattner // module if they both have static ctor/dtor functions. 307956ae9dcSDan Gohman SplitStaticCtorDtor("llvm.global_ctors", M, New, NewValueMap); 308956ae9dcSDan Gohman SplitStaticCtorDtor("llvm.global_dtors", M, New, NewValueMap); 309fdb533a1SChris Lattner 310567543f0SChris Lattner return New; 311567543f0SChris Lattner } 312a060b10eSChris Lattner 313a060b10eSChris Lattner //===----------------------------------------------------------------------===// 314a060b10eSChris Lattner // Basic Block Extraction Code 315a060b10eSChris Lattner //===----------------------------------------------------------------------===// 316a060b10eSChris Lattner 317a060b10eSChris Lattner /// ExtractMappedBlocksFromModule - Extract all but the specified basic blocks 318a060b10eSChris Lattner /// into their own functions. The only detail is that M is actually a module 319a060b10eSChris Lattner /// cloned from the one the BBs are in, so some mapping needs to be performed. 320a060b10eSChris Lattner /// If this operation fails for some reason (ie the implementation is buggy), 321a060b10eSChris Lattner /// this function should return null, otherwise it returns a new Module. 322a060b10eSChris Lattner Module *BugDriver::ExtractMappedBlocksFromModule(const 323a060b10eSChris Lattner std::vector<BasicBlock*> &BBs, 324a060b10eSChris Lattner Module *M) { 325c6243020SNick Lewycky char *ExtraArg = NULL; 326c6243020SNick Lewycky 327c6243020SNick Lewycky sys::Path uniqueFilename("bugpoint-extractblocks"); 328c6243020SNick Lewycky std::string ErrMsg; 329c6243020SNick Lewycky if (uniqueFilename.createTemporaryFileOnDisk(true, &ErrMsg)) { 330ee05152cSDan Gohman outs() << "*** Basic Block extraction failed!\n"; 331d8db3760SDan Gohman errs() << "Error creating temporary file: " << ErrMsg << "\n"; 332c6243020SNick Lewycky M = swapProgramIn(M); 333c6243020SNick Lewycky EmitProgressBitcode("basicblockextractfail", true); 334c6243020SNick Lewycky swapProgramIn(M); 335c6243020SNick Lewycky return 0; 336c6243020SNick Lewycky } 337c6243020SNick Lewycky sys::RemoveFileOnSignal(uniqueFilename); 338c6243020SNick Lewycky 339ee05152cSDan Gohman std::string ErrorInfo; 3409e6f1f16SChris Lattner raw_fd_ostream BlocksToNotExtractFile(uniqueFilename.c_str(), ErrorInfo, 3419e6f1f16SChris Lattner raw_fd_ostream::F_Force); 342ee05152cSDan Gohman if (!ErrorInfo.empty()) { 343ee05152cSDan Gohman outs() << "*** Basic Block extraction failed!\n"; 344ee05152cSDan Gohman errs() << "Error writing list of blocks to not extract: " << ErrorInfo 345c6243020SNick Lewycky << "\n"; 346c6243020SNick Lewycky M = swapProgramIn(M); 347c6243020SNick Lewycky EmitProgressBitcode("basicblockextractfail", true); 348c6243020SNick Lewycky swapProgramIn(M); 349c6243020SNick Lewycky return 0; 350c6243020SNick Lewycky } 351c6243020SNick Lewycky for (std::vector<BasicBlock*>::const_iterator I = BBs.begin(), E = BBs.end(); 352c6243020SNick Lewycky I != E; ++I) { 353c6243020SNick Lewycky BasicBlock *BB = *I; 35497879424SChris Lattner // If the BB doesn't have a name, give it one so we have something to key 35597879424SChris Lattner // off of. 35697879424SChris Lattner if (!BB->hasName()) BB->setName("tmpbb"); 35712368685SDaniel Dunbar BlocksToNotExtractFile << BB->getParent()->getNameStr() << " " 358c6243020SNick Lewycky << BB->getName() << "\n"; 359c6243020SNick Lewycky } 360c6243020SNick Lewycky BlocksToNotExtractFile.close(); 361c6243020SNick Lewycky 362c6243020SNick Lewycky const char *uniqueFN = uniqueFilename.c_str(); 363c6243020SNick Lewycky ExtraArg = (char*)malloc(23 + strlen(uniqueFN)); 364c6243020SNick Lewycky strcat(strcpy(ExtraArg, "--extract-blocks-file="), uniqueFN); 365c6243020SNick Lewycky 366a060b10eSChris Lattner std::vector<const PassInfo*> PI; 367c6243020SNick Lewycky std::vector<BasicBlock *> EmptyBBs; // This parameter is ignored. 368c6243020SNick Lewycky PI.push_back(getPI(createBlockExtractorPass(EmptyBBs))); 369c6243020SNick Lewycky Module *Ret = runPassesOn(M, PI, false, 1, &ExtraArg); 370c6243020SNick Lewycky 371c6243020SNick Lewycky if (uniqueFilename.exists()) 372c6243020SNick Lewycky uniqueFilename.eraseFromDisk(); // Free disk space 373c6243020SNick Lewycky free(ExtraArg); 374c6243020SNick Lewycky 375af32dfa0SChris Lattner if (Ret == 0) { 376ee05152cSDan Gohman outs() << "*** Basic Block extraction failed, please report a bug!\n"; 377af32dfa0SChris Lattner M = swapProgramIn(M); 3780e535c3cSGabor Greif EmitProgressBitcode("basicblockextractfail", true); 379917f8fd1SChris Lattner swapProgramIn(M); 380af32dfa0SChris Lattner } 381a060b10eSChris Lattner return Ret; 382a060b10eSChris Lattner } 383