86 #define DEBUG_TYPE "global-merge"
91 cl::desc(
"Enable the global merge pass"),
96 cl::desc(
"Set maximum offset for global merge pass"),
105 cl::desc(
"Improve global merge pass to ignore globals only used alone"),
110 cl::desc(
"Enable global merge pass on constants"),
117 cl::desc(
"Enable global merge pass on external linkage"));
119 STATISTIC(NumMerged,
"Number of globals merged");
133 bool OnlyOptimizeForSize;
136 bool MergeExternalGlobals;
141 Module &M,
bool isConst,
unsigned AddrSpace)
const;
146 unsigned AddrSpace)
const;
152 return MustKeepGlobalVariables.count(GV);
157 void setMustKeepGlobalVariables(
Module &M);
167 explicit GlobalMerge()
169 OnlyOptimizeForSize(
false), MergeExternalGlobals(
false) {
174 bool OnlyOptimizeForSize,
bool MergeExternalGlobals)
176 OnlyOptimizeForSize(OnlyOptimizeForSize),
177 MergeExternalGlobals(MergeExternalGlobals) {
181 bool doInitialization(
Module &M)
override;
182 bool runOnFunction(
Function &
F)
override;
183 bool doFinalization(
Module &M)
override;
185 StringRef getPassName()
const override {
return "Merge internal globals"; }
198 Module &M,
bool isConst,
unsigned AddrSpace)
const {
199 auto &
DL = M.getDataLayout();
201 std::stable_sort(Globals.begin(), Globals.end(),
211 return doMerge(Globals, AllGlobals, M, isConst, AddrSpace);
233 struct UsedGlobalSet {
234 UsedGlobalSet(
size_t Size) : Globals(Size), UsageCount(1) {}
240 std::vector<UsedGlobalSet> UsedGlobalSets;
243 auto CreateGlobalSet = [&]() -> UsedGlobalSet & {
244 UsedGlobalSets.emplace_back(Globals.size());
245 return UsedGlobalSets.back();
249 CreateGlobalSet().UsageCount = 0;
270 std::vector<size_t> EncounteredUGS;
272 for (
size_t GI = 0,
GE = Globals.size(); GI !=
GE; ++GI) {
276 std::fill(EncounteredUGS.begin(), EncounteredUGS.end(), 0);
278 EncounteredUGS.resize(UsedGlobalSets.size());
282 size_t CurGVOnlySetIdx = 0;
285 for (
auto &U : GV->
uses()) {
289 if (
ConstantExpr *CE = dyn_cast<ConstantExpr>(U.getUser())) {
292 UI = &*
CE->use_begin();
294 }
else if (isa<Instruction>(U.getUser())) {
303 for (; UI != UE; UI = UI->getNext()) {
314 size_t UGSIdx = GlobalUsesByFunction[ParentFn];
320 if (!CurGVOnlySetIdx) {
321 CurGVOnlySetIdx = UsedGlobalSets.
size();
322 CreateGlobalSet().Globals.set(GI);
324 ++UsedGlobalSets[CurGVOnlySetIdx].UsageCount;
327 GlobalUsesByFunction[ParentFn] = CurGVOnlySetIdx;
332 if (UsedGlobalSets[UGSIdx].Globals.test(GI)) {
333 ++UsedGlobalSets[UGSIdx].UsageCount;
338 --UsedGlobalSets[UGSIdx].UsageCount;
342 if (
size_t ExpandedIdx = EncounteredUGS[UGSIdx]) {
343 ++UsedGlobalSets[ExpandedIdx].UsageCount;
344 GlobalUsesByFunction[ParentFn] = ExpandedIdx;
350 GlobalUsesByFunction[ParentFn] = EncounteredUGS[UGSIdx] =
351 UsedGlobalSets.size();
353 UsedGlobalSet &NewUGS = CreateGlobalSet();
354 NewUGS.Globals.set(GI);
355 NewUGS.Globals |= UsedGlobalSets[UGSIdx].Globals;
366 std::sort(UsedGlobalSets.begin(), UsedGlobalSets.end(),
367 [](
const UsedGlobalSet &UGS1,
const UsedGlobalSet &UGS2) {
368 return UGS1.Globals.count() * UGS1.UsageCount <
369 UGS2.Globals.count() * UGS2.UsageCount;
377 for (
size_t i = 0, e = UsedGlobalSets.size(); i != e; ++i) {
378 const UsedGlobalSet &UGS = UsedGlobalSets[e - i - 1];
379 if (UGS.UsageCount == 0)
381 if (UGS.Globals.count() > 1)
382 AllGlobals |= UGS.Globals;
384 return doMerge(Globals, AllGlobals, M, isConst, AddrSpace);
394 bool Changed =
false;
396 for (
size_t i = 0, e = UsedGlobalSets.size(); i != e; ++i) {
397 const UsedGlobalSet &UGS = UsedGlobalSets[e - i - 1];
398 if (UGS.UsageCount == 0)
400 if (PickedGlobals.anyCommon(UGS.Globals))
402 PickedGlobals |= UGS.Globals;
406 if (UGS.Globals.count() < 2)
408 Changed |= doMerge(Globals, UGS.Globals, M, isConst, AddrSpace);
416 unsigned AddrSpace)
const {
422 DEBUG(
dbgs() <<
" Trying to merge set, starts with #"
428 uint64_t MergedSize = 0;
429 std::vector<Type*> Tys;
430 std::vector<Constant*> Inits;
432 bool HasExternal =
false;
434 for (j = i; j != -1; j = GlobalSet.
find_next(j)) {
435 Type *Ty = Globals[j]->getValueType();
436 MergedSize += DL.getTypeAllocSize(Ty);
437 if (MergedSize > MaxOffset) {
441 Inits.push_back(Globals[j]->getInitializer());
443 if (Globals[j]->hasExternalLinkage() && !HasExternal) {
445 FirstExternalName = Globals[j]->getName();
464 (IsMachO && HasExternal)
465 ?
"_MergedGlobals_" + FirstExternalName
469 M, MergedTy, isConst, MergedLinkage, MergedInit, MergedName,
nullptr,
472 const StructLayout *MergedLayout = DL.getStructLayout(MergedTy);
474 for (ssize_t k = i, idx = 0; k != j; k = GlobalSet.
find_next(k), ++idx) {
476 std::string
Name = Globals[k]->getName();
488 Globals[k]->replaceAllUsesWith(GEP);
489 Globals[k]->eraseFromParent();
519 MustKeepGlobalVariables.insert(
G);
522 void GlobalMerge::setMustKeepGlobalVariables(
Module &M) {
525 for (Function &
F : M) {
534 dyn_cast<GlobalVariable>(U->stripPointerCasts()))
535 MustKeepGlobalVariables.insert(GV);
541 bool GlobalMerge::doInitialization(
Module &M) {
550 bool Changed =
false;
551 setMustKeepGlobalVariables(M);
561 if (
TM && !
TM->shouldAssumeDSOLocal(M, &GV))
569 assert(PT &&
"Global variable is not a pointer!");
574 unsigned Alignment = DL.getPreferredAlignment(&GV);
576 if (Alignment > DL.getABITypeAlignment(Ty))
585 if (isMustKeepGlobalVariable(&GV))
588 if (DL.getTypeAllocSize(Ty) < MaxOffset) {
591 BSSGlobals[AddressSpace].push_back(&GV);
593 ConstGlobals[AddressSpace].push_back(&GV);
599 for (
auto &
P : Globals)
600 if (
P.second.size() > 1)
601 Changed |= doMerge(
P.second, M,
false,
P.first);
603 for (
auto &
P : BSSGlobals)
604 if (
P.second.size() > 1)
605 Changed |= doMerge(
P.second, M,
false,
P.first);
608 for (
auto &
P : ConstGlobals)
609 if (
P.second.size() > 1)
610 Changed |= doMerge(
P.second, M,
true,
P.first);
615 bool GlobalMerge::runOnFunction(Function &F) {
619 bool GlobalMerge::doFinalization(
Module &M) {
620 MustKeepGlobalVariables.clear();
625 bool OnlyOptimizeForSize,
626 bool MergeExternalByDefault) {
629 return new GlobalMerge(TM, Offset, OnlyOptimizeForSize, MergeExternal);
Pass interface - Implemented by all 'passes'.
void push_back(const T &Elt)
int find_first() const
find_first - Returns the index of the first set bit, -1 if none of the bits are set.
iterator_range< use_iterator > uses()
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
STATISTIC(NumFunctions,"Total number of functions")
GlobalVariable * collectUsedGlobalVariables(const Module &M, SmallPtrSetImpl< GlobalValue * > &Set, bool CompilerUsed)
Given "llvm.used" or "llvm.compiler.used" as a global name, collect the initializer elements of that ...
A Module instance is used to store all the information related to an LLVM module. ...
static cl::opt< bool > GlobalMergeGroupByUse("global-merge-group-by-use", cl::Hidden, cl::desc("Improve global merge pass to look at uses"), cl::init(true))
const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs, and aliases.
unsigned getNumOperands() const
int find_next(unsigned Prev) const
find_next - Returns the index of the next set bit following the "Prev" bit.
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Type * getValueType() const
Like Internal, but omit from symbol table.
Externally visible function.
Pass * createGlobalMergePass(const TargetMachine *TM, unsigned MaximalOffset, bool OnlyOptimizeForSize=false, bool MergeExternalByDefault=false)
GlobalMerge - This pass merges internal (by default) globals into structs to enable reuse of a base p...
const Function * getParent() const
Return the enclosing method, or null if none.
unsigned getAddressSpace() const
Return the address space of the Pointer type.
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
const std::string & getTargetTriple() const
Get the target triple which is a string describing the target host.
bool optForMinSize() const
Optimize this function for minimum size (-Oz).
StringRef getName() const
Return a constant reference to the value's name.
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Class to represent struct types.
A Use represents the edge between a Value definition and its users.
bool hasInternalLinkage() const
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
This file contains the simple types necessary to represent the attributes associated with functions a...
static cl::opt< bool > EnableGlobalMerge("enable-global-merge", cl::Hidden, cl::desc("Enable the global merge pass"), cl::init(true))
Windows NT (Windows on ARM)
A constant value that is initialized with an expression using other constant values.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Class to represent pointers.
uint64_t getElementOffset(unsigned Idx) const
bool hasSection() const
Check if this global has a custom object file section.
Function Alias Analysis false
initializer< Ty > init(const Ty &Val)
static Constant * getInBoundsGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant * > IdxList)
Create an "inbounds" getelementptr.
LLVM Basic Block Representation.
The instances of the Type class are immutable: once they are created, they are never changed...
This is an important base class in LLVM.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Value * getOperand(unsigned i) const
static Constant * get(StructType *T, ArrayRef< Constant * > V)
bool isThreadLocal() const
If the value is "Thread Local", its value isn't shared by the threads.
Triple - Helper class for working with autoconf configuration names.
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
bool hasExternalLinkage() const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
uint64_t getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
static StructType * get(LLVMContext &Context, ArrayRef< Type * > Elements, bool isPacked=false)
This static method is the primary way to create a literal StructType.
Module.h This file contains the declarations for the Module class.
static cl::opt< bool > GlobalMergeIgnoreSingleUse("global-merge-ignore-single-use", cl::Hidden, cl::desc("Improve global merge pass to ignore globals only used alone"), cl::init(true))
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
static cl::opt< cl::boolOrDefault > EnableGlobalMergeOnExternal("global-merge-on-external", cl::Hidden, cl::desc("Enable global merge pass on external linkage"))
ConstantArray - Constant Array Declarations.
bool hasInitializer() const
Definitions have initializers, declarations don't.
LinkageTypes
An enumeration for the kinds of linkage for global values.
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
bool isEHPad() const
Return true if the instruction is a variety of EH-block.
bool hasImplicitSection() const
Check if section name is present.
PointerType * getType() const
Global values are always pointers.
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
static IntegerType * getInt32Ty(LLVMContext &C)
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Rename collisions when linking (static functions).
static cl::opt< unsigned > GlobalMergeMaxOffset("global-merge-max-offset", cl::Hidden, cl::desc("Set maximum offset for global merge pass"), cl::init(0))
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static cl::opt< bool > EnableGlobalMergeOnConst("global-merge-on-const", cl::Hidden, cl::desc("Enable global merge pass on constants"), cl::init(false))
void initializeGlobalMergePass(PassRegistry &)
Primary interface to the complete machine description for the target machine.
iterator_range< global_iterator > globals()
StringRef - Represent a constant reference to a string, i.e.
static SectionKind getKindForGlobal(const GlobalObject *GO, const TargetMachine &TM)
Classify the specified global variable into a set of target independent categories embodied in Sectio...
void sort(Policy policy, RandomAccessIterator Start, RandomAccessIterator End, const Comparator &Comp=Comparator())
const BasicBlock * getParent() const
static GlobalAlias * create(Type *Ty, unsigned AddressSpace, LinkageTypes Linkage, const Twine &Name, Constant *Aliasee, Module *Parent)
If a parent module is specified, the alias is automatically inserted into the end of the specified mo...
GlobalVariable * getGlobalVariable(StringRef Name) const
Look up the specified global variable in the module symbol table.
LLVMContext & getContext() const
Get the global data context.
This file describes how to lower LLVM code to machine code.