blob: d45a8832391095f3b5f9584fdf428cd2830a6a57 [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/ADT/SetVector.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000015#include "llvm/IR/LLVMContext.h"
16#include "llvm/IR/Module.h"
Andrew Lenharth3f13b662008-03-07 19:51:57 +000017#include "llvm/Pass.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000018#include "llvm/Transforms/IPO.h"
Ted Kremenekd48ed172008-03-09 18:32:50 +000019#include <algorithm>
Andrew Lenharth3f13b662008-03-07 19:51:57 +000020using namespace llvm;
21
Rafael Espindola65979922013-11-22 17:58:12 +000022/// Make sure GV is visible from both modules. Delete is true if it is
23/// being deleted from this module.
24/// This also makes sure GV cannot be dropped so that references from
25/// the split module remain valid.
26static void makeVisible(GlobalValue &GV, bool Delete) {
27 bool Local = GV.hasLocalLinkage();
Rafael Espindola65979922013-11-22 17:58:12 +000028 if (Local || Delete) {
29 GV.setLinkage(GlobalValue::ExternalLinkage);
Duncan P. N. Exon Smithe60adfd2014-05-07 23:00:22 +000030 if (Local)
31 GV.setVisibility(GlobalValue::HiddenVisibility);
Rafael Espindola65979922013-11-22 17:58:12 +000032 return;
33 }
34
35 if (!GV.hasLinkOnceLinkage()) {
36 assert(!GV.isDiscardableIfUnused());
37 return;
38 }
39
40 // Map linkonce* to weak* so that llvm doesn't drop this GV.
41 switch(GV.getLinkage()) {
42 default:
43 llvm_unreachable("Unexpected linkage");
44 case GlobalValue::LinkOnceAnyLinkage:
45 GV.setLinkage(GlobalValue::WeakAnyLinkage);
46 return;
47 case GlobalValue::LinkOnceODRLinkage:
48 GV.setLinkage(GlobalValue::WeakODRLinkage);
49 return;
50 }
51}
52
Andrew Lenharth3f13b662008-03-07 19:51:57 +000053namespace {
Adrian Prantl4dfcc4a2018-05-01 16:10:38 +000054 /// A pass to extract specific global values and their dependencies.
Nick Lewycky02d5f772009-10-25 06:33:48 +000055 class GVExtractorPass : public ModulePass {
Dan Gohman8f292e72010-08-26 00:22:55 +000056 SetVector<GlobalValue *> Named;
Andrew Lenharth3f13b662008-03-07 19:51:57 +000057 bool deleteStuff;
Andrew Lenharth3f13b662008-03-07 19:51:57 +000058 public:
59 static char ID; // Pass identification, replacement for typeid
60
Craig Topper2aa4d392017-06-08 23:38:19 +000061 /// If deleteS is true, this pass deletes the specified global values.
62 /// Otherwise, it deletes as much of the module as possible, except for the
63 /// global values specified.
64 explicit GVExtractorPass(std::vector<GlobalValue*> &GVs,
65 bool deleteS = true)
Dan Gohman8f292e72010-08-26 00:22:55 +000066 : ModulePass(ID), Named(GVs.begin(), GVs.end()), deleteStuff(deleteS) {}
Andrew Lenharth3f13b662008-03-07 19:51:57 +000067
Craig Topper3e4c6972014-03-05 09:10:37 +000068 bool runOnModule(Module &M) override {
Andrew Kayloraa641a52016-04-22 22:06:11 +000069 if (skipModule(M))
70 return false;
71
Dan Gohman8f292e72010-08-26 00:22:55 +000072 // Visit the global inline asm.
73 if (!deleteStuff)
74 M.setModuleInlineAsm("");
Andrew Lenharth3f13b662008-03-07 19:51:57 +000075
Dan Gohman8f292e72010-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 Lenharth3f13b662008-03-07 19:51:57 +000082
Dan Gohman8f292e72010-08-26 00:22:55 +000083 // Visit the GlobalVariables.
84 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
Bob Wilson3aecb152010-09-23 17:25:06 +000085 I != E; ++I) {
Rafael Espindola56183fb2012-10-29 01:59:03 +000086 bool Delete =
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +000087 deleteStuff == (bool)Named.count(&*I) && !I->isDeclaration();
Rafael Espindola56183fb2012-10-29 01:59:03 +000088 if (!Delete) {
Bill Wendlingea6397f2012-07-19 00:11:40 +000089 if (I->hasAvailableExternallyLinkage())
90 continue;
91 if (I->getName() == "llvm.global_ctors")
92 continue;
93 }
Rafael Espindolab77c00f2011-06-09 14:38:09 +000094
NAKAMURA Takumi335a7bc2014-10-28 11:53:30 +000095 makeVisible(*I, Delete);
Rafael Espindola56183fb2012-10-29 01:59:03 +000096
Reid Klecknerfc0f9382015-07-06 18:48:02 +000097 if (Delete) {
98 // Make this a declaration and drop it's comdat.
Craig Topperf40110f2014-04-25 05:29:35 +000099 I->setInitializer(nullptr);
Reid Klecknerfc0f9382015-07-06 18:48:02 +0000100 I->setComdat(nullptr);
101 }
Bob Wilson3aecb152010-09-23 17:25:06 +0000102 }
Dan Gohman8f292e72010-08-26 00:22:55 +0000103
104 // Visit the Functions.
Benjamin Kramer135f7352016-06-26 12:28:59 +0000105 for (Function &F : M) {
Rafael Espindola56183fb2012-10-29 01:59:03 +0000106 bool Delete =
Benjamin Kramer135f7352016-06-26 12:28:59 +0000107 deleteStuff == (bool)Named.count(&F) && !F.isDeclaration();
Rafael Espindola56183fb2012-10-29 01:59:03 +0000108 if (!Delete) {
Benjamin Kramer135f7352016-06-26 12:28:59 +0000109 if (F.hasAvailableExternallyLinkage())
Bill Wendlingea6397f2012-07-19 00:11:40 +0000110 continue;
111 }
Rafael Espindolab77c00f2011-06-09 14:38:09 +0000112
Benjamin Kramer135f7352016-06-26 12:28:59 +0000113 makeVisible(F, Delete);
Rafael Espindola56183fb2012-10-29 01:59:03 +0000114
Reid Klecknerfc0f9382015-07-06 18:48:02 +0000115 if (Delete) {
116 // Make this a declaration and drop it's comdat.
Benjamin Kramer135f7352016-06-26 12:28:59 +0000117 F.deleteBody();
118 F.setComdat(nullptr);
Reid Klecknerfc0f9382015-07-06 18:48:02 +0000119 }
Bob Wilson3aecb152010-09-23 17:25:06 +0000120 }
Andrew Lenharth3f13b662008-03-07 19:51:57 +0000121
Rafael Espindola9d30d0f2012-10-29 00:27:55 +0000122 // Visit the Aliases.
123 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
124 I != E;) {
125 Module::alias_iterator CurI = I;
126 ++I;
127
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000128 bool Delete = deleteStuff == (bool)Named.count(&*CurI);
NAKAMURA Takumi335a7bc2014-10-28 11:53:30 +0000129 makeVisible(*CurI, Delete);
Rafael Espindola9d30d0f2012-10-29 00:27:55 +0000130
Rafael Espindola65979922013-11-22 17:58:12 +0000131 if (Delete) {
Manuel Jacob5f6eaac2016-01-16 20:30:46 +0000132 Type *Ty = CurI->getValueType();
Rafael Espindola9d30d0f2012-10-29 00:27:55 +0000133
134 CurI->removeFromParent();
135 llvm::Value *Declaration;
136 if (FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
137 Declaration = Function::Create(FTy, GlobalValue::ExternalLinkage,
138 CurI->getName(), &M);
139
140 } else {
141 Declaration =
142 new GlobalVariable(M, Ty, false, GlobalValue::ExternalLinkage,
Craig Topperf40110f2014-04-25 05:29:35 +0000143 nullptr, CurI->getName());
Rafael Espindola9d30d0f2012-10-29 00:27:55 +0000144
145 }
146 CurI->replaceAllUsesWith(Declaration);
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000147 delete &*CurI;
Rafael Espindola9d30d0f2012-10-29 00:27:55 +0000148 }
149 }
150
Andrew Lenharth3f13b662008-03-07 19:51:57 +0000151 return true;
152 }
153 };
154
155 char GVExtractorPass::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000156}
Andrew Lenharth3f13b662008-03-07 19:51:57 +0000157
NAKAMURA Takumid0e13af2014-10-28 11:54:52 +0000158ModulePass *llvm::createGVExtractionPass(std::vector<GlobalValue *> &GVs,
Dan Gohman8f292e72010-08-26 00:22:55 +0000159 bool deleteFn) {
160 return new GVExtractorPass(GVs, deleteFn);
Andrew Lenharth3f13b662008-03-07 19:51:57 +0000161}