Chris Lattner | 2148bcb | 2003-09-10 19:42:51 +0000 | [diff] [blame] | 1 | //===- extract.cpp - LLVM function extraction utility ---------------------===// |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 2 | // |
| 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. |
| 7 | // |
| 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" |
| 17 | #include "llvm/Bytecode/Reader.h" |
| 18 | #include "llvm/Bytecode/WriteBytecodePass.h" |
Chris Lattner | 33974ca | 2002-07-23 22:04:40 +0000 | [diff] [blame] | 19 | #include "llvm/Transforms/IPO.h" |
Brian Gaeke | 2040890 | 2003-10-28 22:22:16 +0000 | [diff] [blame] | 20 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 21 | #include "Support/CommandLine.h" |
Chris Lattner | bed85ff | 2004-05-27 05:41:36 +0000 | [diff] [blame] | 22 | #include "llvm/System/Signals.h" |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 23 | #include <memory> |
Chris Lattner | ba9cc1f | 2004-02-18 16:53:59 +0000 | [diff] [blame] | 24 | #include <fstream> |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 25 | using namespace llvm; |
| 26 | |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 27 | // InputFilename - The filename to read from. |
Chris Lattner | c7a0985 | 2002-07-25 16:31:09 +0000 | [diff] [blame] | 28 | static cl::opt<std::string> |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 29 | InputFilename(cl::Positional, cl::desc("<input bytecode file>"), |
| 30 | cl::init("-"), cl::value_desc("filename")); |
| 31 | |
Chris Lattner | ba9cc1f | 2004-02-18 16:53:59 +0000 | [diff] [blame] | 32 | static cl::opt<std::string> |
| 33 | OutputFilename("o", cl::desc("Specify output filename"), |
| 34 | cl::value_desc("filename"), cl::init("-")); |
| 35 | |
| 36 | static cl::opt<bool> |
| 37 | Force("f", cl::desc("Overwrite output files")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 38 | |
Misha Brukman | ca718e4 | 2004-04-22 23:07:39 +0000 | [diff] [blame] | 39 | static cl::opt<bool> |
| 40 | DeleteFn("delete", cl::desc("Delete specified function from Module")); |
| 41 | |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 42 | // ExtractFunc - The function to extract from the module... defaults to main. |
Chris Lattner | c7a0985 | 2002-07-25 16:31:09 +0000 | [diff] [blame] | 43 | static cl::opt<std::string> |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 44 | ExtractFunc("func", cl::desc("Specify function to extract"), cl::init("main"), |
| 45 | cl::value_desc("function")); |
| 46 | |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 47 | int main(int argc, char **argv) { |
| 48 | cl::ParseCommandLineOptions(argc, argv, " llvm extractor\n"); |
Reid Spencer | 9de7b33 | 2004-08-29 19:28:55 +0000 | [diff] [blame^] | 49 | sys::PrintStackTraceOnErrorSignal(); |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 50 | |
| 51 | std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename)); |
| 52 | if (M.get() == 0) { |
Chris Lattner | 50e3a20 | 2002-07-30 21:43:22 +0000 | [diff] [blame] | 53 | std::cerr << argv[0] << ": bytecode didn't read correctly.\n"; |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 54 | return 1; |
| 55 | } |
| 56 | |
Chris Lattner | 5113eb0 | 2002-11-19 18:42:59 +0000 | [diff] [blame] | 57 | // Figure out which function we should extract |
| 58 | Function *F = M.get()->getNamedFunction(ExtractFunc); |
| 59 | if (F == 0) { |
| 60 | std::cerr << argv[0] << ": program doesn't contain function named '" |
| 61 | << ExtractFunc << "'!\n"; |
| 62 | return 1; |
| 63 | } |
| 64 | |
Chris Lattner | ba9cc1f | 2004-02-18 16:53:59 +0000 | [diff] [blame] | 65 | // In addition to deleting all other functions, we also want to spiff it up a |
| 66 | // little bit. Do this now. |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 67 | // |
| 68 | PassManager Passes; |
Brian Gaeke | 2040890 | 2003-10-28 22:22:16 +0000 | [diff] [blame] | 69 | Passes.add(new TargetData("extract", M.get())); // Use correct TargetData |
Misha Brukman | ca718e4 | 2004-04-22 23:07:39 +0000 | [diff] [blame] | 70 | // Either isolate the function or delete it from the Module |
| 71 | Passes.add(createFunctionExtractionPass(F, DeleteFn)); |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 72 | Passes.add(createGlobalDCEPass()); // Delete unreachable globals |
Chris Lattner | 68ed318 | 2002-10-12 20:50:16 +0000 | [diff] [blame] | 73 | Passes.add(createFunctionResolvingPass()); // Delete prototypes |
Chris Lattner | 33974ca | 2002-07-23 22:04:40 +0000 | [diff] [blame] | 74 | Passes.add(createDeadTypeEliminationPass()); // Remove dead types... |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 75 | |
Chris Lattner | ba9cc1f | 2004-02-18 16:53:59 +0000 | [diff] [blame] | 76 | std::ostream *Out = 0; |
| 77 | |
| 78 | if (OutputFilename != "-") { // Not stdout? |
| 79 | if (!Force && std::ifstream(OutputFilename.c_str())) { |
| 80 | // If force is not specified, make sure not to overwrite a file! |
| 81 | std::cerr << argv[0] << ": error opening '" << OutputFilename |
| 82 | << "': file exists!\n" |
| 83 | << "Use -f command line argument to force output\n"; |
| 84 | return 1; |
| 85 | } |
| 86 | Out = new std::ofstream(OutputFilename.c_str()); |
| 87 | } else { // Specified stdout |
| 88 | Out = &std::cout; |
| 89 | } |
| 90 | |
| 91 | Passes.add(new WriteBytecodePass(Out)); // Write bytecode to file... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 92 | Passes.run(*M.get()); |
Chris Lattner | ba9cc1f | 2004-02-18 16:53:59 +0000 | [diff] [blame] | 93 | |
| 94 | if (Out != &std::cout) |
| 95 | delete Out; |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 96 | return 0; |
| 97 | } |