Andrew Lenharth | 3f13b66 | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 1 | //===-- ExtractGV.cpp - Global Value extraction pass ----------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Andrew Lenharth | 3f13b66 | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This pass extracts global values |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/SetVector.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 14 | #include "llvm/IR/LLVMContext.h" |
| 15 | #include "llvm/IR/Module.h" |
Andrew Lenharth | 3f13b66 | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 16 | #include "llvm/Pass.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 17 | #include "llvm/Transforms/IPO.h" |
Ted Kremenek | d48ed17 | 2008-03-09 18:32:50 +0000 | [diff] [blame] | 18 | #include <algorithm> |
Andrew Lenharth | 3f13b66 | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 19 | using namespace llvm; |
| 20 | |
Rafael Espindola | 6597992 | 2013-11-22 17:58:12 +0000 | [diff] [blame] | 21 | /// Make sure GV is visible from both modules. Delete is true if it is |
| 22 | /// being deleted from this module. |
| 23 | /// This also makes sure GV cannot be dropped so that references from |
| 24 | /// the split module remain valid. |
| 25 | static void makeVisible(GlobalValue &GV, bool Delete) { |
| 26 | bool Local = GV.hasLocalLinkage(); |
Rafael Espindola | 6597992 | 2013-11-22 17:58:12 +0000 | [diff] [blame] | 27 | if (Local || Delete) { |
| 28 | GV.setLinkage(GlobalValue::ExternalLinkage); |
Duncan P. N. Exon Smith | e60adfd | 2014-05-07 23:00:22 +0000 | [diff] [blame] | 29 | if (Local) |
| 30 | GV.setVisibility(GlobalValue::HiddenVisibility); |
Rafael Espindola | 6597992 | 2013-11-22 17:58:12 +0000 | [diff] [blame] | 31 | return; |
| 32 | } |
| 33 | |
| 34 | if (!GV.hasLinkOnceLinkage()) { |
| 35 | assert(!GV.isDiscardableIfUnused()); |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | // Map linkonce* to weak* so that llvm doesn't drop this GV. |
| 40 | switch(GV.getLinkage()) { |
| 41 | default: |
| 42 | llvm_unreachable("Unexpected linkage"); |
| 43 | case GlobalValue::LinkOnceAnyLinkage: |
| 44 | GV.setLinkage(GlobalValue::WeakAnyLinkage); |
| 45 | return; |
| 46 | case GlobalValue::LinkOnceODRLinkage: |
| 47 | GV.setLinkage(GlobalValue::WeakODRLinkage); |
| 48 | return; |
| 49 | } |
| 50 | } |
| 51 | |
Andrew Lenharth | 3f13b66 | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 52 | namespace { |
Adrian Prantl | 4dfcc4a | 2018-05-01 16:10:38 +0000 | [diff] [blame] | 53 | /// A pass to extract specific global values and their dependencies. |
Nick Lewycky | 02d5f77 | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 54 | class GVExtractorPass : public ModulePass { |
Dan Gohman | 8f292e7 | 2010-08-26 00:22:55 +0000 | [diff] [blame] | 55 | SetVector<GlobalValue *> Named; |
Andrew Lenharth | 3f13b66 | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 56 | bool deleteStuff; |
Andrew Lenharth | 3f13b66 | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 57 | public: |
| 58 | static char ID; // Pass identification, replacement for typeid |
| 59 | |
Craig Topper | 2aa4d39 | 2017-06-08 23:38:19 +0000 | [diff] [blame] | 60 | /// If deleteS is true, this pass deletes the specified global values. |
| 61 | /// Otherwise, it deletes as much of the module as possible, except for the |
| 62 | /// global values specified. |
| 63 | explicit GVExtractorPass(std::vector<GlobalValue*> &GVs, |
| 64 | bool deleteS = true) |
Dan Gohman | 8f292e7 | 2010-08-26 00:22:55 +0000 | [diff] [blame] | 65 | : ModulePass(ID), Named(GVs.begin(), GVs.end()), deleteStuff(deleteS) {} |
Andrew Lenharth | 3f13b66 | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 66 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 67 | bool runOnModule(Module &M) override { |
Andrew Kaylor | aa641a5 | 2016-04-22 22:06:11 +0000 | [diff] [blame] | 68 | if (skipModule(M)) |
| 69 | return false; |
| 70 | |
Dan Gohman | 8f292e7 | 2010-08-26 00:22:55 +0000 | [diff] [blame] | 71 | // Visit the global inline asm. |
| 72 | if (!deleteStuff) |
| 73 | M.setModuleInlineAsm(""); |
Andrew Lenharth | 3f13b66 | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 74 | |
Dan Gohman | 8f292e7 | 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 | 3f13b66 | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 81 | |
Dan Gohman | 8f292e7 | 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 | 3aecb15 | 2010-09-23 17:25:06 +0000 | [diff] [blame] | 84 | I != E; ++I) { |
Rafael Espindola | 56183fb | 2012-10-29 01:59:03 +0000 | [diff] [blame] | 85 | bool Delete = |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 86 | deleteStuff == (bool)Named.count(&*I) && !I->isDeclaration(); |
Rafael Espindola | 56183fb | 2012-10-29 01:59:03 +0000 | [diff] [blame] | 87 | if (!Delete) { |
Bill Wendling | ea6397f | 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 | b77c00f | 2011-06-09 14:38:09 +0000 | [diff] [blame] | 93 | |
NAKAMURA Takumi | 335a7bc | 2014-10-28 11:53:30 +0000 | [diff] [blame] | 94 | makeVisible(*I, Delete); |
Rafael Espindola | 56183fb | 2012-10-29 01:59:03 +0000 | [diff] [blame] | 95 | |
Reid Kleckner | fc0f938 | 2015-07-06 18:48:02 +0000 | [diff] [blame] | 96 | if (Delete) { |
| 97 | // Make this a declaration and drop it's comdat. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 98 | I->setInitializer(nullptr); |
Reid Kleckner | fc0f938 | 2015-07-06 18:48:02 +0000 | [diff] [blame] | 99 | I->setComdat(nullptr); |
| 100 | } |
Bob Wilson | 3aecb15 | 2010-09-23 17:25:06 +0000 | [diff] [blame] | 101 | } |
Dan Gohman | 8f292e7 | 2010-08-26 00:22:55 +0000 | [diff] [blame] | 102 | |
| 103 | // Visit the Functions. |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 104 | for (Function &F : M) { |
Rafael Espindola | 56183fb | 2012-10-29 01:59:03 +0000 | [diff] [blame] | 105 | bool Delete = |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 106 | deleteStuff == (bool)Named.count(&F) && !F.isDeclaration(); |
Rafael Espindola | 56183fb | 2012-10-29 01:59:03 +0000 | [diff] [blame] | 107 | if (!Delete) { |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 108 | if (F.hasAvailableExternallyLinkage()) |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 109 | continue; |
| 110 | } |
Rafael Espindola | b77c00f | 2011-06-09 14:38:09 +0000 | [diff] [blame] | 111 | |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 112 | makeVisible(F, Delete); |
Rafael Espindola | 56183fb | 2012-10-29 01:59:03 +0000 | [diff] [blame] | 113 | |
Reid Kleckner | fc0f938 | 2015-07-06 18:48:02 +0000 | [diff] [blame] | 114 | if (Delete) { |
| 115 | // Make this a declaration and drop it's comdat. |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 116 | F.deleteBody(); |
| 117 | F.setComdat(nullptr); |
Reid Kleckner | fc0f938 | 2015-07-06 18:48:02 +0000 | [diff] [blame] | 118 | } |
Bob Wilson | 3aecb15 | 2010-09-23 17:25:06 +0000 | [diff] [blame] | 119 | } |
Andrew Lenharth | 3f13b66 | 2008-03-07 19:51:57 +0000 | [diff] [blame] | 120 | |
Rafael Espindola | 9d30d0f | 2012-10-29 00:27:55 +0000 | [diff] [blame] | 121 | // Visit the Aliases. |
| 122 | for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end(); |
| 123 | I != E;) { |
| 124 | Module::alias_iterator CurI = I; |
| 125 | ++I; |
| 126 | |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 127 | bool Delete = deleteStuff == (bool)Named.count(&*CurI); |
NAKAMURA Takumi | 335a7bc | 2014-10-28 11:53:30 +0000 | [diff] [blame] | 128 | makeVisible(*CurI, Delete); |
Rafael Espindola | 9d30d0f | 2012-10-29 00:27:55 +0000 | [diff] [blame] | 129 | |
Rafael Espindola | 6597992 | 2013-11-22 17:58:12 +0000 | [diff] [blame] | 130 | if (Delete) { |
Manuel Jacob | 5f6eaac | 2016-01-16 20:30:46 +0000 | [diff] [blame] | 131 | Type *Ty = CurI->getValueType(); |
Rafael Espindola | 9d30d0f | 2012-10-29 00:27:55 +0000 | [diff] [blame] | 132 | |
| 133 | CurI->removeFromParent(); |
| 134 | llvm::Value *Declaration; |
| 135 | if (FunctionType *FTy = dyn_cast<FunctionType>(Ty)) { |
| 136 | Declaration = Function::Create(FTy, GlobalValue::ExternalLinkage, |
Dylan McKay | f920da0 | 2018-12-18 09:52:52 +0000 | [diff] [blame] | 137 | CurI->getAddressSpace(), |
Rafael Espindola | 9d30d0f | 2012-10-29 00:27:55 +0000 | [diff] [blame] | 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 | } |