1 //===- GlobalsModRef.h - Simple Mod/Ref AA for Globals ----------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 /// This is the interface for a simple mod/ref and alias analysis over globals.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_ANALYSIS_GLOBALSMODREF_H
14 #define LLVM_ANALYSIS_GLOBALSMODREF_H
16 #include "llvm/Analysis/AliasAnalysis.h"
17 #include "llvm/Analysis/CallGraph.h"
18 #include "llvm/IR/Constants.h"
19 #include "llvm/IR/Function.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/IR/ValueHandle.h"
22 #include "llvm/Pass.h"
27 /// An alias analysis result set for globals.
29 /// This focuses on handling aliasing properties of globals and interprocedural
30 /// function call mod/ref information.
31 class GlobalsAAResult
: public AAResultBase
<GlobalsAAResult
> {
32 friend AAResultBase
<GlobalsAAResult
>;
37 std::function
<const TargetLibraryInfo
&(Function
&F
)> GetTLI
;
39 /// The globals that do not have their addresses taken.
40 SmallPtrSet
<const GlobalValue
*, 8> NonAddressTakenGlobals
;
42 /// IndirectGlobals - The memory pointed to by this global is known to be
43 /// 'owned' by the global.
44 SmallPtrSet
<const GlobalValue
*, 8> IndirectGlobals
;
46 /// AllocsForIndirectGlobals - If an instruction allocates memory for an
47 /// indirect global, this map indicates which one.
48 DenseMap
<const Value
*, const GlobalValue
*> AllocsForIndirectGlobals
;
50 /// For each function, keep track of what globals are modified or read.
51 DenseMap
<const Function
*, FunctionInfo
> FunctionInfos
;
53 /// A map of functions to SCC. The SCCs are described by a simple integer
54 /// ID that is only useful for comparing for equality (are two functions
55 /// in the same SCC or not?)
56 DenseMap
<const Function
*, unsigned> FunctionToSCCMap
;
58 /// Handle to clear this analysis on deletion of values.
59 struct DeletionCallbackHandle final
: CallbackVH
{
61 std::list
<DeletionCallbackHandle
>::iterator I
;
63 DeletionCallbackHandle(GlobalsAAResult
&GAR
, Value
*V
)
64 : CallbackVH(V
), GAR(&GAR
) {}
66 void deleted() override
;
69 /// List of callbacks for globals being tracked by this analysis. Note that
70 /// these objects are quite large, but we only anticipate having one per
71 /// global tracked by this analysis. There are numerous optimizations we
72 /// could perform to the memory utilization here if this becomes a problem.
73 std::list
<DeletionCallbackHandle
> Handles
;
75 explicit GlobalsAAResult(
77 std::function
<const TargetLibraryInfo
&(Function
&F
)> GetTLI
);
80 GlobalsAAResult(GlobalsAAResult
&&Arg
);
83 static GlobalsAAResult
84 analyzeModule(Module
&M
,
85 std::function
<const TargetLibraryInfo
&(Function
&F
)> GetTLI
,
88 //------------------------------------------------
89 // Implement the AliasAnalysis API
91 AliasResult
alias(const MemoryLocation
&LocA
, const MemoryLocation
&LocB
,
94 using AAResultBase::getModRefInfo
;
95 ModRefInfo
getModRefInfo(const CallBase
*Call
, const MemoryLocation
&Loc
,
98 /// getModRefBehavior - Return the behavior of the specified function if
99 /// called from the specified call site. The call site may be null in which
100 /// case the most generic behavior of this function should be returned.
101 FunctionModRefBehavior
getModRefBehavior(const Function
*F
);
103 /// getModRefBehavior - Return the behavior of the specified function if
104 /// called from the specified call site. The call site may be null in which
105 /// case the most generic behavior of this function should be returned.
106 FunctionModRefBehavior
getModRefBehavior(const CallBase
*Call
);
109 FunctionInfo
*getFunctionInfo(const Function
*F
);
111 void AnalyzeGlobals(Module
&M
);
112 void AnalyzeCallGraph(CallGraph
&CG
, Module
&M
);
113 bool AnalyzeUsesOfPointer(Value
*V
,
114 SmallPtrSetImpl
<Function
*> *Readers
= nullptr,
115 SmallPtrSetImpl
<Function
*> *Writers
= nullptr,
116 GlobalValue
*OkayStoreDest
= nullptr);
117 bool AnalyzeIndirectGlobalMemory(GlobalVariable
*GV
);
118 void CollectSCCMembership(CallGraph
&CG
);
120 bool isNonEscapingGlobalNoAlias(const GlobalValue
*GV
, const Value
*V
);
121 ModRefInfo
getModRefInfoForArgument(const CallBase
*Call
,
122 const GlobalValue
*GV
, AAQueryInfo
&AAQI
);
125 /// Analysis pass providing a never-invalidated alias analysis result.
126 class GlobalsAA
: public AnalysisInfoMixin
<GlobalsAA
> {
127 friend AnalysisInfoMixin
<GlobalsAA
>;
128 static AnalysisKey Key
;
131 typedef GlobalsAAResult Result
;
133 GlobalsAAResult
run(Module
&M
, ModuleAnalysisManager
&AM
);
136 /// Legacy wrapper pass to provide the GlobalsAAResult object.
137 class GlobalsAAWrapperPass
: public ModulePass
{
138 std::unique_ptr
<GlobalsAAResult
> Result
;
143 GlobalsAAWrapperPass();
145 GlobalsAAResult
&getResult() { return *Result
; }
146 const GlobalsAAResult
&getResult() const { return *Result
; }
148 bool runOnModule(Module
&M
) override
;
149 bool doFinalization(Module
&M
) override
;
150 void getAnalysisUsage(AnalysisUsage
&AU
) const override
;
153 //===--------------------------------------------------------------------===//
155 // createGlobalsAAWrapperPass - This pass provides alias and mod/ref info for
156 // global values that do not have their addresses taken.
158 ModulePass
*createGlobalsAAWrapperPass();