Andrew Lenharth | 3f13b66 | 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 | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/SetVector.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 15 | #include "llvm/IR/LLVMContext.h" |
| 16 | #include "llvm/IR/Module.h" |
Andrew Lenharth | 3f13b66 | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 17 | #include "llvm/Pass.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 18 | #include "llvm/Transforms/IPO.h" |
Ted Kremenek | d48ed17 | 2008-03-09 18:32:50 +0000 | [diff] [blame] | 19 | #include <algorithm> |
Andrew Lenharth | 3f13b66 | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 20 | using namespace llvm; |
| 21 | |
Rafael Espindola | 6597992 | 2013-11-22 17:58:12 +0000 | [diff] [blame] | 22 | /// Make sure GV is visible from both modules. Delete is true if it is |
| 23 | /// being deleted from this module. |
| 24 | /// This also makes sure GV cannot be dropped so that references from |
| 25 | /// the split module remain valid. |
| 26 | static void makeVisible(GlobalValue &GV, bool Delete) { |
| 27 | bool Local = GV.hasLocalLinkage(); |
Rafael Espindola | 6597992 | 2013-11-22 17:58:12 +0000 | [diff] [blame] | 28 | if (Local || Delete) { |
| 29 | GV.setLinkage(GlobalValue::ExternalLinkage); |
Duncan P. N. Exon Smith | e60adfd | 2014-05-07 23:00:22 +0000 | [diff] [blame] | 30 | if (Local) |
| 31 | GV.setVisibility(GlobalValue::HiddenVisibility); |
Rafael Espindola | 6597992 | 2013-11-22 17:58:12 +0000 | [diff] [blame] | 32 | return; |
| 33 | } |
| 34 | |
| 35 | if (!GV.hasLinkOnceLinkage()) { |
| 36 | assert(!GV.isDiscardableIfUnused()); |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | // Map linkonce* to weak* so that llvm doesn't drop this GV. |
| 41 | switch(GV.getLinkage()) { |
| 42 | default: |
| 43 | llvm_unreachable("Unexpected linkage"); |
| 44 | case GlobalValue::LinkOnceAnyLinkage: |
| 45 | GV.setLinkage(GlobalValue::WeakAnyLinkage); |
| 46 | return; |
| 47 | case GlobalValue::LinkOnceODRLinkage: |
| 48 | GV.setLinkage(GlobalValue::WeakODRLinkage); |
| 49 | return; |
| 50 | } |
| 51 | } |
| 52 | |
Andrew Lenharth | 3f13b66 | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 53 | namespace { |
Adrian Prantl | 4dfcc4a | 2018-05-01 16:10:38 +0000 | [diff] [blame] | 54 | /// A pass to extract specific global values and their dependencies. |
Nick Lewycky | 02d5f77 | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 55 | class GVExtractorPass : public ModulePass { |
Dan Gohman | 8f292e7 | 2010-08-26 00:22:55 +0000 | [diff] [blame] | 56 | SetVector<GlobalValue *> Named; |
Andrew Lenharth | 3f13b66 | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 57 | bool deleteStuff; |
Andrew Lenharth | 3f13b66 | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 58 | public: |
| 59 | static char ID; // Pass identification, replacement for typeid |
| 60 | |
Craig Topper | 2aa4d39 | 2017-06-08 23:38:19 +0000 | [diff] [blame] | 61 | /// If deleteS is true, this pass deletes the specified global values. |
| 62 | /// Otherwise, it deletes as much of the module as possible, except for the |
| 63 | /// global values specified. |
| 64 | explicit GVExtractorPass(std::vector<GlobalValue*> &GVs, |
| 65 | bool deleteS = true) |
Dan Gohman | 8f292e7 | 2010-08-26 00:22:55 +0000 | [diff] [blame] | 66 | : ModulePass(ID), Named(GVs.begin(), GVs.end()), deleteStuff(deleteS) {} |
Andrew Lenharth | 3f13b66 | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 67 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 68 | bool runOnModule(Module &M) override { |
Andrew Kaylor | aa641a5 | 2016-04-22 22:06:11 +0000 | [diff] [blame] | 69 | if (skipModule(M)) |
| 70 | return false; |
| 71 | |
Dan Gohman | 8f292e7 | 2010-08-26 00:22:55 +0000 | [diff] [blame] | 72 | // Visit the global inline asm. |
| 73 | if (!deleteStuff) |
| 74 | M.setModuleInlineAsm(""); |
Andrew Lenharth | 3f13b66 | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 75 | |
Dan Gohman | 8f292e7 | 2010-08-26 00:22:55 +0000 | [diff] [blame] | 76 | // For simplicity, just give all GlobalValues ExternalLinkage. A trickier |
| 77 | // implementation could figure out which GlobalValues are actually |
| 78 | // referenced by the Named set, and which GlobalValues in the rest of |
| 79 | // the module are referenced by the NamedSet, and get away with leaving |
| 80 | // more internal and private things internal and private. But for now, |
| 81 | // be conservative and simple. |
Andrew Lenharth | 3f13b66 | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 82 | |
Dan Gohman | 8f292e7 | 2010-08-26 00:22:55 +0000 | [diff] [blame] | 83 | // Visit the GlobalVariables. |
| 84 | for (Module::global_iterator I = M.global_begin(), E = M.global_end(); |
Bob Wilson | 3aecb15 | 2010-09-23 17:25:06 +0000 | [diff] [blame] | 85 | I != E; ++I) { |
Rafael Espindola | 56183fb | 2012-10-29 01:59:03 +0000 | [diff] [blame] | 86 | bool Delete = |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 87 | deleteStuff == (bool)Named.count(&*I) && !I->isDeclaration(); |
Rafael Espindola | 56183fb | 2012-10-29 01:59:03 +0000 | [diff] [blame] | 88 | if (!Delete) { |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 89 | if (I->hasAvailableExternallyLinkage()) |
| 90 | continue; |
| 91 | if (I->getName() == "llvm.global_ctors") |
| 92 | continue; |
| 93 | } |
Rafael Espindola | b77c00f | 2011-06-09 14:38:09 +0000 | [diff] [blame] | 94 | |
NAKAMURA Takumi | 335a7bc | 2014-10-28 11:53:30 +0000 | [diff] [blame] | 95 | makeVisible(*I, Delete); |
Rafael Espindola | 56183fb | 2012-10-29 01:59:03 +0000 | [diff] [blame] | 96 | |
Reid Kleckner | fc0f938 | 2015-07-06 18:48:02 +0000 | [diff] [blame] | 97 | if (Delete) { |
| 98 | // Make this a declaration and drop it's comdat. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 99 | I->setInitializer(nullptr); |
Reid Kleckner | fc0f938 | 2015-07-06 18:48:02 +0000 | [diff] [blame] | 100 | I->setComdat(nullptr); |
| 101 | } |
Bob Wilson | 3aecb15 | 2010-09-23 17:25:06 +0000 | [diff] [blame] | 102 | } |
Dan Gohman | 8f292e7 | 2010-08-26 00:22:55 +0000 | [diff] [blame] | 103 | |
| 104 | // Visit the Functions. |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 105 | for (Function &F : M) { |
Rafael Espindola | 56183fb | 2012-10-29 01:59:03 +0000 | [diff] [blame] | 106 | bool Delete = |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 107 | deleteStuff == (bool)Named.count(&F) && !F.isDeclaration(); |
Rafael Espindola | 56183fb | 2012-10-29 01:59:03 +0000 | [diff] [blame] | 108 | if (!Delete) { |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 109 | if (F.hasAvailableExternallyLinkage()) |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 110 | continue; |
| 111 | } |
Rafael Espindola | b77c00f | 2011-06-09 14:38:09 +0000 | [diff] [blame] | 112 | |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 113 | makeVisible(F, Delete); |
Rafael Espindola | 56183fb | 2012-10-29 01:59:03 +0000 | [diff] [blame] | 114 | |
Reid Kleckner | fc0f938 | 2015-07-06 18:48:02 +0000 | [diff] [blame] | 115 | if (Delete) { |
| 116 | // Make this a declaration and drop it's comdat. |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 117 | F.deleteBody(); |
| 118 | F.setComdat(nullptr); |
Reid Kleckner | fc0f938 | 2015-07-06 18:48:02 +0000 | [diff] [blame] | 119 | } |
Bob Wilson | 3aecb15 | 2010-09-23 17:25:06 +0000 | [diff] [blame] | 120 | } |
Andrew Lenharth | 3f13b66 | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 121 | |
Rafael Espindola | 9d30d0f | 2012-10-29 00:27:55 +0000 | [diff] [blame] | 122 | // Visit the Aliases. |
| 123 | for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end(); |
| 124 | I != E;) { |
| 125 | Module::alias_iterator CurI = I; |
| 126 | ++I; |
| 127 | |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 128 | bool Delete = deleteStuff == (bool)Named.count(&*CurI); |
NAKAMURA Takumi | 335a7bc | 2014-10-28 11:53:30 +0000 | [diff] [blame] | 129 | makeVisible(*CurI, Delete); |
Rafael Espindola | 9d30d0f | 2012-10-29 00:27:55 +0000 | [diff] [blame] | 130 | |
Rafael Espindola | 6597992 | 2013-11-22 17:58:12 +0000 | [diff] [blame] | 131 | if (Delete) { |
Manuel Jacob | 5f6eaac | 2016-01-16 20:30:46 +0000 | [diff] [blame] | 132 | Type *Ty = CurI->getValueType(); |
Rafael Espindola | 9d30d0f | 2012-10-29 00:27:55 +0000 | [diff] [blame] | 133 | |
| 134 | CurI->removeFromParent(); |
| 135 | llvm::Value *Declaration; |
| 136 | if (FunctionType *FTy = dyn_cast<FunctionType>(Ty)) { |
| 137 | Declaration = Function::Create(FTy, GlobalValue::ExternalLinkage, |
| 138 | CurI->getName(), &M); |
| 139 | |
| 140 | } else { |
| 141 | Declaration = |
| 142 | new GlobalVariable(M, Ty, false, GlobalValue::ExternalLinkage, |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 143 | nullptr, CurI->getName()); |
Rafael Espindola | 9d30d0f | 2012-10-29 00:27:55 +0000 | [diff] [blame] | 144 | |
| 145 | } |
| 146 | CurI->replaceAllUsesWith(Declaration); |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 147 | delete &*CurI; |
Rafael Espindola | 9d30d0f | 2012-10-29 00:27:55 +0000 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
Andrew Lenharth | 3f13b66 | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 151 | return true; |
| 152 | } |
| 153 | }; |
| 154 | |
| 155 | char GVExtractorPass::ID = 0; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 156 | } |
Andrew Lenharth | 3f13b66 | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 157 | |
NAKAMURA Takumi | d0e13af | 2014-10-28 11:54:52 +0000 | [diff] [blame] | 158 | ModulePass *llvm::createGVExtractionPass(std::vector<GlobalValue *> &GVs, |
Dan Gohman | 8f292e7 | 2010-08-26 00:22:55 +0000 | [diff] [blame] | 159 | bool deleteFn) { |
| 160 | return new GVExtractorPass(GVs, deleteFn); |
Andrew Lenharth | 3f13b66 | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 161 | } |