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" |
Chris Lattner | 33974ca | 2002-07-23 22:04:40 +0000 | [diff] [blame^] | 16 | #include "llvm/Transforms/IPO.h" |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 17 | #include "Support/CommandLine.h" |
| 18 | #include <memory> |
| 19 | |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 20 | // InputFilename - The filename to read from. |
| 21 | static cl::opt<string> |
| 22 | InputFilename(cl::Positional, cl::desc("<input bytecode file>"), |
| 23 | cl::init("-"), cl::value_desc("filename")); |
| 24 | |
| 25 | |
| 26 | // ExtractFunc - The function to extract from the module... defaults to main. |
| 27 | static cl::opt<string> |
| 28 | ExtractFunc("func", cl::desc("Specify function to extract"), cl::init("main"), |
| 29 | cl::value_desc("function")); |
| 30 | |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 31 | |
| 32 | struct FunctionExtractorPass : public Pass { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 33 | bool run(Module &M) { |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 34 | // Mark all global variables to be internal |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 35 | for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I) |
| 36 | I->setInternalLinkage(true); |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 37 | |
| 38 | Function *Named = 0; |
| 39 | |
| 40 | // Loop over all of the functions in the module, dropping all references in |
| 41 | // functions that are not the named function. |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 42 | for (Module::iterator I = M.begin(), E = M.end(); I != E;) |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 43 | // Check to see if this is the named function! |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 44 | if (!Named && I->getName() == ExtractFunc) { |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 45 | // Yes, it is. Keep track of it... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 46 | Named = I; |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 47 | |
Chris Lattner | 6a13592 | 2002-05-23 18:36:25 +0000 | [diff] [blame] | 48 | // Make sure it's globally accessable... |
| 49 | Named->setInternalLinkage(false); |
| 50 | |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 51 | // Remove the named function from the module. |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 52 | M.getFunctionList().remove(I); |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 53 | } else { |
| 54 | // 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] | 55 | I->dropAllReferences(); |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 56 | ++I; |
| 57 | } |
| 58 | |
| 59 | // All of the functions that still have uses now must be used by global |
| 60 | // variables or the named function. Loop through them and create a new, |
| 61 | // external function for the used ones... making all uses point to the new |
| 62 | // functions. |
| 63 | std::vector<Function*> NewFunctions; |
| 64 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 65 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 66 | if (!I->use_empty()) { |
| 67 | Function *New = new Function(I->getFunctionType(), false, I->getName()); |
| 68 | I->replaceAllUsesWith(New); |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 69 | NewFunctions.push_back(New); |
| 70 | } |
| 71 | |
| 72 | // Now the module only has unused functions with their references dropped. |
| 73 | // Delete them all now! |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 74 | M.getFunctionList().clear(); |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 75 | |
| 76 | // Re-insert the named function... |
| 77 | if (Named) |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 78 | M.getFunctionList().push_back(Named); |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 79 | else |
| 80 | std::cerr << "Warning: Function '" << ExtractFunc << "' not found!\n"; |
| 81 | |
| 82 | // Insert all of the function stubs... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 83 | M.getFunctionList().insert(M.end(), NewFunctions.begin(), |
| 84 | NewFunctions.end()); |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 85 | return true; |
| 86 | } |
| 87 | }; |
| 88 | |
| 89 | |
Chris Lattner | 33974ca | 2002-07-23 22:04:40 +0000 | [diff] [blame^] | 90 | static RegisterPass<FunctionExtractorPass> X("extract", "Function Extractor"); |
| 91 | |
| 92 | |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 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 |
Chris Lattner | 33974ca | 2002-07-23 22:04:40 +0000 | [diff] [blame^] | 109 | Passes.add(createDeadTypeEliminationPass()); // Remove dead types... |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 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 | } |