blob: 7e0dddc15d1035177308d5152b0609c6f2d96024 [file] [log] [blame]
Andrew Lenharth3f13b662008-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 Carruthed0881b2012-12-03 16:50:05 +000014#include "llvm/Transforms/IPO.h"
15#include "llvm/ADT/SetVector.h"
Chandler Carruth9fb823b2013-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 Lenharth3f13b662008-03-07 19:51:57 +000020#include "llvm/Pass.h"
Ted Kremenekd48ed172008-03-09 18:32:50 +000021#include <algorithm>
Andrew Lenharth3f13b662008-03-07 19:51:57 +000022using namespace llvm;
23
Rafael Espindola65979922013-11-22 17:58:12 +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();
Rafael Espindola65979922013-11-22 17:58:12 +000030 if (Local || Delete) {
31 GV.setLinkage(GlobalValue::ExternalLinkage);
Duncan P. N. Exon Smithe60adfd2014-05-07 23:00:22 +000032 if (Local)
33 GV.setVisibility(GlobalValue::HiddenVisibility);
Rafael Espindola65979922013-11-22 17:58:12 +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 Lenharth3f13b662008-03-07 19:51:57 +000055namespace {
56 /// @brief A pass to extract specific functions and their dependencies.
Nick Lewycky02d5f772009-10-25 06:33:48 +000057 class GVExtractorPass : public ModulePass {
Dan Gohman8f292e72010-08-26 00:22:55 +000058 SetVector<GlobalValue *> Named;
Andrew Lenharth3f13b662008-03-07 19:51:57 +000059 bool deleteStuff;
Andrew Lenharth3f13b662008-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 Gohman8f292e72010-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 Lenharth3f13b662008-03-07 19:51:57 +000069
Craig Topper3e4c6972014-03-05 09:10:37 +000070 bool runOnModule(Module &M) override {
Dan Gohman8f292e72010-08-26 00:22:55 +000071 // Visit the global inline asm.
72 if (!deleteStuff)
73 M.setModuleInlineAsm("");
Andrew Lenharth3f13b662008-03-07 19:51:57 +000074
Dan Gohman8f292e72010-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 Lenharth3f13b662008-03-07 19:51:57 +000081
Dan Gohman8f292e72010-08-26 00:22:55 +000082 // Visit the GlobalVariables.
83 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
Bob Wilson3aecb152010-09-23 17:25:06 +000084 I != E; ++I) {
Rafael Espindola56183fb2012-10-29 01:59:03 +000085 bool Delete =
86 deleteStuff == (bool)Named.count(I) && !I->isDeclaration();
87 if (!Delete) {
Bill Wendlingea6397f2012-07-19 00:11:40 +000088 if (I->hasAvailableExternallyLinkage())
89 continue;
90 if (I->getName() == "llvm.global_ctors")
91 continue;
92 }
Rafael Espindolab77c00f2011-06-09 14:38:09 +000093
NAKAMURA Takumi335a7bc2014-10-28 11:53:30 +000094 makeVisible(*I, Delete);
Rafael Espindola56183fb2012-10-29 01:59:03 +000095
96 if (Delete)
Craig Topperf40110f2014-04-25 05:29:35 +000097 I->setInitializer(nullptr);
Bob Wilson3aecb152010-09-23 17:25:06 +000098 }
Dan Gohman8f292e72010-08-26 00:22:55 +000099
100 // Visit the Functions.
Bob Wilson3aecb152010-09-23 17:25:06 +0000101 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Rafael Espindola56183fb2012-10-29 01:59:03 +0000102 bool Delete =
103 deleteStuff == (bool)Named.count(I) && !I->isDeclaration();
104 if (!Delete) {
Bill Wendlingea6397f2012-07-19 00:11:40 +0000105 if (I->hasAvailableExternallyLinkage())
106 continue;
107 }
Rafael Espindolab77c00f2011-06-09 14:38:09 +0000108
NAKAMURA Takumi335a7bc2014-10-28 11:53:30 +0000109 makeVisible(*I, Delete);
Rafael Espindola56183fb2012-10-29 01:59:03 +0000110
111 if (Delete)
112 I->deleteBody();
Bob Wilson3aecb152010-09-23 17:25:06 +0000113 }
Andrew Lenharth3f13b662008-03-07 19:51:57 +0000114
Rafael Espindola9d30d0f2012-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
NAKAMURA Takumi335a7bc2014-10-28 11:53:30 +0000121 bool Delete = deleteStuff == (bool)Named.count(CurI);
122 makeVisible(*CurI, Delete);
Rafael Espindola9d30d0f2012-10-29 00:27:55 +0000123
Rafael Espindola65979922013-11-22 17:58:12 +0000124 if (Delete) {
Rafael Espindola9d30d0f2012-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,
Craig Topperf40110f2014-04-25 05:29:35 +0000136 nullptr, CurI->getName());
Rafael Espindola9d30d0f2012-10-29 00:27:55 +0000137
138 }
139 CurI->replaceAllUsesWith(Declaration);
140 delete CurI;
141 }
142 }
143
Andrew Lenharth3f13b662008-03-07 19:51:57 +0000144 return true;
145 }
146 };
147
148 char GVExtractorPass::ID = 0;
Alexander Kornienko70bc5f12015-06-19 15:57:42 +0000149} // namespace
Andrew Lenharth3f13b662008-03-07 19:51:57 +0000150
NAKAMURA Takumid0e13af2014-10-28 11:54:52 +0000151ModulePass *llvm::createGVExtractionPass(std::vector<GlobalValue *> &GVs,
Dan Gohman8f292e72010-08-26 00:22:55 +0000152 bool deleteFn) {
153 return new GVExtractorPass(GVs, deleteFn);
Andrew Lenharth3f13b662008-03-07 19:51:57 +0000154}