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