blob: ce46d530051711797f8dd1dabea48e6d0cb0c663 [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"
Chandler Carruth62d42152015-01-15 02:16:27 +000030#include "llvm/Analysis/TargetLibraryInfo.h"
Chad Rosiera968caf2012-05-14 20:35:04 +000031#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000032#include "llvm/IR/BasicBlock.h"
33#include "llvm/IR/DataLayout.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000034#include "llvm/IR/Dominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000035#include "llvm/IR/Function.h"
36#include "llvm/IR/Instructions.h"
37#include "llvm/IR/IntrinsicInst.h"
38#include "llvm/IR/LLVMContext.h"
39#include "llvm/IR/Type.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000040#include "llvm/Pass.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
Daniel Berlin8de312d2015-04-13 23:25:41 +000085AliasAnalysis::ModRefResult
86AliasAnalysis::getModRefInfo(Instruction *I, ImmutableCallSite Call) {
87 // We may have two calls
88 if (auto CS = ImmutableCallSite(I)) {
89 // Check if the two calls modify the same memory
90 return getModRefInfo(Call, CS);
91 } else {
92 // Otherwise, check if the call modifies or references the
93 // location this memory access defines. The best we can say
94 // is that if the call references what this instruction
95 // defines, it must be clobbered by this location.
Chandler Carruth70c61c12015-06-04 02:03:15 +000096 const AliasAnalysis::Location DefLoc = MemoryLocation::get(I);
Daniel Berlin8de312d2015-04-13 23:25:41 +000097 if (getModRefInfo(Call, DefLoc) != AliasAnalysis::NoModRef)
98 return AliasAnalysis::ModRef;
99 }
100 return AliasAnalysis::NoModRef;
101}
Owen Andersonb6e4ff02011-01-03 21:38:41 +0000102
Chris Lattner62c37002004-05-23 21:15:48 +0000103AliasAnalysis::ModRefResult
Dan Gohman5f1702e2010-08-06 01:25:49 +0000104AliasAnalysis::getModRefInfo(ImmutableCallSite CS,
Dan Gohman41f14cf2010-09-14 21:25:10 +0000105 const Location &Loc) {
Dan Gohman1033ce62010-10-25 16:28:57 +0000106 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
Dan Gohman5f1702e2010-08-06 01:25:49 +0000107
108 ModRefBehavior MRB = getModRefBehavior(CS);
109 if (MRB == DoesNotAccessMemory)
110 return NoModRef;
111
112 ModRefResult Mask = ModRef;
Dan Gohman5d06f892010-11-09 20:06:55 +0000113 if (onlyReadsMemory(MRB))
Dan Gohman5f1702e2010-08-06 01:25:49 +0000114 Mask = Ref;
Dan Gohman5d06f892010-11-09 20:06:55 +0000115
Dan Gohman066c1bb2010-11-10 18:17:28 +0000116 if (onlyAccessesArgPointees(MRB)) {
Dan Gohman5f1702e2010-08-06 01:25:49 +0000117 bool doesAlias = false;
Hal Finkel354e23b2014-07-17 01:28:25 +0000118 ModRefResult AllArgsMask = NoModRef;
Dan Gohman39b3a1e2011-04-27 18:39:03 +0000119 if (doesAccessArgPointees(MRB)) {
Dan Gohman066c1bb2010-11-10 18:17:28 +0000120 for (ImmutableCallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
Dan Gohman39b3a1e2011-04-27 18:39:03 +0000121 AI != AE; ++AI) {
122 const Value *Arg = *AI;
123 if (!Arg->getType()->isPointerTy())
124 continue;
Hal Finkel354e23b2014-07-17 01:28:25 +0000125 ModRefResult ArgMask;
126 Location CSLoc =
127 getArgLocation(CS, (unsigned) std::distance(CS.arg_begin(), AI),
128 ArgMask);
Dan Gohman39b3a1e2011-04-27 18:39:03 +0000129 if (!isNoAlias(CSLoc, Loc)) {
Dan Gohman066c1bb2010-11-10 18:17:28 +0000130 doesAlias = true;
Hal Finkel354e23b2014-07-17 01:28:25 +0000131 AllArgsMask = ModRefResult(AllArgsMask | ArgMask);
Dan Gohman066c1bb2010-11-10 18:17:28 +0000132 }
Dan Gohman39b3a1e2011-04-27 18:39:03 +0000133 }
134 }
Dan Gohman5f1702e2010-08-06 01:25:49 +0000135 if (!doesAlias)
136 return NoModRef;
Hal Finkel354e23b2014-07-17 01:28:25 +0000137 Mask = ModRefResult(Mask & AllArgsMask);
Dan Gohman5f1702e2010-08-06 01:25:49 +0000138 }
139
Dan Gohman41f14cf2010-09-14 21:25:10 +0000140 // If Loc is a constant memory location, the call definitely could not
Dan Gohman5f1702e2010-08-06 01:25:49 +0000141 // modify the memory location.
Dan Gohman41f14cf2010-09-14 21:25:10 +0000142 if ((Mask & Mod) && pointsToConstantMemory(Loc))
Dan Gohman5f1702e2010-08-06 01:25:49 +0000143 Mask = ModRefResult(Mask & ~Mod);
144
Dan Gohmanabaf2d82010-10-25 16:29:52 +0000145 // If this is the end of the chain, don't forward.
Dan Gohman5f1702e2010-08-06 01:25:49 +0000146 if (!AA) return Mask;
147
148 // Otherwise, fall back to the next AA in the chain. But we can merge
149 // in any mask we've managed to compute.
Dan Gohman41f14cf2010-09-14 21:25:10 +0000150 return ModRefResult(AA->getModRefInfo(CS, Loc) & Mask);
Dan Gohman5f1702e2010-08-06 01:25:49 +0000151}
152
153AliasAnalysis::ModRefResult
Dan Gohman5442c712010-08-03 21:48:53 +0000154AliasAnalysis::getModRefInfo(ImmutableCallSite CS1, ImmutableCallSite CS2) {
Dan Gohman1033ce62010-10-25 16:28:57 +0000155 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
Dan Gohman5f1702e2010-08-06 01:25:49 +0000156
157 // If CS1 or CS2 are readnone, they don't interact.
158 ModRefBehavior CS1B = getModRefBehavior(CS1);
159 if (CS1B == DoesNotAccessMemory) return NoModRef;
160
161 ModRefBehavior CS2B = getModRefBehavior(CS2);
162 if (CS2B == DoesNotAccessMemory) return NoModRef;
163
164 // If they both only read from memory, there is no dependence.
Dan Gohman5d06f892010-11-09 20:06:55 +0000165 if (onlyReadsMemory(CS1B) && onlyReadsMemory(CS2B))
Dan Gohman5f1702e2010-08-06 01:25:49 +0000166 return NoModRef;
167
168 AliasAnalysis::ModRefResult Mask = ModRef;
169
170 // If CS1 only reads memory, the only dependence on CS2 can be
171 // from CS1 reading memory written by CS2.
Dan Gohman5d06f892010-11-09 20:06:55 +0000172 if (onlyReadsMemory(CS1B))
Dan Gohman5f1702e2010-08-06 01:25:49 +0000173 Mask = ModRefResult(Mask & Ref);
174
175 // If CS2 only access memory through arguments, accumulate the mod/ref
176 // information from CS1's references to the memory referenced by
177 // CS2's arguments.
Dan Gohman066c1bb2010-11-10 18:17:28 +0000178 if (onlyAccessesArgPointees(CS2B)) {
Dan Gohman5f1702e2010-08-06 01:25:49 +0000179 AliasAnalysis::ModRefResult R = NoModRef;
Dan Gohman39b3a1e2011-04-27 18:39:03 +0000180 if (doesAccessArgPointees(CS2B)) {
Dan Gohman066c1bb2010-11-10 18:17:28 +0000181 for (ImmutableCallSite::arg_iterator
182 I = CS2.arg_begin(), E = CS2.arg_end(); I != E; ++I) {
Dan Gohman39b3a1e2011-04-27 18:39:03 +0000183 const Value *Arg = *I;
184 if (!Arg->getType()->isPointerTy())
185 continue;
Hal Finkel354e23b2014-07-17 01:28:25 +0000186 ModRefResult ArgMask;
187 Location CS2Loc =
188 getArgLocation(CS2, (unsigned) std::distance(CS2.arg_begin(), I),
189 ArgMask);
190 // ArgMask indicates what CS2 might do to CS2Loc, and the dependence of
191 // CS1 on that location is the inverse.
192 if (ArgMask == Mod)
193 ArgMask = ModRef;
194 else if (ArgMask == Ref)
195 ArgMask = Mod;
196
197 R = ModRefResult((R | (getModRefInfo(CS1, CS2Loc) & ArgMask)) & Mask);
Dan Gohman066c1bb2010-11-10 18:17:28 +0000198 if (R == Mask)
199 break;
200 }
Dan Gohman39b3a1e2011-04-27 18:39:03 +0000201 }
Dan Gohman5f1702e2010-08-06 01:25:49 +0000202 return R;
203 }
204
205 // If CS1 only accesses memory through arguments, check if CS2 references
206 // any of the memory referenced by CS1's arguments. If not, return NoModRef.
Dan Gohman066c1bb2010-11-10 18:17:28 +0000207 if (onlyAccessesArgPointees(CS1B)) {
Dan Gohman5f1702e2010-08-06 01:25:49 +0000208 AliasAnalysis::ModRefResult R = NoModRef;
Dan Gohman39b3a1e2011-04-27 18:39:03 +0000209 if (doesAccessArgPointees(CS1B)) {
Dan Gohman066c1bb2010-11-10 18:17:28 +0000210 for (ImmutableCallSite::arg_iterator
Dan Gohman39b3a1e2011-04-27 18:39:03 +0000211 I = CS1.arg_begin(), E = CS1.arg_end(); I != E; ++I) {
212 const Value *Arg = *I;
213 if (!Arg->getType()->isPointerTy())
214 continue;
Hal Finkel354e23b2014-07-17 01:28:25 +0000215 ModRefResult ArgMask;
NAKAMURA Takumid0e13af2014-10-28 11:54:52 +0000216 Location CS1Loc = getArgLocation(
217 CS1, (unsigned)std::distance(CS1.arg_begin(), I), ArgMask);
NAKAMURA Takumi335a7bc2014-10-28 11:53:30 +0000218 // ArgMask indicates what CS1 might do to CS1Loc; if CS1 might Mod
219 // CS1Loc, then we care about either a Mod or a Ref by CS2. If CS1
220 // might Ref, then we care only about a Mod by CS2.
Hal Finkel354e23b2014-07-17 01:28:25 +0000221 ModRefResult ArgR = getModRefInfo(CS2, CS1Loc);
222 if (((ArgMask & Mod) != NoModRef && (ArgR & ModRef) != NoModRef) ||
223 ((ArgMask & Ref) != NoModRef && (ArgR & Mod) != NoModRef))
224 R = ModRefResult((R | ArgMask) & Mask);
225
226 if (R == Mask)
Dan Gohman066c1bb2010-11-10 18:17:28 +0000227 break;
Dan Gohman39b3a1e2011-04-27 18:39:03 +0000228 }
229 }
Hal Finkel354e23b2014-07-17 01:28:25 +0000230 return R;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000231 }
232
Dan Gohmanabaf2d82010-10-25 16:29:52 +0000233 // If this is the end of the chain, don't forward.
Dan Gohman5f1702e2010-08-06 01:25:49 +0000234 if (!AA) return Mask;
235
236 // Otherwise, fall back to the next AA in the chain. But we can merge
237 // in any mask we've managed to compute.
238 return ModRefResult(AA->getModRefInfo(CS1, CS2) & Mask);
239}
240
241AliasAnalysis::ModRefBehavior
242AliasAnalysis::getModRefBehavior(ImmutableCallSite CS) {
Dan Gohman1033ce62010-10-25 16:28:57 +0000243 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
Dan Gohman5f1702e2010-08-06 01:25:49 +0000244
245 ModRefBehavior Min = UnknownModRefBehavior;
246
247 // Call back into the alias analysis with the other form of getModRefBehavior
248 // to see if it can give a better response.
249 if (const Function *F = CS.getCalledFunction())
250 Min = getModRefBehavior(F);
251
Dan Gohmanabaf2d82010-10-25 16:29:52 +0000252 // If this is the end of the chain, don't forward.
Dan Gohman5f1702e2010-08-06 01:25:49 +0000253 if (!AA) return Min;
254
255 // Otherwise, fall back to the next AA in the chain. But we can merge
256 // in any result we've managed to compute.
Dan Gohman2694e142010-11-10 01:02:18 +0000257 return ModRefBehavior(AA->getModRefBehavior(CS) & Min);
Dan Gohman5f1702e2010-08-06 01:25:49 +0000258}
259
260AliasAnalysis::ModRefBehavior
261AliasAnalysis::getModRefBehavior(const Function *F) {
Chris Lattner62c37002004-05-23 21:15:48 +0000262 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
Dan Gohman5f1702e2010-08-06 01:25:49 +0000263 return AA->getModRefBehavior(F);
Chris Lattner62c37002004-05-23 21:15:48 +0000264}
265
Chris Lattner62c37002004-05-23 21:15:48 +0000266//===----------------------------------------------------------------------===//
267// AliasAnalysis non-virtual helper method implementation
268//===----------------------------------------------------------------------===//
269
Chris Lattner3a311832003-02-26 19:26:51 +0000270AliasAnalysis::ModRefResult
Dan Gohman41f14cf2010-09-14 21:25:10 +0000271AliasAnalysis::getModRefInfo(const LoadInst *L, const Location &Loc) {
Eli Friedman5494ada2011-08-15 20:54:19 +0000272 // Be conservative in the face of volatile/atomic.
273 if (!L->isUnordered())
Dan Gohman6b4671b2010-08-06 18:11:28 +0000274 return ModRef;
275
Dan Gohman52f9d7d2010-08-03 17:27:43 +0000276 // If the load address doesn't alias the given address, it doesn't read
277 // or write the specified memory.
Chandler Carruth70c61c12015-06-04 02:03:15 +0000278 if (Loc.Ptr && !alias(MemoryLocation::get(L), Loc))
Dan Gohman52f9d7d2010-08-03 17:27:43 +0000279 return NoModRef;
280
Dan Gohman52f9d7d2010-08-03 17:27:43 +0000281 // Otherwise, a load just reads.
282 return Ref;
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000283}
284
Chris Lattner3a311832003-02-26 19:26:51 +0000285AliasAnalysis::ModRefResult
Dan Gohman41f14cf2010-09-14 21:25:10 +0000286AliasAnalysis::getModRefInfo(const StoreInst *S, const Location &Loc) {
Eli Friedman5494ada2011-08-15 20:54:19 +0000287 // Be conservative in the face of volatile/atomic.
288 if (!S->isUnordered())
Dan Gohman6b4671b2010-08-06 18:11:28 +0000289 return ModRef;
290
Daniel Berlinb2d22762015-04-13 23:05:45 +0000291 if (Loc.Ptr) {
292 // If the store address cannot alias the pointer in question, then the
293 // specified memory cannot be modified by the store.
Chandler Carruth70c61c12015-06-04 02:03:15 +0000294 if (!alias(MemoryLocation::get(S), Loc))
Daniel Berlinb2d22762015-04-13 23:05:45 +0000295 return NoModRef;
Chris Lattner96055762004-01-30 22:16:42 +0000296
Daniel Berlinb2d22762015-04-13 23:05:45 +0000297 // If the pointer is a pointer to constant memory, then it could not have
298 // been modified by this store.
299 if (pointsToConstantMemory(Loc))
300 return NoModRef;
301
302 }
Dan Gohman52f9d7d2010-08-03 17:27:43 +0000303
304 // Otherwise, a store just writes.
305 return Mod;
Chris Lattner3a311832003-02-26 19:26:51 +0000306}
307
Dan Gohmane68958f2010-08-06 18:24:38 +0000308AliasAnalysis::ModRefResult
Dan Gohman41f14cf2010-09-14 21:25:10 +0000309AliasAnalysis::getModRefInfo(const VAArgInst *V, const Location &Loc) {
Dan Gohmane68958f2010-08-06 18:24:38 +0000310
Daniel Berlinec1de3f2015-04-28 19:19:14 +0000311 if (Loc.Ptr) {
312 // If the va_arg address cannot alias the pointer in question, then the
313 // specified memory cannot be accessed by the va_arg.
Chandler Carruth70c61c12015-06-04 02:03:15 +0000314 if (!alias(MemoryLocation::get(V), Loc))
Daniel Berlinec1de3f2015-04-28 19:19:14 +0000315 return NoModRef;
316
317 // If the pointer is a pointer to constant memory, then it could not have
318 // been modified by this va_arg.
319 if (pointsToConstantMemory(Loc))
320 return NoModRef;
321 }
Dan Gohmane68958f2010-08-06 18:24:38 +0000322
323 // Otherwise, a va_arg reads and writes.
324 return ModRef;
325}
326
Eli Friedman5c918912011-09-26 20:15:28 +0000327AliasAnalysis::ModRefResult
328AliasAnalysis::getModRefInfo(const AtomicCmpXchgInst *CX, const Location &Loc) {
329 // Acquire/Release cmpxchg has properties that matter for arbitrary addresses.
Tim Northovere94a5182014-03-11 10:48:52 +0000330 if (CX->getSuccessOrdering() > Monotonic)
Eli Friedman5c918912011-09-26 20:15:28 +0000331 return ModRef;
332
333 // If the cmpxchg address does not alias the location, it does not access it.
Chandler Carruth70c61c12015-06-04 02:03:15 +0000334 if (Loc.Ptr && !alias(MemoryLocation::get(CX), Loc))
Eli Friedman5c918912011-09-26 20:15:28 +0000335 return NoModRef;
336
337 return ModRef;
338}
339
340AliasAnalysis::ModRefResult
341AliasAnalysis::getModRefInfo(const AtomicRMWInst *RMW, const Location &Loc) {
342 // Acquire/Release atomicrmw has properties that matter for arbitrary addresses.
343 if (RMW->getOrdering() > Monotonic)
344 return ModRef;
345
346 // If the atomicrmw address does not alias the location, it does not access it.
Chandler Carruth70c61c12015-06-04 02:03:15 +0000347 if (Loc.Ptr && !alias(MemoryLocation::get(RMW), Loc))
Eli Friedman5c918912011-09-26 20:15:28 +0000348 return NoModRef;
349
350 return ModRef;
351}
352
Chad Rosiera968caf2012-05-14 20:35:04 +0000353// FIXME: this is really just shoring-up a deficiency in alias analysis.
354// BasicAA isn't willing to spend linear time determining whether an alloca
355// was captured before or after this particular call, while we are. However,
356// with a smarter AA in place, this test is just wasting compile time.
357AliasAnalysis::ModRefResult
358AliasAnalysis::callCapturesBefore(const Instruction *I,
359 const AliasAnalysis::Location &MemLoc,
360 DominatorTree *DT) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000361 if (!DT)
362 return AliasAnalysis::ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000363
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000364 const Value *Object = GetUnderlyingObject(MemLoc.Ptr, *DL);
Chad Rosiera968caf2012-05-14 20:35:04 +0000365 if (!isIdentifiedObject(Object) || isa<GlobalValue>(Object) ||
366 isa<Constant>(Object))
367 return AliasAnalysis::ModRef;
368
369 ImmutableCallSite CS(I);
370 if (!CS.getInstruction() || CS.getInstruction() == Object)
371 return AliasAnalysis::ModRef;
372
Hal Finkelb0356212014-07-21 13:15:48 +0000373 if (llvm::PointerMayBeCapturedBefore(Object, /* ReturnCaptures */ true,
Hal Finkeld32803b2014-07-21 21:30:22 +0000374 /* StoreCaptures */ true, I, DT,
375 /* include Object */ true))
Chad Rosiera968caf2012-05-14 20:35:04 +0000376 return AliasAnalysis::ModRef;
377
378 unsigned ArgNo = 0;
Nick Lewyckyc0514622013-07-07 10:15:16 +0000379 AliasAnalysis::ModRefResult R = AliasAnalysis::NoModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000380 for (ImmutableCallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
381 CI != CE; ++CI, ++ArgNo) {
382 // Only look at the no-capture or byval pointer arguments. If this
383 // pointer were passed to arguments that were neither of these, then it
384 // couldn't be no-capture.
385 if (!(*CI)->getType()->isPointerTy() ||
386 (!CS.doesNotCapture(ArgNo) && !CS.isByValArgument(ArgNo)))
387 continue;
388
389 // If this is a no-capture pointer argument, see if we can tell that it
390 // is impossible to alias the pointer we're checking. If not, we have to
391 // assume that the call could touch the pointer, even though it doesn't
392 // escape.
Nick Lewyckyc0514622013-07-07 10:15:16 +0000393 if (isNoAlias(AliasAnalysis::Location(*CI),
NAKAMURA Takumi335a7bc2014-10-28 11:53:30 +0000394 AliasAnalysis::Location(Object)))
Nick Lewyckyc0514622013-07-07 10:15:16 +0000395 continue;
396 if (CS.doesNotAccessMemory(ArgNo))
397 continue;
398 if (CS.onlyReadsMemory(ArgNo)) {
399 R = AliasAnalysis::Ref;
400 continue;
Chad Rosiera968caf2012-05-14 20:35:04 +0000401 }
Nick Lewyckyc0514622013-07-07 10:15:16 +0000402 return AliasAnalysis::ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000403 }
Nick Lewyckyc0514622013-07-07 10:15:16 +0000404 return R;
Chad Rosiera968caf2012-05-14 20:35:04 +0000405}
Eli Friedman5c918912011-09-26 20:15:28 +0000406
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000407// AliasAnalysis destructor: DO NOT move this to the header file for
408// AliasAnalysis or else clients of the AliasAnalysis class may not depend on
409// the AliasAnalysis.o file in the current .a file, causing alias analysis
410// support to not be included in the tool correctly!
411//
412AliasAnalysis::~AliasAnalysis() {}
413
Dan Gohman7a68b662008-05-30 00:02:02 +0000414/// InitializeAliasAnalysis - Subclasses must call this method to initialize the
Chris Lattner3a311832003-02-26 19:26:51 +0000415/// AliasAnalysis interface before any other methods are called.
416///
Mehdi Amini46a43552015-03-04 18:43:29 +0000417void AliasAnalysis::InitializeAliasAnalysis(Pass *P, const DataLayout *NewDL) {
418 DL = NewDL;
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000419 auto *TLIP = P->getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
420 TLI = TLIP ? &TLIP->getTLI() : nullptr;
Chris Lattner62c37002004-05-23 21:15:48 +0000421 AA = &P->getAnalysis<AliasAnalysis>();
Chris Lattner3a311832003-02-26 19:26:51 +0000422}
423
424// getAnalysisUsage - All alias analysis implementations should invoke this
Dan Gohman43d19d62009-07-25 00:48:42 +0000425// directly (using AliasAnalysis::getAnalysisUsage(AU)).
Chris Lattner3a311832003-02-26 19:26:51 +0000426void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner62c37002004-05-23 21:15:48 +0000427 AU.addRequired<AliasAnalysis>(); // All AA's chain
Chris Lattner3a311832003-02-26 19:26:51 +0000428}
429
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000430/// getTypeStoreSize - Return the DataLayout store size for the given type,
Dan Gohman43d19d62009-07-25 00:48:42 +0000431/// if known, or a conservative value otherwise.
432///
Chris Lattner229907c2011-07-18 04:54:35 +0000433uint64_t AliasAnalysis::getTypeStoreSize(Type *Ty) {
Rafael Espindola7c68beb2014-02-18 15:33:12 +0000434 return DL ? DL->getTypeStoreSize(Ty) : UnknownSize;
Dan Gohman43d19d62009-07-25 00:48:42 +0000435}
436
Chris Lattnerd922a842002-08-22 22:46:39 +0000437/// canBasicBlockModify - Return true if it is possible for execution of the
Elena Demikhovskya5599bf2014-12-15 14:09:53 +0000438/// specified basic block to modify the location Loc.
Chris Lattnerd922a842002-08-22 22:46:39 +0000439///
Chris Lattner3a311832003-02-26 19:26:51 +0000440bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB,
Dan Gohman41f14cf2010-09-14 21:25:10 +0000441 const Location &Loc) {
Elena Demikhovskya5599bf2014-12-15 14:09:53 +0000442 return canInstructionRangeModRef(BB.front(), BB.back(), Loc, Mod);
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000443}
444
Elena Demikhovskya5599bf2014-12-15 14:09:53 +0000445/// canInstructionRangeModRef - Return true if it is possible for the
446/// execution of the specified instructions to mod\ref (according to the
447/// mode) the location Loc. The instructions to consider are all
448/// of the instructions in the range of [I1,I2] INCLUSIVE.
Teresa Johnsonbbcf75e2015-05-13 15:04:14 +0000449/// I1 and I2 must be in the same basic block.
Elena Demikhovskya5599bf2014-12-15 14:09:53 +0000450bool AliasAnalysis::canInstructionRangeModRef(const Instruction &I1,
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000451 const Instruction &I2,
Elena Demikhovskya5599bf2014-12-15 14:09:53 +0000452 const Location &Loc,
453 const ModRefResult Mode) {
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000454 assert(I1.getParent() == I2.getParent() &&
455 "Instructions not in same basic block!");
Dan Gohman5442c712010-08-03 21:48:53 +0000456 BasicBlock::const_iterator I = &I1;
457 BasicBlock::const_iterator E = &I2;
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000458 ++E; // Convert from inclusive to exclusive range.
459
Chris Lattner3a311832003-02-26 19:26:51 +0000460 for (; I != E; ++I) // Check every instruction in range
Elena Demikhovskya5599bf2014-12-15 14:09:53 +0000461 if (getModRefInfo(I, Loc) & Mode)
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000462 return true;
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000463 return false;
464}
465
Dan Gohmanfb306c02009-02-03 01:28:32 +0000466/// isNoAliasCall - Return true if this pointer is returned by a noalias
467/// function.
468bool llvm::isNoAliasCall(const Value *V) {
469 if (isa<CallInst>(V) || isa<InvokeInst>(V))
Dan Gohman5442c712010-08-03 21:48:53 +0000470 return ImmutableCallSite(cast<Instruction>(V))
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000471 .paramHasAttr(0, Attribute::NoAlias);
Dan Gohmanfb306c02009-02-03 01:28:32 +0000472 return false;
473}
474
Michael Kupersteinf3e663a2013-05-28 08:17:48 +0000475/// isNoAliasArgument - Return true if this is an argument with the noalias
476/// attribute.
477bool llvm::isNoAliasArgument(const Value *V)
478{
479 if (const Argument *A = dyn_cast<Argument>(V))
480 return A->hasNoAliasAttr();
481 return false;
482}
483
Dan Gohmanfb306c02009-02-03 01:28:32 +0000484/// isIdentifiedObject - Return true if this pointer refers to a distinct and
485/// identifiable object. This returns true for:
Dan Gohman1f59f012009-08-27 17:52:56 +0000486/// Global Variables and Functions (but not Global Aliases)
Dan Gohmanfb306c02009-02-03 01:28:32 +0000487/// Allocas and Mallocs
Dan Gohman00ef9322010-07-07 14:27:09 +0000488/// ByVal and NoAlias Arguments
489/// NoAlias returns
Dan Gohmanfb306c02009-02-03 01:28:32 +0000490///
Dan Gohman00ef9322010-07-07 14:27:09 +0000491bool llvm::isIdentifiedObject(const Value *V) {
Dan Gohman0824aff2010-06-29 00:50:39 +0000492 if (isa<AllocaInst>(V))
Dan Gohman1f59f012009-08-27 17:52:56 +0000493 return true;
494 if (isa<GlobalValue>(V) && !isa<GlobalAlias>(V))
Dan Gohmanfb306c02009-02-03 01:28:32 +0000495 return true;
Dan Gohman00ef9322010-07-07 14:27:09 +0000496 if (isNoAliasCall(V))
497 return true;
498 if (const Argument *A = dyn_cast<Argument>(V))
499 return A->hasNoAliasAttr() || A->hasByValAttr();
Dan Gohmanfb306c02009-02-03 01:28:32 +0000500 return false;
501}
Hal Finkelc782aa52014-07-21 12:27:23 +0000502
503/// isIdentifiedFunctionLocal - Return true if V is umabigously identified
504/// at the function-level. Different IdentifiedFunctionLocals can't alias.
505/// Further, an IdentifiedFunctionLocal can not alias with any function
506/// arguments other than itself, which is not necessarily true for
507/// IdentifiedObjects.
508bool llvm::isIdentifiedFunctionLocal(const Value *V)
509{
510 return isa<AllocaInst>(V) || isNoAliasCall(V) || isNoAliasArgument(V);
511}