Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 1 | //===- ExtractFunction.cpp - Extract a function from Program --------------===// |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements a method that extracts a function from program, cleans |
| 11 | // it up, and returns it as a new module. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "BugDriver.h" |
Misha Brukman | e49603d | 2003-08-07 21:19:30 +0000 | [diff] [blame] | 16 | #include "llvm/Constant.h" |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 17 | #include "llvm/Module.h" |
| 18 | #include "llvm/PassManager.h" |
Brian Gaeke | d1a85a7 | 2003-09-10 21:11:42 +0000 | [diff] [blame] | 19 | #include "llvm/Pass.h" |
Misha Brukman | e49603d | 2003-08-07 21:19:30 +0000 | [diff] [blame] | 20 | #include "llvm/Type.h" |
| 21 | #include "llvm/Analysis/Verifier.h" |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 22 | #include "llvm/Transforms/IPO.h" |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 23 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 24 | #include "llvm/Transforms/Utils/Cloning.h" |
Chris Lattner | 5da69c7 | 2003-10-23 15:42:55 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 6db70ef | 2003-04-25 22:08:12 +0000 | [diff] [blame] | 26 | #include "Support/CommandLine.h" |
| 27 | |
Chris Lattner | 47ae4a1 | 2003-08-05 15:51:05 +0000 | [diff] [blame] | 28 | bool DisableSimplifyCFG = false; |
| 29 | |
Chris Lattner | 6db70ef | 2003-04-25 22:08:12 +0000 | [diff] [blame] | 30 | namespace { |
| 31 | cl::opt<bool> |
| 32 | NoADCE("disable-adce", |
| 33 | cl::desc("Do not use the -adce pass to reduce testcases")); |
| 34 | cl::opt<bool> |
| 35 | NoDCE ("disable-dce", |
| 36 | cl::desc("Do not use the -dce pass to reduce testcases")); |
Chris Lattner | 47ae4a1 | 2003-08-05 15:51:05 +0000 | [diff] [blame] | 37 | cl::opt<bool, true> |
| 38 | NoSCFG("disable-simplifycfg", cl::location(DisableSimplifyCFG), |
Chris Lattner | 6db70ef | 2003-04-25 22:08:12 +0000 | [diff] [blame] | 39 | cl::desc("Do not use the -simplifycfg pass to reduce testcases")); |
Chris Lattner | dbe48dc | 2003-05-21 20:38:59 +0000 | [diff] [blame] | 40 | cl::opt<bool> |
| 41 | NoFinalCleanup("disable-final-cleanup", |
| 42 | cl::desc("Disable the final cleanup phase of narrowing")); |
Chris Lattner | 6db70ef | 2003-04-25 22:08:12 +0000 | [diff] [blame] | 43 | } |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 44 | |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 45 | /// deleteInstructionFromProgram - This method clones the current Program and |
| 46 | /// deletes the specified instruction from the cloned module. It then runs a |
| 47 | /// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code which |
| 48 | /// depends on the value. The modified module is then returned. |
| 49 | /// |
| 50 | Module *BugDriver::deleteInstructionFromProgram(Instruction *I, |
| 51 | unsigned Simplification) const { |
| 52 | Module *Result = CloneModule(Program); |
| 53 | |
| 54 | BasicBlock *PBB = I->getParent(); |
| 55 | Function *PF = PBB->getParent(); |
| 56 | |
| 57 | Module::iterator RFI = Result->begin(); // Get iterator to corresponding fn |
| 58 | std::advance(RFI, std::distance(Program->begin(), Module::iterator(PF))); |
| 59 | |
| 60 | Function::iterator RBI = RFI->begin(); // Get iterator to corresponding BB |
| 61 | std::advance(RBI, std::distance(PF->begin(), Function::iterator(PBB))); |
| 62 | |
| 63 | BasicBlock::iterator RI = RBI->begin(); // Get iterator to corresponding inst |
| 64 | std::advance(RI, std::distance(PBB->begin(), BasicBlock::iterator(I))); |
| 65 | I = RI; // Got the corresponding instruction! |
| 66 | |
| 67 | // If this instruction produces a value, replace any users with null values |
| 68 | if (I->getType() != Type::VoidTy) |
| 69 | I->replaceAllUsesWith(Constant::getNullValue(I->getType())); |
| 70 | |
| 71 | // Remove the instruction from the program. |
| 72 | I->getParent()->getInstList().erase(I); |
| 73 | |
Chris Lattner | 44be257 | 2003-04-24 22:53:24 +0000 | [diff] [blame] | 74 | // Spiff up the output a little bit. |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 75 | PassManager Passes; |
Chris Lattner | 5da69c7 | 2003-10-23 15:42:55 +0000 | [diff] [blame] | 76 | // Make sure that the appropriate target data is always used... |
| 77 | Passes.add(new TargetData("bugpoint", Result)); |
| 78 | |
Chris Lattner | 6db70ef | 2003-04-25 22:08:12 +0000 | [diff] [blame] | 79 | if (Simplification > 2 && !NoADCE) |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 80 | Passes.add(createAggressiveDCEPass()); // Remove dead code... |
| 81 | //Passes.add(createInstructionCombiningPass()); |
Chris Lattner | 6db70ef | 2003-04-25 22:08:12 +0000 | [diff] [blame] | 82 | if (Simplification > 1 && !NoDCE) |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 83 | Passes.add(createDeadCodeEliminationPass()); |
Chris Lattner | 47ae4a1 | 2003-08-05 15:51:05 +0000 | [diff] [blame] | 84 | if (Simplification && !DisableSimplifyCFG) |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 85 | Passes.add(createCFGSimplificationPass()); // Delete dead control flow |
Chris Lattner | 10f22cb | 2003-03-07 18:17:13 +0000 | [diff] [blame] | 86 | |
| 87 | Passes.add(createVerifierPass()); |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 88 | Passes.run(*Result); |
| 89 | return Result; |
| 90 | } |
Chris Lattner | ba386d9 | 2003-02-28 16:13:20 +0000 | [diff] [blame] | 91 | |
| 92 | /// performFinalCleanups - This method clones the current Program and performs |
| 93 | /// a series of cleanups intended to get rid of extra cruft on the module |
| 94 | /// before handing it to the user... |
| 95 | /// |
Chris Lattner | 417477d | 2003-11-05 21:15:19 +0000 | [diff] [blame^] | 96 | void BugDriver::performFinalCleanups(Module *M, bool MayModifySemantics) const { |
Chris Lattner | dbe48dc | 2003-05-21 20:38:59 +0000 | [diff] [blame] | 97 | // Allow disabling these passes if they crash bugpoint. |
| 98 | // |
| 99 | // FIXME: This should eventually run these passes in a pass list to prevent |
| 100 | // them from being able to crash bugpoint at all! |
| 101 | // |
Chris Lattner | 417477d | 2003-11-05 21:15:19 +0000 | [diff] [blame^] | 102 | if (NoFinalCleanup) return; |
Chris Lattner | dbe48dc | 2003-05-21 20:38:59 +0000 | [diff] [blame] | 103 | |
Chris Lattner | 28b8ed9 | 2003-05-21 19:41:31 +0000 | [diff] [blame] | 104 | // Make all functions external, so GlobalDCE doesn't delete them... |
| 105 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 106 | I->setLinkage(GlobalValue::ExternalLinkage); |
Chris Lattner | dbe48dc | 2003-05-21 20:38:59 +0000 | [diff] [blame] | 107 | |
Chris Lattner | ba386d9 | 2003-02-28 16:13:20 +0000 | [diff] [blame] | 108 | PassManager CleanupPasses; |
Chris Lattner | 5da69c7 | 2003-10-23 15:42:55 +0000 | [diff] [blame] | 109 | // Make sure that the appropriate target data is always used... |
| 110 | CleanupPasses.add(new TargetData("bugpoint", M)); |
Chris Lattner | ba386d9 | 2003-02-28 16:13:20 +0000 | [diff] [blame] | 111 | CleanupPasses.add(createFunctionResolvingPass()); |
| 112 | CleanupPasses.add(createGlobalDCEPass()); |
Chris Lattner | 16608b4 | 2003-04-25 00:52:30 +0000 | [diff] [blame] | 113 | CleanupPasses.add(createDeadTypeEliminationPass()); |
Chris Lattner | 417477d | 2003-11-05 21:15:19 +0000 | [diff] [blame^] | 114 | CleanupPasses.add(createDeadArgEliminationPass(MayModifySemantics)); |
Chris Lattner | 10f22cb | 2003-03-07 18:17:13 +0000 | [diff] [blame] | 115 | CleanupPasses.add(createVerifierPass()); |
Chris Lattner | ba386d9 | 2003-02-28 16:13:20 +0000 | [diff] [blame] | 116 | CleanupPasses.run(*M); |
Chris Lattner | ba386d9 | 2003-02-28 16:13:20 +0000 | [diff] [blame] | 117 | } |