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 | |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 14 | #include "llvm/Transforms/IPO.h" |
| 15 | #include "llvm/ADT/SetVector.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 16 | #include "llvm/IR/Constants.h" |
| 17 | #include "llvm/IR/Instructions.h" |
| 18 | #include "llvm/IR/LLVMContext.h" |
| 19 | #include "llvm/IR/Module.h" |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 20 | #include "llvm/Pass.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 | |
Bill Wendling | 3343ddf | 2013-11-25 05:20:58 +0000 | [diff] [blame] | 24 | /// Make sure GV is visible from both modules. Delete is true if it is |
| 25 | /// being deleted from this module. |
| 26 | /// This also makes sure GV cannot be dropped so that references from |
| 27 | /// the split module remain valid. |
| 28 | static void makeVisible(GlobalValue &GV, bool Delete) { |
| 29 | bool Local = GV.hasLocalLinkage(); |
Bill Wendling | 3343ddf | 2013-11-25 05:20:58 +0000 | [diff] [blame] | 30 | if (Local || Delete) { |
| 31 | GV.setLinkage(GlobalValue::ExternalLinkage); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 32 | if (Local) |
| 33 | GV.setVisibility(GlobalValue::HiddenVisibility); |
Bill Wendling | 3343ddf | 2013-11-25 05:20:58 +0000 | [diff] [blame] | 34 | return; |
| 35 | } |
| 36 | |
| 37 | if (!GV.hasLinkOnceLinkage()) { |
| 38 | assert(!GV.isDiscardableIfUnused()); |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | // Map linkonce* to weak* so that llvm doesn't drop this GV. |
| 43 | switch(GV.getLinkage()) { |
| 44 | default: |
| 45 | llvm_unreachable("Unexpected linkage"); |
| 46 | case GlobalValue::LinkOnceAnyLinkage: |
| 47 | GV.setLinkage(GlobalValue::WeakAnyLinkage); |
| 48 | return; |
| 49 | case GlobalValue::LinkOnceODRLinkage: |
| 50 | GV.setLinkage(GlobalValue::WeakODRLinkage); |
| 51 | return; |
| 52 | } |
| 53 | } |
| 54 | |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 55 | namespace { |
| 56 | /// @brief A pass to extract specific functions and their dependencies. |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 57 | class GVExtractorPass : public ModulePass { |
Dan Gohman | b4e3cda | 2010-08-26 00:22:55 +0000 | [diff] [blame] | 58 | SetVector<GlobalValue *> Named; |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 59 | bool deleteStuff; |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 60 | public: |
| 61 | static char ID; // Pass identification, replacement for typeid |
| 62 | |
| 63 | /// FunctionExtractorPass - If deleteFn is true, this pass deletes as the |
| 64 | /// specified function. Otherwise, it deletes as much of the module as |
| 65 | /// possible, except for the function specified. |
| 66 | /// |
Dan Gohman | b4e3cda | 2010-08-26 00:22:55 +0000 | [diff] [blame] | 67 | explicit GVExtractorPass(std::vector<GlobalValue*>& GVs, bool deleteS = true) |
| 68 | : ModulePass(ID), Named(GVs.begin(), GVs.end()), deleteStuff(deleteS) {} |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 69 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 70 | bool runOnModule(Module &M) override { |
Dan Gohman | b4e3cda | 2010-08-26 00:22:55 +0000 | [diff] [blame] | 71 | // Visit the global inline asm. |
| 72 | if (!deleteStuff) |
| 73 | M.setModuleInlineAsm(""); |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 74 | |
Dan Gohman | b4e3cda | 2010-08-26 00:22:55 +0000 | [diff] [blame] | 75 | // For simplicity, just give all GlobalValues ExternalLinkage. A trickier |
| 76 | // implementation could figure out which GlobalValues are actually |
| 77 | // referenced by the Named set, and which GlobalValues in the rest of |
| 78 | // the module are referenced by the NamedSet, and get away with leaving |
| 79 | // more internal and private things internal and private. But for now, |
| 80 | // be conservative and simple. |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 81 | |
Dan Gohman | b4e3cda | 2010-08-26 00:22:55 +0000 | [diff] [blame] | 82 | // Visit the GlobalVariables. |
| 83 | for (Module::global_iterator I = M.global_begin(), E = M.global_end(); |
Bob Wilson | edf0174 | 2010-09-23 17:25:06 +0000 | [diff] [blame] | 84 | I != E; ++I) { |
Rafael Espindola | 9cb90e7 | 2012-10-29 01:59:03 +0000 | [diff] [blame] | 85 | bool Delete = |
| 86 | deleteStuff == (bool)Named.count(I) && !I->isDeclaration(); |
| 87 | if (!Delete) { |
Bill Wendling | 56cb229 | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 88 | if (I->hasAvailableExternallyLinkage()) |
| 89 | continue; |
| 90 | if (I->getName() == "llvm.global_ctors") |
| 91 | continue; |
| 92 | } |
Rafael Espindola | d896d41 | 2011-06-09 14:38:09 +0000 | [diff] [blame] | 93 | |
Bill Wendling | 3343ddf | 2013-11-25 05:20:58 +0000 | [diff] [blame] | 94 | makeVisible(*I, Delete); |
Rafael Espindola | 9cb90e7 | 2012-10-29 01:59:03 +0000 | [diff] [blame] | 95 | |
| 96 | if (Delete) |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 97 | I->setInitializer(nullptr); |
Bob Wilson | edf0174 | 2010-09-23 17:25:06 +0000 | [diff] [blame] | 98 | } |
Dan Gohman | b4e3cda | 2010-08-26 00:22:55 +0000 | [diff] [blame] | 99 | |
| 100 | // Visit the Functions. |
Bob Wilson | edf0174 | 2010-09-23 17:25:06 +0000 | [diff] [blame] | 101 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) { |
Rafael Espindola | 9cb90e7 | 2012-10-29 01:59:03 +0000 | [diff] [blame] | 102 | bool Delete = |
| 103 | deleteStuff == (bool)Named.count(I) && !I->isDeclaration(); |
| 104 | if (!Delete) { |
Bill Wendling | 56cb229 | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 105 | if (I->hasAvailableExternallyLinkage()) |
| 106 | continue; |
| 107 | } |
Rafael Espindola | d896d41 | 2011-06-09 14:38:09 +0000 | [diff] [blame] | 108 | |
Bill Wendling | 3343ddf | 2013-11-25 05:20:58 +0000 | [diff] [blame] | 109 | makeVisible(*I, Delete); |
Rafael Espindola | 9cb90e7 | 2012-10-29 01:59:03 +0000 | [diff] [blame] | 110 | |
| 111 | if (Delete) |
| 112 | I->deleteBody(); |
Bob Wilson | edf0174 | 2010-09-23 17:25:06 +0000 | [diff] [blame] | 113 | } |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 114 | |
Rafael Espindola | c0916d3 | 2012-10-29 00:27:55 +0000 | [diff] [blame] | 115 | // Visit the Aliases. |
| 116 | for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end(); |
| 117 | I != E;) { |
| 118 | Module::alias_iterator CurI = I; |
| 119 | ++I; |
| 120 | |
Bill Wendling | 3343ddf | 2013-11-25 05:20:58 +0000 | [diff] [blame] | 121 | bool Delete = deleteStuff == (bool)Named.count(CurI); |
| 122 | makeVisible(*CurI, Delete); |
Rafael Espindola | c0916d3 | 2012-10-29 00:27:55 +0000 | [diff] [blame] | 123 | |
Bill Wendling | 3343ddf | 2013-11-25 05:20:58 +0000 | [diff] [blame] | 124 | if (Delete) { |
Rafael Espindola | c0916d3 | 2012-10-29 00:27:55 +0000 | [diff] [blame] | 125 | Type *Ty = CurI->getType()->getElementType(); |
| 126 | |
| 127 | CurI->removeFromParent(); |
| 128 | llvm::Value *Declaration; |
| 129 | if (FunctionType *FTy = dyn_cast<FunctionType>(Ty)) { |
| 130 | Declaration = Function::Create(FTy, GlobalValue::ExternalLinkage, |
| 131 | CurI->getName(), &M); |
| 132 | |
| 133 | } else { |
| 134 | Declaration = |
| 135 | new GlobalVariable(M, Ty, false, GlobalValue::ExternalLinkage, |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 136 | nullptr, CurI->getName()); |
Rafael Espindola | c0916d3 | 2012-10-29 00:27:55 +0000 | [diff] [blame] | 137 | |
| 138 | } |
| 139 | CurI->replaceAllUsesWith(Declaration); |
| 140 | delete CurI; |
| 141 | } |
| 142 | } |
| 143 | |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 144 | return true; |
| 145 | } |
| 146 | }; |
| 147 | |
| 148 | char GVExtractorPass::ID = 0; |
| 149 | } |
| 150 | |
| 151 | ModulePass *llvm::createGVExtractionPass(std::vector<GlobalValue*>& GVs, |
Dan Gohman | b4e3cda | 2010-08-26 00:22:55 +0000 | [diff] [blame] | 152 | bool deleteFn) { |
| 153 | return new GVExtractorPass(GVs, deleteFn); |
Andrew Lenharth | d245a8a | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 154 | } |