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 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source 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 | |
| 15 | #include "llvm/Module.h" |
| 16 | #include "llvm/PassManager.h" |
Chris Lattner | c48e1db | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 17 | #include "llvm/Bitcode/ReaderWriter.h" |
Chris Lattner | 33974ca | 2002-07-23 22:04:40 +0000 | [diff] [blame] | 18 | #include "llvm/Transforms/IPO.h" |
Brian Gaeke | 2040890 | 2003-10-28 22:22:16 +0000 | [diff] [blame] | 19 | #include "llvm/Target/TargetData.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 20 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 21 | #include "llvm/Support/ManagedStatic.h" |
Chris Lattner | c48e1db | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 22 | #include "llvm/Support/MemoryBuffer.h" |
Chris Lattner | bed85ff | 2004-05-27 05:41:36 +0000 | [diff] [blame] | 23 | #include "llvm/System/Signals.h" |
Bill Wendling | 68fe61d | 2006-11-29 00:19:40 +0000 | [diff] [blame] | 24 | #include <iostream> |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 25 | #include <memory> |
Chris Lattner | ba9cc1f | 2004-02-18 16:53:59 +0000 | [diff] [blame] | 26 | #include <fstream> |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 29 | // InputFilename - The filename to read from. |
Chris Lattner | c7a0985 | 2002-07-25 16:31:09 +0000 | [diff] [blame] | 30 | static cl::opt<std::string> |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 31 | InputFilename(cl::Positional, cl::desc("<input bytecode file>"), |
| 32 | cl::init("-"), cl::value_desc("filename")); |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 33 | |
Chris Lattner | ba9cc1f | 2004-02-18 16:53:59 +0000 | [diff] [blame] | 34 | static cl::opt<std::string> |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 35 | OutputFilename("o", cl::desc("Specify output filename"), |
Chris Lattner | ba9cc1f | 2004-02-18 16:53:59 +0000 | [diff] [blame] | 36 | cl::value_desc("filename"), cl::init("-")); |
| 37 | |
| 38 | static cl::opt<bool> |
| 39 | Force("f", cl::desc("Overwrite output files")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 40 | |
Misha Brukman | ca718e4 | 2004-04-22 23:07:39 +0000 | [diff] [blame] | 41 | static cl::opt<bool> |
| 42 | DeleteFn("delete", cl::desc("Delete specified function from Module")); |
| 43 | |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 44 | static cl::opt<bool> |
| 45 | Relink("relink", |
| 46 | cl::desc("Turn external linkage for callees of function to delete")); |
| 47 | |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 48 | // ExtractFunc - The function to extract from the module... defaults to main. |
Chris Lattner | c7a0985 | 2002-07-25 16:31:09 +0000 | [diff] [blame] | 49 | static cl::opt<std::string> |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 50 | ExtractFunc("func", cl::desc("Specify function to extract"), cl::init("main"), |
| 51 | cl::value_desc("function")); |
| 52 | |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 53 | int main(int argc, char **argv) { |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 54 | llvm_shutdown_obj X; // Call llvm_shutdown() on exit. |
Chris Lattner | c48e1db | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 55 | cl::ParseCommandLineOptions(argc, argv, " llvm extractor\n"); |
| 56 | sys::PrintStackTraceOnErrorSignal(); |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 57 | |
Chris Lattner | c48e1db | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 58 | std::auto_ptr<Module> M; |
| 59 | |
Chris Lattner | 065344d | 2007-05-06 23:45:49 +0000 | [diff] [blame^] | 60 | MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename); |
Chris Lattner | 44dadff | 2007-05-06 09:29:57 +0000 | [diff] [blame] | 61 | if (Buffer == 0) { |
| 62 | cerr << "Error reading file '" + InputFilename + "'"; |
| 63 | return 1; |
Chris Lattner | c48e1db | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 64 | } else { |
Chris Lattner | 44dadff | 2007-05-06 09:29:57 +0000 | [diff] [blame] | 65 | M.reset(ParseBitcodeFile(Buffer)); |
Chris Lattner | ba9cc1f | 2004-02-18 16:53:59 +0000 | [diff] [blame] | 66 | } |
Chris Lattner | 44dadff | 2007-05-06 09:29:57 +0000 | [diff] [blame] | 67 | delete Buffer; |
Chris Lattner | c48e1db | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 68 | |
| 69 | if (M.get() == 0) { |
| 70 | cerr << argv[0] << ": bytecode didn't read correctly.\n"; |
| 71 | return 1; |
| 72 | } |
| 73 | |
| 74 | // Figure out which function we should extract |
| 75 | Function *F = M.get()->getFunction(ExtractFunc); |
| 76 | if (F == 0) { |
| 77 | cerr << argv[0] << ": program doesn't contain function named '" |
| 78 | << ExtractFunc << "'!\n"; |
| 79 | return 1; |
| 80 | } |
| 81 | |
| 82 | // In addition to deleting all other functions, we also want to spiff it |
| 83 | // up a little bit. Do this now. |
| 84 | PassManager Passes; |
| 85 | Passes.add(new TargetData(M.get())); // Use correct TargetData |
| 86 | // Either isolate the function or delete it from the Module |
| 87 | Passes.add(createFunctionExtractionPass(F, DeleteFn, Relink)); |
| 88 | if (!DeleteFn) |
| 89 | Passes.add(createGlobalDCEPass()); // Delete unreachable globals |
| 90 | Passes.add(createDeadTypeEliminationPass()); // Remove dead types... |
| 91 | Passes.add(createStripDeadPrototypesPass()); // Remove dead func decls |
| 92 | |
| 93 | std::ostream *Out = 0; |
| 94 | |
| 95 | if (OutputFilename != "-") { // Not stdout? |
| 96 | if (!Force && std::ifstream(OutputFilename.c_str())) { |
| 97 | // If force is not specified, make sure not to overwrite a file! |
| 98 | cerr << argv[0] << ": error opening '" << OutputFilename |
| 99 | << "': file exists!\n" |
| 100 | << "Use -f command line argument to force output\n"; |
| 101 | return 1; |
| 102 | } |
| 103 | std::ios::openmode io_mode = std::ios::out | std::ios::trunc | |
| 104 | std::ios::binary; |
| 105 | Out = new std::ofstream(OutputFilename.c_str(), io_mode); |
| 106 | } else { // Specified stdout |
| 107 | // FIXME: cout is not binary! |
| 108 | Out = &std::cout; |
| 109 | } |
| 110 | |
Chris Lattner | 44dadff | 2007-05-06 09:29:57 +0000 | [diff] [blame] | 111 | Passes.add(CreateBitcodeWriterPass(*Out)); |
Chris Lattner | c48e1db | 2007-05-06 05:13:17 +0000 | [diff] [blame] | 112 | Passes.run(*M.get()); |
| 113 | |
| 114 | if (Out != &std::cout) |
| 115 | delete Out; |
| 116 | return 0; |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 117 | } |