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