cf5bc90e42def1264fa9081fa60dc8acc0422c81
[oota-llvm.git] / tools / llvm-extract / llvm-extract.cpp
1 //===- extract.cpp - LLVM function extraction utility ---------------------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 // 
8 //===----------------------------------------------------------------------===//
9 // 
10 //
11 // This utility changes the input module to only contain a single function,
12 // which is primarily used for debugging transformations.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "llvm/Module.h"
17 #include "llvm/PassManager.h"
18 #include "llvm/Bytecode/Reader.h"
19 #include "llvm/Bytecode/WriteBytecodePass.h"
20 #include "llvm/Transforms/IPO.h"
21 #include "Support/CommandLine.h"
22 #include <memory>
23
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"));
28               
29
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"));
34
35 int main(int argc, char **argv) {
36   cl::ParseCommandLineOptions(argc, argv, " llvm extractor\n");
37
38   std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
39   if (M.get() == 0) {
40     std::cerr << argv[0] << ": bytecode didn't read correctly.\n";
41     return 1;
42   }
43
44   // Figure out which function we should extract
45   Function *F = M.get()->getNamedFunction(ExtractFunc);
46   if (F == 0) {
47     std::cerr << argv[0] << ": program doesn't contain function named '"
48               << ExtractFunc << "'!\n";
49     return 1;
50   }
51
52   // In addition to just parsing the input from GCC, we also want to spiff it up
53   // a little bit.  Do this now.
54   //
55   PassManager Passes;
56   Passes.add(createFunctionExtractionPass(F));    // Extract the function
57   Passes.add(createGlobalDCEPass());              // Delete unreachable globals
58   Passes.add(createFunctionResolvingPass());      // Delete prototypes
59   Passes.add(createDeadTypeEliminationPass());    // Remove dead types...
60   Passes.add(new WriteBytecodePass(&std::cout));  // Write bytecode to file...
61
62   Passes.run(*M.get());
63   return 0;
64 }