1 //===-- ExtractGV.cpp - Global Value extraction pass ----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass extracts global values
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ADT/SetVector.h"
15 #include "llvm/IR/Constants.h"
16 #include "llvm/IR/Instructions.h"
17 #include "llvm/IR/LLVMContext.h"
18 #include "llvm/IR/Module.h"
19 #include "llvm/Pass.h"
20 #include "llvm/Transforms/IPO.h"
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();
30 if (Local || Delete) {
31 GV.setLinkage(GlobalValue::ExternalLinkage);
33 GV.setVisibility(GlobalValue::HiddenVisibility);
37 if (!GV.hasLinkOnceLinkage()) {
38 assert(!GV.isDiscardableIfUnused());
42 // Map linkonce* to weak* so that llvm doesn't drop this GV.
43 switch(GV.getLinkage()) {
45 llvm_unreachable("Unexpected linkage");
46 case GlobalValue::LinkOnceAnyLinkage:
47 GV.setLinkage(GlobalValue::WeakAnyLinkage);
49 case GlobalValue::LinkOnceODRLinkage:
50 GV.setLinkage(GlobalValue::WeakODRLinkage);
56 /// @brief A pass to extract specific global values and their dependencies.
57 class GVExtractorPass : public ModulePass {
58 SetVector<GlobalValue *> Named;
61 static char ID; // Pass identification, replacement for typeid
63 /// If deleteS is true, this pass deletes the specified global values.
64 /// Otherwise, it deletes as much of the module as possible, except for the
65 /// global values specified.
66 explicit GVExtractorPass(std::vector<GlobalValue*> &GVs,
68 : ModulePass(ID), Named(GVs.begin(), GVs.end()), deleteStuff(deleteS) {}
70 bool runOnModule(Module &M) override {
74 // Visit the global inline asm.
76 M.setModuleInlineAsm("");
78 // For simplicity, just give all GlobalValues ExternalLinkage. A trickier
79 // implementation could figure out which GlobalValues are actually
80 // referenced by the Named set, and which GlobalValues in the rest of
81 // the module are referenced by the NamedSet, and get away with leaving
82 // more internal and private things internal and private. But for now,
83 // be conservative and simple.
85 // Visit the GlobalVariables.
86 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
89 deleteStuff == (bool)Named.count(&*I) && !I->isDeclaration();
91 if (I->hasAvailableExternallyLinkage())
93 if (I->getName() == "llvm.global_ctors")
97 makeVisible(*I, Delete);
100 // Make this a declaration and drop it's comdat.
101 I->setInitializer(nullptr);
102 I->setComdat(nullptr);
106 // Visit the Functions.
107 for (Function &F : M) {
109 deleteStuff == (bool)Named.count(&F) && !F.isDeclaration();
111 if (F.hasAvailableExternallyLinkage())
115 makeVisible(F, Delete);
118 // Make this a declaration and drop it's comdat.
120 F.setComdat(nullptr);
124 // Visit the Aliases.
125 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
127 Module::alias_iterator CurI = I;
130 bool Delete = deleteStuff == (bool)Named.count(&*CurI);
131 makeVisible(*CurI, Delete);
134 Type *Ty = CurI->getValueType();
136 CurI->removeFromParent();
137 llvm::Value *Declaration;
138 if (FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
139 Declaration = Function::Create(FTy, GlobalValue::ExternalLinkage,
140 CurI->getName(), &M);
144 new GlobalVariable(M, Ty, false, GlobalValue::ExternalLinkage,
145 nullptr, CurI->getName());
148 CurI->replaceAllUsesWith(Declaration);
157 char GVExtractorPass::ID = 0;
160 ModulePass *llvm::createGVExtractionPass(std::vector<GlobalValue *> &GVs,
162 return new GVExtractorPass(GVs, deleteFn);