Misha Brukman | 444fdea | 2003-11-17 19:35:17 +0000 | [diff] [blame] | 1 | //===-- ExtractFunction.cpp - Function extraction pass --------------------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 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. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 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 | |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 14 | #include "llvm/Instructions.h" |
Chris Lattner | 5113eb0 | 2002-11-19 18:42:59 +0000 | [diff] [blame] | 15 | #include "llvm/Module.h" |
Misha Brukman | 79906c9 | 2004-04-22 22:52:22 +0000 | [diff] [blame] | 16 | #include "llvm/Pass.h" |
| 17 | #include "llvm/Transforms/IPO.h" |
Reid Spencer | 9133fe2 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Compiler.h" |
Chris Lattner | 1e2385b | 2003-11-21 21:54:22 +0000 | [diff] [blame] | 19 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 20 | |
Chris Lattner | 5113eb0 | 2002-11-19 18:42:59 +0000 | [diff] [blame] | 21 | namespace { |
Reid Spencer | 9133fe2 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 22 | /// @brief A pass to extract specific functions and their dependencies. |
| 23 | class VISIBILITY_HIDDEN FunctionExtractorPass : public ModulePass { |
Chris Lattner | 5113eb0 | 2002-11-19 18:42:59 +0000 | [diff] [blame] | 24 | Function *Named; |
Misha Brukman | 7d24839 | 2004-04-22 23:00:51 +0000 | [diff] [blame] | 25 | bool deleteFunc; |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 26 | bool reLink; |
Chris Lattner | 5113eb0 | 2002-11-19 18:42:59 +0000 | [diff] [blame] | 27 | public: |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame^] | 28 | static char ID; // Pass identifcation, replacement for typeid |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 29 | |
Misha Brukman | 7d24839 | 2004-04-22 23:00:51 +0000 | [diff] [blame] | 30 | /// FunctionExtractorPass - If deleteFn is true, this pass deletes as the |
| 31 | /// specified function. Otherwise, it deletes as much of the module as |
| 32 | /// possible, except for the function specified. |
Misha Brukman | 79906c9 | 2004-04-22 22:52:22 +0000 | [diff] [blame] | 33 | /// |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 34 | FunctionExtractorPass(Function *F = 0, bool deleteFn = true, |
| 35 | bool relinkCallees = false) |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 36 | : ModulePass((intptr_t)&ID), Named(F), deleteFunc(deleteFn), |
| 37 | reLink(relinkCallees) {} |
Chris Lattner | 5113eb0 | 2002-11-19 18:42:59 +0000 | [diff] [blame] | 38 | |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 39 | bool runOnModule(Module &M) { |
Chris Lattner | 5113eb0 | 2002-11-19 18:42:59 +0000 | [diff] [blame] | 40 | if (Named == 0) { |
Reid Spencer | 688b049 | 2007-02-05 21:19:13 +0000 | [diff] [blame] | 41 | Named = M.getFunction("main"); |
Chris Lattner | 5113eb0 | 2002-11-19 18:42:59 +0000 | [diff] [blame] | 42 | if (Named == 0) return false; // No function to extract |
| 43 | } |
Chris Lattner | 9dcbf0d | 2006-10-20 21:35:41 +0000 | [diff] [blame] | 44 | |
Misha Brukman | 7d24839 | 2004-04-22 23:00:51 +0000 | [diff] [blame] | 45 | if (deleteFunc) |
Misha Brukman | 79906c9 | 2004-04-22 22:52:22 +0000 | [diff] [blame] | 46 | return deleteFunction(); |
Chris Lattner | 9dcbf0d | 2006-10-20 21:35:41 +0000 | [diff] [blame] | 47 | M.setModuleInlineAsm(""); |
| 48 | return isolateFunction(M); |
Misha Brukman | 79906c9 | 2004-04-22 22:52:22 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | bool deleteFunction() { |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 52 | // If we're in relinking mode, set linkage of all internal callees to |
| 53 | // external. This will allow us extract function, and then - link |
| 54 | // everything together |
| 55 | if (reLink) { |
| 56 | for (Function::iterator B = Named->begin(), BE = Named->end(); |
| 57 | B != BE; ++B) { |
| 58 | for (BasicBlock::iterator I = B->begin(), E = B->end(); |
| 59 | I != E; ++I) { |
| 60 | if (CallInst* callInst = dyn_cast<CallInst>(&*I)) { |
| 61 | Function* Callee = callInst->getCalledFunction(); |
| 62 | if (Callee && Callee->hasInternalLinkage()) |
| 63 | Callee->setLinkage(GlobalValue::ExternalLinkage); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
Misha Brukman | 79906c9 | 2004-04-22 22:52:22 +0000 | [diff] [blame] | 69 | Named->setLinkage(GlobalValue::ExternalLinkage); |
| 70 | Named->deleteBody(); |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 71 | assert(Named->isDeclaration() && "This didn't make the function external!"); |
Misha Brukman | 79906c9 | 2004-04-22 22:52:22 +0000 | [diff] [blame] | 72 | return true; |
| 73 | } |
| 74 | |
| 75 | bool isolateFunction(Module &M) { |
Misha Brukman | cf00c4a | 2003-10-10 17:57:28 +0000 | [diff] [blame] | 76 | // Make sure our result is globally accessible... |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 77 | Named->setLinkage(GlobalValue::ExternalLinkage); |
Chris Lattner | 5113eb0 | 2002-11-19 18:42:59 +0000 | [diff] [blame] | 78 | |
| 79 | // Mark all global variables internal |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 80 | for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I) |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 81 | if (!I->isDeclaration()) { |
Chris Lattner | 5113eb0 | 2002-11-19 18:42:59 +0000 | [diff] [blame] | 82 | I->setInitializer(0); // Make all variables external |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 83 | I->setLinkage(GlobalValue::ExternalLinkage); |
Chris Lattner | 5113eb0 | 2002-11-19 18:42:59 +0000 | [diff] [blame] | 84 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 85 | |
Chris Lattner | 5113eb0 | 2002-11-19 18:42:59 +0000 | [diff] [blame] | 86 | // All of the functions may be used by global variables or the named |
| 87 | // function. Loop through them and create a new, external functions that |
| 88 | // can be "used", instead of ones with bodies. |
Chris Lattner | 5113eb0 | 2002-11-19 18:42:59 +0000 | [diff] [blame] | 89 | std::vector<Function*> NewFunctions; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 90 | |
Chris Lattner | feb1550 | 2005-03-15 05:19:49 +0000 | [diff] [blame] | 91 | Function *Last = --M.end(); // Figure out where the last real fn is. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 92 | |
Chris Lattner | 5113eb0 | 2002-11-19 18:42:59 +0000 | [diff] [blame] | 93 | for (Module::iterator I = M.begin(); ; ++I) { |
| 94 | if (&*I != Named) { |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 95 | Function *New = new Function(I->getFunctionType(), |
Chris Lattner | 046800a | 2007-02-11 01:08:35 +0000 | [diff] [blame] | 96 | GlobalValue::ExternalLinkage); |
Chris Lattner | 338305b | 2007-01-25 17:38:26 +0000 | [diff] [blame] | 97 | New->setCallingConv(I->getCallingConv()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 98 | |
Chris Lattner | 5113eb0 | 2002-11-19 18:42:59 +0000 | [diff] [blame] | 99 | // If it's not the named function, delete the body of the function |
| 100 | I->dropAllReferences(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 101 | |
Chris Lattner | 5113eb0 | 2002-11-19 18:42:59 +0000 | [diff] [blame] | 102 | M.getFunctionList().push_back(New); |
| 103 | NewFunctions.push_back(New); |
Chris Lattner | 046800a | 2007-02-11 01:08:35 +0000 | [diff] [blame] | 104 | New->takeName(I); |
Chris Lattner | 5113eb0 | 2002-11-19 18:42:59 +0000 | [diff] [blame] | 105 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 106 | |
Chris Lattner | 5113eb0 | 2002-11-19 18:42:59 +0000 | [diff] [blame] | 107 | if (&*I == Last) break; // Stop after processing the last function |
| 108 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 109 | |
Chris Lattner | 5113eb0 | 2002-11-19 18:42:59 +0000 | [diff] [blame] | 110 | // Now that we have replacements all set up, loop through the module, |
| 111 | // deleting the old functions, replacing them with the newly created |
| 112 | // functions. |
| 113 | if (!NewFunctions.empty()) { |
| 114 | unsigned FuncNum = 0; |
| 115 | Module::iterator I = M.begin(); |
| 116 | do { |
| 117 | if (&*I != Named) { |
| 118 | // Make everything that uses the old function use the new dummy fn |
| 119 | I->replaceAllUsesWith(NewFunctions[FuncNum++]); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 120 | |
Chris Lattner | 5113eb0 | 2002-11-19 18:42:59 +0000 | [diff] [blame] | 121 | Function *Old = I; |
| 122 | ++I; // Move the iterator to the new function |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 123 | |
Chris Lattner | 5113eb0 | 2002-11-19 18:42:59 +0000 | [diff] [blame] | 124 | // Delete the old function! |
| 125 | M.getFunctionList().erase(Old); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 126 | |
Chris Lattner | 5113eb0 | 2002-11-19 18:42:59 +0000 | [diff] [blame] | 127 | } else { |
| 128 | ++I; // Skip the function we are extracting |
| 129 | } |
| 130 | } while (&*I != NewFunctions[0]); |
| 131 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 132 | |
Chris Lattner | 5113eb0 | 2002-11-19 18:42:59 +0000 | [diff] [blame] | 133 | return true; |
| 134 | } |
| 135 | }; |
| 136 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame^] | 137 | char FunctionExtractorPass::ID = 0; |
Chris Lattner | 5113eb0 | 2002-11-19 18:42:59 +0000 | [diff] [blame] | 138 | RegisterPass<FunctionExtractorPass> X("extract", "Function Extractor"); |
| 139 | } |
| 140 | |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 141 | ModulePass *llvm::createFunctionExtractionPass(Function *F, bool deleteFn, |
| 142 | bool relinkCallees) { |
| 143 | return new FunctionExtractorPass(F, deleteFn, relinkCallees); |
Chris Lattner | 5113eb0 | 2002-11-19 18:42:59 +0000 | [diff] [blame] | 144 | } |