33 #define DEBUG_TYPE "globalsmodref-aa" 36 "Number of global vars without address taken");
37 STATISTIC(NumNonAddrTakenFunctions,
"Number of functions without address taken");
38 STATISTIC(NumNoMemFunctions,
"Number of functions that do not access memory");
39 STATISTIC(NumReadMemFunctions,
"Number of functions that only read memory");
40 STATISTIC(NumIndirectGlobalVars,
"Number of indirect global objects");
68 struct alignas(8) AlignedMap {
70 AlignedMap(
const AlignedMap &
Arg) : Map(Arg.Map) {}
71 GlobalInfoMapType Map;
75 struct AlignedMapPointerTraits {
76 static inline void *getAsVoidPointer(AlignedMap *
P) {
return P; }
77 static inline AlignedMap *getFromVoidPointer(
void *P) {
78 return (AlignedMap *)
P;
80 enum { NumLowBitsAvailable = 3 };
81 static_assert(
alignof(AlignedMap) >= (1 << NumLowBitsAvailable),
82 "AlignedMap insufficiently aligned to have enough low bits.");
91 enum { MayReadAnyGlobal = 4 };
96 "ModRef and the MayReadAnyGlobal flag bits overlap.");
97 static_assert(((MayReadAnyGlobal |
99 AlignedMapPointerTraits::NumLowBitsAvailable) == 0,
100 "Insufficient low bits to store our flag and ModRef info.");
105 delete Info.getPointer();
112 if (
const auto *ArgPtr = Arg.Info.
getPointer())
113 Info.setPointer(
new AlignedMap(*ArgPtr));
117 Arg.Info.setPointerAndInt(
nullptr, 0);
120 delete Info.getPointer();
121 Info.setPointerAndInt(
nullptr, RHS.Info.
getInt());
122 if (
const auto *RHSPtr = RHS.Info.
getPointer())
123 Info.setPointer(
new AlignedMap(*RHSPtr));
127 delete Info.getPointer();
128 Info.setPointerAndInt(RHS.Info.getPointer(), RHS.Info.getInt());
129 RHS.Info.setPointerAndInt(
nullptr, 0);
163 if (AlignedMap *
P =
Info.getPointer()) {
164 auto I =
P->Map.find(&GV);
165 if (
I !=
P->Map.end())
180 for (
const auto &
G :
P->Map)
185 AlignedMap *
P =
Info.getPointer();
187 P =
new AlignedMap();
190 auto &GlobalMRI = P->Map[&GV];
197 if (AlignedMap *
P =
Info.getPointer())
210 void GlobalsAAResult::DeletionCallbackHandle::deleted() {
211 Value *V = getValPtr();
212 if (
auto *
F = dyn_cast<Function>(V))
213 GAR->FunctionInfos.erase(
F);
216 if (GAR->NonAddressTakenGlobals.erase(GV)) {
219 if (GAR->IndirectGlobals.erase(GV)) {
221 for (
auto I = GAR->AllocsForIndirectGlobals.begin(),
222 E = GAR->AllocsForIndirectGlobals.end();
225 GAR->AllocsForIndirectGlobals.erase(
I);
230 for (
auto &FIPair : GAR->FunctionInfos)
231 FIPair.second.eraseModRefInfoForGlobal(*GV);
236 GAR->AllocsForIndirectGlobals.erase(V);
240 GAR->Handles.erase(
I);
250 else if (!
isModSet(FI->getModRefInfo()))
266 else if (!
isModSet(FI->getModRefInfo()))
276 GlobalsAAResult::getFunctionInfo(
const Function *
F) {
277 auto I = FunctionInfos.find(F);
278 if (
I != FunctionInfos.end())
287 void GlobalsAAResult::AnalyzeGlobals(
Module &M) {
291 if (!AnalyzeUsesOfPointer(&F)) {
293 NonAddressTakenGlobals.
insert(&F);
294 TrackedFunctions.
insert(&F);
295 Handles.emplace_front(*
this, &F);
296 Handles.front().I = Handles.begin();
297 ++NumNonAddrTakenFunctions;
299 UnknownFunctionsWithLocalLinkage =
true;
304 if (GV.hasLocalLinkage()) {
305 if (!AnalyzeUsesOfPointer(&GV, &Readers,
306 GV.isConstant() ? nullptr : &Writers)) {
308 NonAddressTakenGlobals.
insert(&GV);
309 Handles.emplace_front(*
this, &GV);
310 Handles.front().I = Handles.begin();
313 if (TrackedFunctions.
insert(Reader).second) {
314 Handles.emplace_front(*
this, Reader);
315 Handles.front().I = Handles.begin();
320 if (!GV.isConstant())
322 if (TrackedFunctions.
insert(Writer).second) {
323 Handles.emplace_front(*
this, Writer);
324 Handles.front().I = Handles.begin();
328 ++NumNonAddrTakenGlobalVars;
331 if (GV.getValueType()->isPointerTy() &&
332 AnalyzeIndirectGlobalMemory(&GV))
333 ++NumIndirectGlobalVars;
346 bool GlobalsAAResult::AnalyzeUsesOfPointer(
Value *V,
354 User *
I = U.getUser();
355 if (
LoadInst *LI = dyn_cast<LoadInst>(I)) {
357 Readers->
insert(LI->getParent()->getParent());
358 }
else if (
StoreInst *
SI = dyn_cast<StoreInst>(I)) {
359 if (V ==
SI->getOperand(1)) {
361 Writers->
insert(
SI->getParent()->getParent());
362 }
else if (
SI->getOperand(1) != OkayStoreDest) {
366 if (AnalyzeUsesOfPointer(I, Readers, Writers))
369 if (AnalyzeUsesOfPointer(I, Readers, Writers, OkayStoreDest))
371 }
else if (
auto *Call = dyn_cast<CallBase>(I)) {
374 if (Call->isDataOperand(&U)) {
376 if (Call->isArgOperand(&U) &&
377 isFreeCall(I, &GetTLI(*Call->getFunction()))) {
379 Writers->
insert(Call->getParent()->getParent());
384 }
else if (
ICmpInst *ICI = dyn_cast<ICmpInst>(I)) {
385 if (!isa<ConstantPointerNull>(ICI->getOperand(1)))
387 }
else if (
Constant *
C = dyn_cast<Constant>(I)) {
389 if (isa<GlobalValue>(
C) ||
C->isConstantUsed())
406 bool GlobalsAAResult::AnalyzeIndirectGlobalMemory(
GlobalVariable *GV) {
409 std::vector<Value *> AllocRelatedValues;
413 if (!
C->isNullValue())
419 if (
LoadInst *LI = dyn_cast<LoadInst>(U)) {
423 if (AnalyzeUsesOfPointer(LI))
426 }
else if (
StoreInst *
SI = dyn_cast<StoreInst>(U)) {
428 if (
SI->getOperand(0) == GV)
432 if (isa<ConstantPointerNull>(
SI->getOperand(0)))
444 if (AnalyzeUsesOfPointer(Ptr,
nullptr,
nullptr,
449 AllocRelatedValues.push_back(Ptr);
458 while (!AllocRelatedValues.empty()) {
459 AllocsForIndirectGlobals[AllocRelatedValues.back()] = GV;
460 Handles.emplace_front(*
this, AllocRelatedValues.back());
461 Handles.front().I = Handles.begin();
462 AllocRelatedValues.pop_back();
464 IndirectGlobals.insert(GV);
465 Handles.emplace_front(*
this, GV);
466 Handles.front().I = Handles.begin();
470 void GlobalsAAResult::CollectSCCMembership(
CallGraph &CG) {
475 const std::vector<CallGraphNode *> &SCC = *
I;
476 assert(!SCC.empty() &&
"SCC with no functions?");
478 for (
auto *CGN : SCC)
480 FunctionToSCCMap[F] = SCCID;
493 const std::vector<CallGraphNode *> &SCC = *
I;
494 assert(!SCC.empty() &&
"SCC with no functions?");
498 if (!F || !F->isDefinitionExact()) {
502 for (
auto *Node : SCC)
503 FunctionInfos.erase(Node->getFunction());
508 Handles.emplace_front(*
this, F);
509 Handles.front().I = Handles.begin();
510 bool KnowNothing =
false;
514 for (
unsigned i = 0,
e = SCC.size(); i !=
e && !KnowNothing; ++i) {
520 if (F->isDeclaration() || F->hasOptNone()) {
522 if (F->doesNotAccessMemory()) {
524 }
else if (F->onlyReadsMemory()) {
526 if (!F->isIntrinsic() && !F->onlyAccessesArgMemory())
532 if (!F->onlyAccessesArgMemory())
534 if (!F->isIntrinsic()) {
543 CI !=
E && !KnowNothing; ++CI)
563 for (
auto *Node : SCC)
564 FunctionInfos.erase(Node->getFunction());
569 for (
auto *Node : SCC) {
576 if (Node->getFunction()->hasOptNone())
585 if (
auto *Call = dyn_cast<CallBase>(&
I)) {
586 auto &TLI = GetTLI(*Node->getFunction());
593 if (
Callee->isIntrinsic()) {
594 if (isa<DbgInfoIntrinsic>(Call))
608 if (
I.mayReadFromMemory())
610 if (
I.mayWriteToMemory())
616 ++NumReadMemFunctions;
625 for (
unsigned i = 1,
e = SCC.size(); i !=
e; ++i)
644 if (isa<GlobalValue>(Input) || isa<Argument>(Input) || isa<CallInst>(Input) ||
645 isa<InvokeInst>(Input))
661 if (
auto *LI = dyn_cast<LoadInst>(Input)) {
665 if (
auto *
SI = dyn_cast<SelectInst>(Input)) {
668 if (Visited.
insert(LHS).second)
670 if (Visited.
insert(RHS).second)
674 if (
auto *PN = dyn_cast<PHINode>(Input)) {
675 for (
const Value *
Op : PN->incoming_values()) {
684 }
while (!Inputs.
empty());
715 bool GlobalsAAResult::isNonEscapingGlobalNoAlias(
const GlobalValue *GV,
731 if (
auto *InputGV = dyn_cast<GlobalValue>(Input)) {
741 if (GVar && InputGVar &&
742 !GVar->isDeclaration() && !InputGVar->isDeclaration() &&
743 !GVar->isInterposable() && !InputGVar->isInterposable()) {
744 Type *GVType = GVar->getInitializer()->getType();
745 Type *InputGVType = InputGVar->getInitializer()->getType();
747 (DL.getTypeAllocSize(GVType) > 0) &&
748 (DL.getTypeAllocSize(InputGVType) > 0))
757 if (isa<Argument>(Input) || isa<CallInst>(Input) ||
758 isa<InvokeInst>(Input)) {
772 if (
auto *LI = dyn_cast<LoadInst>(Input)) {
782 if (
auto *
SI = dyn_cast<SelectInst>(Input)) {
785 if (Visited.
insert(LHS).second)
787 if (Visited.
insert(RHS).second)
791 if (
auto *PN = dyn_cast<PHINode>(Input)) {
792 for (
const Value *
Op : PN->incoming_values()) {
807 }
while (!Inputs.
empty());
830 if (GV1 && !NonAddressTakenGlobals.count(GV1))
832 if (GV2 && !NonAddressTakenGlobals.count(GV2))
837 if (GV1 && GV2 && GV1 != GV2)
844 if ((GV1 || GV2) && GV1 != GV2)
849 if ((GV1 || GV2) && GV1 != GV2) {
851 const Value *UV = GV1 ? UV2 : UV1;
852 if (isNonEscapingGlobalNoAlias(GV, UV))
864 if (
const LoadInst *LI = dyn_cast<LoadInst>(UV1))
865 if (
GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getOperand(0)))
866 if (IndirectGlobals.count(GV))
868 if (
const LoadInst *LI = dyn_cast<LoadInst>(UV2))
869 if (
const GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getOperand(0)))
870 if (IndirectGlobals.count(GV))
876 GV1 = AllocsForIndirectGlobals.lookup(UV1);
878 GV2 = AllocsForIndirectGlobals.lookup(UV2);
883 if (GV1 && GV2 && GV1 != GV2)
890 if ((GV1 || GV2) && GV1 != GV2)
906 for (
auto &A : Call->
args()) {
917 return ConservativeResult;
920 return ConservativeResult;
940 if (NonAddressTakenGlobals.count(GV))
942 Known =
unionModRef(FI->getModRefInfoForGlobal(*GV),
943 getModRefInfoForArgument(Call, GV, AAQI));
950 GlobalsAAResult::GlobalsAAResult(
957 NonAddressTakenGlobals(
std::move(
Arg.NonAddressTakenGlobals)),
958 IndirectGlobals(
std::move(
Arg.IndirectGlobals)),
959 AllocsForIndirectGlobals(
std::move(
Arg.AllocsForIndirectGlobals)),
960 FunctionInfos(
std::move(
Arg.FunctionInfos)),
961 Handles(
std::move(
Arg.Handles)) {
963 for (
auto &
H : Handles) {
977 Result.CollectSCCMembership(CG);
980 Result.AnalyzeGlobals(M);
983 Result.AnalyzeCallGraph(CG, M);
1002 "Globals Alias Analysis",
false,
true)
1009 return new GlobalsAAWrapperPass();
1018 return this->getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
1021 M, GetTLI, getAnalysis<CallGraphWrapperPass>().getCallGraph())));
Legacy wrapper pass to provide the GlobalsAAResult object.
The access may reference and may modify the value stored in memory.
const Function & getFunction() const
LLVM_NODISCARD ModRefInfo unionModRef(const ModRefInfo MRI1, const ModRefInfo MRI2)
bool hasOperandBundles() const
Return true if this User has any operand bundles.
bool isAllocationFn(const Value *V, const TargetLibraryInfo *TLI, bool LookThroughBitCast=false)
Tests if a value is a call or invoke to a library function that allocates or reallocates memory (eith...
This builds on the llvm/ADT/GraphTraits.h file to find the strongly connected components (SCCs) of a ...
A parsed version of the target data layout string in and methods for querying it. ...
bool doFinalization(Module &M) override
doFinalization - Virtual method overriden by subclasses to do any necessary clean up after all passes...
iterator_range< use_iterator > uses()
The access neither references nor modifies the value stored in memory.
bool hasLocalLinkage() const
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
bool doesNotAccessMemory(unsigned OpNo) const
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
void addFunctionInfo(const FunctionInfo &FI)
Add mod/ref info from another function into ours, saturating towards ModRef.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
void setMayReadAnyGlobal()
Sets this function as potentially reading from any global.
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
This class represents lattice values for constants.
PointerTy getPointer() const
static cl::opt< bool > EnableUnsafeGlobalsModRefAliasResults("enable-unsafe-globalsmodref-alias-results", cl::init(false), cl::Hidden)
FunctionInfo & operator=(const FunctionInfo &RHS)
ModRefInfo getModRefInfoForGlobal(const GlobalValue &GV) const
Returns the ModRefInfo info for this function w.r.t.
This is the interface for a simple mod/ref and alias analysis over globals.
A Module instance is used to store all the information related to an LLVM module. ...
bool isSized(SmallPtrSetImpl< Type *> *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
void push_back(const T &Elt)
This class stores info we want to provide to or retain within an alias query.
The two locations do not alias at all.
FunctionInfo & operator=(FunctionInfo &&RHS)
FunctionModRefBehavior getModRefBehavior(const CallBase *Call)
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly...
STATISTIC(NumFunctions, "Total number of functions")
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
block Block Frequency true
An instruction for reading from memory.
AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB, AAQueryInfo &AAQI)
A node in the call graph for a module.
LLVM_NODISCARD bool isModAndRefSet(const ModRefInfo MRI)
The mod/ref information collected for a particular function.
ModRefInfo globalClearMayReadAnyGlobal(int I) const
This method clears MayReadAnyGlobal bit added by GlobalsAAResult to return the corresponding ModRefIn...
void addModRefInfoForGlobal(const GlobalValue &GV, ModRefInfo NewMRI)
const CallInst * isFreeCall(const Value *I, const TargetLibraryInfo *TLI)
isFreeCall - Returns non-null if the value is a call to the builtin free()
This indicates that the function could not be classified into one of the behaviors above...
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
AnalysisUsage & addRequired()
bool mayReadAnyGlobal() const
Returns whether this function may read any global variable, and we don't know which global...
#define INITIALIZE_PASS_DEPENDENCY(depName)
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
static unsigned getInt(StringRef R)
Get an unsigned integer, including error checks.
A Use represents the edge between a Value definition and its users.
The access may reference the value stored in memory.
bool isIdentifiedObject(const Value *V)
Return true if this pointer refers to a distinct and identifiable object.
A CRTP-driven "mixin" base class to help implement the function alias analysis results concept...
scc_iterator< T > scc_begin(const T &G)
Construct the begin iterator for a deduced graph type T.
ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc, AAQueryInfo &AAQI)
Type * getType() const
All values are typed, get the type of this value.
bool runOnModule(Module &M) override
runOnModule - Virtual method overriden by subclasses to process the module being operated on...
GlobalsAAResult run(Module &M, ModuleAnalysisManager &AM)
The access may reference, modify or both the value stored in memory, a mustAlias relation was found...
FunctionModRefBehavior
Summary of how a function affects memory in the program.
An instruction for storing to memory.
FunctionInfo(const FunctionInfo &Arg)
ModRefInfo getModRefInfo() const
Returns the ModRefInfo info for this function.
static Function * getFunction(Constant *C)
Analysis containing CSE Info
AliasResult
The possible results of an alias query.
initializer< Ty > init(const Ty &Val)
void GetUnderlyingObjects(const Value *V, SmallVectorImpl< const Value *> &Objects, const DataLayout &DL, LoopInfo *LI=nullptr, unsigned MaxLookup=6)
This method is similar to GetUnderlyingObject except that it can look through phi and select instruct...
LLVM_NODISCARD ModRefInfo setMust(const ModRefInfo MRI)
The ModulePass which wraps up a CallGraph and the logic to build it.
FunctionInfo(FunctionInfo &&Arg)
The instances of the Type class are immutable: once they are created, they are never changed...
An alias analysis result set for globals.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This is an important base class in LLVM.
bool isPointerTy() const
True if this is an instance of PointerType.
FunctionModRefBehavior getModRefBehavior(const Function *F)
getModRefBehavior - Return the behavior of the specified function if called from the specified call s...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Represent the analysis usage information of a pass.
This instruction compares its operands according to the predicate given to the constructor.
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
iterator_range< User::op_iterator > args()
Iteration adapter for range-for loops.
Value * GetUnderlyingObject(Value *V, const DataLayout &DL, unsigned MaxLookup=6)
This method strips off any GEP address adjustments and pointer casts from the specified value...
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
This function does not perform any non-local loads or stores to memory.
void initializeGlobalsAAWrapperPassPass(PassRegistry &)
const Value * Ptr
The address of the start of the location.
Representation for a specific memory location.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Module.h This file contains the declarations for the Module class.
Provides information about what library functions are available for the current target.
void eraseModRefInfoForGlobal(const GlobalValue &GV)
Clear a global's ModRef info.
LLVM_NODISCARD T pop_back_val()
void addModRefInfo(ModRefInfo NewMRI)
Adds new ModRefInfo for this function to its state.
This function does not perform any non-local stores or volatile loads, but may read from any memory l...
The access may modify the value stored in memory.
amdgpu Simplify well known AMD library false FunctionCallee Callee
void setPreservesAll()
Set by analyses that do not transform their input at all.
iterator_range< user_iterator > users()
static GlobalsAAResult analyzeModule(Module &M, std::function< const TargetLibraryInfo &(Function &F)> GetTLI, CallGraph &CG)
An analysis pass to compute the CallGraph for a Module.
LLVM_NODISCARD bool isModSet(const ModRefInfo MRI)
The basic data container for the call graph of a Module of IR.
LLVM_NODISCARD bool empty() const
FunctionInfo()
Checks to document the invariants of the bit packing here.
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation.
ModulePass * createGlobalsAAWrapperPass()
LLVM_NODISCARD ModRefInfo intersectModRef(const ModRefInfo MRI1, const ModRefInfo MRI2)
bool onlyReadsMemory(unsigned OpNo) const
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
bool isAllocLikeFn(const Value *V, const TargetLibraryInfo *TLI, bool LookThroughBitCast=false)
Tests if a value is a call or invoke to a library function that allocates memory (either malloc...
INITIALIZE_PASS_BEGIN(GlobalsAAWrapperPass, "globals-aa", "Globals Alias Analysis", false, true) INITIALIZE_PASS_END(GlobalsAAWrapperPass
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
std::vector< CallRecord >::iterator iterator
Analysis pass providing the TargetLibraryInfo.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Module * getParent()
Get the module that this global value is contained inside of...
LLVM Value Representation.
unsigned getOpcode() const
Return the opcode for this Instruction or ConstantExpr.
LLVM_NODISCARD ModRefInfo createModRefInfo(const FunctionModRefBehavior FMRB)
ModRefInfo
Flags indicating whether a memory access modifies or references memory.
print Print MemDeps of function
inst_range instructions(Function *F)
A container for analyses that lazily runs them and caches their results.
LLVM_NODISCARD bool isModOrRefSet(const ModRefInfo MRI)
ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc, AAQueryInfo &AAQI)
A special type used by analysis passes to provide an address that identifies that particular analysis...
static bool isNonEscapingGlobalNoAliasWithLoad(const GlobalValue *GV, const Value *V, int &Depth, const DataLayout &DL)
Enumerate the SCCs of a directed graph in reverse topological order of the SCC DAG.
AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB, AAQueryInfo &AAQI)
alias - If one of the pointers is to a global that we are tracking, and the other is some random poin...
An analysis over an "outer" IR unit that provides access to an analysis manager over an "inner" IR un...
bool is_contained(R &&Range, const E &Element)
Wrapper function around std::find to detect if an element exists in a container.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...