xref: /aosp_15_r20/external/llvm/lib/Analysis/AliasAnalysis.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===- AliasAnalysis.cpp - Generic Alias Analysis Interface Implementation -==//
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 file implements the generic AliasAnalysis interface which is used as the
11*9880d681SAndroid Build Coastguard Worker // common interface used by all clients and implementations of alias analysis.
12*9880d681SAndroid Build Coastguard Worker //
13*9880d681SAndroid Build Coastguard Worker // This file also implements the default version of the AliasAnalysis interface
14*9880d681SAndroid Build Coastguard Worker // that is to be used when no other implementation is specified.  This does some
15*9880d681SAndroid Build Coastguard Worker // simple tests that detect obvious cases: two different global pointers cannot
16*9880d681SAndroid Build Coastguard Worker // alias, a global cannot alias a malloc, two different mallocs cannot alias,
17*9880d681SAndroid Build Coastguard Worker // etc.
18*9880d681SAndroid Build Coastguard Worker //
19*9880d681SAndroid Build Coastguard Worker // This alias analysis implementation really isn't very good for anything, but
20*9880d681SAndroid Build Coastguard Worker // it is very fast, and makes a nice clean default implementation.  Because it
21*9880d681SAndroid Build Coastguard Worker // handles lots of little corner cases, other, more complex, alias analysis
22*9880d681SAndroid Build Coastguard Worker // implementations may choose to rely on this pass to resolve these simple and
23*9880d681SAndroid Build Coastguard Worker // easy cases.
24*9880d681SAndroid Build Coastguard Worker //
25*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
26*9880d681SAndroid Build Coastguard Worker 
27*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/AliasAnalysis.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/BasicAliasAnalysis.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/CFG.h"
30*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/CFLAndersAliasAnalysis.h"
31*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/CFLSteensAliasAnalysis.h"
32*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/CaptureTracking.h"
33*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/GlobalsModRef.h"
34*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/ObjCARCAliasAnalysis.h"
35*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
36*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/ScopedNoAliasAA.h"
37*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/TargetLibraryInfo.h"
38*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/TypeBasedAliasAnalysis.h"
39*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/ValueTracking.h"
40*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/BasicBlock.h"
41*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/DataLayout.h"
42*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Dominators.h"
43*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Function.h"
44*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Instructions.h"
45*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/IntrinsicInst.h"
46*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/LLVMContext.h"
47*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Type.h"
48*9880d681SAndroid Build Coastguard Worker #include "llvm/Pass.h"
49*9880d681SAndroid Build Coastguard Worker using namespace llvm;
50*9880d681SAndroid Build Coastguard Worker 
51*9880d681SAndroid Build Coastguard Worker /// Allow disabling BasicAA from the AA results. This is particularly useful
52*9880d681SAndroid Build Coastguard Worker /// when testing to isolate a single AA implementation.
53*9880d681SAndroid Build Coastguard Worker static cl::opt<bool> DisableBasicAA("disable-basicaa", cl::Hidden,
54*9880d681SAndroid Build Coastguard Worker                                     cl::init(false));
55*9880d681SAndroid Build Coastguard Worker 
AAResults(AAResults && Arg)56*9880d681SAndroid Build Coastguard Worker AAResults::AAResults(AAResults &&Arg) : TLI(Arg.TLI), AAs(std::move(Arg.AAs)) {
57*9880d681SAndroid Build Coastguard Worker   for (auto &AA : AAs)
58*9880d681SAndroid Build Coastguard Worker     AA->setAAResults(this);
59*9880d681SAndroid Build Coastguard Worker }
60*9880d681SAndroid Build Coastguard Worker 
~AAResults()61*9880d681SAndroid Build Coastguard Worker AAResults::~AAResults() {
62*9880d681SAndroid Build Coastguard Worker // FIXME; It would be nice to at least clear out the pointers back to this
63*9880d681SAndroid Build Coastguard Worker // aggregation here, but we end up with non-nesting lifetimes in the legacy
64*9880d681SAndroid Build Coastguard Worker // pass manager that prevent this from working. In the legacy pass manager
65*9880d681SAndroid Build Coastguard Worker // we'll end up with dangling references here in some cases.
66*9880d681SAndroid Build Coastguard Worker #if 0
67*9880d681SAndroid Build Coastguard Worker   for (auto &AA : AAs)
68*9880d681SAndroid Build Coastguard Worker     AA->setAAResults(nullptr);
69*9880d681SAndroid Build Coastguard Worker #endif
70*9880d681SAndroid Build Coastguard Worker }
71*9880d681SAndroid Build Coastguard Worker 
72*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
73*9880d681SAndroid Build Coastguard Worker // Default chaining methods
74*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
75*9880d681SAndroid Build Coastguard Worker 
alias(const MemoryLocation & LocA,const MemoryLocation & LocB)76*9880d681SAndroid Build Coastguard Worker AliasResult AAResults::alias(const MemoryLocation &LocA,
77*9880d681SAndroid Build Coastguard Worker                              const MemoryLocation &LocB) {
78*9880d681SAndroid Build Coastguard Worker   for (const auto &AA : AAs) {
79*9880d681SAndroid Build Coastguard Worker     auto Result = AA->alias(LocA, LocB);
80*9880d681SAndroid Build Coastguard Worker     if (Result != MayAlias)
81*9880d681SAndroid Build Coastguard Worker       return Result;
82*9880d681SAndroid Build Coastguard Worker   }
83*9880d681SAndroid Build Coastguard Worker   return MayAlias;
84*9880d681SAndroid Build Coastguard Worker }
85*9880d681SAndroid Build Coastguard Worker 
pointsToConstantMemory(const MemoryLocation & Loc,bool OrLocal)86*9880d681SAndroid Build Coastguard Worker bool AAResults::pointsToConstantMemory(const MemoryLocation &Loc,
87*9880d681SAndroid Build Coastguard Worker                                        bool OrLocal) {
88*9880d681SAndroid Build Coastguard Worker   for (const auto &AA : AAs)
89*9880d681SAndroid Build Coastguard Worker     if (AA->pointsToConstantMemory(Loc, OrLocal))
90*9880d681SAndroid Build Coastguard Worker       return true;
91*9880d681SAndroid Build Coastguard Worker 
92*9880d681SAndroid Build Coastguard Worker   return false;
93*9880d681SAndroid Build Coastguard Worker }
94*9880d681SAndroid Build Coastguard Worker 
getArgModRefInfo(ImmutableCallSite CS,unsigned ArgIdx)95*9880d681SAndroid Build Coastguard Worker ModRefInfo AAResults::getArgModRefInfo(ImmutableCallSite CS, unsigned ArgIdx) {
96*9880d681SAndroid Build Coastguard Worker   ModRefInfo Result = MRI_ModRef;
97*9880d681SAndroid Build Coastguard Worker 
98*9880d681SAndroid Build Coastguard Worker   for (const auto &AA : AAs) {
99*9880d681SAndroid Build Coastguard Worker     Result = ModRefInfo(Result & AA->getArgModRefInfo(CS, ArgIdx));
100*9880d681SAndroid Build Coastguard Worker 
101*9880d681SAndroid Build Coastguard Worker     // Early-exit the moment we reach the bottom of the lattice.
102*9880d681SAndroid Build Coastguard Worker     if (Result == MRI_NoModRef)
103*9880d681SAndroid Build Coastguard Worker       return Result;
104*9880d681SAndroid Build Coastguard Worker   }
105*9880d681SAndroid Build Coastguard Worker 
106*9880d681SAndroid Build Coastguard Worker   return Result;
107*9880d681SAndroid Build Coastguard Worker }
108*9880d681SAndroid Build Coastguard Worker 
getModRefInfo(Instruction * I,ImmutableCallSite Call)109*9880d681SAndroid Build Coastguard Worker ModRefInfo AAResults::getModRefInfo(Instruction *I, ImmutableCallSite Call) {
110*9880d681SAndroid Build Coastguard Worker   // We may have two calls
111*9880d681SAndroid Build Coastguard Worker   if (auto CS = ImmutableCallSite(I)) {
112*9880d681SAndroid Build Coastguard Worker     // Check if the two calls modify the same memory
113*9880d681SAndroid Build Coastguard Worker     return getModRefInfo(CS, Call);
114*9880d681SAndroid Build Coastguard Worker   } else {
115*9880d681SAndroid Build Coastguard Worker     // Otherwise, check if the call modifies or references the
116*9880d681SAndroid Build Coastguard Worker     // location this memory access defines.  The best we can say
117*9880d681SAndroid Build Coastguard Worker     // is that if the call references what this instruction
118*9880d681SAndroid Build Coastguard Worker     // defines, it must be clobbered by this location.
119*9880d681SAndroid Build Coastguard Worker     const MemoryLocation DefLoc = MemoryLocation::get(I);
120*9880d681SAndroid Build Coastguard Worker     if (getModRefInfo(Call, DefLoc) != MRI_NoModRef)
121*9880d681SAndroid Build Coastguard Worker       return MRI_ModRef;
122*9880d681SAndroid Build Coastguard Worker   }
123*9880d681SAndroid Build Coastguard Worker   return MRI_NoModRef;
124*9880d681SAndroid Build Coastguard Worker }
125*9880d681SAndroid Build Coastguard Worker 
getModRefInfo(ImmutableCallSite CS,const MemoryLocation & Loc)126*9880d681SAndroid Build Coastguard Worker ModRefInfo AAResults::getModRefInfo(ImmutableCallSite CS,
127*9880d681SAndroid Build Coastguard Worker                                     const MemoryLocation &Loc) {
128*9880d681SAndroid Build Coastguard Worker   ModRefInfo Result = MRI_ModRef;
129*9880d681SAndroid Build Coastguard Worker 
130*9880d681SAndroid Build Coastguard Worker   for (const auto &AA : AAs) {
131*9880d681SAndroid Build Coastguard Worker     Result = ModRefInfo(Result & AA->getModRefInfo(CS, Loc));
132*9880d681SAndroid Build Coastguard Worker 
133*9880d681SAndroid Build Coastguard Worker     // Early-exit the moment we reach the bottom of the lattice.
134*9880d681SAndroid Build Coastguard Worker     if (Result == MRI_NoModRef)
135*9880d681SAndroid Build Coastguard Worker       return Result;
136*9880d681SAndroid Build Coastguard Worker   }
137*9880d681SAndroid Build Coastguard Worker 
138*9880d681SAndroid Build Coastguard Worker   // Try to refine the mod-ref info further using other API entry points to the
139*9880d681SAndroid Build Coastguard Worker   // aggregate set of AA results.
140*9880d681SAndroid Build Coastguard Worker   auto MRB = getModRefBehavior(CS);
141*9880d681SAndroid Build Coastguard Worker   if (MRB == FMRB_DoesNotAccessMemory)
142*9880d681SAndroid Build Coastguard Worker     return MRI_NoModRef;
143*9880d681SAndroid Build Coastguard Worker 
144*9880d681SAndroid Build Coastguard Worker   if (onlyReadsMemory(MRB))
145*9880d681SAndroid Build Coastguard Worker     Result = ModRefInfo(Result & MRI_Ref);
146*9880d681SAndroid Build Coastguard Worker   else if (doesNotReadMemory(MRB))
147*9880d681SAndroid Build Coastguard Worker     Result = ModRefInfo(Result & MRI_Mod);
148*9880d681SAndroid Build Coastguard Worker 
149*9880d681SAndroid Build Coastguard Worker   if (onlyAccessesArgPointees(MRB)) {
150*9880d681SAndroid Build Coastguard Worker     bool DoesAlias = false;
151*9880d681SAndroid Build Coastguard Worker     ModRefInfo AllArgsMask = MRI_NoModRef;
152*9880d681SAndroid Build Coastguard Worker     if (doesAccessArgPointees(MRB)) {
153*9880d681SAndroid Build Coastguard Worker       for (auto AI = CS.arg_begin(), AE = CS.arg_end(); AI != AE; ++AI) {
154*9880d681SAndroid Build Coastguard Worker         const Value *Arg = *AI;
155*9880d681SAndroid Build Coastguard Worker         if (!Arg->getType()->isPointerTy())
156*9880d681SAndroid Build Coastguard Worker           continue;
157*9880d681SAndroid Build Coastguard Worker         unsigned ArgIdx = std::distance(CS.arg_begin(), AI);
158*9880d681SAndroid Build Coastguard Worker         MemoryLocation ArgLoc = MemoryLocation::getForArgument(CS, ArgIdx, TLI);
159*9880d681SAndroid Build Coastguard Worker         AliasResult ArgAlias = alias(ArgLoc, Loc);
160*9880d681SAndroid Build Coastguard Worker         if (ArgAlias != NoAlias) {
161*9880d681SAndroid Build Coastguard Worker           ModRefInfo ArgMask = getArgModRefInfo(CS, ArgIdx);
162*9880d681SAndroid Build Coastguard Worker           DoesAlias = true;
163*9880d681SAndroid Build Coastguard Worker           AllArgsMask = ModRefInfo(AllArgsMask | ArgMask);
164*9880d681SAndroid Build Coastguard Worker         }
165*9880d681SAndroid Build Coastguard Worker       }
166*9880d681SAndroid Build Coastguard Worker     }
167*9880d681SAndroid Build Coastguard Worker     if (!DoesAlias)
168*9880d681SAndroid Build Coastguard Worker       return MRI_NoModRef;
169*9880d681SAndroid Build Coastguard Worker     Result = ModRefInfo(Result & AllArgsMask);
170*9880d681SAndroid Build Coastguard Worker   }
171*9880d681SAndroid Build Coastguard Worker 
172*9880d681SAndroid Build Coastguard Worker   // If Loc is a constant memory location, the call definitely could not
173*9880d681SAndroid Build Coastguard Worker   // modify the memory location.
174*9880d681SAndroid Build Coastguard Worker   if ((Result & MRI_Mod) &&
175*9880d681SAndroid Build Coastguard Worker       pointsToConstantMemory(Loc, /*OrLocal*/ false))
176*9880d681SAndroid Build Coastguard Worker     Result = ModRefInfo(Result & ~MRI_Mod);
177*9880d681SAndroid Build Coastguard Worker 
178*9880d681SAndroid Build Coastguard Worker   return Result;
179*9880d681SAndroid Build Coastguard Worker }
180*9880d681SAndroid Build Coastguard Worker 
getModRefInfo(ImmutableCallSite CS1,ImmutableCallSite CS2)181*9880d681SAndroid Build Coastguard Worker ModRefInfo AAResults::getModRefInfo(ImmutableCallSite CS1,
182*9880d681SAndroid Build Coastguard Worker                                     ImmutableCallSite CS2) {
183*9880d681SAndroid Build Coastguard Worker   ModRefInfo Result = MRI_ModRef;
184*9880d681SAndroid Build Coastguard Worker 
185*9880d681SAndroid Build Coastguard Worker   for (const auto &AA : AAs) {
186*9880d681SAndroid Build Coastguard Worker     Result = ModRefInfo(Result & AA->getModRefInfo(CS1, CS2));
187*9880d681SAndroid Build Coastguard Worker 
188*9880d681SAndroid Build Coastguard Worker     // Early-exit the moment we reach the bottom of the lattice.
189*9880d681SAndroid Build Coastguard Worker     if (Result == MRI_NoModRef)
190*9880d681SAndroid Build Coastguard Worker       return Result;
191*9880d681SAndroid Build Coastguard Worker   }
192*9880d681SAndroid Build Coastguard Worker 
193*9880d681SAndroid Build Coastguard Worker   // Try to refine the mod-ref info further using other API entry points to the
194*9880d681SAndroid Build Coastguard Worker   // aggregate set of AA results.
195*9880d681SAndroid Build Coastguard Worker 
196*9880d681SAndroid Build Coastguard Worker   // If CS1 or CS2 are readnone, they don't interact.
197*9880d681SAndroid Build Coastguard Worker   auto CS1B = getModRefBehavior(CS1);
198*9880d681SAndroid Build Coastguard Worker   if (CS1B == FMRB_DoesNotAccessMemory)
199*9880d681SAndroid Build Coastguard Worker     return MRI_NoModRef;
200*9880d681SAndroid Build Coastguard Worker 
201*9880d681SAndroid Build Coastguard Worker   auto CS2B = getModRefBehavior(CS2);
202*9880d681SAndroid Build Coastguard Worker   if (CS2B == FMRB_DoesNotAccessMemory)
203*9880d681SAndroid Build Coastguard Worker     return MRI_NoModRef;
204*9880d681SAndroid Build Coastguard Worker 
205*9880d681SAndroid Build Coastguard Worker   // If they both only read from memory, there is no dependence.
206*9880d681SAndroid Build Coastguard Worker   if (onlyReadsMemory(CS1B) && onlyReadsMemory(CS2B))
207*9880d681SAndroid Build Coastguard Worker     return MRI_NoModRef;
208*9880d681SAndroid Build Coastguard Worker 
209*9880d681SAndroid Build Coastguard Worker   // If CS1 only reads memory, the only dependence on CS2 can be
210*9880d681SAndroid Build Coastguard Worker   // from CS1 reading memory written by CS2.
211*9880d681SAndroid Build Coastguard Worker   if (onlyReadsMemory(CS1B))
212*9880d681SAndroid Build Coastguard Worker     Result = ModRefInfo(Result & MRI_Ref);
213*9880d681SAndroid Build Coastguard Worker   else if (doesNotReadMemory(CS1B))
214*9880d681SAndroid Build Coastguard Worker     Result = ModRefInfo(Result & MRI_Mod);
215*9880d681SAndroid Build Coastguard Worker 
216*9880d681SAndroid Build Coastguard Worker   // If CS2 only access memory through arguments, accumulate the mod/ref
217*9880d681SAndroid Build Coastguard Worker   // information from CS1's references to the memory referenced by
218*9880d681SAndroid Build Coastguard Worker   // CS2's arguments.
219*9880d681SAndroid Build Coastguard Worker   if (onlyAccessesArgPointees(CS2B)) {
220*9880d681SAndroid Build Coastguard Worker     ModRefInfo R = MRI_NoModRef;
221*9880d681SAndroid Build Coastguard Worker     if (doesAccessArgPointees(CS2B)) {
222*9880d681SAndroid Build Coastguard Worker       for (auto I = CS2.arg_begin(), E = CS2.arg_end(); I != E; ++I) {
223*9880d681SAndroid Build Coastguard Worker         const Value *Arg = *I;
224*9880d681SAndroid Build Coastguard Worker         if (!Arg->getType()->isPointerTy())
225*9880d681SAndroid Build Coastguard Worker           continue;
226*9880d681SAndroid Build Coastguard Worker         unsigned CS2ArgIdx = std::distance(CS2.arg_begin(), I);
227*9880d681SAndroid Build Coastguard Worker         auto CS2ArgLoc = MemoryLocation::getForArgument(CS2, CS2ArgIdx, TLI);
228*9880d681SAndroid Build Coastguard Worker 
229*9880d681SAndroid Build Coastguard Worker         // ArgMask indicates what CS2 might do to CS2ArgLoc, and the dependence
230*9880d681SAndroid Build Coastguard Worker         // of CS1 on that location is the inverse.
231*9880d681SAndroid Build Coastguard Worker         ModRefInfo ArgMask = getArgModRefInfo(CS2, CS2ArgIdx);
232*9880d681SAndroid Build Coastguard Worker         if (ArgMask == MRI_Mod)
233*9880d681SAndroid Build Coastguard Worker           ArgMask = MRI_ModRef;
234*9880d681SAndroid Build Coastguard Worker         else if (ArgMask == MRI_Ref)
235*9880d681SAndroid Build Coastguard Worker           ArgMask = MRI_Mod;
236*9880d681SAndroid Build Coastguard Worker 
237*9880d681SAndroid Build Coastguard Worker         ArgMask = ModRefInfo(ArgMask & getModRefInfo(CS1, CS2ArgLoc));
238*9880d681SAndroid Build Coastguard Worker 
239*9880d681SAndroid Build Coastguard Worker         R = ModRefInfo((R | ArgMask) & Result);
240*9880d681SAndroid Build Coastguard Worker         if (R == Result)
241*9880d681SAndroid Build Coastguard Worker           break;
242*9880d681SAndroid Build Coastguard Worker       }
243*9880d681SAndroid Build Coastguard Worker     }
244*9880d681SAndroid Build Coastguard Worker     return R;
245*9880d681SAndroid Build Coastguard Worker   }
246*9880d681SAndroid Build Coastguard Worker 
247*9880d681SAndroid Build Coastguard Worker   // If CS1 only accesses memory through arguments, check if CS2 references
248*9880d681SAndroid Build Coastguard Worker   // any of the memory referenced by CS1's arguments. If not, return NoModRef.
249*9880d681SAndroid Build Coastguard Worker   if (onlyAccessesArgPointees(CS1B)) {
250*9880d681SAndroid Build Coastguard Worker     ModRefInfo R = MRI_NoModRef;
251*9880d681SAndroid Build Coastguard Worker     if (doesAccessArgPointees(CS1B)) {
252*9880d681SAndroid Build Coastguard Worker       for (auto I = CS1.arg_begin(), E = CS1.arg_end(); I != E; ++I) {
253*9880d681SAndroid Build Coastguard Worker         const Value *Arg = *I;
254*9880d681SAndroid Build Coastguard Worker         if (!Arg->getType()->isPointerTy())
255*9880d681SAndroid Build Coastguard Worker           continue;
256*9880d681SAndroid Build Coastguard Worker         unsigned CS1ArgIdx = std::distance(CS1.arg_begin(), I);
257*9880d681SAndroid Build Coastguard Worker         auto CS1ArgLoc = MemoryLocation::getForArgument(CS1, CS1ArgIdx, TLI);
258*9880d681SAndroid Build Coastguard Worker 
259*9880d681SAndroid Build Coastguard Worker         // ArgMask indicates what CS1 might do to CS1ArgLoc; if CS1 might Mod
260*9880d681SAndroid Build Coastguard Worker         // CS1ArgLoc, then we care about either a Mod or a Ref by CS2. If CS1
261*9880d681SAndroid Build Coastguard Worker         // might Ref, then we care only about a Mod by CS2.
262*9880d681SAndroid Build Coastguard Worker         ModRefInfo ArgMask = getArgModRefInfo(CS1, CS1ArgIdx);
263*9880d681SAndroid Build Coastguard Worker         ModRefInfo ArgR = getModRefInfo(CS2, CS1ArgLoc);
264*9880d681SAndroid Build Coastguard Worker         if (((ArgMask & MRI_Mod) != MRI_NoModRef &&
265*9880d681SAndroid Build Coastguard Worker              (ArgR & MRI_ModRef) != MRI_NoModRef) ||
266*9880d681SAndroid Build Coastguard Worker             ((ArgMask & MRI_Ref) != MRI_NoModRef &&
267*9880d681SAndroid Build Coastguard Worker              (ArgR & MRI_Mod) != MRI_NoModRef))
268*9880d681SAndroid Build Coastguard Worker           R = ModRefInfo((R | ArgMask) & Result);
269*9880d681SAndroid Build Coastguard Worker 
270*9880d681SAndroid Build Coastguard Worker         if (R == Result)
271*9880d681SAndroid Build Coastguard Worker           break;
272*9880d681SAndroid Build Coastguard Worker       }
273*9880d681SAndroid Build Coastguard Worker     }
274*9880d681SAndroid Build Coastguard Worker     return R;
275*9880d681SAndroid Build Coastguard Worker   }
276*9880d681SAndroid Build Coastguard Worker 
277*9880d681SAndroid Build Coastguard Worker   return Result;
278*9880d681SAndroid Build Coastguard Worker }
279*9880d681SAndroid Build Coastguard Worker 
getModRefBehavior(ImmutableCallSite CS)280*9880d681SAndroid Build Coastguard Worker FunctionModRefBehavior AAResults::getModRefBehavior(ImmutableCallSite CS) {
281*9880d681SAndroid Build Coastguard Worker   FunctionModRefBehavior Result = FMRB_UnknownModRefBehavior;
282*9880d681SAndroid Build Coastguard Worker 
283*9880d681SAndroid Build Coastguard Worker   for (const auto &AA : AAs) {
284*9880d681SAndroid Build Coastguard Worker     Result = FunctionModRefBehavior(Result & AA->getModRefBehavior(CS));
285*9880d681SAndroid Build Coastguard Worker 
286*9880d681SAndroid Build Coastguard Worker     // Early-exit the moment we reach the bottom of the lattice.
287*9880d681SAndroid Build Coastguard Worker     if (Result == FMRB_DoesNotAccessMemory)
288*9880d681SAndroid Build Coastguard Worker       return Result;
289*9880d681SAndroid Build Coastguard Worker   }
290*9880d681SAndroid Build Coastguard Worker 
291*9880d681SAndroid Build Coastguard Worker   return Result;
292*9880d681SAndroid Build Coastguard Worker }
293*9880d681SAndroid Build Coastguard Worker 
getModRefBehavior(const Function * F)294*9880d681SAndroid Build Coastguard Worker FunctionModRefBehavior AAResults::getModRefBehavior(const Function *F) {
295*9880d681SAndroid Build Coastguard Worker   FunctionModRefBehavior Result = FMRB_UnknownModRefBehavior;
296*9880d681SAndroid Build Coastguard Worker 
297*9880d681SAndroid Build Coastguard Worker   for (const auto &AA : AAs) {
298*9880d681SAndroid Build Coastguard Worker     Result = FunctionModRefBehavior(Result & AA->getModRefBehavior(F));
299*9880d681SAndroid Build Coastguard Worker 
300*9880d681SAndroid Build Coastguard Worker     // Early-exit the moment we reach the bottom of the lattice.
301*9880d681SAndroid Build Coastguard Worker     if (Result == FMRB_DoesNotAccessMemory)
302*9880d681SAndroid Build Coastguard Worker       return Result;
303*9880d681SAndroid Build Coastguard Worker   }
304*9880d681SAndroid Build Coastguard Worker 
305*9880d681SAndroid Build Coastguard Worker   return Result;
306*9880d681SAndroid Build Coastguard Worker }
307*9880d681SAndroid Build Coastguard Worker 
308*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
309*9880d681SAndroid Build Coastguard Worker // Helper method implementation
310*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
311*9880d681SAndroid Build Coastguard Worker 
getModRefInfo(const LoadInst * L,const MemoryLocation & Loc)312*9880d681SAndroid Build Coastguard Worker ModRefInfo AAResults::getModRefInfo(const LoadInst *L,
313*9880d681SAndroid Build Coastguard Worker                                     const MemoryLocation &Loc) {
314*9880d681SAndroid Build Coastguard Worker   // Be conservative in the face of volatile/atomic.
315*9880d681SAndroid Build Coastguard Worker   if (!L->isUnordered())
316*9880d681SAndroid Build Coastguard Worker     return MRI_ModRef;
317*9880d681SAndroid Build Coastguard Worker 
318*9880d681SAndroid Build Coastguard Worker   // If the load address doesn't alias the given address, it doesn't read
319*9880d681SAndroid Build Coastguard Worker   // or write the specified memory.
320*9880d681SAndroid Build Coastguard Worker   if (Loc.Ptr && !alias(MemoryLocation::get(L), Loc))
321*9880d681SAndroid Build Coastguard Worker     return MRI_NoModRef;
322*9880d681SAndroid Build Coastguard Worker 
323*9880d681SAndroid Build Coastguard Worker   // Otherwise, a load just reads.
324*9880d681SAndroid Build Coastguard Worker   return MRI_Ref;
325*9880d681SAndroid Build Coastguard Worker }
326*9880d681SAndroid Build Coastguard Worker 
getModRefInfo(const StoreInst * S,const MemoryLocation & Loc)327*9880d681SAndroid Build Coastguard Worker ModRefInfo AAResults::getModRefInfo(const StoreInst *S,
328*9880d681SAndroid Build Coastguard Worker                                     const MemoryLocation &Loc) {
329*9880d681SAndroid Build Coastguard Worker   // Be conservative in the face of volatile/atomic.
330*9880d681SAndroid Build Coastguard Worker   if (!S->isUnordered())
331*9880d681SAndroid Build Coastguard Worker     return MRI_ModRef;
332*9880d681SAndroid Build Coastguard Worker 
333*9880d681SAndroid Build Coastguard Worker   if (Loc.Ptr) {
334*9880d681SAndroid Build Coastguard Worker     // If the store address cannot alias the pointer in question, then the
335*9880d681SAndroid Build Coastguard Worker     // specified memory cannot be modified by the store.
336*9880d681SAndroid Build Coastguard Worker     if (!alias(MemoryLocation::get(S), Loc))
337*9880d681SAndroid Build Coastguard Worker       return MRI_NoModRef;
338*9880d681SAndroid Build Coastguard Worker 
339*9880d681SAndroid Build Coastguard Worker     // If the pointer is a pointer to constant memory, then it could not have
340*9880d681SAndroid Build Coastguard Worker     // been modified by this store.
341*9880d681SAndroid Build Coastguard Worker     if (pointsToConstantMemory(Loc))
342*9880d681SAndroid Build Coastguard Worker       return MRI_NoModRef;
343*9880d681SAndroid Build Coastguard Worker   }
344*9880d681SAndroid Build Coastguard Worker 
345*9880d681SAndroid Build Coastguard Worker   // Otherwise, a store just writes.
346*9880d681SAndroid Build Coastguard Worker   return MRI_Mod;
347*9880d681SAndroid Build Coastguard Worker }
348*9880d681SAndroid Build Coastguard Worker 
getModRefInfo(const VAArgInst * V,const MemoryLocation & Loc)349*9880d681SAndroid Build Coastguard Worker ModRefInfo AAResults::getModRefInfo(const VAArgInst *V,
350*9880d681SAndroid Build Coastguard Worker                                     const MemoryLocation &Loc) {
351*9880d681SAndroid Build Coastguard Worker 
352*9880d681SAndroid Build Coastguard Worker   if (Loc.Ptr) {
353*9880d681SAndroid Build Coastguard Worker     // If the va_arg address cannot alias the pointer in question, then the
354*9880d681SAndroid Build Coastguard Worker     // specified memory cannot be accessed by the va_arg.
355*9880d681SAndroid Build Coastguard Worker     if (!alias(MemoryLocation::get(V), Loc))
356*9880d681SAndroid Build Coastguard Worker       return MRI_NoModRef;
357*9880d681SAndroid Build Coastguard Worker 
358*9880d681SAndroid Build Coastguard Worker     // If the pointer is a pointer to constant memory, then it could not have
359*9880d681SAndroid Build Coastguard Worker     // been modified by this va_arg.
360*9880d681SAndroid Build Coastguard Worker     if (pointsToConstantMemory(Loc))
361*9880d681SAndroid Build Coastguard Worker       return MRI_NoModRef;
362*9880d681SAndroid Build Coastguard Worker   }
363*9880d681SAndroid Build Coastguard Worker 
364*9880d681SAndroid Build Coastguard Worker   // Otherwise, a va_arg reads and writes.
365*9880d681SAndroid Build Coastguard Worker   return MRI_ModRef;
366*9880d681SAndroid Build Coastguard Worker }
367*9880d681SAndroid Build Coastguard Worker 
getModRefInfo(const CatchPadInst * CatchPad,const MemoryLocation & Loc)368*9880d681SAndroid Build Coastguard Worker ModRefInfo AAResults::getModRefInfo(const CatchPadInst *CatchPad,
369*9880d681SAndroid Build Coastguard Worker                                     const MemoryLocation &Loc) {
370*9880d681SAndroid Build Coastguard Worker   if (Loc.Ptr) {
371*9880d681SAndroid Build Coastguard Worker     // If the pointer is a pointer to constant memory,
372*9880d681SAndroid Build Coastguard Worker     // then it could not have been modified by this catchpad.
373*9880d681SAndroid Build Coastguard Worker     if (pointsToConstantMemory(Loc))
374*9880d681SAndroid Build Coastguard Worker       return MRI_NoModRef;
375*9880d681SAndroid Build Coastguard Worker   }
376*9880d681SAndroid Build Coastguard Worker 
377*9880d681SAndroid Build Coastguard Worker   // Otherwise, a catchpad reads and writes.
378*9880d681SAndroid Build Coastguard Worker   return MRI_ModRef;
379*9880d681SAndroid Build Coastguard Worker }
380*9880d681SAndroid Build Coastguard Worker 
getModRefInfo(const CatchReturnInst * CatchRet,const MemoryLocation & Loc)381*9880d681SAndroid Build Coastguard Worker ModRefInfo AAResults::getModRefInfo(const CatchReturnInst *CatchRet,
382*9880d681SAndroid Build Coastguard Worker                                     const MemoryLocation &Loc) {
383*9880d681SAndroid Build Coastguard Worker   if (Loc.Ptr) {
384*9880d681SAndroid Build Coastguard Worker     // If the pointer is a pointer to constant memory,
385*9880d681SAndroid Build Coastguard Worker     // then it could not have been modified by this catchpad.
386*9880d681SAndroid Build Coastguard Worker     if (pointsToConstantMemory(Loc))
387*9880d681SAndroid Build Coastguard Worker       return MRI_NoModRef;
388*9880d681SAndroid Build Coastguard Worker   }
389*9880d681SAndroid Build Coastguard Worker 
390*9880d681SAndroid Build Coastguard Worker   // Otherwise, a catchret reads and writes.
391*9880d681SAndroid Build Coastguard Worker   return MRI_ModRef;
392*9880d681SAndroid Build Coastguard Worker }
393*9880d681SAndroid Build Coastguard Worker 
getModRefInfo(const AtomicCmpXchgInst * CX,const MemoryLocation & Loc)394*9880d681SAndroid Build Coastguard Worker ModRefInfo AAResults::getModRefInfo(const AtomicCmpXchgInst *CX,
395*9880d681SAndroid Build Coastguard Worker                                     const MemoryLocation &Loc) {
396*9880d681SAndroid Build Coastguard Worker   // Acquire/Release cmpxchg has properties that matter for arbitrary addresses.
397*9880d681SAndroid Build Coastguard Worker   if (isStrongerThanMonotonic(CX->getSuccessOrdering()))
398*9880d681SAndroid Build Coastguard Worker     return MRI_ModRef;
399*9880d681SAndroid Build Coastguard Worker 
400*9880d681SAndroid Build Coastguard Worker   // If the cmpxchg address does not alias the location, it does not access it.
401*9880d681SAndroid Build Coastguard Worker   if (Loc.Ptr && !alias(MemoryLocation::get(CX), Loc))
402*9880d681SAndroid Build Coastguard Worker     return MRI_NoModRef;
403*9880d681SAndroid Build Coastguard Worker 
404*9880d681SAndroid Build Coastguard Worker   return MRI_ModRef;
405*9880d681SAndroid Build Coastguard Worker }
406*9880d681SAndroid Build Coastguard Worker 
getModRefInfo(const AtomicRMWInst * RMW,const MemoryLocation & Loc)407*9880d681SAndroid Build Coastguard Worker ModRefInfo AAResults::getModRefInfo(const AtomicRMWInst *RMW,
408*9880d681SAndroid Build Coastguard Worker                                     const MemoryLocation &Loc) {
409*9880d681SAndroid Build Coastguard Worker   // Acquire/Release atomicrmw has properties that matter for arbitrary addresses.
410*9880d681SAndroid Build Coastguard Worker   if (isStrongerThanMonotonic(RMW->getOrdering()))
411*9880d681SAndroid Build Coastguard Worker     return MRI_ModRef;
412*9880d681SAndroid Build Coastguard Worker 
413*9880d681SAndroid Build Coastguard Worker   // If the atomicrmw address does not alias the location, it does not access it.
414*9880d681SAndroid Build Coastguard Worker   if (Loc.Ptr && !alias(MemoryLocation::get(RMW), Loc))
415*9880d681SAndroid Build Coastguard Worker     return MRI_NoModRef;
416*9880d681SAndroid Build Coastguard Worker 
417*9880d681SAndroid Build Coastguard Worker   return MRI_ModRef;
418*9880d681SAndroid Build Coastguard Worker }
419*9880d681SAndroid Build Coastguard Worker 
420*9880d681SAndroid Build Coastguard Worker /// \brief Return information about whether a particular call site modifies
421*9880d681SAndroid Build Coastguard Worker /// or reads the specified memory location \p MemLoc before instruction \p I
422*9880d681SAndroid Build Coastguard Worker /// in a BasicBlock. A ordered basic block \p OBB can be used to speed up
423*9880d681SAndroid Build Coastguard Worker /// instruction-ordering queries inside the BasicBlock containing \p I.
424*9880d681SAndroid Build Coastguard Worker /// FIXME: this is really just shoring-up a deficiency in alias analysis.
425*9880d681SAndroid Build Coastguard Worker /// BasicAA isn't willing to spend linear time determining whether an alloca
426*9880d681SAndroid Build Coastguard Worker /// was captured before or after this particular call, while we are. However,
427*9880d681SAndroid Build Coastguard Worker /// with a smarter AA in place, this test is just wasting compile time.
callCapturesBefore(const Instruction * I,const MemoryLocation & MemLoc,DominatorTree * DT,OrderedBasicBlock * OBB)428*9880d681SAndroid Build Coastguard Worker ModRefInfo AAResults::callCapturesBefore(const Instruction *I,
429*9880d681SAndroid Build Coastguard Worker                                          const MemoryLocation &MemLoc,
430*9880d681SAndroid Build Coastguard Worker                                          DominatorTree *DT,
431*9880d681SAndroid Build Coastguard Worker                                          OrderedBasicBlock *OBB) {
432*9880d681SAndroid Build Coastguard Worker   if (!DT)
433*9880d681SAndroid Build Coastguard Worker     return MRI_ModRef;
434*9880d681SAndroid Build Coastguard Worker 
435*9880d681SAndroid Build Coastguard Worker   const Value *Object =
436*9880d681SAndroid Build Coastguard Worker       GetUnderlyingObject(MemLoc.Ptr, I->getModule()->getDataLayout());
437*9880d681SAndroid Build Coastguard Worker   if (!isIdentifiedObject(Object) || isa<GlobalValue>(Object) ||
438*9880d681SAndroid Build Coastguard Worker       isa<Constant>(Object))
439*9880d681SAndroid Build Coastguard Worker     return MRI_ModRef;
440*9880d681SAndroid Build Coastguard Worker 
441*9880d681SAndroid Build Coastguard Worker   ImmutableCallSite CS(I);
442*9880d681SAndroid Build Coastguard Worker   if (!CS.getInstruction() || CS.getInstruction() == Object)
443*9880d681SAndroid Build Coastguard Worker     return MRI_ModRef;
444*9880d681SAndroid Build Coastguard Worker 
445*9880d681SAndroid Build Coastguard Worker   if (llvm::PointerMayBeCapturedBefore(Object, /* ReturnCaptures */ true,
446*9880d681SAndroid Build Coastguard Worker                                        /* StoreCaptures */ true, I, DT,
447*9880d681SAndroid Build Coastguard Worker                                        /* include Object */ true,
448*9880d681SAndroid Build Coastguard Worker                                        /* OrderedBasicBlock */ OBB))
449*9880d681SAndroid Build Coastguard Worker     return MRI_ModRef;
450*9880d681SAndroid Build Coastguard Worker 
451*9880d681SAndroid Build Coastguard Worker   unsigned ArgNo = 0;
452*9880d681SAndroid Build Coastguard Worker   ModRefInfo R = MRI_NoModRef;
453*9880d681SAndroid Build Coastguard Worker   for (auto CI = CS.data_operands_begin(), CE = CS.data_operands_end();
454*9880d681SAndroid Build Coastguard Worker        CI != CE; ++CI, ++ArgNo) {
455*9880d681SAndroid Build Coastguard Worker     // Only look at the no-capture or byval pointer arguments.  If this
456*9880d681SAndroid Build Coastguard Worker     // pointer were passed to arguments that were neither of these, then it
457*9880d681SAndroid Build Coastguard Worker     // couldn't be no-capture.
458*9880d681SAndroid Build Coastguard Worker     if (!(*CI)->getType()->isPointerTy() ||
459*9880d681SAndroid Build Coastguard Worker         (!CS.doesNotCapture(ArgNo) && !CS.isByValArgument(ArgNo)))
460*9880d681SAndroid Build Coastguard Worker       continue;
461*9880d681SAndroid Build Coastguard Worker 
462*9880d681SAndroid Build Coastguard Worker     // If this is a no-capture pointer argument, see if we can tell that it
463*9880d681SAndroid Build Coastguard Worker     // is impossible to alias the pointer we're checking.  If not, we have to
464*9880d681SAndroid Build Coastguard Worker     // assume that the call could touch the pointer, even though it doesn't
465*9880d681SAndroid Build Coastguard Worker     // escape.
466*9880d681SAndroid Build Coastguard Worker     if (isNoAlias(MemoryLocation(*CI), MemoryLocation(Object)))
467*9880d681SAndroid Build Coastguard Worker       continue;
468*9880d681SAndroid Build Coastguard Worker     if (CS.doesNotAccessMemory(ArgNo))
469*9880d681SAndroid Build Coastguard Worker       continue;
470*9880d681SAndroid Build Coastguard Worker     if (CS.onlyReadsMemory(ArgNo)) {
471*9880d681SAndroid Build Coastguard Worker       R = MRI_Ref;
472*9880d681SAndroid Build Coastguard Worker       continue;
473*9880d681SAndroid Build Coastguard Worker     }
474*9880d681SAndroid Build Coastguard Worker     return MRI_ModRef;
475*9880d681SAndroid Build Coastguard Worker   }
476*9880d681SAndroid Build Coastguard Worker   return R;
477*9880d681SAndroid Build Coastguard Worker }
478*9880d681SAndroid Build Coastguard Worker 
479*9880d681SAndroid Build Coastguard Worker /// canBasicBlockModify - Return true if it is possible for execution of the
480*9880d681SAndroid Build Coastguard Worker /// specified basic block to modify the location Loc.
481*9880d681SAndroid Build Coastguard Worker ///
canBasicBlockModify(const BasicBlock & BB,const MemoryLocation & Loc)482*9880d681SAndroid Build Coastguard Worker bool AAResults::canBasicBlockModify(const BasicBlock &BB,
483*9880d681SAndroid Build Coastguard Worker                                     const MemoryLocation &Loc) {
484*9880d681SAndroid Build Coastguard Worker   return canInstructionRangeModRef(BB.front(), BB.back(), Loc, MRI_Mod);
485*9880d681SAndroid Build Coastguard Worker }
486*9880d681SAndroid Build Coastguard Worker 
487*9880d681SAndroid Build Coastguard Worker /// canInstructionRangeModRef - Return true if it is possible for the
488*9880d681SAndroid Build Coastguard Worker /// execution of the specified instructions to mod\ref (according to the
489*9880d681SAndroid Build Coastguard Worker /// mode) the location Loc. The instructions to consider are all
490*9880d681SAndroid Build Coastguard Worker /// of the instructions in the range of [I1,I2] INCLUSIVE.
491*9880d681SAndroid Build Coastguard Worker /// I1 and I2 must be in the same basic block.
canInstructionRangeModRef(const Instruction & I1,const Instruction & I2,const MemoryLocation & Loc,const ModRefInfo Mode)492*9880d681SAndroid Build Coastguard Worker bool AAResults::canInstructionRangeModRef(const Instruction &I1,
493*9880d681SAndroid Build Coastguard Worker                                           const Instruction &I2,
494*9880d681SAndroid Build Coastguard Worker                                           const MemoryLocation &Loc,
495*9880d681SAndroid Build Coastguard Worker                                           const ModRefInfo Mode) {
496*9880d681SAndroid Build Coastguard Worker   assert(I1.getParent() == I2.getParent() &&
497*9880d681SAndroid Build Coastguard Worker          "Instructions not in same basic block!");
498*9880d681SAndroid Build Coastguard Worker   BasicBlock::const_iterator I = I1.getIterator();
499*9880d681SAndroid Build Coastguard Worker   BasicBlock::const_iterator E = I2.getIterator();
500*9880d681SAndroid Build Coastguard Worker   ++E;  // Convert from inclusive to exclusive range.
501*9880d681SAndroid Build Coastguard Worker 
502*9880d681SAndroid Build Coastguard Worker   for (; I != E; ++I) // Check every instruction in range
503*9880d681SAndroid Build Coastguard Worker     if (getModRefInfo(&*I, Loc) & Mode)
504*9880d681SAndroid Build Coastguard Worker       return true;
505*9880d681SAndroid Build Coastguard Worker   return false;
506*9880d681SAndroid Build Coastguard Worker }
507*9880d681SAndroid Build Coastguard Worker 
508*9880d681SAndroid Build Coastguard Worker // Provide a definition for the root virtual destructor.
~Concept()509*9880d681SAndroid Build Coastguard Worker AAResults::Concept::~Concept() {}
510*9880d681SAndroid Build Coastguard Worker 
511*9880d681SAndroid Build Coastguard Worker // Provide a definition for the static object used to identify passes.
512*9880d681SAndroid Build Coastguard Worker char AAManager::PassID;
513*9880d681SAndroid Build Coastguard Worker 
514*9880d681SAndroid Build Coastguard Worker namespace {
515*9880d681SAndroid Build Coastguard Worker /// A wrapper pass for external alias analyses. This just squirrels away the
516*9880d681SAndroid Build Coastguard Worker /// callback used to run any analyses and register their results.
517*9880d681SAndroid Build Coastguard Worker struct ExternalAAWrapperPass : ImmutablePass {
518*9880d681SAndroid Build Coastguard Worker   typedef std::function<void(Pass &, Function &, AAResults &)> CallbackT;
519*9880d681SAndroid Build Coastguard Worker 
520*9880d681SAndroid Build Coastguard Worker   CallbackT CB;
521*9880d681SAndroid Build Coastguard Worker 
522*9880d681SAndroid Build Coastguard Worker   static char ID;
523*9880d681SAndroid Build Coastguard Worker 
ExternalAAWrapperPass__anonb99426290111::ExternalAAWrapperPass524*9880d681SAndroid Build Coastguard Worker   ExternalAAWrapperPass() : ImmutablePass(ID) {
525*9880d681SAndroid Build Coastguard Worker     initializeExternalAAWrapperPassPass(*PassRegistry::getPassRegistry());
526*9880d681SAndroid Build Coastguard Worker   }
ExternalAAWrapperPass__anonb99426290111::ExternalAAWrapperPass527*9880d681SAndroid Build Coastguard Worker   explicit ExternalAAWrapperPass(CallbackT CB)
528*9880d681SAndroid Build Coastguard Worker       : ImmutablePass(ID), CB(std::move(CB)) {
529*9880d681SAndroid Build Coastguard Worker     initializeExternalAAWrapperPassPass(*PassRegistry::getPassRegistry());
530*9880d681SAndroid Build Coastguard Worker   }
531*9880d681SAndroid Build Coastguard Worker 
getAnalysisUsage__anonb99426290111::ExternalAAWrapperPass532*9880d681SAndroid Build Coastguard Worker   void getAnalysisUsage(AnalysisUsage &AU) const override {
533*9880d681SAndroid Build Coastguard Worker     AU.setPreservesAll();
534*9880d681SAndroid Build Coastguard Worker   }
535*9880d681SAndroid Build Coastguard Worker };
536*9880d681SAndroid Build Coastguard Worker }
537*9880d681SAndroid Build Coastguard Worker 
538*9880d681SAndroid Build Coastguard Worker char ExternalAAWrapperPass::ID = 0;
539*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS(ExternalAAWrapperPass, "external-aa", "External Alias Analysis",
540*9880d681SAndroid Build Coastguard Worker                 false, true)
541*9880d681SAndroid Build Coastguard Worker 
542*9880d681SAndroid Build Coastguard Worker ImmutablePass *
createExternalAAWrapperPass(ExternalAAWrapperPass::CallbackT Callback)543*9880d681SAndroid Build Coastguard Worker llvm::createExternalAAWrapperPass(ExternalAAWrapperPass::CallbackT Callback) {
544*9880d681SAndroid Build Coastguard Worker   return new ExternalAAWrapperPass(std::move(Callback));
545*9880d681SAndroid Build Coastguard Worker }
546*9880d681SAndroid Build Coastguard Worker 
AAResultsWrapperPass()547*9880d681SAndroid Build Coastguard Worker AAResultsWrapperPass::AAResultsWrapperPass() : FunctionPass(ID) {
548*9880d681SAndroid Build Coastguard Worker   initializeAAResultsWrapperPassPass(*PassRegistry::getPassRegistry());
549*9880d681SAndroid Build Coastguard Worker }
550*9880d681SAndroid Build Coastguard Worker 
551*9880d681SAndroid Build Coastguard Worker char AAResultsWrapperPass::ID = 0;
552*9880d681SAndroid Build Coastguard Worker 
553*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_BEGIN(AAResultsWrapperPass, "aa",
554*9880d681SAndroid Build Coastguard Worker                       "Function Alias Analysis Results", false, true)
INITIALIZE_PASS_DEPENDENCY(BasicAAWrapperPass)555*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(BasicAAWrapperPass)
556*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(CFLAndersAAWrapperPass)
557*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(CFLSteensAAWrapperPass)
558*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(ExternalAAWrapperPass)
559*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass)
560*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(ObjCARCAAWrapperPass)
561*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(SCEVAAWrapperPass)
562*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(ScopedNoAliasAAWrapperPass)
563*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(TypeBasedAAWrapperPass)
564*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_END(AAResultsWrapperPass, "aa",
565*9880d681SAndroid Build Coastguard Worker                     "Function Alias Analysis Results", false, true)
566*9880d681SAndroid Build Coastguard Worker 
567*9880d681SAndroid Build Coastguard Worker FunctionPass *llvm::createAAResultsWrapperPass() {
568*9880d681SAndroid Build Coastguard Worker   return new AAResultsWrapperPass();
569*9880d681SAndroid Build Coastguard Worker }
570*9880d681SAndroid Build Coastguard Worker 
571*9880d681SAndroid Build Coastguard Worker /// Run the wrapper pass to rebuild an aggregation over known AA passes.
572*9880d681SAndroid Build Coastguard Worker ///
573*9880d681SAndroid Build Coastguard Worker /// This is the legacy pass manager's interface to the new-style AA results
574*9880d681SAndroid Build Coastguard Worker /// aggregation object. Because this is somewhat shoe-horned into the legacy
575*9880d681SAndroid Build Coastguard Worker /// pass manager, we hard code all the specific alias analyses available into
576*9880d681SAndroid Build Coastguard Worker /// it. While the particular set enabled is configured via commandline flags,
577*9880d681SAndroid Build Coastguard Worker /// adding a new alias analysis to LLVM will require adding support for it to
578*9880d681SAndroid Build Coastguard Worker /// this list.
runOnFunction(Function & F)579*9880d681SAndroid Build Coastguard Worker bool AAResultsWrapperPass::runOnFunction(Function &F) {
580*9880d681SAndroid Build Coastguard Worker   // NB! This *must* be reset before adding new AA results to the new
581*9880d681SAndroid Build Coastguard Worker   // AAResults object because in the legacy pass manager, each instance
582*9880d681SAndroid Build Coastguard Worker   // of these will refer to the *same* immutable analyses, registering and
583*9880d681SAndroid Build Coastguard Worker   // unregistering themselves with them. We need to carefully tear down the
584*9880d681SAndroid Build Coastguard Worker   // previous object first, in this case replacing it with an empty one, before
585*9880d681SAndroid Build Coastguard Worker   // registering new results.
586*9880d681SAndroid Build Coastguard Worker   AAR.reset(
587*9880d681SAndroid Build Coastguard Worker       new AAResults(getAnalysis<TargetLibraryInfoWrapperPass>().getTLI()));
588*9880d681SAndroid Build Coastguard Worker 
589*9880d681SAndroid Build Coastguard Worker   // BasicAA is always available for function analyses. Also, we add it first
590*9880d681SAndroid Build Coastguard Worker   // so that it can trump TBAA results when it proves MustAlias.
591*9880d681SAndroid Build Coastguard Worker   // FIXME: TBAA should have an explicit mode to support this and then we
592*9880d681SAndroid Build Coastguard Worker   // should reconsider the ordering here.
593*9880d681SAndroid Build Coastguard Worker   if (!DisableBasicAA)
594*9880d681SAndroid Build Coastguard Worker     AAR->addAAResult(getAnalysis<BasicAAWrapperPass>().getResult());
595*9880d681SAndroid Build Coastguard Worker 
596*9880d681SAndroid Build Coastguard Worker   // Populate the results with the currently available AAs.
597*9880d681SAndroid Build Coastguard Worker   if (auto *WrapperPass = getAnalysisIfAvailable<ScopedNoAliasAAWrapperPass>())
598*9880d681SAndroid Build Coastguard Worker     AAR->addAAResult(WrapperPass->getResult());
599*9880d681SAndroid Build Coastguard Worker   if (auto *WrapperPass = getAnalysisIfAvailable<TypeBasedAAWrapperPass>())
600*9880d681SAndroid Build Coastguard Worker     AAR->addAAResult(WrapperPass->getResult());
601*9880d681SAndroid Build Coastguard Worker   if (auto *WrapperPass =
602*9880d681SAndroid Build Coastguard Worker           getAnalysisIfAvailable<objcarc::ObjCARCAAWrapperPass>())
603*9880d681SAndroid Build Coastguard Worker     AAR->addAAResult(WrapperPass->getResult());
604*9880d681SAndroid Build Coastguard Worker   if (auto *WrapperPass = getAnalysisIfAvailable<GlobalsAAWrapperPass>())
605*9880d681SAndroid Build Coastguard Worker     AAR->addAAResult(WrapperPass->getResult());
606*9880d681SAndroid Build Coastguard Worker   if (auto *WrapperPass = getAnalysisIfAvailable<SCEVAAWrapperPass>())
607*9880d681SAndroid Build Coastguard Worker     AAR->addAAResult(WrapperPass->getResult());
608*9880d681SAndroid Build Coastguard Worker   if (auto *WrapperPass = getAnalysisIfAvailable<CFLAndersAAWrapperPass>())
609*9880d681SAndroid Build Coastguard Worker     AAR->addAAResult(WrapperPass->getResult());
610*9880d681SAndroid Build Coastguard Worker   if (auto *WrapperPass = getAnalysisIfAvailable<CFLSteensAAWrapperPass>())
611*9880d681SAndroid Build Coastguard Worker     AAR->addAAResult(WrapperPass->getResult());
612*9880d681SAndroid Build Coastguard Worker 
613*9880d681SAndroid Build Coastguard Worker   // If available, run an external AA providing callback over the results as
614*9880d681SAndroid Build Coastguard Worker   // well.
615*9880d681SAndroid Build Coastguard Worker   if (auto *WrapperPass = getAnalysisIfAvailable<ExternalAAWrapperPass>())
616*9880d681SAndroid Build Coastguard Worker     if (WrapperPass->CB)
617*9880d681SAndroid Build Coastguard Worker       WrapperPass->CB(*this, F, *AAR);
618*9880d681SAndroid Build Coastguard Worker 
619*9880d681SAndroid Build Coastguard Worker   // Analyses don't mutate the IR, so return false.
620*9880d681SAndroid Build Coastguard Worker   return false;
621*9880d681SAndroid Build Coastguard Worker }
622*9880d681SAndroid Build Coastguard Worker 
getAnalysisUsage(AnalysisUsage & AU) const623*9880d681SAndroid Build Coastguard Worker void AAResultsWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
624*9880d681SAndroid Build Coastguard Worker   AU.setPreservesAll();
625*9880d681SAndroid Build Coastguard Worker   AU.addRequired<BasicAAWrapperPass>();
626*9880d681SAndroid Build Coastguard Worker   AU.addRequired<TargetLibraryInfoWrapperPass>();
627*9880d681SAndroid Build Coastguard Worker 
628*9880d681SAndroid Build Coastguard Worker   // We also need to mark all the alias analysis passes we will potentially
629*9880d681SAndroid Build Coastguard Worker   // probe in runOnFunction as used here to ensure the legacy pass manager
630*9880d681SAndroid Build Coastguard Worker   // preserves them. This hard coding of lists of alias analyses is specific to
631*9880d681SAndroid Build Coastguard Worker   // the legacy pass manager.
632*9880d681SAndroid Build Coastguard Worker   AU.addUsedIfAvailable<ScopedNoAliasAAWrapperPass>();
633*9880d681SAndroid Build Coastguard Worker   AU.addUsedIfAvailable<TypeBasedAAWrapperPass>();
634*9880d681SAndroid Build Coastguard Worker   AU.addUsedIfAvailable<objcarc::ObjCARCAAWrapperPass>();
635*9880d681SAndroid Build Coastguard Worker   AU.addUsedIfAvailable<GlobalsAAWrapperPass>();
636*9880d681SAndroid Build Coastguard Worker   AU.addUsedIfAvailable<SCEVAAWrapperPass>();
637*9880d681SAndroid Build Coastguard Worker   AU.addUsedIfAvailable<CFLAndersAAWrapperPass>();
638*9880d681SAndroid Build Coastguard Worker   AU.addUsedIfAvailable<CFLSteensAAWrapperPass>();
639*9880d681SAndroid Build Coastguard Worker }
640*9880d681SAndroid Build Coastguard Worker 
createLegacyPMAAResults(Pass & P,Function & F,BasicAAResult & BAR)641*9880d681SAndroid Build Coastguard Worker AAResults llvm::createLegacyPMAAResults(Pass &P, Function &F,
642*9880d681SAndroid Build Coastguard Worker                                         BasicAAResult &BAR) {
643*9880d681SAndroid Build Coastguard Worker   AAResults AAR(P.getAnalysis<TargetLibraryInfoWrapperPass>().getTLI());
644*9880d681SAndroid Build Coastguard Worker 
645*9880d681SAndroid Build Coastguard Worker   // Add in our explicitly constructed BasicAA results.
646*9880d681SAndroid Build Coastguard Worker   if (!DisableBasicAA)
647*9880d681SAndroid Build Coastguard Worker     AAR.addAAResult(BAR);
648*9880d681SAndroid Build Coastguard Worker 
649*9880d681SAndroid Build Coastguard Worker   // Populate the results with the other currently available AAs.
650*9880d681SAndroid Build Coastguard Worker   if (auto *WrapperPass =
651*9880d681SAndroid Build Coastguard Worker           P.getAnalysisIfAvailable<ScopedNoAliasAAWrapperPass>())
652*9880d681SAndroid Build Coastguard Worker     AAR.addAAResult(WrapperPass->getResult());
653*9880d681SAndroid Build Coastguard Worker   if (auto *WrapperPass = P.getAnalysisIfAvailable<TypeBasedAAWrapperPass>())
654*9880d681SAndroid Build Coastguard Worker     AAR.addAAResult(WrapperPass->getResult());
655*9880d681SAndroid Build Coastguard Worker   if (auto *WrapperPass =
656*9880d681SAndroid Build Coastguard Worker           P.getAnalysisIfAvailable<objcarc::ObjCARCAAWrapperPass>())
657*9880d681SAndroid Build Coastguard Worker     AAR.addAAResult(WrapperPass->getResult());
658*9880d681SAndroid Build Coastguard Worker   if (auto *WrapperPass = P.getAnalysisIfAvailable<GlobalsAAWrapperPass>())
659*9880d681SAndroid Build Coastguard Worker     AAR.addAAResult(WrapperPass->getResult());
660*9880d681SAndroid Build Coastguard Worker   if (auto *WrapperPass = P.getAnalysisIfAvailable<CFLAndersAAWrapperPass>())
661*9880d681SAndroid Build Coastguard Worker     AAR.addAAResult(WrapperPass->getResult());
662*9880d681SAndroid Build Coastguard Worker   if (auto *WrapperPass = P.getAnalysisIfAvailable<CFLSteensAAWrapperPass>())
663*9880d681SAndroid Build Coastguard Worker     AAR.addAAResult(WrapperPass->getResult());
664*9880d681SAndroid Build Coastguard Worker 
665*9880d681SAndroid Build Coastguard Worker   return AAR;
666*9880d681SAndroid Build Coastguard Worker }
667*9880d681SAndroid Build Coastguard Worker 
isNoAliasCall(const Value * V)668*9880d681SAndroid Build Coastguard Worker bool llvm::isNoAliasCall(const Value *V) {
669*9880d681SAndroid Build Coastguard Worker   if (auto CS = ImmutableCallSite(V))
670*9880d681SAndroid Build Coastguard Worker     return CS.paramHasAttr(0, Attribute::NoAlias);
671*9880d681SAndroid Build Coastguard Worker   return false;
672*9880d681SAndroid Build Coastguard Worker }
673*9880d681SAndroid Build Coastguard Worker 
isNoAliasArgument(const Value * V)674*9880d681SAndroid Build Coastguard Worker bool llvm::isNoAliasArgument(const Value *V) {
675*9880d681SAndroid Build Coastguard Worker   if (const Argument *A = dyn_cast<Argument>(V))
676*9880d681SAndroid Build Coastguard Worker     return A->hasNoAliasAttr();
677*9880d681SAndroid Build Coastguard Worker   return false;
678*9880d681SAndroid Build Coastguard Worker }
679*9880d681SAndroid Build Coastguard Worker 
isIdentifiedObject(const Value * V)680*9880d681SAndroid Build Coastguard Worker bool llvm::isIdentifiedObject(const Value *V) {
681*9880d681SAndroid Build Coastguard Worker   if (isa<AllocaInst>(V))
682*9880d681SAndroid Build Coastguard Worker     return true;
683*9880d681SAndroid Build Coastguard Worker   if (isa<GlobalValue>(V) && !isa<GlobalAlias>(V))
684*9880d681SAndroid Build Coastguard Worker     return true;
685*9880d681SAndroid Build Coastguard Worker   if (isNoAliasCall(V))
686*9880d681SAndroid Build Coastguard Worker     return true;
687*9880d681SAndroid Build Coastguard Worker   if (const Argument *A = dyn_cast<Argument>(V))
688*9880d681SAndroid Build Coastguard Worker     return A->hasNoAliasAttr() || A->hasByValAttr();
689*9880d681SAndroid Build Coastguard Worker   return false;
690*9880d681SAndroid Build Coastguard Worker }
691*9880d681SAndroid Build Coastguard Worker 
isIdentifiedFunctionLocal(const Value * V)692*9880d681SAndroid Build Coastguard Worker bool llvm::isIdentifiedFunctionLocal(const Value *V) {
693*9880d681SAndroid Build Coastguard Worker   return isa<AllocaInst>(V) || isNoAliasCall(V) || isNoAliasArgument(V);
694*9880d681SAndroid Build Coastguard Worker }
695*9880d681SAndroid Build Coastguard Worker 
getAAResultsAnalysisUsage(AnalysisUsage & AU)696*9880d681SAndroid Build Coastguard Worker void llvm::getAAResultsAnalysisUsage(AnalysisUsage &AU) {
697*9880d681SAndroid Build Coastguard Worker   // This function needs to be in sync with llvm::createLegacyPMAAResults -- if
698*9880d681SAndroid Build Coastguard Worker   // more alias analyses are added to llvm::createLegacyPMAAResults, they need
699*9880d681SAndroid Build Coastguard Worker   // to be added here also.
700*9880d681SAndroid Build Coastguard Worker   AU.addRequired<TargetLibraryInfoWrapperPass>();
701*9880d681SAndroid Build Coastguard Worker   AU.addUsedIfAvailable<ScopedNoAliasAAWrapperPass>();
702*9880d681SAndroid Build Coastguard Worker   AU.addUsedIfAvailable<TypeBasedAAWrapperPass>();
703*9880d681SAndroid Build Coastguard Worker   AU.addUsedIfAvailable<objcarc::ObjCARCAAWrapperPass>();
704*9880d681SAndroid Build Coastguard Worker   AU.addUsedIfAvailable<GlobalsAAWrapperPass>();
705*9880d681SAndroid Build Coastguard Worker   AU.addUsedIfAvailable<CFLAndersAAWrapperPass>();
706*9880d681SAndroid Build Coastguard Worker   AU.addUsedIfAvailable<CFLSteensAAWrapperPass>();
707*9880d681SAndroid Build Coastguard Worker }
708