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 | |
Owen Anderson | 8b477ed | 2009-07-01 16:58:40 +0000 | [diff] [blame] | 15 | #include "llvm/LLVMContext.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame^] | 16 | #include "llvm/ADT/SetVector.h" |
| 17 | #include "llvm/ADT/SmallPtrSet.h" |
Dan Gohman | ec08046 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 18 | #include "llvm/Assembly/PrintModulePass.h" |
Chris Lattner | c48e1db | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 19 | #include "llvm/Bitcode/ReaderWriter.h" |
Micah Villmow | 791cfc2 | 2012-10-08 16:39:34 +0000 | [diff] [blame] | 20 | #include "llvm/DataLayout.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame^] | 21 | #include "llvm/Module.h" |
| 22 | #include "llvm/PassManager.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 23 | #include "llvm/Support/CommandLine.h" |
Dan Gohman | ec08046 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 24 | #include "llvm/Support/IRReader.h" |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 25 | #include "llvm/Support/ManagedStatic.h" |
Chris Lattner | cc14d25 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 26 | #include "llvm/Support/PrettyStackTrace.h" |
Chad Rosier | 4e0a55d | 2011-09-16 21:09:17 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Regex.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame^] | 28 | #include "llvm/Support/Signals.h" |
| 29 | #include "llvm/Support/SystemUtils.h" |
| 30 | #include "llvm/Support/ToolOutputFile.h" |
| 31 | #include "llvm/Transforms/IPO.h" |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 32 | #include <memory> |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 33 | using namespace llvm; |
| 34 | |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 35 | // InputFilename - The filename to read from. |
Chris Lattner | c7a0985 | 2002-07-25 16:31:09 +0000 | [diff] [blame] | 36 | static cl::opt<std::string> |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 37 | InputFilename(cl::Positional, cl::desc("<input bitcode file>"), |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 38 | cl::init("-"), cl::value_desc("filename")); |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 39 | |
Chris Lattner | ba9cc1f | 2004-02-18 16:53:59 +0000 | [diff] [blame] | 40 | static cl::opt<std::string> |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 41 | OutputFilename("o", cl::desc("Specify output filename"), |
Chris Lattner | ba9cc1f | 2004-02-18 16:53:59 +0000 | [diff] [blame] | 42 | cl::value_desc("filename"), cl::init("-")); |
| 43 | |
| 44 | static cl::opt<bool> |
Dan Gohman | baa2639 | 2009-08-25 15:34:52 +0000 | [diff] [blame] | 45 | Force("f", cl::desc("Enable binary output on terminals")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 46 | |
Misha Brukman | ca718e4 | 2004-04-22 23:07:39 +0000 | [diff] [blame] | 47 | static cl::opt<bool> |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 48 | DeleteFn("delete", cl::desc("Delete specified Globals from Module")); |
Misha Brukman | ca718e4 | 2004-04-22 23:07:39 +0000 | [diff] [blame] | 49 | |
Chad Rosier | 4e0a55d | 2011-09-16 21:09:17 +0000 | [diff] [blame] | 50 | // ExtractFuncs - The functions to extract from the module. |
Dan Gohman | a499d20 | 2010-02-10 23:58:53 +0000 | [diff] [blame] | 51 | static cl::list<std::string> |
| 52 | ExtractFuncs("func", cl::desc("Specify function to extract"), |
| 53 | cl::ZeroOrMore, cl::value_desc("function")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 54 | |
Chad Rosier | 4e0a55d | 2011-09-16 21:09:17 +0000 | [diff] [blame] | 55 | // ExtractRegExpFuncs - The functions, matched via regular expression, to |
| 56 | // extract from the module. |
| 57 | static cl::list<std::string> |
| 58 | ExtractRegExpFuncs("rfunc", cl::desc("Specify function(s) to extract using a " |
| 59 | "regular expression"), |
| 60 | cl::ZeroOrMore, cl::value_desc("rfunction")); |
| 61 | |
Rafael Espindola | ca88cee | 2012-10-29 02:23:07 +0000 | [diff] [blame] | 62 | // ExtractAlias - The alias to extract from the module. |
| 63 | static cl::list<std::string> |
| 64 | ExtractAliases("alias", cl::desc("Specify alias to extract"), |
| 65 | cl::ZeroOrMore, cl::value_desc("alias")); |
| 66 | |
| 67 | |
| 68 | // ExtractRegExpAliases - The aliases, matched via regular expression, to |
| 69 | // extract from the module. |
| 70 | static cl::list<std::string> |
| 71 | ExtractRegExpAliases("ralias", cl::desc("Specify alias(es) to extract using a " |
| 72 | "regular expression"), |
| 73 | cl::ZeroOrMore, cl::value_desc("ralias")); |
| 74 | |
Chad Rosier | 4e0a55d | 2011-09-16 21:09:17 +0000 | [diff] [blame] | 75 | // ExtractGlobals - The globals to extract from the module. |
Dan Gohman | a499d20 | 2010-02-10 23:58:53 +0000 | [diff] [blame] | 76 | static cl::list<std::string> |
| 77 | ExtractGlobals("glob", cl::desc("Specify global to extract"), |
| 78 | cl::ZeroOrMore, cl::value_desc("global")); |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 79 | |
Chad Rosier | 4e0a55d | 2011-09-16 21:09:17 +0000 | [diff] [blame] | 80 | // ExtractRegExpGlobals - The globals, matched via regular expression, to |
| 81 | // extract from the module... |
| 82 | static cl::list<std::string> |
| 83 | ExtractRegExpGlobals("rglob", cl::desc("Specify global(s) to extract using a " |
| 84 | "regular expression"), |
| 85 | cl::ZeroOrMore, cl::value_desc("rglobal")); |
| 86 | |
Dan Gohman | ec08046 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 87 | static cl::opt<bool> |
| 88 | OutputAssembly("S", |
| 89 | cl::desc("Write output as LLVM assembly"), cl::Hidden); |
| 90 | |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 91 | int main(int argc, char **argv) { |
Chris Lattner | cc14d25 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 92 | // Print a stack trace if we signal out. |
Chris Lattner | c48e1db | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 93 | sys::PrintStackTraceOnErrorSignal(); |
Chris Lattner | cc14d25 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 94 | PrettyStackTraceProgram X(argc, argv); |
Owen Anderson | 8b477ed | 2009-07-01 16:58:40 +0000 | [diff] [blame] | 95 | |
Owen Anderson | 0d7c695 | 2009-07-15 22:16:10 +0000 | [diff] [blame] | 96 | LLVMContext &Context = getGlobalContext(); |
Chris Lattner | cc14d25 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 97 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 98 | cl::ParseCommandLineOptions(argc, argv, "llvm extractor\n"); |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 99 | |
Dan Gohman | 44f9533 | 2010-08-25 23:33:07 +0000 | [diff] [blame] | 100 | // Use lazy loading, since we only care about selected global values. |
Dan Gohman | ec08046 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 101 | SMDiagnostic Err; |
Chris Lattner | c48e1db | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 102 | std::auto_ptr<Module> M; |
Dan Gohman | 44f9533 | 2010-08-25 23:33:07 +0000 | [diff] [blame] | 103 | M.reset(getLazyIRFileModule(InputFilename, Err, Context)); |
Dan Gohman | ec08046 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 104 | |
Chris Lattner | c48e1db | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 105 | if (M.get() == 0) { |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 106 | Err.print(argv[0], errs()); |
Chris Lattner | c48e1db | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 107 | return 1; |
| 108 | } |
| 109 | |
Chad Rosier | 4e0a55d | 2011-09-16 21:09:17 +0000 | [diff] [blame] | 110 | // Use SetVector to avoid duplicates. |
| 111 | SetVector<GlobalValue *> GVs; |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 112 | |
Rafael Espindola | ca88cee | 2012-10-29 02:23:07 +0000 | [diff] [blame] | 113 | // Figure out which aliases we should extract. |
| 114 | for (size_t i = 0, e = ExtractAliases.size(); i != e; ++i) { |
| 115 | GlobalAlias *GA = M->getNamedAlias(ExtractAliases[i]); |
| 116 | if (!GA) { |
| 117 | errs() << argv[0] << ": program doesn't contain alias named '" |
| 118 | << ExtractAliases[i] << "'!\n"; |
| 119 | return 1; |
| 120 | } |
| 121 | GVs.insert(GA); |
| 122 | } |
| 123 | |
| 124 | // Extract aliases via regular expression matching. |
| 125 | for (size_t i = 0, e = ExtractRegExpAliases.size(); i != e; ++i) { |
| 126 | std::string Error; |
| 127 | Regex RegEx(ExtractRegExpAliases[i]); |
| 128 | if (!RegEx.isValid(Error)) { |
| 129 | errs() << argv[0] << ": '" << ExtractRegExpAliases[i] << "' " |
| 130 | "invalid regex: " << Error; |
| 131 | } |
| 132 | bool match = false; |
| 133 | for (Module::alias_iterator GA = M->alias_begin(), E = M->alias_end(); |
| 134 | GA != E; GA++) { |
| 135 | if (RegEx.match(GA->getName())) { |
| 136 | GVs.insert(&*GA); |
| 137 | match = true; |
| 138 | } |
| 139 | } |
| 140 | if (!match) { |
| 141 | errs() << argv[0] << ": program doesn't contain global named '" |
| 142 | << ExtractRegExpAliases[i] << "'!\n"; |
| 143 | return 1; |
| 144 | } |
| 145 | } |
| 146 | |
Dan Gohman | a499d20 | 2010-02-10 23:58:53 +0000 | [diff] [blame] | 147 | // Figure out which globals we should extract. |
| 148 | for (size_t i = 0, e = ExtractGlobals.size(); i != e; ++i) { |
Nick Lewycky | db186c4 | 2011-12-30 19:17:23 +0000 | [diff] [blame] | 149 | GlobalValue *GV = M->getNamedGlobal(ExtractGlobals[i]); |
Dan Gohman | a499d20 | 2010-02-10 23:58:53 +0000 | [diff] [blame] | 150 | if (!GV) { |
| 151 | errs() << argv[0] << ": program doesn't contain global named '" |
| 152 | << ExtractGlobals[i] << "'!\n"; |
| 153 | return 1; |
| 154 | } |
Chad Rosier | 4e0a55d | 2011-09-16 21:09:17 +0000 | [diff] [blame] | 155 | GVs.insert(GV); |
| 156 | } |
| 157 | |
| 158 | // Extract globals via regular expression matching. |
| 159 | for (size_t i = 0, e = ExtractRegExpGlobals.size(); i != e; ++i) { |
| 160 | std::string Error; |
| 161 | Regex RegEx(ExtractRegExpGlobals[i]); |
| 162 | if (!RegEx.isValid(Error)) { |
| 163 | errs() << argv[0] << ": '" << ExtractRegExpGlobals[i] << "' " |
| 164 | "invalid regex: " << Error; |
| 165 | } |
| 166 | bool match = false; |
Nick Lewycky | db186c4 | 2011-12-30 19:17:23 +0000 | [diff] [blame] | 167 | for (Module::global_iterator GV = M->global_begin(), |
| 168 | E = M->global_end(); GV != E; GV++) { |
Chad Rosier | 4e0a55d | 2011-09-16 21:09:17 +0000 | [diff] [blame] | 169 | if (RegEx.match(GV->getName())) { |
| 170 | GVs.insert(&*GV); |
| 171 | match = true; |
| 172 | } |
| 173 | } |
| 174 | if (!match) { |
| 175 | errs() << argv[0] << ": program doesn't contain global named '" |
| 176 | << ExtractRegExpGlobals[i] << "'!\n"; |
| 177 | return 1; |
| 178 | } |
Dan Gohman | a499d20 | 2010-02-10 23:58:53 +0000 | [diff] [blame] | 179 | } |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 180 | |
Dan Gohman | a499d20 | 2010-02-10 23:58:53 +0000 | [diff] [blame] | 181 | // Figure out which functions we should extract. |
| 182 | for (size_t i = 0, e = ExtractFuncs.size(); i != e; ++i) { |
Nick Lewycky | db186c4 | 2011-12-30 19:17:23 +0000 | [diff] [blame] | 183 | GlobalValue *GV = M->getFunction(ExtractFuncs[i]); |
Dan Gohman | a499d20 | 2010-02-10 23:58:53 +0000 | [diff] [blame] | 184 | if (!GV) { |
| 185 | errs() << argv[0] << ": program doesn't contain function named '" |
| 186 | << ExtractFuncs[i] << "'!\n"; |
| 187 | return 1; |
| 188 | } |
Chad Rosier | 4e0a55d | 2011-09-16 21:09:17 +0000 | [diff] [blame] | 189 | GVs.insert(GV); |
| 190 | } |
| 191 | // Extract functions via regular expression matching. |
| 192 | for (size_t i = 0, e = ExtractRegExpFuncs.size(); i != e; ++i) { |
| 193 | std::string Error; |
| 194 | StringRef RegExStr = ExtractRegExpFuncs[i]; |
| 195 | Regex RegEx(RegExStr); |
| 196 | if (!RegEx.isValid(Error)) { |
| 197 | errs() << argv[0] << ": '" << ExtractRegExpFuncs[i] << "' " |
| 198 | "invalid regex: " << Error; |
| 199 | } |
| 200 | bool match = false; |
Nick Lewycky | db186c4 | 2011-12-30 19:17:23 +0000 | [diff] [blame] | 201 | for (Module::iterator F = M->begin(), E = M->end(); F != E; |
Chad Rosier | 4e0a55d | 2011-09-16 21:09:17 +0000 | [diff] [blame] | 202 | F++) { |
| 203 | if (RegEx.match(F->getName())) { |
| 204 | GVs.insert(&*F); |
| 205 | match = true; |
| 206 | } |
| 207 | } |
| 208 | if (!match) { |
| 209 | errs() << argv[0] << ": program doesn't contain global named '" |
| 210 | << ExtractRegExpFuncs[i] << "'!\n"; |
| 211 | return 1; |
| 212 | } |
Chris Lattner | c48e1db | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Dan Gohman | 44f9533 | 2010-08-25 23:33:07 +0000 | [diff] [blame] | 215 | // Materialize requisite global values. |
Dan Gohman | be2d4e7 | 2010-09-23 00:33:13 +0000 | [diff] [blame] | 216 | if (!DeleteFn) |
| 217 | for (size_t i = 0, e = GVs.size(); i != e; ++i) { |
| 218 | GlobalValue *GV = GVs[i]; |
| 219 | if (GV->isMaterializable()) { |
| 220 | std::string ErrInfo; |
| 221 | if (GV->Materialize(&ErrInfo)) { |
| 222 | errs() << argv[0] << ": error reading input: " << ErrInfo << "\n"; |
| 223 | return 1; |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | else { |
| 228 | // Deleting. Materialize every GV that's *not* in GVs. |
| 229 | SmallPtrSet<GlobalValue *, 8> GVSet(GVs.begin(), GVs.end()); |
| 230 | for (Module::global_iterator I = M->global_begin(), E = M->global_end(); |
| 231 | I != E; ++I) { |
| 232 | GlobalVariable *G = I; |
| 233 | if (!GVSet.count(G) && G->isMaterializable()) { |
| 234 | std::string ErrInfo; |
| 235 | if (G->Materialize(&ErrInfo)) { |
| 236 | errs() << argv[0] << ": error reading input: " << ErrInfo << "\n"; |
| 237 | return 1; |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) { |
| 242 | Function *F = I; |
| 243 | if (!GVSet.count(F) && F->isMaterializable()) { |
| 244 | std::string ErrInfo; |
| 245 | if (F->Materialize(&ErrInfo)) { |
| 246 | errs() << argv[0] << ": error reading input: " << ErrInfo << "\n"; |
| 247 | return 1; |
| 248 | } |
Dan Gohman | 44f9533 | 2010-08-25 23:33:07 +0000 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
Chris Lattner | c48e1db | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 253 | // In addition to deleting all other functions, we also want to spiff it |
| 254 | // up a little bit. Do this now. |
| 255 | PassManager Passes; |
Micah Villmow | 791cfc2 | 2012-10-08 16:39:34 +0000 | [diff] [blame] | 256 | Passes.add(new DataLayout(M.get())); // Use correct DataLayout |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 257 | |
Chad Rosier | 4e0a55d | 2011-09-16 21:09:17 +0000 | [diff] [blame] | 258 | std::vector<GlobalValue*> Gvs(GVs.begin(), GVs.end()); |
| 259 | |
| 260 | Passes.add(createGVExtractionPass(Gvs, DeleteFn)); |
Chris Lattner | c48e1db | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 261 | if (!DeleteFn) |
| 262 | Passes.add(createGlobalDCEPass()); // Delete unreachable globals |
Devang Patel | c1874b7 | 2010-07-01 19:58:05 +0000 | [diff] [blame] | 263 | Passes.add(createStripDeadDebugInfoPass()); // Remove dead debug info |
Chris Lattner | c48e1db | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 264 | Passes.add(createStripDeadPrototypesPass()); // Remove dead func decls |
| 265 | |
Chris Lattner | 51a1132 | 2009-08-23 02:56:05 +0000 | [diff] [blame] | 266 | std::string ErrorInfo; |
Dan Gohman | 2df9504 | 2010-08-20 01:12:13 +0000 | [diff] [blame] | 267 | tool_output_file Out(OutputFilename.c_str(), ErrorInfo, |
| 268 | raw_fd_ostream::F_Binary); |
Chris Lattner | 51a1132 | 2009-08-23 02:56:05 +0000 | [diff] [blame] | 269 | if (!ErrorInfo.empty()) { |
| 270 | errs() << ErrorInfo << '\n'; |
Chris Lattner | 51a1132 | 2009-08-23 02:56:05 +0000 | [diff] [blame] | 271 | return 1; |
Chris Lattner | c48e1db | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Dan Gohman | ec08046 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 274 | if (OutputAssembly) |
Dan Gohman | d4c4543 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 275 | Passes.add(createPrintModulePass(&Out.os())); |
| 276 | else if (Force || !CheckBitcodeOutputToConsole(Out.os(), true)) |
| 277 | Passes.add(createBitcodeWriterPass(Out.os())); |
Dan Gohman | baa2639 | 2009-08-25 15:34:52 +0000 | [diff] [blame] | 278 | |
Chris Lattner | c48e1db | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 279 | Passes.run(*M.get()); |
| 280 | |
Dan Gohman | 2df9504 | 2010-08-20 01:12:13 +0000 | [diff] [blame] | 281 | // Declare success. |
| 282 | Out.keep(); |
| 283 | |
Chris Lattner | c48e1db | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 284 | return 0; |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 285 | } |