b480e13748506787b60a82a7335fea60e8abcbab
[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/Bytecode/Reader.h"
12 #include "llvm/Bytecode/WriteBytecodePass.h"
13 #include "llvm/GlobalVariable.h"
14 #include "llvm/Function.h"
15 #include "llvm/Transforms/IPO/GlobalDCE.h"
16 #include "llvm/Transforms/IPO.h"
17 #include "Support/CommandLine.h"
18 #include <memory>
19
20 // InputFilename - The filename to read from.
21 static cl::opt<string>
22 InputFilename(cl::Positional, cl::desc("<input bytecode file>"),
23               cl::init("-"), cl::value_desc("filename"));
24               
25
26 // ExtractFunc - The function to extract from the module... defaults to main.
27 static cl::opt<string>
28 ExtractFunc("func", cl::desc("Specify function to extract"), cl::init("main"),
29             cl::value_desc("function"));
30
31
32 struct FunctionExtractorPass : public Pass {
33   bool run(Module &M) {
34     // Mark all global variables to be internal
35     for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
36       I->setInternalLinkage(true);
37
38     Function *Named = 0;
39
40     // Loop over all of the functions in the module, dropping all references in
41     // functions that are not the named function.
42     for (Module::iterator I = M.begin(), E = M.end(); I != E;)
43       // Check to see if this is the named function!
44       if (!Named && I->getName() == ExtractFunc) {
45         // Yes, it is.  Keep track of it...
46         Named = I;
47
48         // Make sure it's globally accessable...
49         Named->setInternalLinkage(false);
50
51         // Remove the named function from the module.
52         M.getFunctionList().remove(I);
53       } else {
54         // Nope it's not the named function, delete the body of the function
55         I->dropAllReferences();
56         ++I;
57       }
58
59     // All of the functions that still have uses now must be used by global
60     // variables or the named function.  Loop through them and create a new,
61     // external function for the used ones... making all uses point to the new
62     // functions.
63     std::vector<Function*> NewFunctions;
64     
65     for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
66       if (!I->use_empty()) {
67         Function *New = new Function(I->getFunctionType(), false, I->getName());
68         I->replaceAllUsesWith(New);
69         NewFunctions.push_back(New);
70       }
71     
72     // Now the module only has unused functions with their references dropped.
73     // Delete them all now!
74     M.getFunctionList().clear();
75
76     // Re-insert the named function...
77     if (Named)
78       M.getFunctionList().push_back(Named);
79     else
80       std::cerr << "Warning: Function '" << ExtractFunc << "' not found!\n";
81     
82     // Insert all of the function stubs...
83     M.getFunctionList().insert(M.end(), NewFunctions.begin(),
84                                NewFunctions.end());
85     return true;
86   }
87 };
88
89
90 static RegisterPass<FunctionExtractorPass> X("extract", "Function Extractor");
91
92
93 int main(int argc, char **argv) {
94   cl::ParseCommandLineOptions(argc, argv, " llvm extractor\n");
95
96   std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
97   if (M.get() == 0) {
98     std::cerr << "bytecode didn't read correctly.\n";
99     return 1;
100   }
101
102   // In addition to just parsing the input from GCC, we also want to spiff it up
103   // a little bit.  Do this now.
104   //
105   PassManager Passes;
106   Passes.add(new FunctionExtractorPass());
107   Passes.add(createGlobalDCEPass());              // Delete unreachable globals
108   Passes.add(createConstantMergePass());          // Merge dup global constants
109   Passes.add(createDeadTypeEliminationPass());    // Remove dead types...
110   Passes.add(new WriteBytecodePass(&std::cout));  // Write bytecode to file...
111
112   Passes.run(*M.get());
113   return 0;
114 }