33 #define DEBUG_TYPE "constmerge"
35 STATISTIC(NumMerged,
"Number of global constants merged");
46 bool runOnModule(
Module &M)
override;
60 "Merge Duplicate Global Constants",
false,
false)
69 if (!LLVMUsed)
return;
98 bool ConstantMerge::runOnModule(
Module &M) {
111 bool MadeChange =
false;
135 UsedGlobals.
count(GV))
169 UsedGlobals.
count(GV))
181 if (!Slot || Slot == GV)
191 Replacements.
push_back(std::make_pair(GV, Slot));
194 if (Replacements.
empty())
201 for (
unsigned i = 0, e = Replacements.
size(); i != e; ++i) {
203 if (Replacements[i].first->getAlignment() ||
204 Replacements[i].second->getAlignment()) {
205 Replacements[i].second->setAlignment(
206 std::max(getAlignment(Replacements[i].first),
207 getAlignment(Replacements[i].second)));
211 Replacements[i].first->replaceAllUsesWith(Replacements[i].second);
214 assert(Replacements[i].first->hasLocalLinkage() &&
215 "Refusing to delete an externally visible global variable.");
216 Replacements[i].first->eraseFromParent();
219 NumMerged += Replacements.
size();
220 Replacements.
clear();
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...
global_iterator global_begin()
static bool IsBetterCanonical(const GlobalVariable &A, const GlobalVariable &B)
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
INITIALIZE_PASS(ConstantMerge,"constmerge","Merge Duplicate Global Constants", false, false) ModulePass *llvm
unsigned getAlignment() const
bool hasDefinitiveInitializer() const
hasDefinitiveInitializer - Whether the global variable has an initializer, and any other instances of...
static bool isWeakForLinker(LinkageTypes Linkage)
Whether the definition of this global may be replaced at link time.
Value * stripPointerCastsNoFollowAliases()
Strip off pointer casts and all-zero GEPs.
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...
unsigned getPreferredAlignment(const GlobalVariable *GV) const
Returns the preferred alignment of the specified global.
global_iterator global_end()
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.
void setUnnamedAddr(bool Val)
static cl::opt< AlignMode > Align(cl::desc("Load/store alignment support"), cl::Hidden, cl::init(NoStrictAlign), cl::values(clEnumValN(StrictAlign,"aarch64-strict-align","Disallow all unaligned memory accesses"), clEnumValN(NoStrictAlign,"aarch64-no-strict-align","Allow unaligned memory accesses"), clEnumValEnd))
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...
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.
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
void initializeConstantMergePass(PassRegistry &)
bool hasLocalLinkage() const
void removeDeadConstantUsers() const
removeDeadConstantUsers - If there are any dead constant users dangling off of this constant...
Module * getParent()
Get the module that this global value is contained inside of...
LLVM Value Representation.
bool hasUnnamedAddr() const
GlobalVariable * getGlobalVariable(StringRef Name) const
Look up the specified global variable in the module symbol table.