Misha Brukman | 444fdea | 2003-11-17 19:35:17 +0000 | [diff] [blame] | 1 | //===-- ExtractFunction.cpp - Function extraction pass --------------------===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Misha Brukman | 79906c9 | 2004-04-22 22:52:22 +0000 | [diff] [blame] | 9 | // |
| 10 | // This pass extracts |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
Chris Lattner | 1e2385b | 2003-11-21 21:54:22 +0000 | [diff] [blame] | 13 | |
Chris Lattner | 5113eb0 | 2002-11-19 18:42:59 +0000 | [diff] [blame] | 14 | #include "llvm/Module.h" |
Misha Brukman | 79906c9 | 2004-04-22 22:52:22 +0000 | [diff] [blame] | 15 | #include "llvm/Pass.h" |
| 16 | #include "llvm/Transforms/IPO.h" |
Chris Lattner | 1e2385b | 2003-11-21 21:54:22 +0000 | [diff] [blame] | 17 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 18 | |
Chris Lattner | 5113eb0 | 2002-11-19 18:42:59 +0000 | [diff] [blame] | 19 | namespace { |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 20 | class FunctionExtractorPass : public ModulePass { |
Chris Lattner | 5113eb0 | 2002-11-19 18:42:59 +0000 | [diff] [blame] | 21 | Function *Named; |
Misha Brukman | 7d24839 | 2004-04-22 23:00:51 +0000 | [diff] [blame] | 22 | bool deleteFunc; |
Chris Lattner | 5113eb0 | 2002-11-19 18:42:59 +0000 | [diff] [blame] | 23 | public: |
Misha Brukman | 7d24839 | 2004-04-22 23:00:51 +0000 | [diff] [blame] | 24 | /// FunctionExtractorPass - If deleteFn is true, this pass deletes as the |
| 25 | /// specified function. Otherwise, it deletes as much of the module as |
| 26 | /// possible, except for the function specified. |
Misha Brukman | 79906c9 | 2004-04-22 22:52:22 +0000 | [diff] [blame] | 27 | /// |
Misha Brukman | 7d24839 | 2004-04-22 23:00:51 +0000 | [diff] [blame] | 28 | FunctionExtractorPass(Function *F = 0, bool deleteFn = true) |
| 29 | : Named(F), deleteFunc(deleteFn) {} |
Chris Lattner | 5113eb0 | 2002-11-19 18:42:59 +0000 | [diff] [blame] | 30 | |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 31 | bool runOnModule(Module &M) { |
Chris Lattner | 5113eb0 | 2002-11-19 18:42:59 +0000 | [diff] [blame] | 32 | if (Named == 0) { |
| 33 | Named = M.getMainFunction(); |
| 34 | if (Named == 0) return false; // No function to extract |
| 35 | } |
| 36 | |
Misha Brukman | 7d24839 | 2004-04-22 23:00:51 +0000 | [diff] [blame] | 37 | if (deleteFunc) |
Misha Brukman | 79906c9 | 2004-04-22 22:52:22 +0000 | [diff] [blame] | 38 | return deleteFunction(); |
Misha Brukman | 7d24839 | 2004-04-22 23:00:51 +0000 | [diff] [blame] | 39 | else |
| 40 | return isolateFunction(M); |
Misha Brukman | 79906c9 | 2004-04-22 22:52:22 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | bool deleteFunction() { |
| 44 | Named->setLinkage(GlobalValue::ExternalLinkage); |
| 45 | Named->deleteBody(); |
| 46 | assert(Named->isExternal() && "This didn't make the function external!"); |
| 47 | return true; |
| 48 | } |
| 49 | |
| 50 | bool isolateFunction(Module &M) { |
Misha Brukman | cf00c4a | 2003-10-10 17:57:28 +0000 | [diff] [blame] | 51 | // Make sure our result is globally accessible... |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 52 | Named->setLinkage(GlobalValue::ExternalLinkage); |
Chris Lattner | 5113eb0 | 2002-11-19 18:42:59 +0000 | [diff] [blame] | 53 | |
| 54 | // Mark all global variables internal |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame^] | 55 | for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I) |
Chris Lattner | 5113eb0 | 2002-11-19 18:42:59 +0000 | [diff] [blame] | 56 | if (!I->isExternal()) { |
| 57 | I->setInitializer(0); // Make all variables external |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 58 | I->setLinkage(GlobalValue::ExternalLinkage); |
Chris Lattner | 5113eb0 | 2002-11-19 18:42:59 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | // All of the functions may be used by global variables or the named |
| 62 | // function. Loop through them and create a new, external functions that |
| 63 | // can be "used", instead of ones with bodies. |
Chris Lattner | 5113eb0 | 2002-11-19 18:42:59 +0000 | [diff] [blame] | 64 | std::vector<Function*> NewFunctions; |
| 65 | |
| 66 | Function *Last = &M.back(); // Figure out where the last real fn is... |
| 67 | |
| 68 | for (Module::iterator I = M.begin(); ; ++I) { |
| 69 | if (&*I != Named) { |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 70 | Function *New = new Function(I->getFunctionType(), |
| 71 | GlobalValue::ExternalLinkage, |
| 72 | I->getName()); |
Chris Lattner | 5113eb0 | 2002-11-19 18:42:59 +0000 | [diff] [blame] | 73 | I->setName(""); // Remove Old name |
| 74 | |
| 75 | // If it's not the named function, delete the body of the function |
| 76 | I->dropAllReferences(); |
| 77 | |
| 78 | M.getFunctionList().push_back(New); |
| 79 | NewFunctions.push_back(New); |
| 80 | } |
| 81 | |
| 82 | if (&*I == Last) break; // Stop after processing the last function |
| 83 | } |
| 84 | |
| 85 | // Now that we have replacements all set up, loop through the module, |
| 86 | // deleting the old functions, replacing them with the newly created |
| 87 | // functions. |
| 88 | if (!NewFunctions.empty()) { |
| 89 | unsigned FuncNum = 0; |
| 90 | Module::iterator I = M.begin(); |
| 91 | do { |
| 92 | if (&*I != Named) { |
| 93 | // Make everything that uses the old function use the new dummy fn |
| 94 | I->replaceAllUsesWith(NewFunctions[FuncNum++]); |
| 95 | |
| 96 | Function *Old = I; |
| 97 | ++I; // Move the iterator to the new function |
| 98 | |
| 99 | // Delete the old function! |
| 100 | M.getFunctionList().erase(Old); |
| 101 | |
| 102 | } else { |
| 103 | ++I; // Skip the function we are extracting |
| 104 | } |
| 105 | } while (&*I != NewFunctions[0]); |
| 106 | } |
| 107 | |
| 108 | return true; |
| 109 | } |
| 110 | }; |
| 111 | |
| 112 | RegisterPass<FunctionExtractorPass> X("extract", "Function Extractor"); |
| 113 | } |
| 114 | |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 115 | ModulePass *llvm::createFunctionExtractionPass(Function *F, bool deleteFn) { |
Misha Brukman | 7d24839 | 2004-04-22 23:00:51 +0000 | [diff] [blame] | 116 | return new FunctionExtractorPass(F, deleteFn); |
Chris Lattner | 5113eb0 | 2002-11-19 18:42:59 +0000 | [diff] [blame] | 117 | } |