LLVM  6.0.0svn
ExtractGV.cpp
Go to the documentation of this file.
1 //===-- 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 
14 #include "llvm/ADT/SetVector.h"
15 #include "llvm/IR/Constants.h"
16 #include "llvm/IR/Instructions.h"
17 #include "llvm/IR/LLVMContext.h"
18 #include "llvm/IR/Module.h"
19 #include "llvm/Pass.h"
20 #include "llvm/Transforms/IPO.h"
21 #include <algorithm>
22 using namespace llvm;
23 
24 /// 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.
28 static void makeVisible(GlobalValue &GV, bool Delete) {
29  bool Local = GV.hasLocalLinkage();
30  if (Local || Delete) {
32  if (Local)
34  return;
35  }
36 
37  if (!GV.hasLinkOnceLinkage()) {
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");
48  return;
51  return;
52  }
53 }
54 
55 namespace {
56  /// @brief A pass to extract specific global values and their dependencies.
57  class GVExtractorPass : public ModulePass {
59  bool deleteStuff;
60  public:
61  static char ID; // Pass identification, replacement for typeid
62 
63  /// If deleteS is true, this pass deletes the specified global values.
64  /// Otherwise, it deletes as much of the module as possible, except for the
65  /// global values specified.
66  explicit GVExtractorPass(std::vector<GlobalValue*> &GVs,
67  bool deleteS = true)
68  : ModulePass(ID), Named(GVs.begin(), GVs.end()), deleteStuff(deleteS) {}
69 
70  bool runOnModule(Module &M) override {
71  if (skipModule(M))
72  return false;
73 
74  // Visit the global inline asm.
75  if (!deleteStuff)
76  M.setModuleInlineAsm("");
77 
78  // 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.
84 
85  // Visit the GlobalVariables.
87  I != E; ++I) {
88  bool Delete =
89  deleteStuff == (bool)Named.count(&*I) && !I->isDeclaration();
90  if (!Delete) {
91  if (I->hasAvailableExternallyLinkage())
92  continue;
93  if (I->getName() == "llvm.global_ctors")
94  continue;
95  }
96 
97  makeVisible(*I, Delete);
98 
99  if (Delete) {
100  // Make this a declaration and drop it's comdat.
101  I->setInitializer(nullptr);
102  I->setComdat(nullptr);
103  }
104  }
105 
106  // Visit the Functions.
107  for (Function &F : M) {
108  bool Delete =
109  deleteStuff == (bool)Named.count(&F) && !F.isDeclaration();
110  if (!Delete) {
111  if (F.hasAvailableExternallyLinkage())
112  continue;
113  }
114 
115  makeVisible(F, Delete);
116 
117  if (Delete) {
118  // Make this a declaration and drop it's comdat.
119  F.deleteBody();
120  F.setComdat(nullptr);
121  }
122  }
123 
124  // 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 
130  bool Delete = deleteStuff == (bool)Named.count(&*CurI);
131  makeVisible(*CurI, Delete);
132 
133  if (Delete) {
134  Type *Ty = CurI->getValueType();
135 
136  CurI->removeFromParent();
137  llvm::Value *Declaration;
138  if (FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
140  CurI->getName(), &M);
141 
142  } else {
143  Declaration =
145  nullptr, CurI->getName());
146 
147  }
148  CurI->replaceAllUsesWith(Declaration);
149  delete &*CurI;
150  }
151  }
152 
153  return true;
154  }
155  };
156 
157  char GVExtractorPass::ID = 0;
158 }
159 
160 ModulePass *llvm::createGVExtractionPass(std::vector<GlobalValue *> &GVs,
161  bool deleteFn) {
162  return new GVExtractorPass(GVs, deleteFn);
163 }
void setVisibility(VisibilityTypes V)
Definition: GlobalValue.h:226
bool hasLocalLinkage() const
Definition: GlobalValue.h:416
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:54
ModulePass * createGVExtractionPass(std::vector< GlobalValue *> &GVs, bool deleteFn=false)
createGVExtractionPass - If deleteFn is true, this pass deletes the specified global values...
Definition: ExtractGV.cpp:160
Externally visible function.
Definition: GlobalValue.h:49
void setModuleInlineAsm(StringRef Asm)
Set the module-scope inline assembly blocks.
Definition: Module.h:273
global_iterator global_begin()
Definition: Module.h:555
Class to represent function types.
Definition: DerivedTypes.h:103
#define F(x, y, z)
Definition: MD5.cpp:55
LinkageTypes getLinkage() const
Definition: GlobalValue.h:430
bool hasLinkOnceLinkage() const
Definition: GlobalValue.h:406
size_type count(const key_type &key) const
Count the number of elements of a given key in the SetVector.
Definition: SetVector.h:211
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:52
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static bool isDiscardableIfUnused(LinkageTypes Linkage)
Whether the definition of this global may be discarded if it is not used in its compilation unit...
Definition: GlobalValue.h:341
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
global_iterator global_end()
Definition: Module.h:557
Iterator for intrusive lists based on ilist_node.
#define E
Definition: LargeTest.cpp:27
Keep one copy of function when linking (inline)
Definition: GlobalValue.h:51
Module.h This file contains the declarations for the Module class.
static void makeVisible(GlobalValue &GV, bool Delete)
Make sure GV is visible from both modules.
Definition: ExtractGV.cpp:28
void setLinkage(LinkageTypes LT)
Definition: GlobalValue.h:425
#define I(x, y, z)
Definition: MD5.cpp:58
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:235
Keep one copy of named function when linking (weak)
Definition: GlobalValue.h:53
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:73
A vector that has set insertion semantics.
Definition: SetVector.h:41
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, const Twine &N="", Module *M=nullptr)
Definition: Function.h:128