Misha Brukman | de03bc0 | 2005-04-24 17:36:05 +0000 | [diff] [blame] | 1 | //===- llvm-extract.cpp - LLVM function extraction utility ----------------===// |
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 | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 9 | // |
| 10 | // This utility changes the input module to only contain a single function, |
| 11 | // which is primarily used for debugging transformations. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/SetVector.h" |
| 16 | #include "llvm/ADT/SmallPtrSet.h" |
Chandler Carruth | e2dc71d | 2014-01-13 07:38:24 +0000 | [diff] [blame] | 17 | #include "llvm/Bitcode/BitcodeWriterPass.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 18 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | 8a5351f | 2014-01-12 11:10:32 +0000 | [diff] [blame] | 19 | #include "llvm/IR/IRPrintingPasses.h" |
Keno Fischer | 1f6fa0f | 2017-04-06 20:51:40 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Instructions.h" |
Chandler Carruth | 8a5351f | 2014-01-12 11:10:32 +0000 | [diff] [blame] | 21 | #include "llvm/IR/LLVMContext.h" |
Keno Fischer | 1f6fa0f | 2017-04-06 20:51:40 +0000 | [diff] [blame] | 22 | #include "llvm/IR/LegacyPassManager.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Module.h" |
Chandler Carruth | 7fc162f | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 24 | #include "llvm/IRReader/IRReader.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 25 | #include "llvm/Support/CommandLine.h" |
Peter Collingbourne | 76c218e | 2016-11-09 17:49:19 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Error.h" |
Benjamin Kramer | 7259f14 | 2014-04-29 23:26:49 +0000 | [diff] [blame] | 27 | #include "llvm/Support/FileSystem.h" |
Rui Ueyama | 0b9d56a | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 28 | #include "llvm/Support/InitLLVM.h" |
Chad Rosier | 4e0a55d | 2011-09-16 21:09:17 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Regex.h" |
Chandler Carruth | 7fc162f | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 30 | #include "llvm/Support/SourceMgr.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 31 | #include "llvm/Support/SystemUtils.h" |
| 32 | #include "llvm/Support/ToolOutputFile.h" |
| 33 | #include "llvm/Transforms/IPO.h" |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 34 | #include <memory> |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 35 | using namespace llvm; |
| 36 | |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 37 | // InputFilename - The filename to read from. |
Chris Lattner | c7a0985 | 2002-07-25 16:31:09 +0000 | [diff] [blame] | 38 | static cl::opt<std::string> |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 39 | InputFilename(cl::Positional, cl::desc("<input bitcode file>"), |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 40 | cl::init("-"), cl::value_desc("filename")); |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 41 | |
Chris Lattner | ba9cc1f | 2004-02-18 16:53:59 +0000 | [diff] [blame] | 42 | static cl::opt<std::string> |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 43 | OutputFilename("o", cl::desc("Specify output filename"), |
Chris Lattner | ba9cc1f | 2004-02-18 16:53:59 +0000 | [diff] [blame] | 44 | cl::value_desc("filename"), cl::init("-")); |
| 45 | |
| 46 | static cl::opt<bool> |
Dan Gohman | baa2639 | 2009-08-25 15:34:52 +0000 | [diff] [blame] | 47 | Force("f", cl::desc("Enable binary output on terminals")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 48 | |
Misha Brukman | ca718e4 | 2004-04-22 23:07:39 +0000 | [diff] [blame] | 49 | static cl::opt<bool> |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 50 | DeleteFn("delete", cl::desc("Delete specified Globals from Module")); |
Misha Brukman | ca718e4 | 2004-04-22 23:07:39 +0000 | [diff] [blame] | 51 | |
Keno Fischer | 1f6fa0f | 2017-04-06 20:51:40 +0000 | [diff] [blame] | 52 | static cl::opt<bool> |
| 53 | Recursive("recursive", |
| 54 | cl::desc("Recursively extract all called functions")); |
| 55 | |
Chad Rosier | 4e0a55d | 2011-09-16 21:09:17 +0000 | [diff] [blame] | 56 | // ExtractFuncs - The functions to extract from the module. |
Dan Gohman | a499d20 | 2010-02-10 23:58:53 +0000 | [diff] [blame] | 57 | static cl::list<std::string> |
| 58 | ExtractFuncs("func", cl::desc("Specify function to extract"), |
| 59 | cl::ZeroOrMore, cl::value_desc("function")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 60 | |
Andrew Trick | 2cccc62 | 2013-09-18 23:31:10 +0000 | [diff] [blame] | 61 | // ExtractRegExpFuncs - The functions, matched via regular expression, to |
Chad Rosier | 4e0a55d | 2011-09-16 21:09:17 +0000 | [diff] [blame] | 62 | // extract from the module. |
| 63 | static cl::list<std::string> |
| 64 | ExtractRegExpFuncs("rfunc", cl::desc("Specify function(s) to extract using a " |
| 65 | "regular expression"), |
| 66 | cl::ZeroOrMore, cl::value_desc("rfunction")); |
| 67 | |
Volkan Keles | 22b2564 | 2018-01-23 21:51:34 +0000 | [diff] [blame] | 68 | // ExtractBlocks - The blocks to extract from the module. |
| 69 | static cl::list<std::string> |
| 70 | ExtractBlocks("bb", |
| 71 | cl::desc("Specify <function, basic block> pairs to extract"), |
| 72 | cl::ZeroOrMore, cl::value_desc("function:bb")); |
| 73 | |
Rafael Espindola | ca88cee | 2012-10-29 02:23:07 +0000 | [diff] [blame] | 74 | // ExtractAlias - The alias to extract from the module. |
| 75 | static cl::list<std::string> |
| 76 | ExtractAliases("alias", cl::desc("Specify alias to extract"), |
| 77 | cl::ZeroOrMore, cl::value_desc("alias")); |
| 78 | |
| 79 | |
| 80 | // ExtractRegExpAliases - The aliases, matched via regular expression, to |
| 81 | // extract from the module. |
| 82 | static cl::list<std::string> |
| 83 | ExtractRegExpAliases("ralias", cl::desc("Specify alias(es) to extract using a " |
| 84 | "regular expression"), |
| 85 | cl::ZeroOrMore, cl::value_desc("ralias")); |
| 86 | |
Chad Rosier | 4e0a55d | 2011-09-16 21:09:17 +0000 | [diff] [blame] | 87 | // ExtractGlobals - The globals to extract from the module. |
Dan Gohman | a499d20 | 2010-02-10 23:58:53 +0000 | [diff] [blame] | 88 | static cl::list<std::string> |
| 89 | ExtractGlobals("glob", cl::desc("Specify global to extract"), |
| 90 | cl::ZeroOrMore, cl::value_desc("global")); |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 91 | |
Chad Rosier | 4e0a55d | 2011-09-16 21:09:17 +0000 | [diff] [blame] | 92 | // ExtractRegExpGlobals - The globals, matched via regular expression, to |
| 93 | // extract from the module... |
| 94 | static cl::list<std::string> |
| 95 | ExtractRegExpGlobals("rglob", cl::desc("Specify global(s) to extract using a " |
| 96 | "regular expression"), |
| 97 | cl::ZeroOrMore, cl::value_desc("rglobal")); |
| 98 | |
Dan Gohman | ec08046 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 99 | static cl::opt<bool> |
| 100 | OutputAssembly("S", |
| 101 | cl::desc("Write output as LLVM assembly"), cl::Hidden); |
| 102 | |
Duncan P. N. Exon Smith | 8d61ee9 | 2015-04-15 03:14:06 +0000 | [diff] [blame] | 103 | static cl::opt<bool> PreserveBitcodeUseListOrder( |
| 104 | "preserve-bc-uselistorder", |
| 105 | cl::desc("Preserve use-list order when writing LLVM bitcode."), |
| 106 | cl::init(true), cl::Hidden); |
| 107 | |
| 108 | static cl::opt<bool> PreserveAssemblyUseListOrder( |
| 109 | "preserve-ll-uselistorder", |
| 110 | cl::desc("Preserve use-list order when writing LLVM assembly."), |
| 111 | cl::init(false), cl::Hidden); |
| 112 | |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 113 | int main(int argc, char **argv) { |
Rui Ueyama | 0b9d56a | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 114 | InitLLVM X(argc, argv); |
Owen Anderson | 8b477ed | 2009-07-01 16:58:40 +0000 | [diff] [blame] | 115 | |
Mehdi Amini | 8be7707 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 116 | LLVMContext Context; |
Chris Lattner | cc14d25 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 117 | cl::ParseCommandLineOptions(argc, argv, "llvm extractor\n"); |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 118 | |
Dan Gohman | 44f9533 | 2010-08-25 23:33:07 +0000 | [diff] [blame] | 119 | // Use lazy loading, since we only care about selected global values. |
Dan Gohman | ec08046 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 120 | SMDiagnostic Err; |
Rafael Espindola | 81e4992 | 2014-08-26 17:29:46 +0000 | [diff] [blame] | 121 | std::unique_ptr<Module> M = getLazyIRFileModule(InputFilename, Err, Context); |
Dan Gohman | ec08046 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 122 | |
Craig Topper | 573faec | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 123 | if (!M.get()) { |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 124 | Err.print(argv[0], errs()); |
Chris Lattner | c48e1db | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 125 | return 1; |
| 126 | } |
| 127 | |
Chad Rosier | 4e0a55d | 2011-09-16 21:09:17 +0000 | [diff] [blame] | 128 | // Use SetVector to avoid duplicates. |
| 129 | SetVector<GlobalValue *> GVs; |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 130 | |
Rafael Espindola | ca88cee | 2012-10-29 02:23:07 +0000 | [diff] [blame] | 131 | // Figure out which aliases we should extract. |
| 132 | for (size_t i = 0, e = ExtractAliases.size(); i != e; ++i) { |
| 133 | GlobalAlias *GA = M->getNamedAlias(ExtractAliases[i]); |
| 134 | if (!GA) { |
| 135 | errs() << argv[0] << ": program doesn't contain alias named '" |
| 136 | << ExtractAliases[i] << "'!\n"; |
| 137 | return 1; |
| 138 | } |
| 139 | GVs.insert(GA); |
| 140 | } |
| 141 | |
| 142 | // Extract aliases via regular expression matching. |
| 143 | for (size_t i = 0, e = ExtractRegExpAliases.size(); i != e; ++i) { |
| 144 | std::string Error; |
| 145 | Regex RegEx(ExtractRegExpAliases[i]); |
| 146 | if (!RegEx.isValid(Error)) { |
| 147 | errs() << argv[0] << ": '" << ExtractRegExpAliases[i] << "' " |
| 148 | "invalid regex: " << Error; |
| 149 | } |
| 150 | bool match = false; |
| 151 | for (Module::alias_iterator GA = M->alias_begin(), E = M->alias_end(); |
| 152 | GA != E; GA++) { |
| 153 | if (RegEx.match(GA->getName())) { |
| 154 | GVs.insert(&*GA); |
| 155 | match = true; |
| 156 | } |
| 157 | } |
| 158 | if (!match) { |
| 159 | errs() << argv[0] << ": program doesn't contain global named '" |
| 160 | << ExtractRegExpAliases[i] << "'!\n"; |
| 161 | return 1; |
| 162 | } |
| 163 | } |
| 164 | |
Dan Gohman | a499d20 | 2010-02-10 23:58:53 +0000 | [diff] [blame] | 165 | // Figure out which globals we should extract. |
| 166 | for (size_t i = 0, e = ExtractGlobals.size(); i != e; ++i) { |
Nick Lewycky | db186c4 | 2011-12-30 19:17:23 +0000 | [diff] [blame] | 167 | GlobalValue *GV = M->getNamedGlobal(ExtractGlobals[i]); |
Dan Gohman | a499d20 | 2010-02-10 23:58:53 +0000 | [diff] [blame] | 168 | if (!GV) { |
| 169 | errs() << argv[0] << ": program doesn't contain global named '" |
| 170 | << ExtractGlobals[i] << "'!\n"; |
| 171 | return 1; |
| 172 | } |
Chad Rosier | 4e0a55d | 2011-09-16 21:09:17 +0000 | [diff] [blame] | 173 | GVs.insert(GV); |
| 174 | } |
| 175 | |
| 176 | // Extract globals via regular expression matching. |
| 177 | for (size_t i = 0, e = ExtractRegExpGlobals.size(); i != e; ++i) { |
| 178 | std::string Error; |
| 179 | Regex RegEx(ExtractRegExpGlobals[i]); |
| 180 | if (!RegEx.isValid(Error)) { |
| 181 | errs() << argv[0] << ": '" << ExtractRegExpGlobals[i] << "' " |
| 182 | "invalid regex: " << Error; |
| 183 | } |
| 184 | bool match = false; |
Rafael Espindola | 82cb28e | 2014-05-08 19:30:17 +0000 | [diff] [blame] | 185 | for (auto &GV : M->globals()) { |
| 186 | if (RegEx.match(GV.getName())) { |
| 187 | GVs.insert(&GV); |
Chad Rosier | 4e0a55d | 2011-09-16 21:09:17 +0000 | [diff] [blame] | 188 | match = true; |
| 189 | } |
| 190 | } |
| 191 | if (!match) { |
| 192 | errs() << argv[0] << ": program doesn't contain global named '" |
| 193 | << ExtractRegExpGlobals[i] << "'!\n"; |
| 194 | return 1; |
| 195 | } |
Dan Gohman | a499d20 | 2010-02-10 23:58:53 +0000 | [diff] [blame] | 196 | } |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 197 | |
Dan Gohman | a499d20 | 2010-02-10 23:58:53 +0000 | [diff] [blame] | 198 | // Figure out which functions we should extract. |
| 199 | for (size_t i = 0, e = ExtractFuncs.size(); i != e; ++i) { |
Nick Lewycky | db186c4 | 2011-12-30 19:17:23 +0000 | [diff] [blame] | 200 | GlobalValue *GV = M->getFunction(ExtractFuncs[i]); |
Dan Gohman | a499d20 | 2010-02-10 23:58:53 +0000 | [diff] [blame] | 201 | if (!GV) { |
| 202 | errs() << argv[0] << ": program doesn't contain function named '" |
| 203 | << ExtractFuncs[i] << "'!\n"; |
| 204 | return 1; |
| 205 | } |
Chad Rosier | 4e0a55d | 2011-09-16 21:09:17 +0000 | [diff] [blame] | 206 | GVs.insert(GV); |
| 207 | } |
| 208 | // Extract functions via regular expression matching. |
| 209 | for (size_t i = 0, e = ExtractRegExpFuncs.size(); i != e; ++i) { |
| 210 | std::string Error; |
| 211 | StringRef RegExStr = ExtractRegExpFuncs[i]; |
| 212 | Regex RegEx(RegExStr); |
| 213 | if (!RegEx.isValid(Error)) { |
| 214 | errs() << argv[0] << ": '" << ExtractRegExpFuncs[i] << "' " |
| 215 | "invalid regex: " << Error; |
| 216 | } |
| 217 | bool match = false; |
Nick Lewycky | db186c4 | 2011-12-30 19:17:23 +0000 | [diff] [blame] | 218 | for (Module::iterator F = M->begin(), E = M->end(); F != E; |
Chad Rosier | 4e0a55d | 2011-09-16 21:09:17 +0000 | [diff] [blame] | 219 | F++) { |
| 220 | if (RegEx.match(F->getName())) { |
| 221 | GVs.insert(&*F); |
| 222 | match = true; |
| 223 | } |
| 224 | } |
| 225 | if (!match) { |
| 226 | errs() << argv[0] << ": program doesn't contain global named '" |
| 227 | << ExtractRegExpFuncs[i] << "'!\n"; |
| 228 | return 1; |
| 229 | } |
Chris Lattner | c48e1db | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Volkan Keles | 22b2564 | 2018-01-23 21:51:34 +0000 | [diff] [blame] | 232 | // Figure out which BasicBlocks we should extract. |
| 233 | SmallVector<BasicBlock *, 4> BBs; |
| 234 | for (StringRef StrPair : ExtractBlocks) { |
| 235 | auto BBInfo = StrPair.split(':'); |
| 236 | // Get the function. |
| 237 | Function *F = M->getFunction(BBInfo.first); |
| 238 | if (!F) { |
| 239 | errs() << argv[0] << ": program doesn't contain a function named '" |
| 240 | << BBInfo.first << "'!\n"; |
| 241 | return 1; |
| 242 | } |
| 243 | // Do not materialize this function. |
| 244 | GVs.insert(F); |
| 245 | // Get the basic block. |
| 246 | auto Res = llvm::find_if(*F, [&](const BasicBlock &BB) { |
| 247 | return BB.getName().equals(BBInfo.second); |
| 248 | }); |
| 249 | if (Res == F->end()) { |
| 250 | errs() << argv[0] << ": function " << F->getName() |
| 251 | << " doesn't contain a basic block named '" << BBInfo.second |
| 252 | << "'!\n"; |
| 253 | return 1; |
| 254 | } |
| 255 | BBs.push_back(&*Res); |
| 256 | } |
| 257 | |
Davide Italiano | 0034728 | 2016-11-09 21:30:33 +0000 | [diff] [blame] | 258 | // Use *argv instead of argv[0] to work around a wrong GCC warning. |
| 259 | ExitOnError ExitOnErr(std::string(*argv) + ": error reading input: "); |
Peter Collingbourne | 76c218e | 2016-11-09 17:49:19 +0000 | [diff] [blame] | 260 | |
Keno Fischer | 1f6fa0f | 2017-04-06 20:51:40 +0000 | [diff] [blame] | 261 | if (Recursive) { |
| 262 | std::vector<llvm::Function *> Workqueue; |
| 263 | for (GlobalValue *GV : GVs) { |
| 264 | if (auto *F = dyn_cast<Function>(GV)) { |
| 265 | Workqueue.push_back(F); |
| 266 | } |
| 267 | } |
| 268 | while (!Workqueue.empty()) { |
| 269 | Function *F = &*Workqueue.back(); |
| 270 | Workqueue.pop_back(); |
| 271 | ExitOnErr(F->materialize()); |
| 272 | for (auto &BB : *F) { |
| 273 | for (auto &I : BB) { |
| 274 | auto *CI = dyn_cast<CallInst>(&I); |
| 275 | if (!CI) |
| 276 | continue; |
| 277 | Function *CF = CI->getCalledFunction(); |
| 278 | if (!CF) |
| 279 | continue; |
| 280 | if (CF->isDeclaration() || GVs.count(CF)) |
| 281 | continue; |
| 282 | GVs.insert(CF); |
| 283 | Workqueue.push_back(CF); |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
Peter Collingbourne | 76c218e | 2016-11-09 17:49:19 +0000 | [diff] [blame] | 289 | auto Materialize = [&](GlobalValue &GV) { ExitOnErr(GV.materialize()); }; |
Rafael Espindola | 3bca2f2 | 2015-12-18 22:40:27 +0000 | [diff] [blame] | 290 | |
| 291 | // Materialize requisite global values. |
| 292 | if (!DeleteFn) { |
| 293 | for (size_t i = 0, e = GVs.size(); i != e; ++i) |
| 294 | Materialize(*GVs[i]); |
| 295 | } else { |
Dan Gohman | be2d4e7 | 2010-09-23 00:33:13 +0000 | [diff] [blame] | 296 | // Deleting. Materialize every GV that's *not* in GVs. |
| 297 | SmallPtrSet<GlobalValue *, 8> GVSet(GVs.begin(), GVs.end()); |
Rafael Espindola | 82cb28e | 2014-05-08 19:30:17 +0000 | [diff] [blame] | 298 | for (auto &F : *M) { |
Rafael Espindola | 3bca2f2 | 2015-12-18 22:40:27 +0000 | [diff] [blame] | 299 | if (!GVSet.count(&F)) |
| 300 | Materialize(F); |
Dan Gohman | 44f9533 | 2010-08-25 23:33:07 +0000 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | |
Rafael Espindola | 17920c2 | 2016-01-15 19:00:20 +0000 | [diff] [blame] | 304 | { |
| 305 | std::vector<GlobalValue *> Gvs(GVs.begin(), GVs.end()); |
| 306 | legacy::PassManager Extract; |
| 307 | Extract.add(createGVExtractionPass(Gvs, DeleteFn)); |
| 308 | Extract.run(*M); |
| 309 | |
| 310 | // Now that we have all the GVs we want, mark the module as fully |
| 311 | // materialized. |
| 312 | // FIXME: should the GVExtractionPass handle this? |
Peter Collingbourne | 76c218e | 2016-11-09 17:49:19 +0000 | [diff] [blame] | 313 | ExitOnErr(M->materializeAll()); |
Rafael Espindola | 17920c2 | 2016-01-15 19:00:20 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Volkan Keles | 22b2564 | 2018-01-23 21:51:34 +0000 | [diff] [blame] | 316 | // Extract the specified basic blocks from the module and erase the existing |
| 317 | // functions. |
| 318 | if (!ExtractBlocks.empty()) { |
| 319 | legacy::PassManager PM; |
| 320 | PM.add(createBlockExtractorPass(BBs, true)); |
| 321 | PM.run(*M); |
| 322 | } |
| 323 | |
Chris Lattner | c48e1db | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 324 | // In addition to deleting all other functions, we also want to spiff it |
| 325 | // up a little bit. Do this now. |
Chandler Carruth | 417c5c1 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 326 | legacy::PassManager Passes; |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 327 | |
Chris Lattner | c48e1db | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 328 | if (!DeleteFn) |
| 329 | Passes.add(createGlobalDCEPass()); // Delete unreachable globals |
Devang Patel | c1874b7 | 2010-07-01 19:58:05 +0000 | [diff] [blame] | 330 | Passes.add(createStripDeadDebugInfoPass()); // Remove dead debug info |
Chris Lattner | c48e1db | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 331 | Passes.add(createStripDeadPrototypesPass()); // Remove dead func decls |
| 332 | |
Rafael Espindola | 8c96862 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 333 | std::error_code EC; |
Reid Kleckner | 97ca964 | 2017-09-23 01:03:17 +0000 | [diff] [blame] | 334 | ToolOutputFile Out(OutputFilename, EC, sys::fs::F_None); |
Rafael Espindola | 8c96862 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 335 | if (EC) { |
| 336 | errs() << EC.message() << '\n'; |
Chris Lattner | 51a1132 | 2009-08-23 02:56:05 +0000 | [diff] [blame] | 337 | return 1; |
Chris Lattner | c48e1db | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Dan Gohman | ec08046 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 340 | if (OutputAssembly) |
Duncan P. N. Exon Smith | e005d71 | 2015-04-15 00:34:24 +0000 | [diff] [blame] | 341 | Passes.add( |
Duncan P. N. Exon Smith | 8d61ee9 | 2015-04-15 03:14:06 +0000 | [diff] [blame] | 342 | createPrintModulePass(Out.os(), "", PreserveAssemblyUseListOrder)); |
| 343 | else if (Force || !CheckBitcodeOutputToConsole(Out.os(), true)) |
| 344 | Passes.add(createBitcodeWriterPass(Out.os(), PreserveBitcodeUseListOrder)); |
Dan Gohman | baa2639 | 2009-08-25 15:34:52 +0000 | [diff] [blame] | 345 | |
Chris Lattner | c48e1db | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 346 | Passes.run(*M.get()); |
| 347 | |
Dan Gohman | 2df9504 | 2010-08-20 01:12:13 +0000 | [diff] [blame] | 348 | // Declare success. |
| 349 | Out.keep(); |
| 350 | |
Chris Lattner | c48e1db | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 351 | return 0; |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 352 | } |