34 #define DEBUG_TYPE "constmerge"
36 STATISTIC(NumMerged,
"Number of global constants merged");
41 if (!LLVMUsed)
return;
66 for (
const auto &V : MDs)
99 bool MadeChange =
false;
123 UsedGlobals.
count(GV))
161 UsedGlobals.
count(GV))
173 if (!Slot || Slot == GV)
186 Replacements.
push_back(std::make_pair(GV, Slot));
189 if (Replacements.
empty())
196 for (
unsigned i = 0, e = Replacements.
size(); i != e; ++i) {
198 if (Replacements[i].
first->getAlignment() ||
199 Replacements[i].second->getAlignment()) {
200 Replacements[i].second->setAlignment(
208 Replacements[i].first->replaceAllUsesWith(Replacements[i].second);
211 assert(Replacements[i].first->hasLocalLinkage() &&
212 "Refusing to delete an externally visible global variable.");
213 Replacements[i].first->eraseFromParent();
216 NumMerged += Replacements.
size();
217 Replacements.
clear();
228 struct ConstantMergeLegacyPass :
public ModulePass {
236 bool runOnModule(
Module &M) {
246 "Merge Duplicate Global Constants",
false,
false)
249 return new ConstantMergeLegacyPass();
void getAllMetadata(SmallVectorImpl< std::pair< unsigned, MDNode * >> &MDs) const
Appends all attachments for the global to MDs, sorting by attachment ID.
const Value * stripPointerCastsNoFollowAliases() const
Strip off pointer casts and all-zero GEPs.
void push_back(const T &Elt)
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
STATISTIC(NumFunctions,"Total number of functions")
A Module instance is used to store all the information related to an LLVM module. ...
void getDebugInfo(SmallVectorImpl< DIGlobalVariableExpression * > &GVs) const
Fill the vector with all debug info attachements.
unsigned getNumOperands() const
unsigned getAddressSpace() const
Return the address space of the Pointer type.
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...
LLVM_NODISCARD bool empty() const
global_iterator global_begin()
static bool IsBetterCanonical(const GlobalVariable &A, const GlobalVariable &B)
static unsigned getAlignment(GlobalVariable *GV)
unsigned getAlignment() const
bool hasDefinitiveInitializer() const
hasDefinitiveInitializer - Whether the global variable has an initializer, and any other instances of...
static bool hasMetadataOtherThanDebugLoc(const GlobalVariable *GV)
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
static bool isWeakForLinker(LinkageTypes Linkage)
Whether the definition of this global may be replaced at link time.
bool hasSection() const
Check if this global has a custom object file section.
A set of analyses that are preserved following a run of a transformation pass.
This is an important base class in LLVM.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static void copyDebugLocMetadata(const GlobalVariable *From, GlobalVariable *To)
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
void eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing module and deletes it...
Value * getOperand(unsigned i) const
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.
void initializeConstantMergeLegacyPassPass(PassRegistry &)
unsigned getPreferredAlignment(const GlobalVariable *GV) const
Returns the preferred alignment of the specified global.
global_iterator global_end()
INITIALIZE_PASS(ConstantMergeLegacyPass,"constmerge","Merge Duplicate Global Constants", false, false) ModulePass *llvm
Iterator for intrusive lists based on ilist_node.
bool hasGlobalUnnamedAddr() const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Module.h This file contains the declarations for the Module class.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
ConstantArray - Constant Array Declarations.
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.
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
void setUnnamedAddr(UnnamedAddr Val)
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
static bool mergeConstants(Module &M)
bool hasLocalLinkage() const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void removeDeadConstantUsers() const
If there are any dead constant users dangling off of this constant, remove them.
Module * getParent()
Get the module that this global value is contained inside of...
LLVM Value Representation.
void addDebugInfo(DIGlobalVariableExpression *GV)
Attach a DIGlobalVariableExpression.
A container for analyses that lazily runs them and caches their results.
GlobalVariable * getGlobalVariable(StringRef Name) const
Look up the specified global variable in the module symbol table.