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 "llvm/Target/TargetData.h"
21 #include "Support/CommandLine.h"
24 // InputFilename - The filename to read from.
25 static cl::opt<std::string>
26 InputFilename(cl::Positional, cl::desc("<input bytecode file>"),
27 cl::init("-"), cl::value_desc("filename"));
30 // ExtractFunc - The function to extract from the module... defaults to main.
31 static cl::opt<std::string>
32 ExtractFunc("func", cl::desc("Specify function to extract"), cl::init("main"),
33 cl::value_desc("function"));
35 int main(int argc, char **argv) {
36 cl::ParseCommandLineOptions(argc, argv, " llvm extractor\n");
38 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
40 std::cerr << argv[0] << ": bytecode didn't read correctly.\n";
44 // Figure out which function we should extract
45 Function *F = M.get()->getNamedFunction(ExtractFunc);
47 std::cerr << argv[0] << ": program doesn't contain function named '"
48 << ExtractFunc << "'!\n";
52 // In addition to just parsing the input from GCC, we also want to spiff it up
53 // a little bit. Do this now.
56 Passes.add(new TargetData("extract", M.get())); // Use correct TargetData
57 Passes.add(createFunctionExtractionPass(F)); // Extract the function
58 Passes.add(createGlobalDCEPass()); // Delete unreachable globals
59 Passes.add(createFunctionResolvingPass()); // Delete prototypes
60 Passes.add(createDeadTypeEliminationPass()); // Remove dead types...
61 Passes.add(new WriteBytecodePass(&std::cout)); // Write bytecode to file...