blob: 93e5fc1b112870b81b203d1bff5b8ff301b9450b [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"
Chris Lattner33974ca2002-07-23 22:04:40 +000015#include "llvm/Transforms/IPO.h"
Chris Lattner579d9142002-05-22 20:27:00 +000016#include "Support/CommandLine.h"
17#include <memory>
18
Chris Lattner5ff62e92002-07-22 02:10:13 +000019// InputFilename - The filename to read from.
Chris Lattnerc7a09852002-07-25 16:31:09 +000020static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000021InputFilename(cl::Positional, cl::desc("<input bytecode file>"),
22 cl::init("-"), cl::value_desc("filename"));
23
24
25// ExtractFunc - The function to extract from the module... defaults to main.
Chris Lattnerc7a09852002-07-25 16:31:09 +000026static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000027ExtractFunc("func", cl::desc("Specify function to extract"), cl::init("main"),
28 cl::value_desc("function"));
29
Chris Lattner579d9142002-05-22 20:27:00 +000030
31struct FunctionExtractorPass : public Pass {
Chris Lattner7e708292002-06-25 16:13:24 +000032 bool run(Module &M) {
Chris Lattner579d9142002-05-22 20:27:00 +000033 // Mark all global variables to be internal
Chris Lattner7e708292002-06-25 16:13:24 +000034 for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
35 I->setInternalLinkage(true);
Chris Lattner579d9142002-05-22 20:27:00 +000036
37 Function *Named = 0;
38
39 // Loop over all of the functions in the module, dropping all references in
40 // functions that are not the named function.
Chris Lattner7e708292002-06-25 16:13:24 +000041 for (Module::iterator I = M.begin(), E = M.end(); I != E;)
Chris Lattner579d9142002-05-22 20:27:00 +000042 // Check to see if this is the named function!
Chris Lattner7e708292002-06-25 16:13:24 +000043 if (!Named && I->getName() == ExtractFunc) {
Chris Lattner579d9142002-05-22 20:27:00 +000044 // Yes, it is. Keep track of it...
Chris Lattner7e708292002-06-25 16:13:24 +000045 Named = I;
Chris Lattner579d9142002-05-22 20:27:00 +000046
Chris Lattner6a135922002-05-23 18:36:25 +000047 // Make sure it's globally accessable...
48 Named->setInternalLinkage(false);
49
Chris Lattner579d9142002-05-22 20:27:00 +000050 // Remove the named function from the module.
Chris Lattner7e708292002-06-25 16:13:24 +000051 M.getFunctionList().remove(I);
Chris Lattner579d9142002-05-22 20:27:00 +000052 } else {
53 // Nope it's not the named function, delete the body of the function
Chris Lattner7e708292002-06-25 16:13:24 +000054 I->dropAllReferences();
Chris Lattner579d9142002-05-22 20:27:00 +000055 ++I;
56 }
57
58 // All of the functions that still have uses now must be used by global
59 // variables or the named function. Loop through them and create a new,
60 // external function for the used ones... making all uses point to the new
61 // functions.
62 std::vector<Function*> NewFunctions;
63
Chris Lattner7e708292002-06-25 16:13:24 +000064 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
65 if (!I->use_empty()) {
66 Function *New = new Function(I->getFunctionType(), false, I->getName());
67 I->replaceAllUsesWith(New);
Chris Lattner579d9142002-05-22 20:27:00 +000068 NewFunctions.push_back(New);
69 }
70
71 // Now the module only has unused functions with their references dropped.
72 // Delete them all now!
Chris Lattner7e708292002-06-25 16:13:24 +000073 M.getFunctionList().clear();
Chris Lattner579d9142002-05-22 20:27:00 +000074
75 // Re-insert the named function...
76 if (Named)
Chris Lattner7e708292002-06-25 16:13:24 +000077 M.getFunctionList().push_back(Named);
Chris Lattner579d9142002-05-22 20:27:00 +000078 else
79 std::cerr << "Warning: Function '" << ExtractFunc << "' not found!\n";
80
81 // Insert all of the function stubs...
Chris Lattner7e708292002-06-25 16:13:24 +000082 M.getFunctionList().insert(M.end(), NewFunctions.begin(),
83 NewFunctions.end());
Chris Lattner579d9142002-05-22 20:27:00 +000084 return true;
85 }
86};
87
88
Chris Lattner33974ca2002-07-23 22:04:40 +000089static RegisterPass<FunctionExtractorPass> X("extract", "Function Extractor");
90
91
Chris Lattner579d9142002-05-22 20:27:00 +000092int main(int argc, char **argv) {
93 cl::ParseCommandLineOptions(argc, argv, " llvm extractor\n");
94
95 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
96 if (M.get() == 0) {
97 std::cerr << "bytecode didn't read correctly.\n";
98 return 1;
99 }
100
101 // In addition to just parsing the input from GCC, we also want to spiff it up
102 // a little bit. Do this now.
103 //
104 PassManager Passes;
105 Passes.add(new FunctionExtractorPass());
106 Passes.add(createGlobalDCEPass()); // Delete unreachable globals
107 Passes.add(createConstantMergePass()); // Merge dup global constants
Chris Lattner33974ca2002-07-23 22:04:40 +0000108 Passes.add(createDeadTypeEliminationPass()); // Remove dead types...
Chris Lattner579d9142002-05-22 20:27:00 +0000109 Passes.add(new WriteBytecodePass(&std::cout)); // Write bytecode to file...
110
Chris Lattner7e708292002-06-25 16:13:24 +0000111 Passes.run(*M.get());
Chris Lattner579d9142002-05-22 20:27:00 +0000112 return 0;
113}