fix file header
[oota-llvm.git] / tools / extract / 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 // This utility changes the input module to only contain a single function,
11 // which is primarily used for debugging transformations.
12 //
13 //===----------------------------------------------------------------------===//
14
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"
21 #include <memory>
22
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"));
27               
28
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"));
33
34 int main(int argc, char **argv) {
35   cl::ParseCommandLineOptions(argc, argv, " llvm extractor\n");
36
37   std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
38   if (M.get() == 0) {
39     std::cerr << argv[0] << ": bytecode didn't read correctly.\n";
40     return 1;
41   }
42
43   // Figure out which function we should extract
44   Function *F = M.get()->getNamedFunction(ExtractFunc);
45   if (F == 0) {
46     std::cerr << argv[0] << ": program doesn't contain function named '"
47               << ExtractFunc << "'!\n";
48     return 1;
49   }
50
51   // In addition to just parsing the input from GCC, we also want to spiff it up
52   // a little bit.  Do this now.
53   //
54   PassManager Passes;
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...
60
61   Passes.run(*M.get());
62   return 0;
63 }