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" |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 16 | #include "llvm/Module.h" |
| 17 | #include "llvm/PassManager.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" |
Chris Lattner | 33974ca | 2002-07-23 22:04:40 +0000 | [diff] [blame] | 20 | #include "llvm/Transforms/IPO.h" |
Brian Gaeke | 2040890 | 2003-10-28 22:22:16 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetData.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 22 | #include "llvm/Support/CommandLine.h" |
Dan Gohman | ec08046 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 23 | #include "llvm/Support/IRReader.h" |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ManagedStatic.h" |
Chris Lattner | cc14d25 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 25 | #include "llvm/Support/PrettyStackTrace.h" |
Dan Gohman | e4f1a9b | 2010-10-07 20:32:40 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ToolOutputFile.h" |
Dan Gohman | baa2639 | 2009-08-25 15:34:52 +0000 | [diff] [blame] | 27 | #include "llvm/Support/SystemUtils.h" |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Signals.h" |
Chad Rosier | 4e0a55d | 2011-09-16 21:09:17 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Regex.h" |
Dan Gohman | be2d4e7 | 2010-09-23 00:33:13 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/SmallPtrSet.h" |
Chad Rosier | 4e0a55d | 2011-09-16 21:09:17 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/SetVector.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 | |
| 62 | // ExtractGlobals - The globals to extract from the module. |
Dan Gohman | a499d20 | 2010-02-10 23:58:53 +0000 | [diff] [blame] | 63 | static cl::list<std::string> |
| 64 | ExtractGlobals("glob", cl::desc("Specify global to extract"), |
| 65 | cl::ZeroOrMore, cl::value_desc("global")); |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 66 | |
Chad Rosier | 4e0a55d | 2011-09-16 21:09:17 +0000 | [diff] [blame] | 67 | // ExtractRegExpGlobals - The globals, matched via regular expression, to |
| 68 | // extract from the module... |
| 69 | static cl::list<std::string> |
| 70 | ExtractRegExpGlobals("rglob", cl::desc("Specify global(s) to extract using a " |
| 71 | "regular expression"), |
| 72 | cl::ZeroOrMore, cl::value_desc("rglobal")); |
| 73 | |
Dan Gohman | ec08046 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 74 | static cl::opt<bool> |
| 75 | OutputAssembly("S", |
| 76 | cl::desc("Write output as LLVM assembly"), cl::Hidden); |
| 77 | |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 78 | int main(int argc, char **argv) { |
Chris Lattner | cc14d25 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 79 | // Print a stack trace if we signal out. |
Chris Lattner | c48e1db | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 80 | sys::PrintStackTraceOnErrorSignal(); |
Chris Lattner | cc14d25 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 81 | PrettyStackTraceProgram X(argc, argv); |
Owen Anderson | 8b477ed | 2009-07-01 16:58:40 +0000 | [diff] [blame] | 82 | |
Owen Anderson | 0d7c695 | 2009-07-15 22:16:10 +0000 | [diff] [blame] | 83 | LLVMContext &Context = getGlobalContext(); |
Chris Lattner | cc14d25 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 84 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 85 | cl::ParseCommandLineOptions(argc, argv, "llvm extractor\n"); |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 86 | |
Dan Gohman | 44f9533 | 2010-08-25 23:33:07 +0000 | [diff] [blame] | 87 | // Use lazy loading, since we only care about selected global values. |
Dan Gohman | ec08046 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 88 | SMDiagnostic Err; |
Chris Lattner | c48e1db | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 89 | std::auto_ptr<Module> M; |
Dan Gohman | 44f9533 | 2010-08-25 23:33:07 +0000 | [diff] [blame] | 90 | M.reset(getLazyIRFileModule(InputFilename, Err, Context)); |
Dan Gohman | ec08046 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 91 | |
Chris Lattner | c48e1db | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 92 | if (M.get() == 0) { |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame^] | 93 | Err.print(argv[0], errs()); |
Chris Lattner | c48e1db | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 94 | return 1; |
| 95 | } |
| 96 | |
Chad Rosier | 4e0a55d | 2011-09-16 21:09:17 +0000 | [diff] [blame] | 97 | // Use SetVector to avoid duplicates. |
| 98 | SetVector<GlobalValue *> GVs; |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 99 | |
Dan Gohman | a499d20 | 2010-02-10 23:58:53 +0000 | [diff] [blame] | 100 | // Figure out which globals we should extract. |
| 101 | for (size_t i = 0, e = ExtractGlobals.size(); i != e; ++i) { |
| 102 | GlobalValue *GV = M.get()->getNamedGlobal(ExtractGlobals[i]); |
| 103 | if (!GV) { |
| 104 | errs() << argv[0] << ": program doesn't contain global named '" |
| 105 | << ExtractGlobals[i] << "'!\n"; |
| 106 | return 1; |
| 107 | } |
Chad Rosier | 4e0a55d | 2011-09-16 21:09:17 +0000 | [diff] [blame] | 108 | GVs.insert(GV); |
| 109 | } |
| 110 | |
| 111 | // Extract globals via regular expression matching. |
| 112 | for (size_t i = 0, e = ExtractRegExpGlobals.size(); i != e; ++i) { |
| 113 | std::string Error; |
| 114 | Regex RegEx(ExtractRegExpGlobals[i]); |
| 115 | if (!RegEx.isValid(Error)) { |
| 116 | errs() << argv[0] << ": '" << ExtractRegExpGlobals[i] << "' " |
| 117 | "invalid regex: " << Error; |
| 118 | } |
| 119 | bool match = false; |
| 120 | for (Module::global_iterator GV = M.get()->global_begin(), |
| 121 | E = M.get()->global_end(); GV != E; GV++) { |
| 122 | if (RegEx.match(GV->getName())) { |
| 123 | GVs.insert(&*GV); |
| 124 | match = true; |
| 125 | } |
| 126 | } |
| 127 | if (!match) { |
| 128 | errs() << argv[0] << ": program doesn't contain global named '" |
| 129 | << ExtractRegExpGlobals[i] << "'!\n"; |
| 130 | return 1; |
| 131 | } |
Dan Gohman | a499d20 | 2010-02-10 23:58:53 +0000 | [diff] [blame] | 132 | } |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 133 | |
Dan Gohman | a499d20 | 2010-02-10 23:58:53 +0000 | [diff] [blame] | 134 | // Figure out which functions we should extract. |
| 135 | for (size_t i = 0, e = ExtractFuncs.size(); i != e; ++i) { |
| 136 | GlobalValue *GV = M.get()->getFunction(ExtractFuncs[i]); |
| 137 | if (!GV) { |
| 138 | errs() << argv[0] << ": program doesn't contain function named '" |
| 139 | << ExtractFuncs[i] << "'!\n"; |
| 140 | return 1; |
| 141 | } |
Chad Rosier | 4e0a55d | 2011-09-16 21:09:17 +0000 | [diff] [blame] | 142 | GVs.insert(GV); |
| 143 | } |
| 144 | // Extract functions via regular expression matching. |
| 145 | for (size_t i = 0, e = ExtractRegExpFuncs.size(); i != e; ++i) { |
| 146 | std::string Error; |
| 147 | StringRef RegExStr = ExtractRegExpFuncs[i]; |
| 148 | Regex RegEx(RegExStr); |
| 149 | if (!RegEx.isValid(Error)) { |
| 150 | errs() << argv[0] << ": '" << ExtractRegExpFuncs[i] << "' " |
| 151 | "invalid regex: " << Error; |
| 152 | } |
| 153 | bool match = false; |
| 154 | for (Module::iterator F = M.get()->begin(), E = M.get()->end(); F != E; |
| 155 | F++) { |
| 156 | if (RegEx.match(F->getName())) { |
| 157 | GVs.insert(&*F); |
| 158 | match = true; |
| 159 | } |
| 160 | } |
| 161 | if (!match) { |
| 162 | errs() << argv[0] << ": program doesn't contain global named '" |
| 163 | << ExtractRegExpFuncs[i] << "'!\n"; |
| 164 | return 1; |
| 165 | } |
Chris Lattner | c48e1db | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Dan Gohman | 44f9533 | 2010-08-25 23:33:07 +0000 | [diff] [blame] | 168 | // Materialize requisite global values. |
Dan Gohman | be2d4e7 | 2010-09-23 00:33:13 +0000 | [diff] [blame] | 169 | if (!DeleteFn) |
| 170 | for (size_t i = 0, e = GVs.size(); i != e; ++i) { |
| 171 | GlobalValue *GV = GVs[i]; |
| 172 | if (GV->isMaterializable()) { |
| 173 | std::string ErrInfo; |
| 174 | if (GV->Materialize(&ErrInfo)) { |
| 175 | errs() << argv[0] << ": error reading input: " << ErrInfo << "\n"; |
| 176 | return 1; |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | else { |
| 181 | // Deleting. Materialize every GV that's *not* in GVs. |
| 182 | SmallPtrSet<GlobalValue *, 8> GVSet(GVs.begin(), GVs.end()); |
| 183 | for (Module::global_iterator I = M->global_begin(), E = M->global_end(); |
| 184 | I != E; ++I) { |
| 185 | GlobalVariable *G = I; |
| 186 | if (!GVSet.count(G) && G->isMaterializable()) { |
| 187 | std::string ErrInfo; |
| 188 | if (G->Materialize(&ErrInfo)) { |
| 189 | errs() << argv[0] << ": error reading input: " << ErrInfo << "\n"; |
| 190 | return 1; |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) { |
| 195 | Function *F = I; |
| 196 | if (!GVSet.count(F) && F->isMaterializable()) { |
| 197 | std::string ErrInfo; |
| 198 | if (F->Materialize(&ErrInfo)) { |
| 199 | errs() << argv[0] << ": error reading input: " << ErrInfo << "\n"; |
| 200 | return 1; |
| 201 | } |
Dan Gohman | 44f9533 | 2010-08-25 23:33:07 +0000 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
Chris Lattner | c48e1db | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 206 | // In addition to deleting all other functions, we also want to spiff it |
| 207 | // up a little bit. Do this now. |
| 208 | PassManager Passes; |
| 209 | Passes.add(new TargetData(M.get())); // Use correct TargetData |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 210 | |
Chad Rosier | 4e0a55d | 2011-09-16 21:09:17 +0000 | [diff] [blame] | 211 | std::vector<GlobalValue*> Gvs(GVs.begin(), GVs.end()); |
| 212 | |
| 213 | Passes.add(createGVExtractionPass(Gvs, DeleteFn)); |
Chris Lattner | c48e1db | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 214 | if (!DeleteFn) |
| 215 | Passes.add(createGlobalDCEPass()); // Delete unreachable globals |
Devang Patel | c1874b7 | 2010-07-01 19:58:05 +0000 | [diff] [blame] | 216 | Passes.add(createStripDeadDebugInfoPass()); // Remove dead debug info |
Chris Lattner | c48e1db | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 217 | Passes.add(createStripDeadPrototypesPass()); // Remove dead func decls |
| 218 | |
Chris Lattner | 51a1132 | 2009-08-23 02:56:05 +0000 | [diff] [blame] | 219 | std::string ErrorInfo; |
Dan Gohman | 2df9504 | 2010-08-20 01:12:13 +0000 | [diff] [blame] | 220 | tool_output_file Out(OutputFilename.c_str(), ErrorInfo, |
| 221 | raw_fd_ostream::F_Binary); |
Chris Lattner | 51a1132 | 2009-08-23 02:56:05 +0000 | [diff] [blame] | 222 | if (!ErrorInfo.empty()) { |
| 223 | errs() << ErrorInfo << '\n'; |
Chris Lattner | 51a1132 | 2009-08-23 02:56:05 +0000 | [diff] [blame] | 224 | return 1; |
Chris Lattner | c48e1db | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Dan Gohman | ec08046 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 227 | if (OutputAssembly) |
Dan Gohman | d4c4543 | 2010-09-01 14:20:41 +0000 | [diff] [blame] | 228 | Passes.add(createPrintModulePass(&Out.os())); |
| 229 | else if (Force || !CheckBitcodeOutputToConsole(Out.os(), true)) |
| 230 | Passes.add(createBitcodeWriterPass(Out.os())); |
Dan Gohman | baa2639 | 2009-08-25 15:34:52 +0000 | [diff] [blame] | 231 | |
Chris Lattner | c48e1db | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 232 | Passes.run(*M.get()); |
| 233 | |
Dan Gohman | 2df9504 | 2010-08-20 01:12:13 +0000 | [diff] [blame] | 234 | // Declare success. |
| 235 | Out.keep(); |
| 236 | |
Chris Lattner | c48e1db | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 237 | return 0; |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 238 | } |