1 //===----------------------------------------------------------------------===//
2 // LLVM extract Utility
4 // This utility changes the input module to only contain a single function,
5 // which is primarily used for debugging transformations.
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Module.h"
10 #include "llvm/PassManager.h"
11 #include "llvm/Bytecode/Reader.h"
12 #include "llvm/Bytecode/WriteBytecodePass.h"
13 #include "llvm/Transforms/IPO.h"
14 #include "Support/CommandLine.h"
17 // InputFilename - The filename to read from.
18 static cl::opt<std::string>
19 InputFilename(cl::Positional, cl::desc("<input bytecode file>"),
20 cl::init("-"), cl::value_desc("filename"));
23 // ExtractFunc - The function to extract from the module... defaults to main.
24 static cl::opt<std::string>
25 ExtractFunc("func", cl::desc("Specify function to extract"), cl::init("main"),
26 cl::value_desc("function"));
28 int main(int argc, char **argv) {
29 cl::ParseCommandLineOptions(argc, argv, " llvm extractor\n");
31 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
33 std::cerr << argv[0] << ": bytecode didn't read correctly.\n";
37 // Figure out which function we should extract
38 Function *F = M.get()->getNamedFunction(ExtractFunc);
40 std::cerr << argv[0] << ": program doesn't contain function named '"
41 << ExtractFunc << "'!\n";
45 // In addition to just parsing the input from GCC, we also want to spiff it up
46 // a little bit. Do this now.
49 Passes.add(createFunctionExtractionPass(F)); // Extract the function
50 Passes.add(createGlobalDCEPass()); // Delete unreachable globals
51 Passes.add(createFunctionResolvingPass()); // Delete prototypes
52 Passes.add(createDeadTypeEliminationPass()); // Remove dead types...
53 Passes.add(new WriteBytecodePass(&std::cout)); // Write bytecode to file...