blob: 479fd182598a7ff5e6aeebf4200ef628bbee47c8 [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 {
Andrew Kayloraa641a52016-04-22 22:06:11 +000071 if (skipModule(M))
72 return false;
73
Dan Gohman8f292e72010-08-26 00:22:55 +000074 // Visit the global inline asm.
75 if (!deleteStuff)
76 M.setModuleInlineAsm("");
Andrew Lenharth3f13b662008-03-07 19:51:57 +000077
Dan Gohman8f292e72010-08-26 00:22:55 +000078 // For simplicity, just give all GlobalValues ExternalLinkage. A trickier
79 // implementation could figure out which GlobalValues are actually
80 // referenced by the Named set, and which GlobalValues in the rest of
81 // the module are referenced by the NamedSet, and get away with leaving
82 // more internal and private things internal and private. But for now,
83 // be conservative and simple.
Andrew Lenharth3f13b662008-03-07 19:51:57 +000084
Dan Gohman8f292e72010-08-26 00:22:55 +000085 // Visit the GlobalVariables.
86 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
Bob Wilson3aecb152010-09-23 17:25:06 +000087 I != E; ++I) {
Rafael Espindola56183fb2012-10-29 01:59:03 +000088 bool Delete =
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +000089 deleteStuff == (bool)Named.count(&*I) && !I->isDeclaration();
Rafael Espindola56183fb2012-10-29 01:59:03 +000090 if (!Delete) {
Bill Wendlingea6397f2012-07-19 00:11:40 +000091 if (I->hasAvailableExternallyLinkage())
92 continue;
93 if (I->getName() == "llvm.global_ctors")
94 continue;
95 }
Rafael Espindolab77c00f2011-06-09 14:38:09 +000096
NAKAMURA Takumi335a7bc2014-10-28 11:53:30 +000097 makeVisible(*I, Delete);
Rafael Espindola56183fb2012-10-29 01:59:03 +000098
Reid Klecknerfc0f9382015-07-06 18:48:02 +000099 if (Delete) {
100 // Make this a declaration and drop it's comdat.
Craig Topperf40110f2014-04-25 05:29:35 +0000101 I->setInitializer(nullptr);
Reid Klecknerfc0f9382015-07-06 18:48:02 +0000102 I->setComdat(nullptr);
103 }
Bob Wilson3aecb152010-09-23 17:25:06 +0000104 }
Dan Gohman8f292e72010-08-26 00:22:55 +0000105
106 // Visit the Functions.
Benjamin Kramer135f7352016-06-26 12:28:59 +0000107 for (Function &F : M) {
Rafael Espindola56183fb2012-10-29 01:59:03 +0000108 bool Delete =
Benjamin Kramer135f7352016-06-26 12:28:59 +0000109 deleteStuff == (bool)Named.count(&F) && !F.isDeclaration();
Rafael Espindola56183fb2012-10-29 01:59:03 +0000110 if (!Delete) {
Benjamin Kramer135f7352016-06-26 12:28:59 +0000111 if (F.hasAvailableExternallyLinkage())
Bill Wendlingea6397f2012-07-19 00:11:40 +0000112 continue;
113 }
Rafael Espindolab77c00f2011-06-09 14:38:09 +0000114
Benjamin Kramer135f7352016-06-26 12:28:59 +0000115 makeVisible(F, Delete);
Rafael Espindola56183fb2012-10-29 01:59:03 +0000116
Reid Klecknerfc0f9382015-07-06 18:48:02 +0000117 if (Delete) {
118 // Make this a declaration and drop it's comdat.
Benjamin Kramer135f7352016-06-26 12:28:59 +0000119 F.deleteBody();
120 F.setComdat(nullptr);
Reid Klecknerfc0f9382015-07-06 18:48:02 +0000121 }
Bob Wilson3aecb152010-09-23 17:25:06 +0000122 }
Andrew Lenharth3f13b662008-03-07 19:51:57 +0000123
Rafael Espindola9d30d0f2012-10-29 00:27:55 +0000124 // Visit the Aliases.
125 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
126 I != E;) {
127 Module::alias_iterator CurI = I;
128 ++I;
129
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000130 bool Delete = deleteStuff == (bool)Named.count(&*CurI);
NAKAMURA Takumi335a7bc2014-10-28 11:53:30 +0000131 makeVisible(*CurI, Delete);
Rafael Espindola9d30d0f2012-10-29 00:27:55 +0000132
Rafael Espindola65979922013-11-22 17:58:12 +0000133 if (Delete) {
Manuel Jacob5f6eaac2016-01-16 20:30:46 +0000134 Type *Ty = CurI->getValueType();
Rafael Espindola9d30d0f2012-10-29 00:27:55 +0000135
136 CurI->removeFromParent();
137 llvm::Value *Declaration;
138 if (FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
139 Declaration = Function::Create(FTy, GlobalValue::ExternalLinkage,
140 CurI->getName(), &M);
141
142 } else {
143 Declaration =
144 new GlobalVariable(M, Ty, false, GlobalValue::ExternalLinkage,
Craig Topperf40110f2014-04-25 05:29:35 +0000145 nullptr, CurI->getName());
Rafael Espindola9d30d0f2012-10-29 00:27:55 +0000146
147 }
148 CurI->replaceAllUsesWith(Declaration);
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000149 delete &*CurI;
Rafael Espindola9d30d0f2012-10-29 00:27:55 +0000150 }
151 }
152
Andrew Lenharth3f13b662008-03-07 19:51:57 +0000153 return true;
154 }
155 };
156
157 char GVExtractorPass::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000158}
Andrew Lenharth3f13b662008-03-07 19:51:57 +0000159
NAKAMURA Takumid0e13af2014-10-28 11:54:52 +0000160ModulePass *llvm::createGVExtractionPass(std::vector<GlobalValue *> &GVs,
Dan Gohman8f292e72010-08-26 00:22:55 +0000161 bool deleteFn) {
162 return new GVExtractorPass(GVs, deleteFn);
Andrew Lenharth3f13b662008-03-07 19:51:57 +0000163}