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