blob: 40ec9fa8c1dea825b5b4b8a15e0dd2bdc85f3701 [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();
Bill Wendling3343ddf2013-11-25 05:20:58 +000030 if (Local || Delete) {
31 GV.setLinkage(GlobalValue::ExternalLinkage);
Stephen Hinesdce4a402014-05-29 02:49:00 -070032 if (Local)
33 GV.setVisibility(GlobalValue::HiddenVisibility);
Bill Wendling3343ddf2013-11-25 05:20:58 +000034 return;
35 }
36
37 if (!GV.hasLinkOnceLinkage()) {
38 assert(!GV.isDiscardableIfUnused());
39 return;
40 }
41
42 // Map linkonce* to weak* so that llvm doesn't drop this GV.
43 switch(GV.getLinkage()) {
44 default:
45 llvm_unreachable("Unexpected linkage");
46 case GlobalValue::LinkOnceAnyLinkage:
47 GV.setLinkage(GlobalValue::WeakAnyLinkage);
48 return;
49 case GlobalValue::LinkOnceODRLinkage:
50 GV.setLinkage(GlobalValue::WeakODRLinkage);
51 return;
52 }
53}
54
Andrew Lenharthd245a8a2008-03-07 19:51:57 +000055namespace {
56 /// @brief A pass to extract specific functions and their dependencies.
Nick Lewycky6726b6d2009-10-25 06:33:48 +000057 class GVExtractorPass : public ModulePass {
Dan Gohmanb4e3cda2010-08-26 00:22:55 +000058 SetVector<GlobalValue *> Named;
Andrew Lenharthd245a8a2008-03-07 19:51:57 +000059 bool deleteStuff;
Andrew Lenharthd245a8a2008-03-07 19:51:57 +000060 public:
61 static char ID; // Pass identification, replacement for typeid
62
63 /// FunctionExtractorPass - If deleteFn is true, this pass deletes as the
64 /// specified function. Otherwise, it deletes as much of the module as
65 /// possible, except for the function specified.
66 ///
Dan Gohmanb4e3cda2010-08-26 00:22:55 +000067 explicit GVExtractorPass(std::vector<GlobalValue*>& GVs, bool deleteS = true)
68 : ModulePass(ID), Named(GVs.begin(), GVs.end()), deleteStuff(deleteS) {}
Andrew Lenharthd245a8a2008-03-07 19:51:57 +000069
Stephen Hines36b56882014-04-23 16:57:46 -070070 bool runOnModule(Module &M) override {
Dan Gohmanb4e3cda2010-08-26 00:22:55 +000071 // Visit the global inline asm.
72 if (!deleteStuff)
73 M.setModuleInlineAsm("");
Andrew Lenharthd245a8a2008-03-07 19:51:57 +000074
Dan Gohmanb4e3cda2010-08-26 00:22:55 +000075 // For simplicity, just give all GlobalValues ExternalLinkage. A trickier
76 // implementation could figure out which GlobalValues are actually
77 // referenced by the Named set, and which GlobalValues in the rest of
78 // the module are referenced by the NamedSet, and get away with leaving
79 // more internal and private things internal and private. But for now,
80 // be conservative and simple.
Andrew Lenharthd245a8a2008-03-07 19:51:57 +000081
Dan Gohmanb4e3cda2010-08-26 00:22:55 +000082 // Visit the GlobalVariables.
83 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
Bob Wilsonedf01742010-09-23 17:25:06 +000084 I != E; ++I) {
Rafael Espindola9cb90e72012-10-29 01:59:03 +000085 bool Delete =
86 deleteStuff == (bool)Named.count(I) && !I->isDeclaration();
87 if (!Delete) {
Bill Wendling56cb2292012-07-19 00:11:40 +000088 if (I->hasAvailableExternallyLinkage())
89 continue;
90 if (I->getName() == "llvm.global_ctors")
91 continue;
92 }
Rafael Espindolad896d412011-06-09 14:38:09 +000093
Bill Wendling3343ddf2013-11-25 05:20:58 +000094 makeVisible(*I, Delete);
Rafael Espindola9cb90e72012-10-29 01:59:03 +000095
96 if (Delete)
Stephen Hinesdce4a402014-05-29 02:49:00 -070097 I->setInitializer(nullptr);
Bob Wilsonedf01742010-09-23 17:25:06 +000098 }
Dan Gohmanb4e3cda2010-08-26 00:22:55 +000099
100 // Visit the Functions.
Bob Wilsonedf01742010-09-23 17:25:06 +0000101 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Rafael Espindola9cb90e72012-10-29 01:59:03 +0000102 bool Delete =
103 deleteStuff == (bool)Named.count(I) && !I->isDeclaration();
104 if (!Delete) {
Bill Wendling56cb2292012-07-19 00:11:40 +0000105 if (I->hasAvailableExternallyLinkage())
106 continue;
107 }
Rafael Espindolad896d412011-06-09 14:38:09 +0000108
Bill Wendling3343ddf2013-11-25 05:20:58 +0000109 makeVisible(*I, Delete);
Rafael Espindola9cb90e72012-10-29 01:59:03 +0000110
111 if (Delete)
112 I->deleteBody();
Bob Wilsonedf01742010-09-23 17:25:06 +0000113 }
Andrew Lenharthd245a8a2008-03-07 19:51:57 +0000114
Rafael Espindolac0916d32012-10-29 00:27:55 +0000115 // Visit the Aliases.
116 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
117 I != E;) {
118 Module::alias_iterator CurI = I;
119 ++I;
120
Bill Wendling3343ddf2013-11-25 05:20:58 +0000121 bool Delete = deleteStuff == (bool)Named.count(CurI);
122 makeVisible(*CurI, Delete);
Rafael Espindolac0916d32012-10-29 00:27:55 +0000123
Bill Wendling3343ddf2013-11-25 05:20:58 +0000124 if (Delete) {
Rafael Espindolac0916d32012-10-29 00:27:55 +0000125 Type *Ty = CurI->getType()->getElementType();
126
127 CurI->removeFromParent();
128 llvm::Value *Declaration;
129 if (FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
130 Declaration = Function::Create(FTy, GlobalValue::ExternalLinkage,
131 CurI->getName(), &M);
132
133 } else {
134 Declaration =
135 new GlobalVariable(M, Ty, false, GlobalValue::ExternalLinkage,
Stephen Hinesdce4a402014-05-29 02:49:00 -0700136 nullptr, CurI->getName());
Rafael Espindolac0916d32012-10-29 00:27:55 +0000137
138 }
139 CurI->replaceAllUsesWith(Declaration);
140 delete CurI;
141 }
142 }
143
Andrew Lenharthd245a8a2008-03-07 19:51:57 +0000144 return true;
145 }
146 };
147
148 char GVExtractorPass::ID = 0;
149}
150
151ModulePass *llvm::createGVExtractionPass(std::vector<GlobalValue*>& GVs,
Dan Gohmanb4e3cda2010-08-26 00:22:55 +0000152 bool deleteFn) {
153 return new GVExtractorPass(GVs, deleteFn);
Andrew Lenharthd245a8a2008-03-07 19:51:57 +0000154}