blob: fd865419c9d586b31b492c814bbd0b314245bbdc [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"
Chris Lattner33974ca2002-07-23 22:04:40 +000013#include "llvm/Transforms/IPO.h"
Chris Lattner579d9142002-05-22 20:27:00 +000014#include "Support/CommandLine.h"
15#include <memory>
16
Chris Lattner5ff62e92002-07-22 02:10:13 +000017// InputFilename - The filename to read from.
Chris Lattnerc7a09852002-07-25 16:31:09 +000018static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000019InputFilename(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.
Chris Lattnerc7a09852002-07-25 16:31:09 +000024static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000025ExtractFunc("func", cl::desc("Specify function to extract"), cl::init("main"),
26 cl::value_desc("function"));
27
Chris Lattner579d9142002-05-22 20:27:00 +000028
29struct FunctionExtractorPass : public Pass {
Chris Lattner7e708292002-06-25 16:13:24 +000030 bool run(Module &M) {
Chris Lattner579d9142002-05-22 20:27:00 +000031 // Mark all global variables to be internal
Chris Lattner7e708292002-06-25 16:13:24 +000032 for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
Chris Lattner68ed3182002-10-12 20:50:16 +000033 if (!I->isExternal()) {
34 I->setInitializer(0); // Make all variables external
35 I->setInternalLinkage(false); // Make sure it's not internal
36 }
Chris Lattner579d9142002-05-22 20:27:00 +000037
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.
Chris Lattner68ed3182002-10-12 20:50:16 +000042 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner579d9142002-05-22 20:27:00 +000043 // Check to see if this is the named function!
Chris Lattner7d3f8672002-10-06 21:30:04 +000044 if (I->getName() == ExtractFunc && !I->isExternal()) {
45 if (Named) { // Two functions, same name?
46 std::cerr << "extract ERROR: Two functions named: '" << ExtractFunc
47 << "' found!\n";
48 exit(1);
49 }
50
Chris Lattner579d9142002-05-22 20:27:00 +000051 // Yes, it is. Keep track of it...
Chris Lattner7e708292002-06-25 16:13:24 +000052 Named = I;
Chris Lattner579d9142002-05-22 20:27:00 +000053
Chris Lattner6a135922002-05-23 18:36:25 +000054 // Make sure it's globally accessable...
55 Named->setInternalLinkage(false);
Chris Lattner579d9142002-05-22 20:27:00 +000056 }
Chris Lattner68ed3182002-10-12 20:50:16 +000057
58 if (Named == 0) {
59 std::cerr << "Warning: Function '" << ExtractFunc << "' not found!\n";
60 return false;
61 }
62
63 // All of the functions may be used by global variables or the named
64 // function. Loop through them and create a new, external functions that
65 // can be "used", instead of ones with bodies.
66 //
Chris Lattner579d9142002-05-22 20:27:00 +000067 std::vector<Function*> NewFunctions;
68
Chris Lattner68ed3182002-10-12 20:50:16 +000069 Function *Last = &M.back(); // Figure out where the last real fn is...
70
71 for (Module::iterator I = M.begin(); ; ++I) {
72 if (I->getName() != ExtractFunc) {
Chris Lattner7e708292002-06-25 16:13:24 +000073 Function *New = new Function(I->getFunctionType(), false, I->getName());
Chris Lattner68ed3182002-10-12 20:50:16 +000074 I->setName(""); // Remove Old name
75
76 // If it's not the named function, delete the body of the function
77 I->dropAllReferences();
78
79 M.getFunctionList().push_back(New);
Chris Lattner579d9142002-05-22 20:27:00 +000080 NewFunctions.push_back(New);
81 }
Chris Lattner579d9142002-05-22 20:27:00 +000082
Chris Lattner68ed3182002-10-12 20:50:16 +000083 if (&*I == Last) break; // Stop after processing the last function
84 }
85
86 // Now that we have replacements all set up, loop through the module,
87 // deleting the old functions, replacing them with the newly created
88 // functions.
89 if (!NewFunctions.empty()) {
90 unsigned FuncNum = 0;
91 Module::iterator I = M.begin();
92 do {
93 if (I->getName() != ExtractFunc) {
94 // Make everything that uses the old function use the new dummy fn
95 I->replaceAllUsesWith(NewFunctions[FuncNum++]);
96
97 Function *Old = I;
98 ++I; // Move the iterator to the new function
99
100 // Delete the old function!
101 M.getFunctionList().erase(Old);
102
103 } else {
104 ++I; // Skip the function we are extracting
105 }
106 } while (&*I != NewFunctions[0]);
107 }
Chris Lattner579d9142002-05-22 20:27:00 +0000108
Chris Lattner579d9142002-05-22 20:27:00 +0000109 return true;
110 }
111};
112
113
Chris Lattner33974ca2002-07-23 22:04:40 +0000114static RegisterPass<FunctionExtractorPass> X("extract", "Function Extractor");
115
116
Chris Lattner579d9142002-05-22 20:27:00 +0000117int main(int argc, char **argv) {
118 cl::ParseCommandLineOptions(argc, argv, " llvm extractor\n");
119
120 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
121 if (M.get() == 0) {
Chris Lattner50e3a202002-07-30 21:43:22 +0000122 std::cerr << argv[0] << ": bytecode didn't read correctly.\n";
Chris Lattner579d9142002-05-22 20:27:00 +0000123 return 1;
124 }
125
126 // In addition to just parsing the input from GCC, we also want to spiff it up
127 // a little bit. Do this now.
128 //
129 PassManager Passes;
130 Passes.add(new FunctionExtractorPass());
131 Passes.add(createGlobalDCEPass()); // Delete unreachable globals
Chris Lattner68ed3182002-10-12 20:50:16 +0000132 Passes.add(createFunctionResolvingPass()); // Delete prototypes
Chris Lattner579d9142002-05-22 20:27:00 +0000133 Passes.add(createConstantMergePass()); // Merge dup global constants
Chris Lattner33974ca2002-07-23 22:04:40 +0000134 Passes.add(createDeadTypeEliminationPass()); // Remove dead types...
Chris Lattner579d9142002-05-22 20:27:00 +0000135 Passes.add(new WriteBytecodePass(&std::cout)); // Write bytecode to file...
136
Chris Lattner7e708292002-06-25 16:13:24 +0000137 Passes.run(*M.get());
Chris Lattner579d9142002-05-22 20:27:00 +0000138 return 0;
139}