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 | |
| 47 | if (deleteStuff) |
| 48 | return deleteGV(); |
| 49 | M.setModuleInlineAsm(""); |
| 50 | return isolateGV(M); |
| 51 | } |
| 52 | |
| 53 | bool deleteGV() { |
| 54 | for (std::vector<GlobalValue*>::iterator GI = Named.begin(), |
| 55 | GE = Named.end(); GI != GE; ++GI) { |
Dan Gohman | b9a31a1 | 2008-10-21 01:08:07 +0000 | [diff] [blame] | 56 | if (Function* NamedFunc = dyn_cast<Function>(*GI)) { |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 57 | // If we're in relinking mode, set linkage of all internal callees to |
| 58 | // external. This will allow us extract function, and then - link |
| 59 | // everything together |
| 60 | if (reLink) { |
| 61 | for (Function::iterator B = NamedFunc->begin(), BE = NamedFunc->end(); |
| 62 | B != BE; ++B) { |
| 63 | for (BasicBlock::iterator I = B->begin(), E = B->end(); |
| 64 | I != E; ++I) { |
| 65 | if (CallInst* callInst = dyn_cast<CallInst>(&*I)) { |
| 66 | Function* Callee = callInst->getCalledFunction(); |
Rafael Espindola | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 67 | if (Callee && Callee->hasLocalLinkage()) |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 68 | Callee->setLinkage(GlobalValue::ExternalLinkage); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | NamedFunc->setLinkage(GlobalValue::ExternalLinkage); |
| 75 | NamedFunc->deleteBody(); |
| 76 | assert(NamedFunc->isDeclaration() && "This didn't make the function external!"); |
| 77 | } else { |
| 78 | if (!(*GI)->isDeclaration()) { |
| 79 | cast<GlobalVariable>(*GI)->setInitializer(0); //clear the initializer |
| 80 | (*GI)->setLinkage(GlobalValue::ExternalLinkage); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | bool isolateGV(Module &M) { |
| 88 | // Mark all globals internal |
Rafael Espindola | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 89 | // FIXME: what should we do with private linkage? |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 90 | for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I) |
| 91 | if (!I->isDeclaration()) { |
| 92 | I->setLinkage(GlobalValue::InternalLinkage); |
| 93 | } |
| 94 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 95 | if (!I->isDeclaration()) { |
| 96 | I->setLinkage(GlobalValue::InternalLinkage); |
| 97 | } |
| 98 | |
| 99 | // Make sure our result is globally accessible... |
| 100 | // by putting them in the used array |
| 101 | { |
| 102 | std::vector<Constant *> AUGs; |
Owen Anderson | 14ce9ef | 2009-07-06 01:34:54 +0000 | [diff] [blame] | 103 | const Type *SBP= Context->getPointerTypeUnqual(Type::Int8Ty); |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 104 | for (std::vector<GlobalValue*>::iterator GI = Named.begin(), |
| 105 | GE = Named.end(); GI != GE; ++GI) { |
| 106 | (*GI)->setLinkage(GlobalValue::ExternalLinkage); |
Owen Anderson | 14ce9ef | 2009-07-06 01:34:54 +0000 | [diff] [blame] | 107 | AUGs.push_back(Context->getConstantExprBitCast(*GI, SBP)); |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 108 | } |
Owen Anderson | 14ce9ef | 2009-07-06 01:34:54 +0000 | [diff] [blame] | 109 | ArrayType *AT = Context->getArrayType(SBP, AUGs.size()); |
| 110 | Constant *Init = Context->getConstantArray(AT, AUGs); |
Owen Anderson | 3d29df3 | 2009-07-08 01:26:06 +0000 | [diff] [blame] | 111 | GlobalValue *gv = new GlobalVariable(M.getContext(), AT, false, |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 112 | GlobalValue::AppendingLinkage, |
| 113 | Init, "llvm.used", &M); |
| 114 | gv->setSection("llvm.metadata"); |
| 115 | } |
| 116 | |
| 117 | // All of the functions may be used by global variables or the named |
| 118 | // globals. Loop through them and create a new, external functions that |
| 119 | // can be "used", instead of ones with bodies. |
| 120 | std::vector<Function*> NewFunctions; |
| 121 | |
| 122 | Function *Last = --M.end(); // Figure out where the last real fn is. |
| 123 | |
| 124 | for (Module::iterator I = M.begin(); ; ++I) { |
| 125 | if (std::find(Named.begin(), Named.end(), &*I) == Named.end()) { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 126 | Function *New = Function::Create(I->getFunctionType(), |
| 127 | GlobalValue::ExternalLinkage); |
Duncan Sands | 28c3cff | 2008-05-26 19:58:59 +0000 | [diff] [blame] | 128 | New->copyAttributesFrom(I); |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 129 | |
| 130 | // If it's not the named function, delete the body of the function |
| 131 | I->dropAllReferences(); |
| 132 | |
| 133 | M.getFunctionList().push_back(New); |
| 134 | NewFunctions.push_back(New); |
| 135 | New->takeName(I); |
| 136 | } |
| 137 | |
| 138 | if (&*I == Last) break; // Stop after processing the last function |
| 139 | } |
| 140 | |
| 141 | // Now that we have replacements all set up, loop through the module, |
| 142 | // deleting the old functions, replacing them with the newly created |
| 143 | // functions. |
| 144 | if (!NewFunctions.empty()) { |
| 145 | unsigned FuncNum = 0; |
| 146 | Module::iterator I = M.begin(); |
| 147 | do { |
| 148 | if (std::find(Named.begin(), Named.end(), &*I) == Named.end()) { |
| 149 | // Make everything that uses the old function use the new dummy fn |
| 150 | I->replaceAllUsesWith(NewFunctions[FuncNum++]); |
| 151 | |
| 152 | Function *Old = I; |
| 153 | ++I; // Move the iterator to the new function |
| 154 | |
| 155 | // Delete the old function! |
| 156 | M.getFunctionList().erase(Old); |
| 157 | |
| 158 | } else { |
| 159 | ++I; // Skip the function we are extracting |
| 160 | } |
| 161 | } while (&*I != NewFunctions[0]); |
| 162 | } |
| 163 | |
| 164 | return true; |
| 165 | } |
| 166 | }; |
| 167 | |
| 168 | char GVExtractorPass::ID = 0; |
| 169 | } |
| 170 | |
| 171 | ModulePass *llvm::createGVExtractionPass(std::vector<GlobalValue*>& GVs, |
| 172 | bool deleteFn, bool relinkCallees) { |
| 173 | return new GVExtractorPass(GVs, deleteFn, relinkCallees); |
| 174 | } |