blob: 1a3b9253d72fca8e885abf13e3f926d94dc2e71a [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 =
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.
Bob Wilson3aecb152010-09-23 17:25:06 +0000104 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Rafael Espindola56183fb2012-10-29 01:59:03 +0000105 bool Delete =
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000106 deleteStuff == (bool)Named.count(&*I) && !I->isDeclaration();
Rafael Espindola56183fb2012-10-29 01:59:03 +0000107 if (!Delete) {
Bill Wendlingea6397f2012-07-19 00:11:40 +0000108 if (I->hasAvailableExternallyLinkage())
109 continue;
110 }
Rafael Espindolab77c00f2011-06-09 14:38:09 +0000111
NAKAMURA Takumi335a7bc2014-10-28 11:53:30 +0000112 makeVisible(*I, 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.
Rafael Espindola56183fb2012-10-29 01:59:03 +0000116 I->deleteBody();
Reid Klecknerfc0f9382015-07-06 18:48:02 +0000117 I->setComdat(nullptr);
118 }
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) {
Rafael Espindola9d30d0f2012-10-29 00:27:55 +0000131 Type *Ty = CurI->getType()->getElementType();
132
133 CurI->removeFromParent();
134 llvm::Value *Declaration;
135 if (FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
136 Declaration = Function::Create(FTy, GlobalValue::ExternalLinkage,
137 CurI->getName(), &M);
138
139 } else {
140 Declaration =
141 new GlobalVariable(M, Ty, false, GlobalValue::ExternalLinkage,
Craig Topperf40110f2014-04-25 05:29:35 +0000142 nullptr, CurI->getName());
Rafael Espindola9d30d0f2012-10-29 00:27:55 +0000143
144 }
145 CurI->replaceAllUsesWith(Declaration);
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000146 delete &*CurI;
Rafael Espindola9d30d0f2012-10-29 00:27:55 +0000147 }
148 }
149
Andrew Lenharth3f13b662008-03-07 19:51:57 +0000150 return true;
151 }
152 };
153
154 char GVExtractorPass::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000155}
Andrew Lenharth3f13b662008-03-07 19:51:57 +0000156
NAKAMURA Takumid0e13af2014-10-28 11:54:52 +0000157ModulePass *llvm::createGVExtractionPass(std::vector<GlobalValue *> &GVs,
Dan Gohman8f292e72010-08-26 00:22:55 +0000158 bool deleteFn) {
159 return new GVExtractorPass(GVs, deleteFn);
Andrew Lenharth3f13b662008-03-07 19:51:57 +0000160}