41 #define DEBUG_TYPE "constmerge" 43 STATISTIC(NumIdenticalMerged,
"Number of identical global constants merged");
48 if (!LLVMUsed)
return;
73 for (
const auto &V : MDs)
74 if (V.first != LLVMContext::MD_dbg)
101 UsedGlobals.
count(GV);
131 "Refusing to delete an externally visible global variable.");
145 SameContentReplacements;
147 size_t ChangesMade = 0;
148 size_t OldChangesMade = 0;
190 bool FirstConstantFound = !Slot;
194 << (FirstConstantFound ?
"\n" :
" (updated)\n"));
216 auto Found = CMap.find(Init);
217 if (Found == CMap.end())
230 SameContentReplacements.
push_back(std::make_pair(GV, Slot));
236 for (
unsigned i = 0,
e = SameContentReplacements.
size(); i !=
e; ++i) {
241 ++NumIdenticalMerged;
244 if (ChangesMade == OldChangesMade)
246 OldChangesMade = ChangesMade;
248 SameContentReplacements.
clear();
263 struct ConstantMergeLegacyPass :
public ModulePass {
272 bool runOnModule(
Module &M)
override {
284 "Merge Duplicate Global Constants",
false,
false)
287 return new ConstantMergeLegacyPass();
unsigned getAlignment() const
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...
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
This class represents lattice values for constants.
A Module instance is used to store all the information related to an LLVM module. ...
void push_back(const T &Elt)
void replace(Container &Cont, typename Container::iterator ContIt, typename Container::iterator ContEnd, RandomAccessIterator ValIt, RandomAccessIterator ValEnd)
Given a sequence container Cont, replace the range [ContIt, ContEnd) with the range [ValIt...
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)
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
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.
const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
void initializeConstantMergeLegacyPassPass(PassRegistry &)
This struct is a compact representation of a valid (non-zero power of two) alignment.
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...
BlockVerifier::State From
Align max(MaybeAlign Lhs, Align Rhs)
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
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
unsigned getPreferredAlignment(const GlobalVariable *GV) const
Returns the preferred alignment of the specified global.
ConstantArray - Constant Array Declarations.
PreservedAnalyses run(Module &M, ModuleAnalysisManager &)
void setAlignment(MaybeAlign Align)
void setUnnamedAddr(UnnamedAddr Val)
static bool isUnmergeableGlobal(GlobalVariable *GV, const SmallPtrSetImpl< const GlobalValue *> &UsedGlobals)
StringRef getName() const
Return a constant reference to the value's name.
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.
static CanMerge makeMergeable(GlobalVariable *Old, GlobalVariable *New)
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.