Chris Lattner | 73a6bdd | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 1 | //===- ExtractFunction.cpp - Extract a function from Program --------------===// |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 09344dc | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 345353d | 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 | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 09344dc | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 73a6bdd | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | fd72bed | 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 | 73a6bdd | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "BugDriver.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/Verifier.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 17 | #include "llvm/IR/Constants.h" |
| 18 | #include "llvm/IR/DataLayout.h" |
| 19 | #include "llvm/IR/DerivedTypes.h" |
| 20 | #include "llvm/IR/LLVMContext.h" |
| 21 | #include "llvm/IR/Module.h" |
Brian Gaeke | ef43270 | 2003-09-10 21:11:42 +0000 | [diff] [blame] | 22 | #include "llvm/Pass.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 23 | #include "llvm/PassManager.h" |
| 24 | #include "llvm/Support/CommandLine.h" |
| 25 | #include "llvm/Support/Debug.h" |
| 26 | #include "llvm/Support/FileUtilities.h" |
| 27 | #include "llvm/Support/Path.h" |
| 28 | #include "llvm/Support/Signals.h" |
| 29 | #include "llvm/Support/ToolOutputFile.h" |
Chris Lattner | 73a6bdd | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 30 | #include "llvm/Transforms/IPO.h" |
Chris Lattner | 0eb5ce9 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 31 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 73a6bdd | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 32 | #include "llvm/Transforms/Utils/Cloning.h" |
Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 33 | #include "llvm/Transforms/Utils/CodeExtractor.h" |
Chris Lattner | 3838243 | 2004-04-02 16:28:32 +0000 | [diff] [blame] | 34 | #include <set> |
Chris Lattner | 3f1ad87 | 2003-11-23 04:51:05 +0000 | [diff] [blame] | 35 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 36 | |
| 37 | namespace llvm { |
Chris Lattner | 3f1ad87 | 2003-11-23 04:51:05 +0000 | [diff] [blame] | 38 | bool DisableSimplifyCFG = false; |
Daniel Dunbar | a53337f | 2009-09-07 19:26:11 +0000 | [diff] [blame] | 39 | extern cl::opt<std::string> OutputPrefix; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 40 | } // End llvm namespace |
| 41 | |
Chris Lattner | 8d8de42 | 2003-04-25 22:08:12 +0000 | [diff] [blame] | 42 | namespace { |
| 43 | cl::opt<bool> |
Chris Lattner | 8d8de42 | 2003-04-25 22:08:12 +0000 | [diff] [blame] | 44 | NoDCE ("disable-dce", |
| 45 | cl::desc("Do not use the -dce pass to reduce testcases")); |
Chris Lattner | d1e2aae | 2003-08-05 15:51:05 +0000 | [diff] [blame] | 46 | cl::opt<bool, true> |
| 47 | NoSCFG("disable-simplifycfg", cl::location(DisableSimplifyCFG), |
Chris Lattner | 8d8de42 | 2003-04-25 22:08:12 +0000 | [diff] [blame] | 48 | cl::desc("Do not use the -simplifycfg pass to reduce testcases")); |
Eli Friedman | 9f18ea8 | 2012-02-22 01:43:47 +0000 | [diff] [blame] | 49 | |
| 50 | Function* globalInitUsesExternalBA(GlobalVariable* GV) { |
| 51 | if (!GV->hasInitializer()) |
| 52 | return 0; |
| 53 | |
| 54 | Constant *I = GV->getInitializer(); |
| 55 | |
| 56 | // walk the values used by the initializer |
| 57 | // (and recurse into things like ConstantExpr) |
| 58 | std::vector<Constant*> Todo; |
| 59 | std::set<Constant*> Done; |
| 60 | Todo.push_back(I); |
| 61 | |
| 62 | while (!Todo.empty()) { |
| 63 | Constant* V = Todo.back(); |
| 64 | Todo.pop_back(); |
| 65 | Done.insert(V); |
| 66 | |
| 67 | if (BlockAddress *BA = dyn_cast<BlockAddress>(V)) { |
| 68 | Function *F = BA->getFunction(); |
| 69 | if (F->isDeclaration()) |
| 70 | return F; |
| 71 | } |
| 72 | |
| 73 | for (User::op_iterator i = V->op_begin(), e = V->op_end(); i != e; ++i) { |
| 74 | Constant *C = dyn_cast<Constant>(*i); |
| 75 | if (C && !isa<GlobalValue>(C) && !Done.count(C)) |
| 76 | Todo.push_back(C); |
| 77 | } |
| 78 | } |
| 79 | return 0; |
| 80 | } |
| 81 | } // end anonymous namespace |
Chris Lattner | 73a6bdd | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 82 | |
Chris Lattner | 0eb5ce9 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 83 | /// deleteInstructionFromProgram - This method clones the current Program and |
| 84 | /// deletes the specified instruction from the cloned module. It then runs a |
| 85 | /// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code which |
| 86 | /// depends on the value. The modified module is then returned. |
| 87 | /// |
Chris Lattner | b943b6b | 2004-02-18 21:50:26 +0000 | [diff] [blame] | 88 | Module *BugDriver::deleteInstructionFromProgram(const Instruction *I, |
Rafael Espindola | d1e241a | 2010-08-10 15:46:11 +0000 | [diff] [blame] | 89 | unsigned Simplification) { |
| 90 | // FIXME, use vmap? |
| 91 | Module *Clone = CloneModule(Program); |
Chris Lattner | 0eb5ce9 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 92 | |
Chris Lattner | b943b6b | 2004-02-18 21:50:26 +0000 | [diff] [blame] | 93 | const BasicBlock *PBB = I->getParent(); |
| 94 | const Function *PF = PBB->getParent(); |
Chris Lattner | 0eb5ce9 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 95 | |
Rafael Espindola | d1e241a | 2010-08-10 15:46:11 +0000 | [diff] [blame] | 96 | Module::iterator RFI = Clone->begin(); // Get iterator to corresponding fn |
Chris Lattner | b943b6b | 2004-02-18 21:50:26 +0000 | [diff] [blame] | 97 | std::advance(RFI, std::distance(PF->getParent()->begin(), |
| 98 | Module::const_iterator(PF))); |
Chris Lattner | 0eb5ce9 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 99 | |
| 100 | Function::iterator RBI = RFI->begin(); // Get iterator to corresponding BB |
Chris Lattner | b943b6b | 2004-02-18 21:50:26 +0000 | [diff] [blame] | 101 | std::advance(RBI, std::distance(PF->begin(), Function::const_iterator(PBB))); |
Chris Lattner | 0eb5ce9 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 102 | |
| 103 | BasicBlock::iterator RI = RBI->begin(); // Get iterator to corresponding inst |
Chris Lattner | b943b6b | 2004-02-18 21:50:26 +0000 | [diff] [blame] | 104 | std::advance(RI, std::distance(PBB->begin(), BasicBlock::const_iterator(I))); |
| 105 | Instruction *TheInst = RI; // Got the corresponding instruction! |
Chris Lattner | 0eb5ce9 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 106 | |
| 107 | // If this instruction produces a value, replace any users with null values |
Dan Gohman | 34a2249 | 2010-06-07 20:19:26 +0000 | [diff] [blame] | 108 | if (!TheInst->getType()->isVoidTy()) |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 109 | TheInst->replaceAllUsesWith(Constant::getNullValue(TheInst->getType())); |
Chris Lattner | 0eb5ce9 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 110 | |
| 111 | // Remove the instruction from the program. |
Chris Lattner | b943b6b | 2004-02-18 21:50:26 +0000 | [diff] [blame] | 112 | TheInst->getParent()->getInstList().erase(TheInst); |
Chris Lattner | 0eb5ce9 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 113 | |
Chris Lattner | 10c0469 | 2003-04-24 22:53:24 +0000 | [diff] [blame] | 114 | // Spiff up the output a little bit. |
Rafael Espindola | d1e241a | 2010-08-10 15:46:11 +0000 | [diff] [blame] | 115 | std::vector<std::string> Passes; |
Chris Lattner | f3e8f97 | 2003-10-23 15:42:55 +0000 | [diff] [blame] | 116 | |
Rafael Espindola | d1e241a | 2010-08-10 15:46:11 +0000 | [diff] [blame] | 117 | /// Can we get rid of the -disable-* options? |
Chris Lattner | 8d8de42 | 2003-04-25 22:08:12 +0000 | [diff] [blame] | 118 | if (Simplification > 1 && !NoDCE) |
Rafael Espindola | d1e241a | 2010-08-10 15:46:11 +0000 | [diff] [blame] | 119 | Passes.push_back("dce"); |
Chris Lattner | d1e2aae | 2003-08-05 15:51:05 +0000 | [diff] [blame] | 120 | if (Simplification && !DisableSimplifyCFG) |
Rafael Espindola | d1e241a | 2010-08-10 15:46:11 +0000 | [diff] [blame] | 121 | Passes.push_back("simplifycfg"); // Delete dead control flow |
Chris Lattner | 44ffd7c | 2003-03-07 18:17:13 +0000 | [diff] [blame] | 122 | |
Rafael Espindola | d1e241a | 2010-08-10 15:46:11 +0000 | [diff] [blame] | 123 | Passes.push_back("verify"); |
| 124 | Module *New = runPassesOn(Clone, Passes); |
| 125 | delete Clone; |
| 126 | if (!New) { |
| 127 | errs() << "Instruction removal failed. Sorry. :( Please report a bug!\n"; |
| 128 | exit(1); |
| 129 | } |
| 130 | return New; |
Chris Lattner | 0eb5ce9 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 131 | } |
Chris Lattner | 514c02e | 2003-02-28 16:13:20 +0000 | [diff] [blame] | 132 | |
| 133 | /// performFinalCleanups - This method clones the current Program and performs |
| 134 | /// a series of cleanups intended to get rid of extra cruft on the module |
Chris Lattner | 5e166a5 | 2005-02-23 06:12:11 +0000 | [diff] [blame] | 135 | /// before handing it to the user. |
Chris Lattner | 514c02e | 2003-02-28 16:13:20 +0000 | [diff] [blame] | 136 | /// |
Chris Lattner | 2920100 | 2003-11-05 21:45:35 +0000 | [diff] [blame] | 137 | Module *BugDriver::performFinalCleanups(Module *M, bool MayModifySemantics) { |
Chris Lattner | 2b3ab56 | 2003-05-21 19:41:31 +0000 | [diff] [blame] | 138 | // Make all functions external, so GlobalDCE doesn't delete them... |
| 139 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 140 | I->setLinkage(GlobalValue::ExternalLinkage); |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 141 | |
Rafael Espindola | 33e81a8 | 2010-08-08 03:55:08 +0000 | [diff] [blame] | 142 | std::vector<std::string> CleanupPasses; |
| 143 | CleanupPasses.push_back("globaldce"); |
Chris Lattner | 2920100 | 2003-11-05 21:45:35 +0000 | [diff] [blame] | 144 | |
Chris Lattner | 3f1ad87 | 2003-11-23 04:51:05 +0000 | [diff] [blame] | 145 | if (MayModifySemantics) |
Rafael Espindola | 33e81a8 | 2010-08-08 03:55:08 +0000 | [diff] [blame] | 146 | CleanupPasses.push_back("deadarghaX0r"); |
Chris Lattner | 3f1ad87 | 2003-11-23 04:51:05 +0000 | [diff] [blame] | 147 | else |
Rafael Espindola | 33e81a8 | 2010-08-08 03:55:08 +0000 | [diff] [blame] | 148 | CleanupPasses.push_back("deadargelim"); |
Chris Lattner | 2920100 | 2003-11-05 21:45:35 +0000 | [diff] [blame] | 149 | |
Chris Lattner | 6ce2d03 | 2004-03-14 21:17:22 +0000 | [diff] [blame] | 150 | Module *New = runPassesOn(M, CleanupPasses); |
| 151 | if (New == 0) { |
Dan Gohman | d8db376 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 152 | errs() << "Final cleanups failed. Sorry. :( Please report a bug!\n"; |
Chris Lattner | 5e166a5 | 2005-02-23 06:12:11 +0000 | [diff] [blame] | 153 | return M; |
Chris Lattner | 2920100 | 2003-11-05 21:45:35 +0000 | [diff] [blame] | 154 | } |
Chris Lattner | 6ce2d03 | 2004-03-14 21:17:22 +0000 | [diff] [blame] | 155 | delete M; |
| 156 | return New; |
Chris Lattner | 514c02e | 2003-02-28 16:13:20 +0000 | [diff] [blame] | 157 | } |
Chris Lattner | 567543f | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 158 | |
| 159 | |
Chris Lattner | 3fe96bc | 2004-03-14 20:02:07 +0000 | [diff] [blame] | 160 | /// ExtractLoop - Given a module, extract up to one loop from it into a new |
| 161 | /// function. This returns null if there are no extractable loops in the |
| 162 | /// program or if the loop extractor crashes. |
| 163 | Module *BugDriver::ExtractLoop(Module *M) { |
Rafael Espindola | 33e81a8 | 2010-08-08 03:55:08 +0000 | [diff] [blame] | 164 | std::vector<std::string> LoopExtractPasses; |
| 165 | LoopExtractPasses.push_back("loop-extract-single"); |
Chris Lattner | 3fe96bc | 2004-03-14 20:02:07 +0000 | [diff] [blame] | 166 | |
Chris Lattner | 6ce2d03 | 2004-03-14 21:17:22 +0000 | [diff] [blame] | 167 | Module *NewM = runPassesOn(M, LoopExtractPasses); |
| 168 | if (NewM == 0) { |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 169 | outs() << "*** Loop extraction failed: "; |
Rafael Espindola | 594994a | 2010-07-28 18:12:30 +0000 | [diff] [blame] | 170 | EmitProgressBitcode(M, "loopextraction", true); |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 171 | outs() << "*** Sorry. :( Please report a bug!\n"; |
Chris Lattner | 3fe96bc | 2004-03-14 20:02:07 +0000 | [diff] [blame] | 172 | return 0; |
Chris Lattner | 3fe96bc | 2004-03-14 20:02:07 +0000 | [diff] [blame] | 173 | } |
Chris Lattner | 6ce2d03 | 2004-03-14 21:17:22 +0000 | [diff] [blame] | 174 | |
| 175 | // Check to see if we created any new functions. If not, no loops were |
Chris Lattner | 8cb483b | 2004-11-18 19:40:13 +0000 | [diff] [blame] | 176 | // extracted and we should return null. Limit the number of loops we extract |
| 177 | // to avoid taking forever. |
| 178 | static unsigned NumExtracted = 32; |
Chris Lattner | d308797 | 2004-11-16 06:31:38 +0000 | [diff] [blame] | 179 | if (M->size() == NewM->size() || --NumExtracted == 0) { |
Chris Lattner | 6ce2d03 | 2004-03-14 21:17:22 +0000 | [diff] [blame] | 180 | delete NewM; |
| 181 | return 0; |
Chris Lattner | d308797 | 2004-11-16 06:31:38 +0000 | [diff] [blame] | 182 | } else { |
| 183 | assert(M->size() < NewM->size() && "Loop extract removed functions?"); |
| 184 | Module::iterator MI = NewM->begin(); |
| 185 | for (unsigned i = 0, e = M->size(); i != e; ++i) |
| 186 | ++MI; |
Chris Lattner | 6ce2d03 | 2004-03-14 21:17:22 +0000 | [diff] [blame] | 187 | } |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 188 | |
Chris Lattner | 6ce2d03 | 2004-03-14 21:17:22 +0000 | [diff] [blame] | 189 | return NewM; |
Chris Lattner | 3fe96bc | 2004-03-14 20:02:07 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | |
Chris Lattner | 567543f | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 193 | // DeleteFunctionBody - "Remove" the function by deleting all of its basic |
| 194 | // blocks, making it external. |
| 195 | // |
| 196 | void llvm::DeleteFunctionBody(Function *F) { |
| 197 | // delete the body of the function... |
| 198 | F->deleteBody(); |
Reid Spencer | 5301e7c | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 199 | assert(F->isDeclaration() && "This didn't make the function external!"); |
Chris Lattner | 567543f | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Chris Lattner | fdb533a | 2006-03-08 23:55:38 +0000 | [diff] [blame] | 202 | /// GetTorInit - Given a list of entries for static ctors/dtors, return them |
| 203 | /// as a constant array. |
| 204 | static Constant *GetTorInit(std::vector<std::pair<Function*, int> > &TorList) { |
| 205 | assert(!TorList.empty() && "Don't create empty tor list!"); |
| 206 | std::vector<Constant*> ArrayElts; |
Jay Foad | b804a2b | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 207 | Type *Int32Ty = Type::getInt32Ty(TorList[0].first->getContext()); |
Chris Lattner | cc19efa | 2011-06-20 04:01:31 +0000 | [diff] [blame] | 208 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 209 | StructType *STy = |
Chris Lattner | cc19efa | 2011-06-20 04:01:31 +0000 | [diff] [blame] | 210 | StructType::get(Int32Ty, TorList[0].first->getType(), NULL); |
Chris Lattner | fdb533a | 2006-03-08 23:55:38 +0000 | [diff] [blame] | 211 | for (unsigned i = 0, e = TorList.size(); i != e; ++i) { |
Chris Lattner | cc19efa | 2011-06-20 04:01:31 +0000 | [diff] [blame] | 212 | Constant *Elts[] = { |
| 213 | ConstantInt::get(Int32Ty, TorList[i].second), |
| 214 | TorList[i].first |
| 215 | }; |
| 216 | ArrayElts.push_back(ConstantStruct::get(STy, Elts)); |
Chris Lattner | fdb533a | 2006-03-08 23:55:38 +0000 | [diff] [blame] | 217 | } |
Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 218 | return ConstantArray::get(ArrayType::get(ArrayElts[0]->getType(), |
Chris Lattner | fdb533a | 2006-03-08 23:55:38 +0000 | [diff] [blame] | 219 | ArrayElts.size()), |
| 220 | ArrayElts); |
| 221 | } |
| 222 | |
| 223 | /// SplitStaticCtorDtor - A module was recently split into two parts, M1/M2, and |
| 224 | /// M1 has all of the global variables. If M2 contains any functions that are |
| 225 | /// static ctors/dtors, we need to add an llvm.global_[cd]tors global to M2, and |
| 226 | /// prune appropriate entries out of M1s list. |
Dan Gohman | 956ae9d | 2009-04-22 15:57:18 +0000 | [diff] [blame] | 227 | static void SplitStaticCtorDtor(const char *GlobalName, Module *M1, Module *M2, |
Rafael Espindola | 229e38f | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 228 | ValueToValueMapTy &VMap) { |
Chris Lattner | fdb533a | 2006-03-08 23:55:38 +0000 | [diff] [blame] | 229 | GlobalVariable *GV = M1->getNamedGlobal(GlobalName); |
Rafael Espindola | 6de96a1 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 230 | if (!GV || GV->isDeclaration() || GV->hasLocalLinkage() || |
Chris Lattner | fdb533a | 2006-03-08 23:55:38 +0000 | [diff] [blame] | 231 | !GV->use_empty()) return; |
| 232 | |
| 233 | std::vector<std::pair<Function*, int> > M1Tors, M2Tors; |
| 234 | ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer()); |
| 235 | if (!InitList) return; |
| 236 | |
| 237 | for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) { |
| 238 | if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){ |
| 239 | if (CS->getNumOperands() != 2) return; // Not array of 2-element structs. |
| 240 | |
| 241 | if (CS->getOperand(1)->isNullValue()) |
| 242 | break; // Found a null terminator, stop here. |
| 243 | |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 244 | ConstantInt *CI = dyn_cast<ConstantInt>(CS->getOperand(0)); |
| 245 | int Priority = CI ? CI->getSExtValue() : 0; |
Chris Lattner | fdb533a | 2006-03-08 23:55:38 +0000 | [diff] [blame] | 246 | |
| 247 | Constant *FP = CS->getOperand(1); |
| 248 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP)) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 249 | if (CE->isCast()) |
Chris Lattner | fdb533a | 2006-03-08 23:55:38 +0000 | [diff] [blame] | 250 | FP = CE->getOperand(0); |
| 251 | if (Function *F = dyn_cast<Function>(FP)) { |
Reid Spencer | 5301e7c | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 252 | if (!F->isDeclaration()) |
Chris Lattner | fdb533a | 2006-03-08 23:55:38 +0000 | [diff] [blame] | 253 | M1Tors.push_back(std::make_pair(F, Priority)); |
| 254 | else { |
| 255 | // Map to M2's version of the function. |
Devang Patel | 0dc3c2d | 2010-06-24 00:33:28 +0000 | [diff] [blame] | 256 | F = cast<Function>(VMap[F]); |
Chris Lattner | fdb533a | 2006-03-08 23:55:38 +0000 | [diff] [blame] | 257 | M2Tors.push_back(std::make_pair(F, Priority)); |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | GV->eraseFromParent(); |
| 264 | if (!M1Tors.empty()) { |
| 265 | Constant *M1Init = GetTorInit(M1Tors); |
Owen Anderson | b17f329 | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 266 | new GlobalVariable(*M1, M1Init->getType(), false, |
Owen Anderson | 5948fdf | 2009-07-08 01:26:06 +0000 | [diff] [blame] | 267 | GlobalValue::AppendingLinkage, |
Owen Anderson | b17f329 | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 268 | M1Init, GlobalName); |
Chris Lattner | fdb533a | 2006-03-08 23:55:38 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | GV = M2->getNamedGlobal(GlobalName); |
| 272 | assert(GV && "Not a clone of M1?"); |
| 273 | assert(GV->use_empty() && "llvm.ctors shouldn't have uses!"); |
| 274 | |
| 275 | GV->eraseFromParent(); |
| 276 | if (!M2Tors.empty()) { |
| 277 | Constant *M2Init = GetTorInit(M2Tors); |
Owen Anderson | b17f329 | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 278 | new GlobalVariable(*M2, M2Init->getType(), false, |
Owen Anderson | 5948fdf | 2009-07-08 01:26:06 +0000 | [diff] [blame] | 279 | GlobalValue::AppendingLinkage, |
Owen Anderson | b17f329 | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 280 | M2Init, GlobalName); |
Chris Lattner | fdb533a | 2006-03-08 23:55:38 +0000 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | |
Patrick Jenkins | b417a5c | 2006-07-28 01:19:28 +0000 | [diff] [blame] | 284 | |
Chris Lattner | 567543f | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 285 | /// SplitFunctionsOutOfModule - Given a module and a list of functions in the |
| 286 | /// module, split the functions OUT of the specified module, and place them in |
| 287 | /// the new module. |
Dan Gohman | 956ae9d | 2009-04-22 15:57:18 +0000 | [diff] [blame] | 288 | Module * |
| 289 | llvm::SplitFunctionsOutOfModule(Module *M, |
| 290 | const std::vector<Function*> &F, |
Rafael Espindola | 229e38f | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 291 | ValueToValueMapTy &VMap) { |
Chris Lattner | 567543f | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 292 | // Make sure functions & globals are all external so that linkage |
| 293 | // between the two modules will work. |
| 294 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 295 | I->setLinkage(GlobalValue::ExternalLinkage); |
Chris Lattner | fdb533a | 2006-03-08 23:55:38 +0000 | [diff] [blame] | 296 | for (Module::global_iterator I = M->global_begin(), E = M->global_end(); |
Owen Anderson | c76bacb | 2008-07-08 16:38:42 +0000 | [diff] [blame] | 297 | I != E; ++I) { |
Daniel Dunbar | d786b51 | 2009-07-26 00:34:27 +0000 | [diff] [blame] | 298 | if (I->hasName() && I->getName()[0] == '\01') |
| 299 | I->setName(I->getName().substr(1)); |
Chris Lattner | 567543f | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 300 | I->setLinkage(GlobalValue::ExternalLinkage); |
Owen Anderson | c76bacb | 2008-07-08 16:38:42 +0000 | [diff] [blame] | 301 | } |
Chris Lattner | 567543f | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 302 | |
Rafael Espindola | 229e38f | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 303 | ValueToValueMapTy NewVMap; |
Devang Patel | 0dc3c2d | 2010-06-24 00:33:28 +0000 | [diff] [blame] | 304 | Module *New = CloneModule(M, NewVMap); |
Chris Lattner | 567543f | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 305 | |
Chris Lattner | 7275b02 | 2006-11-09 06:24:56 +0000 | [diff] [blame] | 306 | // Remove the Test functions from the Safe module |
Dan Gohman | 956ae9d | 2009-04-22 15:57:18 +0000 | [diff] [blame] | 307 | std::set<Function *> TestFunctions; |
Chris Lattner | 567543f | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 308 | for (unsigned i = 0, e = F.size(); i != e; ++i) { |
Devang Patel | 0dc3c2d | 2010-06-24 00:33:28 +0000 | [diff] [blame] | 309 | Function *TNOF = cast<Function>(VMap[F[i]]); |
Dan Gohman | d8db376 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 310 | DEBUG(errs() << "Removing function "); |
Chandler Carruth | d48cdbf | 2014-01-09 02:29:41 +0000 | [diff] [blame] | 311 | DEBUG(TNOF->printAsOperand(errs(), false)); |
Dan Gohman | d8db376 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 312 | DEBUG(errs() << "\n"); |
Devang Patel | 0dc3c2d | 2010-06-24 00:33:28 +0000 | [diff] [blame] | 313 | TestFunctions.insert(cast<Function>(NewVMap[TNOF])); |
Chris Lattner | 7275b02 | 2006-11-09 06:24:56 +0000 | [diff] [blame] | 314 | DeleteFunctionBody(TNOF); // Function is now external in this module! |
Chris Lattner | 567543f | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Chris Lattner | fdb533a | 2006-03-08 23:55:38 +0000 | [diff] [blame] | 317 | |
Chris Lattner | 7275b02 | 2006-11-09 06:24:56 +0000 | [diff] [blame] | 318 | // Remove the Safe functions from the Test module |
| 319 | for (Module::iterator I = New->begin(), E = New->end(); I != E; ++I) |
Dan Gohman | 956ae9d | 2009-04-22 15:57:18 +0000 | [diff] [blame] | 320 | if (!TestFunctions.count(I)) |
Chris Lattner | 7275b02 | 2006-11-09 06:24:56 +0000 | [diff] [blame] | 321 | DeleteFunctionBody(I); |
| 322 | |
Patrick Jenkins | b417a5c | 2006-07-28 01:19:28 +0000 | [diff] [blame] | 323 | |
Eli Friedman | 9f18ea8 | 2012-02-22 01:43:47 +0000 | [diff] [blame] | 324 | // Try to split the global initializers evenly |
| 325 | for (Module::global_iterator I = M->global_begin(), E = M->global_end(); |
| 326 | I != E; ++I) { |
| 327 | GlobalVariable *GV = cast<GlobalVariable>(NewVMap[I]); |
| 328 | if (Function *TestFn = globalInitUsesExternalBA(I)) { |
| 329 | if (Function *SafeFn = globalInitUsesExternalBA(GV)) { |
| 330 | errs() << "*** Error: when reducing functions, encountered " |
| 331 | "the global '"; |
Chandler Carruth | d48cdbf | 2014-01-09 02:29:41 +0000 | [diff] [blame] | 332 | GV->printAsOperand(errs(), false); |
Eli Friedman | 9f18ea8 | 2012-02-22 01:43:47 +0000 | [diff] [blame] | 333 | errs() << "' with an initializer that references blockaddresses " |
| 334 | "from safe function '" << SafeFn->getName() |
| 335 | << "' and from test function '" << TestFn->getName() << "'.\n"; |
| 336 | exit(1); |
| 337 | } |
| 338 | I->setInitializer(0); // Delete the initializer to make it external |
| 339 | } else { |
| 340 | // If we keep it in the safe module, then delete it in the test module |
| 341 | GV->setInitializer(0); |
| 342 | } |
| 343 | } |
| 344 | |
Chris Lattner | fdb533a | 2006-03-08 23:55:38 +0000 | [diff] [blame] | 345 | // Make sure that there is a global ctor/dtor array in both halves of the |
| 346 | // module if they both have static ctor/dtor functions. |
Devang Patel | 0dc3c2d | 2010-06-24 00:33:28 +0000 | [diff] [blame] | 347 | SplitStaticCtorDtor("llvm.global_ctors", M, New, NewVMap); |
| 348 | SplitStaticCtorDtor("llvm.global_dtors", M, New, NewVMap); |
Chris Lattner | fdb533a | 2006-03-08 23:55:38 +0000 | [diff] [blame] | 349 | |
Chris Lattner | 567543f | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 350 | return New; |
| 351 | } |
Chris Lattner | a060b10 | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 352 | |
| 353 | //===----------------------------------------------------------------------===// |
| 354 | // Basic Block Extraction Code |
| 355 | //===----------------------------------------------------------------------===// |
| 356 | |
Chris Lattner | a060b10 | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 357 | /// ExtractMappedBlocksFromModule - Extract all but the specified basic blocks |
| 358 | /// into their own functions. The only detail is that M is actually a module |
| 359 | /// cloned from the one the BBs are in, so some mapping needs to be performed. |
| 360 | /// If this operation fails for some reason (ie the implementation is buggy), |
| 361 | /// this function should return null, otherwise it returns a new Module. |
| 362 | Module *BugDriver::ExtractMappedBlocksFromModule(const |
| 363 | std::vector<BasicBlock*> &BBs, |
| 364 | Module *M) { |
Rafael Espindola | 7e936ee | 2013-06-17 18:48:59 +0000 | [diff] [blame] | 365 | SmallString<128> Filename; |
| 366 | int FD; |
Rafael Espindola | c9d2e5b | 2013-07-05 21:01:08 +0000 | [diff] [blame] | 367 | error_code EC = sys::fs::createUniqueFile( |
| 368 | OutputPrefix + "-extractblocks%%%%%%%", FD, Filename); |
Rafael Espindola | 7e936ee | 2013-06-17 18:48:59 +0000 | [diff] [blame] | 369 | if (EC) { |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 370 | outs() << "*** Basic Block extraction failed!\n"; |
Rafael Espindola | 7e936ee | 2013-06-17 18:48:59 +0000 | [diff] [blame] | 371 | errs() << "Error creating temporary file: " << EC.message() << "\n"; |
Rafael Espindola | 594994a | 2010-07-28 18:12:30 +0000 | [diff] [blame] | 372 | EmitProgressBitcode(M, "basicblockextractfail", true); |
Nick Lewycky | c624302 | 2007-11-14 06:47:06 +0000 | [diff] [blame] | 373 | return 0; |
| 374 | } |
Rafael Espindola | 7e936ee | 2013-06-17 18:48:59 +0000 | [diff] [blame] | 375 | sys::RemoveFileOnSignal(Filename); |
Nick Lewycky | c624302 | 2007-11-14 06:47:06 +0000 | [diff] [blame] | 376 | |
Rafael Espindola | 7e936ee | 2013-06-17 18:48:59 +0000 | [diff] [blame] | 377 | tool_output_file BlocksToNotExtractFile(Filename.c_str(), FD); |
Nick Lewycky | c624302 | 2007-11-14 06:47:06 +0000 | [diff] [blame] | 378 | for (std::vector<BasicBlock*>::const_iterator I = BBs.begin(), E = BBs.end(); |
| 379 | I != E; ++I) { |
| 380 | BasicBlock *BB = *I; |
Chris Lattner | 9787942 | 2008-01-08 04:26:20 +0000 | [diff] [blame] | 381 | // If the BB doesn't have a name, give it one so we have something to key |
| 382 | // off of. |
| 383 | if (!BB->hasName()) BB->setName("tmpbb"); |
Benjamin Kramer | 1f97a5a | 2011-11-15 16:27:03 +0000 | [diff] [blame] | 384 | BlocksToNotExtractFile.os() << BB->getParent()->getName() << " " |
Dan Gohman | a2233f2 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 385 | << BB->getName() << "\n"; |
Nick Lewycky | c624302 | 2007-11-14 06:47:06 +0000 | [diff] [blame] | 386 | } |
Dan Gohman | a2233f2 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 387 | BlocksToNotExtractFile.os().close(); |
| 388 | if (BlocksToNotExtractFile.os().has_error()) { |
Rafael Espindola | 7e936ee | 2013-06-17 18:48:59 +0000 | [diff] [blame] | 389 | errs() << "Error writing list of blocks to not extract\n"; |
Dan Gohman | 8525fe7 | 2010-08-20 16:59:15 +0000 | [diff] [blame] | 390 | EmitProgressBitcode(M, "basicblockextractfail", true); |
Dan Gohman | a2233f2 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 391 | BlocksToNotExtractFile.os().clear_error(); |
Dan Gohman | 8525fe7 | 2010-08-20 16:59:15 +0000 | [diff] [blame] | 392 | return 0; |
| 393 | } |
| 394 | BlocksToNotExtractFile.keep(); |
Nick Lewycky | c624302 | 2007-11-14 06:47:06 +0000 | [diff] [blame] | 395 | |
Rafael Espindola | 7e936ee | 2013-06-17 18:48:59 +0000 | [diff] [blame] | 396 | std::string uniqueFN = "--extract-blocks-file="; |
| 397 | uniqueFN += Filename.str(); |
Benjamin Kramer | 29063ea | 2010-01-28 18:04:38 +0000 | [diff] [blame] | 398 | const char *ExtraArg = uniqueFN.c_str(); |
Nick Lewycky | c624302 | 2007-11-14 06:47:06 +0000 | [diff] [blame] | 399 | |
Rafael Espindola | 33e81a8 | 2010-08-08 03:55:08 +0000 | [diff] [blame] | 400 | std::vector<std::string> PI; |
| 401 | PI.push_back("extract-blocks"); |
Nick Lewycky | c624302 | 2007-11-14 06:47:06 +0000 | [diff] [blame] | 402 | Module *Ret = runPassesOn(M, PI, false, 1, &ExtraArg); |
| 403 | |
Rafael Espindola | 7e936ee | 2013-06-17 18:48:59 +0000 | [diff] [blame] | 404 | sys::fs::remove(Filename.c_str()); |
Nick Lewycky | c624302 | 2007-11-14 06:47:06 +0000 | [diff] [blame] | 405 | |
Chris Lattner | af32dfa | 2004-08-12 02:36:50 +0000 | [diff] [blame] | 406 | if (Ret == 0) { |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 407 | outs() << "*** Basic Block extraction failed, please report a bug!\n"; |
Rafael Espindola | 594994a | 2010-07-28 18:12:30 +0000 | [diff] [blame] | 408 | EmitProgressBitcode(M, "basicblockextractfail", true); |
Chris Lattner | af32dfa | 2004-08-12 02:36:50 +0000 | [diff] [blame] | 409 | } |
Chris Lattner | a060b10 | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 410 | return Ret; |
| 411 | } |