Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 1 | //===-- ExtractGV.cpp - Global Value extraction pass ----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass extracts global values |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Instructions.h" |
Owen Anderson | 14ce9ef | 2009-07-06 01:34:54 +0000 | [diff] [blame] | 15 | #include "llvm/LLVMContext.h" |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 16 | #include "llvm/Module.h" |
| 17 | #include "llvm/Pass.h" |
| 18 | #include "llvm/Constants.h" |
| 19 | #include "llvm/Transforms/IPO.h" |
| 20 | #include "llvm/Support/Compiler.h" |
Ted Kremenek | 58d5e05 | 2008-03-09 18:32:50 +0000 | [diff] [blame] | 21 | #include <algorithm> |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| 23 | |
| 24 | namespace { |
| 25 | /// @brief A pass to extract specific functions and their dependencies. |
| 26 | class VISIBILITY_HIDDEN GVExtractorPass : public ModulePass { |
| 27 | std::vector<GlobalValue*> Named; |
| 28 | bool deleteStuff; |
| 29 | bool reLink; |
| 30 | public: |
| 31 | static char ID; // Pass identification, replacement for typeid |
| 32 | |
| 33 | /// FunctionExtractorPass - If deleteFn is true, this pass deletes as the |
| 34 | /// specified function. Otherwise, it deletes as much of the module as |
| 35 | /// possible, except for the function specified. |
| 36 | /// |
| 37 | explicit GVExtractorPass(std::vector<GlobalValue*>& GVs, bool deleteS = true, |
| 38 | bool relinkCallees = false) |
Dan Gohman | ae73dc1 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 39 | : ModulePass(&ID), Named(GVs), deleteStuff(deleteS), |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 40 | reLink(relinkCallees) {} |
| 41 | |
| 42 | bool runOnModule(Module &M) { |
| 43 | if (Named.size() == 0) { |
| 44 | return false; // Nothing to extract |
| 45 | } |
| 46 | |
Owen Anderson | 001dbfe | 2009-07-16 18:04:31 +0000 | [diff] [blame] | 47 | |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 48 | if (deleteStuff) |
| 49 | return deleteGV(); |
| 50 | M.setModuleInlineAsm(""); |
| 51 | return isolateGV(M); |
| 52 | } |
| 53 | |
| 54 | bool deleteGV() { |
| 55 | for (std::vector<GlobalValue*>::iterator GI = Named.begin(), |
| 56 | GE = Named.end(); GI != GE; ++GI) { |
Dan Gohman | b9a31a1 | 2008-10-21 01:08:07 +0000 | [diff] [blame] | 57 | if (Function* NamedFunc = dyn_cast<Function>(*GI)) { |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 58 | // If we're in relinking mode, set linkage of all internal callees to |
| 59 | // external. This will allow us extract function, and then - link |
| 60 | // everything together |
| 61 | if (reLink) { |
| 62 | for (Function::iterator B = NamedFunc->begin(), BE = NamedFunc->end(); |
| 63 | B != BE; ++B) { |
| 64 | for (BasicBlock::iterator I = B->begin(), E = B->end(); |
| 65 | I != E; ++I) { |
| 66 | if (CallInst* callInst = dyn_cast<CallInst>(&*I)) { |
| 67 | Function* Callee = callInst->getCalledFunction(); |
Rafael Espindola | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 68 | if (Callee && Callee->hasLocalLinkage()) |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 69 | Callee->setLinkage(GlobalValue::ExternalLinkage); |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | NamedFunc->setLinkage(GlobalValue::ExternalLinkage); |
| 76 | NamedFunc->deleteBody(); |
| 77 | assert(NamedFunc->isDeclaration() && "This didn't make the function external!"); |
| 78 | } else { |
| 79 | if (!(*GI)->isDeclaration()) { |
| 80 | cast<GlobalVariable>(*GI)->setInitializer(0); //clear the initializer |
| 81 | (*GI)->setLinkage(GlobalValue::ExternalLinkage); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | bool isolateGV(Module &M) { |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 89 | LLVMContext &Context = M.getContext(); |
| 90 | |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 91 | // Mark all globals internal |
Rafael Espindola | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 92 | // FIXME: what should we do with private linkage? |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 93 | for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I) |
| 94 | if (!I->isDeclaration()) { |
| 95 | I->setLinkage(GlobalValue::InternalLinkage); |
| 96 | } |
| 97 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 98 | if (!I->isDeclaration()) { |
| 99 | I->setLinkage(GlobalValue::InternalLinkage); |
| 100 | } |
| 101 | |
| 102 | // Make sure our result is globally accessible... |
| 103 | // by putting them in the used array |
| 104 | { |
| 105 | std::vector<Constant *> AUGs; |
Owen Anderson | debcb01 | 2009-07-29 22:17:13 +0000 | [diff] [blame^] | 106 | const Type *SBP= PointerType::getUnqual(Type::Int8Ty); |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 107 | for (std::vector<GlobalValue*>::iterator GI = Named.begin(), |
| 108 | GE = Named.end(); GI != GE; ++GI) { |
| 109 | (*GI)->setLinkage(GlobalValue::ExternalLinkage); |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 110 | AUGs.push_back(ConstantExpr::getBitCast(*GI, SBP)); |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 111 | } |
Owen Anderson | debcb01 | 2009-07-29 22:17:13 +0000 | [diff] [blame^] | 112 | ArrayType *AT = ArrayType::get(SBP, AUGs.size()); |
Owen Anderson | 1fd7096 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 113 | Constant *Init = ConstantArray::get(AT, AUGs); |
Owen Anderson | e9b11b4 | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 114 | GlobalValue *gv = new GlobalVariable(M, AT, false, |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 115 | GlobalValue::AppendingLinkage, |
Owen Anderson | e9b11b4 | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 116 | Init, "llvm.used"); |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 117 | gv->setSection("llvm.metadata"); |
| 118 | } |
| 119 | |
| 120 | // All of the functions may be used by global variables or the named |
| 121 | // globals. Loop through them and create a new, external functions that |
| 122 | // can be "used", instead of ones with bodies. |
| 123 | std::vector<Function*> NewFunctions; |
| 124 | |
| 125 | Function *Last = --M.end(); // Figure out where the last real fn is. |
| 126 | |
| 127 | for (Module::iterator I = M.begin(); ; ++I) { |
| 128 | if (std::find(Named.begin(), Named.end(), &*I) == Named.end()) { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 129 | Function *New = Function::Create(I->getFunctionType(), |
| 130 | GlobalValue::ExternalLinkage); |
Duncan Sands | 28c3cff | 2008-05-26 19:58:59 +0000 | [diff] [blame] | 131 | New->copyAttributesFrom(I); |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 132 | |
| 133 | // If it's not the named function, delete the body of the function |
| 134 | I->dropAllReferences(); |
| 135 | |
| 136 | M.getFunctionList().push_back(New); |
| 137 | NewFunctions.push_back(New); |
| 138 | New->takeName(I); |
| 139 | } |
| 140 | |
| 141 | if (&*I == Last) break; // Stop after processing the last function |
| 142 | } |
| 143 | |
| 144 | // Now that we have replacements all set up, loop through the module, |
| 145 | // deleting the old functions, replacing them with the newly created |
| 146 | // functions. |
| 147 | if (!NewFunctions.empty()) { |
| 148 | unsigned FuncNum = 0; |
| 149 | Module::iterator I = M.begin(); |
| 150 | do { |
| 151 | if (std::find(Named.begin(), Named.end(), &*I) == Named.end()) { |
| 152 | // Make everything that uses the old function use the new dummy fn |
| 153 | I->replaceAllUsesWith(NewFunctions[FuncNum++]); |
| 154 | |
| 155 | Function *Old = I; |
| 156 | ++I; // Move the iterator to the new function |
| 157 | |
| 158 | // Delete the old function! |
| 159 | M.getFunctionList().erase(Old); |
| 160 | |
| 161 | } else { |
| 162 | ++I; // Skip the function we are extracting |
| 163 | } |
| 164 | } while (&*I != NewFunctions[0]); |
| 165 | } |
| 166 | |
| 167 | return true; |
| 168 | } |
| 169 | }; |
| 170 | |
| 171 | char GVExtractorPass::ID = 0; |
| 172 | } |
| 173 | |
| 174 | ModulePass *llvm::createGVExtractionPass(std::vector<GlobalValue*>& GVs, |
| 175 | bool deleteFn, bool relinkCallees) { |
| 176 | return new GVExtractorPass(GVs, deleteFn, relinkCallees); |
| 177 | } |