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 | |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame^] | 21 | // InputFilename - The filename to read from. |
| 22 | static cl::opt<string> |
| 23 | InputFilename(cl::Positional, cl::desc("<input bytecode file>"), |
| 24 | cl::init("-"), cl::value_desc("filename")); |
| 25 | |
| 26 | |
| 27 | // ExtractFunc - The function to extract from the module... defaults to main. |
| 28 | static cl::opt<string> |
| 29 | ExtractFunc("func", cl::desc("Specify function to extract"), cl::init("main"), |
| 30 | cl::value_desc("function")); |
| 31 | |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 32 | |
| 33 | struct FunctionExtractorPass : public Pass { |
| 34 | const char *getPassName() const { return "Function Extractor"; } |
| 35 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 36 | bool run(Module &M) { |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 37 | // Mark all global variables to be internal |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 38 | for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I) |
| 39 | I->setInternalLinkage(true); |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 40 | |
| 41 | Function *Named = 0; |
| 42 | |
| 43 | // Loop over all of the functions in the module, dropping all references in |
| 44 | // functions that are not the named function. |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 45 | for (Module::iterator I = M.begin(), E = M.end(); I != E;) |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 46 | // Check to see if this is the named function! |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 47 | if (!Named && I->getName() == ExtractFunc) { |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 48 | // Yes, it is. Keep track of it... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 49 | Named = I; |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 50 | |
Chris Lattner | 6a13592 | 2002-05-23 18:36:25 +0000 | [diff] [blame] | 51 | // Make sure it's globally accessable... |
| 52 | Named->setInternalLinkage(false); |
| 53 | |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 54 | // Remove the named function from the module. |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 55 | M.getFunctionList().remove(I); |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 56 | } else { |
| 57 | // 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] | 58 | I->dropAllReferences(); |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 59 | ++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 Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 68 | 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 Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 72 | NewFunctions.push_back(New); |
| 73 | } |
| 74 | |
| 75 | // Now the module only has unused functions with their references dropped. |
| 76 | // Delete them all now! |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 77 | M.getFunctionList().clear(); |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 78 | |
| 79 | // Re-insert the named function... |
| 80 | if (Named) |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 81 | M.getFunctionList().push_back(Named); |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 82 | else |
| 83 | std::cerr << "Warning: Function '" << ExtractFunc << "' not found!\n"; |
| 84 | |
| 85 | // Insert all of the function stubs... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 86 | M.getFunctionList().insert(M.end(), NewFunctions.begin(), |
| 87 | NewFunctions.end()); |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 88 | return true; |
| 89 | } |
| 90 | }; |
| 91 | |
| 92 | |
| 93 | int main(int argc, char **argv) { |
| 94 | cl::ParseCommandLineOptions(argc, argv, " llvm extractor\n"); |
| 95 | |
| 96 | std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename)); |
| 97 | if (M.get() == 0) { |
| 98 | std::cerr << "bytecode didn't read correctly.\n"; |
| 99 | return 1; |
| 100 | } |
| 101 | |
| 102 | // In addition to just parsing the input from GCC, we also want to spiff it up |
| 103 | // a little bit. Do this now. |
| 104 | // |
| 105 | PassManager Passes; |
| 106 | Passes.add(new FunctionExtractorPass()); |
| 107 | Passes.add(createGlobalDCEPass()); // Delete unreachable globals |
| 108 | Passes.add(createConstantMergePass()); // Merge dup global constants |
| 109 | Passes.add(createCleanupGCCOutputPass()); // Fix gccisms |
| 110 | Passes.add(new WriteBytecodePass(&std::cout)); // Write bytecode to file... |
| 111 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 112 | Passes.run(*M.get()); |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 113 | return 0; |
| 114 | } |