blob: 8359270471352f0d134286b846247e232ff111a8 [file] [log] [blame]
Chris Lattner579d9142002-05-22 20:27:00 +00001//===----------------------------------------------------------------------===//
2// LLVM extract Utility
3//
4// This utility changes the input module to only contain a single function,
5// which is primarily used for debugging transformations.
6//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/Module.h"
10#include "llvm/PassManager.h"
Brian Gaeke053134a2003-09-10 19:37:04 +000011#include "llvm/Pass.h"
Chris Lattner579d9142002-05-22 20:27:00 +000012#include "llvm/Bytecode/Reader.h"
13#include "llvm/Bytecode/WriteBytecodePass.h"
Chris Lattner33974ca2002-07-23 22:04:40 +000014#include "llvm/Transforms/IPO.h"
Chris Lattner579d9142002-05-22 20:27:00 +000015#include "Support/CommandLine.h"
16#include <memory>
17
Chris Lattner5ff62e92002-07-22 02:10:13 +000018// InputFilename - The filename to read from.
Chris Lattnerc7a09852002-07-25 16:31:09 +000019static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000020InputFilename(cl::Positional, cl::desc("<input bytecode file>"),
21 cl::init("-"), cl::value_desc("filename"));
22
23
24// ExtractFunc - The function to extract from the module... defaults to main.
Chris Lattnerc7a09852002-07-25 16:31:09 +000025static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000026ExtractFunc("func", cl::desc("Specify function to extract"), cl::init("main"),
27 cl::value_desc("function"));
28
Chris Lattner579d9142002-05-22 20:27:00 +000029int main(int argc, char **argv) {
30 cl::ParseCommandLineOptions(argc, argv, " llvm extractor\n");
31
32 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
33 if (M.get() == 0) {
Chris Lattner50e3a202002-07-30 21:43:22 +000034 std::cerr << argv[0] << ": bytecode didn't read correctly.\n";
Chris Lattner579d9142002-05-22 20:27:00 +000035 return 1;
36 }
37
Chris Lattner5113eb02002-11-19 18:42:59 +000038 // Figure out which function we should extract
39 Function *F = M.get()->getNamedFunction(ExtractFunc);
40 if (F == 0) {
41 std::cerr << argv[0] << ": program doesn't contain function named '"
42 << ExtractFunc << "'!\n";
43 return 1;
44 }
45
Chris Lattner579d9142002-05-22 20:27:00 +000046 // In addition to just parsing the input from GCC, we also want to spiff it up
47 // a little bit. Do this now.
48 //
49 PassManager Passes;
Chris Lattner5113eb02002-11-19 18:42:59 +000050 Passes.add(createFunctionExtractionPass(F)); // Extract the function
Chris Lattner579d9142002-05-22 20:27:00 +000051 Passes.add(createGlobalDCEPass()); // Delete unreachable globals
Chris Lattner68ed3182002-10-12 20:50:16 +000052 Passes.add(createFunctionResolvingPass()); // Delete prototypes
Chris Lattner33974ca2002-07-23 22:04:40 +000053 Passes.add(createDeadTypeEliminationPass()); // Remove dead types...
Chris Lattner579d9142002-05-22 20:27:00 +000054 Passes.add(new WriteBytecodePass(&std::cout)); // Write bytecode to file...
55
Chris Lattner7e708292002-06-25 16:13:24 +000056 Passes.run(*M.get());
Chris Lattner579d9142002-05-22 20:27:00 +000057 return 0;
58}