41 #define DEBUG_TYPE "constmerge" 43 STATISTIC(NumMerged,
"Number of global constants merged");
48 if (!LLVMUsed)
return;
73 for (
const auto &V : MDs)
106 bool MadeChange =
false;
129 UsedGlobals.
count(GV))
167 UsedGlobals.
count(GV))
179 if (!Slot || Slot == GV)
192 Replacements.
push_back(std::make_pair(GV, Slot));
195 if (Replacements.
empty())
202 for (
unsigned i = 0, e = Replacements.
size(); i != e; ++i) {
204 if (Replacements[i].
first->getAlignment() ||
205 Replacements[i].second->getAlignment()) {
206 Replacements[i].second->setAlignment(
214 Replacements[i].first->replaceAllUsesWith(Replacements[i].second);
217 assert(Replacements[i].first->hasLocalLinkage() &&
218 "Refusing to delete an externally visible global variable.");
219 Replacements[i].first->eraseFromParent();
222 NumMerged += Replacements.
size();
223 Replacements.
clear();
235 struct ConstantMergeLegacyPass :
public ModulePass {
244 bool runOnModule(
Module &M)
override {
256 "Merge Duplicate Global Constants",
false,
false)
259 return new ConstantMergeLegacyPass();
unsigned getAlignment() const
void push_back(const T &Elt)
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
bool hasDefinitiveInitializer() const
hasDefinitiveInitializer - Whether the global variable has an initializer, and any other instances of...
bool hasLocalLinkage() const
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
Compute iterated dominance frontiers using a linear time algorithm.
A Module instance is used to store all the information related to an LLVM module. ...
GlobalVariable * getGlobalVariable(StringRef Name) const
Look up the specified global variable in the module symbol table.
STATISTIC(NumFunctions, "Total number of functions")
void getAllMetadata(SmallVectorImpl< std::pair< unsigned, MDNode *>> &MDs) const
Appends all attachments for the global to MDs, sorting by attachment ID.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
global_iterator global_begin()
static bool IsBetterCanonical(const GlobalVariable &A, const GlobalVariable &B)
static unsigned getAlignment(GlobalVariable *GV)
static bool hasMetadataOtherThanDebugLoc(const GlobalVariable *GV)
Value * getOperand(unsigned i) const
void removeDeadConstantUsers() const
If there are any dead constant users dangling off of this constant, remove them.
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.
INITIALIZE_PASS(ConstantMergeLegacyPass, "constmerge", "Merge Duplicate Global Constants", false, false) ModulePass *llvm
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
A set of analyses that are preserved following a run of a transformation pass.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
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 getDebugInfo(SmallVectorImpl< DIGlobalVariableExpression *> &GVs) const
Fill the vector with all debug info attachements.
void eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing module and deletes it...
ModulePass * createConstantMergePass()
createConstantMergePass - This function returns a new pass that merges duplicate global constants tog...
unsigned getAddressSpace() const
Return the address space of the Pointer type.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
void initializeConstantMergeLegacyPassPass(PassRegistry &)
global_iterator global_end()
Iterator for intrusive lists based on ilist_node.
unsigned getNumOperands() const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
bool hasSection() const
Check if this global has a custom object file section.
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.
bool hasGlobalUnnamedAddr() const
unsigned getPreferredAlignment(const GlobalVariable *GV) const
Returns the preferred alignment of the specified global.
ConstantArray - Constant Array Declarations.
PreservedAnalyses run(Module &M, ModuleAnalysisManager &)
void setUnnamedAddr(UnnamedAddr Val)
LLVM_NODISCARD bool empty() const
const Value * stripPointerCastsNoFollowAliases() const
Strip off pointer casts and all-zero GEPs.
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
static bool mergeConstants(Module &M)
static void FindUsedValues(GlobalVariable *LLVMUsed, SmallPtrSetImpl< const GlobalValue *> &UsedValues)
Find values that are marked as llvm.used.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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.
PointerType * getType() const
Global values are always pointers.