1f4a2713aSLionel Sambuc //===- AliasAnalysis.cpp - Generic Alias Analysis Interface Implementation -==//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This file implements the generic AliasAnalysis interface which is used as the
11f4a2713aSLionel Sambuc // common interface used by all clients and implementations of alias analysis.
12f4a2713aSLionel Sambuc //
13f4a2713aSLionel Sambuc // This file also implements the default version of the AliasAnalysis interface
14f4a2713aSLionel Sambuc // that is to be used when no other implementation is specified.  This does some
15f4a2713aSLionel Sambuc // simple tests that detect obvious cases: two different global pointers cannot
16f4a2713aSLionel Sambuc // alias, a global cannot alias a malloc, two different mallocs cannot alias,
17f4a2713aSLionel Sambuc // etc.
18f4a2713aSLionel Sambuc //
19f4a2713aSLionel Sambuc // This alias analysis implementation really isn't very good for anything, but
20f4a2713aSLionel Sambuc // it is very fast, and makes a nice clean default implementation.  Because it
21f4a2713aSLionel Sambuc // handles lots of little corner cases, other, more complex, alias analysis
22f4a2713aSLionel Sambuc // implementations may choose to rely on this pass to resolve these simple and
23f4a2713aSLionel Sambuc // easy cases.
24f4a2713aSLionel Sambuc //
25f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
26f4a2713aSLionel Sambuc 
27f4a2713aSLionel Sambuc #include "llvm/Analysis/AliasAnalysis.h"
28f4a2713aSLionel Sambuc #include "llvm/Analysis/CFG.h"
29*0a6a1f1dSLionel Sambuc #include "llvm/Analysis/CaptureTracking.h"
30f4a2713aSLionel Sambuc #include "llvm/Analysis/ValueTracking.h"
31f4a2713aSLionel Sambuc #include "llvm/IR/BasicBlock.h"
32f4a2713aSLionel Sambuc #include "llvm/IR/DataLayout.h"
33*0a6a1f1dSLionel Sambuc #include "llvm/IR/Dominators.h"
34f4a2713aSLionel Sambuc #include "llvm/IR/Function.h"
35f4a2713aSLionel Sambuc #include "llvm/IR/Instructions.h"
36f4a2713aSLionel Sambuc #include "llvm/IR/IntrinsicInst.h"
37f4a2713aSLionel Sambuc #include "llvm/IR/LLVMContext.h"
38f4a2713aSLionel Sambuc #include "llvm/IR/Type.h"
39f4a2713aSLionel Sambuc #include "llvm/Pass.h"
40f4a2713aSLionel Sambuc #include "llvm/Target/TargetLibraryInfo.h"
41f4a2713aSLionel Sambuc using namespace llvm;
42f4a2713aSLionel Sambuc 
43f4a2713aSLionel Sambuc // Register the AliasAnalysis interface, providing a nice name to refer to.
44f4a2713aSLionel Sambuc INITIALIZE_ANALYSIS_GROUP(AliasAnalysis, "Alias Analysis", NoAA)
45f4a2713aSLionel Sambuc char AliasAnalysis::ID = 0;
46f4a2713aSLionel Sambuc 
47f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
48f4a2713aSLionel Sambuc // Default chaining methods
49f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
50f4a2713aSLionel Sambuc 
51f4a2713aSLionel Sambuc AliasAnalysis::AliasResult
alias(const Location & LocA,const Location & LocB)52f4a2713aSLionel Sambuc AliasAnalysis::alias(const Location &LocA, const Location &LocB) {
53f4a2713aSLionel Sambuc   assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
54f4a2713aSLionel Sambuc   return AA->alias(LocA, LocB);
55f4a2713aSLionel Sambuc }
56f4a2713aSLionel Sambuc 
pointsToConstantMemory(const Location & Loc,bool OrLocal)57f4a2713aSLionel Sambuc bool AliasAnalysis::pointsToConstantMemory(const Location &Loc,
58f4a2713aSLionel Sambuc                                            bool OrLocal) {
59f4a2713aSLionel Sambuc   assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
60f4a2713aSLionel Sambuc   return AA->pointsToConstantMemory(Loc, OrLocal);
61f4a2713aSLionel Sambuc }
62f4a2713aSLionel Sambuc 
63*0a6a1f1dSLionel Sambuc AliasAnalysis::Location
getArgLocation(ImmutableCallSite CS,unsigned ArgIdx,AliasAnalysis::ModRefResult & Mask)64*0a6a1f1dSLionel Sambuc AliasAnalysis::getArgLocation(ImmutableCallSite CS, unsigned ArgIdx,
65*0a6a1f1dSLionel Sambuc                               AliasAnalysis::ModRefResult &Mask) {
66*0a6a1f1dSLionel Sambuc   assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
67*0a6a1f1dSLionel Sambuc   return AA->getArgLocation(CS, ArgIdx, Mask);
68*0a6a1f1dSLionel Sambuc }
69*0a6a1f1dSLionel Sambuc 
deleteValue(Value * V)70f4a2713aSLionel Sambuc void AliasAnalysis::deleteValue(Value *V) {
71f4a2713aSLionel Sambuc   assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
72f4a2713aSLionel Sambuc   AA->deleteValue(V);
73f4a2713aSLionel Sambuc }
74f4a2713aSLionel Sambuc 
copyValue(Value * From,Value * To)75f4a2713aSLionel Sambuc void AliasAnalysis::copyValue(Value *From, Value *To) {
76f4a2713aSLionel Sambuc   assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
77f4a2713aSLionel Sambuc   AA->copyValue(From, To);
78f4a2713aSLionel Sambuc }
79f4a2713aSLionel Sambuc 
addEscapingUse(Use & U)80f4a2713aSLionel Sambuc void AliasAnalysis::addEscapingUse(Use &U) {
81f4a2713aSLionel Sambuc   assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
82f4a2713aSLionel Sambuc   AA->addEscapingUse(U);
83f4a2713aSLionel Sambuc }
84f4a2713aSLionel Sambuc 
85f4a2713aSLionel Sambuc 
86f4a2713aSLionel Sambuc AliasAnalysis::ModRefResult
getModRefInfo(ImmutableCallSite CS,const Location & Loc)87f4a2713aSLionel Sambuc AliasAnalysis::getModRefInfo(ImmutableCallSite CS,
88f4a2713aSLionel Sambuc                              const Location &Loc) {
89f4a2713aSLionel Sambuc   assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
90f4a2713aSLionel Sambuc 
91f4a2713aSLionel Sambuc   ModRefBehavior MRB = getModRefBehavior(CS);
92f4a2713aSLionel Sambuc   if (MRB == DoesNotAccessMemory)
93f4a2713aSLionel Sambuc     return NoModRef;
94f4a2713aSLionel Sambuc 
95f4a2713aSLionel Sambuc   ModRefResult Mask = ModRef;
96f4a2713aSLionel Sambuc   if (onlyReadsMemory(MRB))
97f4a2713aSLionel Sambuc     Mask = Ref;
98f4a2713aSLionel Sambuc 
99f4a2713aSLionel Sambuc   if (onlyAccessesArgPointees(MRB)) {
100f4a2713aSLionel Sambuc     bool doesAlias = false;
101*0a6a1f1dSLionel Sambuc     ModRefResult AllArgsMask = NoModRef;
102f4a2713aSLionel Sambuc     if (doesAccessArgPointees(MRB)) {
103f4a2713aSLionel Sambuc       for (ImmutableCallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
104f4a2713aSLionel Sambuc            AI != AE; ++AI) {
105f4a2713aSLionel Sambuc         const Value *Arg = *AI;
106f4a2713aSLionel Sambuc         if (!Arg->getType()->isPointerTy())
107f4a2713aSLionel Sambuc           continue;
108*0a6a1f1dSLionel Sambuc         ModRefResult ArgMask;
109*0a6a1f1dSLionel Sambuc         Location CSLoc =
110*0a6a1f1dSLionel Sambuc           getArgLocation(CS, (unsigned) std::distance(CS.arg_begin(), AI),
111*0a6a1f1dSLionel Sambuc                          ArgMask);
112f4a2713aSLionel Sambuc         if (!isNoAlias(CSLoc, Loc)) {
113f4a2713aSLionel Sambuc           doesAlias = true;
114*0a6a1f1dSLionel Sambuc           AllArgsMask = ModRefResult(AllArgsMask | ArgMask);
115f4a2713aSLionel Sambuc         }
116f4a2713aSLionel Sambuc       }
117f4a2713aSLionel Sambuc     }
118f4a2713aSLionel Sambuc     if (!doesAlias)
119f4a2713aSLionel Sambuc       return NoModRef;
120*0a6a1f1dSLionel Sambuc     Mask = ModRefResult(Mask & AllArgsMask);
121f4a2713aSLionel Sambuc   }
122f4a2713aSLionel Sambuc 
123f4a2713aSLionel Sambuc   // If Loc is a constant memory location, the call definitely could not
124f4a2713aSLionel Sambuc   // modify the memory location.
125f4a2713aSLionel Sambuc   if ((Mask & Mod) && pointsToConstantMemory(Loc))
126f4a2713aSLionel Sambuc     Mask = ModRefResult(Mask & ~Mod);
127f4a2713aSLionel Sambuc 
128f4a2713aSLionel Sambuc   // If this is the end of the chain, don't forward.
129f4a2713aSLionel Sambuc   if (!AA) return Mask;
130f4a2713aSLionel Sambuc 
131f4a2713aSLionel Sambuc   // Otherwise, fall back to the next AA in the chain. But we can merge
132f4a2713aSLionel Sambuc   // in any mask we've managed to compute.
133f4a2713aSLionel Sambuc   return ModRefResult(AA->getModRefInfo(CS, Loc) & Mask);
134f4a2713aSLionel Sambuc }
135f4a2713aSLionel Sambuc 
136f4a2713aSLionel Sambuc AliasAnalysis::ModRefResult
getModRefInfo(ImmutableCallSite CS1,ImmutableCallSite CS2)137f4a2713aSLionel Sambuc AliasAnalysis::getModRefInfo(ImmutableCallSite CS1, ImmutableCallSite CS2) {
138f4a2713aSLionel Sambuc   assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
139f4a2713aSLionel Sambuc 
140f4a2713aSLionel Sambuc   // If CS1 or CS2 are readnone, they don't interact.
141f4a2713aSLionel Sambuc   ModRefBehavior CS1B = getModRefBehavior(CS1);
142f4a2713aSLionel Sambuc   if (CS1B == DoesNotAccessMemory) return NoModRef;
143f4a2713aSLionel Sambuc 
144f4a2713aSLionel Sambuc   ModRefBehavior CS2B = getModRefBehavior(CS2);
145f4a2713aSLionel Sambuc   if (CS2B == DoesNotAccessMemory) return NoModRef;
146f4a2713aSLionel Sambuc 
147f4a2713aSLionel Sambuc   // If they both only read from memory, there is no dependence.
148f4a2713aSLionel Sambuc   if (onlyReadsMemory(CS1B) && onlyReadsMemory(CS2B))
149f4a2713aSLionel Sambuc     return NoModRef;
150f4a2713aSLionel Sambuc 
151f4a2713aSLionel Sambuc   AliasAnalysis::ModRefResult Mask = ModRef;
152f4a2713aSLionel Sambuc 
153f4a2713aSLionel Sambuc   // If CS1 only reads memory, the only dependence on CS2 can be
154f4a2713aSLionel Sambuc   // from CS1 reading memory written by CS2.
155f4a2713aSLionel Sambuc   if (onlyReadsMemory(CS1B))
156f4a2713aSLionel Sambuc     Mask = ModRefResult(Mask & Ref);
157f4a2713aSLionel Sambuc 
158f4a2713aSLionel Sambuc   // If CS2 only access memory through arguments, accumulate the mod/ref
159f4a2713aSLionel Sambuc   // information from CS1's references to the memory referenced by
160f4a2713aSLionel Sambuc   // CS2's arguments.
161f4a2713aSLionel Sambuc   if (onlyAccessesArgPointees(CS2B)) {
162f4a2713aSLionel Sambuc     AliasAnalysis::ModRefResult R = NoModRef;
163f4a2713aSLionel Sambuc     if (doesAccessArgPointees(CS2B)) {
164f4a2713aSLionel Sambuc       for (ImmutableCallSite::arg_iterator
165f4a2713aSLionel Sambuc            I = CS2.arg_begin(), E = CS2.arg_end(); I != E; ++I) {
166f4a2713aSLionel Sambuc         const Value *Arg = *I;
167f4a2713aSLionel Sambuc         if (!Arg->getType()->isPointerTy())
168f4a2713aSLionel Sambuc           continue;
169*0a6a1f1dSLionel Sambuc         ModRefResult ArgMask;
170*0a6a1f1dSLionel Sambuc         Location CS2Loc =
171*0a6a1f1dSLionel Sambuc           getArgLocation(CS2, (unsigned) std::distance(CS2.arg_begin(), I),
172*0a6a1f1dSLionel Sambuc                          ArgMask);
173*0a6a1f1dSLionel Sambuc         // ArgMask indicates what CS2 might do to CS2Loc, and the dependence of
174*0a6a1f1dSLionel Sambuc         // CS1 on that location is the inverse.
175*0a6a1f1dSLionel Sambuc         if (ArgMask == Mod)
176*0a6a1f1dSLionel Sambuc           ArgMask = ModRef;
177*0a6a1f1dSLionel Sambuc         else if (ArgMask == Ref)
178*0a6a1f1dSLionel Sambuc           ArgMask = Mod;
179*0a6a1f1dSLionel Sambuc 
180*0a6a1f1dSLionel Sambuc         R = ModRefResult((R | (getModRefInfo(CS1, CS2Loc) & ArgMask)) & Mask);
181f4a2713aSLionel Sambuc         if (R == Mask)
182f4a2713aSLionel Sambuc           break;
183f4a2713aSLionel Sambuc       }
184f4a2713aSLionel Sambuc     }
185f4a2713aSLionel Sambuc     return R;
186f4a2713aSLionel Sambuc   }
187f4a2713aSLionel Sambuc 
188f4a2713aSLionel Sambuc   // If CS1 only accesses memory through arguments, check if CS2 references
189f4a2713aSLionel Sambuc   // any of the memory referenced by CS1's arguments. If not, return NoModRef.
190f4a2713aSLionel Sambuc   if (onlyAccessesArgPointees(CS1B)) {
191f4a2713aSLionel Sambuc     AliasAnalysis::ModRefResult R = NoModRef;
192f4a2713aSLionel Sambuc     if (doesAccessArgPointees(CS1B)) {
193f4a2713aSLionel Sambuc       for (ImmutableCallSite::arg_iterator
194f4a2713aSLionel Sambuc            I = CS1.arg_begin(), E = CS1.arg_end(); I != E; ++I) {
195f4a2713aSLionel Sambuc         const Value *Arg = *I;
196f4a2713aSLionel Sambuc         if (!Arg->getType()->isPointerTy())
197f4a2713aSLionel Sambuc           continue;
198*0a6a1f1dSLionel Sambuc         ModRefResult ArgMask;
199*0a6a1f1dSLionel Sambuc         Location CS1Loc = getArgLocation(
200*0a6a1f1dSLionel Sambuc             CS1, (unsigned)std::distance(CS1.arg_begin(), I), ArgMask);
201*0a6a1f1dSLionel Sambuc         // ArgMask indicates what CS1 might do to CS1Loc; if CS1 might Mod
202*0a6a1f1dSLionel Sambuc         // CS1Loc, then we care about either a Mod or a Ref by CS2. If CS1
203*0a6a1f1dSLionel Sambuc         // might Ref, then we care only about a Mod by CS2.
204*0a6a1f1dSLionel Sambuc         ModRefResult ArgR = getModRefInfo(CS2, CS1Loc);
205*0a6a1f1dSLionel Sambuc         if (((ArgMask & Mod) != NoModRef && (ArgR & ModRef) != NoModRef) ||
206*0a6a1f1dSLionel Sambuc             ((ArgMask & Ref) != NoModRef && (ArgR & Mod)    != NoModRef))
207*0a6a1f1dSLionel Sambuc           R = ModRefResult((R | ArgMask) & Mask);
208*0a6a1f1dSLionel Sambuc 
209*0a6a1f1dSLionel Sambuc         if (R == Mask)
210f4a2713aSLionel Sambuc           break;
211f4a2713aSLionel Sambuc       }
212f4a2713aSLionel Sambuc     }
213f4a2713aSLionel Sambuc     return R;
214f4a2713aSLionel Sambuc   }
215f4a2713aSLionel Sambuc 
216f4a2713aSLionel Sambuc   // If this is the end of the chain, don't forward.
217f4a2713aSLionel Sambuc   if (!AA) return Mask;
218f4a2713aSLionel Sambuc 
219f4a2713aSLionel Sambuc   // Otherwise, fall back to the next AA in the chain. But we can merge
220f4a2713aSLionel Sambuc   // in any mask we've managed to compute.
221f4a2713aSLionel Sambuc   return ModRefResult(AA->getModRefInfo(CS1, CS2) & Mask);
222f4a2713aSLionel Sambuc }
223f4a2713aSLionel Sambuc 
224f4a2713aSLionel Sambuc AliasAnalysis::ModRefBehavior
getModRefBehavior(ImmutableCallSite CS)225f4a2713aSLionel Sambuc AliasAnalysis::getModRefBehavior(ImmutableCallSite CS) {
226f4a2713aSLionel Sambuc   assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
227f4a2713aSLionel Sambuc 
228f4a2713aSLionel Sambuc   ModRefBehavior Min = UnknownModRefBehavior;
229f4a2713aSLionel Sambuc 
230f4a2713aSLionel Sambuc   // Call back into the alias analysis with the other form of getModRefBehavior
231f4a2713aSLionel Sambuc   // to see if it can give a better response.
232f4a2713aSLionel Sambuc   if (const Function *F = CS.getCalledFunction())
233f4a2713aSLionel Sambuc     Min = getModRefBehavior(F);
234f4a2713aSLionel Sambuc 
235f4a2713aSLionel Sambuc   // If this is the end of the chain, don't forward.
236f4a2713aSLionel Sambuc   if (!AA) return Min;
237f4a2713aSLionel Sambuc 
238f4a2713aSLionel Sambuc   // Otherwise, fall back to the next AA in the chain. But we can merge
239f4a2713aSLionel Sambuc   // in any result we've managed to compute.
240f4a2713aSLionel Sambuc   return ModRefBehavior(AA->getModRefBehavior(CS) & Min);
241f4a2713aSLionel Sambuc }
242f4a2713aSLionel Sambuc 
243f4a2713aSLionel Sambuc AliasAnalysis::ModRefBehavior
getModRefBehavior(const Function * F)244f4a2713aSLionel Sambuc AliasAnalysis::getModRefBehavior(const Function *F) {
245f4a2713aSLionel Sambuc   assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
246f4a2713aSLionel Sambuc   return AA->getModRefBehavior(F);
247f4a2713aSLionel Sambuc }
248f4a2713aSLionel Sambuc 
249f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
250f4a2713aSLionel Sambuc // AliasAnalysis non-virtual helper method implementation
251f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
252f4a2713aSLionel Sambuc 
getLocation(const LoadInst * LI)253f4a2713aSLionel Sambuc AliasAnalysis::Location AliasAnalysis::getLocation(const LoadInst *LI) {
254*0a6a1f1dSLionel Sambuc   AAMDNodes AATags;
255*0a6a1f1dSLionel Sambuc   LI->getAAMetadata(AATags);
256*0a6a1f1dSLionel Sambuc 
257f4a2713aSLionel Sambuc   return Location(LI->getPointerOperand(),
258*0a6a1f1dSLionel Sambuc                   getTypeStoreSize(LI->getType()), AATags);
259f4a2713aSLionel Sambuc }
260f4a2713aSLionel Sambuc 
getLocation(const StoreInst * SI)261f4a2713aSLionel Sambuc AliasAnalysis::Location AliasAnalysis::getLocation(const StoreInst *SI) {
262*0a6a1f1dSLionel Sambuc   AAMDNodes AATags;
263*0a6a1f1dSLionel Sambuc   SI->getAAMetadata(AATags);
264*0a6a1f1dSLionel Sambuc 
265f4a2713aSLionel Sambuc   return Location(SI->getPointerOperand(),
266*0a6a1f1dSLionel Sambuc                   getTypeStoreSize(SI->getValueOperand()->getType()), AATags);
267f4a2713aSLionel Sambuc }
268f4a2713aSLionel Sambuc 
getLocation(const VAArgInst * VI)269f4a2713aSLionel Sambuc AliasAnalysis::Location AliasAnalysis::getLocation(const VAArgInst *VI) {
270*0a6a1f1dSLionel Sambuc   AAMDNodes AATags;
271*0a6a1f1dSLionel Sambuc   VI->getAAMetadata(AATags);
272*0a6a1f1dSLionel Sambuc 
273*0a6a1f1dSLionel Sambuc   return Location(VI->getPointerOperand(), UnknownSize, AATags);
274f4a2713aSLionel Sambuc }
275f4a2713aSLionel Sambuc 
276f4a2713aSLionel Sambuc AliasAnalysis::Location
getLocation(const AtomicCmpXchgInst * CXI)277f4a2713aSLionel Sambuc AliasAnalysis::getLocation(const AtomicCmpXchgInst *CXI) {
278*0a6a1f1dSLionel Sambuc   AAMDNodes AATags;
279*0a6a1f1dSLionel Sambuc   CXI->getAAMetadata(AATags);
280*0a6a1f1dSLionel Sambuc 
281f4a2713aSLionel Sambuc   return Location(CXI->getPointerOperand(),
282f4a2713aSLionel Sambuc                   getTypeStoreSize(CXI->getCompareOperand()->getType()),
283*0a6a1f1dSLionel Sambuc                   AATags);
284f4a2713aSLionel Sambuc }
285f4a2713aSLionel Sambuc 
286f4a2713aSLionel Sambuc AliasAnalysis::Location
getLocation(const AtomicRMWInst * RMWI)287f4a2713aSLionel Sambuc AliasAnalysis::getLocation(const AtomicRMWInst *RMWI) {
288*0a6a1f1dSLionel Sambuc   AAMDNodes AATags;
289*0a6a1f1dSLionel Sambuc   RMWI->getAAMetadata(AATags);
290*0a6a1f1dSLionel Sambuc 
291f4a2713aSLionel Sambuc   return Location(RMWI->getPointerOperand(),
292*0a6a1f1dSLionel Sambuc                   getTypeStoreSize(RMWI->getValOperand()->getType()), AATags);
293f4a2713aSLionel Sambuc }
294f4a2713aSLionel Sambuc 
295f4a2713aSLionel Sambuc AliasAnalysis::Location
getLocationForSource(const MemTransferInst * MTI)296f4a2713aSLionel Sambuc AliasAnalysis::getLocationForSource(const MemTransferInst *MTI) {
297f4a2713aSLionel Sambuc   uint64_t Size = UnknownSize;
298f4a2713aSLionel Sambuc   if (ConstantInt *C = dyn_cast<ConstantInt>(MTI->getLength()))
299f4a2713aSLionel Sambuc     Size = C->getValue().getZExtValue();
300f4a2713aSLionel Sambuc 
301*0a6a1f1dSLionel Sambuc   // memcpy/memmove can have AA tags. For memcpy, they apply
302f4a2713aSLionel Sambuc   // to both the source and the destination.
303*0a6a1f1dSLionel Sambuc   AAMDNodes AATags;
304*0a6a1f1dSLionel Sambuc   MTI->getAAMetadata(AATags);
305f4a2713aSLionel Sambuc 
306*0a6a1f1dSLionel Sambuc   return Location(MTI->getRawSource(), Size, AATags);
307f4a2713aSLionel Sambuc }
308f4a2713aSLionel Sambuc 
309f4a2713aSLionel Sambuc AliasAnalysis::Location
getLocationForDest(const MemIntrinsic * MTI)310f4a2713aSLionel Sambuc AliasAnalysis::getLocationForDest(const MemIntrinsic *MTI) {
311f4a2713aSLionel Sambuc   uint64_t Size = UnknownSize;
312f4a2713aSLionel Sambuc   if (ConstantInt *C = dyn_cast<ConstantInt>(MTI->getLength()))
313f4a2713aSLionel Sambuc     Size = C->getValue().getZExtValue();
314f4a2713aSLionel Sambuc 
315*0a6a1f1dSLionel Sambuc   // memcpy/memmove can have AA tags. For memcpy, they apply
316f4a2713aSLionel Sambuc   // to both the source and the destination.
317*0a6a1f1dSLionel Sambuc   AAMDNodes AATags;
318*0a6a1f1dSLionel Sambuc   MTI->getAAMetadata(AATags);
319f4a2713aSLionel Sambuc 
320*0a6a1f1dSLionel Sambuc   return Location(MTI->getRawDest(), Size, AATags);
321f4a2713aSLionel Sambuc }
322f4a2713aSLionel Sambuc 
323f4a2713aSLionel Sambuc 
324f4a2713aSLionel Sambuc 
325f4a2713aSLionel Sambuc AliasAnalysis::ModRefResult
getModRefInfo(const LoadInst * L,const Location & Loc)326f4a2713aSLionel Sambuc AliasAnalysis::getModRefInfo(const LoadInst *L, const Location &Loc) {
327f4a2713aSLionel Sambuc   // Be conservative in the face of volatile/atomic.
328f4a2713aSLionel Sambuc   if (!L->isUnordered())
329f4a2713aSLionel Sambuc     return ModRef;
330f4a2713aSLionel Sambuc 
331f4a2713aSLionel Sambuc   // If the load address doesn't alias the given address, it doesn't read
332f4a2713aSLionel Sambuc   // or write the specified memory.
333f4a2713aSLionel Sambuc   if (!alias(getLocation(L), Loc))
334f4a2713aSLionel Sambuc     return NoModRef;
335f4a2713aSLionel Sambuc 
336f4a2713aSLionel Sambuc   // Otherwise, a load just reads.
337f4a2713aSLionel Sambuc   return Ref;
338f4a2713aSLionel Sambuc }
339f4a2713aSLionel Sambuc 
340f4a2713aSLionel Sambuc AliasAnalysis::ModRefResult
getModRefInfo(const StoreInst * S,const Location & Loc)341f4a2713aSLionel Sambuc AliasAnalysis::getModRefInfo(const StoreInst *S, const Location &Loc) {
342f4a2713aSLionel Sambuc   // Be conservative in the face of volatile/atomic.
343f4a2713aSLionel Sambuc   if (!S->isUnordered())
344f4a2713aSLionel Sambuc     return ModRef;
345f4a2713aSLionel Sambuc 
346f4a2713aSLionel Sambuc   // If the store address cannot alias the pointer in question, then the
347f4a2713aSLionel Sambuc   // specified memory cannot be modified by the store.
348f4a2713aSLionel Sambuc   if (!alias(getLocation(S), Loc))
349f4a2713aSLionel Sambuc     return NoModRef;
350f4a2713aSLionel Sambuc 
351f4a2713aSLionel Sambuc   // If the pointer is a pointer to constant memory, then it could not have been
352f4a2713aSLionel Sambuc   // modified by this store.
353f4a2713aSLionel Sambuc   if (pointsToConstantMemory(Loc))
354f4a2713aSLionel Sambuc     return NoModRef;
355f4a2713aSLionel Sambuc 
356f4a2713aSLionel Sambuc   // Otherwise, a store just writes.
357f4a2713aSLionel Sambuc   return Mod;
358f4a2713aSLionel Sambuc }
359f4a2713aSLionel Sambuc 
360f4a2713aSLionel Sambuc AliasAnalysis::ModRefResult
getModRefInfo(const VAArgInst * V,const Location & Loc)361f4a2713aSLionel Sambuc AliasAnalysis::getModRefInfo(const VAArgInst *V, const Location &Loc) {
362f4a2713aSLionel Sambuc   // If the va_arg address cannot alias the pointer in question, then the
363f4a2713aSLionel Sambuc   // specified memory cannot be accessed by the va_arg.
364f4a2713aSLionel Sambuc   if (!alias(getLocation(V), Loc))
365f4a2713aSLionel Sambuc     return NoModRef;
366f4a2713aSLionel Sambuc 
367f4a2713aSLionel Sambuc   // If the pointer is a pointer to constant memory, then it could not have been
368f4a2713aSLionel Sambuc   // modified by this va_arg.
369f4a2713aSLionel Sambuc   if (pointsToConstantMemory(Loc))
370f4a2713aSLionel Sambuc     return NoModRef;
371f4a2713aSLionel Sambuc 
372f4a2713aSLionel Sambuc   // Otherwise, a va_arg reads and writes.
373f4a2713aSLionel Sambuc   return ModRef;
374f4a2713aSLionel Sambuc }
375f4a2713aSLionel Sambuc 
376f4a2713aSLionel Sambuc AliasAnalysis::ModRefResult
getModRefInfo(const AtomicCmpXchgInst * CX,const Location & Loc)377f4a2713aSLionel Sambuc AliasAnalysis::getModRefInfo(const AtomicCmpXchgInst *CX, const Location &Loc) {
378f4a2713aSLionel Sambuc   // Acquire/Release cmpxchg has properties that matter for arbitrary addresses.
379*0a6a1f1dSLionel Sambuc   if (CX->getSuccessOrdering() > Monotonic)
380f4a2713aSLionel Sambuc     return ModRef;
381f4a2713aSLionel Sambuc 
382f4a2713aSLionel Sambuc   // If the cmpxchg address does not alias the location, it does not access it.
383f4a2713aSLionel Sambuc   if (!alias(getLocation(CX), Loc))
384f4a2713aSLionel Sambuc     return NoModRef;
385f4a2713aSLionel Sambuc 
386f4a2713aSLionel Sambuc   return ModRef;
387f4a2713aSLionel Sambuc }
388f4a2713aSLionel Sambuc 
389f4a2713aSLionel Sambuc AliasAnalysis::ModRefResult
getModRefInfo(const AtomicRMWInst * RMW,const Location & Loc)390f4a2713aSLionel Sambuc AliasAnalysis::getModRefInfo(const AtomicRMWInst *RMW, const Location &Loc) {
391f4a2713aSLionel Sambuc   // Acquire/Release atomicrmw has properties that matter for arbitrary addresses.
392f4a2713aSLionel Sambuc   if (RMW->getOrdering() > Monotonic)
393f4a2713aSLionel Sambuc     return ModRef;
394f4a2713aSLionel Sambuc 
395f4a2713aSLionel Sambuc   // If the atomicrmw address does not alias the location, it does not access it.
396f4a2713aSLionel Sambuc   if (!alias(getLocation(RMW), Loc))
397f4a2713aSLionel Sambuc     return NoModRef;
398f4a2713aSLionel Sambuc 
399f4a2713aSLionel Sambuc   return ModRef;
400f4a2713aSLionel Sambuc }
401f4a2713aSLionel Sambuc 
402f4a2713aSLionel Sambuc // FIXME: this is really just shoring-up a deficiency in alias analysis.
403f4a2713aSLionel Sambuc // BasicAA isn't willing to spend linear time determining whether an alloca
404f4a2713aSLionel Sambuc // was captured before or after this particular call, while we are. However,
405f4a2713aSLionel Sambuc // with a smarter AA in place, this test is just wasting compile time.
406f4a2713aSLionel Sambuc AliasAnalysis::ModRefResult
callCapturesBefore(const Instruction * I,const AliasAnalysis::Location & MemLoc,DominatorTree * DT)407f4a2713aSLionel Sambuc AliasAnalysis::callCapturesBefore(const Instruction *I,
408f4a2713aSLionel Sambuc                                   const AliasAnalysis::Location &MemLoc,
409f4a2713aSLionel Sambuc                                   DominatorTree *DT) {
410*0a6a1f1dSLionel Sambuc   if (!DT || !DL) return AliasAnalysis::ModRef;
411f4a2713aSLionel Sambuc 
412*0a6a1f1dSLionel Sambuc   const Value *Object = GetUnderlyingObject(MemLoc.Ptr, DL);
413f4a2713aSLionel Sambuc   if (!isIdentifiedObject(Object) || isa<GlobalValue>(Object) ||
414f4a2713aSLionel Sambuc       isa<Constant>(Object))
415f4a2713aSLionel Sambuc     return AliasAnalysis::ModRef;
416f4a2713aSLionel Sambuc 
417f4a2713aSLionel Sambuc   ImmutableCallSite CS(I);
418f4a2713aSLionel Sambuc   if (!CS.getInstruction() || CS.getInstruction() == Object)
419f4a2713aSLionel Sambuc     return AliasAnalysis::ModRef;
420f4a2713aSLionel Sambuc 
421*0a6a1f1dSLionel Sambuc   if (llvm::PointerMayBeCapturedBefore(Object, /* ReturnCaptures */ true,
422*0a6a1f1dSLionel Sambuc                                        /* StoreCaptures */ true, I, DT,
423*0a6a1f1dSLionel Sambuc                                        /* include Object */ true))
424f4a2713aSLionel Sambuc     return AliasAnalysis::ModRef;
425f4a2713aSLionel Sambuc 
426f4a2713aSLionel Sambuc   unsigned ArgNo = 0;
427f4a2713aSLionel Sambuc   AliasAnalysis::ModRefResult R = AliasAnalysis::NoModRef;
428f4a2713aSLionel Sambuc   for (ImmutableCallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
429f4a2713aSLionel Sambuc        CI != CE; ++CI, ++ArgNo) {
430f4a2713aSLionel Sambuc     // Only look at the no-capture or byval pointer arguments.  If this
431f4a2713aSLionel Sambuc     // pointer were passed to arguments that were neither of these, then it
432f4a2713aSLionel Sambuc     // couldn't be no-capture.
433f4a2713aSLionel Sambuc     if (!(*CI)->getType()->isPointerTy() ||
434f4a2713aSLionel Sambuc         (!CS.doesNotCapture(ArgNo) && !CS.isByValArgument(ArgNo)))
435f4a2713aSLionel Sambuc       continue;
436f4a2713aSLionel Sambuc 
437f4a2713aSLionel Sambuc     // If this is a no-capture pointer argument, see if we can tell that it
438f4a2713aSLionel Sambuc     // is impossible to alias the pointer we're checking.  If not, we have to
439f4a2713aSLionel Sambuc     // assume that the call could touch the pointer, even though it doesn't
440f4a2713aSLionel Sambuc     // escape.
441f4a2713aSLionel Sambuc     if (isNoAlias(AliasAnalysis::Location(*CI),
442f4a2713aSLionel Sambuc                   AliasAnalysis::Location(Object)))
443f4a2713aSLionel Sambuc       continue;
444f4a2713aSLionel Sambuc     if (CS.doesNotAccessMemory(ArgNo))
445f4a2713aSLionel Sambuc       continue;
446f4a2713aSLionel Sambuc     if (CS.onlyReadsMemory(ArgNo)) {
447f4a2713aSLionel Sambuc       R = AliasAnalysis::Ref;
448f4a2713aSLionel Sambuc       continue;
449f4a2713aSLionel Sambuc     }
450f4a2713aSLionel Sambuc     return AliasAnalysis::ModRef;
451f4a2713aSLionel Sambuc   }
452f4a2713aSLionel Sambuc   return R;
453f4a2713aSLionel Sambuc }
454f4a2713aSLionel Sambuc 
455f4a2713aSLionel Sambuc // AliasAnalysis destructor: DO NOT move this to the header file for
456f4a2713aSLionel Sambuc // AliasAnalysis or else clients of the AliasAnalysis class may not depend on
457f4a2713aSLionel Sambuc // the AliasAnalysis.o file in the current .a file, causing alias analysis
458f4a2713aSLionel Sambuc // support to not be included in the tool correctly!
459f4a2713aSLionel Sambuc //
~AliasAnalysis()460f4a2713aSLionel Sambuc AliasAnalysis::~AliasAnalysis() {}
461f4a2713aSLionel Sambuc 
462f4a2713aSLionel Sambuc /// InitializeAliasAnalysis - Subclasses must call this method to initialize the
463f4a2713aSLionel Sambuc /// AliasAnalysis interface before any other methods are called.
464f4a2713aSLionel Sambuc ///
InitializeAliasAnalysis(Pass * P)465f4a2713aSLionel Sambuc void AliasAnalysis::InitializeAliasAnalysis(Pass *P) {
466*0a6a1f1dSLionel Sambuc   DataLayoutPass *DLP = P->getAnalysisIfAvailable<DataLayoutPass>();
467*0a6a1f1dSLionel Sambuc   DL = DLP ? &DLP->getDataLayout() : nullptr;
468f4a2713aSLionel Sambuc   TLI = P->getAnalysisIfAvailable<TargetLibraryInfo>();
469f4a2713aSLionel Sambuc   AA = &P->getAnalysis<AliasAnalysis>();
470f4a2713aSLionel Sambuc }
471f4a2713aSLionel Sambuc 
472f4a2713aSLionel Sambuc // getAnalysisUsage - All alias analysis implementations should invoke this
473f4a2713aSLionel Sambuc // directly (using AliasAnalysis::getAnalysisUsage(AU)).
getAnalysisUsage(AnalysisUsage & AU) const474f4a2713aSLionel Sambuc void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
475f4a2713aSLionel Sambuc   AU.addRequired<AliasAnalysis>();         // All AA's chain
476f4a2713aSLionel Sambuc }
477f4a2713aSLionel Sambuc 
478f4a2713aSLionel Sambuc /// getTypeStoreSize - Return the DataLayout store size for the given type,
479f4a2713aSLionel Sambuc /// if known, or a conservative value otherwise.
480f4a2713aSLionel Sambuc ///
getTypeStoreSize(Type * Ty)481f4a2713aSLionel Sambuc uint64_t AliasAnalysis::getTypeStoreSize(Type *Ty) {
482*0a6a1f1dSLionel Sambuc   return DL ? DL->getTypeStoreSize(Ty) : UnknownSize;
483f4a2713aSLionel Sambuc }
484f4a2713aSLionel Sambuc 
485f4a2713aSLionel Sambuc /// canBasicBlockModify - Return true if it is possible for execution of the
486*0a6a1f1dSLionel Sambuc /// specified basic block to modify the location Loc.
487f4a2713aSLionel Sambuc ///
canBasicBlockModify(const BasicBlock & BB,const Location & Loc)488f4a2713aSLionel Sambuc bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB,
489f4a2713aSLionel Sambuc                                         const Location &Loc) {
490*0a6a1f1dSLionel Sambuc   return canInstructionRangeModRef(BB.front(), BB.back(), Loc, Mod);
491f4a2713aSLionel Sambuc }
492f4a2713aSLionel Sambuc 
493*0a6a1f1dSLionel Sambuc /// canInstructionRangeModRef - Return true if it is possible for the
494*0a6a1f1dSLionel Sambuc /// execution of the specified instructions to mod\ref (according to the
495*0a6a1f1dSLionel Sambuc /// mode) the location Loc. The instructions to consider are all
496*0a6a1f1dSLionel Sambuc /// of the instructions in the range of [I1,I2] INCLUSIVE.
497*0a6a1f1dSLionel Sambuc /// I1 and I2 must be in the same basic block.
canInstructionRangeModRef(const Instruction & I1,const Instruction & I2,const Location & Loc,const ModRefResult Mode)498*0a6a1f1dSLionel Sambuc bool AliasAnalysis::canInstructionRangeModRef(const Instruction &I1,
499f4a2713aSLionel Sambuc                                               const Instruction &I2,
500*0a6a1f1dSLionel Sambuc                                               const Location &Loc,
501*0a6a1f1dSLionel Sambuc                                               const ModRefResult Mode) {
502f4a2713aSLionel Sambuc   assert(I1.getParent() == I2.getParent() &&
503f4a2713aSLionel Sambuc          "Instructions not in same basic block!");
504f4a2713aSLionel Sambuc   BasicBlock::const_iterator I = &I1;
505f4a2713aSLionel Sambuc   BasicBlock::const_iterator E = &I2;
506f4a2713aSLionel Sambuc   ++E;  // Convert from inclusive to exclusive range.
507f4a2713aSLionel Sambuc 
508f4a2713aSLionel Sambuc   for (; I != E; ++I) // Check every instruction in range
509*0a6a1f1dSLionel Sambuc     if (getModRefInfo(I, Loc) & Mode)
510f4a2713aSLionel Sambuc       return true;
511f4a2713aSLionel Sambuc   return false;
512f4a2713aSLionel Sambuc }
513f4a2713aSLionel Sambuc 
514f4a2713aSLionel Sambuc /// isNoAliasCall - Return true if this pointer is returned by a noalias
515f4a2713aSLionel Sambuc /// function.
isNoAliasCall(const Value * V)516f4a2713aSLionel Sambuc bool llvm::isNoAliasCall(const Value *V) {
517f4a2713aSLionel Sambuc   if (isa<CallInst>(V) || isa<InvokeInst>(V))
518f4a2713aSLionel Sambuc     return ImmutableCallSite(cast<Instruction>(V))
519f4a2713aSLionel Sambuc       .paramHasAttr(0, Attribute::NoAlias);
520f4a2713aSLionel Sambuc   return false;
521f4a2713aSLionel Sambuc }
522f4a2713aSLionel Sambuc 
523f4a2713aSLionel Sambuc /// isNoAliasArgument - Return true if this is an argument with the noalias
524f4a2713aSLionel Sambuc /// attribute.
isNoAliasArgument(const Value * V)525f4a2713aSLionel Sambuc bool llvm::isNoAliasArgument(const Value *V)
526f4a2713aSLionel Sambuc {
527f4a2713aSLionel Sambuc   if (const Argument *A = dyn_cast<Argument>(V))
528f4a2713aSLionel Sambuc     return A->hasNoAliasAttr();
529f4a2713aSLionel Sambuc   return false;
530f4a2713aSLionel Sambuc }
531f4a2713aSLionel Sambuc 
532f4a2713aSLionel Sambuc /// isIdentifiedObject - Return true if this pointer refers to a distinct and
533f4a2713aSLionel Sambuc /// identifiable object.  This returns true for:
534f4a2713aSLionel Sambuc ///    Global Variables and Functions (but not Global Aliases)
535f4a2713aSLionel Sambuc ///    Allocas and Mallocs
536f4a2713aSLionel Sambuc ///    ByVal and NoAlias Arguments
537f4a2713aSLionel Sambuc ///    NoAlias returns
538f4a2713aSLionel Sambuc ///
isIdentifiedObject(const Value * V)539f4a2713aSLionel Sambuc bool llvm::isIdentifiedObject(const Value *V) {
540f4a2713aSLionel Sambuc   if (isa<AllocaInst>(V))
541f4a2713aSLionel Sambuc     return true;
542f4a2713aSLionel Sambuc   if (isa<GlobalValue>(V) && !isa<GlobalAlias>(V))
543f4a2713aSLionel Sambuc     return true;
544f4a2713aSLionel Sambuc   if (isNoAliasCall(V))
545f4a2713aSLionel Sambuc     return true;
546f4a2713aSLionel Sambuc   if (const Argument *A = dyn_cast<Argument>(V))
547f4a2713aSLionel Sambuc     return A->hasNoAliasAttr() || A->hasByValAttr();
548f4a2713aSLionel Sambuc   return false;
549f4a2713aSLionel Sambuc }
550*0a6a1f1dSLionel Sambuc 
551*0a6a1f1dSLionel Sambuc /// isIdentifiedFunctionLocal - Return true if V is umabigously identified
552*0a6a1f1dSLionel Sambuc /// at the function-level. Different IdentifiedFunctionLocals can't alias.
553*0a6a1f1dSLionel Sambuc /// Further, an IdentifiedFunctionLocal can not alias with any function
554*0a6a1f1dSLionel Sambuc /// arguments other than itself, which is not necessarily true for
555*0a6a1f1dSLionel Sambuc /// IdentifiedObjects.
isIdentifiedFunctionLocal(const Value * V)556*0a6a1f1dSLionel Sambuc bool llvm::isIdentifiedFunctionLocal(const Value *V)
557*0a6a1f1dSLionel Sambuc {
558*0a6a1f1dSLionel Sambuc   return isa<AllocaInst>(V) || isNoAliasCall(V) || isNoAliasArgument(V);
559*0a6a1f1dSLionel Sambuc }
560