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