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" |
Chris Lattner | 33974ca | 2002-07-23 22:04:40 +0000 | [diff] [blame] | 13 | #include "llvm/Transforms/IPO.h" |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 14 | #include "Support/CommandLine.h" |
| 15 | #include <memory> |
| 16 | |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 17 | // InputFilename - The filename to read from. |
Chris Lattner | c7a0985 | 2002-07-25 16:31:09 +0000 | [diff] [blame] | 18 | static cl::opt<std::string> |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 19 | InputFilename(cl::Positional, cl::desc("<input bytecode file>"), |
| 20 | cl::init("-"), cl::value_desc("filename")); |
| 21 | |
| 22 | |
| 23 | // ExtractFunc - The function to extract from the module... defaults to main. |
Chris Lattner | c7a0985 | 2002-07-25 16:31:09 +0000 | [diff] [blame] | 24 | static cl::opt<std::string> |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 25 | ExtractFunc("func", cl::desc("Specify function to extract"), cl::init("main"), |
| 26 | cl::value_desc("function")); |
| 27 | |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 28 | |
| 29 | struct FunctionExtractorPass : public Pass { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 30 | bool run(Module &M) { |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 31 | // Mark all global variables to be internal |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 32 | for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I) |
Chris Lattner | 68ed318 | 2002-10-12 20:50:16 +0000 | [diff] [blame] | 33 | if (!I->isExternal()) { |
| 34 | I->setInitializer(0); // Make all variables external |
| 35 | I->setInternalLinkage(false); // Make sure it's not internal |
| 36 | } |
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 | 68ed318 | 2002-10-12 20:50:16 +0000 | [diff] [blame] | 42 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 43 | // Check to see if this is the named function! |
Chris Lattner | 7d3f867 | 2002-10-06 21:30:04 +0000 | [diff] [blame] | 44 | if (I->getName() == ExtractFunc && !I->isExternal()) { |
| 45 | if (Named) { // Two functions, same name? |
| 46 | std::cerr << "extract ERROR: Two functions named: '" << ExtractFunc |
| 47 | << "' found!\n"; |
| 48 | exit(1); |
| 49 | } |
| 50 | |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 51 | // Yes, it is. Keep track of it... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 52 | Named = I; |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 53 | |
Chris Lattner | 6a13592 | 2002-05-23 18:36:25 +0000 | [diff] [blame] | 54 | // Make sure it's globally accessable... |
| 55 | Named->setInternalLinkage(false); |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 56 | } |
Chris Lattner | 68ed318 | 2002-10-12 20:50:16 +0000 | [diff] [blame] | 57 | |
| 58 | if (Named == 0) { |
| 59 | std::cerr << "Warning: Function '" << ExtractFunc << "' not found!\n"; |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | // All of the functions may be used by global variables or the named |
| 64 | // function. Loop through them and create a new, external functions that |
| 65 | // can be "used", instead of ones with bodies. |
| 66 | // |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 67 | std::vector<Function*> NewFunctions; |
| 68 | |
Chris Lattner | 68ed318 | 2002-10-12 20:50:16 +0000 | [diff] [blame] | 69 | Function *Last = &M.back(); // Figure out where the last real fn is... |
| 70 | |
| 71 | for (Module::iterator I = M.begin(); ; ++I) { |
| 72 | if (I->getName() != ExtractFunc) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 73 | Function *New = new Function(I->getFunctionType(), false, I->getName()); |
Chris Lattner | 68ed318 | 2002-10-12 20:50:16 +0000 | [diff] [blame] | 74 | I->setName(""); // Remove Old name |
| 75 | |
| 76 | // If it's not the named function, delete the body of the function |
| 77 | I->dropAllReferences(); |
| 78 | |
| 79 | M.getFunctionList().push_back(New); |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 80 | NewFunctions.push_back(New); |
| 81 | } |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 82 | |
Chris Lattner | 68ed318 | 2002-10-12 20:50:16 +0000 | [diff] [blame] | 83 | if (&*I == Last) break; // Stop after processing the last function |
| 84 | } |
| 85 | |
| 86 | // Now that we have replacements all set up, loop through the module, |
| 87 | // deleting the old functions, replacing them with the newly created |
| 88 | // functions. |
| 89 | if (!NewFunctions.empty()) { |
| 90 | unsigned FuncNum = 0; |
| 91 | Module::iterator I = M.begin(); |
| 92 | do { |
| 93 | if (I->getName() != ExtractFunc) { |
| 94 | // Make everything that uses the old function use the new dummy fn |
| 95 | I->replaceAllUsesWith(NewFunctions[FuncNum++]); |
| 96 | |
| 97 | Function *Old = I; |
| 98 | ++I; // Move the iterator to the new function |
| 99 | |
| 100 | // Delete the old function! |
| 101 | M.getFunctionList().erase(Old); |
| 102 | |
| 103 | } else { |
| 104 | ++I; // Skip the function we are extracting |
| 105 | } |
| 106 | } while (&*I != NewFunctions[0]); |
| 107 | } |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 108 | |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 109 | return true; |
| 110 | } |
| 111 | }; |
| 112 | |
| 113 | |
Chris Lattner | 33974ca | 2002-07-23 22:04:40 +0000 | [diff] [blame] | 114 | static RegisterPass<FunctionExtractorPass> X("extract", "Function Extractor"); |
| 115 | |
| 116 | |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 117 | int main(int argc, char **argv) { |
| 118 | cl::ParseCommandLineOptions(argc, argv, " llvm extractor\n"); |
| 119 | |
| 120 | std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename)); |
| 121 | if (M.get() == 0) { |
Chris Lattner | 50e3a20 | 2002-07-30 21:43:22 +0000 | [diff] [blame] | 122 | std::cerr << argv[0] << ": bytecode didn't read correctly.\n"; |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 123 | return 1; |
| 124 | } |
| 125 | |
| 126 | // In addition to just parsing the input from GCC, we also want to spiff it up |
| 127 | // a little bit. Do this now. |
| 128 | // |
| 129 | PassManager Passes; |
| 130 | Passes.add(new FunctionExtractorPass()); |
| 131 | Passes.add(createGlobalDCEPass()); // Delete unreachable globals |
Chris Lattner | 68ed318 | 2002-10-12 20:50:16 +0000 | [diff] [blame] | 132 | Passes.add(createFunctionResolvingPass()); // Delete prototypes |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 133 | Passes.add(createConstantMergePass()); // Merge dup global constants |
Chris Lattner | 33974ca | 2002-07-23 22:04:40 +0000 | [diff] [blame] | 134 | Passes.add(createDeadTypeEliminationPass()); // Remove dead types... |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 135 | Passes.add(new WriteBytecodePass(&std::cout)); // Write bytecode to file... |
| 136 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 137 | Passes.run(*M.get()); |
Chris Lattner | 579d914 | 2002-05-22 20:27:00 +0000 | [diff] [blame] | 138 | return 0; |
| 139 | } |