LLVM  14.0.0git
ExtractGV.cpp
Go to the documentation of this file.
1 //===-- ExtractGV.cpp - Global Value extraction pass ----------------------===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This pass extracts global values
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/ADT/SetVector.h"
14 #include "llvm/IR/LLVMContext.h"
15 #include "llvm/IR/Module.h"
16 #include "llvm/Pass.h"
17 #include "llvm/Transforms/IPO.h"
18 #include <algorithm>
19 using namespace llvm;
20 
21 /// 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.
25 static void makeVisible(GlobalValue &GV, bool Delete) {
26  bool Local = GV.hasLocalLinkage();
27  if (Local || Delete) {
29  if (Local)
31  return;
32  }
33 
34  if (!GV.hasLinkOnceLinkage()) {
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");
45  return;
48  return;
49  }
50 }
51 
52 namespace {
53  /// A pass to extract specific global values and their dependencies.
54  class GVExtractorPass : public ModulePass {
56  bool deleteStuff;
57  bool keepConstInit;
58  public:
59  static char ID; // Pass identification, replacement for typeid
60 
61  /// 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, bool keepConstInit = false)
66  : ModulePass(ID), Named(GVs.begin(), GVs.end()), deleteStuff(deleteS),
67  keepConstInit(keepConstInit) {}
68 
69  bool runOnModule(Module &M) override {
70  if (skipModule(M))
71  return false;
72 
73  // Visit the global inline asm.
74  if (!deleteStuff)
75  M.setModuleInlineAsm("");
76 
77  // For simplicity, just give all GlobalValues ExternalLinkage. A trickier
78  // implementation could figure out which GlobalValues are actually
79  // referenced by the Named set, and which GlobalValues in the rest of
80  // the module are referenced by the NamedSet, and get away with leaving
81  // more internal and private things internal and private. But for now,
82  // be conservative and simple.
83 
84  // Visit the GlobalVariables.
85  for (GlobalVariable &GV : M.globals()) {
86  bool Delete = deleteStuff == (bool)Named.count(&GV) &&
87  !GV.isDeclaration() &&
88  (!GV.isConstant() || !keepConstInit);
89  if (!Delete) {
90  if (GV.hasAvailableExternallyLinkage())
91  continue;
92  if (GV.getName() == "llvm.global_ctors")
93  continue;
94  }
95 
96  makeVisible(GV, Delete);
97 
98  if (Delete) {
99  // Make this a declaration and drop it's comdat.
100  GV.setInitializer(nullptr);
101  GV.setComdat(nullptr);
102  }
103  }
104 
105  // Visit the Functions.
106  for (Function &F : M) {
107  bool Delete =
108  deleteStuff == (bool)Named.count(&F) && !F.isDeclaration();
109  if (!Delete) {
110  if (F.hasAvailableExternallyLinkage())
111  continue;
112  }
113 
114  makeVisible(F, Delete);
115 
116  if (Delete) {
117  // Make this a declaration and drop it's comdat.
118  F.deleteBody();
119  F.setComdat(nullptr);
120  }
121  }
122 
123  // Visit the Aliases.
124  for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
125  I != E;) {
126  Module::alias_iterator CurI = I;
127  ++I;
128 
129  bool Delete = deleteStuff == (bool)Named.count(&*CurI);
130  makeVisible(*CurI, Delete);
131 
132  if (Delete) {
133  Type *Ty = CurI->getValueType();
134 
135  CurI->removeFromParent();
136  llvm::Value *Declaration;
137  if (FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
139  CurI->getAddressSpace(),
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, bool keepConstInit) {
162  return new GVExtractorPass(GVs, deleteFn, keepConstInit);
163 }
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::GlobalValue::LinkOnceAnyLinkage
@ LinkOnceAnyLinkage
Keep one copy of function when linking (inline)
Definition: GlobalValue.h:50
llvm::ModulePass
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:238
llvm::GlobalValue::getLinkage
LinkageTypes getLinkage() const
Definition: GlobalValue.h:467
llvm::GlobalValue::HiddenVisibility
@ HiddenVisibility
The GV is hidden.
Definition: GlobalValue.h:64
llvm::Function
Definition: Function.h:61
Pass.h
llvm::GlobalVariable
Definition: GlobalVariable.h:40
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
Module.h
llvm::sys::path::end
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:233
llvm::sys::path::begin
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
Definition: Path.cpp:224
llvm::ms_demangle::IntrinsicFunctionKind::Delete
@ Delete
F
#define F(x, y, z)
Definition: MD5.cpp:56
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::GlobalValue
Definition: GlobalValue.h:44
IPO.h
llvm::GlobalValue::WeakAnyLinkage
@ WeakAnyLinkage
Keep one copy of named function when linking (weak)
Definition: GlobalValue.h:52
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::GlobalValue::setLinkage
void setLinkage(LinkageTypes LT)
Definition: GlobalValue.h:460
llvm::GlobalValue::isDiscardableIfUnused
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:372
llvm::Function::Create
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
Definition: Function.h:138
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::GlobalValue::hasLocalLinkage
bool hasLocalLinkage() const
Definition: GlobalValue.h:451
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:136
llvm::GlobalValue::WeakODRLinkage
@ WeakODRLinkage
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:53
llvm::Module::alias_iterator
AliasListType::iterator alias_iterator
The Global Alias iterators.
Definition: Module.h:102
llvm::SetVector::count
size_type count(const key_type &key) const
Count the number of elements of a given key in the SetVector.
Definition: SetVector.h:215
makeVisible
static void makeVisible(GlobalValue &GV, bool Delete)
Make sure GV is visible from both modules.
Definition: ExtractGV.cpp:25
llvm::GlobalValue::ExternalLinkage
@ ExternalLinkage
Externally visible function.
Definition: GlobalValue.h:48
llvm::GlobalValue::hasLinkOnceLinkage
bool hasLinkOnceLinkage() const
Definition: GlobalValue.h:438
LLVMContext.h
llvm::SetVector
A vector that has set insertion semantics.
Definition: SetVector.h:40
llvm::createGVExtractionPass
ModulePass * createGVExtractionPass(std::vector< GlobalValue * > &GVs, bool deleteFn=false, bool keepConstInit=false)
createGVExtractionPass - If deleteFn is true, this pass deletes the specified global values.
Definition: ExtractGV.cpp:160
llvm::GlobalValue::LinkOnceODRLinkage
@ LinkOnceODRLinkage
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:51
llvm::GlobalValue::setVisibility
void setVisibility(VisibilityTypes V)
Definition: GlobalValue.h:235
llvm::Value
LLVM Value Representation.
Definition: Value.h:75
SetVector.h
llvm::FunctionType
Class to represent function types.
Definition: DerivedTypes.h:103
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:37