Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 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" |
| 15 | #include "llvm/Transforms/IPO/GlobalDCE.h" |
| 16 | #include "llvm/Transforms/ConstantMerge.h" |
| 17 | #include "llvm/Transforms/CleanupGCCOutput.h" |
| 18 | #include "Support/CommandLine.h" |
| 19 | #include <memory> |
| 20 | |
| 21 | static cl::String InputFilename("", "Specify input bytecode file", 0, "-"); |
| 22 | static cl::String ExtractFunc("func", "Specify function to extract", 0, "main"); |
| 23 | |
| 24 | struct FunctionExtractorPass : public Pass { |
| 25 | const char *getPassName() const { return "Function Extractor"; } |
| 26 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 27 | bool run(Module &M) { |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 28 | // Mark all global variables to be internal |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 29 | for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I) |
| 30 | I->setInternalLinkage(true); |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 31 | |
| 32 | Function *Named = 0; |
| 33 | |
| 34 | // Loop over all of the functions in the module, dropping all references in |
| 35 | // functions that are not the named function. |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 36 | for (Module::iterator I = M.begin(), E = M.end(); I != E;) |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 37 | // Check to see if this is the named function! |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 38 | if (!Named && I->getName() == ExtractFunc) { |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 39 | // Yes, it is. Keep track of it... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 40 | Named = I; |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 41 | |
Chris Lattner | 6a13592 | 2002-05-23 18:36:25 +0000 | [diff] [blame] | 42 | // Make sure it's globally accessable... |
| 43 | Named->setInternalLinkage(false); |
| 44 | |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 45 | // Remove the named function from the module. |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 46 | M.getFunctionList().remove(I); |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 47 | } else { |
| 48 | // Nope it's not the named function, delete the body of the function |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 49 | I->dropAllReferences(); |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 50 | ++I; |
| 51 | } |
| 52 | |
| 53 | // All of the functions that still have uses now must be used by global |
| 54 | // variables or the named function. Loop through them and create a new, |
| 55 | // external function for the used ones... making all uses point to the new |
| 56 | // functions. |
| 57 | std::vector<Function*> NewFunctions; |
| 58 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 59 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 60 | if (!I->use_empty()) { |
| 61 | Function *New = new Function(I->getFunctionType(), false, I->getName()); |
| 62 | I->replaceAllUsesWith(New); |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 63 | NewFunctions.push_back(New); |
| 64 | } |
| 65 | |
| 66 | // Now the module only has unused functions with their references dropped. |
| 67 | // Delete them all now! |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 68 | M.getFunctionList().clear(); |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 69 | |
| 70 | // Re-insert the named function... |
| 71 | if (Named) |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 72 | M.getFunctionList().push_back(Named); |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 73 | else |
| 74 | std::cerr << "Warning: Function '" << ExtractFunc << "' not found!\n"; |
| 75 | |
| 76 | // Insert all of the function stubs... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 77 | M.getFunctionList().insert(M.end(), NewFunctions.begin(), |
| 78 | NewFunctions.end()); |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 79 | return true; |
| 80 | } |
| 81 | }; |
| 82 | |
| 83 | |
| 84 | int main(int argc, char **argv) { |
| 85 | cl::ParseCommandLineOptions(argc, argv, " llvm extractor\n"); |
| 86 | |
| 87 | std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename)); |
| 88 | if (M.get() == 0) { |
| 89 | std::cerr << "bytecode didn't read correctly.\n"; |
| 90 | return 1; |
| 91 | } |
| 92 | |
| 93 | // In addition to just parsing the input from GCC, we also want to spiff it up |
| 94 | // a little bit. Do this now. |
| 95 | // |
| 96 | PassManager Passes; |
| 97 | Passes.add(new FunctionExtractorPass()); |
| 98 | Passes.add(createGlobalDCEPass()); // Delete unreachable globals |
| 99 | Passes.add(createConstantMergePass()); // Merge dup global constants |
| 100 | Passes.add(createCleanupGCCOutputPass()); // Fix gccisms |
| 101 | Passes.add(new WriteBytecodePass(&std::cout)); // Write bytecode to file... |
| 102 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 103 | Passes.run(*M.get()); |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 104 | return 0; |
| 105 | } |