blob: 5171a45674568b346db6851547325eed75833466 [file] [log] [blame]
Chris Lattner7d58f8d2002-08-22 18:25:32 +00001//===- AliasAnalysis.cpp - Generic Alias Analysis Interface Implementation -==//
Misha Brukman01808ca2005-04-21 21:13:18 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman01808ca2005-04-21 21:13:18 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner7d58f8d2002-08-22 18:25:32 +00009//
10// This file implements the generic AliasAnalysis interface which is used as the
11// common interface used by all clients and implementations of alias analysis.
12//
13// This file also implements the default version of the AliasAnalysis interface
14// that is to be used when no other implementation is specified. This does some
15// simple tests that detect obvious cases: two different global pointers cannot
16// alias, a global cannot alias a malloc, two different mallocs cannot alias,
17// etc.
18//
19// This alias analysis implementation really isn't very good for anything, but
20// it is very fast, and makes a nice clean default implementation. Because it
21// handles lots of little corner cases, other, more complex, alias analysis
22// implementations may choose to rely on this pass to resolve these simple and
23// easy cases.
24//
25//===----------------------------------------------------------------------===//
26
Chris Lattnerd6a2a992003-02-26 19:41:54 +000027#include "llvm/Analysis/AliasAnalysis.h"
Nick Lewycky0b682452013-07-27 01:24:00 +000028#include "llvm/Analysis/CFG.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000029#include "llvm/Analysis/CaptureTracking.h"
Chad Rosiera968caf2012-05-14 20:35:04 +000030#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000031#include "llvm/IR/BasicBlock.h"
32#include "llvm/IR/DataLayout.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000033#include "llvm/IR/Dominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000034#include "llvm/IR/Function.h"
35#include "llvm/IR/Instructions.h"
36#include "llvm/IR/IntrinsicInst.h"
37#include "llvm/IR/LLVMContext.h"
38#include "llvm/IR/Type.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000039#include "llvm/Pass.h"
Benjamin Kramer8bcc9712012-08-29 15:32:21 +000040#include "llvm/Target/TargetLibraryInfo.h"
Chris Lattnera67dbd02004-03-15 04:07:29 +000041using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000042
Chris Lattner7d58f8d2002-08-22 18:25:32 +000043// Register the AliasAnalysis interface, providing a nice name to refer to.
Owen Anderson6c18d1a2010-10-19 17:21:58 +000044INITIALIZE_ANALYSIS_GROUP(AliasAnalysis, "Alias Analysis", NoAA)
Devang Patel8c78a0b2007-05-03 01:11:54 +000045char AliasAnalysis::ID = 0;
Chris Lattner7d58f8d2002-08-22 18:25:32 +000046
Chris Lattner62c37002004-05-23 21:15:48 +000047//===----------------------------------------------------------------------===//
48// Default chaining methods
49//===----------------------------------------------------------------------===//
50
51AliasAnalysis::AliasResult
Dan Gohman41f14cf2010-09-14 21:25:10 +000052AliasAnalysis::alias(const Location &LocA, const Location &LocB) {
Chris Lattner62c37002004-05-23 21:15:48 +000053 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
Dan Gohman41f14cf2010-09-14 21:25:10 +000054 return AA->alias(LocA, LocB);
Chris Lattner62c37002004-05-23 21:15:48 +000055}
56
Dan Gohman9130bad2010-11-08 16:45:26 +000057bool AliasAnalysis::pointsToConstantMemory(const Location &Loc,
58 bool OrLocal) {
Chris Lattner62c37002004-05-23 21:15:48 +000059 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
Dan Gohman9130bad2010-11-08 16:45:26 +000060 return AA->pointsToConstantMemory(Loc, OrLocal);
Chris Lattner62c37002004-05-23 21:15:48 +000061}
62
Hal Finkel354e23b2014-07-17 01:28:25 +000063AliasAnalysis::Location
64AliasAnalysis::getArgLocation(ImmutableCallSite CS, unsigned ArgIdx,
65 AliasAnalysis::ModRefResult &Mask) {
66 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
67 return AA->getArgLocation(CS, ArgIdx, Mask);
68}
69
Chris Lattner62c37002004-05-23 21:15:48 +000070void AliasAnalysis::deleteValue(Value *V) {
71 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
72 AA->deleteValue(V);
73}
74
75void AliasAnalysis::copyValue(Value *From, Value *To) {
76 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
77 AA->copyValue(From, To);
78}
79
Owen Andersonb6e4ff02011-01-03 21:38:41 +000080void AliasAnalysis::addEscapingUse(Use &U) {
81 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
82 AA->addEscapingUse(U);
83}
84
85
Chris Lattner62c37002004-05-23 21:15:48 +000086AliasAnalysis::ModRefResult
Dan Gohman5f1702e2010-08-06 01:25:49 +000087AliasAnalysis::getModRefInfo(ImmutableCallSite CS,
Dan Gohman41f14cf2010-09-14 21:25:10 +000088 const Location &Loc) {
Dan Gohman1033ce62010-10-25 16:28:57 +000089 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
Dan Gohman5f1702e2010-08-06 01:25:49 +000090
91 ModRefBehavior MRB = getModRefBehavior(CS);
92 if (MRB == DoesNotAccessMemory)
93 return NoModRef;
94
95 ModRefResult Mask = ModRef;
Dan Gohman5d06f892010-11-09 20:06:55 +000096 if (onlyReadsMemory(MRB))
Dan Gohman5f1702e2010-08-06 01:25:49 +000097 Mask = Ref;
Dan Gohman5d06f892010-11-09 20:06:55 +000098
Dan Gohman066c1bb2010-11-10 18:17:28 +000099 if (onlyAccessesArgPointees(MRB)) {
Dan Gohman5f1702e2010-08-06 01:25:49 +0000100 bool doesAlias = false;
Hal Finkel354e23b2014-07-17 01:28:25 +0000101 ModRefResult AllArgsMask = NoModRef;
Dan Gohman39b3a1e2011-04-27 18:39:03 +0000102 if (doesAccessArgPointees(MRB)) {
Dan Gohman066c1bb2010-11-10 18:17:28 +0000103 for (ImmutableCallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
Dan Gohman39b3a1e2011-04-27 18:39:03 +0000104 AI != AE; ++AI) {
105 const Value *Arg = *AI;
106 if (!Arg->getType()->isPointerTy())
107 continue;
Hal Finkel354e23b2014-07-17 01:28:25 +0000108 ModRefResult ArgMask;
109 Location CSLoc =
110 getArgLocation(CS, (unsigned) std::distance(CS.arg_begin(), AI),
111 ArgMask);
Dan Gohman39b3a1e2011-04-27 18:39:03 +0000112 if (!isNoAlias(CSLoc, Loc)) {
Dan Gohman066c1bb2010-11-10 18:17:28 +0000113 doesAlias = true;
Hal Finkel354e23b2014-07-17 01:28:25 +0000114 AllArgsMask = ModRefResult(AllArgsMask | ArgMask);
Dan Gohman066c1bb2010-11-10 18:17:28 +0000115 }
Dan Gohman39b3a1e2011-04-27 18:39:03 +0000116 }
117 }
Dan Gohman5f1702e2010-08-06 01:25:49 +0000118 if (!doesAlias)
119 return NoModRef;
Hal Finkel354e23b2014-07-17 01:28:25 +0000120 Mask = ModRefResult(Mask & AllArgsMask);
Dan Gohman5f1702e2010-08-06 01:25:49 +0000121 }
122
Dan Gohman41f14cf2010-09-14 21:25:10 +0000123 // If Loc is a constant memory location, the call definitely could not
Dan Gohman5f1702e2010-08-06 01:25:49 +0000124 // modify the memory location.
Dan Gohman41f14cf2010-09-14 21:25:10 +0000125 if ((Mask & Mod) && pointsToConstantMemory(Loc))
Dan Gohman5f1702e2010-08-06 01:25:49 +0000126 Mask = ModRefResult(Mask & ~Mod);
127
Dan Gohmanabaf2d82010-10-25 16:29:52 +0000128 // If this is the end of the chain, don't forward.
Dan Gohman5f1702e2010-08-06 01:25:49 +0000129 if (!AA) return Mask;
130
131 // Otherwise, fall back to the next AA in the chain. But we can merge
132 // in any mask we've managed to compute.
Dan Gohman41f14cf2010-09-14 21:25:10 +0000133 return ModRefResult(AA->getModRefInfo(CS, Loc) & Mask);
Dan Gohman5f1702e2010-08-06 01:25:49 +0000134}
135
136AliasAnalysis::ModRefResult
Dan Gohman5442c712010-08-03 21:48:53 +0000137AliasAnalysis::getModRefInfo(ImmutableCallSite CS1, ImmutableCallSite CS2) {
Dan Gohman1033ce62010-10-25 16:28:57 +0000138 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
Dan Gohman5f1702e2010-08-06 01:25:49 +0000139
140 // If CS1 or CS2 are readnone, they don't interact.
141 ModRefBehavior CS1B = getModRefBehavior(CS1);
142 if (CS1B == DoesNotAccessMemory) return NoModRef;
143
144 ModRefBehavior CS2B = getModRefBehavior(CS2);
145 if (CS2B == DoesNotAccessMemory) return NoModRef;
146
147 // If they both only read from memory, there is no dependence.
Dan Gohman5d06f892010-11-09 20:06:55 +0000148 if (onlyReadsMemory(CS1B) && onlyReadsMemory(CS2B))
Dan Gohman5f1702e2010-08-06 01:25:49 +0000149 return NoModRef;
150
151 AliasAnalysis::ModRefResult Mask = ModRef;
152
153 // If CS1 only reads memory, the only dependence on CS2 can be
154 // from CS1 reading memory written by CS2.
Dan Gohman5d06f892010-11-09 20:06:55 +0000155 if (onlyReadsMemory(CS1B))
Dan Gohman5f1702e2010-08-06 01:25:49 +0000156 Mask = ModRefResult(Mask & Ref);
157
158 // If CS2 only access memory through arguments, accumulate the mod/ref
159 // information from CS1's references to the memory referenced by
160 // CS2's arguments.
Dan Gohman066c1bb2010-11-10 18:17:28 +0000161 if (onlyAccessesArgPointees(CS2B)) {
Dan Gohman5f1702e2010-08-06 01:25:49 +0000162 AliasAnalysis::ModRefResult R = NoModRef;
Dan Gohman39b3a1e2011-04-27 18:39:03 +0000163 if (doesAccessArgPointees(CS2B)) {
Dan Gohman066c1bb2010-11-10 18:17:28 +0000164 for (ImmutableCallSite::arg_iterator
165 I = CS2.arg_begin(), E = CS2.arg_end(); I != E; ++I) {
Dan Gohman39b3a1e2011-04-27 18:39:03 +0000166 const Value *Arg = *I;
167 if (!Arg->getType()->isPointerTy())
168 continue;
Hal Finkel354e23b2014-07-17 01:28:25 +0000169 ModRefResult ArgMask;
170 Location CS2Loc =
171 getArgLocation(CS2, (unsigned) std::distance(CS2.arg_begin(), I),
172 ArgMask);
173 // ArgMask indicates what CS2 might do to CS2Loc, and the dependence of
174 // CS1 on that location is the inverse.
175 if (ArgMask == Mod)
176 ArgMask = ModRef;
177 else if (ArgMask == Ref)
178 ArgMask = Mod;
179
180 R = ModRefResult((R | (getModRefInfo(CS1, CS2Loc) & ArgMask)) & Mask);
Dan Gohman066c1bb2010-11-10 18:17:28 +0000181 if (R == Mask)
182 break;
183 }
Dan Gohman39b3a1e2011-04-27 18:39:03 +0000184 }
Dan Gohman5f1702e2010-08-06 01:25:49 +0000185 return R;
186 }
187
188 // If CS1 only accesses memory through arguments, check if CS2 references
189 // any of the memory referenced by CS1's arguments. If not, return NoModRef.
Dan Gohman066c1bb2010-11-10 18:17:28 +0000190 if (onlyAccessesArgPointees(CS1B)) {
Dan Gohman5f1702e2010-08-06 01:25:49 +0000191 AliasAnalysis::ModRefResult R = NoModRef;
Dan Gohman39b3a1e2011-04-27 18:39:03 +0000192 if (doesAccessArgPointees(CS1B)) {
Dan Gohman066c1bb2010-11-10 18:17:28 +0000193 for (ImmutableCallSite::arg_iterator
Dan Gohman39b3a1e2011-04-27 18:39:03 +0000194 I = CS1.arg_begin(), E = CS1.arg_end(); I != E; ++I) {
195 const Value *Arg = *I;
196 if (!Arg->getType()->isPointerTy())
197 continue;
Hal Finkel354e23b2014-07-17 01:28:25 +0000198 ModRefResult ArgMask;
NAKAMURA Takumid0e13af2014-10-28 11:54:52 +0000199 Location CS1Loc = getArgLocation(
200 CS1, (unsigned)std::distance(CS1.arg_begin(), I), ArgMask);
NAKAMURA Takumi335a7bc2014-10-28 11:53:30 +0000201 // ArgMask indicates what CS1 might do to CS1Loc; if CS1 might Mod
202 // CS1Loc, then we care about either a Mod or a Ref by CS2. If CS1
203 // might Ref, then we care only about a Mod by CS2.
Hal Finkel354e23b2014-07-17 01:28:25 +0000204 ModRefResult ArgR = getModRefInfo(CS2, CS1Loc);
205 if (((ArgMask & Mod) != NoModRef && (ArgR & ModRef) != NoModRef) ||
206 ((ArgMask & Ref) != NoModRef && (ArgR & Mod) != NoModRef))
207 R = ModRefResult((R | ArgMask) & Mask);
208
209 if (R == Mask)
Dan Gohman066c1bb2010-11-10 18:17:28 +0000210 break;
Dan Gohman39b3a1e2011-04-27 18:39:03 +0000211 }
212 }
Hal Finkel354e23b2014-07-17 01:28:25 +0000213 return R;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000214 }
215
Dan Gohmanabaf2d82010-10-25 16:29:52 +0000216 // If this is the end of the chain, don't forward.
Dan Gohman5f1702e2010-08-06 01:25:49 +0000217 if (!AA) return Mask;
218
219 // Otherwise, fall back to the next AA in the chain. But we can merge
220 // in any mask we've managed to compute.
221 return ModRefResult(AA->getModRefInfo(CS1, CS2) & Mask);
222}
223
224AliasAnalysis::ModRefBehavior
225AliasAnalysis::getModRefBehavior(ImmutableCallSite CS) {
Dan Gohman1033ce62010-10-25 16:28:57 +0000226 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
Dan Gohman5f1702e2010-08-06 01:25:49 +0000227
228 ModRefBehavior Min = UnknownModRefBehavior;
229
230 // Call back into the alias analysis with the other form of getModRefBehavior
231 // to see if it can give a better response.
232 if (const Function *F = CS.getCalledFunction())
233 Min = getModRefBehavior(F);
234
Dan Gohmanabaf2d82010-10-25 16:29:52 +0000235 // If this is the end of the chain, don't forward.
Dan Gohman5f1702e2010-08-06 01:25:49 +0000236 if (!AA) return Min;
237
238 // Otherwise, fall back to the next AA in the chain. But we can merge
239 // in any result we've managed to compute.
Dan Gohman2694e142010-11-10 01:02:18 +0000240 return ModRefBehavior(AA->getModRefBehavior(CS) & Min);
Dan Gohman5f1702e2010-08-06 01:25:49 +0000241}
242
243AliasAnalysis::ModRefBehavior
244AliasAnalysis::getModRefBehavior(const Function *F) {
Chris Lattner62c37002004-05-23 21:15:48 +0000245 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
Dan Gohman5f1702e2010-08-06 01:25:49 +0000246 return AA->getModRefBehavior(F);
Chris Lattner62c37002004-05-23 21:15:48 +0000247}
248
Chris Lattner62c37002004-05-23 21:15:48 +0000249//===----------------------------------------------------------------------===//
250// AliasAnalysis non-virtual helper method implementation
251//===----------------------------------------------------------------------===//
252
Dan Gohman65316d62010-11-11 21:50:19 +0000253AliasAnalysis::Location AliasAnalysis::getLocation(const LoadInst *LI) {
Hal Finkelcc39b672014-07-24 12:16:19 +0000254 AAMDNodes AATags;
255 LI->getAAMetadata(AATags);
256
Dan Gohman65316d62010-11-11 21:50:19 +0000257 return Location(LI->getPointerOperand(),
Hal Finkelcc39b672014-07-24 12:16:19 +0000258 getTypeStoreSize(LI->getType()), AATags);
Dan Gohman65316d62010-11-11 21:50:19 +0000259}
260
261AliasAnalysis::Location AliasAnalysis::getLocation(const StoreInst *SI) {
Hal Finkelcc39b672014-07-24 12:16:19 +0000262 AAMDNodes AATags;
263 SI->getAAMetadata(AATags);
264
Dan Gohman65316d62010-11-11 21:50:19 +0000265 return Location(SI->getPointerOperand(),
Hal Finkelcc39b672014-07-24 12:16:19 +0000266 getTypeStoreSize(SI->getValueOperand()->getType()), AATags);
Dan Gohman65316d62010-11-11 21:50:19 +0000267}
268
269AliasAnalysis::Location AliasAnalysis::getLocation(const VAArgInst *VI) {
Hal Finkelcc39b672014-07-24 12:16:19 +0000270 AAMDNodes AATags;
271 VI->getAAMetadata(AATags);
272
273 return Location(VI->getPointerOperand(), UnknownSize, AATags);
Dan Gohman65316d62010-11-11 21:50:19 +0000274}
275
Eli Friedman5c918912011-09-26 20:15:28 +0000276AliasAnalysis::Location
277AliasAnalysis::getLocation(const AtomicCmpXchgInst *CXI) {
Hal Finkelcc39b672014-07-24 12:16:19 +0000278 AAMDNodes AATags;
279 CXI->getAAMetadata(AATags);
280
Eli Friedman5c918912011-09-26 20:15:28 +0000281 return Location(CXI->getPointerOperand(),
282 getTypeStoreSize(CXI->getCompareOperand()->getType()),
Hal Finkelcc39b672014-07-24 12:16:19 +0000283 AATags);
Eli Friedman5c918912011-09-26 20:15:28 +0000284}
285
286AliasAnalysis::Location
287AliasAnalysis::getLocation(const AtomicRMWInst *RMWI) {
Hal Finkelcc39b672014-07-24 12:16:19 +0000288 AAMDNodes AATags;
289 RMWI->getAAMetadata(AATags);
290
Eli Friedman5c918912011-09-26 20:15:28 +0000291 return Location(RMWI->getPointerOperand(),
Hal Finkelcc39b672014-07-24 12:16:19 +0000292 getTypeStoreSize(RMWI->getValOperand()->getType()), AATags);
Eli Friedman5c918912011-09-26 20:15:28 +0000293}
Chris Lattner663ba912010-11-21 07:51:27 +0000294
NAKAMURA Takumi335a7bc2014-10-28 11:53:30 +0000295AliasAnalysis::Location
Chris Lattner663ba912010-11-21 07:51:27 +0000296AliasAnalysis::getLocationForSource(const MemTransferInst *MTI) {
297 uint64_t Size = UnknownSize;
298 if (ConstantInt *C = dyn_cast<ConstantInt>(MTI->getLength()))
299 Size = C->getValue().getZExtValue();
300
Hal Finkelcc39b672014-07-24 12:16:19 +0000301 // memcpy/memmove can have AA tags. For memcpy, they apply
Dan Gohmane1a17a32010-12-16 02:51:19 +0000302 // to both the source and the destination.
Hal Finkelcc39b672014-07-24 12:16:19 +0000303 AAMDNodes AATags;
304 MTI->getAAMetadata(AATags);
NAKAMURA Takumi335a7bc2014-10-28 11:53:30 +0000305
Hal Finkelcc39b672014-07-24 12:16:19 +0000306 return Location(MTI->getRawSource(), Size, AATags);
Chris Lattner663ba912010-11-21 07:51:27 +0000307}
308
NAKAMURA Takumi335a7bc2014-10-28 11:53:30 +0000309AliasAnalysis::Location
Chris Lattnerafbc0c22010-11-30 01:48:20 +0000310AliasAnalysis::getLocationForDest(const MemIntrinsic *MTI) {
Chris Lattner663ba912010-11-21 07:51:27 +0000311 uint64_t Size = UnknownSize;
312 if (ConstantInt *C = dyn_cast<ConstantInt>(MTI->getLength()))
313 Size = C->getValue().getZExtValue();
Dan Gohmane1a17a32010-12-16 02:51:19 +0000314
Hal Finkelcc39b672014-07-24 12:16:19 +0000315 // memcpy/memmove can have AA tags. For memcpy, they apply
Dan Gohmane1a17a32010-12-16 02:51:19 +0000316 // to both the source and the destination.
Hal Finkelcc39b672014-07-24 12:16:19 +0000317 AAMDNodes AATags;
Benjamin Kramer2e52f022014-10-04 22:44:29 +0000318 MTI->getAAMetadata(AATags);
NAKAMURA Takumi335a7bc2014-10-28 11:53:30 +0000319
Hal Finkelcc39b672014-07-24 12:16:19 +0000320 return Location(MTI->getRawDest(), Size, AATags);
Chris Lattner663ba912010-11-21 07:51:27 +0000321}
322
323
324
Chris Lattner3a311832003-02-26 19:26:51 +0000325AliasAnalysis::ModRefResult
Dan Gohman41f14cf2010-09-14 21:25:10 +0000326AliasAnalysis::getModRefInfo(const LoadInst *L, const Location &Loc) {
Eli Friedman5494ada2011-08-15 20:54:19 +0000327 // Be conservative in the face of volatile/atomic.
328 if (!L->isUnordered())
Dan Gohman6b4671b2010-08-06 18:11:28 +0000329 return ModRef;
330
Dan Gohman52f9d7d2010-08-03 17:27:43 +0000331 // If the load address doesn't alias the given address, it doesn't read
332 // or write the specified memory.
Dan Gohman65316d62010-11-11 21:50:19 +0000333 if (!alias(getLocation(L), Loc))
Dan Gohman52f9d7d2010-08-03 17:27:43 +0000334 return NoModRef;
335
Dan Gohman52f9d7d2010-08-03 17:27:43 +0000336 // Otherwise, a load just reads.
337 return Ref;
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000338}
339
Chris Lattner3a311832003-02-26 19:26:51 +0000340AliasAnalysis::ModRefResult
Dan Gohman41f14cf2010-09-14 21:25:10 +0000341AliasAnalysis::getModRefInfo(const StoreInst *S, const Location &Loc) {
Eli Friedman5494ada2011-08-15 20:54:19 +0000342 // Be conservative in the face of volatile/atomic.
343 if (!S->isUnordered())
Dan Gohman6b4671b2010-08-06 18:11:28 +0000344 return ModRef;
345
Dan Gohman23976df2010-08-06 18:10:45 +0000346 // If the store address cannot alias the pointer in question, then the
347 // specified memory cannot be modified by the store.
Dan Gohman65316d62010-11-11 21:50:19 +0000348 if (!alias(getLocation(S), Loc))
Chris Lattner96055762004-01-30 22:16:42 +0000349 return NoModRef;
350
351 // If the pointer is a pointer to constant memory, then it could not have been
352 // modified by this store.
Dan Gohman41f14cf2010-09-14 21:25:10 +0000353 if (pointsToConstantMemory(Loc))
Dan Gohman52f9d7d2010-08-03 17:27:43 +0000354 return NoModRef;
355
356 // Otherwise, a store just writes.
357 return Mod;
Chris Lattner3a311832003-02-26 19:26:51 +0000358}
359
Dan Gohmane68958f2010-08-06 18:24:38 +0000360AliasAnalysis::ModRefResult
Dan Gohman41f14cf2010-09-14 21:25:10 +0000361AliasAnalysis::getModRefInfo(const VAArgInst *V, const Location &Loc) {
Dan Gohmane68958f2010-08-06 18:24:38 +0000362 // If the va_arg address cannot alias the pointer in question, then the
363 // specified memory cannot be accessed by the va_arg.
Dan Gohman65316d62010-11-11 21:50:19 +0000364 if (!alias(getLocation(V), Loc))
Dan Gohmane68958f2010-08-06 18:24:38 +0000365 return NoModRef;
366
367 // If the pointer is a pointer to constant memory, then it could not have been
368 // modified by this va_arg.
Dan Gohman41f14cf2010-09-14 21:25:10 +0000369 if (pointsToConstantMemory(Loc))
Dan Gohmane68958f2010-08-06 18:24:38 +0000370 return NoModRef;
371
372 // Otherwise, a va_arg reads and writes.
373 return ModRef;
374}
375
Eli Friedman5c918912011-09-26 20:15:28 +0000376AliasAnalysis::ModRefResult
377AliasAnalysis::getModRefInfo(const AtomicCmpXchgInst *CX, const Location &Loc) {
378 // Acquire/Release cmpxchg has properties that matter for arbitrary addresses.
Tim Northovere94a5182014-03-11 10:48:52 +0000379 if (CX->getSuccessOrdering() > Monotonic)
Eli Friedman5c918912011-09-26 20:15:28 +0000380 return ModRef;
381
382 // If the cmpxchg address does not alias the location, it does not access it.
383 if (!alias(getLocation(CX), Loc))
384 return NoModRef;
385
386 return ModRef;
387}
388
389AliasAnalysis::ModRefResult
390AliasAnalysis::getModRefInfo(const AtomicRMWInst *RMW, const Location &Loc) {
391 // Acquire/Release atomicrmw has properties that matter for arbitrary addresses.
392 if (RMW->getOrdering() > Monotonic)
393 return ModRef;
394
395 // If the atomicrmw address does not alias the location, it does not access it.
396 if (!alias(getLocation(RMW), Loc))
397 return NoModRef;
398
399 return ModRef;
400}
401
Chad Rosiera968caf2012-05-14 20:35:04 +0000402// FIXME: this is really just shoring-up a deficiency in alias analysis.
403// BasicAA isn't willing to spend linear time determining whether an alloca
404// was captured before or after this particular call, while we are. However,
405// with a smarter AA in place, this test is just wasting compile time.
406AliasAnalysis::ModRefResult
407AliasAnalysis::callCapturesBefore(const Instruction *I,
408 const AliasAnalysis::Location &MemLoc,
409 DominatorTree *DT) {
Rafael Espindola7c68beb2014-02-18 15:33:12 +0000410 if (!DT || !DL) return AliasAnalysis::ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000411
Rafael Espindola7c68beb2014-02-18 15:33:12 +0000412 const Value *Object = GetUnderlyingObject(MemLoc.Ptr, DL);
Chad Rosiera968caf2012-05-14 20:35:04 +0000413 if (!isIdentifiedObject(Object) || isa<GlobalValue>(Object) ||
414 isa<Constant>(Object))
415 return AliasAnalysis::ModRef;
416
417 ImmutableCallSite CS(I);
418 if (!CS.getInstruction() || CS.getInstruction() == Object)
419 return AliasAnalysis::ModRef;
420
Hal Finkelb0356212014-07-21 13:15:48 +0000421 if (llvm::PointerMayBeCapturedBefore(Object, /* ReturnCaptures */ true,
Hal Finkeld32803b2014-07-21 21:30:22 +0000422 /* StoreCaptures */ true, I, DT,
423 /* include Object */ true))
Chad Rosiera968caf2012-05-14 20:35:04 +0000424 return AliasAnalysis::ModRef;
425
426 unsigned ArgNo = 0;
Nick Lewyckyc0514622013-07-07 10:15:16 +0000427 AliasAnalysis::ModRefResult R = AliasAnalysis::NoModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000428 for (ImmutableCallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
429 CI != CE; ++CI, ++ArgNo) {
430 // Only look at the no-capture or byval pointer arguments. If this
431 // pointer were passed to arguments that were neither of these, then it
432 // couldn't be no-capture.
433 if (!(*CI)->getType()->isPointerTy() ||
434 (!CS.doesNotCapture(ArgNo) && !CS.isByValArgument(ArgNo)))
435 continue;
436
437 // If this is a no-capture pointer argument, see if we can tell that it
438 // is impossible to alias the pointer we're checking. If not, we have to
439 // assume that the call could touch the pointer, even though it doesn't
440 // escape.
Nick Lewyckyc0514622013-07-07 10:15:16 +0000441 if (isNoAlias(AliasAnalysis::Location(*CI),
NAKAMURA Takumi335a7bc2014-10-28 11:53:30 +0000442 AliasAnalysis::Location(Object)))
Nick Lewyckyc0514622013-07-07 10:15:16 +0000443 continue;
444 if (CS.doesNotAccessMemory(ArgNo))
445 continue;
446 if (CS.onlyReadsMemory(ArgNo)) {
447 R = AliasAnalysis::Ref;
448 continue;
Chad Rosiera968caf2012-05-14 20:35:04 +0000449 }
Nick Lewyckyc0514622013-07-07 10:15:16 +0000450 return AliasAnalysis::ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000451 }
Nick Lewyckyc0514622013-07-07 10:15:16 +0000452 return R;
Chad Rosiera968caf2012-05-14 20:35:04 +0000453}
Eli Friedman5c918912011-09-26 20:15:28 +0000454
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000455// AliasAnalysis destructor: DO NOT move this to the header file for
456// AliasAnalysis or else clients of the AliasAnalysis class may not depend on
457// the AliasAnalysis.o file in the current .a file, causing alias analysis
458// support to not be included in the tool correctly!
459//
460AliasAnalysis::~AliasAnalysis() {}
461
Dan Gohman7a68b662008-05-30 00:02:02 +0000462/// InitializeAliasAnalysis - Subclasses must call this method to initialize the
Chris Lattner3a311832003-02-26 19:26:51 +0000463/// AliasAnalysis interface before any other methods are called.
464///
465void AliasAnalysis::InitializeAliasAnalysis(Pass *P) {
Rafael Espindola93512512014-02-25 17:30:31 +0000466 DataLayoutPass *DLP = P->getAnalysisIfAvailable<DataLayoutPass>();
Craig Topper9f008862014-04-15 04:59:12 +0000467 DL = DLP ? &DLP->getDataLayout() : nullptr;
Benjamin Kramer8bcc9712012-08-29 15:32:21 +0000468 TLI = P->getAnalysisIfAvailable<TargetLibraryInfo>();
Chris Lattner62c37002004-05-23 21:15:48 +0000469 AA = &P->getAnalysis<AliasAnalysis>();
Chris Lattner3a311832003-02-26 19:26:51 +0000470}
471
472// getAnalysisUsage - All alias analysis implementations should invoke this
Dan Gohman43d19d62009-07-25 00:48:42 +0000473// directly (using AliasAnalysis::getAnalysisUsage(AU)).
Chris Lattner3a311832003-02-26 19:26:51 +0000474void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner62c37002004-05-23 21:15:48 +0000475 AU.addRequired<AliasAnalysis>(); // All AA's chain
Chris Lattner3a311832003-02-26 19:26:51 +0000476}
477
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000478/// getTypeStoreSize - Return the DataLayout store size for the given type,
Dan Gohman43d19d62009-07-25 00:48:42 +0000479/// if known, or a conservative value otherwise.
480///
Chris Lattner229907c2011-07-18 04:54:35 +0000481uint64_t AliasAnalysis::getTypeStoreSize(Type *Ty) {
Rafael Espindola7c68beb2014-02-18 15:33:12 +0000482 return DL ? DL->getTypeStoreSize(Ty) : UnknownSize;
Dan Gohman43d19d62009-07-25 00:48:42 +0000483}
484
Chris Lattnerd922a842002-08-22 22:46:39 +0000485/// canBasicBlockModify - Return true if it is possible for execution of the
486/// specified basic block to modify the value pointed to by Ptr.
487///
Chris Lattner3a311832003-02-26 19:26:51 +0000488bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB,
Dan Gohman41f14cf2010-09-14 21:25:10 +0000489 const Location &Loc) {
490 return canInstructionRangeModify(BB.front(), BB.back(), Loc);
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000491}
492
Chris Lattnerd922a842002-08-22 22:46:39 +0000493/// canInstructionRangeModify - Return true if it is possible for the execution
494/// of the specified instructions to modify the value pointed to by Ptr. The
495/// instructions to consider are all of the instructions in the range of [I1,I2]
496/// INCLUSIVE. I1 and I2 must be in the same basic block.
497///
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000498bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1,
499 const Instruction &I2,
Dan Gohman41f14cf2010-09-14 21:25:10 +0000500 const Location &Loc) {
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000501 assert(I1.getParent() == I2.getParent() &&
502 "Instructions not in same basic block!");
Dan Gohman5442c712010-08-03 21:48:53 +0000503 BasicBlock::const_iterator I = &I1;
504 BasicBlock::const_iterator E = &I2;
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000505 ++E; // Convert from inclusive to exclusive range.
506
Chris Lattner3a311832003-02-26 19:26:51 +0000507 for (; I != E; ++I) // Check every instruction in range
Dan Gohman41f14cf2010-09-14 21:25:10 +0000508 if (getModRefInfo(I, Loc) & Mod)
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000509 return true;
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000510 return false;
511}
512
Dan Gohmanfb306c02009-02-03 01:28:32 +0000513/// isNoAliasCall - Return true if this pointer is returned by a noalias
514/// function.
515bool llvm::isNoAliasCall(const Value *V) {
516 if (isa<CallInst>(V) || isa<InvokeInst>(V))
Dan Gohman5442c712010-08-03 21:48:53 +0000517 return ImmutableCallSite(cast<Instruction>(V))
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000518 .paramHasAttr(0, Attribute::NoAlias);
Dan Gohmanfb306c02009-02-03 01:28:32 +0000519 return false;
520}
521
Michael Kupersteinf3e663a2013-05-28 08:17:48 +0000522/// isNoAliasArgument - Return true if this is an argument with the noalias
523/// attribute.
524bool llvm::isNoAliasArgument(const Value *V)
525{
526 if (const Argument *A = dyn_cast<Argument>(V))
527 return A->hasNoAliasAttr();
528 return false;
529}
530
Dan Gohmanfb306c02009-02-03 01:28:32 +0000531/// isIdentifiedObject - Return true if this pointer refers to a distinct and
532/// identifiable object. This returns true for:
Dan Gohman1f59f012009-08-27 17:52:56 +0000533/// Global Variables and Functions (but not Global Aliases)
Dan Gohmanfb306c02009-02-03 01:28:32 +0000534/// Allocas and Mallocs
Dan Gohman00ef9322010-07-07 14:27:09 +0000535/// ByVal and NoAlias Arguments
536/// NoAlias returns
Dan Gohmanfb306c02009-02-03 01:28:32 +0000537///
Dan Gohman00ef9322010-07-07 14:27:09 +0000538bool llvm::isIdentifiedObject(const Value *V) {
Dan Gohman0824aff2010-06-29 00:50:39 +0000539 if (isa<AllocaInst>(V))
Dan Gohman1f59f012009-08-27 17:52:56 +0000540 return true;
541 if (isa<GlobalValue>(V) && !isa<GlobalAlias>(V))
Dan Gohmanfb306c02009-02-03 01:28:32 +0000542 return true;
Dan Gohman00ef9322010-07-07 14:27:09 +0000543 if (isNoAliasCall(V))
544 return true;
545 if (const Argument *A = dyn_cast<Argument>(V))
546 return A->hasNoAliasAttr() || A->hasByValAttr();
Dan Gohmanfb306c02009-02-03 01:28:32 +0000547 return false;
548}
Hal Finkelc782aa52014-07-21 12:27:23 +0000549
550/// isIdentifiedFunctionLocal - Return true if V is umabigously identified
551/// at the function-level. Different IdentifiedFunctionLocals can't alias.
552/// Further, an IdentifiedFunctionLocal can not alias with any function
553/// arguments other than itself, which is not necessarily true for
554/// IdentifiedObjects.
555bool llvm::isIdentifiedFunctionLocal(const Value *V)
556{
557 return isa<AllocaInst>(V) || isNoAliasCall(V) || isNoAliasArgument(V);
558}