Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 1 | //===- ExtractFunction.cpp - Extract a function from Program --------------===// |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 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. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 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" |
Chris Lattner | 5a7a9e5 | 2006-03-08 23:55:38 +0000 | [diff] [blame] | 16 | #include "llvm/Constants.h" |
| 17 | #include "llvm/DerivedTypes.h" |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 18 | #include "llvm/Module.h" |
| 19 | #include "llvm/PassManager.h" |
Brian Gaeke | d1a85a7 | 2003-09-10 21:11:42 +0000 | [diff] [blame] | 20 | #include "llvm/Pass.h" |
Patrick Jenkins | e47863e | 2006-07-28 01:19:28 +0000 | [diff] [blame] | 21 | #include "llvm/SymbolTable.h" |
Misha Brukman | e49603d | 2003-08-07 21:19:30 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/Verifier.h" |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 23 | #include "llvm/Transforms/IPO.h" |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 24 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 25 | #include "llvm/Transforms/Utils/Cloning.h" |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 26 | #include "llvm/Transforms/Utils/FunctionUtils.h" |
Chris Lattner | 5da69c7 | 2003-10-23 15:42:55 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetData.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 28 | #include "llvm/Support/CommandLine.h" |
| 29 | #include "llvm/Support/Debug.h" |
| 30 | #include "llvm/Support/FileUtilities.h" |
Chris Lattner | fb4b96e | 2004-04-02 16:28:32 +0000 | [diff] [blame] | 31 | #include <set> |
Chris Lattner | e31a9cc | 2006-01-22 22:53:40 +0000 | [diff] [blame] | 32 | #include <iostream> |
Chris Lattner | c6b519d | 2003-11-23 04:51:05 +0000 | [diff] [blame] | 33 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 34 | |
| 35 | namespace llvm { |
Chris Lattner | c6b519d | 2003-11-23 04:51:05 +0000 | [diff] [blame] | 36 | bool DisableSimplifyCFG = false; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 37 | } // End llvm namespace |
| 38 | |
Chris Lattner | 6db70ef | 2003-04-25 22:08:12 +0000 | [diff] [blame] | 39 | namespace { |
| 40 | cl::opt<bool> |
Chris Lattner | 6db70ef | 2003-04-25 22:08:12 +0000 | [diff] [blame] | 41 | NoDCE ("disable-dce", |
| 42 | cl::desc("Do not use the -dce pass to reduce testcases")); |
Chris Lattner | 47ae4a1 | 2003-08-05 15:51:05 +0000 | [diff] [blame] | 43 | cl::opt<bool, true> |
| 44 | NoSCFG("disable-simplifycfg", cl::location(DisableSimplifyCFG), |
Chris Lattner | 6db70ef | 2003-04-25 22:08:12 +0000 | [diff] [blame] | 45 | cl::desc("Do not use the -simplifycfg pass to reduce testcases")); |
| 46 | } |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 47 | |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 48 | /// deleteInstructionFromProgram - This method clones the current Program and |
| 49 | /// deletes the specified instruction from the cloned module. It then runs a |
| 50 | /// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code which |
| 51 | /// depends on the value. The modified module is then returned. |
| 52 | /// |
Chris Lattner | 0cc8807 | 2004-02-18 21:50:26 +0000 | [diff] [blame] | 53 | Module *BugDriver::deleteInstructionFromProgram(const Instruction *I, |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 54 | unsigned Simplification) const { |
| 55 | Module *Result = CloneModule(Program); |
| 56 | |
Chris Lattner | 0cc8807 | 2004-02-18 21:50:26 +0000 | [diff] [blame] | 57 | const BasicBlock *PBB = I->getParent(); |
| 58 | const Function *PF = PBB->getParent(); |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 59 | |
| 60 | Module::iterator RFI = Result->begin(); // Get iterator to corresponding fn |
Chris Lattner | 0cc8807 | 2004-02-18 21:50:26 +0000 | [diff] [blame] | 61 | std::advance(RFI, std::distance(PF->getParent()->begin(), |
| 62 | Module::const_iterator(PF))); |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 63 | |
| 64 | Function::iterator RBI = RFI->begin(); // Get iterator to corresponding BB |
Chris Lattner | 0cc8807 | 2004-02-18 21:50:26 +0000 | [diff] [blame] | 65 | std::advance(RBI, std::distance(PF->begin(), Function::const_iterator(PBB))); |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 66 | |
| 67 | BasicBlock::iterator RI = RBI->begin(); // Get iterator to corresponding inst |
Chris Lattner | 0cc8807 | 2004-02-18 21:50:26 +0000 | [diff] [blame] | 68 | std::advance(RI, std::distance(PBB->begin(), BasicBlock::const_iterator(I))); |
| 69 | Instruction *TheInst = RI; // Got the corresponding instruction! |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 70 | |
| 71 | // If this instruction produces a value, replace any users with null values |
Chris Lattner | 0cc8807 | 2004-02-18 21:50:26 +0000 | [diff] [blame] | 72 | if (TheInst->getType() != Type::VoidTy) |
| 73 | TheInst->replaceAllUsesWith(Constant::getNullValue(TheInst->getType())); |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 74 | |
| 75 | // Remove the instruction from the program. |
Chris Lattner | 0cc8807 | 2004-02-18 21:50:26 +0000 | [diff] [blame] | 76 | TheInst->getParent()->getInstList().erase(TheInst); |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 77 | |
Chris Lattner | 5a7a9e5 | 2006-03-08 23:55:38 +0000 | [diff] [blame] | 78 | |
| 79 | //writeProgramToFile("current.bc", Result); |
| 80 | |
Chris Lattner | 44be257 | 2003-04-24 22:53:24 +0000 | [diff] [blame] | 81 | // Spiff up the output a little bit. |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 82 | PassManager Passes; |
Chris Lattner | 5da69c7 | 2003-10-23 15:42:55 +0000 | [diff] [blame] | 83 | // Make sure that the appropriate target data is always used... |
Chris Lattner | 831b121 | 2006-06-16 18:23:49 +0000 | [diff] [blame] | 84 | Passes.add(new TargetData(Result)); |
Chris Lattner | 5da69c7 | 2003-10-23 15:42:55 +0000 | [diff] [blame] | 85 | |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 86 | /// FIXME: If this used runPasses() like the methods below, we could get rid |
| 87 | /// of the -disable-* options! |
Chris Lattner | 6db70ef | 2003-04-25 22:08:12 +0000 | [diff] [blame] | 88 | if (Simplification > 1 && !NoDCE) |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 89 | Passes.add(createDeadCodeEliminationPass()); |
Chris Lattner | 47ae4a1 | 2003-08-05 15:51:05 +0000 | [diff] [blame] | 90 | if (Simplification && !DisableSimplifyCFG) |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 91 | Passes.add(createCFGSimplificationPass()); // Delete dead control flow |
Chris Lattner | 10f22cb | 2003-03-07 18:17:13 +0000 | [diff] [blame] | 92 | |
| 93 | Passes.add(createVerifierPass()); |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 94 | Passes.run(*Result); |
| 95 | return Result; |
| 96 | } |
Chris Lattner | ba386d9 | 2003-02-28 16:13:20 +0000 | [diff] [blame] | 97 | |
Chris Lattner | fcb6ec0 | 2003-11-05 21:45:35 +0000 | [diff] [blame] | 98 | static const PassInfo *getPI(Pass *P) { |
| 99 | const PassInfo *PI = P->getPassInfo(); |
| 100 | delete P; |
| 101 | return PI; |
| 102 | } |
| 103 | |
Chris Lattner | ba386d9 | 2003-02-28 16:13:20 +0000 | [diff] [blame] | 104 | /// performFinalCleanups - This method clones the current Program and performs |
| 105 | /// a series of cleanups intended to get rid of extra cruft on the module |
Chris Lattner | 9b5b190 | 2005-02-23 06:12:11 +0000 | [diff] [blame] | 106 | /// before handing it to the user. |
Chris Lattner | ba386d9 | 2003-02-28 16:13:20 +0000 | [diff] [blame] | 107 | /// |
Chris Lattner | fcb6ec0 | 2003-11-05 21:45:35 +0000 | [diff] [blame] | 108 | Module *BugDriver::performFinalCleanups(Module *M, bool MayModifySemantics) { |
Chris Lattner | 28b8ed9 | 2003-05-21 19:41:31 +0000 | [diff] [blame] | 109 | // Make all functions external, so GlobalDCE doesn't delete them... |
| 110 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 111 | I->setLinkage(GlobalValue::ExternalLinkage); |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 112 | |
Chris Lattner | fcb6ec0 | 2003-11-05 21:45:35 +0000 | [diff] [blame] | 113 | std::vector<const PassInfo*> CleanupPasses; |
| 114 | CleanupPasses.push_back(getPI(createFunctionResolvingPass())); |
| 115 | CleanupPasses.push_back(getPI(createGlobalDCEPass())); |
| 116 | CleanupPasses.push_back(getPI(createDeadTypeEliminationPass())); |
Chris Lattner | fcb6ec0 | 2003-11-05 21:45:35 +0000 | [diff] [blame] | 117 | |
Chris Lattner | c6b519d | 2003-11-23 04:51:05 +0000 | [diff] [blame] | 118 | if (MayModifySemantics) |
| 119 | CleanupPasses.push_back(getPI(createDeadArgHackingPass())); |
| 120 | else |
| 121 | CleanupPasses.push_back(getPI(createDeadArgEliminationPass())); |
Chris Lattner | fcb6ec0 | 2003-11-05 21:45:35 +0000 | [diff] [blame] | 122 | |
Chris Lattner | a75766a | 2004-03-14 21:17:22 +0000 | [diff] [blame] | 123 | Module *New = runPassesOn(M, CleanupPasses); |
| 124 | if (New == 0) { |
Chris Lattner | 7546c38 | 2004-03-14 20:02:07 +0000 | [diff] [blame] | 125 | std::cerr << "Final cleanups failed. Sorry. :( Please report a bug!\n"; |
Chris Lattner | 9b5b190 | 2005-02-23 06:12:11 +0000 | [diff] [blame] | 126 | return M; |
Chris Lattner | fcb6ec0 | 2003-11-05 21:45:35 +0000 | [diff] [blame] | 127 | } |
Chris Lattner | a75766a | 2004-03-14 21:17:22 +0000 | [diff] [blame] | 128 | delete M; |
| 129 | return New; |
Chris Lattner | ba386d9 | 2003-02-28 16:13:20 +0000 | [diff] [blame] | 130 | } |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 131 | |
| 132 | |
Chris Lattner | 7546c38 | 2004-03-14 20:02:07 +0000 | [diff] [blame] | 133 | /// ExtractLoop - Given a module, extract up to one loop from it into a new |
| 134 | /// function. This returns null if there are no extractable loops in the |
| 135 | /// program or if the loop extractor crashes. |
| 136 | Module *BugDriver::ExtractLoop(Module *M) { |
| 137 | std::vector<const PassInfo*> LoopExtractPasses; |
| 138 | LoopExtractPasses.push_back(getPI(createSingleLoopExtractorPass())); |
| 139 | |
Chris Lattner | a75766a | 2004-03-14 21:17:22 +0000 | [diff] [blame] | 140 | Module *NewM = runPassesOn(M, LoopExtractPasses); |
| 141 | if (NewM == 0) { |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 142 | Module *Old = swapProgramIn(M); |
| 143 | std::cout << "*** Loop extraction failed: "; |
| 144 | EmitProgressBytecode("loopextraction", true); |
| 145 | std::cout << "*** Sorry. :( Please report a bug!\n"; |
| 146 | swapProgramIn(Old); |
Chris Lattner | 7546c38 | 2004-03-14 20:02:07 +0000 | [diff] [blame] | 147 | return 0; |
Chris Lattner | 7546c38 | 2004-03-14 20:02:07 +0000 | [diff] [blame] | 148 | } |
Chris Lattner | a75766a | 2004-03-14 21:17:22 +0000 | [diff] [blame] | 149 | |
| 150 | // Check to see if we created any new functions. If not, no loops were |
Chris Lattner | a269ec7 | 2004-11-18 19:40:13 +0000 | [diff] [blame] | 151 | // extracted and we should return null. Limit the number of loops we extract |
| 152 | // to avoid taking forever. |
| 153 | static unsigned NumExtracted = 32; |
Chris Lattner | 90c18c5 | 2004-11-16 06:31:38 +0000 | [diff] [blame] | 154 | if (M->size() == NewM->size() || --NumExtracted == 0) { |
Chris Lattner | a75766a | 2004-03-14 21:17:22 +0000 | [diff] [blame] | 155 | delete NewM; |
| 156 | return 0; |
Chris Lattner | 90c18c5 | 2004-11-16 06:31:38 +0000 | [diff] [blame] | 157 | } else { |
| 158 | assert(M->size() < NewM->size() && "Loop extract removed functions?"); |
| 159 | Module::iterator MI = NewM->begin(); |
| 160 | for (unsigned i = 0, e = M->size(); i != e; ++i) |
| 161 | ++MI; |
Chris Lattner | a75766a | 2004-03-14 21:17:22 +0000 | [diff] [blame] | 162 | } |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 163 | |
Chris Lattner | a75766a | 2004-03-14 21:17:22 +0000 | [diff] [blame] | 164 | return NewM; |
Chris Lattner | 7546c38 | 2004-03-14 20:02:07 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 168 | // DeleteFunctionBody - "Remove" the function by deleting all of its basic |
| 169 | // blocks, making it external. |
| 170 | // |
| 171 | void llvm::DeleteFunctionBody(Function *F) { |
| 172 | // delete the body of the function... |
| 173 | F->deleteBody(); |
| 174 | assert(F->isExternal() && "This didn't make the function external!"); |
| 175 | } |
| 176 | |
Chris Lattner | 5a7a9e5 | 2006-03-08 23:55:38 +0000 | [diff] [blame] | 177 | /// GetTorInit - Given a list of entries for static ctors/dtors, return them |
| 178 | /// as a constant array. |
| 179 | static Constant *GetTorInit(std::vector<std::pair<Function*, int> > &TorList) { |
| 180 | assert(!TorList.empty() && "Don't create empty tor list!"); |
| 181 | std::vector<Constant*> ArrayElts; |
| 182 | for (unsigned i = 0, e = TorList.size(); i != e; ++i) { |
| 183 | std::vector<Constant*> Elts; |
| 184 | Elts.push_back(ConstantSInt::get(Type::IntTy, TorList[i].second)); |
| 185 | Elts.push_back(TorList[i].first); |
| 186 | ArrayElts.push_back(ConstantStruct::get(Elts)); |
| 187 | } |
| 188 | return ConstantArray::get(ArrayType::get(ArrayElts[0]->getType(), |
| 189 | ArrayElts.size()), |
| 190 | ArrayElts); |
| 191 | } |
| 192 | |
| 193 | /// SplitStaticCtorDtor - A module was recently split into two parts, M1/M2, and |
| 194 | /// M1 has all of the global variables. If M2 contains any functions that are |
| 195 | /// static ctors/dtors, we need to add an llvm.global_[cd]tors global to M2, and |
| 196 | /// prune appropriate entries out of M1s list. |
| 197 | static void SplitStaticCtorDtor(const char *GlobalName, Module *M1, Module *M2){ |
| 198 | GlobalVariable *GV = M1->getNamedGlobal(GlobalName); |
| 199 | if (!GV || GV->isExternal() || GV->hasInternalLinkage() || |
| 200 | !GV->use_empty()) return; |
| 201 | |
| 202 | std::vector<std::pair<Function*, int> > M1Tors, M2Tors; |
| 203 | ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer()); |
| 204 | if (!InitList) return; |
| 205 | |
| 206 | for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) { |
| 207 | if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){ |
| 208 | if (CS->getNumOperands() != 2) return; // Not array of 2-element structs. |
| 209 | |
| 210 | if (CS->getOperand(1)->isNullValue()) |
| 211 | break; // Found a null terminator, stop here. |
| 212 | |
| 213 | ConstantSInt *CI = dyn_cast<ConstantSInt>(CS->getOperand(0)); |
| 214 | int Priority = CI ? CI->getValue() : 0; |
| 215 | |
| 216 | Constant *FP = CS->getOperand(1); |
| 217 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP)) |
| 218 | if (CE->getOpcode() == Instruction::Cast) |
| 219 | FP = CE->getOperand(0); |
| 220 | if (Function *F = dyn_cast<Function>(FP)) { |
| 221 | if (!F->isExternal()) |
| 222 | M1Tors.push_back(std::make_pair(F, Priority)); |
| 223 | else { |
| 224 | // Map to M2's version of the function. |
| 225 | F = M2->getFunction(F->getName(), F->getFunctionType()); |
| 226 | M2Tors.push_back(std::make_pair(F, Priority)); |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | GV->eraseFromParent(); |
| 233 | if (!M1Tors.empty()) { |
| 234 | Constant *M1Init = GetTorInit(M1Tors); |
| 235 | new GlobalVariable(M1Init->getType(), false, GlobalValue::AppendingLinkage, |
| 236 | M1Init, GlobalName, M1); |
| 237 | } |
| 238 | |
| 239 | GV = M2->getNamedGlobal(GlobalName); |
| 240 | assert(GV && "Not a clone of M1?"); |
| 241 | assert(GV->use_empty() && "llvm.ctors shouldn't have uses!"); |
| 242 | |
| 243 | GV->eraseFromParent(); |
| 244 | if (!M2Tors.empty()) { |
| 245 | Constant *M2Init = GetTorInit(M2Tors); |
| 246 | new GlobalVariable(M2Init->getType(), false, GlobalValue::AppendingLinkage, |
| 247 | M2Init, GlobalName, M2); |
| 248 | } |
| 249 | } |
| 250 | |
Patrick Jenkins | e47863e | 2006-07-28 01:19:28 +0000 | [diff] [blame] | 251 | //// RewriteUsesInNewModule - takes a Module and a reference to a globalvalue |
| 252 | //// (OrigVal) in that module and changes the reference to a different |
| 253 | //// globalvalue (NewVal) in a seperate module. |
| 254 | static void RewriteUsesInNewModule(Constant *OrigVal, Constant *NewVal, |
| 255 | Module *TargetMod) { |
| 256 | assert(OrigVal->getType() == NewVal->getType() && |
| 257 | "Can't replace something with a different type"); |
| 258 | for (Value::use_iterator UI = OrigVal->use_begin(), E = OrigVal->use_end(); |
| 259 | UI != E; ) { |
| 260 | Value::use_iterator TmpUI = UI++; |
| 261 | User *U = *TmpUI; |
| 262 | if (Instruction *Inst = dyn_cast<Instruction>(U)) { |
| 263 | Module *InstM = Inst->getParent()->getParent()->getParent(); |
| 264 | if (InstM != TargetMod) { |
| 265 | TmpUI.getUse() = NewVal; |
| 266 | } |
| 267 | } else if (GlobalVariable *GV = dyn_cast<GlobalVariable>(U)) { |
| 268 | if (GV->getParent() != TargetMod) { |
| 269 | TmpUI.getUse() = NewVal; |
| 270 | } |
| 271 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) { |
| 272 | // If nothing uses this, don't bother making a copy. |
| 273 | if (CE->use_empty()) continue; |
| 274 | Constant *NewCE = CE->getWithOperandReplaced(TmpUI.getOperandNo(), |
| 275 | NewVal); |
| 276 | RewriteUsesInNewModule(CE, NewCE, TargetMod); |
| 277 | } else if (ConstantStruct *CS = dyn_cast<ConstantStruct>(U)) { |
| 278 | // If nothing uses this, don't bother making a copy. |
| 279 | if (CS->use_empty()) continue; |
| 280 | unsigned OpNo = TmpUI.getOperandNo(); |
| 281 | std::vector<Constant*> Ops; |
| 282 | for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) |
| 283 | Ops.push_back(i == OpNo ? NewVal : CS->getOperand(i)); |
| 284 | Constant *NewStruct = ConstantStruct::get(Ops); |
| 285 | RewriteUsesInNewModule(CS, NewStruct, TargetMod); |
| 286 | } else if (ConstantPacked *CP = dyn_cast<ConstantPacked>(U)) { |
| 287 | // If nothing uses this, don't bother making a copy. |
| 288 | if (CP->use_empty()) continue; |
| 289 | unsigned OpNo = TmpUI.getOperandNo(); |
| 290 | std::vector<Constant*> Ops; |
| 291 | for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) |
| 292 | Ops.push_back(i == OpNo ? NewVal : CP->getOperand(i)); |
| 293 | Constant *NewPacked = ConstantPacked::get(Ops); |
| 294 | RewriteUsesInNewModule(CP, NewPacked, TargetMod); |
| 295 | } else if (ConstantArray *CA = dyn_cast<ConstantArray>(U)) { |
| 296 | // If nothing uses this, don't bother making a copy. |
| 297 | if (CA->use_empty()) continue; |
| 298 | unsigned OpNo = TmpUI.getOperandNo(); |
| 299 | std::vector<Constant*> Ops; |
| 300 | for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i) { |
| 301 | Ops.push_back(i == OpNo ? NewVal : CA->getOperand(i)); |
| 302 | } |
| 303 | Constant *NewArray = ConstantArray::get(CA->getType(), Ops); |
| 304 | RewriteUsesInNewModule(CA, NewArray, TargetMod); |
| 305 | } else { |
| 306 | assert(0 && "Unexpected user"); |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 312 | /// SplitFunctionsOutOfModule - Given a module and a list of functions in the |
| 313 | /// module, split the functions OUT of the specified module, and place them in |
| 314 | /// the new module. |
| 315 | Module *llvm::SplitFunctionsOutOfModule(Module *M, |
| 316 | const std::vector<Function*> &F) { |
| 317 | // Make sure functions & globals are all external so that linkage |
| 318 | // between the two modules will work. |
| 319 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 320 | I->setLinkage(GlobalValue::ExternalLinkage); |
Chris Lattner | 5a7a9e5 | 2006-03-08 23:55:38 +0000 | [diff] [blame] | 321 | for (Module::global_iterator I = M->global_begin(), E = M->global_end(); |
| 322 | I != E; ++I) |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 323 | I->setLinkage(GlobalValue::ExternalLinkage); |
| 324 | |
Patrick Jenkins | e47863e | 2006-07-28 01:19:28 +0000 | [diff] [blame] | 325 | // First off, we need to create the new module... |
| 326 | Module *New = new Module(M->getModuleIdentifier()); |
| 327 | New->setEndianness(M->getEndianness()); |
| 328 | New->setPointerSize(M->getPointerSize()); |
| 329 | New->setTargetTriple(M->getTargetTriple()); |
| 330 | New->setModuleInlineAsm(M->getModuleInlineAsm()); |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 331 | |
Patrick Jenkins | e47863e | 2006-07-28 01:19:28 +0000 | [diff] [blame] | 332 | // Copy all of the dependent libraries over. |
| 333 | for (Module::lib_iterator I = M->lib_begin(), E = M->lib_end(); I != E; ++I) |
| 334 | New->addLibrary(*I); |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 335 | |
Patrick Jenkins | e47863e | 2006-07-28 01:19:28 +0000 | [diff] [blame] | 336 | // build a set of the functions to search later... |
Chris Lattner | fb4b96e | 2004-04-02 16:28:32 +0000 | [diff] [blame] | 337 | std::set<std::pair<std::string, const PointerType*> > TestFunctions; |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 338 | for (unsigned i = 0, e = F.size(); i != e; ++i) { |
Patrick Jenkins | e47863e | 2006-07-28 01:19:28 +0000 | [diff] [blame] | 339 | TestFunctions.insert(std::make_pair(F[i]->getName(), F[i]->getType())); |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Patrick Jenkins | e47863e | 2006-07-28 01:19:28 +0000 | [diff] [blame] | 342 | std::map<GlobalValue*, GlobalValue*> GlobalToPrototypeMap; |
| 343 | std::vector<GlobalValue*> OrigGlobals; |
| 344 | |
| 345 | // Adding specified functions to new module... |
| 346 | for (Module::iterator I = M->begin(), E = M->end(); I != E;) { |
| 347 | OrigGlobals.push_back(I); |
| 348 | if(TestFunctions.count(std::make_pair(I->getName(), I->getType()))) { |
| 349 | Module::iterator tempI = I; |
| 350 | I++; |
| 351 | Function * func = new Function(tempI->getFunctionType(), |
| 352 | GlobalValue::ExternalLinkage); |
| 353 | M->getFunctionList().insert(tempI, func); |
| 354 | New->getFunctionList().splice(New->end(), |
| 355 | M->getFunctionList(), |
| 356 | tempI); |
| 357 | func->setName(tempI->getName()); |
| 358 | func->setCallingConv(tempI->getCallingConv()); |
| 359 | GlobalToPrototypeMap[tempI] = func; |
| 360 | // NEW TO OLD |
| 361 | } else { |
| 362 | Function * func = new Function(I->getFunctionType(), |
| 363 | GlobalValue::ExternalLinkage, |
| 364 | I->getName(), |
| 365 | New); |
| 366 | func->setCallingConv(I->getCallingConv()); |
| 367 | GlobalToPrototypeMap[I] = func; |
| 368 | // NEW TO OLD |
| 369 | I++; |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | //copy over global list |
| 374 | for (Module::global_iterator I = M->global_begin(), |
| 375 | E = M->global_end(); I != E; ++I) { |
| 376 | OrigGlobals.push_back(I); |
| 377 | GlobalVariable *glob = new GlobalVariable (I->getType()->getElementType(), |
| 378 | I->isConstant(), |
| 379 | GlobalValue::ExternalLinkage, |
| 380 | 0, |
| 381 | I->getName(), |
| 382 | New); |
| 383 | GlobalToPrototypeMap[I] = glob; |
| 384 | } |
Chris Lattner | 5a7a9e5 | 2006-03-08 23:55:38 +0000 | [diff] [blame] | 385 | |
Patrick Jenkins | e47863e | 2006-07-28 01:19:28 +0000 | [diff] [blame] | 386 | // Copy all of the type symbol table entries over. |
| 387 | const SymbolTable &SymTab = M->getSymbolTable(); |
| 388 | SymbolTable::type_const_iterator TypeI = SymTab.type_begin(); |
| 389 | SymbolTable::type_const_iterator TypeE = SymTab.type_end(); |
| 390 | for (; TypeI != TypeE; ++TypeI) |
| 391 | New->addTypeName(TypeI->first, TypeI->second); |
| 392 | |
| 393 | // Loop over globals, rewriting uses in the module the prototype is in to use |
| 394 | // the prototype. |
| 395 | for (unsigned i = 0, e = OrigGlobals.size(); i != e; ++i) { |
| 396 | assert(OrigGlobals[i]->getName() == |
| 397 | GlobalToPrototypeMap[OrigGlobals[i]]->getName()); |
| 398 | RewriteUsesInNewModule(OrigGlobals[i], GlobalToPrototypeMap[OrigGlobals[i]], |
| 399 | OrigGlobals[i]->getParent()); |
| 400 | } |
| 401 | |
Chris Lattner | 5a7a9e5 | 2006-03-08 23:55:38 +0000 | [diff] [blame] | 402 | // Make sure that there is a global ctor/dtor array in both halves of the |
| 403 | // module if they both have static ctor/dtor functions. |
| 404 | SplitStaticCtorDtor("llvm.global_ctors", M, New); |
| 405 | SplitStaticCtorDtor("llvm.global_dtors", M, New); |
| 406 | |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 407 | return New; |
| 408 | } |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 409 | |
| 410 | //===----------------------------------------------------------------------===// |
| 411 | // Basic Block Extraction Code |
| 412 | //===----------------------------------------------------------------------===// |
| 413 | |
| 414 | namespace { |
| 415 | std::vector<BasicBlock*> BlocksToNotExtract; |
| 416 | |
| 417 | /// BlockExtractorPass - This pass is used by bugpoint to extract all blocks |
| 418 | /// from the module into their own functions except for those specified by the |
| 419 | /// BlocksToNotExtract list. |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 420 | class BlockExtractorPass : public ModulePass { |
| 421 | bool runOnModule(Module &M); |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 422 | }; |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 423 | RegisterPass<BlockExtractorPass> |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 424 | XX("extract-bbs", "Extract Basic Blocks From Module (for bugpoint use)"); |
| 425 | } |
| 426 | |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 427 | bool BlockExtractorPass::runOnModule(Module &M) { |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 428 | std::set<BasicBlock*> TranslatedBlocksToNotExtract; |
| 429 | for (unsigned i = 0, e = BlocksToNotExtract.size(); i != e; ++i) { |
| 430 | BasicBlock *BB = BlocksToNotExtract[i]; |
| 431 | Function *F = BB->getParent(); |
| 432 | |
| 433 | // Map the corresponding function in this module. |
| 434 | Function *MF = M.getFunction(F->getName(), F->getFunctionType()); |
| 435 | |
| 436 | // Figure out which index the basic block is in its function. |
| 437 | Function::iterator BBI = MF->begin(); |
| 438 | std::advance(BBI, std::distance(F->begin(), Function::iterator(BB))); |
| 439 | TranslatedBlocksToNotExtract.insert(BBI); |
| 440 | } |
| 441 | |
| 442 | // Now that we know which blocks to not extract, figure out which ones we WANT |
| 443 | // to extract. |
| 444 | std::vector<BasicBlock*> BlocksToExtract; |
| 445 | for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) |
| 446 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) |
| 447 | if (!TranslatedBlocksToNotExtract.count(BB)) |
| 448 | BlocksToExtract.push_back(BB); |
| 449 | |
| 450 | for (unsigned i = 0, e = BlocksToExtract.size(); i != e; ++i) |
| 451 | ExtractBasicBlock(BlocksToExtract[i]); |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 452 | |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 453 | return !BlocksToExtract.empty(); |
| 454 | } |
| 455 | |
| 456 | /// ExtractMappedBlocksFromModule - Extract all but the specified basic blocks |
| 457 | /// into their own functions. The only detail is that M is actually a module |
| 458 | /// cloned from the one the BBs are in, so some mapping needs to be performed. |
| 459 | /// If this operation fails for some reason (ie the implementation is buggy), |
| 460 | /// this function should return null, otherwise it returns a new Module. |
| 461 | Module *BugDriver::ExtractMappedBlocksFromModule(const |
| 462 | std::vector<BasicBlock*> &BBs, |
| 463 | Module *M) { |
| 464 | // Set the global list so that pass will be able to access it. |
| 465 | BlocksToNotExtract = BBs; |
| 466 | |
| 467 | std::vector<const PassInfo*> PI; |
| 468 | PI.push_back(getPI(new BlockExtractorPass())); |
| 469 | Module *Ret = runPassesOn(M, PI); |
| 470 | BlocksToNotExtract.clear(); |
Chris Lattner | 891150f | 2004-08-12 02:36:50 +0000 | [diff] [blame] | 471 | if (Ret == 0) { |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 472 | std::cout << "*** Basic Block extraction failed, please report a bug!\n"; |
Chris Lattner | 891150f | 2004-08-12 02:36:50 +0000 | [diff] [blame] | 473 | M = swapProgramIn(M); |
| 474 | EmitProgressBytecode("basicblockextractfail", true); |
Chris Lattner | b923b2e | 2006-05-12 17:28:36 +0000 | [diff] [blame] | 475 | swapProgramIn(M); |
Chris Lattner | 891150f | 2004-08-12 02:36:50 +0000 | [diff] [blame] | 476 | } |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 477 | return Ret; |
| 478 | } |