34 #define DEBUG_TYPE "constmerge"
36 STATISTIC(NumMerged,
"Number of global constants merged");
41 if (!LLVMUsed)
return;
82 bool MadeChange =
false;
106 UsedGlobals.
count(GV))
140 UsedGlobals.
count(GV))
152 if (!Slot || Slot == GV)
162 Replacements.
push_back(std::make_pair(GV, Slot));
165 if (Replacements.
empty())
172 for (
unsigned i = 0, e = Replacements.
size();
i != e; ++
i) {
174 if (Replacements[
i].first->getAlignment() ||
175 Replacements[
i].second->getAlignment()) {
176 Replacements[
i].second->setAlignment(
182 Replacements[
i].first->replaceAllUsesWith(Replacements[
i].second);
185 assert(Replacements[
i].first->hasLocalLinkage() &&
186 "Refusing to delete an externally visible global variable.");
187 Replacements[
i].first->eraseFromParent();
190 NumMerged += Replacements.
size();
191 Replacements.
clear();
202 struct ConstantMergeLegacyPass :
public ModulePass {
210 bool runOnModule(
Module &M) {
220 "Merge Duplicate Global Constants",
false,
false)
223 return new ConstantMergeLegacyPass();
void push_back(const T &Elt)
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
STATISTIC(NumFunctions,"Total number of functions")
A Module instance is used to store all the information related to an LLVM module. ...
unsigned getNumOperands() const
size_type count(PtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
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...
void eraseFromParent() override
eraseFromParent - This method unlinks 'this' from the containing module and deletes it...
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
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.
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.
bool hasSection() const
Check if this global has a custom object file section.
Value * stripPointerCastsNoFollowAliases()
Strip off pointer casts and all-zero GEPs.
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...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
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.
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...
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.
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.