Put all LLVM code into the llvm namespace, as per bug 109.
[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 // 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 "llvm/Target/TargetData.h"
21 #include "Support/CommandLine.h"
22 #include <memory>
23
24 using namespace llvm;
25
26 // InputFilename - The filename to read from.
27 static cl::opt<std::string>
28 InputFilename(cl::Positional, cl::desc("<input bytecode file>"),
29               cl::init("-"), cl::value_desc("filename"));
30               
31
32 // ExtractFunc - The function to extract from the module... defaults to main.
33 static cl::opt<std::string>
34 ExtractFunc("func", cl::desc("Specify function to extract"), cl::init("main"),
35             cl::value_desc("function"));
36
37 int main(int argc, char **argv) {
38   cl::ParseCommandLineOptions(argc, argv, " llvm extractor\n");
39
40   std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
41   if (M.get() == 0) {
42     std::cerr << argv[0] << ": bytecode didn't read correctly.\n";
43     return 1;
44   }
45
46   // Figure out which function we should extract
47   Function *F = M.get()->getNamedFunction(ExtractFunc);
48   if (F == 0) {
49     std::cerr << argv[0] << ": program doesn't contain function named '"
50               << ExtractFunc << "'!\n";
51     return 1;
52   }
53
54   // In addition to just parsing the input from GCC, we also want to spiff it up
55   // a little bit.  Do this now.
56   //
57   PassManager Passes;
58   Passes.add(new TargetData("extract", M.get())); // Use correct TargetData
59   Passes.add(createFunctionExtractionPass(F));    // Extract the function
60   Passes.add(createGlobalDCEPass());              // Delete unreachable globals
61   Passes.add(createFunctionResolvingPass());      // Delete prototypes
62   Passes.add(createDeadTypeEliminationPass());    // Remove dead types...
63   Passes.add(new WriteBytecodePass(&std::cout));  // Write bytecode to file...
64
65   Passes.run(*M.get());
66   return 0;
67 }