blob: 50fb3e691c5fd8aff6905e22f26824a3757e21d9 [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
Chandler Carruthd04a8d42012-12-03 16:50:05 +000014#include "llvm/Transforms/IPO.h"
15#include "llvm/ADT/SetVector.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000016#include "llvm/IR/Constants.h"
17#include "llvm/IR/Instructions.h"
18#include "llvm/IR/LLVMContext.h"
19#include "llvm/IR/Module.h"
Andrew Lenharthd245a8a2008-03-07 19:51:57 +000020#include "llvm/Pass.h"
Ted Kremenek58d5e052008-03-09 18:32:50 +000021#include <algorithm>
Andrew Lenharthd245a8a2008-03-07 19:51:57 +000022using namespace llvm;
23
Bill Wendling3343ddf2013-11-25 05:20:58 +000024/// 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.
28static void makeVisible(GlobalValue &GV, bool Delete) {
29 bool Local = GV.hasLocalLinkage();
30 if (Local)
31 GV.setVisibility(GlobalValue::HiddenVisibility);
32
33 if (Local || Delete) {
34 GV.setLinkage(GlobalValue::ExternalLinkage);
35 return;
36 }
37
38 if (!GV.hasLinkOnceLinkage()) {
39 assert(!GV.isDiscardableIfUnused());
40 return;
41 }
42
43 // Map linkonce* to weak* so that llvm doesn't drop this GV.
44 switch(GV.getLinkage()) {
45 default:
46 llvm_unreachable("Unexpected linkage");
47 case GlobalValue::LinkOnceAnyLinkage:
48 GV.setLinkage(GlobalValue::WeakAnyLinkage);
49 return;
50 case GlobalValue::LinkOnceODRLinkage:
51 GV.setLinkage(GlobalValue::WeakODRLinkage);
52 return;
53 }
54}
55
Andrew Lenharthd245a8a2008-03-07 19:51:57 +000056namespace {
57 /// @brief A pass to extract specific functions and their dependencies.
Nick Lewycky6726b6d2009-10-25 06:33:48 +000058 class GVExtractorPass : public ModulePass {
Dan Gohmanb4e3cda2010-08-26 00:22:55 +000059 SetVector<GlobalValue *> Named;
Andrew Lenharthd245a8a2008-03-07 19:51:57 +000060 bool deleteStuff;
Andrew Lenharthd245a8a2008-03-07 19:51:57 +000061 public:
62 static char ID; // Pass identification, replacement for typeid
63
64 /// FunctionExtractorPass - If deleteFn is true, this pass deletes as the
65 /// specified function. Otherwise, it deletes as much of the module as
66 /// possible, except for the function specified.
67 ///
Dan Gohmanb4e3cda2010-08-26 00:22:55 +000068 explicit GVExtractorPass(std::vector<GlobalValue*>& GVs, bool deleteS = true)
69 : ModulePass(ID), Named(GVs.begin(), GVs.end()), deleteStuff(deleteS) {}
Andrew Lenharthd245a8a2008-03-07 19:51:57 +000070
71 bool runOnModule(Module &M) {
Dan Gohmanb4e3cda2010-08-26 00:22:55 +000072 // Visit the global inline asm.
73 if (!deleteStuff)
74 M.setModuleInlineAsm("");
Andrew Lenharthd245a8a2008-03-07 19:51:57 +000075
Dan Gohmanb4e3cda2010-08-26 00:22:55 +000076 // For simplicity, just give all GlobalValues ExternalLinkage. A trickier
77 // implementation could figure out which GlobalValues are actually
78 // referenced by the Named set, and which GlobalValues in the rest of
79 // the module are referenced by the NamedSet, and get away with leaving
80 // more internal and private things internal and private. But for now,
81 // be conservative and simple.
Andrew Lenharthd245a8a2008-03-07 19:51:57 +000082
Dan Gohmanb4e3cda2010-08-26 00:22:55 +000083 // Visit the GlobalVariables.
84 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
Bob Wilsonedf01742010-09-23 17:25:06 +000085 I != E; ++I) {
Rafael Espindola9cb90e72012-10-29 01:59:03 +000086 bool Delete =
87 deleteStuff == (bool)Named.count(I) && !I->isDeclaration();
88 if (!Delete) {
Bill Wendling56cb2292012-07-19 00:11:40 +000089 if (I->hasAvailableExternallyLinkage())
90 continue;
91 if (I->getName() == "llvm.global_ctors")
92 continue;
93 }
Rafael Espindolad896d412011-06-09 14:38:09 +000094
Bill Wendling3343ddf2013-11-25 05:20:58 +000095 makeVisible(*I, Delete);
Rafael Espindola9cb90e72012-10-29 01:59:03 +000096
97 if (Delete)
98 I->setInitializer(0);
Bob Wilsonedf01742010-09-23 17:25:06 +000099 }
Dan Gohmanb4e3cda2010-08-26 00:22:55 +0000100
101 // Visit the Functions.
Bob Wilsonedf01742010-09-23 17:25:06 +0000102 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Rafael Espindola9cb90e72012-10-29 01:59:03 +0000103 bool Delete =
104 deleteStuff == (bool)Named.count(I) && !I->isDeclaration();
105 if (!Delete) {
Bill Wendling56cb2292012-07-19 00:11:40 +0000106 if (I->hasAvailableExternallyLinkage())
107 continue;
108 }
Rafael Espindolad896d412011-06-09 14:38:09 +0000109
Bill Wendling3343ddf2013-11-25 05:20:58 +0000110 makeVisible(*I, Delete);
Rafael Espindola9cb90e72012-10-29 01:59:03 +0000111
112 if (Delete)
113 I->deleteBody();
Bob Wilsonedf01742010-09-23 17:25:06 +0000114 }
Andrew Lenharthd245a8a2008-03-07 19:51:57 +0000115
Rafael Espindolac0916d32012-10-29 00:27:55 +0000116 // Visit the Aliases.
117 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
118 I != E;) {
119 Module::alias_iterator CurI = I;
120 ++I;
121
Bill Wendling3343ddf2013-11-25 05:20:58 +0000122 bool Delete = deleteStuff == (bool)Named.count(CurI);
123 makeVisible(*CurI, Delete);
Rafael Espindolac0916d32012-10-29 00:27:55 +0000124
Bill Wendling3343ddf2013-11-25 05:20:58 +0000125 if (Delete) {
Rafael Espindolac0916d32012-10-29 00:27:55 +0000126 Type *Ty = CurI->getType()->getElementType();
127
128 CurI->removeFromParent();
129 llvm::Value *Declaration;
130 if (FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
131 Declaration = Function::Create(FTy, GlobalValue::ExternalLinkage,
132 CurI->getName(), &M);
133
134 } else {
135 Declaration =
136 new GlobalVariable(M, Ty, false, GlobalValue::ExternalLinkage,
137 0, CurI->getName());
138
139 }
140 CurI->replaceAllUsesWith(Declaration);
141 delete CurI;
142 }
143 }
144
Andrew Lenharthd245a8a2008-03-07 19:51:57 +0000145 return true;
146 }
147 };
148
149 char GVExtractorPass::ID = 0;
150}
151
152ModulePass *llvm::createGVExtractionPass(std::vector<GlobalValue*>& GVs,
Dan Gohmanb4e3cda2010-08-26 00:22:55 +0000153 bool deleteFn) {
154 return new GVExtractorPass(GVs, deleteFn);
Andrew Lenharthd245a8a2008-03-07 19:51:57 +0000155}