LLVM: lib/Transforms/IPO/GlobalOpt.cpp File Reference (original) (raw)

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"
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 (NumAtExitRemoved, "Number of atexit handlers 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?
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 GlobalVariable * SRAGlobal (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 GlobalVariable * OptimizeGlobalAddressOfAllocation (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 Function * FindAtExitLibFunc (Module &M, function_ref< TargetLibraryInfo &(Function &)> GetTLI, LibFunc Func)
static bool IsEmptyAtExitFunction (const Function &Fn)
Returns whether the given function is an empty C++ destructor or atexit handler and can therefore be eliminated.
static bool OptimizeEmptyGlobalAtExitDtors (Function *CXAAtExitFn, bool isCXX)
static Function * hasSideeffectFreeStaticResolution (GlobalIFunc &IF)
static bool OptimizeStaticIFuncs (Module &M)
Find IFuncs that have resolvers that always point at the same statically known callee, and replace their callers with a direct call.
static bool DeleteDeadIFuncs (Module &M, SmallPtrSetImpl< const Comdat * > &NotDiscardableComdats)
static bool collectVersions (Value *V, SmallVectorImpl< Function * > &Versions, function_ref< TargetTransformInfo &(Function &)> GetTTI)
static bool OptimizeNonTrivialIFuncs (Module &M, function_ref< TargetTransformInfo &(Function &)> GetTTI)
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< bool > OptimizeNonFMVCallers ("optimize-non-fmv-callers", cl::desc("Statically resolve calls to versioned " "functions from non-versioned callers."), cl::init(true), cl::Hidden)
static cl::opt< unsigned > MaxIFuncVersions ("max-ifunc-versions", cl::Hidden, cl::init(5), cl::desc("Maximum number of caller/callee versions that is allowed for " "using the expensive (cubic) static resolution algorithm."))
static 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)
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"))

DEBUG_TYPE

#define DEBUG_TYPE "globalopt"

ChangeableCCCacheTy

allUsesOfLoadAndStores()

allUsesOfLoadedValueWillTrapIfNull()

AllUsesOfValueWillTrapIfNull()

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 664 of file GlobalOpt.cpp.

References AllUsesOfValueWillTrapIfNull(), assert(), llvm::cast(), llvm::dyn_cast(), I, II, llvm::SmallPtrSetImpl< PtrType >::insert(), llvm::isa(), llvm::CmpInst::isSigned(), llvm::NullPointerIsDefined(), and llvm::Value::users().

Referenced by allUsesOfLoadedValueWillTrapIfNull(), and AllUsesOfValueWillTrapIfNull().

ChangeCalleesToFastCall()

void ChangeCalleesToFastCall ( Function * F) static

changeCallSitesToColdCC()

void changeCallSitesToColdCC ( Function * F) static

CleanupConstantGlobalUsers()

We just marked GV constant.

Loop over all users of the global, cleaning up the obvious ones. This is largely just a quick scan over the use list to clean up the easy and obvious cruft. This returns true if it made a change.

Definition at line 281 of file GlobalOpt.cpp.

References llvm::append_range(), Changed, llvm::ConstantFoldLoadFromConst(), llvm::ConstantFoldLoadFromUniformValue(), DL, llvm::dyn_cast(), llvm::SmallVectorTemplateCommon< T, typename >::empty(), GEP, llvm::GlobalVariable::getInitializer(), llvm::Value::getType(), llvm::getUnderlyingObject(), I, II, llvm::SmallPtrSetImpl< PtrType >::insert(), MI, llvm::Offset, llvm::SmallVectorImpl< T >::pop_back_val(), llvm::SmallVectorTemplateBase< T, bool >::push_back(), llvm::RecursivelyDeleteTriviallyDeadInstructionsPermissive(), llvm::Constant::removeDeadConstantUsers(), llvm::Value::stripAndAccumulateConstantOffsets(), and llvm::Value::users().

Referenced by OptimizeAwayTrappingUsesOfLoads(), and processInternalGlobal().

CleanupPointerRootUsers()

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 203 of file GlobalOpt.cpp.

References llvm::append_range(), Changed, llvm::dyn_cast(), llvm::SmallVectorTemplateCommon< T, typename >::empty(), I, llvm::isa(), 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()

Look at all uses of the global and determine which (offset, type) pairs it can be split into.

Definition at line 360 of file GlobalOpt.cpp.

References llvm::CallingConv::C, llvm::ConstantFoldLoadFromConst(), llvm::dbgs(), DL, llvm::dyn_cast(), llvm::SmallVectorTemplateCommon< T, typename >::empty(), GEP, llvm::GlobalVariable::getInitializer(), llvm::getLoadStorePointerOperand(), llvm::getLoadStoreType(), llvm::SmallPtrSetImpl< PtrType >::insert(), llvm::isa(), llvm::isSafeToDestroyConstant(), LLVM_DEBUG, llvm::Offset, llvm::SmallVectorImpl< T >::pop_back_val(), llvm::SmallVectorTemplateBase< T, bool >::push_back(), llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT >::try_emplace(), and llvm::Value::uses().

Referenced by SRAGlobal().

collectVersions()

compareNames()

ConstantPropUsersOf()

DeleteDeadIFuncs()

deleteIfDead()

Definition at line 1331 of file GlobalOpt.cpp.

References llvm::CallingConv::C, llvm::SmallPtrSetImpl< PtrType >::count(), llvm::dbgs(), llvm::dyn_cast(), llvm::GlobalValue::eraseFromParent(), F, llvm::GlobalValue::getComdat(), llvm::GlobalValue::hasLocalLinkage(), llvm::GlobalValue::isDeclaration(), llvm::GlobalValue::isDiscardableIfUnused(), LLVM_DEBUG, llvm::Constant::removeDeadConstantUsers(), llvm::ReplaceableMetadataImpl::SalvageDebugInfo(), and llvm::Value::use_empty().

Referenced by DeleteDeadIFuncs(), OptimizeFunctions(), OptimizeGlobalAliases(), and OptimizeGlobalVars().

EvaluateStaticConstructor()

FindAtExitLibFunc()

forwardStoredOnceStore()

Definition at line 1431 of file GlobalOpt.cpp.

References llvm::dyn_cast(), llvm::SmallVectorTemplateCommon< T, typename >::empty(), F, llvm::Instruction::getFunction(), llvm::Value::getType(), llvm::StoreInst::getValueOperand(), llvm::isa(), llvm::SmallVectorTemplateBase< T, bool >::push_back(), and llvm::Value::users().

Referenced by processInternalGlobal().

hasChangeableCC()

hasChangeableCCImpl()

hasInvokeCallers()

hasMustTailCallers()

hasOnlyColdCalls()

hasSideeffectFreeStaticResolution()

hasUseOtherThanLLVMUsed()

hasUsesToReplace()

isColdCallSite()

IsEmptyAtExitFunction()

isLeakCheckerRoot()

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.

Definition at line 122 of file GlobalOpt.cpp.

References llvm::Type::ArrayTyID, llvm::cast(), llvm::StructType::elements(), llvm::Type::FixedVectorTyID, llvm::GlobalValue::getValueType(), llvm::GlobalValue::hasPrivateLinkage(), llvm::isa(), llvm::StructType::isOpaque(), llvm::isPointerTy(), llvm::Type::PointerTyID, llvm::SmallVectorTemplateBase< T, bool >::push_back(), llvm::Type::ScalableVectorTyID, and llvm::Type::StructTyID.

Referenced by OptimizeAwayTrappingUsesOfLoads(), and processInternalGlobal().

isPointerValueDeadOnEntryToFunction()

Definition at line 1362 of file GlobalOpt.cpp.

References assert(), DL, llvm::dyn_cast(), F, llvm::GlobalValue::getDataLayout(), llvm::Value::getType(), llvm::StoreInst::getValueOperand(), I, llvm::none_of(), llvm::SmallVectorTemplateBase< T, bool >::push_back(), llvm::SmallVectorTemplateCommon< T, typename >::size(), and llvm::Value::users().

Referenced by processInternalGlobal().

IsSafeComputationToRemove()

isValidCandidateForColdCC()

mayHaveOtherReferences()

OptimizeAwayTrappingUsesOfLoads()

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 841 of file GlobalOpt.cpp.

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

Referenced by optimizeOnceStoredGlobal().

OptimizeAwayTrappingUsesOfValue()

Definition at line 771 of file GlobalOpt.cpp.

References llvm::CallBase::arg_size(), llvm::CallingConv::C, llvm::cast(), Changed, llvm::dyn_cast(), E(), llvm::ConstantExpr::getAddrSpaceCast(), llvm::CallBase::getArgOperand(), llvm::CallBase::getCalledOperand(), llvm::ConstantExpr::getGetElementPtr(), llvm::Value::getType(), I, llvm::isa(), llvm::NullPointerIsDefined(), OptimizeAwayTrappingUsesOfValue(), llvm::SmallVectorTemplateBase< T, bool >::push_back(), llvm::SmallVectorImpl< T >::reserve(), llvm::CallBase::setArgOperand(), llvm::CallBase::setCalledOperand(), and llvm::SmallVectorTemplateCommon< T, typename >::size().

Referenced by OptimizeAwayTrappingUsesOfLoads(), and OptimizeAwayTrappingUsesOfValue().

OptimizeEmptyGlobalAtExitDtors()

Itanium C++ ABI p3.3.5:

After constructing a global (or local static) object, that will require destruction on exit, a termination function is registered as follows:

extern "C" int __cxa_atexit ( void (*f)(void *), void *p, void *d );

This registration, e.g. __cxa_atexit(f,p,d), is intended to cause the call f(p) when DSO d is unloaded, before all such termination calls registered before this one. It returns zero if registration is successful, nonzero on failure.

Definition at line 2395 of file GlobalOpt.cpp.

References Changed, llvm::dyn_cast(), llvm::Instruction::eraseFromParent(), llvm::CallBase::getArgOperand(), llvm::Constant::getNullValue(), llvm::Value::getType(), IsEmptyAtExitFunction(), llvm::make_early_inc_range(), llvm::Value::replaceAllUsesWith(), llvm::Value::stripPointerCasts(), and llvm::Value::users().

Referenced by optimizeGlobalsInModule().

OptimizeFunctions()

Definition at line 1930 of file GlobalOpt.cpp.

References ChangeCalleesToFastCall(), changeCallSitesToColdCC(), Changed, llvm::CallingConv::Cold, deleteIfDead(), EnableColdCCStressTest, llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT >::erase(), F, llvm::CallingConv::Fast, hasChangeableCC(), hasInvokeCallers(), hasMustTailCallers(), hasOnlyColdCalls(), llvm::GlobalValue::InternalLinkage, isValidCandidateForColdCC(), llvm::make_early_inc_range(), processGlobal(), RemoveAttribute(), RemovePreallocated(), and llvm::removeUnreachableBlocks().

Referenced by optimizeGlobalsInModule().

OptimizeGlobalAddressOfAllocation()

This function takes the specified global variable, and transforms the program as if it always contained the result of the specified malloc.

Because it is always the result of the specified malloc, there is no reason to actually DO the malloc. Instead, turn the malloc into a global, and any loads of GV as uses of the new global.

Definition at line 928 of file GlobalOpt.cpp.

References allUsesOfLoadAndStores(), llvm::cast(), ConstantPropUsersOf(), llvm::BinaryOperator::CreateNot(), DL, llvm::dyn_cast(), llvm::GlobalVariable::eraseFromParent(), llvm::Instruction::eraseFromParent(), llvm::errs(), llvm::ArrayType::get(), llvm::UndefValue::get(), llvm::GlobalValue::getAddressSpace(), llvm::ConstantInt::getBool(), llvm::Value::getContext(), llvm::Instruction::getDebugLoc(), llvm::ConstantInt::getFalse(), llvm::Type::getInt1Ty(), llvm::Type::getInt8Ty(), llvm::ilist_node_impl< OptionsT >::getIterator(), llvm::Value::getName(), llvm::ilist_node_with_parent< NodeTy, ParentTy, Options >::getNextNode(), llvm::LoadInst::getOrdering(), llvm::GlobalValue::getParent(), llvm::CmpInst::getPredicate(), llvm::LoadInst::getSyncScopeID(), llvm::GlobalValue::getThreadLocalMode(), llvm::ConstantInt::getTrue(), llvm::Use::getUser(), llvm::GlobalValue::getValueType(), llvm::CmpInst::ICMP_EQ, llvm::CmpInst::ICMP_NE, llvm::CmpInst::ICMP_UGE, llvm::CmpInst::ICMP_UGT, llvm::CmpInst::ICMP_ULE, llvm::CmpInst::ICMP_ULT, llvm::Module::insertGlobalVariable(), llvm::GlobalValue::InternalLinkage, llvm::isa(), LLVM_DEBUG, llvm_unreachable, llvm::Value::replaceAllUsesWith(), llvm::Use::set(), llvm::Value::use_begin(), llvm::Value::use_empty(), and llvm::Value::user_back().

Referenced by tryToOptimizeStoreOfAllocationToGlobal().

OptimizeGlobalAliases()

Definition at line 2269 of file GlobalOpt.cpp.

References Changed, deleteIfDead(), llvm::dyn_cast(), llvm::GlobalValue::getLinkage(), hasUsesToReplace(), llvm::GlobalValue::InternalLinkage, llvm::GlobalValue::isDSOLocal(), llvm::GlobalValue::isImplicitDSOLocal(), llvm::GlobalValue::isInterposableLinkage(), llvm::make_early_inc_range(), mayHaveOtherReferences(), and llvm::Constant::stripPointerCasts().

Referenced by optimizeGlobalsInModule().

optimizeGlobalsInModule()

Definition at line 2741 of file GlobalOpt.cpp.

References llvm::CallingConv::C, Changed, llvm::SmallPtrSetImplBase::clear(), DeleteDeadIFuncs(), DL, EvaluateStaticConstructor(), F, FindAtExitLibFunc(), llvm::GlobalValue::getComdat(), llvm::SmallPtrSetImpl< PtrType >::insert(), llvm::GlobalValue::isDiscardableIfUnused(), OptimizeEmptyGlobalAtExitDtors(), OptimizeFunctions(), OptimizeGlobalAliases(), llvm::optimizeGlobalCtorsList(), OptimizeGlobalVars(), OptimizeNonTrivialIFuncs(), OptimizeStaticIFuncs(), and llvm::Value::use_empty().

Referenced by llvm::GlobalOptPass::run().

OptimizeGlobalVars()

Definition at line 2050 of file GlobalOpt.cpp.

References llvm::CallingConv::C, Changed, llvm::ConstantFoldConstant(), deleteIfDead(), DL, llvm::dyn_cast(), llvm::GlobalValue::hasLocalLinkage(), llvm::Value::hasName(), llvm::GlobalValue::InternalLinkage, llvm::GlobalValue::isDeclaration(), llvm::make_early_inc_range(), processGlobal(), and llvm::GlobalValue::setLinkage().

Referenced by optimizeGlobalsInModule().

OptimizeNonTrivialIFuncs()

Definition at line 2543 of file GlobalOpt.cpp.

References llvm::all_of(), llvm::any_of(), Changed, collectVersions(), llvm::dbgs(), llvm::dyn_cast(), llvm::dyn_cast_or_null(), E(), llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT >::empty(), llvm::SmallVectorTemplateCommon< T, typename >::empty(), llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT >::end(), llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT >::find(), llvm::BasicBlock::getTerminator(), I, llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT >::insert(), llvm::detail::DenseSetImpl< ValueT, MapTy, ValueInfoT >::insert(), llvm::APInt::isSubsetOf(), LHS, LLVM_DEBUG, MaxIFuncVersions, OptimizeNonFMVCallers, llvm::SmallVectorTemplateBase< T, bool >::push_back(), RHS, llvm::CallBase::setCalledOperand(), llvm::ArrayRef< T >::size(), llvm::sort(), llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT >::try_emplace(), and llvm::Value::users().

Referenced by optimizeGlobalsInModule().

optimizeOnceStoredGlobal()

Definition at line 1138 of file GlobalOpt.cpp.

References DL, llvm::dyn_cast(), llvm::GlobalVariable::getInitializer(), llvm::Type::getPointerAddressSpace(), llvm::Value::getType(), llvm::isAllocationFn(), llvm::Constant::isNullValue(), llvm::Type::isPointerTy(), llvm::NullPointerIsDefined(), OptimizeAwayTrappingUsesOfLoads(), and tryToOptimizeStoreOfAllocationToGlobal().

Referenced by processInternalGlobal().

OptimizeStaticIFuncs()

processGlobal()

Analyze the specified global variable and optimize it if possible.

If we make a change, return true.

Definition at line 1646 of file GlobalOpt.cpp.

References llvm::GlobalStatus::analyzeGlobal(), Changed, llvm::dyn_cast(), llvm::Value::getName(), llvm::GlobalValue::getUnnamedAddr(), llvm::GlobalValue::Global, llvm::GlobalValue::hasGlobalUnnamedAddr(), llvm::GlobalValue::hasLocalLinkage(), llvm::GlobalValue::Local, processInternalGlobal(), llvm::GlobalValue::setUnnamedAddr(), and llvm::StringRef::starts_with().

Referenced by OptimizeFunctions(), and OptimizeGlobalVars().

processInternalGlobal()

Analyze the specified global variable and optimize it if possible.

If we make a change, return true.

Definition at line 1467 of file GlobalOpt.cpp.

References assert(), Changed, CleanupConstantGlobalUsers(), CleanupPointerRootUsers(), llvm::dbgs(), DL, llvm::dyn_cast(), llvm::GlobalVariable::eraseFromParent(), forwardStoredOnceStore(), llvm::GlobalValue::getAddressSpace(), llvm::PointerType::getAddressSpace(), llvm::DebugLoc::getCompilerGenerated(), llvm::GlobalValue::getDataLayout(), llvm::GlobalVariable::getInitializer(), llvm::GlobalValue::getLinkage(), llvm::Value::getName(), llvm::GlobalValue::getParent(), llvm::GlobalValue::getThreadLocalMode(), llvm::GlobalValue::getType(), llvm::Value::getType(), llvm::GlobalValue::getValueType(), llvm::GlobalStatus::InitializerStored, llvm::isa(), llvm::GlobalVariable::isConstant(), llvm::GlobalVariable::isExternallyInitialized(), isLeakCheckerRoot(), isPointerValueDeadOnEntryToFunction(), llvm::Type::isSingleValueType(), LLVM_DEBUG, llvm::NotAtomic, optimizeOnceStoredGlobal(), llvm::Value::replaceAllUsesWith(), llvm::GlobalVariable::setConstant(), llvm::Instruction::setDebugLoc(), llvm::GlobalVariable::setInitializer(), SRAGlobal(), llvm::GlobalStatus::StoredOnce, TryToShrinkGlobalToBoolean(), and llvm::Value::use_empty().

Referenced by processGlobal().

RemoveAttribute()

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

RemovePreallocated()

void RemovePreallocated ( Function * F) static

Definition at line 1848 of file GlobalOpt.cpp.

References assert(), llvm::SmallVectorTemplateCommon< T, typename >::begin(), llvm::cast(), llvm::CallBase::Create(), llvm::dyn_cast(), llvm::SmallVectorTemplateCommon< T, typename >::end(), llvm::SmallVectorImpl< T >::erase(), llvm::Instruction::eraseFromParent(), F, llvm::CallBase::getArgOperand(), llvm::ilist_node_impl< OptionsT >::getIterator(), llvm::ilist_node_with_parent< NodeTy, ParentTy, Options >::getNextNode(), llvm::CallBase::getOperandBundlesAsDefs(), llvm::isa(), llvm::CallBase::isMustTailCall(), RemoveAttribute(), llvm::Value::replaceAllUsesWith(), llvm::Value::takeName(), and llvm::Value::users().

Referenced by OptimizeFunctions().

setUsedInitializer()

Definition at line 2119 of file GlobalOpt.cpp.

References llvm::GlobalValue::AppendingLinkage, llvm::array_pod_sort(), llvm::SmallVectorTemplateCommon< T, typename >::begin(), llvm::cast(), compareNames(), llvm::SmallVectorTemplateCommon< T, typename >::end(), llvm::ArrayType::get(), llvm::ConstantArray::get(), llvm::PointerType::get(), llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(), llvm::SmallVectorTemplateBase< T, bool >::push_back(), and llvm::SmallVectorTemplateCommon< T, typename >::size().

SRAGlobal()

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 529 of file GlobalOpt.cpp.

References _, assert(), llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT >::begin(), llvm::cast(), collectSRATypes(), llvm::commonAlignment(), llvm::GlobalVariable::copyAttributesFrom(), llvm::count_if(), llvm::dbgs(), DL, llvm::dyn_cast(), llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT >::empty(), llvm::SmallVectorTemplateCommon< T, typename >::empty(), llvm::GlobalVariable::eraseFromParent(), llvm::GlobalValue::getAddressSpace(), llvm::GlobalVariable::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::GlobalValue::InternalLinkage, llvm::isa(), llvm::isSafeToDestroyConstant(), LLVM_DEBUG, llvm::Offset, llvm::SmallVectorImpl< T >::pop_back_val(), llvm::SmallVectorTemplateBase< T, bool >::push_back(), llvm::RecursivelyDeleteTriviallyDeadInstructions(), llvm::Constant::removeDeadConstantUsers(), llvm::GlobalVariable::setAlignment(), llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT >::size(), llvm::sort(), transferSRADebugInfo(), and llvm::Value::users().

Referenced by processInternalGlobal().

STATISTIC() [1/19]

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

STATISTIC() [2/19]

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

STATISTIC() [3/19]

STATISTIC ( NumAtExitRemoved ,
"Number of atexit handlers removed" )

STATISTIC() [4/19]

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

STATISTIC() [5/19]

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

STATISTIC() [6/19]

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

STATISTIC() [7/19]

STATISTIC ( NumDeleted ,
"Number of globals deleted" )

STATISTIC() [8/19]

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

STATISTIC() [9/19]

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

STATISTIC() [10/19]

STATISTIC ( NumIFuncsDeleted ,
"Number of IFuncs removed" )

STATISTIC() [11/19]

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

STATISTIC() [12/19]

STATISTIC ( NumInternalFunc ,
"Number of internal functions" )

STATISTIC() [13/19]

STATISTIC ( NumLocalized ,
"Number of globals localized" )

STATISTIC() [14/19]

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

STATISTIC() [15/19]

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

STATISTIC() [16/19]

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

STATISTIC() [17/19]

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

STATISTIC() [18/19]

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

STATISTIC() [19/19]

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

StripAttr()

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

transferSRADebugInfo()

tryToOptimizeStoreOfAllocationToGlobal()

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 1091 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()

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 1171 of file GlobalOpt.cpp.

References AbstractManglingParser< Derived, Alloc >::Ops, llvm::GlobalVariable::addDebugInfo(), assert(), llvm::cast(), llvm::GlobalVariable::copyAttributesFrom(), llvm::SelectInst::Create(), llvm::dbgs(), DL, llvm::dyn_cast(), E(), llvm::GlobalVariable::eraseFromParent(), llvm::Instruction::eraseFromParent(), llvm::MDNode::get(), llvm::PointerType::getAddressSpace(), llvm::Value::getContext(), llvm::GlobalValue::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::isa(), 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()

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 1052 of file GlobalOpt.cpp.

References llvm::dyn_cast(), llvm::SmallVectorTemplateCommon< T, typename >::empty(), llvm::SmallPtrSetImpl< PtrType >::insert(), llvm::isa(), llvm::SmallVectorImpl< T >::pop_back_val(), llvm::SmallVectorTemplateBase< T, bool >::push_back(), and llvm::Value::uses().

Referenced by tryToOptimizeStoreOfAllocationToGlobal().

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

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

MaxIFuncVersions

cl::opt< unsigned > MaxIFuncVersions("max-ifunc-versions", cl::Hidden, cl::init(5), cl::desc("Maximum number of caller/callee versions that is allowed for " "using the expensive (cubic) static resolution algorithm.")) ( "max-ifunc-versions" , cl::Hidden , cl::init(5) , cl::desc("Maximum number of caller/callee versions that is allowed for " "using the expensive (cubic) static resolution algorithm.") ) static

OptimizeNonFMVCallers

cl::opt< bool > OptimizeNonFMVCallers("optimize-non-fmv-callers", cl::desc("Statically resolve calls to versioned " "functions from non-versioned callers."), cl::init(true), cl::Hidden) ( "optimize-non-fmv-callers" , cl::desc("Statically resolve calls to versioned " "functions from non-versioned callers.") , cl::init(true) , cl::Hidden ) static