LLVM: lib/Transforms/IPO/ConstantMerge.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
34#include
35#include
36#include
37
38using namespace llvm;
39
40#define DEBUG_TYPE "constmerge"
41
42STATISTIC(NumIdenticalMerged, "Number of identical global constants merged");
43
44
47 if (!LLVMUsed) return;
49
50 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i) {
53 UsedValues.insert(GV);
54 }
55}
56
57
60 if (.hasLocalLinkage() && B.hasLocalLinkage())
61 return true;
62
63 if (A.hasLocalLinkage() && .hasLocalLinkage())
64 return false;
65
66 return A.hasGlobalUnnamedAddr();
67}
68
72 for (const auto &V : MDs)
73 if (V.first != LLVMContext::MD_dbg)
74 return true;
75 return false;
76}
77
82 for (auto *MD : MDs)
84}
85
90
91static bool
94
97
99
100 UsedGlobals.count(GV);
101}
102
114
117
119 << New->getName() << "\n");
120
121
122 if (Old->getAlign() || New->getAlign())
124
127
128
130 "Refusing to delete an externally visible global variable.");
132}
133
135
137 FindUsedValues(M.getGlobalVariable("llvm.used"), UsedGlobals);
138 FindUsedValues(M.getGlobalVariable("llvm.compiler.used"), UsedGlobals);
139
140
142
144 SameContentReplacements;
145
146 size_t ChangesMade = 0;
147 size_t OldChangesMade = 0;
148
149
150
151
152
153 while (true) {
154
156
157 GV.removeDeadConstantUsers();
158 if (GV.use_empty() && GV.hasLocalLinkage()) {
159 GV.eraseFromParent();
160 ++ChangesMade;
161 continue;
162 }
163
165 continue;
166
167
168
169
170
171 if (GV.isWeakForLinker())
172 continue;
173
174
176 continue;
177
179
180
182
183
184
185
186 bool FirstConstantFound = !Slot;
188 Slot = &GV;
190 << (FirstConstantFound ? "\n" : " (updated)\n"));
191 }
192 }
193
194
195
196
197
200 continue;
201
202
203 if (!GV.hasLocalLinkage())
204 continue;
205
207
208
209 auto Found = CMap.find(Init);
210 if (Found == CMap.end())
211 continue;
212
214 if (Slot == &GV)
215 continue;
216
218 continue;
219
220
221 LLVM_DEBUG(dbgs() << "Will replace: @" << GV.getName() << " -> @"
222 << Slot->getName() << "\n");
223 SameContentReplacements.push_back(std::make_pair(&GV, Slot));
224 }
225
226
227
228
229 for (const auto &[Old, New] : SameContentReplacements) {
231 ++ChangesMade;
232 ++NumIdenticalMerged;
233 }
234
235 if (ChangesMade == OldChangesMade)
236 break;
237 OldChangesMade = ChangesMade;
238
239 SameContentReplacements.clear();
241 }
242
243 return ChangesMade;
244}
245
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static bool IsBetterCanonical(const GlobalVariable &A, const GlobalVariable &B)
Definition ConstantMerge.cpp:58
static bool hasMetadataOtherThanDebugLoc(const GlobalVariable *GV)
Definition ConstantMerge.cpp:69
static void copyDebugLocMetadata(const GlobalVariable *From, GlobalVariable *To)
Definition ConstantMerge.cpp:78
static bool mergeConstants(Module &M)
Definition ConstantMerge.cpp:134
static bool isUnmergeableGlobal(GlobalVariable *GV, const SmallPtrSetImpl< const GlobalValue * > &UsedGlobals)
Definition ConstantMerge.cpp:92
static void FindUsedValues(GlobalVariable *LLVMUsed, SmallPtrSetImpl< const GlobalValue * > &UsedValues)
Find values that are marked as llvm.used.
Definition ConstantMerge.cpp:45
CanMerge
Definition ConstantMerge.cpp:103
@ Yes
Definition ConstantMerge.cpp:103
@ No
Definition ConstantMerge.cpp:103
static CanMerge makeMergeable(GlobalVariable *Old, GlobalVariable *New)
Definition ConstantMerge.cpp:104
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file defines the DenseMap class.
Module.h This file contains the declarations for the Module class.
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
ConstantArray - Constant Array Declarations.
PreservedAnalyses run(Module &M, ModuleAnalysisManager &)
Definition ConstantMerge.cpp:246
This is an important base class in LLVM.
LLVM_ABI Align getPreferredAlign(const GlobalVariable *GV) const
Returns the preferred alignment of the specified global.
iterator find(const_arg_type_t< KeyT > Val)
LLVM_ABI void getAllMetadata(SmallVectorImpl< std::pair< unsigned, MDNode * > > &MDs) const
Appends all metadata attached to this value to MDs, sorting by KindID.
bool hasSection() const
Check if this global has a custom object file section.
bool isThreadLocal() const
If the value is "Thread Local", its value isn't shared by the threads.
bool hasLocalLinkage() const
PointerType * getType() const
Global values are always pointers.
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this global belongs to.
bool hasGlobalUnnamedAddr() const
MaybeAlign getAlign() const
Returns the alignment of the given variable.
LLVM_ABI void getDebugInfo(SmallVectorImpl< DIGlobalVariableExpression * > &GVs) const
Fill the vector with all debug info attachements.
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
LLVM_ABI void eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing module and deletes it.
LLVM_ABI void addDebugInfo(DIGlobalVariableExpression *GV)
Attach a DIGlobalVariableExpression.
bool hasDefinitiveInitializer() const
hasDefinitiveInitializer - Whether the global variable has an initializer, and any other instances of...
A Module instance is used to store all the information related to an LLVM module.
unsigned getAddressSpace() const
Return the address space of the Pointer type.
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Value * getOperand(unsigned i) const
unsigned getNumOperands() const
LLVM Value Representation.
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
LLVM_ABI const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
This is an optimization pass for GlobalISel generic memory operations.
MaybeAlign getAlign(const CallInst &I, unsigned Index)
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
void replace(R &&Range, const T &OldValue, const T &NewValue)
Provide wrappers to std::replace which take ranges instead of having to pass begin/end explicitly.
decltype(auto) cast(const From &Val)
cast - Return the argument parameter cast to the specified type.
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
This struct is a compact representation of a valid (non-zero power of two) alignment.