blob: 57c2f6d410d1a5687e5bc7b4996b4c1c7799a665 [file] [log] [blame]
Andrew Lenharthd245a8a2008-03-07 19:51:57 +00001//===-- ExtractGV.cpp - Global Value extraction pass ----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This pass extracts global values
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Instructions.h"
Owen Anderson14ce9ef2009-07-06 01:34:54 +000015#include "llvm/LLVMContext.h"
Andrew Lenharthd245a8a2008-03-07 19:51:57 +000016#include "llvm/Module.h"
17#include "llvm/Pass.h"
18#include "llvm/Constants.h"
19#include "llvm/Transforms/IPO.h"
Dan Gohmanb4e3cda2010-08-26 00:22:55 +000020#include "llvm/ADT/SetVector.h"
Ted Kremenek58d5e052008-03-09 18:32:50 +000021#include <algorithm>
Andrew Lenharthd245a8a2008-03-07 19:51:57 +000022using namespace llvm;
23
24namespace {
25 /// @brief A pass to extract specific functions and their dependencies.
Nick Lewycky6726b6d2009-10-25 06:33:48 +000026 class GVExtractorPass : public ModulePass {
Dan Gohmanb4e3cda2010-08-26 00:22:55 +000027 SetVector<GlobalValue *> Named;
Andrew Lenharthd245a8a2008-03-07 19:51:57 +000028 bool deleteStuff;
Andrew Lenharthd245a8a2008-03-07 19:51:57 +000029 public:
30 static char ID; // Pass identification, replacement for typeid
31
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.
35 ///
Dan Gohmanb4e3cda2010-08-26 00:22:55 +000036 explicit GVExtractorPass(std::vector<GlobalValue*>& GVs, bool deleteS = true)
37 : ModulePass(ID), Named(GVs.begin(), GVs.end()), deleteStuff(deleteS) {}
Andrew Lenharthd245a8a2008-03-07 19:51:57 +000038
39 bool runOnModule(Module &M) {
Dan Gohmanb4e3cda2010-08-26 00:22:55 +000040 // Visit the global inline asm.
41 if (!deleteStuff)
42 M.setModuleInlineAsm("");
Andrew Lenharthd245a8a2008-03-07 19:51:57 +000043
Dan Gohmanb4e3cda2010-08-26 00:22:55 +000044 // 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.
Andrew Lenharthd245a8a2008-03-07 19:51:57 +000050
Dan Gohmanb4e3cda2010-08-26 00:22:55 +000051 // Visit the GlobalVariables.
52 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
Bob Wilsonedf01742010-09-23 17:25:06 +000053 I != E; ++I) {
Rafael Espindolad896d412011-06-09 14:38:09 +000054 if (deleteStuff == (bool)Named.count(I) && !I->isDeclaration()) {
55 I->setInitializer(0);
Bill Wendling56cb2292012-07-19 00:11:40 +000056 } else {
57 if (I->hasAvailableExternallyLinkage())
58 continue;
59 if (I->getName() == "llvm.global_ctors")
60 continue;
61 }
Rafael Espindolad896d412011-06-09 14:38:09 +000062
Bob Wilsonedf01742010-09-23 17:25:06 +000063 if (I->hasLocalLinkage())
64 I->setVisibility(GlobalValue::HiddenVisibility);
65 I->setLinkage(GlobalValue::ExternalLinkage);
Bob Wilsonedf01742010-09-23 17:25:06 +000066 }
Dan Gohmanb4e3cda2010-08-26 00:22:55 +000067
68 // Visit the Functions.
Bob Wilsonedf01742010-09-23 17:25:06 +000069 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Rafael Espindolad896d412011-06-09 14:38:09 +000070 if (deleteStuff == (bool)Named.count(I) && !I->isDeclaration()) {
71 I->deleteBody();
Bill Wendling56cb2292012-07-19 00:11:40 +000072 } else {
73 if (I->hasAvailableExternallyLinkage())
74 continue;
75 }
Rafael Espindolad896d412011-06-09 14:38:09 +000076
Bob Wilsonedf01742010-09-23 17:25:06 +000077 if (I->hasLocalLinkage())
78 I->setVisibility(GlobalValue::HiddenVisibility);
79 I->setLinkage(GlobalValue::ExternalLinkage);
Bob Wilsonedf01742010-09-23 17:25:06 +000080 }
Andrew Lenharthd245a8a2008-03-07 19:51:57 +000081
Rafael Espindolac0916d32012-10-29 00:27:55 +000082 // Visit the Aliases.
83 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
84 I != E;) {
85 Module::alias_iterator CurI = I;
86 ++I;
87
88 if (CurI->hasLocalLinkage())
89 CurI->setVisibility(GlobalValue::HiddenVisibility);
90 CurI->setLinkage(GlobalValue::ExternalLinkage);
91
92 if (deleteStuff == (bool)Named.count(CurI)) {
93 Type *Ty = CurI->getType()->getElementType();
94
95 CurI->removeFromParent();
96 llvm::Value *Declaration;
97 if (FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
98 Declaration = Function::Create(FTy, GlobalValue::ExternalLinkage,
99 CurI->getName(), &M);
100
101 } else {
102 Declaration =
103 new GlobalVariable(M, Ty, false, GlobalValue::ExternalLinkage,
104 0, CurI->getName());
105
106 }
107 CurI->replaceAllUsesWith(Declaration);
108 delete CurI;
109 }
110 }
111
Andrew Lenharthd245a8a2008-03-07 19:51:57 +0000112 return true;
113 }
114 };
115
116 char GVExtractorPass::ID = 0;
117}
118
119ModulePass *llvm::createGVExtractionPass(std::vector<GlobalValue*>& GVs,
Dan Gohmanb4e3cda2010-08-26 00:22:55 +0000120 bool deleteFn) {
121 return new GVExtractorPass(GVs, deleteFn);
Andrew Lenharthd245a8a2008-03-07 19:51:57 +0000122}