1*9880d681SAndroid Build Coastguard Worker //===- GlobalsModRef.cpp - Simple Mod/Ref Analysis for Globals ------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This simple pass provides alias and mod/ref information for global values
11*9880d681SAndroid Build Coastguard Worker // that do not have their address taken, and keeps track of whether functions
12*9880d681SAndroid Build Coastguard Worker // read or write memory (are "pure"). For this simple (but very common) case,
13*9880d681SAndroid Build Coastguard Worker // we can provide pretty accurate and useful information.
14*9880d681SAndroid Build Coastguard Worker //
15*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
16*9880d681SAndroid Build Coastguard Worker
17*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/GlobalsModRef.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SCCIterator.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SmallPtrSet.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/Statistic.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/MemoryBuiltins.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/TargetLibraryInfo.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/ValueTracking.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/DerivedTypes.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/InstIterator.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Instructions.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/IntrinsicInst.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Module.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/Pass.h"
30*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/CommandLine.h"
31*9880d681SAndroid Build Coastguard Worker using namespace llvm;
32*9880d681SAndroid Build Coastguard Worker
33*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "globalsmodref-aa"
34*9880d681SAndroid Build Coastguard Worker
35*9880d681SAndroid Build Coastguard Worker STATISTIC(NumNonAddrTakenGlobalVars,
36*9880d681SAndroid Build Coastguard Worker "Number of global vars without address taken");
37*9880d681SAndroid Build Coastguard Worker STATISTIC(NumNonAddrTakenFunctions,"Number of functions without address taken");
38*9880d681SAndroid Build Coastguard Worker STATISTIC(NumNoMemFunctions, "Number of functions that do not access memory");
39*9880d681SAndroid Build Coastguard Worker STATISTIC(NumReadMemFunctions, "Number of functions that only read memory");
40*9880d681SAndroid Build Coastguard Worker STATISTIC(NumIndirectGlobalVars, "Number of indirect global objects");
41*9880d681SAndroid Build Coastguard Worker
42*9880d681SAndroid Build Coastguard Worker // An option to enable unsafe alias results from the GlobalsModRef analysis.
43*9880d681SAndroid Build Coastguard Worker // When enabled, GlobalsModRef will provide no-alias results which in extremely
44*9880d681SAndroid Build Coastguard Worker // rare cases may not be conservatively correct. In particular, in the face of
45*9880d681SAndroid Build Coastguard Worker // transforms which cause assymetry between how effective GetUnderlyingObject
46*9880d681SAndroid Build Coastguard Worker // is for two pointers, it may produce incorrect results.
47*9880d681SAndroid Build Coastguard Worker //
48*9880d681SAndroid Build Coastguard Worker // These unsafe results have been returned by GMR for many years without
49*9880d681SAndroid Build Coastguard Worker // causing significant issues in the wild and so we provide a mechanism to
50*9880d681SAndroid Build Coastguard Worker // re-enable them for users of LLVM that have a particular performance
51*9880d681SAndroid Build Coastguard Worker // sensitivity and no known issues. The option also makes it easy to evaluate
52*9880d681SAndroid Build Coastguard Worker // the performance impact of these results.
53*9880d681SAndroid Build Coastguard Worker static cl::opt<bool> EnableUnsafeGlobalsModRefAliasResults(
54*9880d681SAndroid Build Coastguard Worker "enable-unsafe-globalsmodref-alias-results", cl::init(false), cl::Hidden);
55*9880d681SAndroid Build Coastguard Worker
56*9880d681SAndroid Build Coastguard Worker /// The mod/ref information collected for a particular function.
57*9880d681SAndroid Build Coastguard Worker ///
58*9880d681SAndroid Build Coastguard Worker /// We collect information about mod/ref behavior of a function here, both in
59*9880d681SAndroid Build Coastguard Worker /// general and as pertains to specific globals. We only have this detailed
60*9880d681SAndroid Build Coastguard Worker /// information when we know *something* useful about the behavior. If we
61*9880d681SAndroid Build Coastguard Worker /// saturate to fully general mod/ref, we remove the info for the function.
62*9880d681SAndroid Build Coastguard Worker class GlobalsAAResult::FunctionInfo {
63*9880d681SAndroid Build Coastguard Worker typedef SmallDenseMap<const GlobalValue *, ModRefInfo, 16> GlobalInfoMapType;
64*9880d681SAndroid Build Coastguard Worker
65*9880d681SAndroid Build Coastguard Worker /// Build a wrapper struct that has 8-byte alignment. All heap allocations
66*9880d681SAndroid Build Coastguard Worker /// should provide this much alignment at least, but this makes it clear we
67*9880d681SAndroid Build Coastguard Worker /// specifically rely on this amount of alignment.
68*9880d681SAndroid Build Coastguard Worker struct LLVM_ALIGNAS(8) AlignedMap {
AlignedMapGlobalsAAResult::FunctionInfo::AlignedMap69*9880d681SAndroid Build Coastguard Worker AlignedMap() {}
AlignedMapGlobalsAAResult::FunctionInfo::AlignedMap70*9880d681SAndroid Build Coastguard Worker AlignedMap(const AlignedMap &Arg) : Map(Arg.Map) {}
71*9880d681SAndroid Build Coastguard Worker GlobalInfoMapType Map;
72*9880d681SAndroid Build Coastguard Worker };
73*9880d681SAndroid Build Coastguard Worker
74*9880d681SAndroid Build Coastguard Worker /// Pointer traits for our aligned map.
75*9880d681SAndroid Build Coastguard Worker struct AlignedMapPointerTraits {
getAsVoidPointerGlobalsAAResult::FunctionInfo::AlignedMapPointerTraits76*9880d681SAndroid Build Coastguard Worker static inline void *getAsVoidPointer(AlignedMap *P) { return P; }
getFromVoidPointerGlobalsAAResult::FunctionInfo::AlignedMapPointerTraits77*9880d681SAndroid Build Coastguard Worker static inline AlignedMap *getFromVoidPointer(void *P) {
78*9880d681SAndroid Build Coastguard Worker return (AlignedMap *)P;
79*9880d681SAndroid Build Coastguard Worker }
80*9880d681SAndroid Build Coastguard Worker enum { NumLowBitsAvailable = 3 };
81*9880d681SAndroid Build Coastguard Worker static_assert(AlignOf<AlignedMap>::Alignment >= (1 << NumLowBitsAvailable),
82*9880d681SAndroid Build Coastguard Worker "AlignedMap insufficiently aligned to have enough low bits.");
83*9880d681SAndroid Build Coastguard Worker };
84*9880d681SAndroid Build Coastguard Worker
85*9880d681SAndroid Build Coastguard Worker /// The bit that flags that this function may read any global. This is
86*9880d681SAndroid Build Coastguard Worker /// chosen to mix together with ModRefInfo bits.
87*9880d681SAndroid Build Coastguard Worker enum { MayReadAnyGlobal = 4 };
88*9880d681SAndroid Build Coastguard Worker
89*9880d681SAndroid Build Coastguard Worker /// Checks to document the invariants of the bit packing here.
90*9880d681SAndroid Build Coastguard Worker static_assert((MayReadAnyGlobal & MRI_ModRef) == 0,
91*9880d681SAndroid Build Coastguard Worker "ModRef and the MayReadAnyGlobal flag bits overlap.");
92*9880d681SAndroid Build Coastguard Worker static_assert(((MayReadAnyGlobal | MRI_ModRef) >>
93*9880d681SAndroid Build Coastguard Worker AlignedMapPointerTraits::NumLowBitsAvailable) == 0,
94*9880d681SAndroid Build Coastguard Worker "Insufficient low bits to store our flag and ModRef info.");
95*9880d681SAndroid Build Coastguard Worker
96*9880d681SAndroid Build Coastguard Worker public:
FunctionInfo()97*9880d681SAndroid Build Coastguard Worker FunctionInfo() : Info() {}
~FunctionInfo()98*9880d681SAndroid Build Coastguard Worker ~FunctionInfo() {
99*9880d681SAndroid Build Coastguard Worker delete Info.getPointer();
100*9880d681SAndroid Build Coastguard Worker }
101*9880d681SAndroid Build Coastguard Worker // Spell out the copy ond move constructors and assignment operators to get
102*9880d681SAndroid Build Coastguard Worker // deep copy semantics and correct move semantics in the face of the
103*9880d681SAndroid Build Coastguard Worker // pointer-int pair.
FunctionInfo(const FunctionInfo & Arg)104*9880d681SAndroid Build Coastguard Worker FunctionInfo(const FunctionInfo &Arg)
105*9880d681SAndroid Build Coastguard Worker : Info(nullptr, Arg.Info.getInt()) {
106*9880d681SAndroid Build Coastguard Worker if (const auto *ArgPtr = Arg.Info.getPointer())
107*9880d681SAndroid Build Coastguard Worker Info.setPointer(new AlignedMap(*ArgPtr));
108*9880d681SAndroid Build Coastguard Worker }
FunctionInfo(FunctionInfo && Arg)109*9880d681SAndroid Build Coastguard Worker FunctionInfo(FunctionInfo &&Arg)
110*9880d681SAndroid Build Coastguard Worker : Info(Arg.Info.getPointer(), Arg.Info.getInt()) {
111*9880d681SAndroid Build Coastguard Worker Arg.Info.setPointerAndInt(nullptr, 0);
112*9880d681SAndroid Build Coastguard Worker }
operator =(const FunctionInfo & RHS)113*9880d681SAndroid Build Coastguard Worker FunctionInfo &operator=(const FunctionInfo &RHS) {
114*9880d681SAndroid Build Coastguard Worker delete Info.getPointer();
115*9880d681SAndroid Build Coastguard Worker Info.setPointerAndInt(nullptr, RHS.Info.getInt());
116*9880d681SAndroid Build Coastguard Worker if (const auto *RHSPtr = RHS.Info.getPointer())
117*9880d681SAndroid Build Coastguard Worker Info.setPointer(new AlignedMap(*RHSPtr));
118*9880d681SAndroid Build Coastguard Worker return *this;
119*9880d681SAndroid Build Coastguard Worker }
operator =(FunctionInfo && RHS)120*9880d681SAndroid Build Coastguard Worker FunctionInfo &operator=(FunctionInfo &&RHS) {
121*9880d681SAndroid Build Coastguard Worker delete Info.getPointer();
122*9880d681SAndroid Build Coastguard Worker Info.setPointerAndInt(RHS.Info.getPointer(), RHS.Info.getInt());
123*9880d681SAndroid Build Coastguard Worker RHS.Info.setPointerAndInt(nullptr, 0);
124*9880d681SAndroid Build Coastguard Worker return *this;
125*9880d681SAndroid Build Coastguard Worker }
126*9880d681SAndroid Build Coastguard Worker
127*9880d681SAndroid Build Coastguard Worker /// Returns the \c ModRefInfo info for this function.
getModRefInfo() const128*9880d681SAndroid Build Coastguard Worker ModRefInfo getModRefInfo() const {
129*9880d681SAndroid Build Coastguard Worker return ModRefInfo(Info.getInt() & MRI_ModRef);
130*9880d681SAndroid Build Coastguard Worker }
131*9880d681SAndroid Build Coastguard Worker
132*9880d681SAndroid Build Coastguard Worker /// Adds new \c ModRefInfo for this function to its state.
addModRefInfo(ModRefInfo NewMRI)133*9880d681SAndroid Build Coastguard Worker void addModRefInfo(ModRefInfo NewMRI) {
134*9880d681SAndroid Build Coastguard Worker Info.setInt(Info.getInt() | NewMRI);
135*9880d681SAndroid Build Coastguard Worker }
136*9880d681SAndroid Build Coastguard Worker
137*9880d681SAndroid Build Coastguard Worker /// Returns whether this function may read any global variable, and we don't
138*9880d681SAndroid Build Coastguard Worker /// know which global.
mayReadAnyGlobal() const139*9880d681SAndroid Build Coastguard Worker bool mayReadAnyGlobal() const { return Info.getInt() & MayReadAnyGlobal; }
140*9880d681SAndroid Build Coastguard Worker
141*9880d681SAndroid Build Coastguard Worker /// Sets this function as potentially reading from any global.
setMayReadAnyGlobal()142*9880d681SAndroid Build Coastguard Worker void setMayReadAnyGlobal() { Info.setInt(Info.getInt() | MayReadAnyGlobal); }
143*9880d681SAndroid Build Coastguard Worker
144*9880d681SAndroid Build Coastguard Worker /// Returns the \c ModRefInfo info for this function w.r.t. a particular
145*9880d681SAndroid Build Coastguard Worker /// global, which may be more precise than the general information above.
getModRefInfoForGlobal(const GlobalValue & GV) const146*9880d681SAndroid Build Coastguard Worker ModRefInfo getModRefInfoForGlobal(const GlobalValue &GV) const {
147*9880d681SAndroid Build Coastguard Worker ModRefInfo GlobalMRI = mayReadAnyGlobal() ? MRI_Ref : MRI_NoModRef;
148*9880d681SAndroid Build Coastguard Worker if (AlignedMap *P = Info.getPointer()) {
149*9880d681SAndroid Build Coastguard Worker auto I = P->Map.find(&GV);
150*9880d681SAndroid Build Coastguard Worker if (I != P->Map.end())
151*9880d681SAndroid Build Coastguard Worker GlobalMRI = ModRefInfo(GlobalMRI | I->second);
152*9880d681SAndroid Build Coastguard Worker }
153*9880d681SAndroid Build Coastguard Worker return GlobalMRI;
154*9880d681SAndroid Build Coastguard Worker }
155*9880d681SAndroid Build Coastguard Worker
156*9880d681SAndroid Build Coastguard Worker /// Add mod/ref info from another function into ours, saturating towards
157*9880d681SAndroid Build Coastguard Worker /// MRI_ModRef.
addFunctionInfo(const FunctionInfo & FI)158*9880d681SAndroid Build Coastguard Worker void addFunctionInfo(const FunctionInfo &FI) {
159*9880d681SAndroid Build Coastguard Worker addModRefInfo(FI.getModRefInfo());
160*9880d681SAndroid Build Coastguard Worker
161*9880d681SAndroid Build Coastguard Worker if (FI.mayReadAnyGlobal())
162*9880d681SAndroid Build Coastguard Worker setMayReadAnyGlobal();
163*9880d681SAndroid Build Coastguard Worker
164*9880d681SAndroid Build Coastguard Worker if (AlignedMap *P = FI.Info.getPointer())
165*9880d681SAndroid Build Coastguard Worker for (const auto &G : P->Map)
166*9880d681SAndroid Build Coastguard Worker addModRefInfoForGlobal(*G.first, G.second);
167*9880d681SAndroid Build Coastguard Worker }
168*9880d681SAndroid Build Coastguard Worker
addModRefInfoForGlobal(const GlobalValue & GV,ModRefInfo NewMRI)169*9880d681SAndroid Build Coastguard Worker void addModRefInfoForGlobal(const GlobalValue &GV, ModRefInfo NewMRI) {
170*9880d681SAndroid Build Coastguard Worker AlignedMap *P = Info.getPointer();
171*9880d681SAndroid Build Coastguard Worker if (!P) {
172*9880d681SAndroid Build Coastguard Worker P = new AlignedMap();
173*9880d681SAndroid Build Coastguard Worker Info.setPointer(P);
174*9880d681SAndroid Build Coastguard Worker }
175*9880d681SAndroid Build Coastguard Worker auto &GlobalMRI = P->Map[&GV];
176*9880d681SAndroid Build Coastguard Worker GlobalMRI = ModRefInfo(GlobalMRI | NewMRI);
177*9880d681SAndroid Build Coastguard Worker }
178*9880d681SAndroid Build Coastguard Worker
179*9880d681SAndroid Build Coastguard Worker /// Clear a global's ModRef info. Should be used when a global is being
180*9880d681SAndroid Build Coastguard Worker /// deleted.
eraseModRefInfoForGlobal(const GlobalValue & GV)181*9880d681SAndroid Build Coastguard Worker void eraseModRefInfoForGlobal(const GlobalValue &GV) {
182*9880d681SAndroid Build Coastguard Worker if (AlignedMap *P = Info.getPointer())
183*9880d681SAndroid Build Coastguard Worker P->Map.erase(&GV);
184*9880d681SAndroid Build Coastguard Worker }
185*9880d681SAndroid Build Coastguard Worker
186*9880d681SAndroid Build Coastguard Worker private:
187*9880d681SAndroid Build Coastguard Worker /// All of the information is encoded into a single pointer, with a three bit
188*9880d681SAndroid Build Coastguard Worker /// integer in the low three bits. The high bit provides a flag for when this
189*9880d681SAndroid Build Coastguard Worker /// function may read any global. The low two bits are the ModRefInfo. And
190*9880d681SAndroid Build Coastguard Worker /// the pointer, when non-null, points to a map from GlobalValue to
191*9880d681SAndroid Build Coastguard Worker /// ModRefInfo specific to that GlobalValue.
192*9880d681SAndroid Build Coastguard Worker PointerIntPair<AlignedMap *, 3, unsigned, AlignedMapPointerTraits> Info;
193*9880d681SAndroid Build Coastguard Worker };
194*9880d681SAndroid Build Coastguard Worker
deleted()195*9880d681SAndroid Build Coastguard Worker void GlobalsAAResult::DeletionCallbackHandle::deleted() {
196*9880d681SAndroid Build Coastguard Worker Value *V = getValPtr();
197*9880d681SAndroid Build Coastguard Worker if (auto *F = dyn_cast<Function>(V))
198*9880d681SAndroid Build Coastguard Worker GAR->FunctionInfos.erase(F);
199*9880d681SAndroid Build Coastguard Worker
200*9880d681SAndroid Build Coastguard Worker if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
201*9880d681SAndroid Build Coastguard Worker if (GAR->NonAddressTakenGlobals.erase(GV)) {
202*9880d681SAndroid Build Coastguard Worker // This global might be an indirect global. If so, remove it and
203*9880d681SAndroid Build Coastguard Worker // remove any AllocRelatedValues for it.
204*9880d681SAndroid Build Coastguard Worker if (GAR->IndirectGlobals.erase(GV)) {
205*9880d681SAndroid Build Coastguard Worker // Remove any entries in AllocsForIndirectGlobals for this global.
206*9880d681SAndroid Build Coastguard Worker for (auto I = GAR->AllocsForIndirectGlobals.begin(),
207*9880d681SAndroid Build Coastguard Worker E = GAR->AllocsForIndirectGlobals.end();
208*9880d681SAndroid Build Coastguard Worker I != E; ++I)
209*9880d681SAndroid Build Coastguard Worker if (I->second == GV)
210*9880d681SAndroid Build Coastguard Worker GAR->AllocsForIndirectGlobals.erase(I);
211*9880d681SAndroid Build Coastguard Worker }
212*9880d681SAndroid Build Coastguard Worker
213*9880d681SAndroid Build Coastguard Worker // Scan the function info we have collected and remove this global
214*9880d681SAndroid Build Coastguard Worker // from all of them.
215*9880d681SAndroid Build Coastguard Worker for (auto &FIPair : GAR->FunctionInfos)
216*9880d681SAndroid Build Coastguard Worker FIPair.second.eraseModRefInfoForGlobal(*GV);
217*9880d681SAndroid Build Coastguard Worker }
218*9880d681SAndroid Build Coastguard Worker }
219*9880d681SAndroid Build Coastguard Worker
220*9880d681SAndroid Build Coastguard Worker // If this is an allocation related to an indirect global, remove it.
221*9880d681SAndroid Build Coastguard Worker GAR->AllocsForIndirectGlobals.erase(V);
222*9880d681SAndroid Build Coastguard Worker
223*9880d681SAndroid Build Coastguard Worker // And clear out the handle.
224*9880d681SAndroid Build Coastguard Worker setValPtr(nullptr);
225*9880d681SAndroid Build Coastguard Worker GAR->Handles.erase(I);
226*9880d681SAndroid Build Coastguard Worker // This object is now destroyed!
227*9880d681SAndroid Build Coastguard Worker }
228*9880d681SAndroid Build Coastguard Worker
getModRefBehavior(const Function * F)229*9880d681SAndroid Build Coastguard Worker FunctionModRefBehavior GlobalsAAResult::getModRefBehavior(const Function *F) {
230*9880d681SAndroid Build Coastguard Worker FunctionModRefBehavior Min = FMRB_UnknownModRefBehavior;
231*9880d681SAndroid Build Coastguard Worker
232*9880d681SAndroid Build Coastguard Worker if (FunctionInfo *FI = getFunctionInfo(F)) {
233*9880d681SAndroid Build Coastguard Worker if (FI->getModRefInfo() == MRI_NoModRef)
234*9880d681SAndroid Build Coastguard Worker Min = FMRB_DoesNotAccessMemory;
235*9880d681SAndroid Build Coastguard Worker else if ((FI->getModRefInfo() & MRI_Mod) == 0)
236*9880d681SAndroid Build Coastguard Worker Min = FMRB_OnlyReadsMemory;
237*9880d681SAndroid Build Coastguard Worker }
238*9880d681SAndroid Build Coastguard Worker
239*9880d681SAndroid Build Coastguard Worker return FunctionModRefBehavior(AAResultBase::getModRefBehavior(F) & Min);
240*9880d681SAndroid Build Coastguard Worker }
241*9880d681SAndroid Build Coastguard Worker
242*9880d681SAndroid Build Coastguard Worker FunctionModRefBehavior
getModRefBehavior(ImmutableCallSite CS)243*9880d681SAndroid Build Coastguard Worker GlobalsAAResult::getModRefBehavior(ImmutableCallSite CS) {
244*9880d681SAndroid Build Coastguard Worker FunctionModRefBehavior Min = FMRB_UnknownModRefBehavior;
245*9880d681SAndroid Build Coastguard Worker
246*9880d681SAndroid Build Coastguard Worker if (!CS.hasOperandBundles())
247*9880d681SAndroid Build Coastguard Worker if (const Function *F = CS.getCalledFunction())
248*9880d681SAndroid Build Coastguard Worker if (FunctionInfo *FI = getFunctionInfo(F)) {
249*9880d681SAndroid Build Coastguard Worker if (FI->getModRefInfo() == MRI_NoModRef)
250*9880d681SAndroid Build Coastguard Worker Min = FMRB_DoesNotAccessMemory;
251*9880d681SAndroid Build Coastguard Worker else if ((FI->getModRefInfo() & MRI_Mod) == 0)
252*9880d681SAndroid Build Coastguard Worker Min = FMRB_OnlyReadsMemory;
253*9880d681SAndroid Build Coastguard Worker }
254*9880d681SAndroid Build Coastguard Worker
255*9880d681SAndroid Build Coastguard Worker return FunctionModRefBehavior(AAResultBase::getModRefBehavior(CS) & Min);
256*9880d681SAndroid Build Coastguard Worker }
257*9880d681SAndroid Build Coastguard Worker
258*9880d681SAndroid Build Coastguard Worker /// Returns the function info for the function, or null if we don't have
259*9880d681SAndroid Build Coastguard Worker /// anything useful to say about it.
260*9880d681SAndroid Build Coastguard Worker GlobalsAAResult::FunctionInfo *
getFunctionInfo(const Function * F)261*9880d681SAndroid Build Coastguard Worker GlobalsAAResult::getFunctionInfo(const Function *F) {
262*9880d681SAndroid Build Coastguard Worker auto I = FunctionInfos.find(F);
263*9880d681SAndroid Build Coastguard Worker if (I != FunctionInfos.end())
264*9880d681SAndroid Build Coastguard Worker return &I->second;
265*9880d681SAndroid Build Coastguard Worker return nullptr;
266*9880d681SAndroid Build Coastguard Worker }
267*9880d681SAndroid Build Coastguard Worker
268*9880d681SAndroid Build Coastguard Worker /// AnalyzeGlobals - Scan through the users of all of the internal
269*9880d681SAndroid Build Coastguard Worker /// GlobalValue's in the program. If none of them have their "address taken"
270*9880d681SAndroid Build Coastguard Worker /// (really, their address passed to something nontrivial), record this fact,
271*9880d681SAndroid Build Coastguard Worker /// and record the functions that they are used directly in.
AnalyzeGlobals(Module & M)272*9880d681SAndroid Build Coastguard Worker void GlobalsAAResult::AnalyzeGlobals(Module &M) {
273*9880d681SAndroid Build Coastguard Worker SmallPtrSet<Function *, 32> TrackedFunctions;
274*9880d681SAndroid Build Coastguard Worker for (Function &F : M)
275*9880d681SAndroid Build Coastguard Worker if (F.hasLocalLinkage())
276*9880d681SAndroid Build Coastguard Worker if (!AnalyzeUsesOfPointer(&F)) {
277*9880d681SAndroid Build Coastguard Worker // Remember that we are tracking this global.
278*9880d681SAndroid Build Coastguard Worker NonAddressTakenGlobals.insert(&F);
279*9880d681SAndroid Build Coastguard Worker TrackedFunctions.insert(&F);
280*9880d681SAndroid Build Coastguard Worker Handles.emplace_front(*this, &F);
281*9880d681SAndroid Build Coastguard Worker Handles.front().I = Handles.begin();
282*9880d681SAndroid Build Coastguard Worker ++NumNonAddrTakenFunctions;
283*9880d681SAndroid Build Coastguard Worker }
284*9880d681SAndroid Build Coastguard Worker
285*9880d681SAndroid Build Coastguard Worker SmallPtrSet<Function *, 16> Readers, Writers;
286*9880d681SAndroid Build Coastguard Worker for (GlobalVariable &GV : M.globals())
287*9880d681SAndroid Build Coastguard Worker if (GV.hasLocalLinkage()) {
288*9880d681SAndroid Build Coastguard Worker if (!AnalyzeUsesOfPointer(&GV, &Readers,
289*9880d681SAndroid Build Coastguard Worker GV.isConstant() ? nullptr : &Writers)) {
290*9880d681SAndroid Build Coastguard Worker // Remember that we are tracking this global, and the mod/ref fns
291*9880d681SAndroid Build Coastguard Worker NonAddressTakenGlobals.insert(&GV);
292*9880d681SAndroid Build Coastguard Worker Handles.emplace_front(*this, &GV);
293*9880d681SAndroid Build Coastguard Worker Handles.front().I = Handles.begin();
294*9880d681SAndroid Build Coastguard Worker
295*9880d681SAndroid Build Coastguard Worker for (Function *Reader : Readers) {
296*9880d681SAndroid Build Coastguard Worker if (TrackedFunctions.insert(Reader).second) {
297*9880d681SAndroid Build Coastguard Worker Handles.emplace_front(*this, Reader);
298*9880d681SAndroid Build Coastguard Worker Handles.front().I = Handles.begin();
299*9880d681SAndroid Build Coastguard Worker }
300*9880d681SAndroid Build Coastguard Worker FunctionInfos[Reader].addModRefInfoForGlobal(GV, MRI_Ref);
301*9880d681SAndroid Build Coastguard Worker }
302*9880d681SAndroid Build Coastguard Worker
303*9880d681SAndroid Build Coastguard Worker if (!GV.isConstant()) // No need to keep track of writers to constants
304*9880d681SAndroid Build Coastguard Worker for (Function *Writer : Writers) {
305*9880d681SAndroid Build Coastguard Worker if (TrackedFunctions.insert(Writer).second) {
306*9880d681SAndroid Build Coastguard Worker Handles.emplace_front(*this, Writer);
307*9880d681SAndroid Build Coastguard Worker Handles.front().I = Handles.begin();
308*9880d681SAndroid Build Coastguard Worker }
309*9880d681SAndroid Build Coastguard Worker FunctionInfos[Writer].addModRefInfoForGlobal(GV, MRI_Mod);
310*9880d681SAndroid Build Coastguard Worker }
311*9880d681SAndroid Build Coastguard Worker ++NumNonAddrTakenGlobalVars;
312*9880d681SAndroid Build Coastguard Worker
313*9880d681SAndroid Build Coastguard Worker // If this global holds a pointer type, see if it is an indirect global.
314*9880d681SAndroid Build Coastguard Worker if (GV.getValueType()->isPointerTy() &&
315*9880d681SAndroid Build Coastguard Worker AnalyzeIndirectGlobalMemory(&GV))
316*9880d681SAndroid Build Coastguard Worker ++NumIndirectGlobalVars;
317*9880d681SAndroid Build Coastguard Worker }
318*9880d681SAndroid Build Coastguard Worker Readers.clear();
319*9880d681SAndroid Build Coastguard Worker Writers.clear();
320*9880d681SAndroid Build Coastguard Worker }
321*9880d681SAndroid Build Coastguard Worker }
322*9880d681SAndroid Build Coastguard Worker
323*9880d681SAndroid Build Coastguard Worker /// AnalyzeUsesOfPointer - Look at all of the users of the specified pointer.
324*9880d681SAndroid Build Coastguard Worker /// If this is used by anything complex (i.e., the address escapes), return
325*9880d681SAndroid Build Coastguard Worker /// true. Also, while we are at it, keep track of those functions that read and
326*9880d681SAndroid Build Coastguard Worker /// write to the value.
327*9880d681SAndroid Build Coastguard Worker ///
328*9880d681SAndroid Build Coastguard Worker /// If OkayStoreDest is non-null, stores into this global are allowed.
AnalyzeUsesOfPointer(Value * V,SmallPtrSetImpl<Function * > * Readers,SmallPtrSetImpl<Function * > * Writers,GlobalValue * OkayStoreDest)329*9880d681SAndroid Build Coastguard Worker bool GlobalsAAResult::AnalyzeUsesOfPointer(Value *V,
330*9880d681SAndroid Build Coastguard Worker SmallPtrSetImpl<Function *> *Readers,
331*9880d681SAndroid Build Coastguard Worker SmallPtrSetImpl<Function *> *Writers,
332*9880d681SAndroid Build Coastguard Worker GlobalValue *OkayStoreDest) {
333*9880d681SAndroid Build Coastguard Worker if (!V->getType()->isPointerTy())
334*9880d681SAndroid Build Coastguard Worker return true;
335*9880d681SAndroid Build Coastguard Worker
336*9880d681SAndroid Build Coastguard Worker for (Use &U : V->uses()) {
337*9880d681SAndroid Build Coastguard Worker User *I = U.getUser();
338*9880d681SAndroid Build Coastguard Worker if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
339*9880d681SAndroid Build Coastguard Worker if (Readers)
340*9880d681SAndroid Build Coastguard Worker Readers->insert(LI->getParent()->getParent());
341*9880d681SAndroid Build Coastguard Worker } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
342*9880d681SAndroid Build Coastguard Worker if (V == SI->getOperand(1)) {
343*9880d681SAndroid Build Coastguard Worker if (Writers)
344*9880d681SAndroid Build Coastguard Worker Writers->insert(SI->getParent()->getParent());
345*9880d681SAndroid Build Coastguard Worker } else if (SI->getOperand(1) != OkayStoreDest) {
346*9880d681SAndroid Build Coastguard Worker return true; // Storing the pointer
347*9880d681SAndroid Build Coastguard Worker }
348*9880d681SAndroid Build Coastguard Worker } else if (Operator::getOpcode(I) == Instruction::GetElementPtr) {
349*9880d681SAndroid Build Coastguard Worker if (AnalyzeUsesOfPointer(I, Readers, Writers))
350*9880d681SAndroid Build Coastguard Worker return true;
351*9880d681SAndroid Build Coastguard Worker } else if (Operator::getOpcode(I) == Instruction::BitCast) {
352*9880d681SAndroid Build Coastguard Worker if (AnalyzeUsesOfPointer(I, Readers, Writers, OkayStoreDest))
353*9880d681SAndroid Build Coastguard Worker return true;
354*9880d681SAndroid Build Coastguard Worker } else if (auto CS = CallSite(I)) {
355*9880d681SAndroid Build Coastguard Worker // Make sure that this is just the function being called, not that it is
356*9880d681SAndroid Build Coastguard Worker // passing into the function.
357*9880d681SAndroid Build Coastguard Worker if (CS.isDataOperand(&U)) {
358*9880d681SAndroid Build Coastguard Worker // Detect calls to free.
359*9880d681SAndroid Build Coastguard Worker if (CS.isArgOperand(&U) && isFreeCall(I, &TLI)) {
360*9880d681SAndroid Build Coastguard Worker if (Writers)
361*9880d681SAndroid Build Coastguard Worker Writers->insert(CS->getParent()->getParent());
362*9880d681SAndroid Build Coastguard Worker } else {
363*9880d681SAndroid Build Coastguard Worker return true; // Argument of an unknown call.
364*9880d681SAndroid Build Coastguard Worker }
365*9880d681SAndroid Build Coastguard Worker }
366*9880d681SAndroid Build Coastguard Worker } else if (ICmpInst *ICI = dyn_cast<ICmpInst>(I)) {
367*9880d681SAndroid Build Coastguard Worker if (!isa<ConstantPointerNull>(ICI->getOperand(1)))
368*9880d681SAndroid Build Coastguard Worker return true; // Allow comparison against null.
369*9880d681SAndroid Build Coastguard Worker } else {
370*9880d681SAndroid Build Coastguard Worker return true;
371*9880d681SAndroid Build Coastguard Worker }
372*9880d681SAndroid Build Coastguard Worker }
373*9880d681SAndroid Build Coastguard Worker
374*9880d681SAndroid Build Coastguard Worker return false;
375*9880d681SAndroid Build Coastguard Worker }
376*9880d681SAndroid Build Coastguard Worker
377*9880d681SAndroid Build Coastguard Worker /// AnalyzeIndirectGlobalMemory - We found an non-address-taken global variable
378*9880d681SAndroid Build Coastguard Worker /// which holds a pointer type. See if the global always points to non-aliased
379*9880d681SAndroid Build Coastguard Worker /// heap memory: that is, all initializers of the globals are allocations, and
380*9880d681SAndroid Build Coastguard Worker /// those allocations have no use other than initialization of the global.
381*9880d681SAndroid Build Coastguard Worker /// Further, all loads out of GV must directly use the memory, not store the
382*9880d681SAndroid Build Coastguard Worker /// pointer somewhere. If this is true, we consider the memory pointed to by
383*9880d681SAndroid Build Coastguard Worker /// GV to be owned by GV and can disambiguate other pointers from it.
AnalyzeIndirectGlobalMemory(GlobalVariable * GV)384*9880d681SAndroid Build Coastguard Worker bool GlobalsAAResult::AnalyzeIndirectGlobalMemory(GlobalVariable *GV) {
385*9880d681SAndroid Build Coastguard Worker // Keep track of values related to the allocation of the memory, f.e. the
386*9880d681SAndroid Build Coastguard Worker // value produced by the malloc call and any casts.
387*9880d681SAndroid Build Coastguard Worker std::vector<Value *> AllocRelatedValues;
388*9880d681SAndroid Build Coastguard Worker
389*9880d681SAndroid Build Coastguard Worker // If the initializer is a valid pointer, bail.
390*9880d681SAndroid Build Coastguard Worker if (Constant *C = GV->getInitializer())
391*9880d681SAndroid Build Coastguard Worker if (!C->isNullValue())
392*9880d681SAndroid Build Coastguard Worker return false;
393*9880d681SAndroid Build Coastguard Worker
394*9880d681SAndroid Build Coastguard Worker // Walk the user list of the global. If we find anything other than a direct
395*9880d681SAndroid Build Coastguard Worker // load or store, bail out.
396*9880d681SAndroid Build Coastguard Worker for (User *U : GV->users()) {
397*9880d681SAndroid Build Coastguard Worker if (LoadInst *LI = dyn_cast<LoadInst>(U)) {
398*9880d681SAndroid Build Coastguard Worker // The pointer loaded from the global can only be used in simple ways:
399*9880d681SAndroid Build Coastguard Worker // we allow addressing of it and loading storing to it. We do *not* allow
400*9880d681SAndroid Build Coastguard Worker // storing the loaded pointer somewhere else or passing to a function.
401*9880d681SAndroid Build Coastguard Worker if (AnalyzeUsesOfPointer(LI))
402*9880d681SAndroid Build Coastguard Worker return false; // Loaded pointer escapes.
403*9880d681SAndroid Build Coastguard Worker // TODO: Could try some IP mod/ref of the loaded pointer.
404*9880d681SAndroid Build Coastguard Worker } else if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
405*9880d681SAndroid Build Coastguard Worker // Storing the global itself.
406*9880d681SAndroid Build Coastguard Worker if (SI->getOperand(0) == GV)
407*9880d681SAndroid Build Coastguard Worker return false;
408*9880d681SAndroid Build Coastguard Worker
409*9880d681SAndroid Build Coastguard Worker // If storing the null pointer, ignore it.
410*9880d681SAndroid Build Coastguard Worker if (isa<ConstantPointerNull>(SI->getOperand(0)))
411*9880d681SAndroid Build Coastguard Worker continue;
412*9880d681SAndroid Build Coastguard Worker
413*9880d681SAndroid Build Coastguard Worker // Check the value being stored.
414*9880d681SAndroid Build Coastguard Worker Value *Ptr = GetUnderlyingObject(SI->getOperand(0),
415*9880d681SAndroid Build Coastguard Worker GV->getParent()->getDataLayout());
416*9880d681SAndroid Build Coastguard Worker
417*9880d681SAndroid Build Coastguard Worker if (!isAllocLikeFn(Ptr, &TLI))
418*9880d681SAndroid Build Coastguard Worker return false; // Too hard to analyze.
419*9880d681SAndroid Build Coastguard Worker
420*9880d681SAndroid Build Coastguard Worker // Analyze all uses of the allocation. If any of them are used in a
421*9880d681SAndroid Build Coastguard Worker // non-simple way (e.g. stored to another global) bail out.
422*9880d681SAndroid Build Coastguard Worker if (AnalyzeUsesOfPointer(Ptr, /*Readers*/ nullptr, /*Writers*/ nullptr,
423*9880d681SAndroid Build Coastguard Worker GV))
424*9880d681SAndroid Build Coastguard Worker return false; // Loaded pointer escapes.
425*9880d681SAndroid Build Coastguard Worker
426*9880d681SAndroid Build Coastguard Worker // Remember that this allocation is related to the indirect global.
427*9880d681SAndroid Build Coastguard Worker AllocRelatedValues.push_back(Ptr);
428*9880d681SAndroid Build Coastguard Worker } else {
429*9880d681SAndroid Build Coastguard Worker // Something complex, bail out.
430*9880d681SAndroid Build Coastguard Worker return false;
431*9880d681SAndroid Build Coastguard Worker }
432*9880d681SAndroid Build Coastguard Worker }
433*9880d681SAndroid Build Coastguard Worker
434*9880d681SAndroid Build Coastguard Worker // Okay, this is an indirect global. Remember all of the allocations for
435*9880d681SAndroid Build Coastguard Worker // this global in AllocsForIndirectGlobals.
436*9880d681SAndroid Build Coastguard Worker while (!AllocRelatedValues.empty()) {
437*9880d681SAndroid Build Coastguard Worker AllocsForIndirectGlobals[AllocRelatedValues.back()] = GV;
438*9880d681SAndroid Build Coastguard Worker Handles.emplace_front(*this, AllocRelatedValues.back());
439*9880d681SAndroid Build Coastguard Worker Handles.front().I = Handles.begin();
440*9880d681SAndroid Build Coastguard Worker AllocRelatedValues.pop_back();
441*9880d681SAndroid Build Coastguard Worker }
442*9880d681SAndroid Build Coastguard Worker IndirectGlobals.insert(GV);
443*9880d681SAndroid Build Coastguard Worker Handles.emplace_front(*this, GV);
444*9880d681SAndroid Build Coastguard Worker Handles.front().I = Handles.begin();
445*9880d681SAndroid Build Coastguard Worker return true;
446*9880d681SAndroid Build Coastguard Worker }
447*9880d681SAndroid Build Coastguard Worker
CollectSCCMembership(CallGraph & CG)448*9880d681SAndroid Build Coastguard Worker void GlobalsAAResult::CollectSCCMembership(CallGraph &CG) {
449*9880d681SAndroid Build Coastguard Worker // We do a bottom-up SCC traversal of the call graph. In other words, we
450*9880d681SAndroid Build Coastguard Worker // visit all callees before callers (leaf-first).
451*9880d681SAndroid Build Coastguard Worker unsigned SCCID = 0;
452*9880d681SAndroid Build Coastguard Worker for (scc_iterator<CallGraph *> I = scc_begin(&CG); !I.isAtEnd(); ++I) {
453*9880d681SAndroid Build Coastguard Worker const std::vector<CallGraphNode *> &SCC = *I;
454*9880d681SAndroid Build Coastguard Worker assert(!SCC.empty() && "SCC with no functions?");
455*9880d681SAndroid Build Coastguard Worker
456*9880d681SAndroid Build Coastguard Worker for (auto *CGN : SCC)
457*9880d681SAndroid Build Coastguard Worker if (Function *F = CGN->getFunction())
458*9880d681SAndroid Build Coastguard Worker FunctionToSCCMap[F] = SCCID;
459*9880d681SAndroid Build Coastguard Worker ++SCCID;
460*9880d681SAndroid Build Coastguard Worker }
461*9880d681SAndroid Build Coastguard Worker }
462*9880d681SAndroid Build Coastguard Worker
463*9880d681SAndroid Build Coastguard Worker /// AnalyzeCallGraph - At this point, we know the functions where globals are
464*9880d681SAndroid Build Coastguard Worker /// immediately stored to and read from. Propagate this information up the call
465*9880d681SAndroid Build Coastguard Worker /// graph to all callers and compute the mod/ref info for all memory for each
466*9880d681SAndroid Build Coastguard Worker /// function.
AnalyzeCallGraph(CallGraph & CG,Module & M)467*9880d681SAndroid Build Coastguard Worker void GlobalsAAResult::AnalyzeCallGraph(CallGraph &CG, Module &M) {
468*9880d681SAndroid Build Coastguard Worker // We do a bottom-up SCC traversal of the call graph. In other words, we
469*9880d681SAndroid Build Coastguard Worker // visit all callees before callers (leaf-first).
470*9880d681SAndroid Build Coastguard Worker for (scc_iterator<CallGraph *> I = scc_begin(&CG); !I.isAtEnd(); ++I) {
471*9880d681SAndroid Build Coastguard Worker const std::vector<CallGraphNode *> &SCC = *I;
472*9880d681SAndroid Build Coastguard Worker assert(!SCC.empty() && "SCC with no functions?");
473*9880d681SAndroid Build Coastguard Worker
474*9880d681SAndroid Build Coastguard Worker if (!SCC[0]->getFunction() || !SCC[0]->getFunction()->isDefinitionExact()) {
475*9880d681SAndroid Build Coastguard Worker // Calls externally or not exact - can't say anything useful. Remove any
476*9880d681SAndroid Build Coastguard Worker // existing function records (may have been created when scanning
477*9880d681SAndroid Build Coastguard Worker // globals).
478*9880d681SAndroid Build Coastguard Worker for (auto *Node : SCC)
479*9880d681SAndroid Build Coastguard Worker FunctionInfos.erase(Node->getFunction());
480*9880d681SAndroid Build Coastguard Worker continue;
481*9880d681SAndroid Build Coastguard Worker }
482*9880d681SAndroid Build Coastguard Worker
483*9880d681SAndroid Build Coastguard Worker FunctionInfo &FI = FunctionInfos[SCC[0]->getFunction()];
484*9880d681SAndroid Build Coastguard Worker bool KnowNothing = false;
485*9880d681SAndroid Build Coastguard Worker
486*9880d681SAndroid Build Coastguard Worker // Collect the mod/ref properties due to called functions. We only compute
487*9880d681SAndroid Build Coastguard Worker // one mod-ref set.
488*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = SCC.size(); i != e && !KnowNothing; ++i) {
489*9880d681SAndroid Build Coastguard Worker Function *F = SCC[i]->getFunction();
490*9880d681SAndroid Build Coastguard Worker if (!F) {
491*9880d681SAndroid Build Coastguard Worker KnowNothing = true;
492*9880d681SAndroid Build Coastguard Worker break;
493*9880d681SAndroid Build Coastguard Worker }
494*9880d681SAndroid Build Coastguard Worker
495*9880d681SAndroid Build Coastguard Worker if (F->isDeclaration()) {
496*9880d681SAndroid Build Coastguard Worker // Try to get mod/ref behaviour from function attributes.
497*9880d681SAndroid Build Coastguard Worker if (F->doesNotAccessMemory()) {
498*9880d681SAndroid Build Coastguard Worker // Can't do better than that!
499*9880d681SAndroid Build Coastguard Worker } else if (F->onlyReadsMemory()) {
500*9880d681SAndroid Build Coastguard Worker FI.addModRefInfo(MRI_Ref);
501*9880d681SAndroid Build Coastguard Worker if (!F->isIntrinsic() && !F->onlyAccessesArgMemory())
502*9880d681SAndroid Build Coastguard Worker // This function might call back into the module and read a global -
503*9880d681SAndroid Build Coastguard Worker // consider every global as possibly being read by this function.
504*9880d681SAndroid Build Coastguard Worker FI.setMayReadAnyGlobal();
505*9880d681SAndroid Build Coastguard Worker } else {
506*9880d681SAndroid Build Coastguard Worker FI.addModRefInfo(MRI_ModRef);
507*9880d681SAndroid Build Coastguard Worker // Can't say anything useful unless it's an intrinsic - they don't
508*9880d681SAndroid Build Coastguard Worker // read or write global variables of the kind considered here.
509*9880d681SAndroid Build Coastguard Worker KnowNothing = !F->isIntrinsic();
510*9880d681SAndroid Build Coastguard Worker }
511*9880d681SAndroid Build Coastguard Worker continue;
512*9880d681SAndroid Build Coastguard Worker }
513*9880d681SAndroid Build Coastguard Worker
514*9880d681SAndroid Build Coastguard Worker for (CallGraphNode::iterator CI = SCC[i]->begin(), E = SCC[i]->end();
515*9880d681SAndroid Build Coastguard Worker CI != E && !KnowNothing; ++CI)
516*9880d681SAndroid Build Coastguard Worker if (Function *Callee = CI->second->getFunction()) {
517*9880d681SAndroid Build Coastguard Worker if (FunctionInfo *CalleeFI = getFunctionInfo(Callee)) {
518*9880d681SAndroid Build Coastguard Worker // Propagate function effect up.
519*9880d681SAndroid Build Coastguard Worker FI.addFunctionInfo(*CalleeFI);
520*9880d681SAndroid Build Coastguard Worker } else {
521*9880d681SAndroid Build Coastguard Worker // Can't say anything about it. However, if it is inside our SCC,
522*9880d681SAndroid Build Coastguard Worker // then nothing needs to be done.
523*9880d681SAndroid Build Coastguard Worker CallGraphNode *CalleeNode = CG[Callee];
524*9880d681SAndroid Build Coastguard Worker if (std::find(SCC.begin(), SCC.end(), CalleeNode) == SCC.end())
525*9880d681SAndroid Build Coastguard Worker KnowNothing = true;
526*9880d681SAndroid Build Coastguard Worker }
527*9880d681SAndroid Build Coastguard Worker } else {
528*9880d681SAndroid Build Coastguard Worker KnowNothing = true;
529*9880d681SAndroid Build Coastguard Worker }
530*9880d681SAndroid Build Coastguard Worker }
531*9880d681SAndroid Build Coastguard Worker
532*9880d681SAndroid Build Coastguard Worker // If we can't say anything useful about this SCC, remove all SCC functions
533*9880d681SAndroid Build Coastguard Worker // from the FunctionInfos map.
534*9880d681SAndroid Build Coastguard Worker if (KnowNothing) {
535*9880d681SAndroid Build Coastguard Worker for (auto *Node : SCC)
536*9880d681SAndroid Build Coastguard Worker FunctionInfos.erase(Node->getFunction());
537*9880d681SAndroid Build Coastguard Worker continue;
538*9880d681SAndroid Build Coastguard Worker }
539*9880d681SAndroid Build Coastguard Worker
540*9880d681SAndroid Build Coastguard Worker // Scan the function bodies for explicit loads or stores.
541*9880d681SAndroid Build Coastguard Worker for (auto *Node : SCC) {
542*9880d681SAndroid Build Coastguard Worker if (FI.getModRefInfo() == MRI_ModRef)
543*9880d681SAndroid Build Coastguard Worker break; // The mod/ref lattice saturates here.
544*9880d681SAndroid Build Coastguard Worker for (Instruction &I : instructions(Node->getFunction())) {
545*9880d681SAndroid Build Coastguard Worker if (FI.getModRefInfo() == MRI_ModRef)
546*9880d681SAndroid Build Coastguard Worker break; // The mod/ref lattice saturates here.
547*9880d681SAndroid Build Coastguard Worker
548*9880d681SAndroid Build Coastguard Worker // We handle calls specially because the graph-relevant aspects are
549*9880d681SAndroid Build Coastguard Worker // handled above.
550*9880d681SAndroid Build Coastguard Worker if (auto CS = CallSite(&I)) {
551*9880d681SAndroid Build Coastguard Worker if (isAllocationFn(&I, &TLI) || isFreeCall(&I, &TLI)) {
552*9880d681SAndroid Build Coastguard Worker // FIXME: It is completely unclear why this is necessary and not
553*9880d681SAndroid Build Coastguard Worker // handled by the above graph code.
554*9880d681SAndroid Build Coastguard Worker FI.addModRefInfo(MRI_ModRef);
555*9880d681SAndroid Build Coastguard Worker } else if (Function *Callee = CS.getCalledFunction()) {
556*9880d681SAndroid Build Coastguard Worker // The callgraph doesn't include intrinsic calls.
557*9880d681SAndroid Build Coastguard Worker if (Callee->isIntrinsic()) {
558*9880d681SAndroid Build Coastguard Worker FunctionModRefBehavior Behaviour =
559*9880d681SAndroid Build Coastguard Worker AAResultBase::getModRefBehavior(Callee);
560*9880d681SAndroid Build Coastguard Worker FI.addModRefInfo(ModRefInfo(Behaviour & MRI_ModRef));
561*9880d681SAndroid Build Coastguard Worker }
562*9880d681SAndroid Build Coastguard Worker }
563*9880d681SAndroid Build Coastguard Worker continue;
564*9880d681SAndroid Build Coastguard Worker }
565*9880d681SAndroid Build Coastguard Worker
566*9880d681SAndroid Build Coastguard Worker // All non-call instructions we use the primary predicates for whether
567*9880d681SAndroid Build Coastguard Worker // thay read or write memory.
568*9880d681SAndroid Build Coastguard Worker if (I.mayReadFromMemory())
569*9880d681SAndroid Build Coastguard Worker FI.addModRefInfo(MRI_Ref);
570*9880d681SAndroid Build Coastguard Worker if (I.mayWriteToMemory())
571*9880d681SAndroid Build Coastguard Worker FI.addModRefInfo(MRI_Mod);
572*9880d681SAndroid Build Coastguard Worker }
573*9880d681SAndroid Build Coastguard Worker }
574*9880d681SAndroid Build Coastguard Worker
575*9880d681SAndroid Build Coastguard Worker if ((FI.getModRefInfo() & MRI_Mod) == 0)
576*9880d681SAndroid Build Coastguard Worker ++NumReadMemFunctions;
577*9880d681SAndroid Build Coastguard Worker if (FI.getModRefInfo() == MRI_NoModRef)
578*9880d681SAndroid Build Coastguard Worker ++NumNoMemFunctions;
579*9880d681SAndroid Build Coastguard Worker
580*9880d681SAndroid Build Coastguard Worker // Finally, now that we know the full effect on this SCC, clone the
581*9880d681SAndroid Build Coastguard Worker // information to each function in the SCC.
582*9880d681SAndroid Build Coastguard Worker // FI is a reference into FunctionInfos, so copy it now so that it doesn't
583*9880d681SAndroid Build Coastguard Worker // get invalidated if DenseMap decides to re-hash.
584*9880d681SAndroid Build Coastguard Worker FunctionInfo CachedFI = FI;
585*9880d681SAndroid Build Coastguard Worker for (unsigned i = 1, e = SCC.size(); i != e; ++i)
586*9880d681SAndroid Build Coastguard Worker FunctionInfos[SCC[i]->getFunction()] = CachedFI;
587*9880d681SAndroid Build Coastguard Worker }
588*9880d681SAndroid Build Coastguard Worker }
589*9880d681SAndroid Build Coastguard Worker
590*9880d681SAndroid Build Coastguard Worker // GV is a non-escaping global. V is a pointer address that has been loaded from.
591*9880d681SAndroid Build Coastguard Worker // If we can prove that V must escape, we can conclude that a load from V cannot
592*9880d681SAndroid Build Coastguard Worker // alias GV.
isNonEscapingGlobalNoAliasWithLoad(const GlobalValue * GV,const Value * V,int & Depth,const DataLayout & DL)593*9880d681SAndroid Build Coastguard Worker static bool isNonEscapingGlobalNoAliasWithLoad(const GlobalValue *GV,
594*9880d681SAndroid Build Coastguard Worker const Value *V,
595*9880d681SAndroid Build Coastguard Worker int &Depth,
596*9880d681SAndroid Build Coastguard Worker const DataLayout &DL) {
597*9880d681SAndroid Build Coastguard Worker SmallPtrSet<const Value *, 8> Visited;
598*9880d681SAndroid Build Coastguard Worker SmallVector<const Value *, 8> Inputs;
599*9880d681SAndroid Build Coastguard Worker Visited.insert(V);
600*9880d681SAndroid Build Coastguard Worker Inputs.push_back(V);
601*9880d681SAndroid Build Coastguard Worker do {
602*9880d681SAndroid Build Coastguard Worker const Value *Input = Inputs.pop_back_val();
603*9880d681SAndroid Build Coastguard Worker
604*9880d681SAndroid Build Coastguard Worker if (isa<GlobalValue>(Input) || isa<Argument>(Input) || isa<CallInst>(Input) ||
605*9880d681SAndroid Build Coastguard Worker isa<InvokeInst>(Input))
606*9880d681SAndroid Build Coastguard Worker // Arguments to functions or returns from functions are inherently
607*9880d681SAndroid Build Coastguard Worker // escaping, so we can immediately classify those as not aliasing any
608*9880d681SAndroid Build Coastguard Worker // non-addr-taken globals.
609*9880d681SAndroid Build Coastguard Worker //
610*9880d681SAndroid Build Coastguard Worker // (Transitive) loads from a global are also safe - if this aliased
611*9880d681SAndroid Build Coastguard Worker // another global, its address would escape, so no alias.
612*9880d681SAndroid Build Coastguard Worker continue;
613*9880d681SAndroid Build Coastguard Worker
614*9880d681SAndroid Build Coastguard Worker // Recurse through a limited number of selects, loads and PHIs. This is an
615*9880d681SAndroid Build Coastguard Worker // arbitrary depth of 4, lower numbers could be used to fix compile time
616*9880d681SAndroid Build Coastguard Worker // issues if needed, but this is generally expected to be only be important
617*9880d681SAndroid Build Coastguard Worker // for small depths.
618*9880d681SAndroid Build Coastguard Worker if (++Depth > 4)
619*9880d681SAndroid Build Coastguard Worker return false;
620*9880d681SAndroid Build Coastguard Worker
621*9880d681SAndroid Build Coastguard Worker if (auto *LI = dyn_cast<LoadInst>(Input)) {
622*9880d681SAndroid Build Coastguard Worker Inputs.push_back(GetUnderlyingObject(LI->getPointerOperand(), DL));
623*9880d681SAndroid Build Coastguard Worker continue;
624*9880d681SAndroid Build Coastguard Worker }
625*9880d681SAndroid Build Coastguard Worker if (auto *SI = dyn_cast<SelectInst>(Input)) {
626*9880d681SAndroid Build Coastguard Worker const Value *LHS = GetUnderlyingObject(SI->getTrueValue(), DL);
627*9880d681SAndroid Build Coastguard Worker const Value *RHS = GetUnderlyingObject(SI->getFalseValue(), DL);
628*9880d681SAndroid Build Coastguard Worker if (Visited.insert(LHS).second)
629*9880d681SAndroid Build Coastguard Worker Inputs.push_back(LHS);
630*9880d681SAndroid Build Coastguard Worker if (Visited.insert(RHS).second)
631*9880d681SAndroid Build Coastguard Worker Inputs.push_back(RHS);
632*9880d681SAndroid Build Coastguard Worker continue;
633*9880d681SAndroid Build Coastguard Worker }
634*9880d681SAndroid Build Coastguard Worker if (auto *PN = dyn_cast<PHINode>(Input)) {
635*9880d681SAndroid Build Coastguard Worker for (const Value *Op : PN->incoming_values()) {
636*9880d681SAndroid Build Coastguard Worker Op = GetUnderlyingObject(Op, DL);
637*9880d681SAndroid Build Coastguard Worker if (Visited.insert(Op).second)
638*9880d681SAndroid Build Coastguard Worker Inputs.push_back(Op);
639*9880d681SAndroid Build Coastguard Worker }
640*9880d681SAndroid Build Coastguard Worker continue;
641*9880d681SAndroid Build Coastguard Worker }
642*9880d681SAndroid Build Coastguard Worker
643*9880d681SAndroid Build Coastguard Worker return false;
644*9880d681SAndroid Build Coastguard Worker } while (!Inputs.empty());
645*9880d681SAndroid Build Coastguard Worker
646*9880d681SAndroid Build Coastguard Worker // All inputs were known to be no-alias.
647*9880d681SAndroid Build Coastguard Worker return true;
648*9880d681SAndroid Build Coastguard Worker }
649*9880d681SAndroid Build Coastguard Worker
650*9880d681SAndroid Build Coastguard Worker // There are particular cases where we can conclude no-alias between
651*9880d681SAndroid Build Coastguard Worker // a non-addr-taken global and some other underlying object. Specifically,
652*9880d681SAndroid Build Coastguard Worker // a non-addr-taken global is known to not be escaped from any function. It is
653*9880d681SAndroid Build Coastguard Worker // also incorrect for a transformation to introduce an escape of a global in
654*9880d681SAndroid Build Coastguard Worker // a way that is observable when it was not there previously. One function
655*9880d681SAndroid Build Coastguard Worker // being transformed to introduce an escape which could possibly be observed
656*9880d681SAndroid Build Coastguard Worker // (via loading from a global or the return value for example) within another
657*9880d681SAndroid Build Coastguard Worker // function is never safe. If the observation is made through non-atomic
658*9880d681SAndroid Build Coastguard Worker // operations on different threads, it is a data-race and UB. If the
659*9880d681SAndroid Build Coastguard Worker // observation is well defined, by being observed the transformation would have
660*9880d681SAndroid Build Coastguard Worker // changed program behavior by introducing the observed escape, making it an
661*9880d681SAndroid Build Coastguard Worker // invalid transform.
662*9880d681SAndroid Build Coastguard Worker //
663*9880d681SAndroid Build Coastguard Worker // This property does require that transformations which *temporarily* escape
664*9880d681SAndroid Build Coastguard Worker // a global that was not previously escaped, prior to restoring it, cannot rely
665*9880d681SAndroid Build Coastguard Worker // on the results of GMR::alias. This seems a reasonable restriction, although
666*9880d681SAndroid Build Coastguard Worker // currently there is no way to enforce it. There is also no realistic
667*9880d681SAndroid Build Coastguard Worker // optimization pass that would make this mistake. The closest example is
668*9880d681SAndroid Build Coastguard Worker // a transformation pass which does reg2mem of SSA values but stores them into
669*9880d681SAndroid Build Coastguard Worker // global variables temporarily before restoring the global variable's value.
670*9880d681SAndroid Build Coastguard Worker // This could be useful to expose "benign" races for example. However, it seems
671*9880d681SAndroid Build Coastguard Worker // reasonable to require that a pass which introduces escapes of global
672*9880d681SAndroid Build Coastguard Worker // variables in this way to either not trust AA results while the escape is
673*9880d681SAndroid Build Coastguard Worker // active, or to be forced to operate as a module pass that cannot co-exist
674*9880d681SAndroid Build Coastguard Worker // with an alias analysis such as GMR.
isNonEscapingGlobalNoAlias(const GlobalValue * GV,const Value * V)675*9880d681SAndroid Build Coastguard Worker bool GlobalsAAResult::isNonEscapingGlobalNoAlias(const GlobalValue *GV,
676*9880d681SAndroid Build Coastguard Worker const Value *V) {
677*9880d681SAndroid Build Coastguard Worker // In order to know that the underlying object cannot alias the
678*9880d681SAndroid Build Coastguard Worker // non-addr-taken global, we must know that it would have to be an escape.
679*9880d681SAndroid Build Coastguard Worker // Thus if the underlying object is a function argument, a load from
680*9880d681SAndroid Build Coastguard Worker // a global, or the return of a function, it cannot alias. We can also
681*9880d681SAndroid Build Coastguard Worker // recurse through PHI nodes and select nodes provided all of their inputs
682*9880d681SAndroid Build Coastguard Worker // resolve to one of these known-escaping roots.
683*9880d681SAndroid Build Coastguard Worker SmallPtrSet<const Value *, 8> Visited;
684*9880d681SAndroid Build Coastguard Worker SmallVector<const Value *, 8> Inputs;
685*9880d681SAndroid Build Coastguard Worker Visited.insert(V);
686*9880d681SAndroid Build Coastguard Worker Inputs.push_back(V);
687*9880d681SAndroid Build Coastguard Worker int Depth = 0;
688*9880d681SAndroid Build Coastguard Worker do {
689*9880d681SAndroid Build Coastguard Worker const Value *Input = Inputs.pop_back_val();
690*9880d681SAndroid Build Coastguard Worker
691*9880d681SAndroid Build Coastguard Worker if (auto *InputGV = dyn_cast<GlobalValue>(Input)) {
692*9880d681SAndroid Build Coastguard Worker // If one input is the very global we're querying against, then we can't
693*9880d681SAndroid Build Coastguard Worker // conclude no-alias.
694*9880d681SAndroid Build Coastguard Worker if (InputGV == GV)
695*9880d681SAndroid Build Coastguard Worker return false;
696*9880d681SAndroid Build Coastguard Worker
697*9880d681SAndroid Build Coastguard Worker // Distinct GlobalVariables never alias, unless overriden or zero-sized.
698*9880d681SAndroid Build Coastguard Worker // FIXME: The condition can be refined, but be conservative for now.
699*9880d681SAndroid Build Coastguard Worker auto *GVar = dyn_cast<GlobalVariable>(GV);
700*9880d681SAndroid Build Coastguard Worker auto *InputGVar = dyn_cast<GlobalVariable>(InputGV);
701*9880d681SAndroid Build Coastguard Worker if (GVar && InputGVar &&
702*9880d681SAndroid Build Coastguard Worker !GVar->isDeclaration() && !InputGVar->isDeclaration() &&
703*9880d681SAndroid Build Coastguard Worker !GVar->isInterposable() && !InputGVar->isInterposable()) {
704*9880d681SAndroid Build Coastguard Worker Type *GVType = GVar->getInitializer()->getType();
705*9880d681SAndroid Build Coastguard Worker Type *InputGVType = InputGVar->getInitializer()->getType();
706*9880d681SAndroid Build Coastguard Worker if (GVType->isSized() && InputGVType->isSized() &&
707*9880d681SAndroid Build Coastguard Worker (DL.getTypeAllocSize(GVType) > 0) &&
708*9880d681SAndroid Build Coastguard Worker (DL.getTypeAllocSize(InputGVType) > 0))
709*9880d681SAndroid Build Coastguard Worker continue;
710*9880d681SAndroid Build Coastguard Worker }
711*9880d681SAndroid Build Coastguard Worker
712*9880d681SAndroid Build Coastguard Worker // Conservatively return false, even though we could be smarter
713*9880d681SAndroid Build Coastguard Worker // (e.g. look through GlobalAliases).
714*9880d681SAndroid Build Coastguard Worker return false;
715*9880d681SAndroid Build Coastguard Worker }
716*9880d681SAndroid Build Coastguard Worker
717*9880d681SAndroid Build Coastguard Worker if (isa<Argument>(Input) || isa<CallInst>(Input) ||
718*9880d681SAndroid Build Coastguard Worker isa<InvokeInst>(Input)) {
719*9880d681SAndroid Build Coastguard Worker // Arguments to functions or returns from functions are inherently
720*9880d681SAndroid Build Coastguard Worker // escaping, so we can immediately classify those as not aliasing any
721*9880d681SAndroid Build Coastguard Worker // non-addr-taken globals.
722*9880d681SAndroid Build Coastguard Worker continue;
723*9880d681SAndroid Build Coastguard Worker }
724*9880d681SAndroid Build Coastguard Worker
725*9880d681SAndroid Build Coastguard Worker // Recurse through a limited number of selects, loads and PHIs. This is an
726*9880d681SAndroid Build Coastguard Worker // arbitrary depth of 4, lower numbers could be used to fix compile time
727*9880d681SAndroid Build Coastguard Worker // issues if needed, but this is generally expected to be only be important
728*9880d681SAndroid Build Coastguard Worker // for small depths.
729*9880d681SAndroid Build Coastguard Worker if (++Depth > 4)
730*9880d681SAndroid Build Coastguard Worker return false;
731*9880d681SAndroid Build Coastguard Worker
732*9880d681SAndroid Build Coastguard Worker if (auto *LI = dyn_cast<LoadInst>(Input)) {
733*9880d681SAndroid Build Coastguard Worker // A pointer loaded from a global would have been captured, and we know
734*9880d681SAndroid Build Coastguard Worker // that the global is non-escaping, so no alias.
735*9880d681SAndroid Build Coastguard Worker const Value *Ptr = GetUnderlyingObject(LI->getPointerOperand(), DL);
736*9880d681SAndroid Build Coastguard Worker if (isNonEscapingGlobalNoAliasWithLoad(GV, Ptr, Depth, DL))
737*9880d681SAndroid Build Coastguard Worker // The load does not alias with GV.
738*9880d681SAndroid Build Coastguard Worker continue;
739*9880d681SAndroid Build Coastguard Worker // Otherwise, a load could come from anywhere, so bail.
740*9880d681SAndroid Build Coastguard Worker return false;
741*9880d681SAndroid Build Coastguard Worker }
742*9880d681SAndroid Build Coastguard Worker if (auto *SI = dyn_cast<SelectInst>(Input)) {
743*9880d681SAndroid Build Coastguard Worker const Value *LHS = GetUnderlyingObject(SI->getTrueValue(), DL);
744*9880d681SAndroid Build Coastguard Worker const Value *RHS = GetUnderlyingObject(SI->getFalseValue(), DL);
745*9880d681SAndroid Build Coastguard Worker if (Visited.insert(LHS).second)
746*9880d681SAndroid Build Coastguard Worker Inputs.push_back(LHS);
747*9880d681SAndroid Build Coastguard Worker if (Visited.insert(RHS).second)
748*9880d681SAndroid Build Coastguard Worker Inputs.push_back(RHS);
749*9880d681SAndroid Build Coastguard Worker continue;
750*9880d681SAndroid Build Coastguard Worker }
751*9880d681SAndroid Build Coastguard Worker if (auto *PN = dyn_cast<PHINode>(Input)) {
752*9880d681SAndroid Build Coastguard Worker for (const Value *Op : PN->incoming_values()) {
753*9880d681SAndroid Build Coastguard Worker Op = GetUnderlyingObject(Op, DL);
754*9880d681SAndroid Build Coastguard Worker if (Visited.insert(Op).second)
755*9880d681SAndroid Build Coastguard Worker Inputs.push_back(Op);
756*9880d681SAndroid Build Coastguard Worker }
757*9880d681SAndroid Build Coastguard Worker continue;
758*9880d681SAndroid Build Coastguard Worker }
759*9880d681SAndroid Build Coastguard Worker
760*9880d681SAndroid Build Coastguard Worker // FIXME: It would be good to handle other obvious no-alias cases here, but
761*9880d681SAndroid Build Coastguard Worker // it isn't clear how to do so reasonbly without building a small version
762*9880d681SAndroid Build Coastguard Worker // of BasicAA into this code. We could recurse into AAResultBase::alias
763*9880d681SAndroid Build Coastguard Worker // here but that seems likely to go poorly as we're inside the
764*9880d681SAndroid Build Coastguard Worker // implementation of such a query. Until then, just conservatievly retun
765*9880d681SAndroid Build Coastguard Worker // false.
766*9880d681SAndroid Build Coastguard Worker return false;
767*9880d681SAndroid Build Coastguard Worker } while (!Inputs.empty());
768*9880d681SAndroid Build Coastguard Worker
769*9880d681SAndroid Build Coastguard Worker // If all the inputs to V were definitively no-alias, then V is no-alias.
770*9880d681SAndroid Build Coastguard Worker return true;
771*9880d681SAndroid Build Coastguard Worker }
772*9880d681SAndroid Build Coastguard Worker
773*9880d681SAndroid Build Coastguard Worker /// alias - If one of the pointers is to a global that we are tracking, and the
774*9880d681SAndroid Build Coastguard Worker /// other is some random pointer, we know there cannot be an alias, because the
775*9880d681SAndroid Build Coastguard Worker /// address of the global isn't taken.
alias(const MemoryLocation & LocA,const MemoryLocation & LocB)776*9880d681SAndroid Build Coastguard Worker AliasResult GlobalsAAResult::alias(const MemoryLocation &LocA,
777*9880d681SAndroid Build Coastguard Worker const MemoryLocation &LocB) {
778*9880d681SAndroid Build Coastguard Worker // Get the base object these pointers point to.
779*9880d681SAndroid Build Coastguard Worker const Value *UV1 = GetUnderlyingObject(LocA.Ptr, DL);
780*9880d681SAndroid Build Coastguard Worker const Value *UV2 = GetUnderlyingObject(LocB.Ptr, DL);
781*9880d681SAndroid Build Coastguard Worker
782*9880d681SAndroid Build Coastguard Worker // If either of the underlying values is a global, they may be non-addr-taken
783*9880d681SAndroid Build Coastguard Worker // globals, which we can answer queries about.
784*9880d681SAndroid Build Coastguard Worker const GlobalValue *GV1 = dyn_cast<GlobalValue>(UV1);
785*9880d681SAndroid Build Coastguard Worker const GlobalValue *GV2 = dyn_cast<GlobalValue>(UV2);
786*9880d681SAndroid Build Coastguard Worker if (GV1 || GV2) {
787*9880d681SAndroid Build Coastguard Worker // If the global's address is taken, pretend we don't know it's a pointer to
788*9880d681SAndroid Build Coastguard Worker // the global.
789*9880d681SAndroid Build Coastguard Worker if (GV1 && !NonAddressTakenGlobals.count(GV1))
790*9880d681SAndroid Build Coastguard Worker GV1 = nullptr;
791*9880d681SAndroid Build Coastguard Worker if (GV2 && !NonAddressTakenGlobals.count(GV2))
792*9880d681SAndroid Build Coastguard Worker GV2 = nullptr;
793*9880d681SAndroid Build Coastguard Worker
794*9880d681SAndroid Build Coastguard Worker // If the two pointers are derived from two different non-addr-taken
795*9880d681SAndroid Build Coastguard Worker // globals we know these can't alias.
796*9880d681SAndroid Build Coastguard Worker if (GV1 && GV2 && GV1 != GV2)
797*9880d681SAndroid Build Coastguard Worker return NoAlias;
798*9880d681SAndroid Build Coastguard Worker
799*9880d681SAndroid Build Coastguard Worker // If one is and the other isn't, it isn't strictly safe but we can fake
800*9880d681SAndroid Build Coastguard Worker // this result if necessary for performance. This does not appear to be
801*9880d681SAndroid Build Coastguard Worker // a common problem in practice.
802*9880d681SAndroid Build Coastguard Worker if (EnableUnsafeGlobalsModRefAliasResults)
803*9880d681SAndroid Build Coastguard Worker if ((GV1 || GV2) && GV1 != GV2)
804*9880d681SAndroid Build Coastguard Worker return NoAlias;
805*9880d681SAndroid Build Coastguard Worker
806*9880d681SAndroid Build Coastguard Worker // Check for a special case where a non-escaping global can be used to
807*9880d681SAndroid Build Coastguard Worker // conclude no-alias.
808*9880d681SAndroid Build Coastguard Worker if ((GV1 || GV2) && GV1 != GV2) {
809*9880d681SAndroid Build Coastguard Worker const GlobalValue *GV = GV1 ? GV1 : GV2;
810*9880d681SAndroid Build Coastguard Worker const Value *UV = GV1 ? UV2 : UV1;
811*9880d681SAndroid Build Coastguard Worker if (isNonEscapingGlobalNoAlias(GV, UV))
812*9880d681SAndroid Build Coastguard Worker return NoAlias;
813*9880d681SAndroid Build Coastguard Worker }
814*9880d681SAndroid Build Coastguard Worker
815*9880d681SAndroid Build Coastguard Worker // Otherwise if they are both derived from the same addr-taken global, we
816*9880d681SAndroid Build Coastguard Worker // can't know the two accesses don't overlap.
817*9880d681SAndroid Build Coastguard Worker }
818*9880d681SAndroid Build Coastguard Worker
819*9880d681SAndroid Build Coastguard Worker // These pointers may be based on the memory owned by an indirect global. If
820*9880d681SAndroid Build Coastguard Worker // so, we may be able to handle this. First check to see if the base pointer
821*9880d681SAndroid Build Coastguard Worker // is a direct load from an indirect global.
822*9880d681SAndroid Build Coastguard Worker GV1 = GV2 = nullptr;
823*9880d681SAndroid Build Coastguard Worker if (const LoadInst *LI = dyn_cast<LoadInst>(UV1))
824*9880d681SAndroid Build Coastguard Worker if (GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getOperand(0)))
825*9880d681SAndroid Build Coastguard Worker if (IndirectGlobals.count(GV))
826*9880d681SAndroid Build Coastguard Worker GV1 = GV;
827*9880d681SAndroid Build Coastguard Worker if (const LoadInst *LI = dyn_cast<LoadInst>(UV2))
828*9880d681SAndroid Build Coastguard Worker if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getOperand(0)))
829*9880d681SAndroid Build Coastguard Worker if (IndirectGlobals.count(GV))
830*9880d681SAndroid Build Coastguard Worker GV2 = GV;
831*9880d681SAndroid Build Coastguard Worker
832*9880d681SAndroid Build Coastguard Worker // These pointers may also be from an allocation for the indirect global. If
833*9880d681SAndroid Build Coastguard Worker // so, also handle them.
834*9880d681SAndroid Build Coastguard Worker if (!GV1)
835*9880d681SAndroid Build Coastguard Worker GV1 = AllocsForIndirectGlobals.lookup(UV1);
836*9880d681SAndroid Build Coastguard Worker if (!GV2)
837*9880d681SAndroid Build Coastguard Worker GV2 = AllocsForIndirectGlobals.lookup(UV2);
838*9880d681SAndroid Build Coastguard Worker
839*9880d681SAndroid Build Coastguard Worker // Now that we know whether the two pointers are related to indirect globals,
840*9880d681SAndroid Build Coastguard Worker // use this to disambiguate the pointers. If the pointers are based on
841*9880d681SAndroid Build Coastguard Worker // different indirect globals they cannot alias.
842*9880d681SAndroid Build Coastguard Worker if (GV1 && GV2 && GV1 != GV2)
843*9880d681SAndroid Build Coastguard Worker return NoAlias;
844*9880d681SAndroid Build Coastguard Worker
845*9880d681SAndroid Build Coastguard Worker // If one is based on an indirect global and the other isn't, it isn't
846*9880d681SAndroid Build Coastguard Worker // strictly safe but we can fake this result if necessary for performance.
847*9880d681SAndroid Build Coastguard Worker // This does not appear to be a common problem in practice.
848*9880d681SAndroid Build Coastguard Worker if (EnableUnsafeGlobalsModRefAliasResults)
849*9880d681SAndroid Build Coastguard Worker if ((GV1 || GV2) && GV1 != GV2)
850*9880d681SAndroid Build Coastguard Worker return NoAlias;
851*9880d681SAndroid Build Coastguard Worker
852*9880d681SAndroid Build Coastguard Worker return AAResultBase::alias(LocA, LocB);
853*9880d681SAndroid Build Coastguard Worker }
854*9880d681SAndroid Build Coastguard Worker
getModRefInfoForArgument(ImmutableCallSite CS,const GlobalValue * GV)855*9880d681SAndroid Build Coastguard Worker ModRefInfo GlobalsAAResult::getModRefInfoForArgument(ImmutableCallSite CS,
856*9880d681SAndroid Build Coastguard Worker const GlobalValue *GV) {
857*9880d681SAndroid Build Coastguard Worker if (CS.doesNotAccessMemory())
858*9880d681SAndroid Build Coastguard Worker return MRI_NoModRef;
859*9880d681SAndroid Build Coastguard Worker ModRefInfo ConservativeResult = CS.onlyReadsMemory() ? MRI_Ref : MRI_ModRef;
860*9880d681SAndroid Build Coastguard Worker
861*9880d681SAndroid Build Coastguard Worker // Iterate through all the arguments to the called function. If any argument
862*9880d681SAndroid Build Coastguard Worker // is based on GV, return the conservative result.
863*9880d681SAndroid Build Coastguard Worker for (auto &A : CS.args()) {
864*9880d681SAndroid Build Coastguard Worker SmallVector<Value*, 4> Objects;
865*9880d681SAndroid Build Coastguard Worker GetUnderlyingObjects(A, Objects, DL);
866*9880d681SAndroid Build Coastguard Worker
867*9880d681SAndroid Build Coastguard Worker // All objects must be identified.
868*9880d681SAndroid Build Coastguard Worker if (!std::all_of(Objects.begin(), Objects.end(), isIdentifiedObject) &&
869*9880d681SAndroid Build Coastguard Worker // Try ::alias to see if all objects are known not to alias GV.
870*9880d681SAndroid Build Coastguard Worker !std::all_of(Objects.begin(), Objects.end(), [&](Value *V) {
871*9880d681SAndroid Build Coastguard Worker return this->alias(MemoryLocation(V), MemoryLocation(GV)) == NoAlias;
872*9880d681SAndroid Build Coastguard Worker }))
873*9880d681SAndroid Build Coastguard Worker return ConservativeResult;
874*9880d681SAndroid Build Coastguard Worker
875*9880d681SAndroid Build Coastguard Worker if (std::find(Objects.begin(), Objects.end(), GV) != Objects.end())
876*9880d681SAndroid Build Coastguard Worker return ConservativeResult;
877*9880d681SAndroid Build Coastguard Worker }
878*9880d681SAndroid Build Coastguard Worker
879*9880d681SAndroid Build Coastguard Worker // We identified all objects in the argument list, and none of them were GV.
880*9880d681SAndroid Build Coastguard Worker return MRI_NoModRef;
881*9880d681SAndroid Build Coastguard Worker }
882*9880d681SAndroid Build Coastguard Worker
getModRefInfo(ImmutableCallSite CS,const MemoryLocation & Loc)883*9880d681SAndroid Build Coastguard Worker ModRefInfo GlobalsAAResult::getModRefInfo(ImmutableCallSite CS,
884*9880d681SAndroid Build Coastguard Worker const MemoryLocation &Loc) {
885*9880d681SAndroid Build Coastguard Worker unsigned Known = MRI_ModRef;
886*9880d681SAndroid Build Coastguard Worker
887*9880d681SAndroid Build Coastguard Worker // If we are asking for mod/ref info of a direct call with a pointer to a
888*9880d681SAndroid Build Coastguard Worker // global we are tracking, return information if we have it.
889*9880d681SAndroid Build Coastguard Worker if (const GlobalValue *GV =
890*9880d681SAndroid Build Coastguard Worker dyn_cast<GlobalValue>(GetUnderlyingObject(Loc.Ptr, DL)))
891*9880d681SAndroid Build Coastguard Worker if (GV->hasLocalLinkage())
892*9880d681SAndroid Build Coastguard Worker if (const Function *F = CS.getCalledFunction())
893*9880d681SAndroid Build Coastguard Worker if (NonAddressTakenGlobals.count(GV))
894*9880d681SAndroid Build Coastguard Worker if (const FunctionInfo *FI = getFunctionInfo(F))
895*9880d681SAndroid Build Coastguard Worker Known = FI->getModRefInfoForGlobal(*GV) |
896*9880d681SAndroid Build Coastguard Worker getModRefInfoForArgument(CS, GV);
897*9880d681SAndroid Build Coastguard Worker
898*9880d681SAndroid Build Coastguard Worker if (Known == MRI_NoModRef)
899*9880d681SAndroid Build Coastguard Worker return MRI_NoModRef; // No need to query other mod/ref analyses
900*9880d681SAndroid Build Coastguard Worker return ModRefInfo(Known & AAResultBase::getModRefInfo(CS, Loc));
901*9880d681SAndroid Build Coastguard Worker }
902*9880d681SAndroid Build Coastguard Worker
GlobalsAAResult(const DataLayout & DL,const TargetLibraryInfo & TLI)903*9880d681SAndroid Build Coastguard Worker GlobalsAAResult::GlobalsAAResult(const DataLayout &DL,
904*9880d681SAndroid Build Coastguard Worker const TargetLibraryInfo &TLI)
905*9880d681SAndroid Build Coastguard Worker : AAResultBase(), DL(DL), TLI(TLI) {}
906*9880d681SAndroid Build Coastguard Worker
GlobalsAAResult(GlobalsAAResult && Arg)907*9880d681SAndroid Build Coastguard Worker GlobalsAAResult::GlobalsAAResult(GlobalsAAResult &&Arg)
908*9880d681SAndroid Build Coastguard Worker : AAResultBase(std::move(Arg)), DL(Arg.DL), TLI(Arg.TLI),
909*9880d681SAndroid Build Coastguard Worker NonAddressTakenGlobals(std::move(Arg.NonAddressTakenGlobals)),
910*9880d681SAndroid Build Coastguard Worker IndirectGlobals(std::move(Arg.IndirectGlobals)),
911*9880d681SAndroid Build Coastguard Worker AllocsForIndirectGlobals(std::move(Arg.AllocsForIndirectGlobals)),
912*9880d681SAndroid Build Coastguard Worker FunctionInfos(std::move(Arg.FunctionInfos)),
913*9880d681SAndroid Build Coastguard Worker Handles(std::move(Arg.Handles)) {
914*9880d681SAndroid Build Coastguard Worker // Update the parent for each DeletionCallbackHandle.
915*9880d681SAndroid Build Coastguard Worker for (auto &H : Handles) {
916*9880d681SAndroid Build Coastguard Worker assert(H.GAR == &Arg);
917*9880d681SAndroid Build Coastguard Worker H.GAR = this;
918*9880d681SAndroid Build Coastguard Worker }
919*9880d681SAndroid Build Coastguard Worker }
920*9880d681SAndroid Build Coastguard Worker
~GlobalsAAResult()921*9880d681SAndroid Build Coastguard Worker GlobalsAAResult::~GlobalsAAResult() {}
922*9880d681SAndroid Build Coastguard Worker
923*9880d681SAndroid Build Coastguard Worker /*static*/ GlobalsAAResult
analyzeModule(Module & M,const TargetLibraryInfo & TLI,CallGraph & CG)924*9880d681SAndroid Build Coastguard Worker GlobalsAAResult::analyzeModule(Module &M, const TargetLibraryInfo &TLI,
925*9880d681SAndroid Build Coastguard Worker CallGraph &CG) {
926*9880d681SAndroid Build Coastguard Worker GlobalsAAResult Result(M.getDataLayout(), TLI);
927*9880d681SAndroid Build Coastguard Worker
928*9880d681SAndroid Build Coastguard Worker // Discover which functions aren't recursive, to feed into AnalyzeGlobals.
929*9880d681SAndroid Build Coastguard Worker Result.CollectSCCMembership(CG);
930*9880d681SAndroid Build Coastguard Worker
931*9880d681SAndroid Build Coastguard Worker // Find non-addr taken globals.
932*9880d681SAndroid Build Coastguard Worker Result.AnalyzeGlobals(M);
933*9880d681SAndroid Build Coastguard Worker
934*9880d681SAndroid Build Coastguard Worker // Propagate on CG.
935*9880d681SAndroid Build Coastguard Worker Result.AnalyzeCallGraph(CG, M);
936*9880d681SAndroid Build Coastguard Worker
937*9880d681SAndroid Build Coastguard Worker return Result;
938*9880d681SAndroid Build Coastguard Worker }
939*9880d681SAndroid Build Coastguard Worker
940*9880d681SAndroid Build Coastguard Worker char GlobalsAA::PassID;
941*9880d681SAndroid Build Coastguard Worker
run(Module & M,AnalysisManager<Module> & AM)942*9880d681SAndroid Build Coastguard Worker GlobalsAAResult GlobalsAA::run(Module &M, AnalysisManager<Module> &AM) {
943*9880d681SAndroid Build Coastguard Worker return GlobalsAAResult::analyzeModule(M,
944*9880d681SAndroid Build Coastguard Worker AM.getResult<TargetLibraryAnalysis>(M),
945*9880d681SAndroid Build Coastguard Worker AM.getResult<CallGraphAnalysis>(M));
946*9880d681SAndroid Build Coastguard Worker }
947*9880d681SAndroid Build Coastguard Worker
948*9880d681SAndroid Build Coastguard Worker char GlobalsAAWrapperPass::ID = 0;
949*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_BEGIN(GlobalsAAWrapperPass, "globals-aa",
950*9880d681SAndroid Build Coastguard Worker "Globals Alias Analysis", false, true)
INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)951*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
952*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
953*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_END(GlobalsAAWrapperPass, "globals-aa",
954*9880d681SAndroid Build Coastguard Worker "Globals Alias Analysis", false, true)
955*9880d681SAndroid Build Coastguard Worker
956*9880d681SAndroid Build Coastguard Worker ModulePass *llvm::createGlobalsAAWrapperPass() {
957*9880d681SAndroid Build Coastguard Worker return new GlobalsAAWrapperPass();
958*9880d681SAndroid Build Coastguard Worker }
959*9880d681SAndroid Build Coastguard Worker
GlobalsAAWrapperPass()960*9880d681SAndroid Build Coastguard Worker GlobalsAAWrapperPass::GlobalsAAWrapperPass() : ModulePass(ID) {
961*9880d681SAndroid Build Coastguard Worker initializeGlobalsAAWrapperPassPass(*PassRegistry::getPassRegistry());
962*9880d681SAndroid Build Coastguard Worker }
963*9880d681SAndroid Build Coastguard Worker
runOnModule(Module & M)964*9880d681SAndroid Build Coastguard Worker bool GlobalsAAWrapperPass::runOnModule(Module &M) {
965*9880d681SAndroid Build Coastguard Worker Result.reset(new GlobalsAAResult(GlobalsAAResult::analyzeModule(
966*9880d681SAndroid Build Coastguard Worker M, getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(),
967*9880d681SAndroid Build Coastguard Worker getAnalysis<CallGraphWrapperPass>().getCallGraph())));
968*9880d681SAndroid Build Coastguard Worker return false;
969*9880d681SAndroid Build Coastguard Worker }
970*9880d681SAndroid Build Coastguard Worker
doFinalization(Module & M)971*9880d681SAndroid Build Coastguard Worker bool GlobalsAAWrapperPass::doFinalization(Module &M) {
972*9880d681SAndroid Build Coastguard Worker Result.reset();
973*9880d681SAndroid Build Coastguard Worker return false;
974*9880d681SAndroid Build Coastguard Worker }
975*9880d681SAndroid Build Coastguard Worker
getAnalysisUsage(AnalysisUsage & AU) const976*9880d681SAndroid Build Coastguard Worker void GlobalsAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
977*9880d681SAndroid Build Coastguard Worker AU.setPreservesAll();
978*9880d681SAndroid Build Coastguard Worker AU.addRequired<CallGraphWrapperPass>();
979*9880d681SAndroid Build Coastguard Worker AU.addRequired<TargetLibraryInfoWrapperPass>();
980*9880d681SAndroid Build Coastguard Worker }
981