blob: 5df4cb283543258d1996166a21586200c0588a2c [file] [log] [blame]
Chris Lattner055de182002-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 Lattnerda02d412002-07-23 22:04:40 +000013#include "llvm/Transforms/IPO.h"
Chris Lattner055de182002-05-22 20:27:00 +000014#include "Support/CommandLine.h"
15#include <memory>
16
Chris Lattnerf5cad152002-07-22 02:10:13 +000017// InputFilename - The filename to read from.
Chris Lattner64a67272002-07-25 16:31:09 +000018static cl::opt<std::string>
Chris Lattnerf5cad152002-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 Lattner64a67272002-07-25 16:31:09 +000024static cl::opt<std::string>
Chris Lattnerf5cad152002-07-22 02:10:13 +000025ExtractFunc("func", cl::desc("Specify function to extract"), cl::init("main"),
26 cl::value_desc("function"));
27
Chris Lattner055de182002-05-22 20:27:00 +000028
29struct FunctionExtractorPass : public Pass {
Chris Lattner113f4f42002-06-25 16:13:24 +000030 bool run(Module &M) {
Chris Lattner055de182002-05-22 20:27:00 +000031 // Mark all global variables to be internal
Chris Lattner113f4f42002-06-25 16:13:24 +000032 for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
Chris Lattner8e2c1972002-10-07 18:33:53 +000033 if (!I->isExternal())
34 I->setInternalLinkage(true);
Chris Lattner055de182002-05-22 20:27:00 +000035
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.
Chris Lattner113f4f42002-06-25 16:13:24 +000040 for (Module::iterator I = M.begin(), E = M.end(); I != E;)
Chris Lattner055de182002-05-22 20:27:00 +000041 // Check to see if this is the named function!
Chris Lattnerae4cf522002-10-06 21:30:04 +000042 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
Chris Lattner055de182002-05-22 20:27:00 +000049 // Yes, it is. Keep track of it...
Chris Lattner113f4f42002-06-25 16:13:24 +000050 Named = I;
Chris Lattner055de182002-05-22 20:27:00 +000051
Chris Lattnerc17c38f2002-05-23 18:36:25 +000052 // Make sure it's globally accessable...
53 Named->setInternalLinkage(false);
54
Chris Lattner055de182002-05-22 20:27:00 +000055 // Remove the named function from the module.
Chris Lattner113f4f42002-06-25 16:13:24 +000056 M.getFunctionList().remove(I);
Chris Lattner055de182002-05-22 20:27:00 +000057 } else {
58 // Nope it's not the named function, delete the body of the function
Chris Lattner113f4f42002-06-25 16:13:24 +000059 I->dropAllReferences();
Chris Lattner055de182002-05-22 20:27:00 +000060 ++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
Chris Lattner113f4f42002-06-25 16:13:24 +000069 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);
Chris Lattner055de182002-05-22 20:27:00 +000073 NewFunctions.push_back(New);
74 }
75
76 // Now the module only has unused functions with their references dropped.
77 // Delete them all now!
Chris Lattner113f4f42002-06-25 16:13:24 +000078 M.getFunctionList().clear();
Chris Lattner055de182002-05-22 20:27:00 +000079
80 // Re-insert the named function...
81 if (Named)
Chris Lattner113f4f42002-06-25 16:13:24 +000082 M.getFunctionList().push_back(Named);
Chris Lattner055de182002-05-22 20:27:00 +000083 else
84 std::cerr << "Warning: Function '" << ExtractFunc << "' not found!\n";
85
86 // Insert all of the function stubs...
Chris Lattner113f4f42002-06-25 16:13:24 +000087 M.getFunctionList().insert(M.end(), NewFunctions.begin(),
88 NewFunctions.end());
Chris Lattner055de182002-05-22 20:27:00 +000089 return true;
90 }
91};
92
93
Chris Lattnerda02d412002-07-23 22:04:40 +000094static RegisterPass<FunctionExtractorPass> X("extract", "Function Extractor");
95
96
Chris Lattner055de182002-05-22 20:27:00 +000097int 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) {
Chris Lattnerda4c6cc2002-07-30 21:43:22 +0000102 std::cerr << argv[0] << ": bytecode didn't read correctly.\n";
Chris Lattner055de182002-05-22 20:27:00 +0000103 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
Chris Lattnerda02d412002-07-23 22:04:40 +0000113 Passes.add(createDeadTypeEliminationPass()); // Remove dead types...
Chris Lattner055de182002-05-22 20:27:00 +0000114 Passes.add(new WriteBytecodePass(&std::cout)); // Write bytecode to file...
115
Chris Lattner113f4f42002-06-25 16:13:24 +0000116 Passes.run(*M.get());
Chris Lattner055de182002-05-22 20:27:00 +0000117 return 0;
118}