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 | a1bdced | 2009-07-15 17:29:42 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | baa2639 | 2009-08-25 15:34:52 +0000 | [diff] [blame] | 27 | #include "llvm/Support/SystemUtils.h" |
Chris Lattner | bed85ff | 2004-05-27 05:41:36 +0000 | [diff] [blame] | 28 | #include "llvm/System/Signals.h" |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 29 | #include <memory> |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 30 | using namespace llvm; |
| 31 | |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 32 | // InputFilename - The filename to read from. |
Chris Lattner | c7a0985 | 2002-07-25 16:31:09 +0000 | [diff] [blame] | 33 | static cl::opt<std::string> |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 34 | InputFilename(cl::Positional, cl::desc("<input bitcode file>"), |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 35 | cl::init("-"), cl::value_desc("filename")); |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 36 | |
Chris Lattner | ba9cc1f | 2004-02-18 16:53:59 +0000 | [diff] [blame] | 37 | static cl::opt<std::string> |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 38 | OutputFilename("o", cl::desc("Specify output filename"), |
Chris Lattner | ba9cc1f | 2004-02-18 16:53:59 +0000 | [diff] [blame] | 39 | cl::value_desc("filename"), cl::init("-")); |
| 40 | |
| 41 | static cl::opt<bool> |
Dan Gohman | baa2639 | 2009-08-25 15:34:52 +0000 | [diff] [blame] | 42 | Force("f", cl::desc("Enable binary output on terminals")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 43 | |
Misha Brukman | ca718e4 | 2004-04-22 23:07:39 +0000 | [diff] [blame] | 44 | static cl::opt<bool> |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 45 | DeleteFn("delete", cl::desc("Delete specified Globals from Module")); |
Misha Brukman | ca718e4 | 2004-04-22 23:07:39 +0000 | [diff] [blame] | 46 | |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 47 | static cl::opt<bool> |
| 48 | Relink("relink", |
| 49 | cl::desc("Turn external linkage for callees of function to delete")); |
| 50 | |
Dan Gohman | a499d20 | 2010-02-10 23:58:53 +0000 | [diff] [blame] | 51 | // ExtractFuncs - The functions to extract from the module... |
| 52 | static cl::list<std::string> |
| 53 | ExtractFuncs("func", cl::desc("Specify function to extract"), |
| 54 | cl::ZeroOrMore, cl::value_desc("function")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 55 | |
Dan Gohman | a499d20 | 2010-02-10 23:58:53 +0000 | [diff] [blame] | 56 | // ExtractGlobals - The globals to extract from the module... |
| 57 | static cl::list<std::string> |
| 58 | ExtractGlobals("glob", cl::desc("Specify global to extract"), |
| 59 | cl::ZeroOrMore, cl::value_desc("global")); |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 60 | |
Dan Gohman | ec08046 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 61 | static cl::opt<bool> |
| 62 | OutputAssembly("S", |
| 63 | cl::desc("Write output as LLVM assembly"), cl::Hidden); |
| 64 | |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 65 | int main(int argc, char **argv) { |
Chris Lattner | cc14d25 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 66 | // Print a stack trace if we signal out. |
Chris Lattner | c48e1db | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 67 | sys::PrintStackTraceOnErrorSignal(); |
Chris Lattner | cc14d25 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 68 | PrettyStackTraceProgram X(argc, argv); |
Owen Anderson | 8b477ed | 2009-07-01 16:58:40 +0000 | [diff] [blame] | 69 | |
Owen Anderson | 0d7c695 | 2009-07-15 22:16:10 +0000 | [diff] [blame] | 70 | LLVMContext &Context = getGlobalContext(); |
Chris Lattner | cc14d25 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 71 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 72 | cl::ParseCommandLineOptions(argc, argv, "llvm extractor\n"); |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 73 | |
Dan Gohman | ec08046 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 74 | SMDiagnostic Err; |
Chris Lattner | c48e1db | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 75 | std::auto_ptr<Module> M; |
Dan Gohman | ec08046 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 76 | M.reset(ParseIRFile(InputFilename, Err, Context)); |
| 77 | |
Chris Lattner | c48e1db | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 78 | if (M.get() == 0) { |
Dan Gohman | ec08046 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 79 | Err.Print(argv[0], errs()); |
Chris Lattner | c48e1db | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 80 | return 1; |
| 81 | } |
| 82 | |
Dan Gohman | a499d20 | 2010-02-10 23:58:53 +0000 | [diff] [blame] | 83 | std::vector<GlobalValue *> GVs; |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 84 | |
Dan Gohman | a499d20 | 2010-02-10 23:58:53 +0000 | [diff] [blame] | 85 | // Figure out which globals we should extract. |
| 86 | for (size_t i = 0, e = ExtractGlobals.size(); i != e; ++i) { |
| 87 | GlobalValue *GV = M.get()->getNamedGlobal(ExtractGlobals[i]); |
| 88 | if (!GV) { |
| 89 | errs() << argv[0] << ": program doesn't contain global named '" |
| 90 | << ExtractGlobals[i] << "'!\n"; |
| 91 | return 1; |
| 92 | } |
| 93 | GVs.push_back(GV); |
| 94 | } |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 95 | |
Dan Gohman | a499d20 | 2010-02-10 23:58:53 +0000 | [diff] [blame] | 96 | // Figure out which functions we should extract. |
| 97 | for (size_t i = 0, e = ExtractFuncs.size(); i != e; ++i) { |
| 98 | GlobalValue *GV = M.get()->getFunction(ExtractFuncs[i]); |
| 99 | if (!GV) { |
| 100 | errs() << argv[0] << ": program doesn't contain function named '" |
| 101 | << ExtractFuncs[i] << "'!\n"; |
| 102 | return 1; |
| 103 | } |
| 104 | GVs.push_back(GV); |
Chris Lattner | c48e1db | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | // In addition to deleting all other functions, we also want to spiff it |
| 108 | // up a little bit. Do this now. |
| 109 | PassManager Passes; |
| 110 | Passes.add(new TargetData(M.get())); // Use correct TargetData |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 111 | |
| 112 | Passes.add(createGVExtractionPass(GVs, DeleteFn, Relink)); |
Chris Lattner | c48e1db | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 113 | if (!DeleteFn) |
| 114 | Passes.add(createGlobalDCEPass()); // Delete unreachable globals |
Devang Patel | c1874b7 | 2010-07-01 19:58:05 +0000 | [diff] [blame^] | 115 | Passes.add(createStripDeadDebugInfoPass()); // Remove dead debug info |
Chris Lattner | c48e1db | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 116 | Passes.add(createDeadTypeEliminationPass()); // Remove dead types... |
| 117 | Passes.add(createStripDeadPrototypesPass()); // Remove dead func decls |
| 118 | |
Dan Gohman | ec08046 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 119 | // Make sure that the Output file gets unlinked from the disk if we get a |
| 120 | // SIGINT |
| 121 | sys::RemoveFileOnSignal(sys::Path(OutputFilename)); |
| 122 | |
Chris Lattner | 51a1132 | 2009-08-23 02:56:05 +0000 | [diff] [blame] | 123 | std::string ErrorInfo; |
Dan Gohman | ec08046 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 124 | raw_fd_ostream Out(OutputFilename.c_str(), ErrorInfo, |
| 125 | raw_fd_ostream::F_Binary); |
Chris Lattner | 51a1132 | 2009-08-23 02:56:05 +0000 | [diff] [blame] | 126 | if (!ErrorInfo.empty()) { |
| 127 | errs() << ErrorInfo << '\n'; |
Chris Lattner | 51a1132 | 2009-08-23 02:56:05 +0000 | [diff] [blame] | 128 | return 1; |
Chris Lattner | c48e1db | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Dan Gohman | ec08046 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 131 | if (OutputAssembly) |
| 132 | Passes.add(createPrintModulePass(&Out)); |
| 133 | else if (Force || !CheckBitcodeOutputToConsole(Out, true)) |
| 134 | Passes.add(createBitcodeWriterPass(Out)); |
Dan Gohman | baa2639 | 2009-08-25 15:34:52 +0000 | [diff] [blame] | 135 | |
Chris Lattner | c48e1db | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 136 | Passes.run(*M.get()); |
| 137 | |
Chris Lattner | c48e1db | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 138 | return 0; |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 139 | } |