ExtractFunction.cpp revision 6db70ef879916e6115ac97eb76e4fea973652e2c
1afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner//===- ExtractFunction.cpp - Extract a function from Program --------------===// 2afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner// 3afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner// This file implements a method that extracts a function from program, cleans 4afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner// it up, and returns it as a new module. 5afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner// 6afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner//===----------------------------------------------------------------------===// 7afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner 8afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner#include "BugDriver.h" 9afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner#include "llvm/Module.h" 10afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner#include "llvm/PassManager.h" 11afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner#include "llvm/Transforms/IPO.h" 126520785dcd22012535934098942d57c07c7631c2Chris Lattner#include "llvm/Transforms/Scalar.h" 13afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner#include "llvm/Transforms/Utils/Cloning.h" 1410f22cb1a0f2755050218cd0e07221a0985c6b63Chris Lattner#include "llvm/Analysis/Verifier.h" 156520785dcd22012535934098942d57c07c7631c2Chris Lattner#include "llvm/Type.h" 166520785dcd22012535934098942d57c07c7631c2Chris Lattner#include "llvm/Constant.h" 176db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner#include "Support/CommandLine.h" 186db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner 196db70ef879916e6115ac97eb76e4fea973652e2cChris Lattnernamespace { 206db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner cl::opt<bool> 216db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner NoADCE("disable-adce", 226db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner cl::desc("Do not use the -adce pass to reduce testcases")); 236db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner cl::opt<bool> 246db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner NoDCE ("disable-dce", 256db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner cl::desc("Do not use the -dce pass to reduce testcases")); 266db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner cl::opt<bool> 276db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner NoSCFG("disable-simplifycfg", 286db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner cl::desc("Do not use the -simplifycfg pass to reduce testcases")); 296db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner} 30afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner 316520785dcd22012535934098942d57c07c7631c2Chris Lattner/// deleteInstructionFromProgram - This method clones the current Program and 326520785dcd22012535934098942d57c07c7631c2Chris Lattner/// deletes the specified instruction from the cloned module. It then runs a 336520785dcd22012535934098942d57c07c7631c2Chris Lattner/// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code which 346520785dcd22012535934098942d57c07c7631c2Chris Lattner/// depends on the value. The modified module is then returned. 356520785dcd22012535934098942d57c07c7631c2Chris Lattner/// 366520785dcd22012535934098942d57c07c7631c2Chris LattnerModule *BugDriver::deleteInstructionFromProgram(Instruction *I, 376520785dcd22012535934098942d57c07c7631c2Chris Lattner unsigned Simplification) const { 386520785dcd22012535934098942d57c07c7631c2Chris Lattner Module *Result = CloneModule(Program); 396520785dcd22012535934098942d57c07c7631c2Chris Lattner 406520785dcd22012535934098942d57c07c7631c2Chris Lattner BasicBlock *PBB = I->getParent(); 416520785dcd22012535934098942d57c07c7631c2Chris Lattner Function *PF = PBB->getParent(); 426520785dcd22012535934098942d57c07c7631c2Chris Lattner 436520785dcd22012535934098942d57c07c7631c2Chris Lattner Module::iterator RFI = Result->begin(); // Get iterator to corresponding fn 446520785dcd22012535934098942d57c07c7631c2Chris Lattner std::advance(RFI, std::distance(Program->begin(), Module::iterator(PF))); 456520785dcd22012535934098942d57c07c7631c2Chris Lattner 466520785dcd22012535934098942d57c07c7631c2Chris Lattner Function::iterator RBI = RFI->begin(); // Get iterator to corresponding BB 476520785dcd22012535934098942d57c07c7631c2Chris Lattner std::advance(RBI, std::distance(PF->begin(), Function::iterator(PBB))); 486520785dcd22012535934098942d57c07c7631c2Chris Lattner 496520785dcd22012535934098942d57c07c7631c2Chris Lattner BasicBlock::iterator RI = RBI->begin(); // Get iterator to corresponding inst 506520785dcd22012535934098942d57c07c7631c2Chris Lattner std::advance(RI, std::distance(PBB->begin(), BasicBlock::iterator(I))); 516520785dcd22012535934098942d57c07c7631c2Chris Lattner I = RI; // Got the corresponding instruction! 526520785dcd22012535934098942d57c07c7631c2Chris Lattner 536520785dcd22012535934098942d57c07c7631c2Chris Lattner // If this instruction produces a value, replace any users with null values 546520785dcd22012535934098942d57c07c7631c2Chris Lattner if (I->getType() != Type::VoidTy) 556520785dcd22012535934098942d57c07c7631c2Chris Lattner I->replaceAllUsesWith(Constant::getNullValue(I->getType())); 566520785dcd22012535934098942d57c07c7631c2Chris Lattner 576520785dcd22012535934098942d57c07c7631c2Chris Lattner // Remove the instruction from the program. 586520785dcd22012535934098942d57c07c7631c2Chris Lattner I->getParent()->getInstList().erase(I); 596520785dcd22012535934098942d57c07c7631c2Chris Lattner 6044be25716628941b4cccccf56a28ee0ba2606850Chris Lattner // Spiff up the output a little bit. 616520785dcd22012535934098942d57c07c7631c2Chris Lattner PassManager Passes; 626db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner if (Simplification > 2 && !NoADCE) 636520785dcd22012535934098942d57c07c7631c2Chris Lattner Passes.add(createAggressiveDCEPass()); // Remove dead code... 646520785dcd22012535934098942d57c07c7631c2Chris Lattner //Passes.add(createInstructionCombiningPass()); 656db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner if (Simplification > 1 && !NoDCE) 666520785dcd22012535934098942d57c07c7631c2Chris Lattner Passes.add(createDeadCodeEliminationPass()); 676db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner if (Simplification && !NoSCFG) 686520785dcd22012535934098942d57c07c7631c2Chris Lattner Passes.add(createCFGSimplificationPass()); // Delete dead control flow 6910f22cb1a0f2755050218cd0e07221a0985c6b63Chris Lattner 7010f22cb1a0f2755050218cd0e07221a0985c6b63Chris Lattner Passes.add(createVerifierPass()); 716520785dcd22012535934098942d57c07c7631c2Chris Lattner Passes.run(*Result); 726520785dcd22012535934098942d57c07c7631c2Chris Lattner return Result; 736520785dcd22012535934098942d57c07c7631c2Chris Lattner} 74ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner 75ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner/// performFinalCleanups - This method clones the current Program and performs 76ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner/// a series of cleanups intended to get rid of extra cruft on the module 77ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner/// before handing it to the user... 78ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner/// 79ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris LattnerModule *BugDriver::performFinalCleanups() const { 8016608b4c853ba883ac4bf5c642c4a9b4977a4fbaChris Lattner Module *M = CloneModule(Program); 81ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner PassManager CleanupPasses; 82ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner CleanupPasses.add(createFunctionResolvingPass()); 83ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner CleanupPasses.add(createGlobalDCEPass()); 8416608b4c853ba883ac4bf5c642c4a9b4977a4fbaChris Lattner CleanupPasses.add(createDeadTypeEliminationPass()); 8510f22cb1a0f2755050218cd0e07221a0985c6b63Chris Lattner CleanupPasses.add(createVerifierPass()); 86ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner CleanupPasses.run(*M); 87ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner return M; 88ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner} 89