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 | // |
Chris Lattner | 21c62da | 2007-12-29 20:44:31 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // 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" |
Owen Anderson | db1cd5e | 2009-07-13 22:40:32 +0000 | [diff] [blame] | 18 | #include "llvm/LLVMContext.h" |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 19 | #include "llvm/Module.h" |
| 20 | #include "llvm/PassManager.h" |
Brian Gaeke | d1a85a7 | 2003-09-10 21:11:42 +0000 | [diff] [blame] | 21 | #include "llvm/Pass.h" |
Misha Brukman | e49603d | 2003-08-07 21:19:30 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/Verifier.h" |
Dan Gohman | e860dcb | 2009-07-13 22:56:37 +0000 | [diff] [blame] | 23 | #include "llvm/Assembly/Writer.h" |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 24 | #include "llvm/Transforms/IPO.h" |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 25 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 26 | #include "llvm/Transforms/Utils/Cloning.h" |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 27 | #include "llvm/Transforms/Utils/FunctionUtils.h" |
Chris Lattner | 5da69c7 | 2003-10-23 15:42:55 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetData.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 29 | #include "llvm/Support/CommandLine.h" |
| 30 | #include "llvm/Support/Debug.h" |
| 31 | #include "llvm/Support/FileUtilities.h" |
Nick Lewycky | 6fa98b1 | 2007-11-14 06:47:06 +0000 | [diff] [blame] | 32 | #include "llvm/System/Path.h" |
| 33 | #include "llvm/System/Signals.h" |
Chris Lattner | fb4b96e | 2004-04-02 16:28:32 +0000 | [diff] [blame] | 34 | #include <set> |
Chris Lattner | c6b519d | 2003-11-23 04:51:05 +0000 | [diff] [blame] | 35 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 36 | |
| 37 | namespace llvm { |
Chris Lattner | c6b519d | 2003-11-23 04:51:05 +0000 | [diff] [blame] | 38 | bool DisableSimplifyCFG = false; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 39 | } // End llvm namespace |
| 40 | |
Chris Lattner | 6db70ef | 2003-04-25 22:08:12 +0000 | [diff] [blame] | 41 | namespace { |
| 42 | cl::opt<bool> |
Chris Lattner | 6db70ef | 2003-04-25 22:08:12 +0000 | [diff] [blame] | 43 | NoDCE ("disable-dce", |
| 44 | cl::desc("Do not use the -dce pass to reduce testcases")); |
Chris Lattner | 47ae4a1 | 2003-08-05 15:51:05 +0000 | [diff] [blame] | 45 | cl::opt<bool, true> |
| 46 | NoSCFG("disable-simplifycfg", cl::location(DisableSimplifyCFG), |
Chris Lattner | 6db70ef | 2003-04-25 22:08:12 +0000 | [diff] [blame] | 47 | cl::desc("Do not use the -simplifycfg pass to reduce testcases")); |
| 48 | } |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 49 | |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 50 | /// deleteInstructionFromProgram - This method clones the current Program and |
| 51 | /// deletes the specified instruction from the cloned module. It then runs a |
| 52 | /// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code which |
| 53 | /// depends on the value. The modified module is then returned. |
| 54 | /// |
Chris Lattner | 0cc8807 | 2004-02-18 21:50:26 +0000 | [diff] [blame] | 55 | Module *BugDriver::deleteInstructionFromProgram(const Instruction *I, |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 56 | unsigned Simplification) const { |
| 57 | Module *Result = CloneModule(Program); |
| 58 | |
Chris Lattner | 0cc8807 | 2004-02-18 21:50:26 +0000 | [diff] [blame] | 59 | const BasicBlock *PBB = I->getParent(); |
| 60 | const Function *PF = PBB->getParent(); |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 61 | |
| 62 | Module::iterator RFI = Result->begin(); // Get iterator to corresponding fn |
Chris Lattner | 0cc8807 | 2004-02-18 21:50:26 +0000 | [diff] [blame] | 63 | std::advance(RFI, std::distance(PF->getParent()->begin(), |
| 64 | Module::const_iterator(PF))); |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 65 | |
| 66 | Function::iterator RBI = RFI->begin(); // Get iterator to corresponding BB |
Chris Lattner | 0cc8807 | 2004-02-18 21:50:26 +0000 | [diff] [blame] | 67 | std::advance(RBI, std::distance(PF->begin(), Function::const_iterator(PBB))); |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 68 | |
| 69 | BasicBlock::iterator RI = RBI->begin(); // Get iterator to corresponding inst |
Chris Lattner | 0cc8807 | 2004-02-18 21:50:26 +0000 | [diff] [blame] | 70 | std::advance(RI, std::distance(PBB->begin(), BasicBlock::const_iterator(I))); |
| 71 | Instruction *TheInst = RI; // Got the corresponding instruction! |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 72 | |
| 73 | // If this instruction produces a value, replace any users with null values |
Chris Lattner | e78109e | 2008-04-28 00:04:58 +0000 | [diff] [blame] | 74 | if (isa<StructType>(TheInst->getType())) |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 75 | TheInst->replaceAllUsesWith(UndefValue::get(TheInst->getType())); |
Chris Lattner | e78109e | 2008-04-28 00:04:58 +0000 | [diff] [blame] | 76 | else if (TheInst->getType() != Type::VoidTy) |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 77 | TheInst->replaceAllUsesWith(Constant::getNullValue(TheInst->getType())); |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 78 | |
| 79 | // Remove the instruction from the program. |
Chris Lattner | 0cc8807 | 2004-02-18 21:50:26 +0000 | [diff] [blame] | 80 | TheInst->getParent()->getInstList().erase(TheInst); |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 81 | |
Chris Lattner | 5a7a9e5 | 2006-03-08 23:55:38 +0000 | [diff] [blame] | 82 | |
| 83 | //writeProgramToFile("current.bc", Result); |
| 84 | |
Chris Lattner | 44be257 | 2003-04-24 22:53:24 +0000 | [diff] [blame] | 85 | // Spiff up the output a little bit. |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 86 | PassManager Passes; |
Chris Lattner | 5da69c7 | 2003-10-23 15:42:55 +0000 | [diff] [blame] | 87 | // Make sure that the appropriate target data is always used... |
Chris Lattner | 831b121 | 2006-06-16 18:23:49 +0000 | [diff] [blame] | 88 | Passes.add(new TargetData(Result)); |
Chris Lattner | 5da69c7 | 2003-10-23 15:42:55 +0000 | [diff] [blame] | 89 | |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 90 | /// FIXME: If this used runPasses() like the methods below, we could get rid |
| 91 | /// of the -disable-* options! |
Chris Lattner | 6db70ef | 2003-04-25 22:08:12 +0000 | [diff] [blame] | 92 | if (Simplification > 1 && !NoDCE) |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 93 | Passes.add(createDeadCodeEliminationPass()); |
Chris Lattner | 47ae4a1 | 2003-08-05 15:51:05 +0000 | [diff] [blame] | 94 | if (Simplification && !DisableSimplifyCFG) |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 95 | Passes.add(createCFGSimplificationPass()); // Delete dead control flow |
Chris Lattner | 10f22cb | 2003-03-07 18:17:13 +0000 | [diff] [blame] | 96 | |
| 97 | Passes.add(createVerifierPass()); |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 98 | Passes.run(*Result); |
| 99 | return Result; |
| 100 | } |
Chris Lattner | ba386d9 | 2003-02-28 16:13:20 +0000 | [diff] [blame] | 101 | |
Chris Lattner | fcb6ec0 | 2003-11-05 21:45:35 +0000 | [diff] [blame] | 102 | static const PassInfo *getPI(Pass *P) { |
| 103 | const PassInfo *PI = P->getPassInfo(); |
| 104 | delete P; |
| 105 | return PI; |
| 106 | } |
| 107 | |
Chris Lattner | ba386d9 | 2003-02-28 16:13:20 +0000 | [diff] [blame] | 108 | /// performFinalCleanups - This method clones the current Program and performs |
| 109 | /// 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] | 110 | /// before handing it to the user. |
Chris Lattner | ba386d9 | 2003-02-28 16:13:20 +0000 | [diff] [blame] | 111 | /// |
Chris Lattner | fcb6ec0 | 2003-11-05 21:45:35 +0000 | [diff] [blame] | 112 | Module *BugDriver::performFinalCleanups(Module *M, bool MayModifySemantics) { |
Chris Lattner | 28b8ed9 | 2003-05-21 19:41:31 +0000 | [diff] [blame] | 113 | // Make all functions external, so GlobalDCE doesn't delete them... |
| 114 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 115 | I->setLinkage(GlobalValue::ExternalLinkage); |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 116 | |
Chris Lattner | fcb6ec0 | 2003-11-05 21:45:35 +0000 | [diff] [blame] | 117 | std::vector<const PassInfo*> CleanupPasses; |
Chris Lattner | fcb6ec0 | 2003-11-05 21:45:35 +0000 | [diff] [blame] | 118 | CleanupPasses.push_back(getPI(createGlobalDCEPass())); |
| 119 | CleanupPasses.push_back(getPI(createDeadTypeEliminationPass())); |
Chris Lattner | fcb6ec0 | 2003-11-05 21:45:35 +0000 | [diff] [blame] | 120 | |
Chris Lattner | c6b519d | 2003-11-23 04:51:05 +0000 | [diff] [blame] | 121 | if (MayModifySemantics) |
| 122 | CleanupPasses.push_back(getPI(createDeadArgHackingPass())); |
| 123 | else |
| 124 | CleanupPasses.push_back(getPI(createDeadArgEliminationPass())); |
Chris Lattner | fcb6ec0 | 2003-11-05 21:45:35 +0000 | [diff] [blame] | 125 | |
Chris Lattner | a75766a | 2004-03-14 21:17:22 +0000 | [diff] [blame] | 126 | Module *New = runPassesOn(M, CleanupPasses); |
| 127 | if (New == 0) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 128 | errs() << "Final cleanups failed. Sorry. :( Please report a bug!\n"; |
Chris Lattner | 9b5b190 | 2005-02-23 06:12:11 +0000 | [diff] [blame] | 129 | return M; |
Chris Lattner | fcb6ec0 | 2003-11-05 21:45:35 +0000 | [diff] [blame] | 130 | } |
Chris Lattner | a75766a | 2004-03-14 21:17:22 +0000 | [diff] [blame] | 131 | delete M; |
| 132 | return New; |
Chris Lattner | ba386d9 | 2003-02-28 16:13:20 +0000 | [diff] [blame] | 133 | } |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 134 | |
| 135 | |
Chris Lattner | 7546c38 | 2004-03-14 20:02:07 +0000 | [diff] [blame] | 136 | /// ExtractLoop - Given a module, extract up to one loop from it into a new |
| 137 | /// function. This returns null if there are no extractable loops in the |
| 138 | /// program or if the loop extractor crashes. |
| 139 | Module *BugDriver::ExtractLoop(Module *M) { |
| 140 | std::vector<const PassInfo*> LoopExtractPasses; |
| 141 | LoopExtractPasses.push_back(getPI(createSingleLoopExtractorPass())); |
| 142 | |
Chris Lattner | a75766a | 2004-03-14 21:17:22 +0000 | [diff] [blame] | 143 | Module *NewM = runPassesOn(M, LoopExtractPasses); |
| 144 | if (NewM == 0) { |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 145 | Module *Old = swapProgramIn(M); |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 146 | outs() << "*** Loop extraction failed: "; |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 147 | EmitProgressBitcode("loopextraction", true); |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 148 | outs() << "*** Sorry. :( Please report a bug!\n"; |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 149 | swapProgramIn(Old); |
Chris Lattner | 7546c38 | 2004-03-14 20:02:07 +0000 | [diff] [blame] | 150 | return 0; |
Chris Lattner | 7546c38 | 2004-03-14 20:02:07 +0000 | [diff] [blame] | 151 | } |
Chris Lattner | a75766a | 2004-03-14 21:17:22 +0000 | [diff] [blame] | 152 | |
| 153 | // 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] | 154 | // extracted and we should return null. Limit the number of loops we extract |
| 155 | // to avoid taking forever. |
| 156 | static unsigned NumExtracted = 32; |
Chris Lattner | 90c18c5 | 2004-11-16 06:31:38 +0000 | [diff] [blame] | 157 | if (M->size() == NewM->size() || --NumExtracted == 0) { |
Chris Lattner | a75766a | 2004-03-14 21:17:22 +0000 | [diff] [blame] | 158 | delete NewM; |
| 159 | return 0; |
Chris Lattner | 90c18c5 | 2004-11-16 06:31:38 +0000 | [diff] [blame] | 160 | } else { |
| 161 | assert(M->size() < NewM->size() && "Loop extract removed functions?"); |
| 162 | Module::iterator MI = NewM->begin(); |
| 163 | for (unsigned i = 0, e = M->size(); i != e; ++i) |
| 164 | ++MI; |
Chris Lattner | a75766a | 2004-03-14 21:17:22 +0000 | [diff] [blame] | 165 | } |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 166 | |
Chris Lattner | a75766a | 2004-03-14 21:17:22 +0000 | [diff] [blame] | 167 | return NewM; |
Chris Lattner | 7546c38 | 2004-03-14 20:02:07 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 171 | // DeleteFunctionBody - "Remove" the function by deleting all of its basic |
| 172 | // blocks, making it external. |
| 173 | // |
| 174 | void llvm::DeleteFunctionBody(Function *F) { |
| 175 | // delete the body of the function... |
| 176 | F->deleteBody(); |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 177 | assert(F->isDeclaration() && "This didn't make the function external!"); |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Chris Lattner | 5a7a9e5 | 2006-03-08 23:55:38 +0000 | [diff] [blame] | 180 | /// GetTorInit - Given a list of entries for static ctors/dtors, return them |
| 181 | /// as a constant array. |
| 182 | static Constant *GetTorInit(std::vector<std::pair<Function*, int> > &TorList) { |
| 183 | assert(!TorList.empty() && "Don't create empty tor list!"); |
| 184 | std::vector<Constant*> ArrayElts; |
| 185 | for (unsigned i = 0, e = TorList.size(); i != e; ++i) { |
| 186 | std::vector<Constant*> Elts; |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 187 | Elts.push_back(ConstantInt::get(Type::Int32Ty, TorList[i].second)); |
Chris Lattner | 5a7a9e5 | 2006-03-08 23:55:38 +0000 | [diff] [blame] | 188 | Elts.push_back(TorList[i].first); |
Owen Anderson | d7f2a6c | 2009-08-05 23:16:16 +0000 | [diff] [blame] | 189 | ArrayElts.push_back(ConstantStruct::get( |
| 190 | TorList[i].first->getContext(), Elts)); |
Chris Lattner | 5a7a9e5 | 2006-03-08 23:55:38 +0000 | [diff] [blame] | 191 | } |
Owen Anderson | debcb01 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 192 | return ConstantArray::get(ArrayType::get(ArrayElts[0]->getType(), |
Chris Lattner | 5a7a9e5 | 2006-03-08 23:55:38 +0000 | [diff] [blame] | 193 | ArrayElts.size()), |
| 194 | ArrayElts); |
| 195 | } |
| 196 | |
| 197 | /// SplitStaticCtorDtor - A module was recently split into two parts, M1/M2, and |
| 198 | /// M1 has all of the global variables. If M2 contains any functions that are |
| 199 | /// static ctors/dtors, we need to add an llvm.global_[cd]tors global to M2, and |
| 200 | /// prune appropriate entries out of M1s list. |
Dan Gohman | d50330c | 2009-04-22 15:57:18 +0000 | [diff] [blame] | 201 | static void SplitStaticCtorDtor(const char *GlobalName, Module *M1, Module *M2, |
| 202 | DenseMap<const Value*, Value*> ValueMap) { |
Chris Lattner | 5a7a9e5 | 2006-03-08 23:55:38 +0000 | [diff] [blame] | 203 | GlobalVariable *GV = M1->getNamedGlobal(GlobalName); |
Rafael Espindola | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 204 | if (!GV || GV->isDeclaration() || GV->hasLocalLinkage() || |
Chris Lattner | 5a7a9e5 | 2006-03-08 23:55:38 +0000 | [diff] [blame] | 205 | !GV->use_empty()) return; |
| 206 | |
| 207 | std::vector<std::pair<Function*, int> > M1Tors, M2Tors; |
| 208 | ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer()); |
| 209 | if (!InitList) return; |
| 210 | |
| 211 | for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) { |
| 212 | if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){ |
| 213 | if (CS->getNumOperands() != 2) return; // Not array of 2-element structs. |
| 214 | |
| 215 | if (CS->getOperand(1)->isNullValue()) |
| 216 | break; // Found a null terminator, stop here. |
| 217 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 218 | ConstantInt *CI = dyn_cast<ConstantInt>(CS->getOperand(0)); |
| 219 | int Priority = CI ? CI->getSExtValue() : 0; |
Chris Lattner | 5a7a9e5 | 2006-03-08 23:55:38 +0000 | [diff] [blame] | 220 | |
| 221 | Constant *FP = CS->getOperand(1); |
| 222 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP)) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 223 | if (CE->isCast()) |
Chris Lattner | 5a7a9e5 | 2006-03-08 23:55:38 +0000 | [diff] [blame] | 224 | FP = CE->getOperand(0); |
| 225 | if (Function *F = dyn_cast<Function>(FP)) { |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 226 | if (!F->isDeclaration()) |
Chris Lattner | 5a7a9e5 | 2006-03-08 23:55:38 +0000 | [diff] [blame] | 227 | M1Tors.push_back(std::make_pair(F, Priority)); |
| 228 | else { |
| 229 | // Map to M2's version of the function. |
Dan Gohman | d50330c | 2009-04-22 15:57:18 +0000 | [diff] [blame] | 230 | F = cast<Function>(ValueMap[F]); |
Chris Lattner | 5a7a9e5 | 2006-03-08 23:55:38 +0000 | [diff] [blame] | 231 | M2Tors.push_back(std::make_pair(F, Priority)); |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | GV->eraseFromParent(); |
| 238 | if (!M1Tors.empty()) { |
| 239 | Constant *M1Init = GetTorInit(M1Tors); |
Owen Anderson | e9b11b4 | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 240 | new GlobalVariable(*M1, M1Init->getType(), false, |
Owen Anderson | 3d29df3 | 2009-07-08 01:26:06 +0000 | [diff] [blame] | 241 | GlobalValue::AppendingLinkage, |
Owen Anderson | e9b11b4 | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 242 | M1Init, GlobalName); |
Chris Lattner | 5a7a9e5 | 2006-03-08 23:55:38 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | GV = M2->getNamedGlobal(GlobalName); |
| 246 | assert(GV && "Not a clone of M1?"); |
| 247 | assert(GV->use_empty() && "llvm.ctors shouldn't have uses!"); |
| 248 | |
| 249 | GV->eraseFromParent(); |
| 250 | if (!M2Tors.empty()) { |
| 251 | Constant *M2Init = GetTorInit(M2Tors); |
Owen Anderson | e9b11b4 | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 252 | new GlobalVariable(*M2, M2Init->getType(), false, |
Owen Anderson | 3d29df3 | 2009-07-08 01:26:06 +0000 | [diff] [blame] | 253 | GlobalValue::AppendingLinkage, |
Owen Anderson | e9b11b4 | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 254 | M2Init, GlobalName); |
Chris Lattner | 5a7a9e5 | 2006-03-08 23:55:38 +0000 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
Patrick Jenkins | e47863e | 2006-07-28 01:19:28 +0000 | [diff] [blame] | 258 | |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 259 | /// SplitFunctionsOutOfModule - Given a module and a list of functions in the |
| 260 | /// module, split the functions OUT of the specified module, and place them in |
| 261 | /// the new module. |
Dan Gohman | d50330c | 2009-04-22 15:57:18 +0000 | [diff] [blame] | 262 | Module * |
| 263 | llvm::SplitFunctionsOutOfModule(Module *M, |
| 264 | const std::vector<Function*> &F, |
| 265 | DenseMap<const Value*, Value*> &ValueMap) { |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 266 | // Make sure functions & globals are all external so that linkage |
| 267 | // between the two modules will work. |
| 268 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 269 | I->setLinkage(GlobalValue::ExternalLinkage); |
Chris Lattner | 5a7a9e5 | 2006-03-08 23:55:38 +0000 | [diff] [blame] | 270 | for (Module::global_iterator I = M->global_begin(), E = M->global_end(); |
Owen Anderson | 7220b81 | 2008-07-08 16:38:42 +0000 | [diff] [blame] | 271 | I != E; ++I) { |
Daniel Dunbar | 3f53fa9 | 2009-07-26 00:34:27 +0000 | [diff] [blame] | 272 | if (I->hasName() && I->getName()[0] == '\01') |
| 273 | I->setName(I->getName().substr(1)); |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 274 | I->setLinkage(GlobalValue::ExternalLinkage); |
Owen Anderson | 7220b81 | 2008-07-08 16:38:42 +0000 | [diff] [blame] | 275 | } |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 276 | |
Dan Gohman | d50330c | 2009-04-22 15:57:18 +0000 | [diff] [blame] | 277 | DenseMap<const Value*, Value*> NewValueMap; |
| 278 | Module *New = CloneModule(M, NewValueMap); |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 279 | |
Chris Lattner | fef0242 | 2006-11-09 06:24:56 +0000 | [diff] [blame] | 280 | // Make sure global initializers exist only in the safe module (CBE->.so) |
| 281 | for (Module::global_iterator I = New->global_begin(), E = New->global_end(); |
| 282 | I != E; ++I) |
| 283 | I->setInitializer(0); // Delete the initializer to make it external |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 284 | |
Chris Lattner | fef0242 | 2006-11-09 06:24:56 +0000 | [diff] [blame] | 285 | // Remove the Test functions from the Safe module |
Dan Gohman | d50330c | 2009-04-22 15:57:18 +0000 | [diff] [blame] | 286 | std::set<Function *> TestFunctions; |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 287 | for (unsigned i = 0, e = F.size(); i != e; ++i) { |
Dan Gohman | d50330c | 2009-04-22 15:57:18 +0000 | [diff] [blame] | 288 | Function *TNOF = cast<Function>(ValueMap[F[i]]); |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 289 | DEBUG(errs() << "Removing function "); |
| 290 | DEBUG(WriteAsOperand(errs(), TNOF, false)); |
| 291 | DEBUG(errs() << "\n"); |
Dan Gohman | d50330c | 2009-04-22 15:57:18 +0000 | [diff] [blame] | 292 | TestFunctions.insert(cast<Function>(NewValueMap[TNOF])); |
Chris Lattner | fef0242 | 2006-11-09 06:24:56 +0000 | [diff] [blame] | 293 | DeleteFunctionBody(TNOF); // Function is now external in this module! |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Chris Lattner | 5a7a9e5 | 2006-03-08 23:55:38 +0000 | [diff] [blame] | 296 | |
Chris Lattner | fef0242 | 2006-11-09 06:24:56 +0000 | [diff] [blame] | 297 | // Remove the Safe functions from the Test module |
| 298 | for (Module::iterator I = New->begin(), E = New->end(); I != E; ++I) |
Dan Gohman | d50330c | 2009-04-22 15:57:18 +0000 | [diff] [blame] | 299 | if (!TestFunctions.count(I)) |
Chris Lattner | fef0242 | 2006-11-09 06:24:56 +0000 | [diff] [blame] | 300 | DeleteFunctionBody(I); |
| 301 | |
Patrick Jenkins | e47863e | 2006-07-28 01:19:28 +0000 | [diff] [blame] | 302 | |
Chris Lattner | 5a7a9e5 | 2006-03-08 23:55:38 +0000 | [diff] [blame] | 303 | // Make sure that there is a global ctor/dtor array in both halves of the |
| 304 | // module if they both have static ctor/dtor functions. |
Dan Gohman | d50330c | 2009-04-22 15:57:18 +0000 | [diff] [blame] | 305 | SplitStaticCtorDtor("llvm.global_ctors", M, New, NewValueMap); |
| 306 | SplitStaticCtorDtor("llvm.global_dtors", M, New, NewValueMap); |
Chris Lattner | 5a7a9e5 | 2006-03-08 23:55:38 +0000 | [diff] [blame] | 307 | |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 308 | return New; |
| 309 | } |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 310 | |
| 311 | //===----------------------------------------------------------------------===// |
| 312 | // Basic Block Extraction Code |
| 313 | //===----------------------------------------------------------------------===// |
| 314 | |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 315 | /// ExtractMappedBlocksFromModule - Extract all but the specified basic blocks |
| 316 | /// into their own functions. The only detail is that M is actually a module |
| 317 | /// cloned from the one the BBs are in, so some mapping needs to be performed. |
| 318 | /// If this operation fails for some reason (ie the implementation is buggy), |
| 319 | /// this function should return null, otherwise it returns a new Module. |
| 320 | Module *BugDriver::ExtractMappedBlocksFromModule(const |
| 321 | std::vector<BasicBlock*> &BBs, |
| 322 | Module *M) { |
Nick Lewycky | 6fa98b1 | 2007-11-14 06:47:06 +0000 | [diff] [blame] | 323 | char *ExtraArg = NULL; |
| 324 | |
| 325 | sys::Path uniqueFilename("bugpoint-extractblocks"); |
| 326 | std::string ErrMsg; |
| 327 | if (uniqueFilename.createTemporaryFileOnDisk(true, &ErrMsg)) { |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 328 | outs() << "*** Basic Block extraction failed!\n"; |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 329 | errs() << "Error creating temporary file: " << ErrMsg << "\n"; |
Nick Lewycky | 6fa98b1 | 2007-11-14 06:47:06 +0000 | [diff] [blame] | 330 | M = swapProgramIn(M); |
| 331 | EmitProgressBitcode("basicblockextractfail", true); |
| 332 | swapProgramIn(M); |
| 333 | return 0; |
| 334 | } |
| 335 | sys::RemoveFileOnSignal(uniqueFilename); |
| 336 | |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 337 | std::string ErrorInfo; |
| 338 | raw_fd_ostream BlocksToNotExtractFile(uniqueFilename.c_str(), |
| 339 | /*Binary=*/false, /*Force=*/true, |
| 340 | ErrorInfo); |
| 341 | if (!ErrorInfo.empty()) { |
| 342 | outs() << "*** Basic Block extraction failed!\n"; |
| 343 | errs() << "Error writing list of blocks to not extract: " << ErrorInfo |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 344 | << "\n"; |
Nick Lewycky | 6fa98b1 | 2007-11-14 06:47:06 +0000 | [diff] [blame] | 345 | M = swapProgramIn(M); |
| 346 | EmitProgressBitcode("basicblockextractfail", true); |
| 347 | swapProgramIn(M); |
| 348 | return 0; |
| 349 | } |
| 350 | for (std::vector<BasicBlock*>::const_iterator I = BBs.begin(), E = BBs.end(); |
| 351 | I != E; ++I) { |
| 352 | BasicBlock *BB = *I; |
Chris Lattner | 4a6a6f2 | 2008-01-08 04:26:20 +0000 | [diff] [blame] | 353 | // If the BB doesn't have a name, give it one so we have something to key |
| 354 | // off of. |
| 355 | if (!BB->hasName()) BB->setName("tmpbb"); |
Daniel Dunbar | f6ccee5 | 2009-07-24 08:24:36 +0000 | [diff] [blame] | 356 | BlocksToNotExtractFile << BB->getParent()->getNameStr() << " " |
Nick Lewycky | 6fa98b1 | 2007-11-14 06:47:06 +0000 | [diff] [blame] | 357 | << BB->getName() << "\n"; |
| 358 | } |
| 359 | BlocksToNotExtractFile.close(); |
| 360 | |
| 361 | const char *uniqueFN = uniqueFilename.c_str(); |
| 362 | ExtraArg = (char*)malloc(23 + strlen(uniqueFN)); |
| 363 | strcat(strcpy(ExtraArg, "--extract-blocks-file="), uniqueFN); |
| 364 | |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 365 | std::vector<const PassInfo*> PI; |
Nick Lewycky | 6fa98b1 | 2007-11-14 06:47:06 +0000 | [diff] [blame] | 366 | std::vector<BasicBlock *> EmptyBBs; // This parameter is ignored. |
| 367 | PI.push_back(getPI(createBlockExtractorPass(EmptyBBs))); |
| 368 | Module *Ret = runPassesOn(M, PI, false, 1, &ExtraArg); |
| 369 | |
| 370 | if (uniqueFilename.exists()) |
| 371 | uniqueFilename.eraseFromDisk(); // Free disk space |
| 372 | free(ExtraArg); |
| 373 | |
Chris Lattner | 891150f | 2004-08-12 02:36:50 +0000 | [diff] [blame] | 374 | if (Ret == 0) { |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 375 | outs() << "*** Basic Block extraction failed, please report a bug!\n"; |
Chris Lattner | 891150f | 2004-08-12 02:36:50 +0000 | [diff] [blame] | 376 | M = swapProgramIn(M); |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 377 | EmitProgressBitcode("basicblockextractfail", true); |
Chris Lattner | b923b2e | 2006-05-12 17:28:36 +0000 | [diff] [blame] | 378 | swapProgramIn(M); |
Chris Lattner | 891150f | 2004-08-12 02:36:50 +0000 | [diff] [blame] | 379 | } |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 380 | return Ret; |
| 381 | } |