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 | //===----------------------------------------------------------------------===// |
Chris Lattner | 1e2385b | 2003-11-21 21:54:22 +0000 | [diff] [blame] | 9 | |
Chris Lattner | 5113eb0 | 2002-11-19 18:42:59 +0000 | [diff] [blame] | 10 | #include "llvm/Transforms/IPO.h" |
| 11 | #include "llvm/Pass.h" |
| 12 | #include "llvm/Module.h" |
Chris Lattner | 1e2385b | 2003-11-21 21:54:22 +0000 | [diff] [blame] | 13 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 14 | |
Chris Lattner | 5113eb0 | 2002-11-19 18:42:59 +0000 | [diff] [blame] | 15 | namespace { |
| 16 | class FunctionExtractorPass : public Pass { |
| 17 | Function *Named; |
| 18 | public: |
| 19 | FunctionExtractorPass(Function *F = 0) : Named(F) {} |
| 20 | |
| 21 | bool run(Module &M) { |
| 22 | if (Named == 0) { |
| 23 | Named = M.getMainFunction(); |
| 24 | if (Named == 0) return false; // No function to extract |
| 25 | } |
| 26 | |
Misha Brukman | cf00c4a | 2003-10-10 17:57:28 +0000 | [diff] [blame] | 27 | // Make sure our result is globally accessible... |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 28 | Named->setLinkage(GlobalValue::ExternalLinkage); |
Chris Lattner | 5113eb0 | 2002-11-19 18:42:59 +0000 | [diff] [blame] | 29 | |
| 30 | // Mark all global variables internal |
| 31 | for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I) |
| 32 | if (!I->isExternal()) { |
| 33 | I->setInitializer(0); // Make all variables external |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 34 | I->setLinkage(GlobalValue::ExternalLinkage); |
Chris Lattner | 5113eb0 | 2002-11-19 18:42:59 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | // All of the functions may be used by global variables or the named |
| 38 | // function. Loop through them and create a new, external functions that |
| 39 | // can be "used", instead of ones with bodies. |
| 40 | // |
| 41 | std::vector<Function*> NewFunctions; |
| 42 | |
| 43 | Function *Last = &M.back(); // Figure out where the last real fn is... |
| 44 | |
| 45 | for (Module::iterator I = M.begin(); ; ++I) { |
| 46 | if (&*I != Named) { |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 47 | Function *New = new Function(I->getFunctionType(), |
| 48 | GlobalValue::ExternalLinkage, |
| 49 | I->getName()); |
Chris Lattner | 5113eb0 | 2002-11-19 18:42:59 +0000 | [diff] [blame] | 50 | I->setName(""); // Remove Old name |
| 51 | |
| 52 | // If it's not the named function, delete the body of the function |
| 53 | I->dropAllReferences(); |
| 54 | |
| 55 | M.getFunctionList().push_back(New); |
| 56 | NewFunctions.push_back(New); |
| 57 | } |
| 58 | |
| 59 | if (&*I == Last) break; // Stop after processing the last function |
| 60 | } |
| 61 | |
| 62 | // Now that we have replacements all set up, loop through the module, |
| 63 | // deleting the old functions, replacing them with the newly created |
| 64 | // functions. |
| 65 | if (!NewFunctions.empty()) { |
| 66 | unsigned FuncNum = 0; |
| 67 | Module::iterator I = M.begin(); |
| 68 | do { |
| 69 | if (&*I != Named) { |
| 70 | // Make everything that uses the old function use the new dummy fn |
| 71 | I->replaceAllUsesWith(NewFunctions[FuncNum++]); |
| 72 | |
| 73 | Function *Old = I; |
| 74 | ++I; // Move the iterator to the new function |
| 75 | |
| 76 | // Delete the old function! |
| 77 | M.getFunctionList().erase(Old); |
| 78 | |
| 79 | } else { |
| 80 | ++I; // Skip the function we are extracting |
| 81 | } |
| 82 | } while (&*I != NewFunctions[0]); |
| 83 | } |
| 84 | |
| 85 | return true; |
| 86 | } |
| 87 | }; |
| 88 | |
| 89 | RegisterPass<FunctionExtractorPass> X("extract", "Function Extractor"); |
| 90 | } |
| 91 | |
Chris Lattner | 1e2385b | 2003-11-21 21:54:22 +0000 | [diff] [blame] | 92 | Pass *llvm::createFunctionExtractionPass(Function *F) { |
Chris Lattner | 5113eb0 | 2002-11-19 18:42:59 +0000 | [diff] [blame] | 93 | return new FunctionExtractorPass(F); |
| 94 | } |