Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 1 | //===- ExtractFunction.cpp - Extract a function from Program --------------===// |
| 2 | // |
| 3 | // This file implements a method that extracts a function from program, cleans |
| 4 | // it up, and returns it as a new module. |
| 5 | // |
| 6 | //===----------------------------------------------------------------------===// |
| 7 | |
| 8 | #include "BugDriver.h" |
| 9 | #include "llvm/Module.h" |
| 10 | #include "llvm/PassManager.h" |
| 11 | #include "llvm/Transforms/IPO.h" |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 12 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 13 | #include "llvm/Transforms/Utils/Cloning.h" |
Chris Lattner | 10f22cb | 2003-03-07 18:17:13 +0000 | [diff] [blame] | 14 | #include "llvm/Analysis/Verifier.h" |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 15 | #include "llvm/Type.h" |
| 16 | #include "llvm/Constant.h" |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 17 | |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 18 | /// deleteInstructionFromProgram - This method clones the current Program and |
| 19 | /// deletes the specified instruction from the cloned module. It then runs a |
| 20 | /// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code which |
| 21 | /// depends on the value. The modified module is then returned. |
| 22 | /// |
| 23 | Module *BugDriver::deleteInstructionFromProgram(Instruction *I, |
| 24 | unsigned Simplification) const { |
| 25 | Module *Result = CloneModule(Program); |
| 26 | |
| 27 | BasicBlock *PBB = I->getParent(); |
| 28 | Function *PF = PBB->getParent(); |
| 29 | |
| 30 | Module::iterator RFI = Result->begin(); // Get iterator to corresponding fn |
| 31 | std::advance(RFI, std::distance(Program->begin(), Module::iterator(PF))); |
| 32 | |
| 33 | Function::iterator RBI = RFI->begin(); // Get iterator to corresponding BB |
| 34 | std::advance(RBI, std::distance(PF->begin(), Function::iterator(PBB))); |
| 35 | |
| 36 | BasicBlock::iterator RI = RBI->begin(); // Get iterator to corresponding inst |
| 37 | std::advance(RI, std::distance(PBB->begin(), BasicBlock::iterator(I))); |
| 38 | I = RI; // Got the corresponding instruction! |
| 39 | |
| 40 | // If this instruction produces a value, replace any users with null values |
| 41 | if (I->getType() != Type::VoidTy) |
| 42 | I->replaceAllUsesWith(Constant::getNullValue(I->getType())); |
| 43 | |
| 44 | // Remove the instruction from the program. |
| 45 | I->getParent()->getInstList().erase(I); |
| 46 | |
Chris Lattner | 44be257 | 2003-04-24 22:53:24 +0000 | [diff] [blame] | 47 | // Spiff up the output a little bit. |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 48 | PassManager Passes; |
| 49 | if (Simplification > 2) |
| 50 | Passes.add(createAggressiveDCEPass()); // Remove dead code... |
| 51 | //Passes.add(createInstructionCombiningPass()); |
| 52 | if (Simplification > 1) |
| 53 | Passes.add(createDeadCodeEliminationPass()); |
| 54 | if (Simplification) |
| 55 | Passes.add(createCFGSimplificationPass()); // Delete dead control flow |
Chris Lattner | 10f22cb | 2003-03-07 18:17:13 +0000 | [diff] [blame] | 56 | |
| 57 | Passes.add(createVerifierPass()); |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 58 | Passes.run(*Result); |
| 59 | return Result; |
| 60 | } |
Chris Lattner | ba386d9 | 2003-02-28 16:13:20 +0000 | [diff] [blame] | 61 | |
| 62 | /// performFinalCleanups - This method clones the current Program and performs |
| 63 | /// a series of cleanups intended to get rid of extra cruft on the module |
| 64 | /// before handing it to the user... |
| 65 | /// |
| 66 | Module *BugDriver::performFinalCleanups() const { |
Chris Lattner | 16608b4 | 2003-04-25 00:52:30 +0000 | [diff] [blame] | 67 | Module *M = CloneModule(Program); |
Chris Lattner | ba386d9 | 2003-02-28 16:13:20 +0000 | [diff] [blame] | 68 | PassManager CleanupPasses; |
| 69 | CleanupPasses.add(createFunctionResolvingPass()); |
| 70 | CleanupPasses.add(createGlobalDCEPass()); |
Chris Lattner | 16608b4 | 2003-04-25 00:52:30 +0000 | [diff] [blame] | 71 | CleanupPasses.add(createDeadTypeEliminationPass()); |
Chris Lattner | 10f22cb | 2003-03-07 18:17:13 +0000 | [diff] [blame] | 72 | CleanupPasses.add(createVerifierPass()); |
Chris Lattner | ba386d9 | 2003-02-28 16:13:20 +0000 | [diff] [blame] | 73 | CleanupPasses.run(*M); |
| 74 | return M; |
| 75 | } |