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/Transforms/IPO.h"
15 #include "llvm/ADT/SetVector.h"
16 #include "llvm/Constants.h"
17 #include "llvm/Instructions.h"
18 #include "llvm/LLVMContext.h"
19 #include "llvm/Module.h"
20 #include "llvm/Pass.h"
25 /// @brief A pass to extract specific functions and their dependencies.
26 class GVExtractorPass : public ModulePass {
27 SetVector<GlobalValue *> Named;
30 static char ID; // Pass identification, replacement for typeid
32 /// FunctionExtractorPass - If deleteFn is true, this pass deletes as the
33 /// specified function. Otherwise, it deletes as much of the module as
34 /// possible, except for the function specified.
36 explicit GVExtractorPass(std::vector<GlobalValue*>& GVs, bool deleteS = true)
37 : ModulePass(ID), Named(GVs.begin(), GVs.end()), deleteStuff(deleteS) {}
39 bool runOnModule(Module &M) {
40 // Visit the global inline asm.
42 M.setModuleInlineAsm("");
44 // For simplicity, just give all GlobalValues ExternalLinkage. A trickier
45 // implementation could figure out which GlobalValues are actually
46 // referenced by the Named set, and which GlobalValues in the rest of
47 // the module are referenced by the NamedSet, and get away with leaving
48 // more internal and private things internal and private. But for now,
49 // be conservative and simple.
51 // Visit the GlobalVariables.
52 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
55 deleteStuff == (bool)Named.count(I) && !I->isDeclaration();
57 if (I->hasAvailableExternallyLinkage())
59 if (I->getName() == "llvm.global_ctors")
63 bool Local = I->hasLocalLinkage();
65 I->setVisibility(GlobalValue::HiddenVisibility);
68 I->setLinkage(GlobalValue::ExternalLinkage);
74 // Visit the Functions.
75 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
77 deleteStuff == (bool)Named.count(I) && !I->isDeclaration();
79 if (I->hasAvailableExternallyLinkage())
83 bool Local = I->hasLocalLinkage();
85 I->setVisibility(GlobalValue::HiddenVisibility);
88 I->setLinkage(GlobalValue::ExternalLinkage);
95 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
97 Module::alias_iterator CurI = I;
100 if (CurI->hasLocalLinkage()) {
101 CurI->setVisibility(GlobalValue::HiddenVisibility);
102 CurI->setLinkage(GlobalValue::ExternalLinkage);
105 if (deleteStuff == (bool)Named.count(CurI)) {
106 Type *Ty = CurI->getType()->getElementType();
108 CurI->removeFromParent();
109 llvm::Value *Declaration;
110 if (FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
111 Declaration = Function::Create(FTy, GlobalValue::ExternalLinkage,
112 CurI->getName(), &M);
116 new GlobalVariable(M, Ty, false, GlobalValue::ExternalLinkage,
120 CurI->replaceAllUsesWith(Declaration);
129 char GVExtractorPass::ID = 0;
132 ModulePass *llvm::createGVExtractionPass(std::vector<GlobalValue*>& GVs,
134 return new GVExtractorPass(GVs, deleteFn);