blob: f77b528fc42da3f79786cd2d43a53634e381e809 [file] [log] [blame]
Andrew Lenharth3f13b662008-03-07 19:51:57 +00001//===-- ExtractGV.cpp - Global Value extraction pass ----------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Andrew Lenharth3f13b662008-03-07 19:51:57 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This pass extracts global values
10//
11//===----------------------------------------------------------------------===//
12
Chandler Carruthed0881b2012-12-03 16:50:05 +000013#include "llvm/ADT/SetVector.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000014#include "llvm/IR/LLVMContext.h"
15#include "llvm/IR/Module.h"
Andrew Lenharth3f13b662008-03-07 19:51:57 +000016#include "llvm/Pass.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000017#include "llvm/Transforms/IPO.h"
Ted Kremenekd48ed172008-03-09 18:32:50 +000018#include <algorithm>
Andrew Lenharth3f13b662008-03-07 19:51:57 +000019using namespace llvm;
20
Rafael Espindola65979922013-11-22 17:58:12 +000021/// Make sure GV is visible from both modules. Delete is true if it is
22/// being deleted from this module.
23/// This also makes sure GV cannot be dropped so that references from
24/// the split module remain valid.
25static void makeVisible(GlobalValue &GV, bool Delete) {
26 bool Local = GV.hasLocalLinkage();
Rafael Espindola65979922013-11-22 17:58:12 +000027 if (Local || Delete) {
28 GV.setLinkage(GlobalValue::ExternalLinkage);
Duncan P. N. Exon Smithe60adfd2014-05-07 23:00:22 +000029 if (Local)
30 GV.setVisibility(GlobalValue::HiddenVisibility);
Rafael Espindola65979922013-11-22 17:58:12 +000031 return;
32 }
33
34 if (!GV.hasLinkOnceLinkage()) {
35 assert(!GV.isDiscardableIfUnused());
36 return;
37 }
38
39 // Map linkonce* to weak* so that llvm doesn't drop this GV.
40 switch(GV.getLinkage()) {
41 default:
42 llvm_unreachable("Unexpected linkage");
43 case GlobalValue::LinkOnceAnyLinkage:
44 GV.setLinkage(GlobalValue::WeakAnyLinkage);
45 return;
46 case GlobalValue::LinkOnceODRLinkage:
47 GV.setLinkage(GlobalValue::WeakODRLinkage);
48 return;
49 }
50}
51
Andrew Lenharth3f13b662008-03-07 19:51:57 +000052namespace {
Adrian Prantl4dfcc4a2018-05-01 16:10:38 +000053 /// A pass to extract specific global values and their dependencies.
Nick Lewycky02d5f772009-10-25 06:33:48 +000054 class GVExtractorPass : public ModulePass {
Dan Gohman8f292e72010-08-26 00:22:55 +000055 SetVector<GlobalValue *> Named;
Andrew Lenharth3f13b662008-03-07 19:51:57 +000056 bool deleteStuff;
Andrew Lenharth3f13b662008-03-07 19:51:57 +000057 public:
58 static char ID; // Pass identification, replacement for typeid
59
Craig Topper2aa4d392017-06-08 23:38:19 +000060 /// If deleteS is true, this pass deletes the specified global values.
61 /// Otherwise, it deletes as much of the module as possible, except for the
62 /// global values specified.
63 explicit GVExtractorPass(std::vector<GlobalValue*> &GVs,
64 bool deleteS = true)
Dan Gohman8f292e72010-08-26 00:22:55 +000065 : ModulePass(ID), Named(GVs.begin(), GVs.end()), deleteStuff(deleteS) {}
Andrew Lenharth3f13b662008-03-07 19:51:57 +000066
Craig Topper3e4c6972014-03-05 09:10:37 +000067 bool runOnModule(Module &M) override {
Andrew Kayloraa641a52016-04-22 22:06:11 +000068 if (skipModule(M))
69 return false;
70
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 =
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +000086 deleteStuff == (bool)Named.count(&*I) && !I->isDeclaration();
Rafael Espindola56183fb2012-10-29 01:59:03 +000087 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
Reid Klecknerfc0f9382015-07-06 18:48:02 +000096 if (Delete) {
97 // Make this a declaration and drop it's comdat.
Craig Topperf40110f2014-04-25 05:29:35 +000098 I->setInitializer(nullptr);
Reid Klecknerfc0f9382015-07-06 18:48:02 +000099 I->setComdat(nullptr);
100 }
Bob Wilson3aecb152010-09-23 17:25:06 +0000101 }
Dan Gohman8f292e72010-08-26 00:22:55 +0000102
103 // Visit the Functions.
Benjamin Kramer135f7352016-06-26 12:28:59 +0000104 for (Function &F : M) {
Rafael Espindola56183fb2012-10-29 01:59:03 +0000105 bool Delete =
Benjamin Kramer135f7352016-06-26 12:28:59 +0000106 deleteStuff == (bool)Named.count(&F) && !F.isDeclaration();
Rafael Espindola56183fb2012-10-29 01:59:03 +0000107 if (!Delete) {
Benjamin Kramer135f7352016-06-26 12:28:59 +0000108 if (F.hasAvailableExternallyLinkage())
Bill Wendlingea6397f2012-07-19 00:11:40 +0000109 continue;
110 }
Rafael Espindolab77c00f2011-06-09 14:38:09 +0000111
Benjamin Kramer135f7352016-06-26 12:28:59 +0000112 makeVisible(F, Delete);
Rafael Espindola56183fb2012-10-29 01:59:03 +0000113
Reid Klecknerfc0f9382015-07-06 18:48:02 +0000114 if (Delete) {
115 // Make this a declaration and drop it's comdat.
Benjamin Kramer135f7352016-06-26 12:28:59 +0000116 F.deleteBody();
117 F.setComdat(nullptr);
Reid Klecknerfc0f9382015-07-06 18:48:02 +0000118 }
Bob Wilson3aecb152010-09-23 17:25:06 +0000119 }
Andrew Lenharth3f13b662008-03-07 19:51:57 +0000120
Rafael Espindola9d30d0f2012-10-29 00:27:55 +0000121 // Visit the Aliases.
122 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
123 I != E;) {
124 Module::alias_iterator CurI = I;
125 ++I;
126
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000127 bool Delete = deleteStuff == (bool)Named.count(&*CurI);
NAKAMURA Takumi335a7bc2014-10-28 11:53:30 +0000128 makeVisible(*CurI, Delete);
Rafael Espindola9d30d0f2012-10-29 00:27:55 +0000129
Rafael Espindola65979922013-11-22 17:58:12 +0000130 if (Delete) {
Manuel Jacob5f6eaac2016-01-16 20:30:46 +0000131 Type *Ty = CurI->getValueType();
Rafael Espindola9d30d0f2012-10-29 00:27:55 +0000132
133 CurI->removeFromParent();
134 llvm::Value *Declaration;
135 if (FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
136 Declaration = Function::Create(FTy, GlobalValue::ExternalLinkage,
Dylan McKayf920da02018-12-18 09:52:52 +0000137 CurI->getAddressSpace(),
Rafael Espindola9d30d0f2012-10-29 00:27:55 +0000138 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}