LLVM  4.0.0
ConstantMerge.cpp
Go to the documentation of this file.
1 //===- ConstantMerge.cpp - Merge duplicate global constants ---------------===//
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 file defines the interface to a pass that merges duplicate global
11 // constants together into a single constant that is shared. This is useful
12 // because some passes (ie TraceValues) insert a lot of string constants into
13 // the program, regardless of whether or not an existing string is available.
14 //
15 // Algorithm: ConstantMerge is designed to build up a map of available constants
16 // and eliminate duplicates when it is initialized.
17 //
18 //===----------------------------------------------------------------------===//
19 
21 #include "llvm/ADT/DenseMap.h"
23 #include "llvm/ADT/SmallPtrSet.h"
24 #include "llvm/ADT/Statistic.h"
25 #include "llvm/IR/Constants.h"
26 #include "llvm/IR/DataLayout.h"
27 #include "llvm/IR/DerivedTypes.h"
28 #include "llvm/IR/Module.h"
29 #include "llvm/IR/Operator.h"
30 #include "llvm/Pass.h"
31 #include "llvm/Transforms/IPO.h"
32 using namespace llvm;
33 
34 #define DEBUG_TYPE "constmerge"
35 
36 STATISTIC(NumMerged, "Number of global constants merged");
37 
38 /// Find values that are marked as llvm.used.
39 static void FindUsedValues(GlobalVariable *LLVMUsed,
41  if (!LLVMUsed) return;
42  ConstantArray *Inits = cast<ConstantArray>(LLVMUsed->getInitializer());
43 
44  for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i) {
45  Value *Operand = Inits->getOperand(i)->stripPointerCastsNoFollowAliases();
46  GlobalValue *GV = cast<GlobalValue>(Operand);
47  UsedValues.insert(GV);
48  }
49 }
50 
51 // True if A is better than B.
52 static bool IsBetterCanonical(const GlobalVariable &A,
53  const GlobalVariable &B) {
54  if (!A.hasLocalLinkage() && B.hasLocalLinkage())
55  return true;
56 
57  if (A.hasLocalLinkage() && !B.hasLocalLinkage())
58  return false;
59 
60  return A.hasGlobalUnnamedAddr();
61 }
62 
63 static unsigned getAlignment(GlobalVariable *GV) {
64  unsigned Align = GV->getAlignment();
65  if (Align)
66  return Align;
67  return GV->getParent()->getDataLayout().getPreferredAlignment(GV);
68 }
69 
70 static bool mergeConstants(Module &M) {
71  // Find all the globals that are marked "used". These cannot be merged.
73  FindUsedValues(M.getGlobalVariable("llvm.used"), UsedGlobals);
74  FindUsedValues(M.getGlobalVariable("llvm.compiler.used"), UsedGlobals);
75 
76  // Map unique constants to globals.
78 
79  // Replacements - This vector contains a list of replacements to perform.
81 
82  bool MadeChange = false;
83 
84  // Iterate constant merging while we are still making progress. Merging two
85  // constants together may allow us to merge other constants together if the
86  // second level constants have initializers which point to the globals that
87  // were just merged.
88  while (1) {
89 
90  // First: Find the canonical constants others will be merged with.
91  for (Module::global_iterator GVI = M.global_begin(), E = M.global_end();
92  GVI != E; ) {
93  GlobalVariable *GV = &*GVI++;
94 
95  // If this GV is dead, remove it.
97  if (GV->use_empty() && GV->hasLocalLinkage()) {
98  GV->eraseFromParent();
99  continue;
100  }
101 
102  // Only process constants with initializers in the default address space.
103  if (!GV->isConstant() || !GV->hasDefinitiveInitializer() ||
104  GV->getType()->getAddressSpace() != 0 || GV->hasSection() ||
105  // Don't touch values marked with attribute(used).
106  UsedGlobals.count(GV))
107  continue;
108 
109  // This transformation is legal for weak ODR globals in the sense it
110  // doesn't change semantics, but we really don't want to perform it
111  // anyway; it's likely to pessimize code generation, and some tools
112  // (like the Darwin linker in cases involving CFString) don't expect it.
113  if (GV->isWeakForLinker())
114  continue;
115 
116  Constant *Init = GV->getInitializer();
117 
118  // Check to see if the initializer is already known.
119  GlobalVariable *&Slot = CMap[Init];
120 
121  // If this is the first constant we find or if the old one is local,
122  // replace with the current one. If the current is externally visible
123  // it cannot be replace, but can be the canonical constant we merge with.
124  if (!Slot || IsBetterCanonical(*GV, *Slot))
125  Slot = GV;
126  }
127 
128  // Second: identify all globals that can be merged together, filling in
129  // the Replacements vector. We cannot do the replacement in this pass
130  // because doing so may cause initializers of other globals to be rewritten,
131  // invalidating the Constant* pointers in CMap.
132  for (Module::global_iterator GVI = M.global_begin(), E = M.global_end();
133  GVI != E; ) {
134  GlobalVariable *GV = &*GVI++;
135 
136  // Only process constants with initializers in the default address space.
137  if (!GV->isConstant() || !GV->hasDefinitiveInitializer() ||
138  GV->getType()->getAddressSpace() != 0 || GV->hasSection() ||
139  // Don't touch values marked with attribute(used).
140  UsedGlobals.count(GV))
141  continue;
142 
143  // We can only replace constant with local linkage.
144  if (!GV->hasLocalLinkage())
145  continue;
146 
147  Constant *Init = GV->getInitializer();
148 
149  // Check to see if the initializer is already known.
150  GlobalVariable *Slot = CMap[Init];
151 
152  if (!Slot || Slot == GV)
153  continue;
154 
155  if (!Slot->hasGlobalUnnamedAddr() && !GV->hasGlobalUnnamedAddr())
156  continue;
157 
158  if (!GV->hasGlobalUnnamedAddr())
160 
161  // Make all uses of the duplicate constant use the canonical version.
162  Replacements.push_back(std::make_pair(GV, Slot));
163  }
164 
165  if (Replacements.empty())
166  return MadeChange;
167  CMap.clear();
168 
169  // Now that we have figured out which replacements must be made, do them all
170  // now. This avoid invalidating the pointers in CMap, which are unneeded
171  // now.
172  for (unsigned i = 0, e = Replacements.size(); i != e; ++i) {
173  // Bump the alignment if necessary.
174  if (Replacements[i].first->getAlignment() ||
175  Replacements[i].second->getAlignment()) {
176  Replacements[i].second->setAlignment(
177  std::max(getAlignment(Replacements[i].first),
178  getAlignment(Replacements[i].second)));
179  }
180 
181  // Eliminate any uses of the dead global.
182  Replacements[i].first->replaceAllUsesWith(Replacements[i].second);
183 
184  // Delete the global value from the module.
185  assert(Replacements[i].first->hasLocalLinkage() &&
186  "Refusing to delete an externally visible global variable.");
187  Replacements[i].first->eraseFromParent();
188  }
189 
190  NumMerged += Replacements.size();
191  Replacements.clear();
192  }
193 }
194 
196  if (!mergeConstants(M))
197  return PreservedAnalyses::all();
198  return PreservedAnalyses::none();
199 }
200 
201 namespace {
202 struct ConstantMergeLegacyPass : public ModulePass {
203  static char ID; // Pass identification, replacement for typeid
204  ConstantMergeLegacyPass() : ModulePass(ID) {
206  }
207 
208  // For this pass, process all of the globals in the module, eliminating
209  // duplicate constants.
210  bool runOnModule(Module &M) {
211  if (skipModule(M))
212  return false;
213  return mergeConstants(M);
214  }
215 };
216 }
217 
219 INITIALIZE_PASS(ConstantMergeLegacyPass, "constmerge",
220  "Merge Duplicate Global Constants", false, false)
221 
223  return new ConstantMergeLegacyPass();
224 }
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
STATISTIC(NumFunctions,"Total number of functions")
size_t i
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:52
unsigned getNumOperands() const
Definition: User.h:167
size_type count(PtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:380
unsigned getAddressSpace() const
Return the address space of the Pointer type.
Definition: DerivedTypes.h:471
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:345
void eraseFromParent() override
eraseFromParent - This method unlinks 'this' from the containing module and deletes it...
Definition: Globals.cpp:319
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:60
global_iterator global_begin()
Definition: Module.h:518
static bool IsBetterCanonical(const GlobalVariable &A, const GlobalVariable &B)
static unsigned getAlignment(GlobalVariable *GV)
unsigned getAlignment() const
Definition: GlobalObject.h:59
static GCRegistry::Add< OcamlGC > B("ocaml","ocaml 3.10-compatible GC")
bool hasDefinitiveInitializer() const
hasDefinitiveInitializer - Whether the global variable has an initializer, and any other instances of...
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: PassManager.h:110
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
static bool isWeakForLinker(LinkageTypes Linkage)
Whether the definition of this global may be replaced at link time.
Definition: GlobalValue.h:349
bool hasSection() const
Check if this global has a custom object file section.
Definition: GlobalObject.h:73
Value * stripPointerCastsNoFollowAliases()
Strip off pointer casts and all-zero GEPs.
Definition: Value.cpp:494
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:107
This is an important base class in LLVM.
Definition: Constant.h:42
This file contains the declarations for the subclasses of Constant, which represent the different fla...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:368
Value * getOperand(unsigned i) const
Definition: User.h:145
ModulePass * createConstantMergePass()
createConstantMergePass - This function returns a new pass that merges duplicate global constants tog...
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:113
void initializeConstantMergeLegacyPassPass(PassRegistry &)
unsigned getPreferredAlignment(const GlobalVariable *GV) const
Returns the preferred alignment of the specified global.
Definition: DataLayout.cpp:767
global_iterator global_end()
Definition: Module.h:520
INITIALIZE_PASS(ConstantMergeLegacyPass,"constmerge","Merge Duplicate Global Constants", false, false) ModulePass *llvm
Iterator for intrusive lists based on ilist_node.
bool hasGlobalUnnamedAddr() const
Definition: GlobalValue.h:187
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:425
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:843
Module.h This file contains the declarations for the Module class.
ConstantArray - Constant Array Declarations.
Definition: Constants.h:411
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
PreservedAnalyses run(Module &M, ModuleAnalysisManager &)
static void FindUsedValues(GlobalVariable *LLVMUsed, SmallPtrSetImpl< const GlobalValue * > &UsedValues)
Find values that are marked as llvm.used.
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:259
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:384
void setUnnamedAddr(UnnamedAddr Val)
Definition: GlobalValue.h:203
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:135
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:235
static bool mergeConstants(Module &M)
bool hasLocalLinkage() const
Definition: GlobalValue.h:415
bool use_empty() const
Definition: Value.h:299
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void removeDeadConstantUsers() const
If there are any dead constant users dangling off of this constant, remove them.
Definition: Constants.cpp:463
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:537
LLVM Value Representation.
Definition: Value.h:71
A container for analyses that lazily runs them and caches their results.
static GCRegistry::Add< ErlangGC > A("erlang","erlang-compatible garbage collector")
GlobalVariable * getGlobalVariable(StringRef Name) const
Look up the specified global variable in the module symbol table.
Definition: Module.h:344