Path: blob/main/contrib/llvm-project/llvm/lib/Transforms/IPO/GlobalOpt.cpp
35269 views
//===- GlobalOpt.cpp - Optimize Global Variables --------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This pass transforms simple global variables that never have their address9// taken. If obviously true, it marks read/write globals as constant, deletes10// variables only stored to, etc.11//12//===----------------------------------------------------------------------===//1314#include "llvm/Transforms/IPO/GlobalOpt.h"15#include "llvm/ADT/DenseMap.h"16#include "llvm/ADT/STLExtras.h"17#include "llvm/ADT/SmallPtrSet.h"18#include "llvm/ADT/SmallVector.h"19#include "llvm/ADT/Statistic.h"20#include "llvm/ADT/Twine.h"21#include "llvm/ADT/iterator_range.h"22#include "llvm/Analysis/BlockFrequencyInfo.h"23#include "llvm/Analysis/ConstantFolding.h"24#include "llvm/Analysis/MemoryBuiltins.h"25#include "llvm/Analysis/TargetLibraryInfo.h"26#include "llvm/Analysis/TargetTransformInfo.h"27#include "llvm/Analysis/ValueTracking.h"28#include "llvm/BinaryFormat/Dwarf.h"29#include "llvm/IR/Attributes.h"30#include "llvm/IR/BasicBlock.h"31#include "llvm/IR/CallingConv.h"32#include "llvm/IR/Constant.h"33#include "llvm/IR/Constants.h"34#include "llvm/IR/DataLayout.h"35#include "llvm/IR/DebugInfoMetadata.h"36#include "llvm/IR/DerivedTypes.h"37#include "llvm/IR/Dominators.h"38#include "llvm/IR/Function.h"39#include "llvm/IR/GlobalAlias.h"40#include "llvm/IR/GlobalValue.h"41#include "llvm/IR/GlobalVariable.h"42#include "llvm/IR/IRBuilder.h"43#include "llvm/IR/InstrTypes.h"44#include "llvm/IR/Instruction.h"45#include "llvm/IR/Instructions.h"46#include "llvm/IR/IntrinsicInst.h"47#include "llvm/IR/Module.h"48#include "llvm/IR/Operator.h"49#include "llvm/IR/Type.h"50#include "llvm/IR/Use.h"51#include "llvm/IR/User.h"52#include "llvm/IR/Value.h"53#include "llvm/IR/ValueHandle.h"54#include "llvm/Support/AtomicOrdering.h"55#include "llvm/Support/Casting.h"56#include "llvm/Support/CommandLine.h"57#include "llvm/Support/Debug.h"58#include "llvm/Support/ErrorHandling.h"59#include "llvm/Support/raw_ostream.h"60#include "llvm/Transforms/IPO.h"61#include "llvm/Transforms/Utils/CtorUtils.h"62#include "llvm/Transforms/Utils/Evaluator.h"63#include "llvm/Transforms/Utils/GlobalStatus.h"64#include "llvm/Transforms/Utils/Local.h"65#include <cassert>66#include <cstdint>67#include <optional>68#include <utility>69#include <vector>7071using namespace llvm;7273#define DEBUG_TYPE "globalopt"7475STATISTIC(NumMarked , "Number of globals marked constant");76STATISTIC(NumUnnamed , "Number of globals marked unnamed_addr");77STATISTIC(NumSRA , "Number of aggregate globals broken into scalars");78STATISTIC(NumSubstitute,"Number of globals with initializers stored into them");79STATISTIC(NumDeleted , "Number of globals deleted");80STATISTIC(NumGlobUses , "Number of global uses devirtualized");81STATISTIC(NumLocalized , "Number of globals localized");82STATISTIC(NumShrunkToBool , "Number of global vars shrunk to booleans");83STATISTIC(NumFastCallFns , "Number of functions converted to fastcc");84STATISTIC(NumCtorsEvaluated, "Number of static ctors evaluated");85STATISTIC(NumNestRemoved , "Number of nest attributes removed");86STATISTIC(NumAliasesResolved, "Number of global aliases resolved");87STATISTIC(NumAliasesRemoved, "Number of global aliases eliminated");88STATISTIC(NumCXXDtorsRemoved, "Number of global C++ destructors removed");89STATISTIC(NumAtExitRemoved, "Number of atexit handlers removed");90STATISTIC(NumInternalFunc, "Number of internal functions");91STATISTIC(NumColdCC, "Number of functions marked coldcc");92STATISTIC(NumIFuncsResolved, "Number of statically resolved IFuncs");93STATISTIC(NumIFuncsDeleted, "Number of IFuncs removed");9495static cl::opt<bool>96EnableColdCCStressTest("enable-coldcc-stress-test",97cl::desc("Enable stress test of coldcc by adding "98"calling conv to all internal functions."),99cl::init(false), cl::Hidden);100101static cl::opt<int> ColdCCRelFreq(102"coldcc-rel-freq", cl::Hidden, cl::init(2),103cl::desc(104"Maximum block frequency, expressed as a percentage of caller's "105"entry frequency, for a call site to be considered cold for enabling"106"coldcc"));107108/// Is this global variable possibly used by a leak checker as a root? If so,109/// we might not really want to eliminate the stores to it.110static bool isLeakCheckerRoot(GlobalVariable *GV) {111// A global variable is a root if it is a pointer, or could plausibly contain112// a pointer. There are two challenges; one is that we could have a struct113// the has an inner member which is a pointer. We recurse through the type to114// detect these (up to a point). The other is that we may actually be a union115// of a pointer and another type, and so our LLVM type is an integer which116// gets converted into a pointer, or our type is an [i8 x #] with a pointer117// potentially contained here.118119if (GV->hasPrivateLinkage())120return false;121122SmallVector<Type *, 4> Types;123Types.push_back(GV->getValueType());124125unsigned Limit = 20;126do {127Type *Ty = Types.pop_back_val();128switch (Ty->getTypeID()) {129default: break;130case Type::PointerTyID:131return true;132case Type::FixedVectorTyID:133case Type::ScalableVectorTyID:134if (cast<VectorType>(Ty)->getElementType()->isPointerTy())135return true;136break;137case Type::ArrayTyID:138Types.push_back(cast<ArrayType>(Ty)->getElementType());139break;140case Type::StructTyID: {141StructType *STy = cast<StructType>(Ty);142if (STy->isOpaque()) return true;143for (Type *InnerTy : STy->elements()) {144if (isa<PointerType>(InnerTy)) return true;145if (isa<StructType>(InnerTy) || isa<ArrayType>(InnerTy) ||146isa<VectorType>(InnerTy))147Types.push_back(InnerTy);148}149break;150}151}152if (--Limit == 0) return true;153} while (!Types.empty());154return false;155}156157/// Given a value that is stored to a global but never read, determine whether158/// it's safe to remove the store and the chain of computation that feeds the159/// store.160static bool IsSafeComputationToRemove(161Value *V, function_ref<TargetLibraryInfo &(Function &)> GetTLI) {162do {163if (isa<Constant>(V))164return true;165if (!V->hasOneUse())166return false;167if (isa<LoadInst>(V) || isa<InvokeInst>(V) || isa<Argument>(V) ||168isa<GlobalValue>(V))169return false;170if (isAllocationFn(V, GetTLI))171return true;172173Instruction *I = cast<Instruction>(V);174if (I->mayHaveSideEffects())175return false;176if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I)) {177if (!GEP->hasAllConstantIndices())178return false;179} else if (I->getNumOperands() != 1) {180return false;181}182183V = I->getOperand(0);184} while (true);185}186187/// This GV is a pointer root. Loop over all users of the global and clean up188/// any that obviously don't assign the global a value that isn't dynamically189/// allocated.190static bool191CleanupPointerRootUsers(GlobalVariable *GV,192function_ref<TargetLibraryInfo &(Function &)> GetTLI) {193// A brief explanation of leak checkers. The goal is to find bugs where194// pointers are forgotten, causing an accumulating growth in memory195// usage over time. The common strategy for leak checkers is to explicitly196// allow the memory pointed to by globals at exit. This is popular because it197// also solves another problem where the main thread of a C++ program may shut198// down before other threads that are still expecting to use those globals. To199// handle that case, we expect the program may create a singleton and never200// destroy it.201202bool Changed = false;203204// If Dead[n].first is the only use of a malloc result, we can delete its205// chain of computation and the store to the global in Dead[n].second.206SmallVector<std::pair<Instruction *, Instruction *>, 32> Dead;207208SmallVector<User *> Worklist(GV->users());209// Constants can't be pointers to dynamically allocated memory.210while (!Worklist.empty()) {211User *U = Worklist.pop_back_val();212if (StoreInst *SI = dyn_cast<StoreInst>(U)) {213Value *V = SI->getValueOperand();214if (isa<Constant>(V)) {215Changed = true;216SI->eraseFromParent();217} else if (Instruction *I = dyn_cast<Instruction>(V)) {218if (I->hasOneUse())219Dead.push_back(std::make_pair(I, SI));220}221} else if (MemSetInst *MSI = dyn_cast<MemSetInst>(U)) {222if (isa<Constant>(MSI->getValue())) {223Changed = true;224MSI->eraseFromParent();225} else if (Instruction *I = dyn_cast<Instruction>(MSI->getValue())) {226if (I->hasOneUse())227Dead.push_back(std::make_pair(I, MSI));228}229} else if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(U)) {230GlobalVariable *MemSrc = dyn_cast<GlobalVariable>(MTI->getSource());231if (MemSrc && MemSrc->isConstant()) {232Changed = true;233MTI->eraseFromParent();234} else if (Instruction *I = dyn_cast<Instruction>(MTI->getSource())) {235if (I->hasOneUse())236Dead.push_back(std::make_pair(I, MTI));237}238} else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) {239if (isa<GEPOperator>(CE))240append_range(Worklist, CE->users());241}242}243244for (int i = 0, e = Dead.size(); i != e; ++i) {245if (IsSafeComputationToRemove(Dead[i].first, GetTLI)) {246Dead[i].second->eraseFromParent();247Instruction *I = Dead[i].first;248do {249if (isAllocationFn(I, GetTLI))250break;251Instruction *J = dyn_cast<Instruction>(I->getOperand(0));252if (!J)253break;254I->eraseFromParent();255I = J;256} while (true);257I->eraseFromParent();258Changed = true;259}260}261262GV->removeDeadConstantUsers();263return Changed;264}265266/// We just marked GV constant. Loop over all users of the global, cleaning up267/// the obvious ones. This is largely just a quick scan over the use list to268/// clean up the easy and obvious cruft. This returns true if it made a change.269static bool CleanupConstantGlobalUsers(GlobalVariable *GV,270const DataLayout &DL) {271Constant *Init = GV->getInitializer();272SmallVector<User *, 8> WorkList(GV->users());273SmallPtrSet<User *, 8> Visited;274bool Changed = false;275276SmallVector<WeakTrackingVH> MaybeDeadInsts;277auto EraseFromParent = [&](Instruction *I) {278for (Value *Op : I->operands())279if (auto *OpI = dyn_cast<Instruction>(Op))280MaybeDeadInsts.push_back(OpI);281I->eraseFromParent();282Changed = true;283};284while (!WorkList.empty()) {285User *U = WorkList.pop_back_val();286if (!Visited.insert(U).second)287continue;288289if (auto *BO = dyn_cast<BitCastOperator>(U))290append_range(WorkList, BO->users());291if (auto *ASC = dyn_cast<AddrSpaceCastOperator>(U))292append_range(WorkList, ASC->users());293else if (auto *GEP = dyn_cast<GEPOperator>(U))294append_range(WorkList, GEP->users());295else if (auto *LI = dyn_cast<LoadInst>(U)) {296// A load from a uniform value is always the same, regardless of any297// applied offset.298Type *Ty = LI->getType();299if (Constant *Res = ConstantFoldLoadFromUniformValue(Init, Ty, DL)) {300LI->replaceAllUsesWith(Res);301EraseFromParent(LI);302continue;303}304305Value *PtrOp = LI->getPointerOperand();306APInt Offset(DL.getIndexTypeSizeInBits(PtrOp->getType()), 0);307PtrOp = PtrOp->stripAndAccumulateConstantOffsets(308DL, Offset, /* AllowNonInbounds */ true);309if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(PtrOp)) {310if (II->getIntrinsicID() == Intrinsic::threadlocal_address)311PtrOp = II->getArgOperand(0);312}313if (PtrOp == GV) {314if (auto *Value = ConstantFoldLoadFromConst(Init, Ty, Offset, DL)) {315LI->replaceAllUsesWith(Value);316EraseFromParent(LI);317}318}319} else if (StoreInst *SI = dyn_cast<StoreInst>(U)) {320// Store must be unreachable or storing Init into the global.321EraseFromParent(SI);322} else if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(U)) { // memset/cpy/mv323if (getUnderlyingObject(MI->getRawDest()) == GV)324EraseFromParent(MI);325} else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(U)) {326if (II->getIntrinsicID() == Intrinsic::threadlocal_address)327append_range(WorkList, II->users());328}329}330331Changed |=332RecursivelyDeleteTriviallyDeadInstructionsPermissive(MaybeDeadInsts);333GV->removeDeadConstantUsers();334return Changed;335}336337/// Part of the global at a specific offset, which is only accessed through338/// loads and stores with the given type.339struct GlobalPart {340Type *Ty;341Constant *Initializer = nullptr;342bool IsLoaded = false;343bool IsStored = false;344};345346/// Look at all uses of the global and determine which (offset, type) pairs it347/// can be split into.348static bool collectSRATypes(DenseMap<uint64_t, GlobalPart> &Parts,349GlobalVariable *GV, const DataLayout &DL) {350SmallVector<Use *, 16> Worklist;351SmallPtrSet<Use *, 16> Visited;352auto AppendUses = [&](Value *V) {353for (Use &U : V->uses())354if (Visited.insert(&U).second)355Worklist.push_back(&U);356};357AppendUses(GV);358while (!Worklist.empty()) {359Use *U = Worklist.pop_back_val();360User *V = U->getUser();361362auto *GEP = dyn_cast<GEPOperator>(V);363if (isa<BitCastOperator>(V) || isa<AddrSpaceCastOperator>(V) ||364(GEP && GEP->hasAllConstantIndices())) {365AppendUses(V);366continue;367}368369if (Value *Ptr = getLoadStorePointerOperand(V)) {370// This is storing the global address into somewhere, not storing into371// the global.372if (isa<StoreInst>(V) && U->getOperandNo() == 0)373return false;374375APInt Offset(DL.getIndexTypeSizeInBits(Ptr->getType()), 0);376Ptr = Ptr->stripAndAccumulateConstantOffsets(DL, Offset,377/* AllowNonInbounds */ true);378if (Ptr != GV || Offset.getActiveBits() >= 64)379return false;380381// TODO: We currently require that all accesses at a given offset must382// use the same type. This could be relaxed.383Type *Ty = getLoadStoreType(V);384const auto &[It, Inserted] =385Parts.try_emplace(Offset.getZExtValue(), GlobalPart{Ty});386if (Ty != It->second.Ty)387return false;388389if (Inserted) {390It->second.Initializer =391ConstantFoldLoadFromConst(GV->getInitializer(), Ty, Offset, DL);392if (!It->second.Initializer) {393LLVM_DEBUG(dbgs() << "Global SRA: Failed to evaluate initializer of "394<< *GV << " with type " << *Ty << " at offset "395<< Offset.getZExtValue());396return false;397}398}399400// Scalable types not currently supported.401if (Ty->isScalableTy())402return false;403404auto IsStored = [](Value *V, Constant *Initializer) {405auto *SI = dyn_cast<StoreInst>(V);406if (!SI)407return false;408409Constant *StoredConst = dyn_cast<Constant>(SI->getOperand(0));410if (!StoredConst)411return true;412413// Don't consider stores that only write the initializer value.414return Initializer != StoredConst;415};416417It->second.IsLoaded |= isa<LoadInst>(V);418It->second.IsStored |= IsStored(V, It->second.Initializer);419continue;420}421422// Ignore dead constant users.423if (auto *C = dyn_cast<Constant>(V)) {424if (!isSafeToDestroyConstant(C))425return false;426continue;427}428429// Unknown user.430return false;431}432433return true;434}435436/// Copy over the debug info for a variable to its SRA replacements.437static void transferSRADebugInfo(GlobalVariable *GV, GlobalVariable *NGV,438uint64_t FragmentOffsetInBits,439uint64_t FragmentSizeInBits,440uint64_t VarSize) {441SmallVector<DIGlobalVariableExpression *, 1> GVs;442GV->getDebugInfo(GVs);443for (auto *GVE : GVs) {444DIVariable *Var = GVE->getVariable();445DIExpression *Expr = GVE->getExpression();446int64_t CurVarOffsetInBytes = 0;447uint64_t CurVarOffsetInBits = 0;448uint64_t FragmentEndInBits = FragmentOffsetInBits + FragmentSizeInBits;449450// Calculate the offset (Bytes), Continue if unknown.451if (!Expr->extractIfOffset(CurVarOffsetInBytes))452continue;453454// Ignore negative offset.455if (CurVarOffsetInBytes < 0)456continue;457458// Convert offset to bits.459CurVarOffsetInBits = CHAR_BIT * (uint64_t)CurVarOffsetInBytes;460461// Current var starts after the fragment, ignore.462if (CurVarOffsetInBits >= FragmentEndInBits)463continue;464465uint64_t CurVarSize = Var->getType()->getSizeInBits();466uint64_t CurVarEndInBits = CurVarOffsetInBits + CurVarSize;467// Current variable ends before start of fragment, ignore.468if (CurVarSize != 0 && /* CurVarSize is known */469CurVarEndInBits <= FragmentOffsetInBits)470continue;471472// Current variable fits in (not greater than) the fragment,473// does not need fragment expression.474if (CurVarSize != 0 && /* CurVarSize is known */475CurVarOffsetInBits >= FragmentOffsetInBits &&476CurVarEndInBits <= FragmentEndInBits) {477uint64_t CurVarOffsetInFragment =478(CurVarOffsetInBits - FragmentOffsetInBits) / 8;479if (CurVarOffsetInFragment != 0)480Expr = DIExpression::get(Expr->getContext(), {dwarf::DW_OP_plus_uconst,481CurVarOffsetInFragment});482else483Expr = DIExpression::get(Expr->getContext(), {});484auto *NGVE =485DIGlobalVariableExpression::get(GVE->getContext(), Var, Expr);486NGV->addDebugInfo(NGVE);487continue;488}489// Current variable does not fit in single fragment,490// emit a fragment expression.491if (FragmentSizeInBits < VarSize) {492if (CurVarOffsetInBits > FragmentOffsetInBits)493continue;494uint64_t CurVarFragmentOffsetInBits =495FragmentOffsetInBits - CurVarOffsetInBits;496uint64_t CurVarFragmentSizeInBits = FragmentSizeInBits;497if (CurVarSize != 0 && CurVarEndInBits < FragmentEndInBits)498CurVarFragmentSizeInBits -= (FragmentEndInBits - CurVarEndInBits);499if (CurVarOffsetInBits)500Expr = DIExpression::get(Expr->getContext(), {});501if (auto E = DIExpression::createFragmentExpression(502Expr, CurVarFragmentOffsetInBits, CurVarFragmentSizeInBits))503Expr = *E;504else505continue;506}507auto *NGVE = DIGlobalVariableExpression::get(GVE->getContext(), Var, Expr);508NGV->addDebugInfo(NGVE);509}510}511512/// Perform scalar replacement of aggregates on the specified global variable.513/// This opens the door for other optimizations by exposing the behavior of the514/// program in a more fine-grained way. We have determined that this515/// transformation is safe already. We return the first global variable we516/// insert so that the caller can reprocess it.517static GlobalVariable *SRAGlobal(GlobalVariable *GV, const DataLayout &DL) {518assert(GV->hasLocalLinkage());519520// Collect types to split into.521DenseMap<uint64_t, GlobalPart> Parts;522if (!collectSRATypes(Parts, GV, DL) || Parts.empty())523return nullptr;524525// Make sure we don't SRA back to the same type.526if (Parts.size() == 1 && Parts.begin()->second.Ty == GV->getValueType())527return nullptr;528529// Don't perform SRA if we would have to split into many globals. Ignore530// parts that are either only loaded or only stored, because we expect them531// to be optimized away.532unsigned NumParts = count_if(Parts, [](const auto &Pair) {533return Pair.second.IsLoaded && Pair.second.IsStored;534});535if (NumParts > 16)536return nullptr;537538// Sort by offset.539SmallVector<std::tuple<uint64_t, Type *, Constant *>, 16> TypesVector;540for (const auto &Pair : Parts) {541TypesVector.push_back(542{Pair.first, Pair.second.Ty, Pair.second.Initializer});543}544sort(TypesVector, llvm::less_first());545546// Check that the types are non-overlapping.547uint64_t Offset = 0;548for (const auto &[OffsetForTy, Ty, _] : TypesVector) {549// Overlaps with previous type.550if (OffsetForTy < Offset)551return nullptr;552553Offset = OffsetForTy + DL.getTypeAllocSize(Ty);554}555556// Some accesses go beyond the end of the global, don't bother.557if (Offset > DL.getTypeAllocSize(GV->getValueType()))558return nullptr;559560LLVM_DEBUG(dbgs() << "PERFORMING GLOBAL SRA ON: " << *GV << "\n");561562// Get the alignment of the global, either explicit or target-specific.563Align StartAlignment =564DL.getValueOrABITypeAlignment(GV->getAlign(), GV->getValueType());565uint64_t VarSize = DL.getTypeSizeInBits(GV->getValueType());566567// Create replacement globals.568DenseMap<uint64_t, GlobalVariable *> NewGlobals;569unsigned NameSuffix = 0;570for (auto &[OffsetForTy, Ty, Initializer] : TypesVector) {571GlobalVariable *NGV = new GlobalVariable(572*GV->getParent(), Ty, false, GlobalVariable::InternalLinkage,573Initializer, GV->getName() + "." + Twine(NameSuffix++), GV,574GV->getThreadLocalMode(), GV->getAddressSpace());575NGV->copyAttributesFrom(GV);576NewGlobals.insert({OffsetForTy, NGV});577578// Calculate the known alignment of the field. If the original aggregate579// had 256 byte alignment for example, something might depend on that:580// propagate info to each field.581Align NewAlign = commonAlignment(StartAlignment, OffsetForTy);582if (NewAlign > DL.getABITypeAlign(Ty))583NGV->setAlignment(NewAlign);584585// Copy over the debug info for the variable.586transferSRADebugInfo(GV, NGV, OffsetForTy * 8,587DL.getTypeAllocSizeInBits(Ty), VarSize);588}589590// Replace uses of the original global with uses of the new global.591SmallVector<Value *, 16> Worklist;592SmallPtrSet<Value *, 16> Visited;593SmallVector<WeakTrackingVH, 16> DeadInsts;594auto AppendUsers = [&](Value *V) {595for (User *U : V->users())596if (Visited.insert(U).second)597Worklist.push_back(U);598};599AppendUsers(GV);600while (!Worklist.empty()) {601Value *V = Worklist.pop_back_val();602if (isa<BitCastOperator>(V) || isa<AddrSpaceCastOperator>(V) ||603isa<GEPOperator>(V)) {604AppendUsers(V);605if (isa<Instruction>(V))606DeadInsts.push_back(V);607continue;608}609610if (Value *Ptr = getLoadStorePointerOperand(V)) {611APInt Offset(DL.getIndexTypeSizeInBits(Ptr->getType()), 0);612Ptr = Ptr->stripAndAccumulateConstantOffsets(DL, Offset,613/* AllowNonInbounds */ true);614assert(Ptr == GV && "Load/store must be from/to global");615GlobalVariable *NGV = NewGlobals[Offset.getZExtValue()];616assert(NGV && "Must have replacement global for this offset");617618// Update the pointer operand and recalculate alignment.619Align PrefAlign = DL.getPrefTypeAlign(getLoadStoreType(V));620Align NewAlign =621getOrEnforceKnownAlignment(NGV, PrefAlign, DL, cast<Instruction>(V));622623if (auto *LI = dyn_cast<LoadInst>(V)) {624LI->setOperand(0, NGV);625LI->setAlignment(NewAlign);626} else {627auto *SI = cast<StoreInst>(V);628SI->setOperand(1, NGV);629SI->setAlignment(NewAlign);630}631continue;632}633634assert(isa<Constant>(V) && isSafeToDestroyConstant(cast<Constant>(V)) &&635"Other users can only be dead constants");636}637638// Delete old instructions and global.639RecursivelyDeleteTriviallyDeadInstructions(DeadInsts);640GV->removeDeadConstantUsers();641GV->eraseFromParent();642++NumSRA;643644assert(NewGlobals.size() > 0);645return NewGlobals.begin()->second;646}647648/// Return true if all users of the specified value will trap if the value is649/// dynamically null. PHIs keeps track of any phi nodes we've seen to avoid650/// reprocessing them.651static bool AllUsesOfValueWillTrapIfNull(const Value *V,652SmallPtrSetImpl<const PHINode*> &PHIs) {653for (const User *U : V->users()) {654if (const Instruction *I = dyn_cast<Instruction>(U)) {655// If null pointer is considered valid, then all uses are non-trapping.656// Non address-space 0 globals have already been pruned by the caller.657if (NullPointerIsDefined(I->getFunction()))658return false;659}660if (isa<LoadInst>(U)) {661// Will trap.662} else if (const StoreInst *SI = dyn_cast<StoreInst>(U)) {663if (SI->getOperand(0) == V) {664return false; // Storing the value.665}666} else if (const CallInst *CI = dyn_cast<CallInst>(U)) {667if (CI->getCalledOperand() != V) {668return false; // Not calling the ptr669}670} else if (const InvokeInst *II = dyn_cast<InvokeInst>(U)) {671if (II->getCalledOperand() != V) {672return false; // Not calling the ptr673}674} else if (const AddrSpaceCastInst *CI = dyn_cast<AddrSpaceCastInst>(U)) {675if (!AllUsesOfValueWillTrapIfNull(CI, PHIs))676return false;677} else if (const GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(U)) {678if (!AllUsesOfValueWillTrapIfNull(GEPI, PHIs)) return false;679} else if (const PHINode *PN = dyn_cast<PHINode>(U)) {680// If we've already seen this phi node, ignore it, it has already been681// checked.682if (PHIs.insert(PN).second && !AllUsesOfValueWillTrapIfNull(PN, PHIs))683return false;684} else if (isa<ICmpInst>(U) &&685!ICmpInst::isSigned(cast<ICmpInst>(U)->getPredicate()) &&686isa<LoadInst>(U->getOperand(0)) &&687isa<ConstantPointerNull>(U->getOperand(1))) {688assert(isa<GlobalValue>(cast<LoadInst>(U->getOperand(0))689->getPointerOperand()690->stripPointerCasts()) &&691"Should be GlobalVariable");692// This and only this kind of non-signed ICmpInst is to be replaced with693// the comparing of the value of the created global init bool later in694// optimizeGlobalAddressOfAllocation for the global variable.695} else {696return false;697}698}699return true;700}701702/// Return true if all uses of any loads from GV will trap if the loaded value703/// is null. Note that this also permits comparisons of the loaded value704/// against null, as a special case.705static bool allUsesOfLoadedValueWillTrapIfNull(const GlobalVariable *GV) {706SmallVector<const Value *, 4> Worklist;707Worklist.push_back(GV);708while (!Worklist.empty()) {709const Value *P = Worklist.pop_back_val();710for (const auto *U : P->users()) {711if (auto *LI = dyn_cast<LoadInst>(U)) {712SmallPtrSet<const PHINode *, 8> PHIs;713if (!AllUsesOfValueWillTrapIfNull(LI, PHIs))714return false;715} else if (auto *SI = dyn_cast<StoreInst>(U)) {716// Ignore stores to the global.717if (SI->getPointerOperand() != P)718return false;719} else if (auto *CE = dyn_cast<ConstantExpr>(U)) {720if (CE->stripPointerCasts() != GV)721return false;722// Check further the ConstantExpr.723Worklist.push_back(CE);724} else {725// We don't know or understand this user, bail out.726return false;727}728}729}730731return true;732}733734/// Get all the loads/store uses for global variable \p GV.735static void allUsesOfLoadAndStores(GlobalVariable *GV,736SmallVector<Value *, 4> &Uses) {737SmallVector<Value *, 4> Worklist;738Worklist.push_back(GV);739while (!Worklist.empty()) {740auto *P = Worklist.pop_back_val();741for (auto *U : P->users()) {742if (auto *CE = dyn_cast<ConstantExpr>(U)) {743Worklist.push_back(CE);744continue;745}746747assert((isa<LoadInst>(U) || isa<StoreInst>(U)) &&748"Expect only load or store instructions");749Uses.push_back(U);750}751}752}753754static bool OptimizeAwayTrappingUsesOfValue(Value *V, Constant *NewV) {755bool Changed = false;756for (auto UI = V->user_begin(), E = V->user_end(); UI != E; ) {757Instruction *I = cast<Instruction>(*UI++);758// Uses are non-trapping if null pointer is considered valid.759// Non address-space 0 globals are already pruned by the caller.760if (NullPointerIsDefined(I->getFunction()))761return false;762if (LoadInst *LI = dyn_cast<LoadInst>(I)) {763LI->setOperand(0, NewV);764Changed = true;765} else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {766if (SI->getOperand(1) == V) {767SI->setOperand(1, NewV);768Changed = true;769}770} else if (isa<CallInst>(I) || isa<InvokeInst>(I)) {771CallBase *CB = cast<CallBase>(I);772if (CB->getCalledOperand() == V) {773// Calling through the pointer! Turn into a direct call, but be careful774// that the pointer is not also being passed as an argument.775CB->setCalledOperand(NewV);776Changed = true;777bool PassedAsArg = false;778for (unsigned i = 0, e = CB->arg_size(); i != e; ++i)779if (CB->getArgOperand(i) == V) {780PassedAsArg = true;781CB->setArgOperand(i, NewV);782}783784if (PassedAsArg) {785// Being passed as an argument also. Be careful to not invalidate UI!786UI = V->user_begin();787}788}789} else if (AddrSpaceCastInst *CI = dyn_cast<AddrSpaceCastInst>(I)) {790Changed |= OptimizeAwayTrappingUsesOfValue(791CI, ConstantExpr::getAddrSpaceCast(NewV, CI->getType()));792if (CI->use_empty()) {793Changed = true;794CI->eraseFromParent();795}796} else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) {797// Should handle GEP here.798SmallVector<Constant*, 8> Idxs;799Idxs.reserve(GEPI->getNumOperands()-1);800for (User::op_iterator i = GEPI->op_begin() + 1, e = GEPI->op_end();801i != e; ++i)802if (Constant *C = dyn_cast<Constant>(*i))803Idxs.push_back(C);804else805break;806if (Idxs.size() == GEPI->getNumOperands()-1)807Changed |= OptimizeAwayTrappingUsesOfValue(808GEPI, ConstantExpr::getGetElementPtr(GEPI->getSourceElementType(),809NewV, Idxs));810if (GEPI->use_empty()) {811Changed = true;812GEPI->eraseFromParent();813}814}815}816817return Changed;818}819820/// The specified global has only one non-null value stored into it. If there821/// are uses of the loaded value that would trap if the loaded value is822/// dynamically null, then we know that they cannot be reachable with a null823/// optimize away the load.824static bool OptimizeAwayTrappingUsesOfLoads(825GlobalVariable *GV, Constant *LV, const DataLayout &DL,826function_ref<TargetLibraryInfo &(Function &)> GetTLI) {827bool Changed = false;828829// Keep track of whether we are able to remove all the uses of the global830// other than the store that defines it.831bool AllNonStoreUsesGone = true;832833// Replace all uses of loads with uses of uses of the stored value.834for (User *GlobalUser : llvm::make_early_inc_range(GV->users())) {835if (LoadInst *LI = dyn_cast<LoadInst>(GlobalUser)) {836Changed |= OptimizeAwayTrappingUsesOfValue(LI, LV);837// If we were able to delete all uses of the loads838if (LI->use_empty()) {839LI->eraseFromParent();840Changed = true;841} else {842AllNonStoreUsesGone = false;843}844} else if (isa<StoreInst>(GlobalUser)) {845// Ignore the store that stores "LV" to the global.846assert(GlobalUser->getOperand(1) == GV &&847"Must be storing *to* the global");848} else {849AllNonStoreUsesGone = false;850851// If we get here we could have other crazy uses that are transitively852// loaded.853assert((isa<PHINode>(GlobalUser) || isa<SelectInst>(GlobalUser) ||854isa<ConstantExpr>(GlobalUser) || isa<CmpInst>(GlobalUser) ||855isa<BitCastInst>(GlobalUser) ||856isa<GetElementPtrInst>(GlobalUser) ||857isa<AddrSpaceCastInst>(GlobalUser)) &&858"Only expect load and stores!");859}860}861862if (Changed) {863LLVM_DEBUG(dbgs() << "OPTIMIZED LOADS FROM STORED ONCE POINTER: " << *GV864<< "\n");865++NumGlobUses;866}867868// If we nuked all of the loads, then none of the stores are needed either,869// nor is the global.870if (AllNonStoreUsesGone) {871if (isLeakCheckerRoot(GV)) {872Changed |= CleanupPointerRootUsers(GV, GetTLI);873} else {874Changed = true;875CleanupConstantGlobalUsers(GV, DL);876}877if (GV->use_empty()) {878LLVM_DEBUG(dbgs() << " *** GLOBAL NOW DEAD!\n");879Changed = true;880GV->eraseFromParent();881++NumDeleted;882}883}884return Changed;885}886887/// Walk the use list of V, constant folding all of the instructions that are888/// foldable.889static void ConstantPropUsersOf(Value *V, const DataLayout &DL,890TargetLibraryInfo *TLI) {891for (Value::user_iterator UI = V->user_begin(), E = V->user_end(); UI != E; )892if (Instruction *I = dyn_cast<Instruction>(*UI++))893if (Constant *NewC = ConstantFoldInstruction(I, DL, TLI)) {894I->replaceAllUsesWith(NewC);895896// Advance UI to the next non-I use to avoid invalidating it!897// Instructions could multiply use V.898while (UI != E && *UI == I)899++UI;900if (isInstructionTriviallyDead(I, TLI))901I->eraseFromParent();902}903}904905/// This function takes the specified global variable, and transforms the906/// program as if it always contained the result of the specified malloc.907/// Because it is always the result of the specified malloc, there is no reason908/// to actually DO the malloc. Instead, turn the malloc into a global, and any909/// loads of GV as uses of the new global.910static GlobalVariable *911OptimizeGlobalAddressOfAllocation(GlobalVariable *GV, CallInst *CI,912uint64_t AllocSize, Constant *InitVal,913const DataLayout &DL,914TargetLibraryInfo *TLI) {915LLVM_DEBUG(errs() << "PROMOTING GLOBAL: " << *GV << " CALL = " << *CI916<< '\n');917918// Create global of type [AllocSize x i8].919Type *GlobalType = ArrayType::get(Type::getInt8Ty(GV->getContext()),920AllocSize);921922// Create the new global variable. The contents of the allocated memory is923// undefined initially, so initialize with an undef value.924GlobalVariable *NewGV = new GlobalVariable(925*GV->getParent(), GlobalType, false, GlobalValue::InternalLinkage,926UndefValue::get(GlobalType), GV->getName() + ".body", nullptr,927GV->getThreadLocalMode());928929// Initialize the global at the point of the original call. Note that this930// is a different point from the initialization referred to below for the931// nullability handling. Sublety: We have not proven the original global was932// only initialized once. As such, we can not fold this into the initializer933// of the new global as may need to re-init the storage multiple times.934if (!isa<UndefValue>(InitVal)) {935IRBuilder<> Builder(CI->getNextNode());936// TODO: Use alignment above if align!=1937Builder.CreateMemSet(NewGV, InitVal, AllocSize, std::nullopt);938}939940// Update users of the allocation to use the new global instead.941CI->replaceAllUsesWith(NewGV);942943// If there is a comparison against null, we will insert a global bool to944// keep track of whether the global was initialized yet or not.945GlobalVariable *InitBool =946new GlobalVariable(Type::getInt1Ty(GV->getContext()), false,947GlobalValue::InternalLinkage,948ConstantInt::getFalse(GV->getContext()),949GV->getName()+".init", GV->getThreadLocalMode());950bool InitBoolUsed = false;951952// Loop over all instruction uses of GV, processing them in turn.953SmallVector<Value *, 4> Guses;954allUsesOfLoadAndStores(GV, Guses);955for (auto *U : Guses) {956if (StoreInst *SI = dyn_cast<StoreInst>(U)) {957// The global is initialized when the store to it occurs. If the stored958// value is null value, the global bool is set to false, otherwise true.959new StoreInst(ConstantInt::getBool(960GV->getContext(),961!isa<ConstantPointerNull>(SI->getValueOperand())),962InitBool, false, Align(1), SI->getOrdering(),963SI->getSyncScopeID(), SI->getIterator());964SI->eraseFromParent();965continue;966}967968LoadInst *LI = cast<LoadInst>(U);969while (!LI->use_empty()) {970Use &LoadUse = *LI->use_begin();971ICmpInst *ICI = dyn_cast<ICmpInst>(LoadUse.getUser());972if (!ICI) {973LoadUse.set(NewGV);974continue;975}976977// Replace the cmp X, 0 with a use of the bool value.978Value *LV = new LoadInst(InitBool->getValueType(), InitBool,979InitBool->getName() + ".val", false, Align(1),980LI->getOrdering(), LI->getSyncScopeID(),981LI->getIterator());982InitBoolUsed = true;983switch (ICI->getPredicate()) {984default: llvm_unreachable("Unknown ICmp Predicate!");985case ICmpInst::ICMP_ULT: // X < null -> always false986LV = ConstantInt::getFalse(GV->getContext());987break;988case ICmpInst::ICMP_UGE: // X >= null -> always true989LV = ConstantInt::getTrue(GV->getContext());990break;991case ICmpInst::ICMP_ULE:992case ICmpInst::ICMP_EQ:993LV = BinaryOperator::CreateNot(LV, "notinit", ICI->getIterator());994break;995case ICmpInst::ICMP_NE:996case ICmpInst::ICMP_UGT:997break; // no change.998}999ICI->replaceAllUsesWith(LV);1000ICI->eraseFromParent();1001}1002LI->eraseFromParent();1003}10041005// If the initialization boolean was used, insert it, otherwise delete it.1006if (!InitBoolUsed) {1007while (!InitBool->use_empty()) // Delete initializations1008cast<StoreInst>(InitBool->user_back())->eraseFromParent();1009delete InitBool;1010} else1011GV->getParent()->insertGlobalVariable(GV->getIterator(), InitBool);10121013// Now the GV is dead, nuke it and the allocation..1014GV->eraseFromParent();1015CI->eraseFromParent();10161017// To further other optimizations, loop over all users of NewGV and try to1018// constant prop them. This will promote GEP instructions with constant1019// indices into GEP constant-exprs, which will allow global-opt to hack on it.1020ConstantPropUsersOf(NewGV, DL, TLI);10211022return NewGV;1023}10241025/// Scan the use-list of GV checking to make sure that there are no complex uses1026/// of GV. We permit simple things like dereferencing the pointer, but not1027/// storing through the address, unless it is to the specified global.1028static bool1029valueIsOnlyUsedLocallyOrStoredToOneGlobal(const CallInst *CI,1030const GlobalVariable *GV) {1031SmallPtrSet<const Value *, 4> Visited;1032SmallVector<const Value *, 4> Worklist;1033Worklist.push_back(CI);10341035while (!Worklist.empty()) {1036const Value *V = Worklist.pop_back_val();1037if (!Visited.insert(V).second)1038continue;10391040for (const Use &VUse : V->uses()) {1041const User *U = VUse.getUser();1042if (isa<LoadInst>(U) || isa<CmpInst>(U))1043continue; // Fine, ignore.10441045if (auto *SI = dyn_cast<StoreInst>(U)) {1046if (SI->getValueOperand() == V &&1047SI->getPointerOperand()->stripPointerCasts() != GV)1048return false; // Storing the pointer not into GV... bad.1049continue; // Otherwise, storing through it, or storing into GV... fine.1050}10511052if (auto *BCI = dyn_cast<BitCastInst>(U)) {1053Worklist.push_back(BCI);1054continue;1055}10561057if (auto *GEPI = dyn_cast<GetElementPtrInst>(U)) {1058Worklist.push_back(GEPI);1059continue;1060}10611062return false;1063}1064}10651066return true;1067}10681069/// If we have a global that is only initialized with a fixed size allocation1070/// try to transform the program to use global memory instead of heap1071/// allocated memory. This eliminates dynamic allocation, avoids an indirection1072/// accessing the data, and exposes the resultant global to further GlobalOpt.1073static bool tryToOptimizeStoreOfAllocationToGlobal(GlobalVariable *GV,1074CallInst *CI,1075const DataLayout &DL,1076TargetLibraryInfo *TLI) {1077if (!isRemovableAlloc(CI, TLI))1078// Must be able to remove the call when we get done..1079return false;10801081Type *Int8Ty = Type::getInt8Ty(CI->getFunction()->getContext());1082Constant *InitVal = getInitialValueOfAllocation(CI, TLI, Int8Ty);1083if (!InitVal)1084// Must be able to emit a memset for initialization1085return false;10861087uint64_t AllocSize;1088if (!getObjectSize(CI, AllocSize, DL, TLI, ObjectSizeOpts()))1089return false;10901091// Restrict this transformation to only working on small allocations1092// (2048 bytes currently), as we don't want to introduce a 16M global or1093// something.1094if (AllocSize >= 2048)1095return false;10961097// We can't optimize this global unless all uses of it are *known* to be1098// of the malloc value, not of the null initializer value (consider a use1099// that compares the global's value against zero to see if the malloc has1100// been reached). To do this, we check to see if all uses of the global1101// would trap if the global were null: this proves that they must all1102// happen after the malloc.1103if (!allUsesOfLoadedValueWillTrapIfNull(GV))1104return false;11051106// We can't optimize this if the malloc itself is used in a complex way,1107// for example, being stored into multiple globals. This allows the1108// malloc to be stored into the specified global, loaded, gep, icmp'd.1109// These are all things we could transform to using the global for.1110if (!valueIsOnlyUsedLocallyOrStoredToOneGlobal(CI, GV))1111return false;11121113OptimizeGlobalAddressOfAllocation(GV, CI, AllocSize, InitVal, DL, TLI);1114return true;1115}11161117// Try to optimize globals based on the knowledge that only one value (besides1118// its initializer) is ever stored to the global.1119static bool1120optimizeOnceStoredGlobal(GlobalVariable *GV, Value *StoredOnceVal,1121const DataLayout &DL,1122function_ref<TargetLibraryInfo &(Function &)> GetTLI) {1123// Ignore no-op GEPs and bitcasts.1124StoredOnceVal = StoredOnceVal->stripPointerCasts();11251126// If we are dealing with a pointer global that is initialized to null and1127// only has one (non-null) value stored into it, then we can optimize any1128// users of the loaded value (often calls and loads) that would trap if the1129// value was null.1130if (GV->getInitializer()->getType()->isPointerTy() &&1131GV->getInitializer()->isNullValue() &&1132StoredOnceVal->getType()->isPointerTy() &&1133!NullPointerIsDefined(1134nullptr /* F */,1135GV->getInitializer()->getType()->getPointerAddressSpace())) {1136if (Constant *SOVC = dyn_cast<Constant>(StoredOnceVal)) {1137// Optimize away any trapping uses of the loaded value.1138if (OptimizeAwayTrappingUsesOfLoads(GV, SOVC, DL, GetTLI))1139return true;1140} else if (isAllocationFn(StoredOnceVal, GetTLI)) {1141if (auto *CI = dyn_cast<CallInst>(StoredOnceVal)) {1142auto *TLI = &GetTLI(*CI->getFunction());1143if (tryToOptimizeStoreOfAllocationToGlobal(GV, CI, DL, TLI))1144return true;1145}1146}1147}11481149return false;1150}11511152/// At this point, we have learned that the only two values ever stored into GV1153/// are its initializer and OtherVal. See if we can shrink the global into a1154/// boolean and select between the two values whenever it is used. This exposes1155/// the values to other scalar optimizations.1156static bool TryToShrinkGlobalToBoolean(GlobalVariable *GV, Constant *OtherVal) {1157Type *GVElType = GV->getValueType();11581159// If GVElType is already i1, it is already shrunk. If the type of the GV is1160// an FP value, pointer or vector, don't do this optimization because a select1161// between them is very expensive and unlikely to lead to later1162// simplification. In these cases, we typically end up with "cond ? v1 : v2"1163// where v1 and v2 both require constant pool loads, a big loss.1164if (GVElType == Type::getInt1Ty(GV->getContext()) ||1165GVElType->isFloatingPointTy() ||1166GVElType->isPointerTy() || GVElType->isVectorTy())1167return false;11681169// Walk the use list of the global seeing if all the uses are load or store.1170// If there is anything else, bail out.1171for (User *U : GV->users()) {1172if (!isa<LoadInst>(U) && !isa<StoreInst>(U))1173return false;1174if (getLoadStoreType(U) != GVElType)1175return false;1176}11771178LLVM_DEBUG(dbgs() << " *** SHRINKING TO BOOL: " << *GV << "\n");11791180// Create the new global, initializing it to false.1181GlobalVariable *NewGV = new GlobalVariable(Type::getInt1Ty(GV->getContext()),1182false,1183GlobalValue::InternalLinkage,1184ConstantInt::getFalse(GV->getContext()),1185GV->getName()+".b",1186GV->getThreadLocalMode(),1187GV->getType()->getAddressSpace());1188NewGV->copyAttributesFrom(GV);1189GV->getParent()->insertGlobalVariable(GV->getIterator(), NewGV);11901191Constant *InitVal = GV->getInitializer();1192assert(InitVal->getType() != Type::getInt1Ty(GV->getContext()) &&1193"No reason to shrink to bool!");11941195SmallVector<DIGlobalVariableExpression *, 1> GVs;1196GV->getDebugInfo(GVs);11971198// If initialized to zero and storing one into the global, we can use a cast1199// instead of a select to synthesize the desired value.1200bool IsOneZero = false;1201bool EmitOneOrZero = true;1202auto *CI = dyn_cast<ConstantInt>(OtherVal);1203if (CI && CI->getValue().getActiveBits() <= 64) {1204IsOneZero = InitVal->isNullValue() && CI->isOne();12051206auto *CIInit = dyn_cast<ConstantInt>(GV->getInitializer());1207if (CIInit && CIInit->getValue().getActiveBits() <= 64) {1208uint64_t ValInit = CIInit->getZExtValue();1209uint64_t ValOther = CI->getZExtValue();1210uint64_t ValMinus = ValOther - ValInit;12111212for(auto *GVe : GVs){1213DIGlobalVariable *DGV = GVe->getVariable();1214DIExpression *E = GVe->getExpression();1215const DataLayout &DL = GV->getDataLayout();1216unsigned SizeInOctets =1217DL.getTypeAllocSizeInBits(NewGV->getValueType()) / 8;12181219// It is expected that the address of global optimized variable is on1220// top of the stack. After optimization, value of that variable will1221// be ether 0 for initial value or 1 for other value. The following1222// expression should return constant integer value depending on the1223// value at global object address:1224// val * (ValOther - ValInit) + ValInit:1225// DW_OP_deref DW_OP_constu <ValMinus>1226// DW_OP_mul DW_OP_constu <ValInit> DW_OP_plus DW_OP_stack_value1227SmallVector<uint64_t, 12> Ops = {1228dwarf::DW_OP_deref_size, SizeInOctets,1229dwarf::DW_OP_constu, ValMinus,1230dwarf::DW_OP_mul, dwarf::DW_OP_constu, ValInit,1231dwarf::DW_OP_plus};1232bool WithStackValue = true;1233E = DIExpression::prependOpcodes(E, Ops, WithStackValue);1234DIGlobalVariableExpression *DGVE =1235DIGlobalVariableExpression::get(NewGV->getContext(), DGV, E);1236NewGV->addDebugInfo(DGVE);1237}1238EmitOneOrZero = false;1239}1240}12411242if (EmitOneOrZero) {1243// FIXME: This will only emit address for debugger on which will1244// be written only 0 or 1.1245for(auto *GV : GVs)1246NewGV->addDebugInfo(GV);1247}12481249while (!GV->use_empty()) {1250Instruction *UI = cast<Instruction>(GV->user_back());1251if (StoreInst *SI = dyn_cast<StoreInst>(UI)) {1252// Change the store into a boolean store.1253bool StoringOther = SI->getOperand(0) == OtherVal;1254// Only do this if we weren't storing a loaded value.1255Value *StoreVal;1256if (StoringOther || SI->getOperand(0) == InitVal) {1257StoreVal = ConstantInt::get(Type::getInt1Ty(GV->getContext()),1258StoringOther);1259} else {1260// Otherwise, we are storing a previously loaded copy. To do this,1261// change the copy from copying the original value to just copying the1262// bool.1263Instruction *StoredVal = cast<Instruction>(SI->getOperand(0));12641265// If we've already replaced the input, StoredVal will be a cast or1266// select instruction. If not, it will be a load of the original1267// global.1268if (LoadInst *LI = dyn_cast<LoadInst>(StoredVal)) {1269assert(LI->getOperand(0) == GV && "Not a copy!");1270// Insert a new load, to preserve the saved value.1271StoreVal =1272new LoadInst(NewGV->getValueType(), NewGV, LI->getName() + ".b",1273false, Align(1), LI->getOrdering(),1274LI->getSyncScopeID(), LI->getIterator());1275} else {1276assert((isa<CastInst>(StoredVal) || isa<SelectInst>(StoredVal)) &&1277"This is not a form that we understand!");1278StoreVal = StoredVal->getOperand(0);1279assert(isa<LoadInst>(StoreVal) && "Not a load of NewGV!");1280}1281}1282StoreInst *NSI =1283new StoreInst(StoreVal, NewGV, false, Align(1), SI->getOrdering(),1284SI->getSyncScopeID(), SI->getIterator());1285NSI->setDebugLoc(SI->getDebugLoc());1286} else {1287// Change the load into a load of bool then a select.1288LoadInst *LI = cast<LoadInst>(UI);1289LoadInst *NLI = new LoadInst(1290NewGV->getValueType(), NewGV, LI->getName() + ".b", false, Align(1),1291LI->getOrdering(), LI->getSyncScopeID(), LI->getIterator());1292Instruction *NSI;1293if (IsOneZero)1294NSI = new ZExtInst(NLI, LI->getType(), "", LI->getIterator());1295else1296NSI = SelectInst::Create(NLI, OtherVal, InitVal, "", LI->getIterator());1297NSI->takeName(LI);1298// Since LI is split into two instructions, NLI and NSI both inherit the1299// same DebugLoc1300NLI->setDebugLoc(LI->getDebugLoc());1301NSI->setDebugLoc(LI->getDebugLoc());1302LI->replaceAllUsesWith(NSI);1303}1304UI->eraseFromParent();1305}13061307// Retain the name of the old global variable. People who are debugging their1308// programs may expect these variables to be named the same.1309NewGV->takeName(GV);1310GV->eraseFromParent();1311return true;1312}13131314static bool1315deleteIfDead(GlobalValue &GV,1316SmallPtrSetImpl<const Comdat *> &NotDiscardableComdats,1317function_ref<void(Function &)> DeleteFnCallback = nullptr) {1318GV.removeDeadConstantUsers();13191320if (!GV.isDiscardableIfUnused() && !GV.isDeclaration())1321return false;13221323if (const Comdat *C = GV.getComdat())1324if (!GV.hasLocalLinkage() && NotDiscardableComdats.count(C))1325return false;13261327bool Dead;1328if (auto *F = dyn_cast<Function>(&GV))1329Dead = (F->isDeclaration() && F->use_empty()) || F->isDefTriviallyDead();1330else1331Dead = GV.use_empty();1332if (!Dead)1333return false;13341335LLVM_DEBUG(dbgs() << "GLOBAL DEAD: " << GV << "\n");1336if (auto *F = dyn_cast<Function>(&GV)) {1337if (DeleteFnCallback)1338DeleteFnCallback(*F);1339}1340GV.eraseFromParent();1341++NumDeleted;1342return true;1343}13441345static bool isPointerValueDeadOnEntryToFunction(1346const Function *F, GlobalValue *GV,1347function_ref<DominatorTree &(Function &)> LookupDomTree) {1348// Find all uses of GV. We expect them all to be in F, and if we can't1349// identify any of the uses we bail out.1350//1351// On each of these uses, identify if the memory that GV points to is1352// used/required/live at the start of the function. If it is not, for example1353// if the first thing the function does is store to the GV, the GV can1354// possibly be demoted.1355//1356// We don't do an exhaustive search for memory operations - simply look1357// through bitcasts as they're quite common and benign.1358const DataLayout &DL = GV->getDataLayout();1359SmallVector<LoadInst *, 4> Loads;1360SmallVector<StoreInst *, 4> Stores;1361for (auto *U : GV->users()) {1362Instruction *I = dyn_cast<Instruction>(U);1363if (!I)1364return false;1365assert(I->getParent()->getParent() == F);13661367if (auto *LI = dyn_cast<LoadInst>(I))1368Loads.push_back(LI);1369else if (auto *SI = dyn_cast<StoreInst>(I))1370Stores.push_back(SI);1371else1372return false;1373}13741375// We have identified all uses of GV into loads and stores. Now check if all1376// of them are known not to depend on the value of the global at the function1377// entry point. We do this by ensuring that every load is dominated by at1378// least one store.1379auto &DT = LookupDomTree(*const_cast<Function *>(F));13801381// The below check is quadratic. Check we're not going to do too many tests.1382// FIXME: Even though this will always have worst-case quadratic time, we1383// could put effort into minimizing the average time by putting stores that1384// have been shown to dominate at least one load at the beginning of the1385// Stores array, making subsequent dominance checks more likely to succeed1386// early.1387//1388// The threshold here is fairly large because global->local demotion is a1389// very powerful optimization should it fire.1390const unsigned Threshold = 100;1391if (Loads.size() * Stores.size() > Threshold)1392return false;13931394for (auto *L : Loads) {1395auto *LTy = L->getType();1396if (none_of(Stores, [&](const StoreInst *S) {1397auto *STy = S->getValueOperand()->getType();1398// The load is only dominated by the store if DomTree says so1399// and the number of bits loaded in L is less than or equal to1400// the number of bits stored in S.1401return DT.dominates(S, L) &&1402DL.getTypeStoreSize(LTy).getFixedValue() <=1403DL.getTypeStoreSize(STy).getFixedValue();1404}))1405return false;1406}1407// All loads have known dependences inside F, so the global can be localized.1408return true;1409}14101411// For a global variable with one store, if the store dominates any loads,1412// those loads will always load the stored value (as opposed to the1413// initializer), even in the presence of recursion.1414static bool forwardStoredOnceStore(1415GlobalVariable *GV, const StoreInst *StoredOnceStore,1416function_ref<DominatorTree &(Function &)> LookupDomTree) {1417const Value *StoredOnceValue = StoredOnceStore->getValueOperand();1418// We can do this optimization for non-constants in nosync + norecurse1419// functions, but globals used in exactly one norecurse functions are already1420// promoted to an alloca.1421if (!isa<Constant>(StoredOnceValue))1422return false;1423const Function *F = StoredOnceStore->getFunction();1424SmallVector<LoadInst *> Loads;1425for (User *U : GV->users()) {1426if (auto *LI = dyn_cast<LoadInst>(U)) {1427if (LI->getFunction() == F &&1428LI->getType() == StoredOnceValue->getType() && LI->isSimple())1429Loads.push_back(LI);1430}1431}1432// Only compute DT if we have any loads to examine.1433bool MadeChange = false;1434if (!Loads.empty()) {1435auto &DT = LookupDomTree(*const_cast<Function *>(F));1436for (auto *LI : Loads) {1437if (DT.dominates(StoredOnceStore, LI)) {1438LI->replaceAllUsesWith(const_cast<Value *>(StoredOnceValue));1439LI->eraseFromParent();1440MadeChange = true;1441}1442}1443}1444return MadeChange;1445}14461447/// Analyze the specified global variable and optimize1448/// it if possible. If we make a change, return true.1449static bool1450processInternalGlobal(GlobalVariable *GV, const GlobalStatus &GS,1451function_ref<TargetTransformInfo &(Function &)> GetTTI,1452function_ref<TargetLibraryInfo &(Function &)> GetTLI,1453function_ref<DominatorTree &(Function &)> LookupDomTree) {1454auto &DL = GV->getDataLayout();1455// If this is a first class global and has only one accessing function and1456// this function is non-recursive, we replace the global with a local alloca1457// in this function.1458//1459// NOTE: It doesn't make sense to promote non-single-value types since we1460// are just replacing static memory to stack memory.1461//1462// If the global is in different address space, don't bring it to stack.1463if (!GS.HasMultipleAccessingFunctions &&1464GS.AccessingFunction &&1465GV->getValueType()->isSingleValueType() &&1466GV->getType()->getAddressSpace() == DL.getAllocaAddrSpace() &&1467!GV->isExternallyInitialized() &&1468GS.AccessingFunction->doesNotRecurse() &&1469isPointerValueDeadOnEntryToFunction(GS.AccessingFunction, GV,1470LookupDomTree)) {1471const DataLayout &DL = GV->getDataLayout();14721473LLVM_DEBUG(dbgs() << "LOCALIZING GLOBAL: " << *GV << "\n");1474BasicBlock::iterator FirstI =1475GS.AccessingFunction->getEntryBlock().begin().getNonConst();1476Type *ElemTy = GV->getValueType();1477// FIXME: Pass Global's alignment when globals have alignment1478AllocaInst *Alloca = new AllocaInst(ElemTy, DL.getAllocaAddrSpace(),1479nullptr, GV->getName(), FirstI);1480if (!isa<UndefValue>(GV->getInitializer()))1481new StoreInst(GV->getInitializer(), Alloca, FirstI);14821483GV->replaceAllUsesWith(Alloca);1484GV->eraseFromParent();1485++NumLocalized;1486return true;1487}14881489bool Changed = false;14901491// If the global is never loaded (but may be stored to), it is dead.1492// Delete it now.1493if (!GS.IsLoaded) {1494LLVM_DEBUG(dbgs() << "GLOBAL NEVER LOADED: " << *GV << "\n");14951496if (isLeakCheckerRoot(GV)) {1497// Delete any constant stores to the global.1498Changed = CleanupPointerRootUsers(GV, GetTLI);1499} else {1500// Delete any stores we can find to the global. We may not be able to1501// make it completely dead though.1502Changed = CleanupConstantGlobalUsers(GV, DL);1503}15041505// If the global is dead now, delete it.1506if (GV->use_empty()) {1507GV->eraseFromParent();1508++NumDeleted;1509Changed = true;1510}1511return Changed;15121513}1514if (GS.StoredType <= GlobalStatus::InitializerStored) {1515LLVM_DEBUG(dbgs() << "MARKING CONSTANT: " << *GV << "\n");15161517// Don't actually mark a global constant if it's atomic because atomic loads1518// are implemented by a trivial cmpxchg in some edge-cases and that usually1519// requires write access to the variable even if it's not actually changed.1520if (GS.Ordering == AtomicOrdering::NotAtomic) {1521assert(!GV->isConstant() && "Expected a non-constant global");1522GV->setConstant(true);1523Changed = true;1524}15251526// Clean up any obviously simplifiable users now.1527Changed |= CleanupConstantGlobalUsers(GV, DL);15281529// If the global is dead now, just nuke it.1530if (GV->use_empty()) {1531LLVM_DEBUG(dbgs() << " *** Marking constant allowed us to simplify "1532<< "all users and delete global!\n");1533GV->eraseFromParent();1534++NumDeleted;1535return true;1536}15371538// Fall through to the next check; see if we can optimize further.1539++NumMarked;1540}1541if (!GV->getInitializer()->getType()->isSingleValueType()) {1542const DataLayout &DL = GV->getDataLayout();1543if (SRAGlobal(GV, DL))1544return true;1545}1546Value *StoredOnceValue = GS.getStoredOnceValue();1547if (GS.StoredType == GlobalStatus::StoredOnce && StoredOnceValue) {1548Function &StoreFn =1549const_cast<Function &>(*GS.StoredOnceStore->getFunction());1550bool CanHaveNonUndefGlobalInitializer =1551GetTTI(StoreFn).canHaveNonUndefGlobalInitializerInAddressSpace(1552GV->getType()->getAddressSpace());1553// If the initial value for the global was an undef value, and if only1554// one other value was stored into it, we can just change the1555// initializer to be the stored value, then delete all stores to the1556// global. This allows us to mark it constant.1557// This is restricted to address spaces that allow globals to have1558// initializers. NVPTX, for example, does not support initializers for1559// shared memory (AS 3).1560auto *SOVConstant = dyn_cast<Constant>(StoredOnceValue);1561if (SOVConstant && isa<UndefValue>(GV->getInitializer()) &&1562DL.getTypeAllocSize(SOVConstant->getType()) ==1563DL.getTypeAllocSize(GV->getValueType()) &&1564CanHaveNonUndefGlobalInitializer) {1565if (SOVConstant->getType() == GV->getValueType()) {1566// Change the initializer in place.1567GV->setInitializer(SOVConstant);1568} else {1569// Create a new global with adjusted type.1570auto *NGV = new GlobalVariable(1571*GV->getParent(), SOVConstant->getType(), GV->isConstant(),1572GV->getLinkage(), SOVConstant, "", GV, GV->getThreadLocalMode(),1573GV->getAddressSpace());1574NGV->takeName(GV);1575NGV->copyAttributesFrom(GV);1576GV->replaceAllUsesWith(NGV);1577GV->eraseFromParent();1578GV = NGV;1579}15801581// Clean up any obviously simplifiable users now.1582CleanupConstantGlobalUsers(GV, DL);15831584if (GV->use_empty()) {1585LLVM_DEBUG(dbgs() << " *** Substituting initializer allowed us to "1586<< "simplify all users and delete global!\n");1587GV->eraseFromParent();1588++NumDeleted;1589}1590++NumSubstitute;1591return true;1592}15931594// Try to optimize globals based on the knowledge that only one value1595// (besides its initializer) is ever stored to the global.1596if (optimizeOnceStoredGlobal(GV, StoredOnceValue, DL, GetTLI))1597return true;15981599// Try to forward the store to any loads. If we have more than one store, we1600// may have a store of the initializer between StoredOnceStore and a load.1601if (GS.NumStores == 1)1602if (forwardStoredOnceStore(GV, GS.StoredOnceStore, LookupDomTree))1603return true;16041605// Otherwise, if the global was not a boolean, we can shrink it to be a1606// boolean. Skip this optimization for AS that doesn't allow an initializer.1607if (SOVConstant && GS.Ordering == AtomicOrdering::NotAtomic &&1608(!isa<UndefValue>(GV->getInitializer()) ||1609CanHaveNonUndefGlobalInitializer)) {1610if (TryToShrinkGlobalToBoolean(GV, SOVConstant)) {1611++NumShrunkToBool;1612return true;1613}1614}1615}16161617return Changed;1618}16191620/// Analyze the specified global variable and optimize it if possible. If we1621/// make a change, return true.1622static bool1623processGlobal(GlobalValue &GV,1624function_ref<TargetTransformInfo &(Function &)> GetTTI,1625function_ref<TargetLibraryInfo &(Function &)> GetTLI,1626function_ref<DominatorTree &(Function &)> LookupDomTree) {1627if (GV.getName().starts_with("llvm."))1628return false;16291630GlobalStatus GS;16311632if (GlobalStatus::analyzeGlobal(&GV, GS))1633return false;16341635bool Changed = false;1636if (!GS.IsCompared && !GV.hasGlobalUnnamedAddr()) {1637auto NewUnnamedAddr = GV.hasLocalLinkage() ? GlobalValue::UnnamedAddr::Global1638: GlobalValue::UnnamedAddr::Local;1639if (NewUnnamedAddr != GV.getUnnamedAddr()) {1640GV.setUnnamedAddr(NewUnnamedAddr);1641NumUnnamed++;1642Changed = true;1643}1644}16451646// Do more involved optimizations if the global is internal.1647if (!GV.hasLocalLinkage())1648return Changed;16491650auto *GVar = dyn_cast<GlobalVariable>(&GV);1651if (!GVar)1652return Changed;16531654if (GVar->isConstant() || !GVar->hasInitializer())1655return Changed;16561657return processInternalGlobal(GVar, GS, GetTTI, GetTLI, LookupDomTree) ||1658Changed;1659}16601661/// Walk all of the direct calls of the specified function, changing them to1662/// FastCC.1663static void ChangeCalleesToFastCall(Function *F) {1664for (User *U : F->users()) {1665if (isa<BlockAddress>(U))1666continue;1667cast<CallBase>(U)->setCallingConv(CallingConv::Fast);1668}1669}16701671static AttributeList StripAttr(LLVMContext &C, AttributeList Attrs,1672Attribute::AttrKind A) {1673unsigned AttrIndex;1674if (Attrs.hasAttrSomewhere(A, &AttrIndex))1675return Attrs.removeAttributeAtIndex(C, AttrIndex, A);1676return Attrs;1677}16781679static void RemoveAttribute(Function *F, Attribute::AttrKind A) {1680F->setAttributes(StripAttr(F->getContext(), F->getAttributes(), A));1681for (User *U : F->users()) {1682if (isa<BlockAddress>(U))1683continue;1684CallBase *CB = cast<CallBase>(U);1685CB->setAttributes(StripAttr(F->getContext(), CB->getAttributes(), A));1686}1687}16881689/// Return true if this is a calling convention that we'd like to change. The1690/// idea here is that we don't want to mess with the convention if the user1691/// explicitly requested something with performance implications like coldcc,1692/// GHC, or anyregcc.1693static bool hasChangeableCCImpl(Function *F) {1694CallingConv::ID CC = F->getCallingConv();16951696// FIXME: Is it worth transforming x86_stdcallcc and x86_fastcallcc?1697if (CC != CallingConv::C && CC != CallingConv::X86_ThisCall)1698return false;16991700if (F->isVarArg())1701return false;17021703// FIXME: Change CC for the whole chain of musttail calls when possible.1704//1705// Can't change CC of the function that either has musttail calls, or is a1706// musttail callee itself1707for (User *U : F->users()) {1708if (isa<BlockAddress>(U))1709continue;1710CallInst* CI = dyn_cast<CallInst>(U);1711if (!CI)1712continue;17131714if (CI->isMustTailCall())1715return false;1716}17171718for (BasicBlock &BB : *F)1719if (BB.getTerminatingMustTailCall())1720return false;17211722return !F->hasAddressTaken();1723}17241725using ChangeableCCCacheTy = SmallDenseMap<Function *, bool, 8>;1726static bool hasChangeableCC(Function *F,1727ChangeableCCCacheTy &ChangeableCCCache) {1728auto Res = ChangeableCCCache.try_emplace(F, false);1729if (Res.second)1730Res.first->second = hasChangeableCCImpl(F);1731return Res.first->second;1732}17331734/// Return true if the block containing the call site has a BlockFrequency of1735/// less than ColdCCRelFreq% of the entry block.1736static bool isColdCallSite(CallBase &CB, BlockFrequencyInfo &CallerBFI) {1737const BranchProbability ColdProb(ColdCCRelFreq, 100);1738auto *CallSiteBB = CB.getParent();1739auto CallSiteFreq = CallerBFI.getBlockFreq(CallSiteBB);1740auto CallerEntryFreq =1741CallerBFI.getBlockFreq(&(CB.getCaller()->getEntryBlock()));1742return CallSiteFreq < CallerEntryFreq * ColdProb;1743}17441745// This function checks if the input function F is cold at all call sites. It1746// also looks each call site's containing function, returning false if the1747// caller function contains other non cold calls. The input vector AllCallsCold1748// contains a list of functions that only have call sites in cold blocks.1749static bool1750isValidCandidateForColdCC(Function &F,1751function_ref<BlockFrequencyInfo &(Function &)> GetBFI,1752const std::vector<Function *> &AllCallsCold) {17531754if (F.user_empty())1755return false;17561757for (User *U : F.users()) {1758if (isa<BlockAddress>(U))1759continue;17601761CallBase &CB = cast<CallBase>(*U);1762Function *CallerFunc = CB.getParent()->getParent();1763BlockFrequencyInfo &CallerBFI = GetBFI(*CallerFunc);1764if (!isColdCallSite(CB, CallerBFI))1765return false;1766if (!llvm::is_contained(AllCallsCold, CallerFunc))1767return false;1768}1769return true;1770}17711772static void changeCallSitesToColdCC(Function *F) {1773for (User *U : F->users()) {1774if (isa<BlockAddress>(U))1775continue;1776cast<CallBase>(U)->setCallingConv(CallingConv::Cold);1777}1778}17791780// This function iterates over all the call instructions in the input Function1781// and checks that all call sites are in cold blocks and are allowed to use the1782// coldcc calling convention.1783static bool1784hasOnlyColdCalls(Function &F,1785function_ref<BlockFrequencyInfo &(Function &)> GetBFI,1786ChangeableCCCacheTy &ChangeableCCCache) {1787for (BasicBlock &BB : F) {1788for (Instruction &I : BB) {1789if (CallInst *CI = dyn_cast<CallInst>(&I)) {1790// Skip over isline asm instructions since they aren't function calls.1791if (CI->isInlineAsm())1792continue;1793Function *CalledFn = CI->getCalledFunction();1794if (!CalledFn)1795return false;1796// Skip over intrinsics since they won't remain as function calls.1797// Important to do this check before the linkage check below so we1798// won't bail out on debug intrinsics, possibly making the generated1799// code dependent on the presence of debug info.1800if (CalledFn->getIntrinsicID() != Intrinsic::not_intrinsic)1801continue;1802if (!CalledFn->hasLocalLinkage())1803return false;1804// Check if it's valid to use coldcc calling convention.1805if (!hasChangeableCC(CalledFn, ChangeableCCCache))1806return false;1807BlockFrequencyInfo &CallerBFI = GetBFI(F);1808if (!isColdCallSite(*CI, CallerBFI))1809return false;1810}1811}1812}1813return true;1814}18151816static bool hasMustTailCallers(Function *F) {1817for (User *U : F->users()) {1818CallBase *CB = dyn_cast<CallBase>(U);1819if (!CB) {1820assert(isa<BlockAddress>(U) &&1821"Expected either CallBase or BlockAddress");1822continue;1823}1824if (CB->isMustTailCall())1825return true;1826}1827return false;1828}18291830static bool hasInvokeCallers(Function *F) {1831for (User *U : F->users())1832if (isa<InvokeInst>(U))1833return true;1834return false;1835}18361837static void RemovePreallocated(Function *F) {1838RemoveAttribute(F, Attribute::Preallocated);18391840auto *M = F->getParent();18411842IRBuilder<> Builder(M->getContext());18431844// Cannot modify users() while iterating over it, so make a copy.1845SmallVector<User *, 4> PreallocatedCalls(F->users());1846for (User *U : PreallocatedCalls) {1847CallBase *CB = dyn_cast<CallBase>(U);1848if (!CB)1849continue;18501851assert(1852!CB->isMustTailCall() &&1853"Shouldn't call RemotePreallocated() on a musttail preallocated call");1854// Create copy of call without "preallocated" operand bundle.1855SmallVector<OperandBundleDef, 1> OpBundles;1856CB->getOperandBundlesAsDefs(OpBundles);1857CallBase *PreallocatedSetup = nullptr;1858for (auto *It = OpBundles.begin(); It != OpBundles.end(); ++It) {1859if (It->getTag() == "preallocated") {1860PreallocatedSetup = cast<CallBase>(*It->input_begin());1861OpBundles.erase(It);1862break;1863}1864}1865assert(PreallocatedSetup && "Did not find preallocated bundle");1866uint64_t ArgCount =1867cast<ConstantInt>(PreallocatedSetup->getArgOperand(0))->getZExtValue();18681869assert((isa<CallInst>(CB) || isa<InvokeInst>(CB)) &&1870"Unknown indirect call type");1871CallBase *NewCB = CallBase::Create(CB, OpBundles, CB->getIterator());1872CB->replaceAllUsesWith(NewCB);1873NewCB->takeName(CB);1874CB->eraseFromParent();18751876Builder.SetInsertPoint(PreallocatedSetup);1877auto *StackSave = Builder.CreateStackSave();1878Builder.SetInsertPoint(NewCB->getNextNonDebugInstruction());1879Builder.CreateStackRestore(StackSave);18801881// Replace @llvm.call.preallocated.arg() with alloca.1882// Cannot modify users() while iterating over it, so make a copy.1883// @llvm.call.preallocated.arg() can be called with the same index multiple1884// times. So for each @llvm.call.preallocated.arg(), we see if we have1885// already created a Value* for the index, and if not, create an alloca and1886// bitcast right after the @llvm.call.preallocated.setup() so that it1887// dominates all uses.1888SmallVector<Value *, 2> ArgAllocas(ArgCount);1889SmallVector<User *, 2> PreallocatedArgs(PreallocatedSetup->users());1890for (auto *User : PreallocatedArgs) {1891auto *UseCall = cast<CallBase>(User);1892assert(UseCall->getCalledFunction()->getIntrinsicID() ==1893Intrinsic::call_preallocated_arg &&1894"preallocated token use was not a llvm.call.preallocated.arg");1895uint64_t AllocArgIndex =1896cast<ConstantInt>(UseCall->getArgOperand(1))->getZExtValue();1897Value *AllocaReplacement = ArgAllocas[AllocArgIndex];1898if (!AllocaReplacement) {1899auto AddressSpace = UseCall->getType()->getPointerAddressSpace();1900auto *ArgType =1901UseCall->getFnAttr(Attribute::Preallocated).getValueAsType();1902auto *InsertBefore = PreallocatedSetup->getNextNonDebugInstruction();1903Builder.SetInsertPoint(InsertBefore);1904auto *Alloca =1905Builder.CreateAlloca(ArgType, AddressSpace, nullptr, "paarg");1906ArgAllocas[AllocArgIndex] = Alloca;1907AllocaReplacement = Alloca;1908}19091910UseCall->replaceAllUsesWith(AllocaReplacement);1911UseCall->eraseFromParent();1912}1913// Remove @llvm.call.preallocated.setup().1914cast<Instruction>(PreallocatedSetup)->eraseFromParent();1915}1916}19171918static bool1919OptimizeFunctions(Module &M,1920function_ref<TargetLibraryInfo &(Function &)> GetTLI,1921function_ref<TargetTransformInfo &(Function &)> GetTTI,1922function_ref<BlockFrequencyInfo &(Function &)> GetBFI,1923function_ref<DominatorTree &(Function &)> LookupDomTree,1924SmallPtrSetImpl<const Comdat *> &NotDiscardableComdats,1925function_ref<void(Function &F)> ChangedCFGCallback,1926function_ref<void(Function &F)> DeleteFnCallback) {19271928bool Changed = false;19291930ChangeableCCCacheTy ChangeableCCCache;1931std::vector<Function *> AllCallsCold;1932for (Function &F : llvm::make_early_inc_range(M))1933if (hasOnlyColdCalls(F, GetBFI, ChangeableCCCache))1934AllCallsCold.push_back(&F);19351936// Optimize functions.1937for (Function &F : llvm::make_early_inc_range(M)) {1938// Don't perform global opt pass on naked functions; we don't want fast1939// calling conventions for naked functions.1940if (F.hasFnAttribute(Attribute::Naked))1941continue;19421943// Functions without names cannot be referenced outside this module.1944if (!F.hasName() && !F.isDeclaration() && !F.hasLocalLinkage())1945F.setLinkage(GlobalValue::InternalLinkage);19461947if (deleteIfDead(F, NotDiscardableComdats, DeleteFnCallback)) {1948Changed = true;1949continue;1950}19511952// LLVM's definition of dominance allows instructions that are cyclic1953// in unreachable blocks, e.g.:1954// %pat = select i1 %condition, @global, i16* %pat1955// because any instruction dominates an instruction in a block that's1956// not reachable from entry.1957// So, remove unreachable blocks from the function, because a) there's1958// no point in analyzing them and b) GlobalOpt should otherwise grow1959// some more complicated logic to break these cycles.1960// Notify the analysis manager that we've modified the function's CFG.1961if (!F.isDeclaration()) {1962if (removeUnreachableBlocks(F)) {1963Changed = true;1964ChangedCFGCallback(F);1965}1966}19671968Changed |= processGlobal(F, GetTTI, GetTLI, LookupDomTree);19691970if (!F.hasLocalLinkage())1971continue;19721973// If we have an inalloca parameter that we can safely remove the1974// inalloca attribute from, do so. This unlocks optimizations that1975// wouldn't be safe in the presence of inalloca.1976// FIXME: We should also hoist alloca affected by this to the entry1977// block if possible.1978if (F.getAttributes().hasAttrSomewhere(Attribute::InAlloca) &&1979!F.hasAddressTaken() && !hasMustTailCallers(&F) && !F.isVarArg()) {1980RemoveAttribute(&F, Attribute::InAlloca);1981Changed = true;1982}19831984// FIXME: handle invokes1985// FIXME: handle musttail1986if (F.getAttributes().hasAttrSomewhere(Attribute::Preallocated)) {1987if (!F.hasAddressTaken() && !hasMustTailCallers(&F) &&1988!hasInvokeCallers(&F)) {1989RemovePreallocated(&F);1990Changed = true;1991}1992continue;1993}19941995if (hasChangeableCC(&F, ChangeableCCCache)) {1996NumInternalFunc++;1997TargetTransformInfo &TTI = GetTTI(F);1998// Change the calling convention to coldcc if either stress testing is1999// enabled or the target would like to use coldcc on functions which are2000// cold at all call sites and the callers contain no other non coldcc2001// calls.2002if (EnableColdCCStressTest ||2003(TTI.useColdCCForColdCall(F) &&2004isValidCandidateForColdCC(F, GetBFI, AllCallsCold))) {2005ChangeableCCCache.erase(&F);2006F.setCallingConv(CallingConv::Cold);2007changeCallSitesToColdCC(&F);2008Changed = true;2009NumColdCC++;2010}2011}20122013if (hasChangeableCC(&F, ChangeableCCCache)) {2014// If this function has a calling convention worth changing, is not a2015// varargs function, and is only called directly, promote it to use the2016// Fast calling convention.2017F.setCallingConv(CallingConv::Fast);2018ChangeCalleesToFastCall(&F);2019++NumFastCallFns;2020Changed = true;2021}20222023if (F.getAttributes().hasAttrSomewhere(Attribute::Nest) &&2024!F.hasAddressTaken()) {2025// The function is not used by a trampoline intrinsic, so it is safe2026// to remove the 'nest' attribute.2027RemoveAttribute(&F, Attribute::Nest);2028++NumNestRemoved;2029Changed = true;2030}2031}2032return Changed;2033}20342035static bool2036OptimizeGlobalVars(Module &M,2037function_ref<TargetTransformInfo &(Function &)> GetTTI,2038function_ref<TargetLibraryInfo &(Function &)> GetTLI,2039function_ref<DominatorTree &(Function &)> LookupDomTree,2040SmallPtrSetImpl<const Comdat *> &NotDiscardableComdats) {2041bool Changed = false;20422043for (GlobalVariable &GV : llvm::make_early_inc_range(M.globals())) {2044// Global variables without names cannot be referenced outside this module.2045if (!GV.hasName() && !GV.isDeclaration() && !GV.hasLocalLinkage())2046GV.setLinkage(GlobalValue::InternalLinkage);2047// Simplify the initializer.2048if (GV.hasInitializer())2049if (auto *C = dyn_cast<Constant>(GV.getInitializer())) {2050auto &DL = M.getDataLayout();2051// TLI is not used in the case of a Constant, so use default nullptr2052// for that optional parameter, since we don't have a Function to2053// provide GetTLI anyway.2054Constant *New = ConstantFoldConstant(C, DL, /*TLI*/ nullptr);2055if (New != C)2056GV.setInitializer(New);2057}20582059if (deleteIfDead(GV, NotDiscardableComdats)) {2060Changed = true;2061continue;2062}20632064Changed |= processGlobal(GV, GetTTI, GetTLI, LookupDomTree);2065}2066return Changed;2067}20682069/// Evaluate static constructors in the function, if we can. Return true if we2070/// can, false otherwise.2071static bool EvaluateStaticConstructor(Function *F, const DataLayout &DL,2072TargetLibraryInfo *TLI) {2073// Skip external functions.2074if (F->isDeclaration())2075return false;2076// Call the function.2077Evaluator Eval(DL, TLI);2078Constant *RetValDummy;2079bool EvalSuccess = Eval.EvaluateFunction(F, RetValDummy,2080SmallVector<Constant*, 0>());20812082if (EvalSuccess) {2083++NumCtorsEvaluated;20842085// We succeeded at evaluation: commit the result.2086auto NewInitializers = Eval.getMutatedInitializers();2087LLVM_DEBUG(dbgs() << "FULLY EVALUATED GLOBAL CTOR FUNCTION '"2088<< F->getName() << "' to " << NewInitializers.size()2089<< " stores.\n");2090for (const auto &Pair : NewInitializers)2091Pair.first->setInitializer(Pair.second);2092for (GlobalVariable *GV : Eval.getInvariants())2093GV->setConstant(true);2094}20952096return EvalSuccess;2097}20982099static int compareNames(Constant *const *A, Constant *const *B) {2100Value *AStripped = (*A)->stripPointerCasts();2101Value *BStripped = (*B)->stripPointerCasts();2102return AStripped->getName().compare(BStripped->getName());2103}21042105static void setUsedInitializer(GlobalVariable &V,2106const SmallPtrSetImpl<GlobalValue *> &Init) {2107if (Init.empty()) {2108V.eraseFromParent();2109return;2110}21112112// Get address space of pointers in the array of pointers.2113const Type *UsedArrayType = V.getValueType();2114const auto *VAT = cast<ArrayType>(UsedArrayType);2115const auto *VEPT = cast<PointerType>(VAT->getArrayElementType());21162117// Type of pointer to the array of pointers.2118PointerType *PtrTy =2119PointerType::get(V.getContext(), VEPT->getAddressSpace());21202121SmallVector<Constant *, 8> UsedArray;2122for (GlobalValue *GV : Init) {2123Constant *Cast = ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, PtrTy);2124UsedArray.push_back(Cast);2125}21262127// Sort to get deterministic order.2128array_pod_sort(UsedArray.begin(), UsedArray.end(), compareNames);2129ArrayType *ATy = ArrayType::get(PtrTy, UsedArray.size());21302131Module *M = V.getParent();2132V.removeFromParent();2133GlobalVariable *NV =2134new GlobalVariable(*M, ATy, false, GlobalValue::AppendingLinkage,2135ConstantArray::get(ATy, UsedArray), "");2136NV->takeName(&V);2137NV->setSection("llvm.metadata");2138delete &V;2139}21402141namespace {21422143/// An easy to access representation of llvm.used and llvm.compiler.used.2144class LLVMUsed {2145SmallPtrSet<GlobalValue *, 4> Used;2146SmallPtrSet<GlobalValue *, 4> CompilerUsed;2147GlobalVariable *UsedV;2148GlobalVariable *CompilerUsedV;21492150public:2151LLVMUsed(Module &M) {2152SmallVector<GlobalValue *, 4> Vec;2153UsedV = collectUsedGlobalVariables(M, Vec, false);2154Used = {Vec.begin(), Vec.end()};2155Vec.clear();2156CompilerUsedV = collectUsedGlobalVariables(M, Vec, true);2157CompilerUsed = {Vec.begin(), Vec.end()};2158}21592160using iterator = SmallPtrSet<GlobalValue *, 4>::iterator;2161using used_iterator_range = iterator_range<iterator>;21622163iterator usedBegin() { return Used.begin(); }2164iterator usedEnd() { return Used.end(); }21652166used_iterator_range used() {2167return used_iterator_range(usedBegin(), usedEnd());2168}21692170iterator compilerUsedBegin() { return CompilerUsed.begin(); }2171iterator compilerUsedEnd() { return CompilerUsed.end(); }21722173used_iterator_range compilerUsed() {2174return used_iterator_range(compilerUsedBegin(), compilerUsedEnd());2175}21762177bool usedCount(GlobalValue *GV) const { return Used.count(GV); }21782179bool compilerUsedCount(GlobalValue *GV) const {2180return CompilerUsed.count(GV);2181}21822183bool usedErase(GlobalValue *GV) { return Used.erase(GV); }2184bool compilerUsedErase(GlobalValue *GV) { return CompilerUsed.erase(GV); }2185bool usedInsert(GlobalValue *GV) { return Used.insert(GV).second; }21862187bool compilerUsedInsert(GlobalValue *GV) {2188return CompilerUsed.insert(GV).second;2189}21902191void syncVariablesAndSets() {2192if (UsedV)2193setUsedInitializer(*UsedV, Used);2194if (CompilerUsedV)2195setUsedInitializer(*CompilerUsedV, CompilerUsed);2196}2197};21982199} // end anonymous namespace22002201static bool hasUseOtherThanLLVMUsed(GlobalAlias &GA, const LLVMUsed &U) {2202if (GA.use_empty()) // No use at all.2203return false;22042205assert((!U.usedCount(&GA) || !U.compilerUsedCount(&GA)) &&2206"We should have removed the duplicated "2207"element from llvm.compiler.used");2208if (!GA.hasOneUse())2209// Strictly more than one use. So at least one is not in llvm.used and2210// llvm.compiler.used.2211return true;22122213// Exactly one use. Check if it is in llvm.used or llvm.compiler.used.2214return !U.usedCount(&GA) && !U.compilerUsedCount(&GA);2215}22162217static bool mayHaveOtherReferences(GlobalValue &GV, const LLVMUsed &U) {2218if (!GV.hasLocalLinkage())2219return true;22202221return U.usedCount(&GV) || U.compilerUsedCount(&GV);2222}22232224static bool hasUsesToReplace(GlobalAlias &GA, const LLVMUsed &U,2225bool &RenameTarget) {2226if (GA.isWeakForLinker())2227return false;22282229RenameTarget = false;2230bool Ret = false;2231if (hasUseOtherThanLLVMUsed(GA, U))2232Ret = true;22332234// If the alias is externally visible, we may still be able to simplify it.2235if (!mayHaveOtherReferences(GA, U))2236return Ret;22372238// If the aliasee has internal linkage and no other references (e.g.,2239// @llvm.used, @llvm.compiler.used), give it the name and linkage of the2240// alias, and delete the alias. This turns:2241// define internal ... @f(...)2242// @a = alias ... @f2243// into:2244// define ... @a(...)2245Constant *Aliasee = GA.getAliasee();2246GlobalValue *Target = cast<GlobalValue>(Aliasee->stripPointerCasts());2247if (mayHaveOtherReferences(*Target, U))2248return Ret;22492250RenameTarget = true;2251return true;2252}22532254static bool2255OptimizeGlobalAliases(Module &M,2256SmallPtrSetImpl<const Comdat *> &NotDiscardableComdats) {2257bool Changed = false;2258LLVMUsed Used(M);22592260for (GlobalValue *GV : Used.used())2261Used.compilerUsedErase(GV);22622263// Return whether GV is explicitly or implicitly dso_local and not replaceable2264// by another definition in the current linkage unit.2265auto IsModuleLocal = [](GlobalValue &GV) {2266return !GlobalValue::isInterposableLinkage(GV.getLinkage()) &&2267(GV.isDSOLocal() || GV.isImplicitDSOLocal());2268};22692270for (GlobalAlias &J : llvm::make_early_inc_range(M.aliases())) {2271// Aliases without names cannot be referenced outside this module.2272if (!J.hasName() && !J.isDeclaration() && !J.hasLocalLinkage())2273J.setLinkage(GlobalValue::InternalLinkage);22742275if (deleteIfDead(J, NotDiscardableComdats)) {2276Changed = true;2277continue;2278}22792280// If the alias can change at link time, nothing can be done - bail out.2281if (!IsModuleLocal(J))2282continue;22832284Constant *Aliasee = J.getAliasee();2285GlobalValue *Target = dyn_cast<GlobalValue>(Aliasee->stripPointerCasts());2286// We can't trivially replace the alias with the aliasee if the aliasee is2287// non-trivial in some way. We also can't replace the alias with the aliasee2288// if the aliasee may be preemptible at runtime. On ELF, a non-preemptible2289// alias can be used to access the definition as if preemption did not2290// happen.2291// TODO: Try to handle non-zero GEPs of local aliasees.2292if (!Target || !IsModuleLocal(*Target))2293continue;22942295Target->removeDeadConstantUsers();22962297// Make all users of the alias use the aliasee instead.2298bool RenameTarget;2299if (!hasUsesToReplace(J, Used, RenameTarget))2300continue;23012302J.replaceAllUsesWith(Aliasee);2303++NumAliasesResolved;2304Changed = true;23052306if (RenameTarget) {2307// Give the aliasee the name, linkage and other attributes of the alias.2308Target->takeName(&J);2309Target->setLinkage(J.getLinkage());2310Target->setDSOLocal(J.isDSOLocal());2311Target->setVisibility(J.getVisibility());2312Target->setDLLStorageClass(J.getDLLStorageClass());23132314if (Used.usedErase(&J))2315Used.usedInsert(Target);23162317if (Used.compilerUsedErase(&J))2318Used.compilerUsedInsert(Target);2319} else if (mayHaveOtherReferences(J, Used))2320continue;23212322// Delete the alias.2323M.eraseAlias(&J);2324++NumAliasesRemoved;2325Changed = true;2326}23272328Used.syncVariablesAndSets();23292330return Changed;2331}23322333static Function *2334FindAtExitLibFunc(Module &M,2335function_ref<TargetLibraryInfo &(Function &)> GetTLI,2336LibFunc Func) {2337// Hack to get a default TLI before we have actual Function.2338auto FuncIter = M.begin();2339if (FuncIter == M.end())2340return nullptr;2341auto *TLI = &GetTLI(*FuncIter);23422343if (!TLI->has(Func))2344return nullptr;23452346Function *Fn = M.getFunction(TLI->getName(Func));2347if (!Fn)2348return nullptr;23492350// Now get the actual TLI for Fn.2351TLI = &GetTLI(*Fn);23522353// Make sure that the function has the correct prototype.2354LibFunc F;2355if (!TLI->getLibFunc(*Fn, F) || F != Func)2356return nullptr;23572358return Fn;2359}23602361/// Returns whether the given function is an empty C++ destructor or atexit2362/// handler and can therefore be eliminated. Note that we assume that other2363/// optimization passes have already simplified the code so we simply check for2364/// 'ret'.2365static bool IsEmptyAtExitFunction(const Function &Fn) {2366// FIXME: We could eliminate C++ destructors if they're readonly/readnone and2367// nounwind, but that doesn't seem worth doing.2368if (Fn.isDeclaration())2369return false;23702371for (const auto &I : Fn.getEntryBlock()) {2372if (I.isDebugOrPseudoInst())2373continue;2374if (isa<ReturnInst>(I))2375return true;2376break;2377}2378return false;2379}23802381static bool OptimizeEmptyGlobalAtExitDtors(Function *CXAAtExitFn, bool isCXX) {2382/// Itanium C++ ABI p3.3.5:2383///2384/// After constructing a global (or local static) object, that will require2385/// destruction on exit, a termination function is registered as follows:2386///2387/// extern "C" int __cxa_atexit ( void (*f)(void *), void *p, void *d );2388///2389/// This registration, e.g. __cxa_atexit(f,p,d), is intended to cause the2390/// call f(p) when DSO d is unloaded, before all such termination calls2391/// registered before this one. It returns zero if registration is2392/// successful, nonzero on failure.23932394// This pass will look for calls to __cxa_atexit or atexit where the function2395// is trivial and remove them.2396bool Changed = false;23972398for (User *U : llvm::make_early_inc_range(CXAAtExitFn->users())) {2399// We're only interested in calls. Theoretically, we could handle invoke2400// instructions as well, but neither llvm-gcc nor clang generate invokes2401// to __cxa_atexit.2402CallInst *CI = dyn_cast<CallInst>(U);2403if (!CI)2404continue;24052406Function *DtorFn =2407dyn_cast<Function>(CI->getArgOperand(0)->stripPointerCasts());2408if (!DtorFn || !IsEmptyAtExitFunction(*DtorFn))2409continue;24102411// Just remove the call.2412CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));2413CI->eraseFromParent();24142415if (isCXX)2416++NumCXXDtorsRemoved;2417else2418++NumAtExitRemoved;24192420Changed |= true;2421}24222423return Changed;2424}24252426static Function *hasSideeffectFreeStaticResolution(GlobalIFunc &IF) {2427if (IF.isInterposable())2428return nullptr;24292430Function *Resolver = IF.getResolverFunction();2431if (!Resolver)2432return nullptr;24332434if (Resolver->isInterposable())2435return nullptr;24362437// Only handle functions that have been optimized into a single basic block.2438auto It = Resolver->begin();2439if (++It != Resolver->end())2440return nullptr;24412442BasicBlock &BB = Resolver->getEntryBlock();24432444if (any_of(BB, [](Instruction &I) { return I.mayHaveSideEffects(); }))2445return nullptr;24462447auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator());2448if (!Ret)2449return nullptr;24502451return dyn_cast<Function>(Ret->getReturnValue());2452}24532454/// Find IFuncs that have resolvers that always point at the same statically2455/// known callee, and replace their callers with a direct call.2456static bool OptimizeStaticIFuncs(Module &M) {2457bool Changed = false;2458for (GlobalIFunc &IF : M.ifuncs())2459if (Function *Callee = hasSideeffectFreeStaticResolution(IF))2460if (!IF.use_empty() &&2461(!Callee->isDeclaration() ||2462none_of(IF.users(), [](User *U) { return isa<GlobalAlias>(U); }))) {2463IF.replaceAllUsesWith(Callee);2464NumIFuncsResolved++;2465Changed = true;2466}2467return Changed;2468}24692470static bool2471DeleteDeadIFuncs(Module &M,2472SmallPtrSetImpl<const Comdat *> &NotDiscardableComdats) {2473bool Changed = false;2474for (GlobalIFunc &IF : make_early_inc_range(M.ifuncs()))2475if (deleteIfDead(IF, NotDiscardableComdats)) {2476NumIFuncsDeleted++;2477Changed = true;2478}2479return Changed;2480}24812482static bool2483optimizeGlobalsInModule(Module &M, const DataLayout &DL,2484function_ref<TargetLibraryInfo &(Function &)> GetTLI,2485function_ref<TargetTransformInfo &(Function &)> GetTTI,2486function_ref<BlockFrequencyInfo &(Function &)> GetBFI,2487function_ref<DominatorTree &(Function &)> LookupDomTree,2488function_ref<void(Function &F)> ChangedCFGCallback,2489function_ref<void(Function &F)> DeleteFnCallback) {2490SmallPtrSet<const Comdat *, 8> NotDiscardableComdats;2491bool Changed = false;2492bool LocalChange = true;2493std::optional<uint32_t> FirstNotFullyEvaluatedPriority;24942495while (LocalChange) {2496LocalChange = false;24972498NotDiscardableComdats.clear();2499for (const GlobalVariable &GV : M.globals())2500if (const Comdat *C = GV.getComdat())2501if (!GV.isDiscardableIfUnused() || !GV.use_empty())2502NotDiscardableComdats.insert(C);2503for (Function &F : M)2504if (const Comdat *C = F.getComdat())2505if (!F.isDefTriviallyDead())2506NotDiscardableComdats.insert(C);2507for (GlobalAlias &GA : M.aliases())2508if (const Comdat *C = GA.getComdat())2509if (!GA.isDiscardableIfUnused() || !GA.use_empty())2510NotDiscardableComdats.insert(C);25112512// Delete functions that are trivially dead, ccc -> fastcc2513LocalChange |= OptimizeFunctions(M, GetTLI, GetTTI, GetBFI, LookupDomTree,2514NotDiscardableComdats, ChangedCFGCallback,2515DeleteFnCallback);25162517// Optimize global_ctors list.2518LocalChange |=2519optimizeGlobalCtorsList(M, [&](uint32_t Priority, Function *F) {2520if (FirstNotFullyEvaluatedPriority &&2521*FirstNotFullyEvaluatedPriority != Priority)2522return false;2523bool Evaluated = EvaluateStaticConstructor(F, DL, &GetTLI(*F));2524if (!Evaluated)2525FirstNotFullyEvaluatedPriority = Priority;2526return Evaluated;2527});25282529// Optimize non-address-taken globals.2530LocalChange |= OptimizeGlobalVars(M, GetTTI, GetTLI, LookupDomTree,2531NotDiscardableComdats);25322533// Resolve aliases, when possible.2534LocalChange |= OptimizeGlobalAliases(M, NotDiscardableComdats);25352536// Try to remove trivial global destructors if they are not removed2537// already.2538if (Function *CXAAtExitFn =2539FindAtExitLibFunc(M, GetTLI, LibFunc_cxa_atexit))2540LocalChange |= OptimizeEmptyGlobalAtExitDtors(CXAAtExitFn, true);25412542if (Function *AtExitFn = FindAtExitLibFunc(M, GetTLI, LibFunc_atexit))2543LocalChange |= OptimizeEmptyGlobalAtExitDtors(AtExitFn, false);25442545// Optimize IFuncs whose callee's are statically known.2546LocalChange |= OptimizeStaticIFuncs(M);25472548// Remove any IFuncs that are now dead.2549LocalChange |= DeleteDeadIFuncs(M, NotDiscardableComdats);25502551Changed |= LocalChange;2552}25532554// TODO: Move all global ctors functions to the end of the module for code2555// layout.25562557return Changed;2558}25592560PreservedAnalyses GlobalOptPass::run(Module &M, ModuleAnalysisManager &AM) {2561auto &DL = M.getDataLayout();2562auto &FAM =2563AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();2564auto LookupDomTree = [&FAM](Function &F) -> DominatorTree &{2565return FAM.getResult<DominatorTreeAnalysis>(F);2566};2567auto GetTLI = [&FAM](Function &F) -> TargetLibraryInfo & {2568return FAM.getResult<TargetLibraryAnalysis>(F);2569};2570auto GetTTI = [&FAM](Function &F) -> TargetTransformInfo & {2571return FAM.getResult<TargetIRAnalysis>(F);2572};25732574auto GetBFI = [&FAM](Function &F) -> BlockFrequencyInfo & {2575return FAM.getResult<BlockFrequencyAnalysis>(F);2576};2577auto ChangedCFGCallback = [&FAM](Function &F) {2578FAM.invalidate(F, PreservedAnalyses::none());2579};2580auto DeleteFnCallback = [&FAM](Function &F) { FAM.clear(F, F.getName()); };25812582if (!optimizeGlobalsInModule(M, DL, GetTLI, GetTTI, GetBFI, LookupDomTree,2583ChangedCFGCallback, DeleteFnCallback))2584return PreservedAnalyses::all();25852586PreservedAnalyses PA = PreservedAnalyses::none();2587// We made sure to clear analyses for deleted functions.2588PA.preserve<FunctionAnalysisManagerModuleProxy>();2589// The only place we modify the CFG is when calling2590// removeUnreachableBlocks(), but there we make sure to invalidate analyses2591// for modified functions.2592PA.preserveSet<CFGAnalyses>();2593return PA;2594}259525962597