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 | // |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame^] | 10 | // This file implements several methods that are used to extract functions, |
| 11 | // loops, or portions of a module from the rest of the module. |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 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" |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 27 | #include "Support/Debug.h" |
Chris Lattner | 1b74716 | 2003-12-07 02:31:03 +0000 | [diff] [blame] | 28 | #include "Support/FileUtilities.h" |
Chris Lattner | c6b519d | 2003-11-23 04:51:05 +0000 | [diff] [blame] | 29 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 30 | |
| 31 | namespace llvm { |
Chris Lattner | c6b519d | 2003-11-23 04:51:05 +0000 | [diff] [blame] | 32 | bool DisableSimplifyCFG = false; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 33 | } // End llvm namespace |
| 34 | |
Chris Lattner | 6db70ef | 2003-04-25 22:08:12 +0000 | [diff] [blame] | 35 | namespace { |
| 36 | cl::opt<bool> |
Chris Lattner | 6db70ef | 2003-04-25 22:08:12 +0000 | [diff] [blame] | 37 | NoDCE ("disable-dce", |
| 38 | cl::desc("Do not use the -dce pass to reduce testcases")); |
Chris Lattner | 47ae4a1 | 2003-08-05 15:51:05 +0000 | [diff] [blame] | 39 | cl::opt<bool, true> |
| 40 | NoSCFG("disable-simplifycfg", cl::location(DisableSimplifyCFG), |
Chris Lattner | 6db70ef | 2003-04-25 22:08:12 +0000 | [diff] [blame] | 41 | cl::desc("Do not use the -simplifycfg pass to reduce testcases")); |
| 42 | } |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 43 | |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 44 | /// deleteInstructionFromProgram - This method clones the current Program and |
| 45 | /// deletes the specified instruction from the cloned module. It then runs a |
| 46 | /// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code which |
| 47 | /// depends on the value. The modified module is then returned. |
| 48 | /// |
Chris Lattner | 0cc8807 | 2004-02-18 21:50:26 +0000 | [diff] [blame] | 49 | Module *BugDriver::deleteInstructionFromProgram(const Instruction *I, |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 50 | unsigned Simplification) const { |
| 51 | Module *Result = CloneModule(Program); |
| 52 | |
Chris Lattner | 0cc8807 | 2004-02-18 21:50:26 +0000 | [diff] [blame] | 53 | const BasicBlock *PBB = I->getParent(); |
| 54 | const Function *PF = PBB->getParent(); |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 55 | |
| 56 | Module::iterator RFI = Result->begin(); // Get iterator to corresponding fn |
Chris Lattner | 0cc8807 | 2004-02-18 21:50:26 +0000 | [diff] [blame] | 57 | std::advance(RFI, std::distance(PF->getParent()->begin(), |
| 58 | Module::const_iterator(PF))); |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 59 | |
| 60 | Function::iterator RBI = RFI->begin(); // Get iterator to corresponding BB |
Chris Lattner | 0cc8807 | 2004-02-18 21:50:26 +0000 | [diff] [blame] | 61 | std::advance(RBI, std::distance(PF->begin(), Function::const_iterator(PBB))); |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 62 | |
| 63 | BasicBlock::iterator RI = RBI->begin(); // Get iterator to corresponding inst |
Chris Lattner | 0cc8807 | 2004-02-18 21:50:26 +0000 | [diff] [blame] | 64 | std::advance(RI, std::distance(PBB->begin(), BasicBlock::const_iterator(I))); |
| 65 | Instruction *TheInst = RI; // Got the corresponding instruction! |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 66 | |
| 67 | // If this instruction produces a value, replace any users with null values |
Chris Lattner | 0cc8807 | 2004-02-18 21:50:26 +0000 | [diff] [blame] | 68 | if (TheInst->getType() != Type::VoidTy) |
| 69 | TheInst->replaceAllUsesWith(Constant::getNullValue(TheInst->getType())); |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 70 | |
| 71 | // Remove the instruction from the program. |
Chris Lattner | 0cc8807 | 2004-02-18 21:50:26 +0000 | [diff] [blame] | 72 | TheInst->getParent()->getInstList().erase(TheInst); |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 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 | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame^] | 79 | /// FIXME: If this used runPasses() like the methods below, we could get rid |
| 80 | /// of the -disable-* options! |
Chris Lattner | 6db70ef | 2003-04-25 22:08:12 +0000 | [diff] [blame] | 81 | if (Simplification > 1 && !NoDCE) |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 82 | Passes.add(createDeadCodeEliminationPass()); |
Chris Lattner | 47ae4a1 | 2003-08-05 15:51:05 +0000 | [diff] [blame] | 83 | if (Simplification && !DisableSimplifyCFG) |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 84 | Passes.add(createCFGSimplificationPass()); // Delete dead control flow |
Chris Lattner | 10f22cb | 2003-03-07 18:17:13 +0000 | [diff] [blame] | 85 | |
| 86 | Passes.add(createVerifierPass()); |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 87 | Passes.run(*Result); |
| 88 | return Result; |
| 89 | } |
Chris Lattner | ba386d9 | 2003-02-28 16:13:20 +0000 | [diff] [blame] | 90 | |
Chris Lattner | fcb6ec0 | 2003-11-05 21:45:35 +0000 | [diff] [blame] | 91 | static const PassInfo *getPI(Pass *P) { |
| 92 | const PassInfo *PI = P->getPassInfo(); |
| 93 | delete P; |
| 94 | return PI; |
| 95 | } |
| 96 | |
Chris Lattner | ba386d9 | 2003-02-28 16:13:20 +0000 | [diff] [blame] | 97 | /// performFinalCleanups - This method clones the current Program and performs |
| 98 | /// a series of cleanups intended to get rid of extra cruft on the module |
| 99 | /// before handing it to the user... |
| 100 | /// |
Chris Lattner | fcb6ec0 | 2003-11-05 21:45:35 +0000 | [diff] [blame] | 101 | Module *BugDriver::performFinalCleanups(Module *M, bool MayModifySemantics) { |
Chris Lattner | 28b8ed9 | 2003-05-21 19:41:31 +0000 | [diff] [blame] | 102 | // Make all functions external, so GlobalDCE doesn't delete them... |
| 103 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 104 | I->setLinkage(GlobalValue::ExternalLinkage); |
Chris Lattner | dbe48dc | 2003-05-21 20:38:59 +0000 | [diff] [blame] | 105 | |
Chris Lattner | fcb6ec0 | 2003-11-05 21:45:35 +0000 | [diff] [blame] | 106 | std::vector<const PassInfo*> CleanupPasses; |
| 107 | CleanupPasses.push_back(getPI(createFunctionResolvingPass())); |
| 108 | CleanupPasses.push_back(getPI(createGlobalDCEPass())); |
| 109 | CleanupPasses.push_back(getPI(createDeadTypeEliminationPass())); |
Chris Lattner | fcb6ec0 | 2003-11-05 21:45:35 +0000 | [diff] [blame] | 110 | |
Chris Lattner | c6b519d | 2003-11-23 04:51:05 +0000 | [diff] [blame] | 111 | if (MayModifySemantics) |
| 112 | CleanupPasses.push_back(getPI(createDeadArgHackingPass())); |
| 113 | else |
| 114 | CleanupPasses.push_back(getPI(createDeadArgEliminationPass())); |
| 115 | |
Chris Lattner | fcb6ec0 | 2003-11-05 21:45:35 +0000 | [diff] [blame] | 116 | std::swap(Program, M); |
| 117 | std::string Filename; |
| 118 | bool Failed = runPasses(CleanupPasses, Filename); |
| 119 | std::swap(Program, M); |
| 120 | |
| 121 | if (Failed) { |
Chris Lattner | 7546c38 | 2004-03-14 20:02:07 +0000 | [diff] [blame] | 122 | std::cerr << "Final cleanups failed. Sorry. :( Please report a bug!\n"; |
Chris Lattner | fcb6ec0 | 2003-11-05 21:45:35 +0000 | [diff] [blame] | 123 | } else { |
| 124 | delete M; |
| 125 | M = ParseInputFile(Filename); |
| 126 | if (M == 0) { |
| 127 | std::cerr << getToolName() << ": Error reading bytecode file '" |
| 128 | << Filename << "'!\n"; |
| 129 | exit(1); |
| 130 | } |
Chris Lattner | 1b74716 | 2003-12-07 02:31:03 +0000 | [diff] [blame] | 131 | removeFile(Filename); |
Chris Lattner | fcb6ec0 | 2003-11-05 21:45:35 +0000 | [diff] [blame] | 132 | } |
| 133 | return M; |
Chris Lattner | ba386d9 | 2003-02-28 16:13:20 +0000 | [diff] [blame] | 134 | } |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 135 | |
| 136 | |
Chris Lattner | 7546c38 | 2004-03-14 20:02:07 +0000 | [diff] [blame] | 137 | /// ExtractLoop - Given a module, extract up to one loop from it into a new |
| 138 | /// function. This returns null if there are no extractable loops in the |
| 139 | /// program or if the loop extractor crashes. |
| 140 | Module *BugDriver::ExtractLoop(Module *M) { |
| 141 | std::vector<const PassInfo*> LoopExtractPasses; |
| 142 | LoopExtractPasses.push_back(getPI(createSingleLoopExtractorPass())); |
| 143 | |
| 144 | std::swap(Program, M); |
| 145 | std::string Filename; |
| 146 | bool Failed = runPasses(LoopExtractPasses, Filename); |
| 147 | std::swap(Program, M); |
| 148 | |
| 149 | if (Failed) { |
| 150 | std::cerr << "Loop extraction failed. Sorry. :( Please report a bug!\n"; |
| 151 | return 0; |
| 152 | } else { |
| 153 | Module *NewM = ParseInputFile(Filename); |
| 154 | if (NewM == 0) { |
| 155 | std::cerr << getToolName() << ": Error reading bytecode file '" |
| 156 | << Filename << "'!\n"; |
| 157 | exit(1); |
| 158 | } |
| 159 | removeFile(Filename); |
| 160 | |
| 161 | // Check to see if we created any new functions. If not, no loops were |
| 162 | // extracted and we should return null. |
| 163 | if (M->size() != NewM->size()) { |
| 164 | delete NewM; |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | return NewM; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 173 | // DeleteFunctionBody - "Remove" the function by deleting all of its basic |
| 174 | // blocks, making it external. |
| 175 | // |
| 176 | void llvm::DeleteFunctionBody(Function *F) { |
| 177 | // delete the body of the function... |
| 178 | F->deleteBody(); |
| 179 | assert(F->isExternal() && "This didn't make the function external!"); |
| 180 | } |
| 181 | |
| 182 | /// SplitFunctionsOutOfModule - Given a module and a list of functions in the |
| 183 | /// module, split the functions OUT of the specified module, and place them in |
| 184 | /// the new module. |
Chris Lattner | 5eda1f2 | 2004-03-14 19:31:00 +0000 | [diff] [blame] | 185 | /// |
| 186 | /// FIXME: this could be made DRAMATICALLY more efficient for large programs if |
| 187 | /// we just MOVED functions from one module to the other, instead of cloning the |
| 188 | /// whole module, then proceeding to delete an entire module's worth of stuff. |
| 189 | /// |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 190 | Module *llvm::SplitFunctionsOutOfModule(Module *M, |
| 191 | const std::vector<Function*> &F) { |
| 192 | // Make sure functions & globals are all external so that linkage |
| 193 | // between the two modules will work. |
| 194 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 195 | I->setLinkage(GlobalValue::ExternalLinkage); |
| 196 | for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) |
| 197 | I->setLinkage(GlobalValue::ExternalLinkage); |
| 198 | |
| 199 | Module *New = CloneModule(M); |
| 200 | |
| 201 | // Make sure global initializers exist only in the safe module (CBE->.so) |
| 202 | for (Module::giterator I = New->gbegin(), E = New->gend(); I != E; ++I) |
| 203 | I->setInitializer(0); // Delete the initializer to make it external |
| 204 | |
| 205 | // Remove the Test functions from the Safe module |
| 206 | for (unsigned i = 0, e = F.size(); i != e; ++i) { |
| 207 | Function *TNOF = M->getFunction(F[i]->getName(), F[i]->getFunctionType()); |
| 208 | DEBUG(std::cerr << "Removing function " << F[i]->getName() << "\n"); |
| 209 | assert(TNOF && "Function doesn't exist in module!"); |
| 210 | DeleteFunctionBody(TNOF); // Function is now external in this module! |
| 211 | } |
| 212 | |
| 213 | // Remove the Safe functions from the Test module |
| 214 | for (Module::iterator I = New->begin(), E = New->end(); I != E; ++I) { |
| 215 | bool funcFound = false; |
| 216 | for (std::vector<Function*>::const_iterator FI = F.begin(), Fe = F.end(); |
| 217 | FI != Fe; ++FI) |
| 218 | if (I->getName() == (*FI)->getName()) funcFound = true; |
| 219 | |
| 220 | if (!funcFound) |
| 221 | DeleteFunctionBody(I); |
| 222 | } |
| 223 | return New; |
| 224 | } |