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