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