1 //===----------------------------------------------------------------------===//
2 // LLVM extract Utility
4 // This utility changes the input module to only contain a single function,
5 // which is primarily used for debugging transformations.
7 //===----------------------------------------------------------------------===//
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.h"
16 #include "Support/CommandLine.h"
19 // InputFilename - The filename to read from.
20 static cl::opt<std::string>
21 InputFilename(cl::Positional, cl::desc("<input bytecode file>"),
22 cl::init("-"), cl::value_desc("filename"));
25 // ExtractFunc - The function to extract from the module... defaults to main.
26 static cl::opt<std::string>
27 ExtractFunc("func", cl::desc("Specify function to extract"), cl::init("main"),
28 cl::value_desc("function"));
31 struct FunctionExtractorPass : public Pass {
33 // Mark all global variables to be internal
34 for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
35 I->setInternalLinkage(true);
39 // Loop over all of the functions in the module, dropping all references in
40 // functions that are not the named function.
41 for (Module::iterator I = M.begin(), E = M.end(); I != E;)
42 // Check to see if this is the named function!
43 if (!Named && I->getName() == ExtractFunc) {
44 // Yes, it is. Keep track of it...
47 // Make sure it's globally accessable...
48 Named->setInternalLinkage(false);
50 // Remove the named function from the module.
51 M.getFunctionList().remove(I);
53 // Nope it's not the named function, delete the body of the function
54 I->dropAllReferences();
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
62 std::vector<Function*> NewFunctions;
64 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);
68 NewFunctions.push_back(New);
71 // Now the module only has unused functions with their references dropped.
72 // Delete them all now!
73 M.getFunctionList().clear();
75 // Re-insert the named function...
77 M.getFunctionList().push_back(Named);
79 std::cerr << "Warning: Function '" << ExtractFunc << "' not found!\n";
81 // Insert all of the function stubs...
82 M.getFunctionList().insert(M.end(), NewFunctions.begin(),
89 static RegisterPass<FunctionExtractorPass> X("extract", "Function Extractor");
92 int main(int argc, char **argv) {
93 cl::ParseCommandLineOptions(argc, argv, " llvm extractor\n");
95 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
97 std::cerr << "bytecode didn't read correctly.\n";
101 // In addition to just parsing the input from GCC, we also want to spiff it up
102 // a little bit. Do this now.
105 Passes.add(new FunctionExtractorPass());
106 Passes.add(createGlobalDCEPass()); // Delete unreachable globals
107 Passes.add(createConstantMergePass()); // Merge dup global constants
108 Passes.add(createDeadTypeEliminationPass()); // Remove dead types...
109 Passes.add(new WriteBytecodePass(&std::cout)); // Write bytecode to file...
111 Passes.run(*M.get());