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/GlobalDCE.h"
16 #include "llvm/Transforms/ConstantMerge.h"
17 #include "llvm/Transforms/CleanupGCCOutput.h"
18 #include "Support/CommandLine.h"
21 static cl::String InputFilename("", "Specify input bytecode file", 0, "-");
22 static cl::String ExtractFunc("func", "Specify function to extract", 0, "main");
24 struct FunctionExtractorPass : public Pass {
25 const char *getPassName() const { return "Function Extractor"; }
28 // Mark all global variables to be internal
29 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
30 (*I)->setInternalLinkage(true);
34 // Loop over all of the functions in the module, dropping all references in
35 // functions that are not the named function.
36 for (Module::iterator I = M->begin(), E = M->end(); I != E;)
37 // Check to see if this is the named function!
38 if (!Named && (*I)->getName() == ExtractFunc) {
39 // Yes, it is. Keep track of it...
42 // Remove the named function from the module.
43 M->getFunctionList().remove(I);
46 // Nope it's not the named function, delete the body of the function
47 (*I)->dropAllReferences();
51 // All of the functions that still have uses now must be used by global
52 // variables or the named function. Loop through them and create a new,
53 // external function for the used ones... making all uses point to the new
55 std::vector<Function*> NewFunctions;
57 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
58 if (!(*I)->use_empty()) {
59 Function *New = new Function((*I)->getFunctionType(), false,
61 (*I)->replaceAllUsesWith(New);
62 NewFunctions.push_back(New);
65 // Now the module only has unused functions with their references dropped.
66 // Delete them all now!
67 M->getFunctionList().delete_all();
69 // Re-insert the named function...
71 M->getFunctionList().push_back(Named);
73 std::cerr << "Warning: Function '" << ExtractFunc << "' not found!\n";
75 // Insert all of the function stubs...
76 M->getFunctionList().insert(M->end(), NewFunctions.begin(),
83 int main(int argc, char **argv) {
84 cl::ParseCommandLineOptions(argc, argv, " llvm extractor\n");
86 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
88 std::cerr << "bytecode didn't read correctly.\n";
92 // In addition to just parsing the input from GCC, we also want to spiff it up
93 // a little bit. Do this now.
96 Passes.add(new FunctionExtractorPass());
97 Passes.add(createGlobalDCEPass()); // Delete unreachable globals
98 Passes.add(createConstantMergePass()); // Merge dup global constants
99 Passes.add(createCleanupGCCOutputPass()); // Fix gccisms
100 Passes.add(new WriteBytecodePass(&std::cout)); // Write bytecode to file...