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 | 6db70ef | 2003-04-25 22:08:12 +0000 | [diff] [blame] | 17 | #include "Support/CommandLine.h" |
| 18 | |
| 19 | namespace { |
| 20 | cl::opt<bool> |
| 21 | NoADCE("disable-adce", |
| 22 | cl::desc("Do not use the -adce pass to reduce testcases")); |
| 23 | cl::opt<bool> |
| 24 | NoDCE ("disable-dce", |
| 25 | cl::desc("Do not use the -dce pass to reduce testcases")); |
| 26 | cl::opt<bool> |
| 27 | NoSCFG("disable-simplifycfg", |
| 28 | cl::desc("Do not use the -simplifycfg pass to reduce testcases")); |
Chris Lattner | dbe48dc | 2003-05-21 20:38:59 +0000 | [diff] [blame] | 29 | cl::opt<bool> |
| 30 | NoFinalCleanup("disable-final-cleanup", |
| 31 | cl::desc("Disable the final cleanup phase of narrowing")); |
Chris Lattner | 6db70ef | 2003-04-25 22:08:12 +0000 | [diff] [blame] | 32 | } |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 33 | |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 34 | /// deleteInstructionFromProgram - This method clones the current Program and |
| 35 | /// deletes the specified instruction from the cloned module. It then runs a |
| 36 | /// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code which |
| 37 | /// depends on the value. The modified module is then returned. |
| 38 | /// |
| 39 | Module *BugDriver::deleteInstructionFromProgram(Instruction *I, |
| 40 | unsigned Simplification) const { |
| 41 | Module *Result = CloneModule(Program); |
| 42 | |
| 43 | BasicBlock *PBB = I->getParent(); |
| 44 | Function *PF = PBB->getParent(); |
| 45 | |
| 46 | Module::iterator RFI = Result->begin(); // Get iterator to corresponding fn |
| 47 | std::advance(RFI, std::distance(Program->begin(), Module::iterator(PF))); |
| 48 | |
| 49 | Function::iterator RBI = RFI->begin(); // Get iterator to corresponding BB |
| 50 | std::advance(RBI, std::distance(PF->begin(), Function::iterator(PBB))); |
| 51 | |
| 52 | BasicBlock::iterator RI = RBI->begin(); // Get iterator to corresponding inst |
| 53 | std::advance(RI, std::distance(PBB->begin(), BasicBlock::iterator(I))); |
| 54 | I = RI; // Got the corresponding instruction! |
| 55 | |
| 56 | // If this instruction produces a value, replace any users with null values |
| 57 | if (I->getType() != Type::VoidTy) |
| 58 | I->replaceAllUsesWith(Constant::getNullValue(I->getType())); |
| 59 | |
| 60 | // Remove the instruction from the program. |
| 61 | I->getParent()->getInstList().erase(I); |
| 62 | |
Chris Lattner | 44be257 | 2003-04-24 22:53:24 +0000 | [diff] [blame] | 63 | // Spiff up the output a little bit. |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 64 | PassManager Passes; |
Chris Lattner | 6db70ef | 2003-04-25 22:08:12 +0000 | [diff] [blame] | 65 | if (Simplification > 2 && !NoADCE) |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 66 | Passes.add(createAggressiveDCEPass()); // Remove dead code... |
| 67 | //Passes.add(createInstructionCombiningPass()); |
Chris Lattner | 6db70ef | 2003-04-25 22:08:12 +0000 | [diff] [blame] | 68 | if (Simplification > 1 && !NoDCE) |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 69 | Passes.add(createDeadCodeEliminationPass()); |
Chris Lattner | 6db70ef | 2003-04-25 22:08:12 +0000 | [diff] [blame] | 70 | if (Simplification && !NoSCFG) |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 71 | Passes.add(createCFGSimplificationPass()); // Delete dead control flow |
Chris Lattner | 10f22cb | 2003-03-07 18:17:13 +0000 | [diff] [blame] | 72 | |
| 73 | Passes.add(createVerifierPass()); |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 74 | Passes.run(*Result); |
| 75 | return Result; |
| 76 | } |
Chris Lattner | ba386d9 | 2003-02-28 16:13:20 +0000 | [diff] [blame] | 77 | |
| 78 | /// performFinalCleanups - This method clones the current Program and performs |
| 79 | /// a series of cleanups intended to get rid of extra cruft on the module |
| 80 | /// before handing it to the user... |
| 81 | /// |
| 82 | Module *BugDriver::performFinalCleanups() const { |
Chris Lattner | 16608b4 | 2003-04-25 00:52:30 +0000 | [diff] [blame] | 83 | Module *M = CloneModule(Program); |
Chris Lattner | 28b8ed9 | 2003-05-21 19:41:31 +0000 | [diff] [blame] | 84 | |
Chris Lattner | dbe48dc | 2003-05-21 20:38:59 +0000 | [diff] [blame] | 85 | // Allow disabling these passes if they crash bugpoint. |
| 86 | // |
| 87 | // FIXME: This should eventually run these passes in a pass list to prevent |
| 88 | // them from being able to crash bugpoint at all! |
| 89 | // |
| 90 | if (NoFinalCleanup) return M; |
| 91 | |
Chris Lattner | 28b8ed9 | 2003-05-21 19:41:31 +0000 | [diff] [blame] | 92 | // Make all functions external, so GlobalDCE doesn't delete them... |
| 93 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 94 | I->setLinkage(GlobalValue::ExternalLinkage); |
Chris Lattner | dbe48dc | 2003-05-21 20:38:59 +0000 | [diff] [blame] | 95 | |
Chris Lattner | ba386d9 | 2003-02-28 16:13:20 +0000 | [diff] [blame] | 96 | PassManager CleanupPasses; |
| 97 | CleanupPasses.add(createFunctionResolvingPass()); |
| 98 | CleanupPasses.add(createGlobalDCEPass()); |
Chris Lattner | 16608b4 | 2003-04-25 00:52:30 +0000 | [diff] [blame] | 99 | CleanupPasses.add(createDeadTypeEliminationPass()); |
Chris Lattner | 06c818e | 2003-06-25 04:13:36 +0000 | [diff] [blame^] | 100 | CleanupPasses.add(createDeadArgEliminationPass(true)); |
Chris Lattner | 10f22cb | 2003-03-07 18:17:13 +0000 | [diff] [blame] | 101 | CleanupPasses.add(createVerifierPass()); |
Chris Lattner | ba386d9 | 2003-02-28 16:13:20 +0000 | [diff] [blame] | 102 | CleanupPasses.run(*M); |
| 103 | return M; |
| 104 | } |