blob: b16b4f609e18b0e6b2e38cf50d76cbeb5acafc36 [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)
33 I->setInternalLinkage(true);
Chris Lattner579d9142002-05-22 20:27:00 +000034
35 Function *Named = 0;
36
37 // Loop over all of the functions in the module, dropping all references in
38 // functions that are not the named function.
Chris Lattner7e708292002-06-25 16:13:24 +000039 for (Module::iterator I = M.begin(), E = M.end(); I != E;)
Chris Lattner579d9142002-05-22 20:27:00 +000040 // Check to see if this is the named function!
Chris Lattner7d3f8672002-10-06 21:30:04 +000041 if (I->getName() == ExtractFunc && !I->isExternal()) {
42 if (Named) { // Two functions, same name?
43 std::cerr << "extract ERROR: Two functions named: '" << ExtractFunc
44 << "' found!\n";
45 exit(1);
46 }
47
Chris Lattner579d9142002-05-22 20:27:00 +000048 // Yes, it is. Keep track of it...
Chris Lattner7e708292002-06-25 16:13:24 +000049 Named = I;
Chris Lattner579d9142002-05-22 20:27:00 +000050
Chris Lattner6a135922002-05-23 18:36:25 +000051 // Make sure it's globally accessable...
52 Named->setInternalLinkage(false);
53
Chris Lattner579d9142002-05-22 20:27:00 +000054 // Remove the named function from the module.
Chris Lattner7e708292002-06-25 16:13:24 +000055 M.getFunctionList().remove(I);
Chris Lattner579d9142002-05-22 20:27:00 +000056 } else {
57 // Nope it's not the named function, delete the body of the function
Chris Lattner7e708292002-06-25 16:13:24 +000058 I->dropAllReferences();
Chris Lattner579d9142002-05-22 20:27:00 +000059 ++I;
60 }
61
62 // All of the functions that still have uses now must be used by global
63 // variables or the named function. Loop through them and create a new,
64 // external function for the used ones... making all uses point to the new
65 // functions.
66 std::vector<Function*> NewFunctions;
67
Chris Lattner7e708292002-06-25 16:13:24 +000068 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
69 if (!I->use_empty()) {
70 Function *New = new Function(I->getFunctionType(), false, I->getName());
71 I->replaceAllUsesWith(New);
Chris Lattner579d9142002-05-22 20:27:00 +000072 NewFunctions.push_back(New);
73 }
74
75 // Now the module only has unused functions with their references dropped.
76 // Delete them all now!
Chris Lattner7e708292002-06-25 16:13:24 +000077 M.getFunctionList().clear();
Chris Lattner579d9142002-05-22 20:27:00 +000078
79 // Re-insert the named function...
80 if (Named)
Chris Lattner7e708292002-06-25 16:13:24 +000081 M.getFunctionList().push_back(Named);
Chris Lattner579d9142002-05-22 20:27:00 +000082 else
83 std::cerr << "Warning: Function '" << ExtractFunc << "' not found!\n";
84
85 // Insert all of the function stubs...
Chris Lattner7e708292002-06-25 16:13:24 +000086 M.getFunctionList().insert(M.end(), NewFunctions.begin(),
87 NewFunctions.end());
Chris Lattner579d9142002-05-22 20:27:00 +000088 return true;
89 }
90};
91
92
Chris Lattner33974ca2002-07-23 22:04:40 +000093static RegisterPass<FunctionExtractorPass> X("extract", "Function Extractor");
94
95
Chris Lattner579d9142002-05-22 20:27:00 +000096int main(int argc, char **argv) {
97 cl::ParseCommandLineOptions(argc, argv, " llvm extractor\n");
98
99 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
100 if (M.get() == 0) {
Chris Lattner50e3a202002-07-30 21:43:22 +0000101 std::cerr << argv[0] << ": bytecode didn't read correctly.\n";
Chris Lattner579d9142002-05-22 20:27:00 +0000102 return 1;
103 }
104
105 // In addition to just parsing the input from GCC, we also want to spiff it up
106 // a little bit. Do this now.
107 //
108 PassManager Passes;
109 Passes.add(new FunctionExtractorPass());
110 Passes.add(createGlobalDCEPass()); // Delete unreachable globals
111 Passes.add(createConstantMergePass()); // Merge dup global constants
Chris Lattner33974ca2002-07-23 22:04:40 +0000112 Passes.add(createDeadTypeEliminationPass()); // Remove dead types...
Chris Lattner579d9142002-05-22 20:27:00 +0000113 Passes.add(new WriteBytecodePass(&std::cout)); // Write bytecode to file...
114
Chris Lattner7e708292002-06-25 16:13:24 +0000115 Passes.run(*M.get());
Chris Lattner579d9142002-05-22 20:27:00 +0000116 return 0;
117}