1 //===-- ExtractGV.cpp - Global Value extraction pass ----------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This pass extracts global values
11 //===----------------------------------------------------------------------===//
13 #include "llvm/ADT/SetVector.h"
14 #include "llvm/IR/LLVMContext.h"
15 #include "llvm/IR/Module.h"
16 #include "llvm/Pass.h"
17 #include "llvm/Transforms/IPO.h"
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();
27 if (Local
|| Delete
) {
28 GV
.setLinkage(GlobalValue::ExternalLinkage
);
30 GV
.setVisibility(GlobalValue::HiddenVisibility
);
34 if (!GV
.hasLinkOnceLinkage()) {
35 assert(!GV
.isDiscardableIfUnused());
39 // Map linkonce* to weak* so that llvm doesn't drop this GV.
40 switch(GV
.getLinkage()) {
42 llvm_unreachable("Unexpected linkage");
43 case GlobalValue::LinkOnceAnyLinkage
:
44 GV
.setLinkage(GlobalValue::WeakAnyLinkage
);
46 case GlobalValue::LinkOnceODRLinkage
:
47 GV
.setLinkage(GlobalValue::WeakODRLinkage
);
53 /// A pass to extract specific global values and their dependencies.
54 class GVExtractorPass
: public ModulePass
{
55 SetVector
<GlobalValue
*> Named
;
58 static char ID
; // Pass identification, replacement for typeid
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
,
65 : ModulePass(ID
), Named(GVs
.begin(), GVs
.end()), deleteStuff(deleteS
) {}
67 bool runOnModule(Module
&M
) override
{
71 // Visit the global inline asm.
73 M
.setModuleInlineAsm("");
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.
82 // Visit the GlobalVariables.
83 for (Module::global_iterator I
= M
.global_begin(), E
= M
.global_end();
86 deleteStuff
== (bool)Named
.count(&*I
) && !I
->isDeclaration();
88 if (I
->hasAvailableExternallyLinkage())
90 if (I
->getName() == "llvm.global_ctors")
94 makeVisible(*I
, Delete
);
97 // Make this a declaration and drop it's comdat.
98 I
->setInitializer(nullptr);
99 I
->setComdat(nullptr);
103 // Visit the Functions.
104 for (Function
&F
: M
) {
106 deleteStuff
== (bool)Named
.count(&F
) && !F
.isDeclaration();
108 if (F
.hasAvailableExternallyLinkage())
112 makeVisible(F
, Delete
);
115 // Make this a declaration and drop it's comdat.
117 F
.setComdat(nullptr);
121 // Visit the Aliases.
122 for (Module::alias_iterator I
= M
.alias_begin(), E
= M
.alias_end();
124 Module::alias_iterator CurI
= I
;
127 bool Delete
= deleteStuff
== (bool)Named
.count(&*CurI
);
128 makeVisible(*CurI
, Delete
);
131 Type
*Ty
= CurI
->getValueType();
133 CurI
->removeFromParent();
134 llvm::Value
*Declaration
;
135 if (FunctionType
*FTy
= dyn_cast
<FunctionType
>(Ty
)) {
136 Declaration
= Function::Create(FTy
, GlobalValue::ExternalLinkage
,
137 CurI
->getAddressSpace(),
138 CurI
->getName(), &M
);
142 new GlobalVariable(M
, Ty
, false, GlobalValue::ExternalLinkage
,
143 nullptr, CurI
->getName());
146 CurI
->replaceAllUsesWith(Declaration
);
155 char GVExtractorPass::ID
= 0;
158 ModulePass
*llvm::createGVExtractionPass(std::vector
<GlobalValue
*> &GVs
,
160 return new GVExtractorPass(GVs
, deleteFn
);