Path: blob/main/contrib/llvm-project/llvm/lib/Analysis/GlobalsModRef.cpp
35233 views
//===- GlobalsModRef.cpp - Simple Mod/Ref Analysis for Globals ------------===//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 simple pass provides alias and mod/ref information for global values9// that do not have their address taken, and keeps track of whether functions10// read or write memory (are "pure"). For this simple (but very common) case,11// we can provide pretty accurate and useful information.12//13//===----------------------------------------------------------------------===//1415#include "llvm/Analysis/GlobalsModRef.h"16#include "llvm/ADT/SCCIterator.h"17#include "llvm/ADT/SmallPtrSet.h"18#include "llvm/ADT/Statistic.h"19#include "llvm/Analysis/CallGraph.h"20#include "llvm/Analysis/MemoryBuiltins.h"21#include "llvm/Analysis/TargetLibraryInfo.h"22#include "llvm/Analysis/ValueTracking.h"23#include "llvm/IR/InstIterator.h"24#include "llvm/IR/Instructions.h"25#include "llvm/IR/Module.h"26#include "llvm/IR/PassManager.h"27#include "llvm/InitializePasses.h"28#include "llvm/Pass.h"29#include "llvm/Support/CommandLine.h"3031using namespace llvm;3233#define DEBUG_TYPE "globalsmodref-aa"3435STATISTIC(NumNonAddrTakenGlobalVars,36"Number of global vars without address taken");37STATISTIC(NumNonAddrTakenFunctions,"Number of functions without address taken");38STATISTIC(NumNoMemFunctions, "Number of functions that do not access memory");39STATISTIC(NumReadMemFunctions, "Number of functions that only read memory");40STATISTIC(NumIndirectGlobalVars, "Number of indirect global objects");4142// An option to enable unsafe alias results from the GlobalsModRef analysis.43// When enabled, GlobalsModRef will provide no-alias results which in extremely44// rare cases may not be conservatively correct. In particular, in the face of45// transforms which cause asymmetry between how effective getUnderlyingObject46// is for two pointers, it may produce incorrect results.47//48// These unsafe results have been returned by GMR for many years without49// causing significant issues in the wild and so we provide a mechanism to50// re-enable them for users of LLVM that have a particular performance51// sensitivity and no known issues. The option also makes it easy to evaluate52// the performance impact of these results.53static cl::opt<bool> EnableUnsafeGlobalsModRefAliasResults(54"enable-unsafe-globalsmodref-alias-results", cl::init(false), cl::Hidden);5556/// The mod/ref information collected for a particular function.57///58/// We collect information about mod/ref behavior of a function here, both in59/// general and as pertains to specific globals. We only have this detailed60/// information when we know *something* useful about the behavior. If we61/// saturate to fully general mod/ref, we remove the info for the function.62class GlobalsAAResult::FunctionInfo {63typedef SmallDenseMap<const GlobalValue *, ModRefInfo, 16> GlobalInfoMapType;6465/// Build a wrapper struct that has 8-byte alignment. All heap allocations66/// should provide this much alignment at least, but this makes it clear we67/// specifically rely on this amount of alignment.68struct alignas(8) AlignedMap {69AlignedMap() = default;70AlignedMap(const AlignedMap &Arg) = default;71GlobalInfoMapType Map;72};7374/// Pointer traits for our aligned map.75struct AlignedMapPointerTraits {76static inline void *getAsVoidPointer(AlignedMap *P) { return P; }77static inline AlignedMap *getFromVoidPointer(void *P) {78return (AlignedMap *)P;79}80static constexpr int NumLowBitsAvailable = 3;81static_assert(alignof(AlignedMap) >= (1 << NumLowBitsAvailable),82"AlignedMap insufficiently aligned to have enough low bits.");83};8485/// The bit that flags that this function may read any global. This is86/// chosen to mix together with ModRefInfo bits.87/// FIXME: This assumes ModRefInfo lattice will remain 4 bits!88/// FunctionInfo.getModRefInfo() masks out everything except ModRef so89/// this remains correct.90enum { MayReadAnyGlobal = 4 };9192/// Checks to document the invariants of the bit packing here.93static_assert((MayReadAnyGlobal & static_cast<int>(ModRefInfo::ModRef)) == 0,94"ModRef and the MayReadAnyGlobal flag bits overlap.");95static_assert(((MayReadAnyGlobal | static_cast<int>(ModRefInfo::ModRef)) >>96AlignedMapPointerTraits::NumLowBitsAvailable) == 0,97"Insufficient low bits to store our flag and ModRef info.");9899public:100FunctionInfo() = default;101~FunctionInfo() {102delete Info.getPointer();103}104// Spell out the copy ond move constructors and assignment operators to get105// deep copy semantics and correct move semantics in the face of the106// pointer-int pair.107FunctionInfo(const FunctionInfo &Arg)108: Info(nullptr, Arg.Info.getInt()) {109if (const auto *ArgPtr = Arg.Info.getPointer())110Info.setPointer(new AlignedMap(*ArgPtr));111}112FunctionInfo(FunctionInfo &&Arg)113: Info(Arg.Info.getPointer(), Arg.Info.getInt()) {114Arg.Info.setPointerAndInt(nullptr, 0);115}116FunctionInfo &operator=(const FunctionInfo &RHS) {117delete Info.getPointer();118Info.setPointerAndInt(nullptr, RHS.Info.getInt());119if (const auto *RHSPtr = RHS.Info.getPointer())120Info.setPointer(new AlignedMap(*RHSPtr));121return *this;122}123FunctionInfo &operator=(FunctionInfo &&RHS) {124delete Info.getPointer();125Info.setPointerAndInt(RHS.Info.getPointer(), RHS.Info.getInt());126RHS.Info.setPointerAndInt(nullptr, 0);127return *this;128}129130/// This method clears MayReadAnyGlobal bit added by GlobalsAAResult to return131/// the corresponding ModRefInfo.132ModRefInfo globalClearMayReadAnyGlobal(int I) const {133return ModRefInfo(I & static_cast<int>(ModRefInfo::ModRef));134}135136/// Returns the \c ModRefInfo info for this function.137ModRefInfo getModRefInfo() const {138return globalClearMayReadAnyGlobal(Info.getInt());139}140141/// Adds new \c ModRefInfo for this function to its state.142void addModRefInfo(ModRefInfo NewMRI) {143Info.setInt(Info.getInt() | static_cast<int>(NewMRI));144}145146/// Returns whether this function may read any global variable, and we don't147/// know which global.148bool mayReadAnyGlobal() const { return Info.getInt() & MayReadAnyGlobal; }149150/// Sets this function as potentially reading from any global.151void setMayReadAnyGlobal() { Info.setInt(Info.getInt() | MayReadAnyGlobal); }152153/// Returns the \c ModRefInfo info for this function w.r.t. a particular154/// global, which may be more precise than the general information above.155ModRefInfo getModRefInfoForGlobal(const GlobalValue &GV) const {156ModRefInfo GlobalMRI =157mayReadAnyGlobal() ? ModRefInfo::Ref : ModRefInfo::NoModRef;158if (AlignedMap *P = Info.getPointer()) {159auto I = P->Map.find(&GV);160if (I != P->Map.end())161GlobalMRI |= I->second;162}163return GlobalMRI;164}165166/// Add mod/ref info from another function into ours, saturating towards167/// ModRef.168void addFunctionInfo(const FunctionInfo &FI) {169addModRefInfo(FI.getModRefInfo());170171if (FI.mayReadAnyGlobal())172setMayReadAnyGlobal();173174if (AlignedMap *P = FI.Info.getPointer())175for (const auto &G : P->Map)176addModRefInfoForGlobal(*G.first, G.second);177}178179void addModRefInfoForGlobal(const GlobalValue &GV, ModRefInfo NewMRI) {180AlignedMap *P = Info.getPointer();181if (!P) {182P = new AlignedMap();183Info.setPointer(P);184}185auto &GlobalMRI = P->Map[&GV];186GlobalMRI |= NewMRI;187}188189/// Clear a global's ModRef info. Should be used when a global is being190/// deleted.191void eraseModRefInfoForGlobal(const GlobalValue &GV) {192if (AlignedMap *P = Info.getPointer())193P->Map.erase(&GV);194}195196private:197/// All of the information is encoded into a single pointer, with a three bit198/// integer in the low three bits. The high bit provides a flag for when this199/// function may read any global. The low two bits are the ModRefInfo. And200/// the pointer, when non-null, points to a map from GlobalValue to201/// ModRefInfo specific to that GlobalValue.202PointerIntPair<AlignedMap *, 3, unsigned, AlignedMapPointerTraits> Info;203};204205void GlobalsAAResult::DeletionCallbackHandle::deleted() {206Value *V = getValPtr();207if (auto *F = dyn_cast<Function>(V))208GAR->FunctionInfos.erase(F);209210if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {211if (GAR->NonAddressTakenGlobals.erase(GV)) {212// This global might be an indirect global. If so, remove it and213// remove any AllocRelatedValues for it.214if (GAR->IndirectGlobals.erase(GV)) {215// Remove any entries in AllocsForIndirectGlobals for this global.216for (auto I = GAR->AllocsForIndirectGlobals.begin(),217E = GAR->AllocsForIndirectGlobals.end();218I != E; ++I)219if (I->second == GV)220GAR->AllocsForIndirectGlobals.erase(I);221}222223// Scan the function info we have collected and remove this global224// from all of them.225for (auto &FIPair : GAR->FunctionInfos)226FIPair.second.eraseModRefInfoForGlobal(*GV);227}228}229230// If this is an allocation related to an indirect global, remove it.231GAR->AllocsForIndirectGlobals.erase(V);232233// And clear out the handle.234setValPtr(nullptr);235GAR->Handles.erase(I);236// This object is now destroyed!237}238239MemoryEffects GlobalsAAResult::getMemoryEffects(const Function *F) {240if (FunctionInfo *FI = getFunctionInfo(F))241return MemoryEffects(FI->getModRefInfo());242243return MemoryEffects::unknown();244}245246/// Returns the function info for the function, or null if we don't have247/// anything useful to say about it.248GlobalsAAResult::FunctionInfo *249GlobalsAAResult::getFunctionInfo(const Function *F) {250auto I = FunctionInfos.find(F);251if (I != FunctionInfos.end())252return &I->second;253return nullptr;254}255256/// AnalyzeGlobals - Scan through the users of all of the internal257/// GlobalValue's in the program. If none of them have their "address taken"258/// (really, their address passed to something nontrivial), record this fact,259/// and record the functions that they are used directly in.260void GlobalsAAResult::AnalyzeGlobals(Module &M) {261SmallPtrSet<Function *, 32> TrackedFunctions;262for (Function &F : M)263if (F.hasLocalLinkage()) {264if (!AnalyzeUsesOfPointer(&F)) {265// Remember that we are tracking this global.266NonAddressTakenGlobals.insert(&F);267TrackedFunctions.insert(&F);268Handles.emplace_front(*this, &F);269Handles.front().I = Handles.begin();270++NumNonAddrTakenFunctions;271} else272UnknownFunctionsWithLocalLinkage = true;273}274275SmallPtrSet<Function *, 16> Readers, Writers;276for (GlobalVariable &GV : M.globals())277if (GV.hasLocalLinkage()) {278if (!AnalyzeUsesOfPointer(&GV, &Readers,279GV.isConstant() ? nullptr : &Writers)) {280// Remember that we are tracking this global, and the mod/ref fns281NonAddressTakenGlobals.insert(&GV);282Handles.emplace_front(*this, &GV);283Handles.front().I = Handles.begin();284285for (Function *Reader : Readers) {286if (TrackedFunctions.insert(Reader).second) {287Handles.emplace_front(*this, Reader);288Handles.front().I = Handles.begin();289}290FunctionInfos[Reader].addModRefInfoForGlobal(GV, ModRefInfo::Ref);291}292293if (!GV.isConstant()) // No need to keep track of writers to constants294for (Function *Writer : Writers) {295if (TrackedFunctions.insert(Writer).second) {296Handles.emplace_front(*this, Writer);297Handles.front().I = Handles.begin();298}299FunctionInfos[Writer].addModRefInfoForGlobal(GV, ModRefInfo::Mod);300}301++NumNonAddrTakenGlobalVars;302303// If this global holds a pointer type, see if it is an indirect global.304if (GV.getValueType()->isPointerTy() &&305AnalyzeIndirectGlobalMemory(&GV))306++NumIndirectGlobalVars;307}308Readers.clear();309Writers.clear();310}311}312313/// AnalyzeUsesOfPointer - Look at all of the users of the specified pointer.314/// If this is used by anything complex (i.e., the address escapes), return315/// true. Also, while we are at it, keep track of those functions that read and316/// write to the value.317///318/// If OkayStoreDest is non-null, stores into this global are allowed.319bool GlobalsAAResult::AnalyzeUsesOfPointer(Value *V,320SmallPtrSetImpl<Function *> *Readers,321SmallPtrSetImpl<Function *> *Writers,322GlobalValue *OkayStoreDest) {323if (!V->getType()->isPointerTy())324return true;325326for (Use &U : V->uses()) {327User *I = U.getUser();328if (LoadInst *LI = dyn_cast<LoadInst>(I)) {329if (Readers)330Readers->insert(LI->getParent()->getParent());331} else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {332if (V == SI->getOperand(1)) {333if (Writers)334Writers->insert(SI->getParent()->getParent());335} else if (SI->getOperand(1) != OkayStoreDest) {336return true; // Storing the pointer337}338} else if (Operator::getOpcode(I) == Instruction::GetElementPtr) {339if (AnalyzeUsesOfPointer(I, Readers, Writers))340return true;341} else if (Operator::getOpcode(I) == Instruction::BitCast ||342Operator::getOpcode(I) == Instruction::AddrSpaceCast) {343if (AnalyzeUsesOfPointer(I, Readers, Writers, OkayStoreDest))344return true;345} else if (auto *Call = dyn_cast<CallBase>(I)) {346if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {347if (II->getIntrinsicID() == Intrinsic::threadlocal_address &&348V == II->getArgOperand(0)) {349if (AnalyzeUsesOfPointer(II, Readers, Writers))350return true;351continue;352}353}354// Make sure that this is just the function being called, not that it is355// passing into the function.356if (Call->isDataOperand(&U)) {357// Detect calls to free.358if (Call->isArgOperand(&U) &&359getFreedOperand(Call, &GetTLI(*Call->getFunction())) == U) {360if (Writers)361Writers->insert(Call->getParent()->getParent());362} else {363// In general, we return true for unknown calls, but there are364// some simple checks that we can do for functions that365// will never call back into the module.366auto *F = Call->getCalledFunction();367// TODO: we should be able to remove isDeclaration() check368// and let the function body analysis check for captures,369// and collect the mod-ref effects. This information will370// be later propagated via the call graph.371if (!F || !F->isDeclaration())372return true;373// Note that the NoCallback check here is a little bit too374// conservative. If there are no captures of the global375// in the module, then this call may not be a capture even376// if it does not have NoCallback.377if (!Call->hasFnAttr(Attribute::NoCallback) ||378!Call->isArgOperand(&U) ||379!Call->doesNotCapture(Call->getArgOperandNo(&U)))380return true;381382// Conservatively, assume the call reads and writes the global.383// We could use memory attributes to make it more precise.384if (Readers)385Readers->insert(Call->getParent()->getParent());386if (Writers)387Writers->insert(Call->getParent()->getParent());388}389}390} else if (ICmpInst *ICI = dyn_cast<ICmpInst>(I)) {391if (!isa<ConstantPointerNull>(ICI->getOperand(1)))392return true; // Allow comparison against null.393} else if (Constant *C = dyn_cast<Constant>(I)) {394// Ignore constants which don't have any live uses.395if (isa<GlobalValue>(C) || C->isConstantUsed())396return true;397} else {398return true;399}400}401402return false;403}404405/// AnalyzeIndirectGlobalMemory - We found an non-address-taken global variable406/// which holds a pointer type. See if the global always points to non-aliased407/// heap memory: that is, all initializers of the globals store a value known408/// to be obtained via a noalias return function call which have no other use.409/// Further, all loads out of GV must directly use the memory, not store the410/// pointer somewhere. If this is true, we consider the memory pointed to by411/// GV to be owned by GV and can disambiguate other pointers from it.412bool GlobalsAAResult::AnalyzeIndirectGlobalMemory(GlobalVariable *GV) {413// Keep track of values related to the allocation of the memory, f.e. the414// value produced by the noalias call and any casts.415std::vector<Value *> AllocRelatedValues;416417// If the initializer is a valid pointer, bail.418if (Constant *C = GV->getInitializer())419if (!C->isNullValue())420return false;421422// Walk the user list of the global. If we find anything other than a direct423// load or store, bail out.424for (User *U : GV->users()) {425if (LoadInst *LI = dyn_cast<LoadInst>(U)) {426// The pointer loaded from the global can only be used in simple ways:427// we allow addressing of it and loading storing to it. We do *not* allow428// storing the loaded pointer somewhere else or passing to a function.429if (AnalyzeUsesOfPointer(LI))430return false; // Loaded pointer escapes.431// TODO: Could try some IP mod/ref of the loaded pointer.432} else if (StoreInst *SI = dyn_cast<StoreInst>(U)) {433// Storing the global itself.434if (SI->getOperand(0) == GV)435return false;436437// If storing the null pointer, ignore it.438if (isa<ConstantPointerNull>(SI->getOperand(0)))439continue;440441// Check the value being stored.442Value *Ptr = getUnderlyingObject(SI->getOperand(0));443444if (!isNoAliasCall(Ptr))445return false; // Too hard to analyze.446447// Analyze all uses of the allocation. If any of them are used in a448// non-simple way (e.g. stored to another global) bail out.449if (AnalyzeUsesOfPointer(Ptr, /*Readers*/ nullptr, /*Writers*/ nullptr,450GV))451return false; // Loaded pointer escapes.452453// Remember that this allocation is related to the indirect global.454AllocRelatedValues.push_back(Ptr);455} else {456// Something complex, bail out.457return false;458}459}460461// Okay, this is an indirect global. Remember all of the allocations for462// this global in AllocsForIndirectGlobals.463while (!AllocRelatedValues.empty()) {464AllocsForIndirectGlobals[AllocRelatedValues.back()] = GV;465Handles.emplace_front(*this, AllocRelatedValues.back());466Handles.front().I = Handles.begin();467AllocRelatedValues.pop_back();468}469IndirectGlobals.insert(GV);470Handles.emplace_front(*this, GV);471Handles.front().I = Handles.begin();472return true;473}474475void GlobalsAAResult::CollectSCCMembership(CallGraph &CG) {476// We do a bottom-up SCC traversal of the call graph. In other words, we477// visit all callees before callers (leaf-first).478unsigned SCCID = 0;479for (scc_iterator<CallGraph *> I = scc_begin(&CG); !I.isAtEnd(); ++I) {480const std::vector<CallGraphNode *> &SCC = *I;481assert(!SCC.empty() && "SCC with no functions?");482483for (auto *CGN : SCC)484if (Function *F = CGN->getFunction())485FunctionToSCCMap[F] = SCCID;486++SCCID;487}488}489490/// AnalyzeCallGraph - At this point, we know the functions where globals are491/// immediately stored to and read from. Propagate this information up the call492/// graph to all callers and compute the mod/ref info for all memory for each493/// function.494void GlobalsAAResult::AnalyzeCallGraph(CallGraph &CG, Module &M) {495// We do a bottom-up SCC traversal of the call graph. In other words, we496// visit all callees before callers (leaf-first).497for (scc_iterator<CallGraph *> I = scc_begin(&CG); !I.isAtEnd(); ++I) {498const std::vector<CallGraphNode *> &SCC = *I;499assert(!SCC.empty() && "SCC with no functions?");500501Function *F = SCC[0]->getFunction();502503if (!F || !F->isDefinitionExact()) {504// Calls externally or not exact - can't say anything useful. Remove any505// existing function records (may have been created when scanning506// globals).507for (auto *Node : SCC)508FunctionInfos.erase(Node->getFunction());509continue;510}511512FunctionInfo &FI = FunctionInfos[F];513Handles.emplace_front(*this, F);514Handles.front().I = Handles.begin();515bool KnowNothing = false;516517// Intrinsics, like any other synchronizing function, can make effects518// of other threads visible. Without nosync we know nothing really.519// Similarly, if `nocallback` is missing the function, or intrinsic,520// can call into the module arbitrarily. If both are set the function521// has an effect but will not interact with accesses of internal522// globals inside the module. We are conservative here for optnone523// functions, might not be necessary.524auto MaySyncOrCallIntoModule = [](const Function &F) {525return !F.isDeclaration() || !F.hasNoSync() ||526!F.hasFnAttribute(Attribute::NoCallback);527};528529// Collect the mod/ref properties due to called functions. We only compute530// one mod-ref set.531for (unsigned i = 0, e = SCC.size(); i != e && !KnowNothing; ++i) {532if (!F) {533KnowNothing = true;534break;535}536537if (F->isDeclaration() || F->hasOptNone()) {538// Try to get mod/ref behaviour from function attributes.539if (F->doesNotAccessMemory()) {540// Can't do better than that!541} else if (F->onlyReadsMemory()) {542FI.addModRefInfo(ModRefInfo::Ref);543if (!F->onlyAccessesArgMemory() && MaySyncOrCallIntoModule(*F))544// This function might call back into the module and read a global -545// consider every global as possibly being read by this function.546FI.setMayReadAnyGlobal();547} else {548FI.addModRefInfo(ModRefInfo::ModRef);549if (!F->onlyAccessesArgMemory())550FI.setMayReadAnyGlobal();551if (MaySyncOrCallIntoModule(*F)) {552KnowNothing = true;553break;554}555}556continue;557}558559for (CallGraphNode::iterator CI = SCC[i]->begin(), E = SCC[i]->end();560CI != E && !KnowNothing; ++CI)561if (Function *Callee = CI->second->getFunction()) {562if (FunctionInfo *CalleeFI = getFunctionInfo(Callee)) {563// Propagate function effect up.564FI.addFunctionInfo(*CalleeFI);565} else {566// Can't say anything about it. However, if it is inside our SCC,567// then nothing needs to be done.568CallGraphNode *CalleeNode = CG[Callee];569if (!is_contained(SCC, CalleeNode))570KnowNothing = true;571}572} else {573KnowNothing = true;574}575}576577// If we can't say anything useful about this SCC, remove all SCC functions578// from the FunctionInfos map.579if (KnowNothing) {580for (auto *Node : SCC)581FunctionInfos.erase(Node->getFunction());582continue;583}584585// Scan the function bodies for explicit loads or stores.586for (auto *Node : SCC) {587if (isModAndRefSet(FI.getModRefInfo()))588break; // The mod/ref lattice saturates here.589590// Don't prove any properties based on the implementation of an optnone591// function. Function attributes were already used as a best approximation592// above.593if (Node->getFunction()->hasOptNone())594continue;595596for (Instruction &I : instructions(Node->getFunction())) {597if (isModAndRefSet(FI.getModRefInfo()))598break; // The mod/ref lattice saturates here.599600// We handle calls specially because the graph-relevant aspects are601// handled above.602if (isa<CallBase>(&I))603continue;604605// All non-call instructions we use the primary predicates for whether606// they read or write memory.607if (I.mayReadFromMemory())608FI.addModRefInfo(ModRefInfo::Ref);609if (I.mayWriteToMemory())610FI.addModRefInfo(ModRefInfo::Mod);611}612}613614if (!isModSet(FI.getModRefInfo()))615++NumReadMemFunctions;616if (!isModOrRefSet(FI.getModRefInfo()))617++NumNoMemFunctions;618619// Finally, now that we know the full effect on this SCC, clone the620// information to each function in the SCC.621// FI is a reference into FunctionInfos, so copy it now so that it doesn't622// get invalidated if DenseMap decides to re-hash.623FunctionInfo CachedFI = FI;624for (unsigned i = 1, e = SCC.size(); i != e; ++i)625FunctionInfos[SCC[i]->getFunction()] = CachedFI;626}627}628629// GV is a non-escaping global. V is a pointer address that has been loaded from.630// If we can prove that V must escape, we can conclude that a load from V cannot631// alias GV.632static bool isNonEscapingGlobalNoAliasWithLoad(const GlobalValue *GV,633const Value *V,634int &Depth,635const DataLayout &DL) {636SmallPtrSet<const Value *, 8> Visited;637SmallVector<const Value *, 8> Inputs;638Visited.insert(V);639Inputs.push_back(V);640do {641const Value *Input = Inputs.pop_back_val();642643if (isa<GlobalValue>(Input) || isa<Argument>(Input) || isa<CallInst>(Input) ||644isa<InvokeInst>(Input))645// Arguments to functions or returns from functions are inherently646// escaping, so we can immediately classify those as not aliasing any647// non-addr-taken globals.648//649// (Transitive) loads from a global are also safe - if this aliased650// another global, its address would escape, so no alias.651continue;652653// Recurse through a limited number of selects, loads and PHIs. This is an654// arbitrary depth of 4, lower numbers could be used to fix compile time655// issues if needed, but this is generally expected to be only be important656// for small depths.657if (++Depth > 4)658return false;659660if (auto *LI = dyn_cast<LoadInst>(Input)) {661Inputs.push_back(getUnderlyingObject(LI->getPointerOperand()));662continue;663}664if (auto *SI = dyn_cast<SelectInst>(Input)) {665const Value *LHS = getUnderlyingObject(SI->getTrueValue());666const Value *RHS = getUnderlyingObject(SI->getFalseValue());667if (Visited.insert(LHS).second)668Inputs.push_back(LHS);669if (Visited.insert(RHS).second)670Inputs.push_back(RHS);671continue;672}673if (auto *PN = dyn_cast<PHINode>(Input)) {674for (const Value *Op : PN->incoming_values()) {675Op = getUnderlyingObject(Op);676if (Visited.insert(Op).second)677Inputs.push_back(Op);678}679continue;680}681682return false;683} while (!Inputs.empty());684685// All inputs were known to be no-alias.686return true;687}688689// There are particular cases where we can conclude no-alias between690// a non-addr-taken global and some other underlying object. Specifically,691// a non-addr-taken global is known to not be escaped from any function. It is692// also incorrect for a transformation to introduce an escape of a global in693// a way that is observable when it was not there previously. One function694// being transformed to introduce an escape which could possibly be observed695// (via loading from a global or the return value for example) within another696// function is never safe. If the observation is made through non-atomic697// operations on different threads, it is a data-race and UB. If the698// observation is well defined, by being observed the transformation would have699// changed program behavior by introducing the observed escape, making it an700// invalid transform.701//702// This property does require that transformations which *temporarily* escape703// a global that was not previously escaped, prior to restoring it, cannot rely704// on the results of GMR::alias. This seems a reasonable restriction, although705// currently there is no way to enforce it. There is also no realistic706// optimization pass that would make this mistake. The closest example is707// a transformation pass which does reg2mem of SSA values but stores them into708// global variables temporarily before restoring the global variable's value.709// This could be useful to expose "benign" races for example. However, it seems710// reasonable to require that a pass which introduces escapes of global711// variables in this way to either not trust AA results while the escape is712// active, or to be forced to operate as a module pass that cannot co-exist713// with an alias analysis such as GMR.714bool GlobalsAAResult::isNonEscapingGlobalNoAlias(const GlobalValue *GV,715const Value *V) {716// In order to know that the underlying object cannot alias the717// non-addr-taken global, we must know that it would have to be an escape.718// Thus if the underlying object is a function argument, a load from719// a global, or the return of a function, it cannot alias. We can also720// recurse through PHI nodes and select nodes provided all of their inputs721// resolve to one of these known-escaping roots.722SmallPtrSet<const Value *, 8> Visited;723SmallVector<const Value *, 8> Inputs;724Visited.insert(V);725Inputs.push_back(V);726int Depth = 0;727do {728const Value *Input = Inputs.pop_back_val();729730if (auto *InputGV = dyn_cast<GlobalValue>(Input)) {731// If one input is the very global we're querying against, then we can't732// conclude no-alias.733if (InputGV == GV)734return false;735736// Distinct GlobalVariables never alias, unless overriden or zero-sized.737// FIXME: The condition can be refined, but be conservative for now.738auto *GVar = dyn_cast<GlobalVariable>(GV);739auto *InputGVar = dyn_cast<GlobalVariable>(InputGV);740if (GVar && InputGVar &&741!GVar->isDeclaration() && !InputGVar->isDeclaration() &&742!GVar->isInterposable() && !InputGVar->isInterposable()) {743Type *GVType = GVar->getInitializer()->getType();744Type *InputGVType = InputGVar->getInitializer()->getType();745if (GVType->isSized() && InputGVType->isSized() &&746(DL.getTypeAllocSize(GVType) > 0) &&747(DL.getTypeAllocSize(InputGVType) > 0))748continue;749}750751// Conservatively return false, even though we could be smarter752// (e.g. look through GlobalAliases).753return false;754}755756if (isa<Argument>(Input) || isa<CallInst>(Input) ||757isa<InvokeInst>(Input)) {758// Arguments to functions or returns from functions are inherently759// escaping, so we can immediately classify those as not aliasing any760// non-addr-taken globals.761continue;762}763764// Recurse through a limited number of selects, loads and PHIs. This is an765// arbitrary depth of 4, lower numbers could be used to fix compile time766// issues if needed, but this is generally expected to be only be important767// for small depths.768if (++Depth > 4)769return false;770771if (auto *LI = dyn_cast<LoadInst>(Input)) {772// A pointer loaded from a global would have been captured, and we know773// that the global is non-escaping, so no alias.774const Value *Ptr = getUnderlyingObject(LI->getPointerOperand());775if (isNonEscapingGlobalNoAliasWithLoad(GV, Ptr, Depth, DL))776// The load does not alias with GV.777continue;778// Otherwise, a load could come from anywhere, so bail.779return false;780}781if (auto *SI = dyn_cast<SelectInst>(Input)) {782const Value *LHS = getUnderlyingObject(SI->getTrueValue());783const Value *RHS = getUnderlyingObject(SI->getFalseValue());784if (Visited.insert(LHS).second)785Inputs.push_back(LHS);786if (Visited.insert(RHS).second)787Inputs.push_back(RHS);788continue;789}790if (auto *PN = dyn_cast<PHINode>(Input)) {791for (const Value *Op : PN->incoming_values()) {792Op = getUnderlyingObject(Op);793if (Visited.insert(Op).second)794Inputs.push_back(Op);795}796continue;797}798799// FIXME: It would be good to handle other obvious no-alias cases here, but800// it isn't clear how to do so reasonably without building a small version801// of BasicAA into this code.802return false;803} while (!Inputs.empty());804805// If all the inputs to V were definitively no-alias, then V is no-alias.806return true;807}808809bool GlobalsAAResult::invalidate(Module &, const PreservedAnalyses &PA,810ModuleAnalysisManager::Invalidator &) {811// Check whether the analysis has been explicitly invalidated. Otherwise, it's812// stateless and remains preserved.813auto PAC = PA.getChecker<GlobalsAA>();814return !PAC.preservedWhenStateless();815}816817/// alias - If one of the pointers is to a global that we are tracking, and the818/// other is some random pointer, we know there cannot be an alias, because the819/// address of the global isn't taken.820AliasResult GlobalsAAResult::alias(const MemoryLocation &LocA,821const MemoryLocation &LocB,822AAQueryInfo &AAQI, const Instruction *) {823// Get the base object these pointers point to.824const Value *UV1 =825getUnderlyingObject(LocA.Ptr->stripPointerCastsForAliasAnalysis());826const Value *UV2 =827getUnderlyingObject(LocB.Ptr->stripPointerCastsForAliasAnalysis());828829// If either of the underlying values is a global, they may be non-addr-taken830// globals, which we can answer queries about.831const GlobalValue *GV1 = dyn_cast<GlobalValue>(UV1);832const GlobalValue *GV2 = dyn_cast<GlobalValue>(UV2);833if (GV1 || GV2) {834// If the global's address is taken, pretend we don't know it's a pointer to835// the global.836if (GV1 && !NonAddressTakenGlobals.count(GV1))837GV1 = nullptr;838if (GV2 && !NonAddressTakenGlobals.count(GV2))839GV2 = nullptr;840841// If the two pointers are derived from two different non-addr-taken842// globals we know these can't alias.843if (GV1 && GV2 && GV1 != GV2)844return AliasResult::NoAlias;845846// If one is and the other isn't, it isn't strictly safe but we can fake847// this result if necessary for performance. This does not appear to be848// a common problem in practice.849if (EnableUnsafeGlobalsModRefAliasResults)850if ((GV1 || GV2) && GV1 != GV2)851return AliasResult::NoAlias;852853// Check for a special case where a non-escaping global can be used to854// conclude no-alias.855if ((GV1 || GV2) && GV1 != GV2) {856const GlobalValue *GV = GV1 ? GV1 : GV2;857const Value *UV = GV1 ? UV2 : UV1;858if (isNonEscapingGlobalNoAlias(GV, UV))859return AliasResult::NoAlias;860}861862// Otherwise if they are both derived from the same addr-taken global, we863// can't know the two accesses don't overlap.864}865866// These pointers may be based on the memory owned by an indirect global. If867// so, we may be able to handle this. First check to see if the base pointer868// is a direct load from an indirect global.869GV1 = GV2 = nullptr;870if (const LoadInst *LI = dyn_cast<LoadInst>(UV1))871if (GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getOperand(0)))872if (IndirectGlobals.count(GV))873GV1 = GV;874if (const LoadInst *LI = dyn_cast<LoadInst>(UV2))875if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getOperand(0)))876if (IndirectGlobals.count(GV))877GV2 = GV;878879// These pointers may also be from an allocation for the indirect global. If880// so, also handle them.881if (!GV1)882GV1 = AllocsForIndirectGlobals.lookup(UV1);883if (!GV2)884GV2 = AllocsForIndirectGlobals.lookup(UV2);885886// Now that we know whether the two pointers are related to indirect globals,887// use this to disambiguate the pointers. If the pointers are based on888// different indirect globals they cannot alias.889if (GV1 && GV2 && GV1 != GV2)890return AliasResult::NoAlias;891892// If one is based on an indirect global and the other isn't, it isn't893// strictly safe but we can fake this result if necessary for performance.894// This does not appear to be a common problem in practice.895if (EnableUnsafeGlobalsModRefAliasResults)896if ((GV1 || GV2) && GV1 != GV2)897return AliasResult::NoAlias;898899return AliasResult::MayAlias;900}901902ModRefInfo GlobalsAAResult::getModRefInfoForArgument(const CallBase *Call,903const GlobalValue *GV,904AAQueryInfo &AAQI) {905if (Call->doesNotAccessMemory())906return ModRefInfo::NoModRef;907ModRefInfo ConservativeResult =908Call->onlyReadsMemory() ? ModRefInfo::Ref : ModRefInfo::ModRef;909910// Iterate through all the arguments to the called function. If any argument911// is based on GV, return the conservative result.912for (const auto &A : Call->args()) {913SmallVector<const Value*, 4> Objects;914getUnderlyingObjects(A, Objects);915916// All objects must be identified.917if (!all_of(Objects, isIdentifiedObject) &&918// Try ::alias to see if all objects are known not to alias GV.919!all_of(Objects, [&](const Value *V) {920return this->alias(MemoryLocation::getBeforeOrAfter(V),921MemoryLocation::getBeforeOrAfter(GV), AAQI,922nullptr) == AliasResult::NoAlias;923}))924return ConservativeResult;925926if (is_contained(Objects, GV))927return ConservativeResult;928}929930// We identified all objects in the argument list, and none of them were GV.931return ModRefInfo::NoModRef;932}933934ModRefInfo GlobalsAAResult::getModRefInfo(const CallBase *Call,935const MemoryLocation &Loc,936AAQueryInfo &AAQI) {937ModRefInfo Known = ModRefInfo::ModRef;938939// If we are asking for mod/ref info of a direct call with a pointer to a940// global we are tracking, return information if we have it.941if (const GlobalValue *GV =942dyn_cast<GlobalValue>(getUnderlyingObject(Loc.Ptr)))943// If GV is internal to this IR and there is no function with local linkage944// that has had their address taken, keep looking for a tighter ModRefInfo.945if (GV->hasLocalLinkage() && !UnknownFunctionsWithLocalLinkage)946if (const Function *F = Call->getCalledFunction())947if (NonAddressTakenGlobals.count(GV))948if (const FunctionInfo *FI = getFunctionInfo(F))949Known = FI->getModRefInfoForGlobal(*GV) |950getModRefInfoForArgument(Call, GV, AAQI);951952return Known;953}954955GlobalsAAResult::GlobalsAAResult(956const DataLayout &DL,957std::function<const TargetLibraryInfo &(Function &F)> GetTLI)958: DL(DL), GetTLI(std::move(GetTLI)) {}959960GlobalsAAResult::GlobalsAAResult(GlobalsAAResult &&Arg)961: AAResultBase(std::move(Arg)), DL(Arg.DL), GetTLI(std::move(Arg.GetTLI)),962NonAddressTakenGlobals(std::move(Arg.NonAddressTakenGlobals)),963IndirectGlobals(std::move(Arg.IndirectGlobals)),964AllocsForIndirectGlobals(std::move(Arg.AllocsForIndirectGlobals)),965FunctionInfos(std::move(Arg.FunctionInfos)),966Handles(std::move(Arg.Handles)) {967// Update the parent for each DeletionCallbackHandle.968for (auto &H : Handles) {969assert(H.GAR == &Arg);970H.GAR = this;971}972}973974GlobalsAAResult::~GlobalsAAResult() = default;975976/*static*/ GlobalsAAResult GlobalsAAResult::analyzeModule(977Module &M, std::function<const TargetLibraryInfo &(Function &F)> GetTLI,978CallGraph &CG) {979GlobalsAAResult Result(M.getDataLayout(), GetTLI);980981// Discover which functions aren't recursive, to feed into AnalyzeGlobals.982Result.CollectSCCMembership(CG);983984// Find non-addr taken globals.985Result.AnalyzeGlobals(M);986987// Propagate on CG.988Result.AnalyzeCallGraph(CG, M);989990return Result;991}992993AnalysisKey GlobalsAA::Key;994995GlobalsAAResult GlobalsAA::run(Module &M, ModuleAnalysisManager &AM) {996FunctionAnalysisManager &FAM =997AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();998auto GetTLI = [&FAM](Function &F) -> TargetLibraryInfo & {999return FAM.getResult<TargetLibraryAnalysis>(F);1000};1001return GlobalsAAResult::analyzeModule(M, GetTLI,1002AM.getResult<CallGraphAnalysis>(M));1003}10041005PreservedAnalyses RecomputeGlobalsAAPass::run(Module &M,1006ModuleAnalysisManager &AM) {1007if (auto *G = AM.getCachedResult<GlobalsAA>(M)) {1008auto &CG = AM.getResult<CallGraphAnalysis>(M);1009G->NonAddressTakenGlobals.clear();1010G->UnknownFunctionsWithLocalLinkage = false;1011G->IndirectGlobals.clear();1012G->AllocsForIndirectGlobals.clear();1013G->FunctionInfos.clear();1014G->FunctionToSCCMap.clear();1015G->Handles.clear();1016G->CollectSCCMembership(CG);1017G->AnalyzeGlobals(M);1018G->AnalyzeCallGraph(CG, M);1019}1020return PreservedAnalyses::all();1021}10221023char GlobalsAAWrapperPass::ID = 0;1024INITIALIZE_PASS_BEGIN(GlobalsAAWrapperPass, "globals-aa",1025"Globals Alias Analysis", false, true)1026INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)1027INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)1028INITIALIZE_PASS_END(GlobalsAAWrapperPass, "globals-aa",1029"Globals Alias Analysis", false, true)10301031ModulePass *llvm::createGlobalsAAWrapperPass() {1032return new GlobalsAAWrapperPass();1033}10341035GlobalsAAWrapperPass::GlobalsAAWrapperPass() : ModulePass(ID) {1036initializeGlobalsAAWrapperPassPass(*PassRegistry::getPassRegistry());1037}10381039bool GlobalsAAWrapperPass::runOnModule(Module &M) {1040auto GetTLI = [this](Function &F) -> TargetLibraryInfo & {1041return this->getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);1042};1043Result.reset(new GlobalsAAResult(GlobalsAAResult::analyzeModule(1044M, GetTLI, getAnalysis<CallGraphWrapperPass>().getCallGraph())));1045return false;1046}10471048bool GlobalsAAWrapperPass::doFinalization(Module &M) {1049Result.reset();1050return false;1051}10521053void GlobalsAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {1054AU.setPreservesAll();1055AU.addRequired<CallGraphWrapperPass>();1056AU.addRequired<TargetLibraryInfoWrapperPass>();1057}105810591060