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/Transforms/IPO/ExtractGV.h"
14 #include "llvm/IR/Module.h"
15 #include "llvm/IR/PassManager.h"
20 /// Make sure GV is visible from both modules. Delete is true if it is
21 /// being deleted from this module.
22 /// This also makes sure GV cannot be dropped so that references from
23 /// the split module remain valid.
24 static void makeVisible(GlobalValue
&GV
, bool Delete
) {
25 bool Local
= GV
.hasLocalLinkage();
26 if (Local
|| Delete
) {
27 GV
.setLinkage(GlobalValue::ExternalLinkage
);
29 GV
.setVisibility(GlobalValue::HiddenVisibility
);
33 if (!GV
.hasLinkOnceLinkage()) {
34 assert(!GV
.isDiscardableIfUnused());
38 // Map linkonce* to weak* so that llvm doesn't drop this GV.
39 switch (GV
.getLinkage()) {
41 llvm_unreachable("Unexpected linkage");
42 case GlobalValue::LinkOnceAnyLinkage
:
43 GV
.setLinkage(GlobalValue::WeakAnyLinkage
);
45 case GlobalValue::LinkOnceODRLinkage
:
46 GV
.setLinkage(GlobalValue::WeakODRLinkage
);
51 /// If deleteS is true, this pass deletes the specified global values.
52 /// Otherwise, it deletes as much of the module as possible, except for the
53 /// global values specified.
54 ExtractGVPass::ExtractGVPass(std::vector
<GlobalValue
*> &GVs
, bool deleteS
,
56 : Named(GVs
.begin(), GVs
.end()), deleteStuff(deleteS
),
57 keepConstInit(keepConstInit
) {}
59 PreservedAnalyses
ExtractGVPass::run(Module
&M
, ModuleAnalysisManager
&) {
60 // Visit the global inline asm.
62 M
.setModuleInlineAsm("");
64 // For simplicity, just give all GlobalValues ExternalLinkage. A trickier
65 // implementation could figure out which GlobalValues are actually
66 // referenced by the Named set, and which GlobalValues in the rest of
67 // the module are referenced by the NamedSet, and get away with leaving
68 // more internal and private things internal and private. But for now,
69 // be conservative and simple.
71 // Visit the GlobalVariables.
72 for (GlobalVariable
&GV
: M
.globals()) {
73 bool Delete
= deleteStuff
== (bool)Named
.count(&GV
) &&
74 !GV
.isDeclaration() && (!GV
.isConstant() || !keepConstInit
);
76 if (GV
.hasAvailableExternallyLinkage())
78 if (GV
.getName() == "llvm.global_ctors")
82 makeVisible(GV
, Delete
);
85 // Make this a declaration and drop it's comdat.
86 GV
.setInitializer(nullptr);
87 GV
.setComdat(nullptr);
91 // Visit the Functions.
92 for (Function
&F
: M
) {
93 bool Delete
= deleteStuff
== (bool)Named
.count(&F
) && !F
.isDeclaration();
95 if (F
.hasAvailableExternallyLinkage())
99 makeVisible(F
, Delete
);
102 // Make this a declaration and drop it's comdat.
104 F
.setComdat(nullptr);
108 // Visit the Aliases.
109 for (GlobalAlias
&GA
: llvm::make_early_inc_range(M
.aliases())) {
110 bool Delete
= deleteStuff
== (bool)Named
.count(&GA
);
111 makeVisible(GA
, Delete
);
114 Type
*Ty
= GA
.getValueType();
116 GA
.removeFromParent();
117 llvm::Value
*Declaration
;
118 if (FunctionType
*FTy
= dyn_cast
<FunctionType
>(Ty
)) {
119 Declaration
= Function::Create(FTy
, GlobalValue::ExternalLinkage
,
120 GA
.getAddressSpace(), GA
.getName(), &M
);
123 Declaration
= new GlobalVariable(
124 M
, Ty
, false, GlobalValue::ExternalLinkage
, nullptr, GA
.getName());
126 GA
.replaceAllUsesWith(Declaration
);
132 for (GlobalIFunc
&IF
: llvm::make_early_inc_range(M
.ifuncs())) {
133 bool Delete
= deleteStuff
== (bool)Named
.count(&IF
);
134 makeVisible(IF
, Delete
);
139 auto *FuncType
= dyn_cast
<FunctionType
>(IF
.getValueType());
140 IF
.removeFromParent();
141 llvm::Value
*Declaration
=
142 Function::Create(FuncType
, GlobalValue::ExternalLinkage
,
143 IF
.getAddressSpace(), IF
.getName(), &M
);
144 IF
.replaceAllUsesWith(Declaration
);
148 return PreservedAnalyses::none();