LLVM 19.0.0git
Classes | Macros | Typedefs | Functions | Variables
GlobalOpt.cpp File Reference
#include "llvm/Transforms/IPO/GlobalOpt.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/ADT/Twine.h"
#include "llvm/ADT/iterator_range.h"
#include "llvm/Analysis/BlockFrequencyInfo.h"
#include "llvm/Analysis/ConstantFolding.h"
#include "llvm/Analysis/MemoryBuiltins.h"
#include "llvm/Analysis/TargetLibraryInfo.h"
#include "llvm/Analysis/TargetTransformInfo.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/BinaryFormat/Dwarf.h"
#include "llvm/IR/Attributes.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/CallingConv.h"
#include "llvm/IR/Constant.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/GlobalAlias.h"
#include "llvm/IR/GlobalValue.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Operator.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Use.h"
#include "llvm/IR/User.h"
#include "llvm/IR/Value.h"
#include "llvm/IR/ValueHandle.h"
#include "llvm/Support/AtomicOrdering.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/IPO.h"
#include "llvm/Transforms/Utils/CtorUtils.h"
#include "llvm/Transforms/Utils/Evaluator.h"
#include "llvm/Transforms/Utils/GlobalStatus.h"
#include "llvm/Transforms/Utils/Local.h"
#include <cassert>
#include <cstdint>
#include <optional>
#include <utility>
#include <vector>

Go to the source code of this file.

Classes

struct  GlobalPart
 Part of the global at a specific offset, which is only accessed through loads and stores with the given type. More...
 

Macros

#define DEBUG_TYPE   "globalopt"
 

Typedefs

using ChangeableCCCacheTy = SmallDenseMap< Function *, bool, 8 >
 

Functions

 STATISTIC (NumMarked, "Number of globals marked constant")
 
 STATISTIC (NumUnnamed, "Number of globals marked unnamed_addr")
 
 STATISTIC (NumSRA, "Number of aggregate globals broken into scalars")
 
 STATISTIC (NumSubstitute,"Number of globals with initializers stored into them")
 
 STATISTIC (NumDeleted, "Number of globals deleted")
 
 STATISTIC (NumGlobUses, "Number of global uses devirtualized")
 
 STATISTIC (NumLocalized, "Number of globals localized")
 
 STATISTIC (NumShrunkToBool, "Number of global vars shrunk to booleans")
 
 STATISTIC (NumFastCallFns, "Number of functions converted to fastcc")
 
 STATISTIC (NumCtorsEvaluated, "Number of static ctors evaluated")
 
 STATISTIC (NumNestRemoved, "Number of nest attributes removed")
 
 STATISTIC (NumAliasesResolved, "Number of global aliases resolved")
 
 STATISTIC (NumAliasesRemoved, "Number of global aliases eliminated")
 
 STATISTIC (NumCXXDtorsRemoved, "Number of global C++ destructors removed")
 
 STATISTIC (NumInternalFunc, "Number of internal functions")
 
 STATISTIC (NumColdCC, "Number of functions marked coldcc")
 
 STATISTIC (NumIFuncsResolved, "Number of statically resolved IFuncs")
 
 STATISTIC (NumIFuncsDeleted, "Number of IFuncs removed")
 
static bool isLeakCheckerRoot (GlobalVariable *GV)
 Is this global variable possibly used by a leak checker as a root? If so, we might not really want to eliminate the stores to it.
 
static bool IsSafeComputationToRemove (Value *V, function_ref< TargetLibraryInfo &(Function &)> GetTLI)
 Given a value that is stored to a global but never read, determine whether it's safe to remove the store and the chain of computation that feeds the store.
 
static bool CleanupPointerRootUsers (GlobalVariable *GV, function_ref< TargetLibraryInfo &(Function &)> GetTLI)
 This GV is a pointer root.
 
static bool CleanupConstantGlobalUsers (GlobalVariable *GV, const DataLayout &DL)
 We just marked GV constant.
 
static bool collectSRATypes (DenseMap< uint64_t, GlobalPart > &Parts, GlobalVariable *GV, const DataLayout &DL)
 Look at all uses of the global and determine which (offset, type) pairs it can be split into.
 
static void transferSRADebugInfo (GlobalVariable *GV, GlobalVariable *NGV, uint64_t FragmentOffsetInBits, uint64_t FragmentSizeInBits, uint64_t VarSize)
 Copy over the debug info for a variable to its SRA replacements.
 
static GlobalVariableSRAGlobal (GlobalVariable *GV, const DataLayout &DL)
 Perform scalar replacement of aggregates on the specified global variable.
 
static bool AllUsesOfValueWillTrapIfNull (const Value *V, SmallPtrSetImpl< const PHINode * > &PHIs)
 Return true if all users of the specified value will trap if the value is dynamically null.
 
static bool allUsesOfLoadedValueWillTrapIfNull (const GlobalVariable *GV)
 Return true if all uses of any loads from GV will trap if the loaded value is null.
 
static void allUsesOfLoadAndStores (GlobalVariable *GV, SmallVector< Value *, 4 > &Uses)
 Get all the loads/store uses for global variable GV.
 
static bool OptimizeAwayTrappingUsesOfValue (Value *V, Constant *NewV)
 
static bool OptimizeAwayTrappingUsesOfLoads (GlobalVariable *GV, Constant *LV, const DataLayout &DL, function_ref< TargetLibraryInfo &(Function &)> GetTLI)
 The specified global has only one non-null value stored into it.
 
static void ConstantPropUsersOf (Value *V, const DataLayout &DL, TargetLibraryInfo *TLI)
 Walk the use list of V, constant folding all of the instructions that are foldable.
 
static GlobalVariableOptimizeGlobalAddressOfAllocation (GlobalVariable *GV, CallInst *CI, uint64_t AllocSize, Constant *InitVal, const DataLayout &DL, TargetLibraryInfo *TLI)
 This function takes the specified global variable, and transforms the program as if it always contained the result of the specified malloc.
 
static bool valueIsOnlyUsedLocallyOrStoredToOneGlobal (const CallInst *CI, const GlobalVariable *GV)
 Scan the use-list of GV checking to make sure that there are no complex uses of GV.
 
static bool tryToOptimizeStoreOfAllocationToGlobal (GlobalVariable *GV, CallInst *CI, const DataLayout &DL, TargetLibraryInfo *TLI)
 If we have a global that is only initialized with a fixed size allocation try to transform the program to use global memory instead of heap allocated memory.
 
static bool optimizeOnceStoredGlobal (GlobalVariable *GV, Value *StoredOnceVal, const DataLayout &DL, function_ref< TargetLibraryInfo &(Function &)> GetTLI)
 
static bool TryToShrinkGlobalToBoolean (GlobalVariable *GV, Constant *OtherVal)
 At this point, we have learned that the only two values ever stored into GV are its initializer and OtherVal.
 
static bool deleteIfDead (GlobalValue &GV, SmallPtrSetImpl< const Comdat * > &NotDiscardableComdats, function_ref< void(Function &)> DeleteFnCallback=nullptr)
 
static bool isPointerValueDeadOnEntryToFunction (const Function *F, GlobalValue *GV, function_ref< DominatorTree &(Function &)> LookupDomTree)
 
static bool forwardStoredOnceStore (GlobalVariable *GV, const StoreInst *StoredOnceStore, function_ref< DominatorTree &(Function &)> LookupDomTree)
 
static bool processInternalGlobal (GlobalVariable *GV, const GlobalStatus &GS, function_ref< TargetTransformInfo &(Function &)> GetTTI, function_ref< TargetLibraryInfo &(Function &)> GetTLI, function_ref< DominatorTree &(Function &)> LookupDomTree)
 Analyze the specified global variable and optimize it if possible.
 
static bool processGlobal (GlobalValue &GV, function_ref< TargetTransformInfo &(Function &)> GetTTI, function_ref< TargetLibraryInfo &(Function &)> GetTLI, function_ref< DominatorTree &(Function &)> LookupDomTree)
 Analyze the specified global variable and optimize it if possible.
 
static void ChangeCalleesToFastCall (Function *F)
 Walk all of the direct calls of the specified function, changing them to FastCC.
 
static AttributeList StripAttr (LLVMContext &C, AttributeList Attrs, Attribute::AttrKind A)
 
static void RemoveAttribute (Function *F, Attribute::AttrKind A)
 
static bool hasChangeableCCImpl (Function *F)
 Return true if this is a calling convention that we'd like to change.
 
static bool hasChangeableCC (Function *F, ChangeableCCCacheTy &ChangeableCCCache)
 
static bool isColdCallSite (CallBase &CB, BlockFrequencyInfo &CallerBFI)
 Return true if the block containing the call site has a BlockFrequency of less than ColdCCRelFreq% of the entry block.
 
static bool isValidCandidateForColdCC (Function &F, function_ref< BlockFrequencyInfo &(Function &)> GetBFI, const std::vector< Function * > &AllCallsCold)
 
static void changeCallSitesToColdCC (Function *F)
 
static bool hasOnlyColdCalls (Function &F, function_ref< BlockFrequencyInfo &(Function &)> GetBFI, ChangeableCCCacheTy &ChangeableCCCache)
 
static bool hasMustTailCallers (Function *F)
 
static bool hasInvokeCallers (Function *F)
 
static void RemovePreallocated (Function *F)
 
static bool OptimizeFunctions (Module &M, function_ref< TargetLibraryInfo &(Function &)> GetTLI, function_ref< TargetTransformInfo &(Function &)> GetTTI, function_ref< BlockFrequencyInfo &(Function &)> GetBFI, function_ref< DominatorTree &(Function &)> LookupDomTree, SmallPtrSetImpl< const Comdat * > &NotDiscardableComdats, function_ref< void(Function &F)> ChangedCFGCallback, function_ref< void(Function &F)> DeleteFnCallback)
 
static bool OptimizeGlobalVars (Module &M, function_ref< TargetTransformInfo &(Function &)> GetTTI, function_ref< TargetLibraryInfo &(Function &)> GetTLI, function_ref< DominatorTree &(Function &)> LookupDomTree, SmallPtrSetImpl< const Comdat * > &NotDiscardableComdats)
 
static bool EvaluateStaticConstructor (Function *F, const DataLayout &DL, TargetLibraryInfo *TLI)
 Evaluate static constructors in the function, if we can.
 
static int compareNames (Constant *const *A, Constant *const *B)
 
static void setUsedInitializer (GlobalVariable &V, const SmallPtrSetImpl< GlobalValue * > &Init)
 
static bool hasUseOtherThanLLVMUsed (GlobalAlias &GA, const LLVMUsed &U)
 
static bool mayHaveOtherReferences (GlobalValue &GV, const LLVMUsed &U)
 
static bool hasUsesToReplace (GlobalAlias &GA, const LLVMUsed &U, bool &RenameTarget)
 
static bool OptimizeGlobalAliases (Module &M, SmallPtrSetImpl< const Comdat * > &NotDiscardableComdats)
 
static FunctionFindCXAAtExit (Module &M, function_ref< TargetLibraryInfo &(Function &)> GetTLI)
 
Returns whether the given function is an empty C destructor and can therefore be eliminated Note that we assume that other optimization passes have already simplified the code so we simply check for static ret bool cxxDtorIsEmpty (const Function &Fn)
 
static bool OptimizeEmptyGlobalCXXDtors (Function *CXAAtExitFn)
 
static FunctionhasSideeffectFreeStaticResolution (GlobalIFunc &IF)
 
Find IFuncs that have resolvers that always point at the same statically known and replace their callers with a direct static call bool OptimizeStaticIFuncs (Module &M)
 
static bool DeleteDeadIFuncs (Module &M, SmallPtrSetImpl< const Comdat * > &NotDiscardableComdats)
 
static bool optimizeGlobalsInModule (Module &M, const DataLayout &DL, function_ref< TargetLibraryInfo &(Function &)> GetTLI, function_ref< TargetTransformInfo &(Function &)> GetTTI, function_ref< BlockFrequencyInfo &(Function &)> GetBFI, function_ref< DominatorTree &(Function &)> LookupDomTree, function_ref< void(Function &F)> ChangedCFGCallback, function_ref< void(Function &F)> DeleteFnCallback)
 

Variables

static cl::opt< boolEnableColdCCStressTest ("enable-coldcc-stress-test", cl::desc("Enable stress test of coldcc by adding " "calling conv to all internal functions."), cl::init(false), cl::Hidden)
 
static cl::opt< int > ColdCCRelFreq ("coldcc-rel-freq", cl::Hidden, cl::init(2), cl::desc("Maximum block frequency, expressed as a percentage of caller's " "entry frequency, for a call site to be considered cold for enabling" "coldcc"))
 
Find IFuncs that have resolvers that always point at the same statically known callee
 

Macro Definition Documentation

◆ DEBUG_TYPE

#define DEBUG_TYPE   "globalopt"

Definition at line 74 of file GlobalOpt.cpp.

Typedef Documentation

◆ ChangeableCCCacheTy

Definition at line 1716 of file GlobalOpt.cpp.

Function Documentation

◆ allUsesOfLoadAndStores()

static void allUsesOfLoadAndStores ( GlobalVariable GV,
SmallVector< Value *, 4 > &  Uses 
)
static

◆ allUsesOfLoadedValueWillTrapIfNull()

static bool allUsesOfLoadedValueWillTrapIfNull ( const GlobalVariable GV)
static

Return true if all uses of any loads from GV will trap if the loaded value is null.

Note that this also permits comparisons of the loaded value against null, as a special case.

Definition at line 698 of file GlobalOpt.cpp.

References AllUsesOfValueWillTrapIfNull(), llvm::SmallVectorBase< Size_T >::empty(), P, llvm::SmallVectorImpl< T >::pop_back_val(), and llvm::SmallVectorTemplateBase< T, bool >::push_back().

Referenced by tryToOptimizeStoreOfAllocationToGlobal().

◆ AllUsesOfValueWillTrapIfNull()

static bool AllUsesOfValueWillTrapIfNull ( const Value V,
SmallPtrSetImpl< const PHINode * > &  PHIs 
)
static

Return true if all users of the specified value will trap if the value is dynamically null.

PHIs keeps track of any phi nodes we've seen to avoid reprocessing them.

Definition at line 644 of file GlobalOpt.cpp.

References AllUsesOfValueWillTrapIfNull(), assert(), I, llvm::SmallPtrSetImpl< PtrType >::insert(), and llvm::NullPointerIsDefined().

Referenced by allUsesOfLoadedValueWillTrapIfNull(), and AllUsesOfValueWillTrapIfNull().

◆ ChangeCalleesToFastCall()

static void ChangeCalleesToFastCall ( Function F)
static

Walk all of the direct calls of the specified function, changing them to FastCC.

Definition at line 1654 of file GlobalOpt.cpp.

References F, and llvm::CallingConv::Fast.

Referenced by OptimizeFunctions().

◆ changeCallSitesToColdCC()

static void changeCallSitesToColdCC ( Function F)
static

Definition at line 1763 of file GlobalOpt.cpp.

References llvm::CallingConv::Cold, and F.

Referenced by OptimizeFunctions().

◆ CleanupConstantGlobalUsers()

static bool CleanupConstantGlobalUsers ( GlobalVariable GV,
const DataLayout DL 
)
static

◆ CleanupPointerRootUsers()

static bool CleanupPointerRootUsers ( GlobalVariable GV,
function_ref< TargetLibraryInfo &(Function &)>  GetTLI 
)
static

This GV is a pointer root.

Loop over all users of the global and clean up any that obviously don't assign the global a value that isn't dynamically allocated.

Definition at line 191 of file GlobalOpt.cpp.

References llvm::append_range(), llvm::SmallVectorBase< Size_T >::empty(), I, llvm::isAllocationFn(), llvm::GlobalVariable::isConstant(), IsSafeComputationToRemove(), llvm::SmallVectorImpl< T >::pop_back_val(), llvm::Constant::removeDeadConstantUsers(), and llvm::Value::users().

Referenced by OptimizeAwayTrappingUsesOfLoads(), and processInternalGlobal().

◆ collectSRATypes()

static bool collectSRATypes ( DenseMap< uint64_t, GlobalPart > &  Parts,
GlobalVariable GV,
const DataLayout DL 
)
static

◆ compareNames()

static int compareNames ( Constant *const A,
Constant *const B 
)
static

◆ ConstantPropUsersOf()

static void ConstantPropUsersOf ( Value V,
const DataLayout DL,
TargetLibraryInfo TLI 
)
static

Walk the use list of V, constant folding all of the instructions that are foldable.

Definition at line 882 of file GlobalOpt.cpp.

References llvm::ConstantFoldInstruction(), DL, E, I, and llvm::isInstructionTriviallyDead().

Referenced by OptimizeGlobalAddressOfAllocation().

◆ cxxDtorIsEmpty()

Returns whether the given function is an empty C destructor and can therefore be eliminated Note that we assume that other optimization passes have already simplified the code so we simply check for static ret bool cxxDtorIsEmpty ( const Function Fn)
static

◆ DeleteDeadIFuncs()

static bool DeleteDeadIFuncs ( Module M,
SmallPtrSetImpl< const Comdat * > &  NotDiscardableComdats 
)
static

Definition at line 2452 of file GlobalOpt.cpp.

References deleteIfDead(), and llvm::make_early_inc_range().

Referenced by optimizeGlobalsInModule().

◆ deleteIfDead()

static bool deleteIfDead ( GlobalValue GV,
SmallPtrSetImpl< const Comdat * > &  NotDiscardableComdats,
function_ref< void(Function &)>  DeleteFnCallback = nullptr 
)
static

◆ EvaluateStaticConstructor()

static bool EvaluateStaticConstructor ( Function F,
const DataLayout DL,
TargetLibraryInfo TLI 
)
static

Evaluate static constructors in the function, if we can.

Return true if we can, false otherwise.

Definition at line 2062 of file GlobalOpt.cpp.

References llvm::dbgs(), DL, llvm::Evaluator::EvaluateFunction(), F, llvm::Evaluator::getInvariants(), llvm::Evaluator::getMutatedInitializers(), and LLVM_DEBUG.

Referenced by optimizeGlobalsInModule().

◆ FindCXAAtExit()

static Function * FindCXAAtExit ( Module M,
function_ref< TargetLibraryInfo &(Function &)>  GetTLI 
)
static

Definition at line 2322 of file GlobalOpt.cpp.

References F.

Referenced by optimizeGlobalsInModule().

◆ forwardStoredOnceStore()

static bool forwardStoredOnceStore ( GlobalVariable GV,
const StoreInst StoredOnceStore,
function_ref< DominatorTree &(Function &)>  LookupDomTree 
)
static

◆ hasChangeableCC()

static bool hasChangeableCC ( Function F,
ChangeableCCCacheTy ChangeableCCCache 
)
static

◆ hasChangeableCCImpl()

static bool hasChangeableCCImpl ( Function F)
static

Return true if this is a calling convention that we'd like to change.

The idea here is that we don't want to mess with the convention if the user explicitly requested something with performance implications like coldcc, GHC, or anyregcc.

Definition at line 1684 of file GlobalOpt.cpp.

References llvm::CallingConv::C, CC, F, llvm::CallInst::isMustTailCall(), and llvm::CallingConv::X86_ThisCall.

Referenced by hasChangeableCC().

◆ hasInvokeCallers()

static bool hasInvokeCallers ( Function F)
static

Definition at line 1821 of file GlobalOpt.cpp.

References F.

Referenced by OptimizeFunctions().

◆ hasMustTailCallers()

static bool hasMustTailCallers ( Function F)
static

Definition at line 1807 of file GlobalOpt.cpp.

References assert(), F, and llvm::CallBase::isMustTailCall().

Referenced by OptimizeFunctions().

◆ hasOnlyColdCalls()

static bool hasOnlyColdCalls ( Function F,
function_ref< BlockFrequencyInfo &(Function &)>  GetBFI,
ChangeableCCCacheTy ChangeableCCCache 
)
static

◆ hasSideeffectFreeStaticResolution()

static Function * hasSideeffectFreeStaticResolution ( GlobalIFunc IF)
static

Definition at line 2409 of file GlobalOpt.cpp.

References llvm::any_of(), llvm::BasicBlock::getTerminator(), and I.

Referenced by OptimizeStaticIFuncs().

◆ hasUseOtherThanLLVMUsed()

static bool hasUseOtherThanLLVMUsed ( GlobalAlias GA,
const LLVMUsed &  U 
)
static

Definition at line 2192 of file GlobalOpt.cpp.

References assert(), llvm::Value::hasOneUse(), and llvm::Value::use_empty().

Referenced by hasUsesToReplace().

◆ hasUsesToReplace()

static bool hasUsesToReplace ( GlobalAlias GA,
const LLVMUsed &  U,
bool RenameTarget 
)
static

◆ isColdCallSite()

static bool isColdCallSite ( CallBase CB,
BlockFrequencyInfo CallerBFI 
)
static

Return true if the block containing the call site has a BlockFrequency of less than ColdCCRelFreq% of the entry block.

Definition at line 1727 of file GlobalOpt.cpp.

References ColdCCRelFreq, llvm::BlockFrequencyInfo::getBlockFreq(), llvm::CallBase::getCaller(), llvm::Function::getEntryBlock(), and llvm::Instruction::getParent().

Referenced by hasOnlyColdCalls(), and isValidCandidateForColdCC().

◆ isLeakCheckerRoot()

static bool isLeakCheckerRoot ( GlobalVariable GV)
static

◆ isPointerValueDeadOnEntryToFunction()

static bool isPointerValueDeadOnEntryToFunction ( const Function F,
GlobalValue GV,
function_ref< DominatorTree &(Function &)>  LookupDomTree 
)
static

◆ IsSafeComputationToRemove()

static bool IsSafeComputationToRemove ( Value V,
function_ref< TargetLibraryInfo &(Function &)>  GetTLI 
)
static

Given a value that is stored to a global but never read, determine whether it's safe to remove the store and the chain of computation that feeds the store.

Definition at line 160 of file GlobalOpt.cpp.

References GEP, I, and llvm::isAllocationFn().

Referenced by CleanupPointerRootUsers().

◆ isValidCandidateForColdCC()

static bool isValidCandidateForColdCC ( Function F,
function_ref< BlockFrequencyInfo &(Function &)>  GetBFI,
const std::vector< Function * > &  AllCallsCold 
)
static

◆ mayHaveOtherReferences()

static bool mayHaveOtherReferences ( GlobalValue GV,
const LLVMUsed &  U 
)
static

Definition at line 2208 of file GlobalOpt.cpp.

References llvm::GlobalValue::hasLocalLinkage().

Referenced by hasUsesToReplace(), and OptimizeGlobalAliases().

◆ OptimizeAwayTrappingUsesOfLoads()

static bool OptimizeAwayTrappingUsesOfLoads ( GlobalVariable GV,
Constant LV,
const DataLayout DL,
function_ref< TargetLibraryInfo &(Function &)>  GetTLI 
)
static

The specified global has only one non-null value stored into it.

If there are uses of the loaded value that would trap if the loaded value is dynamically null, then we know that they cannot be reachable with a null optimize away the load.

Definition at line 817 of file GlobalOpt.cpp.

References assert(), CleanupConstantGlobalUsers(), CleanupPointerRootUsers(), llvm::dbgs(), DL, llvm::GlobalVariable::eraseFromParent(), isLeakCheckerRoot(), LLVM_DEBUG, llvm::make_early_inc_range(), OptimizeAwayTrappingUsesOfValue(), llvm::Value::use_empty(), and llvm::Value::users().

Referenced by optimizeOnceStoredGlobal().

◆ OptimizeAwayTrappingUsesOfValue()

static bool OptimizeAwayTrappingUsesOfValue ( Value V,
Constant NewV 
)
static

◆ OptimizeEmptyGlobalCXXDtors()

static bool OptimizeEmptyGlobalCXXDtors ( Function CXAAtExitFn)
static

◆ OptimizeFunctions()

static bool OptimizeFunctions ( Module M,
function_ref< TargetLibraryInfo &(Function &)>  GetTLI,
function_ref< TargetTransformInfo &(Function &)>  GetTTI,
function_ref< BlockFrequencyInfo &(Function &)>  GetBFI,
function_ref< DominatorTree &(Function &)>  LookupDomTree,
SmallPtrSetImpl< const Comdat * > &  NotDiscardableComdats,
function_ref< void(Function &F)>  ChangedCFGCallback,
function_ref< void(Function &F)>  DeleteFnCallback 
)
static

◆ OptimizeGlobalAddressOfAllocation()

static GlobalVariable * OptimizeGlobalAddressOfAllocation ( GlobalVariable GV,
CallInst CI,
uint64_t  AllocSize,
Constant InitVal,
const DataLayout DL,
TargetLibraryInfo TLI 
)
static

◆ OptimizeGlobalAliases()

static bool OptimizeGlobalAliases ( Module M,
SmallPtrSetImpl< const Comdat * > &  NotDiscardableComdats 
)
static

◆ optimizeGlobalsInModule()

static bool optimizeGlobalsInModule ( Module M,
const DataLayout DL,
function_ref< TargetLibraryInfo &(Function &)>  GetTLI,
function_ref< TargetTransformInfo &(Function &)>  GetTTI,
function_ref< BlockFrequencyInfo &(Function &)>  GetBFI,
function_ref< DominatorTree &(Function &)>  LookupDomTree,
function_ref< void(Function &F)>  ChangedCFGCallback,
function_ref< void(Function &F)>  DeleteFnCallback 
)
static

◆ OptimizeGlobalVars()

static bool OptimizeGlobalVars ( Module M,
function_ref< TargetTransformInfo &(Function &)>  GetTTI,
function_ref< TargetLibraryInfo &(Function &)>  GetTLI,
function_ref< DominatorTree &(Function &)>  LookupDomTree,
SmallPtrSetImpl< const Comdat * > &  NotDiscardableComdats 
)
static

◆ optimizeOnceStoredGlobal()

static bool optimizeOnceStoredGlobal ( GlobalVariable GV,
Value StoredOnceVal,
const DataLayout DL,
function_ref< TargetLibraryInfo &(Function &)>  GetTLI 
)
static

◆ OptimizeStaticIFuncs()

Find IFuncs that have resolvers that always point at the same statically known and replace their callers with a direct static call bool OptimizeStaticIFuncs ( Module M)
static

Definition at line 2439 of file GlobalOpt.cpp.

References hasSideeffectFreeStaticResolution().

Referenced by optimizeGlobalsInModule().

◆ processGlobal()

static bool processGlobal ( GlobalValue GV,
function_ref< TargetTransformInfo &(Function &)>  GetTTI,
function_ref< TargetLibraryInfo &(Function &)>  GetTLI,
function_ref< DominatorTree &(Function &)>  LookupDomTree 
)
static

◆ processInternalGlobal()

static bool processInternalGlobal ( GlobalVariable GV,
const GlobalStatus GS,
function_ref< TargetTransformInfo &(Function &)>  GetTTI,
function_ref< TargetLibraryInfo &(Function &)>  GetTLI,
function_ref< DominatorTree &(Function &)>  LookupDomTree 
)
static

◆ RemoveAttribute()

static void RemoveAttribute ( Function F,
Attribute::AttrKind  A 
)
static

◆ RemovePreallocated()

static void RemovePreallocated ( Function F)
static

◆ setUsedInitializer()

static void setUsedInitializer ( GlobalVariable V,
const SmallPtrSetImpl< GlobalValue * > &  Init 
)
static

◆ SRAGlobal()

static GlobalVariable * SRAGlobal ( GlobalVariable GV,
const DataLayout DL 
)
static

Perform scalar replacement of aggregates on the specified global variable.

This opens the door for other optimizations by exposing the behavior of the program in a more fine-grained way. We have determined that this transformation is safe already. We return the first global variable we insert so that the caller can reprocess it.

Definition at line 510 of file GlobalOpt.cpp.

References _, assert(), llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT >::begin(), collectSRATypes(), llvm::commonAlignment(), llvm::GlobalVariable::copyAttributesFrom(), llvm::count_if(), llvm::dbgs(), DL, llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT >::empty(), llvm::SmallVectorBase< Size_T >::empty(), llvm::GlobalVariable::eraseFromParent(), llvm::GlobalValue::getAddressSpace(), llvm::GlobalObject::getAlign(), llvm::getLoadStorePointerOperand(), llvm::getLoadStoreType(), llvm::Value::getName(), llvm::getOrEnforceKnownAlignment(), llvm::GlobalValue::getParent(), llvm::GlobalValue::getThreadLocalMode(), llvm::GlobalValue::getValueType(), llvm::GlobalValue::hasLocalLinkage(), llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT >::insert(), llvm::SmallPtrSetImpl< PtrType >::insert(), llvm::isSafeToDestroyConstant(), LLVM_DEBUG, llvm::Offset, llvm::SmallVectorImpl< T >::pop_back_val(), Ptr, llvm::SmallVectorTemplateBase< T, bool >::push_back(), llvm::RecursivelyDeleteTriviallyDeadInstructions(), llvm::Constant::removeDeadConstantUsers(), llvm::GlobalObject::setAlignment(), llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT >::size(), llvm::sort(), and transferSRADebugInfo().

Referenced by processInternalGlobal().

◆ STATISTIC() [1/18]

STATISTIC ( NumAliasesRemoved  ,
"Number of global aliases eliminated"   
)

◆ STATISTIC() [2/18]

STATISTIC ( NumAliasesResolved  ,
"Number of global aliases resolved"   
)

◆ STATISTIC() [3/18]

STATISTIC ( NumColdCC  ,
"Number of functions marked coldcc"   
)

◆ STATISTIC() [4/18]

STATISTIC ( NumCtorsEvaluated  ,
"Number of static ctors evaluated"   
)

◆ STATISTIC() [5/18]

STATISTIC ( NumCXXDtorsRemoved  ,
"Number of global C++ destructors removed"   
)

◆ STATISTIC() [6/18]

STATISTIC ( NumDeleted  ,
"Number of globals deleted"   
)

◆ STATISTIC() [7/18]

STATISTIC ( NumFastCallFns  ,
"Number of functions converted to fastcc"   
)

◆ STATISTIC() [8/18]

STATISTIC ( NumGlobUses  ,
"Number of global uses devirtualized"   
)

◆ STATISTIC() [9/18]

STATISTIC ( NumIFuncsDeleted  ,
"Number of IFuncs removed"   
)

◆ STATISTIC() [10/18]

STATISTIC ( NumIFuncsResolved  ,
"Number of statically resolved IFuncs"   
)

◆ STATISTIC() [11/18]

STATISTIC ( NumInternalFunc  ,
"Number of internal functions"   
)

◆ STATISTIC() [12/18]

STATISTIC ( NumLocalized  ,
"Number of globals localized"   
)

◆ STATISTIC() [13/18]

STATISTIC ( NumMarked  ,
"Number of globals marked constant"   
)

◆ STATISTIC() [14/18]

STATISTIC ( NumNestRemoved  ,
"Number of nest attributes removed"   
)

◆ STATISTIC() [15/18]

STATISTIC ( NumShrunkToBool  ,
"Number of global vars shrunk to booleans"   
)

◆ STATISTIC() [16/18]

STATISTIC ( NumSRA  ,
"Number of aggregate globals broken into scalars"   
)

◆ STATISTIC() [17/18]

STATISTIC ( NumSubstitute  ,
"Number of globals with initializers stored into them"   
)

◆ STATISTIC() [18/18]

STATISTIC ( NumUnnamed  ,
"Number of globals marked unnamed_addr"   
)

◆ StripAttr()

static AttributeList StripAttr ( LLVMContext C,
AttributeList  Attrs,
Attribute::AttrKind  A 
)
static

Definition at line 1662 of file GlobalOpt.cpp.

References A, and llvm::CallingConv::C.

Referenced by RemoveAttribute().

◆ transferSRADebugInfo()

static void transferSRADebugInfo ( GlobalVariable GV,
GlobalVariable NGV,
uint64_t  FragmentOffsetInBits,
uint64_t  FragmentSizeInBits,
uint64_t  VarSize 
)
static

◆ tryToOptimizeStoreOfAllocationToGlobal()

static bool tryToOptimizeStoreOfAllocationToGlobal ( GlobalVariable GV,
CallInst CI,
const DataLayout DL,
TargetLibraryInfo TLI 
)
static

If we have a global that is only initialized with a fixed size allocation try to transform the program to use global memory instead of heap allocated memory.

This eliminates dynamic allocation, avoids an indirection accessing the data, and exposes the resultant global to further GlobalOpt.

Definition at line 1065 of file GlobalOpt.cpp.

References allUsesOfLoadedValueWillTrapIfNull(), DL, llvm::Function::getContext(), llvm::Instruction::getFunction(), llvm::getInitialValueOfAllocation(), llvm::Type::getInt8Ty(), llvm::getObjectSize(), llvm::isRemovableAlloc(), OptimizeGlobalAddressOfAllocation(), and valueIsOnlyUsedLocallyOrStoredToOneGlobal().

Referenced by optimizeOnceStoredGlobal().

◆ TryToShrinkGlobalToBoolean()

static bool TryToShrinkGlobalToBoolean ( GlobalVariable GV,
Constant OtherVal 
)
static

At this point, we have learned that the only two values ever stored into GV are its initializer and OtherVal.

See if we can shrink the global into a boolean and select between the two values whenever it is used. This exposes the values to other scalar optimizations.

Definition at line 1148 of file GlobalOpt.cpp.

References llvm::GlobalVariable::addDebugInfo(), assert(), llvm::GlobalVariable::copyAttributesFrom(), llvm::SelectInst::Create(), llvm::dbgs(), DL, E, llvm::GlobalVariable::eraseFromParent(), llvm::Instruction::eraseFromParent(), llvm::ConstantInt::get(), llvm::PointerType::getAddressSpace(), llvm::Value::getContext(), llvm::Module::getDataLayout(), llvm::GlobalVariable::getDebugInfo(), llvm::Instruction::getDebugLoc(), llvm::ConstantInt::getFalse(), llvm::GlobalVariable::getInitializer(), llvm::Type::getInt1Ty(), llvm::ilist_node_impl< OptionsT >::getIterator(), llvm::getLoadStoreType(), llvm::Value::getName(), llvm::User::getOperand(), llvm::LoadInst::getOrdering(), llvm::GlobalValue::getParent(), llvm::LoadInst::getSyncScopeID(), llvm::GlobalValue::getThreadLocalMode(), llvm::GlobalValue::getType(), llvm::Value::getType(), llvm::GlobalValue::getValueType(), llvm::Module::insertGlobalVariable(), llvm::GlobalValue::InternalLinkage, llvm::Type::isFloatingPointTy(), llvm::Constant::isNullValue(), llvm::Type::isPointerTy(), llvm::Type::isVectorTy(), LLVM_DEBUG, llvm::DIExpression::prependOpcodes(), llvm::Value::replaceAllUsesWith(), llvm::Instruction::setDebugLoc(), llvm::Value::takeName(), llvm::Value::use_empty(), llvm::Value::user_back(), and llvm::Value::users().

Referenced by processInternalGlobal().

◆ valueIsOnlyUsedLocallyOrStoredToOneGlobal()

static bool valueIsOnlyUsedLocallyOrStoredToOneGlobal ( const CallInst CI,
const GlobalVariable GV 
)
static

Scan the use-list of GV checking to make sure that there are no complex uses of GV.

We permit simple things like dereferencing the pointer, but not storing through the address, unless it is to the specified global.

Definition at line 1021 of file GlobalOpt.cpp.

References llvm::SmallVectorBase< Size_T >::empty(), llvm::SmallPtrSetImpl< PtrType >::insert(), llvm::SmallVectorImpl< T >::pop_back_val(), and llvm::SmallVectorTemplateBase< T, bool >::push_back().

Referenced by tryToOptimizeStoreOfAllocationToGlobal().

Variable Documentation

◆ callee

Find IFuncs that have resolvers that always point at the same statically known callee

Definition at line 2438 of file GlobalOpt.cpp.

◆ ColdCCRelFreq

cl::opt< int > ColdCCRelFreq("coldcc-rel-freq", cl::Hidden, cl::init(2), cl::desc( "Maximum block frequency, expressed as a percentage of caller's " "entry frequency, for a call site to be considered cold for enabling" "coldcc")) ( "coldcc-rel-freq"  ,
cl::Hidden  ,
cl::init(2)  ,
cl::desc( "Maximum block frequency, expressed as a percentage of caller's " "entry frequency, for a call site to be considered cold for enabling" "coldcc")   
)
static

Referenced by isColdCallSite().

◆ EnableColdCCStressTest

cl::opt< bool > EnableColdCCStressTest("enable-coldcc-stress-test", cl::desc("Enable stress test of coldcc by adding " "calling conv to all internal functions."), cl::init(false), cl::Hidden) ( "enable-coldcc-stress-test"  ,
cl::desc("Enable stress test of coldcc by adding " "calling conv to all internal functions.")  ,
cl::init(false)  ,
cl::Hidden   
)
static

Referenced by OptimizeFunctions().