blob: 453bc3dbe674e322521170d15dfa8f07e6f85851 [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"
Chandler Carruth7b560d42015-09-09 17:55:00 +000028#include "llvm/Analysis/BasicAliasAnalysis.h"
Nick Lewycky0b682452013-07-27 01:24:00 +000029#include "llvm/Analysis/CFG.h"
Chandler Carruth7b560d42015-09-09 17:55:00 +000030#include "llvm/Analysis/CFLAliasAnalysis.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000031#include "llvm/Analysis/CaptureTracking.h"
Chandler Carruth7b560d42015-09-09 17:55:00 +000032#include "llvm/Analysis/GlobalsModRef.h"
33#include "llvm/Analysis/ObjCARCAliasAnalysis.h"
34#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
35#include "llvm/Analysis/ScopedNoAliasAA.h"
Chandler Carruth62d42152015-01-15 02:16:27 +000036#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruth7b560d42015-09-09 17:55:00 +000037#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
Chad Rosiera968caf2012-05-14 20:35:04 +000038#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000039#include "llvm/IR/BasicBlock.h"
40#include "llvm/IR/DataLayout.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000041#include "llvm/IR/Dominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000042#include "llvm/IR/Function.h"
43#include "llvm/IR/Instructions.h"
44#include "llvm/IR/IntrinsicInst.h"
45#include "llvm/IR/LLVMContext.h"
46#include "llvm/IR/Type.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000047#include "llvm/Pass.h"
Chris Lattnera67dbd02004-03-15 04:07:29 +000048using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000049
Chandler Carruth7b560d42015-09-09 17:55:00 +000050/// Allow disabling BasicAA from the AA results. This is particularly useful
51/// when testing to isolate a single AA implementation.
52static cl::opt<bool> DisableBasicAA("disable-basicaa", cl::Hidden,
53 cl::init(false));
54
Chandler Carruth12884f72016-03-02 15:56:53 +000055AAResults::AAResults(AAResults &&Arg) : TLI(Arg.TLI), AAs(std::move(Arg.AAs)) {
Chandler Carruth7b560d42015-09-09 17:55:00 +000056 for (auto &AA : AAs)
57 AA->setAAResults(this);
58}
59
Chandler Carruth7b560d42015-09-09 17:55:00 +000060AAResults::~AAResults() {
61// FIXME; It would be nice to at least clear out the pointers back to this
62// aggregation here, but we end up with non-nesting lifetimes in the legacy
63// pass manager that prevent this from working. In the legacy pass manager
64// we'll end up with dangling references here in some cases.
65#if 0
66 for (auto &AA : AAs)
67 AA->setAAResults(nullptr);
68#endif
69}
Chris Lattner7d58f8d2002-08-22 18:25:32 +000070
Chris Lattner62c37002004-05-23 21:15:48 +000071//===----------------------------------------------------------------------===//
72// Default chaining methods
73//===----------------------------------------------------------------------===//
74
Chandler Carruth7b560d42015-09-09 17:55:00 +000075AliasResult AAResults::alias(const MemoryLocation &LocA,
76 const MemoryLocation &LocB) {
77 for (const auto &AA : AAs) {
78 auto Result = AA->alias(LocA, LocB);
79 if (Result != MayAlias)
80 return Result;
81 }
82 return MayAlias;
Chris Lattner62c37002004-05-23 21:15:48 +000083}
84
Chandler Carruth7b560d42015-09-09 17:55:00 +000085bool AAResults::pointsToConstantMemory(const MemoryLocation &Loc,
86 bool OrLocal) {
87 for (const auto &AA : AAs)
88 if (AA->pointsToConstantMemory(Loc, OrLocal))
89 return true;
90
91 return false;
Chris Lattner62c37002004-05-23 21:15:48 +000092}
93
Chandler Carruth7b560d42015-09-09 17:55:00 +000094ModRefInfo AAResults::getArgModRefInfo(ImmutableCallSite CS, unsigned ArgIdx) {
95 ModRefInfo Result = MRI_ModRef;
96
97 for (const auto &AA : AAs) {
98 Result = ModRefInfo(Result & AA->getArgModRefInfo(CS, ArgIdx));
99
100 // Early-exit the moment we reach the bottom of the lattice.
101 if (Result == MRI_NoModRef)
102 return Result;
103 }
104
105 return Result;
Hal Finkel354e23b2014-07-17 01:28:25 +0000106}
107
Chandler Carruth7b560d42015-09-09 17:55:00 +0000108ModRefInfo AAResults::getModRefInfo(Instruction *I, ImmutableCallSite Call) {
Daniel Berlin8de312d2015-04-13 23:25:41 +0000109 // We may have two calls
110 if (auto CS = ImmutableCallSite(I)) {
111 // Check if the two calls modify the same memory
112 return getModRefInfo(Call, CS);
113 } else {
114 // Otherwise, check if the call modifies or references the
115 // location this memory access defines. The best we can say
116 // is that if the call references what this instruction
117 // defines, it must be clobbered by this location.
Chandler Carruthac80dc72015-06-17 07:18:54 +0000118 const MemoryLocation DefLoc = MemoryLocation::get(I);
Chandler Carruth194f59c2015-07-22 23:15:57 +0000119 if (getModRefInfo(Call, DefLoc) != MRI_NoModRef)
120 return MRI_ModRef;
Daniel Berlin8de312d2015-04-13 23:25:41 +0000121 }
Chandler Carruth194f59c2015-07-22 23:15:57 +0000122 return MRI_NoModRef;
Daniel Berlin8de312d2015-04-13 23:25:41 +0000123}
Owen Andersonb6e4ff02011-01-03 21:38:41 +0000124
Chandler Carruth7b560d42015-09-09 17:55:00 +0000125ModRefInfo AAResults::getModRefInfo(ImmutableCallSite CS,
126 const MemoryLocation &Loc) {
127 ModRefInfo Result = MRI_ModRef;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000128
Chandler Carruth7b560d42015-09-09 17:55:00 +0000129 for (const auto &AA : AAs) {
130 Result = ModRefInfo(Result & AA->getModRefInfo(CS, Loc));
Dan Gohman5f1702e2010-08-06 01:25:49 +0000131
Chandler Carruth7b560d42015-09-09 17:55:00 +0000132 // Early-exit the moment we reach the bottom of the lattice.
133 if (Result == MRI_NoModRef)
134 return Result;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000135 }
136
Chandler Carruth12884f72016-03-02 15:56:53 +0000137 // Try to refine the mod-ref info further using other API entry points to the
138 // aggregate set of AA results.
139 auto MRB = getModRefBehavior(CS);
140 if (MRB == FMRB_DoesNotAccessMemory)
141 return MRI_NoModRef;
142
143 if (onlyReadsMemory(MRB))
144 Result = ModRefInfo(Result & MRI_Ref);
145
146 if (onlyAccessesArgPointees(MRB)) {
147 bool DoesAlias = false;
148 ModRefInfo AllArgsMask = MRI_NoModRef;
149 if (doesAccessArgPointees(MRB)) {
150 for (auto AI = CS.arg_begin(), AE = CS.arg_end(); AI != AE; ++AI) {
151 const Value *Arg = *AI;
152 if (!Arg->getType()->isPointerTy())
153 continue;
154 unsigned ArgIdx = std::distance(CS.arg_begin(), AI);
155 MemoryLocation ArgLoc = MemoryLocation::getForArgument(CS, ArgIdx, TLI);
156 AliasResult ArgAlias = alias(ArgLoc, Loc);
157 if (ArgAlias != NoAlias) {
158 ModRefInfo ArgMask = getArgModRefInfo(CS, ArgIdx);
159 DoesAlias = true;
160 AllArgsMask = ModRefInfo(AllArgsMask | ArgMask);
161 }
162 }
163 }
164 if (!DoesAlias)
165 return MRI_NoModRef;
166 Result = ModRefInfo(Result & AllArgsMask);
167 }
168
169 // If Loc is a constant memory location, the call definitely could not
170 // modify the memory location.
171 if ((Result & MRI_Mod) &&
172 pointsToConstantMemory(Loc, /*OrLocal*/ false))
173 Result = ModRefInfo(Result & ~MRI_Mod);
174
Chandler Carruth7b560d42015-09-09 17:55:00 +0000175 return Result;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000176}
177
Chandler Carruth7b560d42015-09-09 17:55:00 +0000178ModRefInfo AAResults::getModRefInfo(ImmutableCallSite CS1,
179 ImmutableCallSite CS2) {
180 ModRefInfo Result = MRI_ModRef;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000181
Chandler Carruth7b560d42015-09-09 17:55:00 +0000182 for (const auto &AA : AAs) {
183 Result = ModRefInfo(Result & AA->getModRefInfo(CS1, CS2));
Dan Gohman5f1702e2010-08-06 01:25:49 +0000184
Chandler Carruth7b560d42015-09-09 17:55:00 +0000185 // Early-exit the moment we reach the bottom of the lattice.
186 if (Result == MRI_NoModRef)
187 return Result;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000188 }
189
Chandler Carruth12884f72016-03-02 15:56:53 +0000190 // Try to refine the mod-ref info further using other API entry points to the
191 // aggregate set of AA results.
192
193 // If CS1 or CS2 are readnone, they don't interact.
194 auto CS1B = getModRefBehavior(CS1);
195 if (CS1B == FMRB_DoesNotAccessMemory)
196 return MRI_NoModRef;
197
198 auto CS2B = getModRefBehavior(CS2);
199 if (CS2B == FMRB_DoesNotAccessMemory)
200 return MRI_NoModRef;
201
202 // If they both only read from memory, there is no dependence.
203 if (onlyReadsMemory(CS1B) && onlyReadsMemory(CS2B))
204 return MRI_NoModRef;
205
206 // If CS1 only reads memory, the only dependence on CS2 can be
207 // from CS1 reading memory written by CS2.
208 if (onlyReadsMemory(CS1B))
209 Result = ModRefInfo(Result & MRI_Ref);
210
211 // If CS2 only access memory through arguments, accumulate the mod/ref
212 // information from CS1's references to the memory referenced by
213 // CS2's arguments.
214 if (onlyAccessesArgPointees(CS2B)) {
215 ModRefInfo R = MRI_NoModRef;
216 if (doesAccessArgPointees(CS2B)) {
217 for (auto I = CS2.arg_begin(), E = CS2.arg_end(); I != E; ++I) {
218 const Value *Arg = *I;
219 if (!Arg->getType()->isPointerTy())
220 continue;
221 unsigned CS2ArgIdx = std::distance(CS2.arg_begin(), I);
222 auto CS2ArgLoc = MemoryLocation::getForArgument(CS2, CS2ArgIdx, TLI);
223
224 // ArgMask indicates what CS2 might do to CS2ArgLoc, and the dependence
225 // of CS1 on that location is the inverse.
226 ModRefInfo ArgMask = getArgModRefInfo(CS2, CS2ArgIdx);
227 if (ArgMask == MRI_Mod)
228 ArgMask = MRI_ModRef;
229 else if (ArgMask == MRI_Ref)
230 ArgMask = MRI_Mod;
231
232 ArgMask = ModRefInfo(ArgMask & getModRefInfo(CS1, CS2ArgLoc));
233
234 R = ModRefInfo((R | ArgMask) & Result);
235 if (R == Result)
236 break;
237 }
238 }
239 return R;
240 }
241
242 // If CS1 only accesses memory through arguments, check if CS2 references
243 // any of the memory referenced by CS1's arguments. If not, return NoModRef.
244 if (onlyAccessesArgPointees(CS1B)) {
245 ModRefInfo R = MRI_NoModRef;
246 if (doesAccessArgPointees(CS1B)) {
247 for (auto I = CS1.arg_begin(), E = CS1.arg_end(); I != E; ++I) {
248 const Value *Arg = *I;
249 if (!Arg->getType()->isPointerTy())
250 continue;
251 unsigned CS1ArgIdx = std::distance(CS1.arg_begin(), I);
252 auto CS1ArgLoc = MemoryLocation::getForArgument(CS1, CS1ArgIdx, TLI);
253
254 // ArgMask indicates what CS1 might do to CS1ArgLoc; if CS1 might Mod
255 // CS1ArgLoc, then we care about either a Mod or a Ref by CS2. If CS1
256 // might Ref, then we care only about a Mod by CS2.
257 ModRefInfo ArgMask = getArgModRefInfo(CS1, CS1ArgIdx);
258 ModRefInfo ArgR = getModRefInfo(CS2, CS1ArgLoc);
259 if (((ArgMask & MRI_Mod) != MRI_NoModRef &&
260 (ArgR & MRI_ModRef) != MRI_NoModRef) ||
261 ((ArgMask & MRI_Ref) != MRI_NoModRef &&
262 (ArgR & MRI_Mod) != MRI_NoModRef))
263 R = ModRefInfo((R | ArgMask) & Result);
264
265 if (R == Result)
266 break;
267 }
268 }
269 return R;
270 }
271
Chandler Carruth7b560d42015-09-09 17:55:00 +0000272 return Result;
273}
Chandler Carruthc41404a2015-06-17 07:12:40 +0000274
Chandler Carruth7b560d42015-09-09 17:55:00 +0000275FunctionModRefBehavior AAResults::getModRefBehavior(ImmutableCallSite CS) {
276 FunctionModRefBehavior Result = FMRB_UnknownModRefBehavior;
Hal Finkel354e23b2014-07-17 01:28:25 +0000277
Chandler Carruth7b560d42015-09-09 17:55:00 +0000278 for (const auto &AA : AAs) {
279 Result = FunctionModRefBehavior(Result & AA->getModRefBehavior(CS));
280
281 // Early-exit the moment we reach the bottom of the lattice.
282 if (Result == FMRB_DoesNotAccessMemory)
283 return Result;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000284 }
285
Chandler Carruth7b560d42015-09-09 17:55:00 +0000286 return Result;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000287}
288
Chandler Carruth7b560d42015-09-09 17:55:00 +0000289FunctionModRefBehavior AAResults::getModRefBehavior(const Function *F) {
290 FunctionModRefBehavior Result = FMRB_UnknownModRefBehavior;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000291
Chandler Carruth7b560d42015-09-09 17:55:00 +0000292 for (const auto &AA : AAs) {
293 Result = FunctionModRefBehavior(Result & AA->getModRefBehavior(F));
Dan Gohman5f1702e2010-08-06 01:25:49 +0000294
Chandler Carruth7b560d42015-09-09 17:55:00 +0000295 // Early-exit the moment we reach the bottom of the lattice.
296 if (Result == FMRB_DoesNotAccessMemory)
297 return Result;
298 }
Dan Gohman5f1702e2010-08-06 01:25:49 +0000299
Chandler Carruth7b560d42015-09-09 17:55:00 +0000300 return Result;
Chris Lattner62c37002004-05-23 21:15:48 +0000301}
302
Chris Lattner62c37002004-05-23 21:15:48 +0000303//===----------------------------------------------------------------------===//
Chandler Carruth7b560d42015-09-09 17:55:00 +0000304// Helper method implementation
Chris Lattner62c37002004-05-23 21:15:48 +0000305//===----------------------------------------------------------------------===//
306
Chandler Carruth7b560d42015-09-09 17:55:00 +0000307ModRefInfo AAResults::getModRefInfo(const LoadInst *L,
308 const MemoryLocation &Loc) {
Eli Friedman5494ada2011-08-15 20:54:19 +0000309 // Be conservative in the face of volatile/atomic.
310 if (!L->isUnordered())
Chandler Carruth194f59c2015-07-22 23:15:57 +0000311 return MRI_ModRef;
Dan Gohman6b4671b2010-08-06 18:11:28 +0000312
Dan Gohman52f9d7d2010-08-03 17:27:43 +0000313 // If the load address doesn't alias the given address, it doesn't read
314 // or write the specified memory.
Chandler Carruth70c61c12015-06-04 02:03:15 +0000315 if (Loc.Ptr && !alias(MemoryLocation::get(L), Loc))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000316 return MRI_NoModRef;
Dan Gohman52f9d7d2010-08-03 17:27:43 +0000317
Dan Gohman52f9d7d2010-08-03 17:27:43 +0000318 // Otherwise, a load just reads.
Chandler Carruth194f59c2015-07-22 23:15:57 +0000319 return MRI_Ref;
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000320}
321
Chandler Carruth7b560d42015-09-09 17:55:00 +0000322ModRefInfo AAResults::getModRefInfo(const StoreInst *S,
323 const MemoryLocation &Loc) {
Eli Friedman5494ada2011-08-15 20:54:19 +0000324 // Be conservative in the face of volatile/atomic.
325 if (!S->isUnordered())
Chandler Carruth194f59c2015-07-22 23:15:57 +0000326 return MRI_ModRef;
Dan Gohman6b4671b2010-08-06 18:11:28 +0000327
Daniel Berlinb2d22762015-04-13 23:05:45 +0000328 if (Loc.Ptr) {
329 // If the store address cannot alias the pointer in question, then the
330 // specified memory cannot be modified by the store.
Chandler Carruth70c61c12015-06-04 02:03:15 +0000331 if (!alias(MemoryLocation::get(S), Loc))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000332 return MRI_NoModRef;
Chris Lattner96055762004-01-30 22:16:42 +0000333
Daniel Berlinb2d22762015-04-13 23:05:45 +0000334 // If the pointer is a pointer to constant memory, then it could not have
335 // been modified by this store.
336 if (pointsToConstantMemory(Loc))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000337 return MRI_NoModRef;
Daniel Berlinb2d22762015-04-13 23:05:45 +0000338 }
Dan Gohman52f9d7d2010-08-03 17:27:43 +0000339
340 // Otherwise, a store just writes.
Chandler Carruth194f59c2015-07-22 23:15:57 +0000341 return MRI_Mod;
Chris Lattner3a311832003-02-26 19:26:51 +0000342}
343
Chandler Carruth7b560d42015-09-09 17:55:00 +0000344ModRefInfo AAResults::getModRefInfo(const VAArgInst *V,
345 const MemoryLocation &Loc) {
Dan Gohmane68958f2010-08-06 18:24:38 +0000346
Daniel Berlinec1de3f2015-04-28 19:19:14 +0000347 if (Loc.Ptr) {
348 // If the va_arg address cannot alias the pointer in question, then the
349 // specified memory cannot be accessed by the va_arg.
Chandler Carruth70c61c12015-06-04 02:03:15 +0000350 if (!alias(MemoryLocation::get(V), Loc))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000351 return MRI_NoModRef;
Daniel Berlinec1de3f2015-04-28 19:19:14 +0000352
353 // If the pointer is a pointer to constant memory, then it could not have
354 // been modified by this va_arg.
355 if (pointsToConstantMemory(Loc))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000356 return MRI_NoModRef;
Daniel Berlinec1de3f2015-04-28 19:19:14 +0000357 }
Dan Gohmane68958f2010-08-06 18:24:38 +0000358
359 // Otherwise, a va_arg reads and writes.
Chandler Carruth194f59c2015-07-22 23:15:57 +0000360 return MRI_ModRef;
Dan Gohmane68958f2010-08-06 18:24:38 +0000361}
362
David Majnemer6727c012015-11-17 08:15:14 +0000363ModRefInfo AAResults::getModRefInfo(const CatchPadInst *CatchPad,
364 const MemoryLocation &Loc) {
365 if (Loc.Ptr) {
366 // If the pointer is a pointer to constant memory,
367 // then it could not have been modified by this catchpad.
368 if (pointsToConstantMemory(Loc))
369 return MRI_NoModRef;
370 }
371
372 // Otherwise, a catchpad reads and writes.
373 return MRI_ModRef;
374}
375
376ModRefInfo AAResults::getModRefInfo(const CatchReturnInst *CatchRet,
377 const MemoryLocation &Loc) {
378 if (Loc.Ptr) {
379 // If the pointer is a pointer to constant memory,
380 // then it could not have been modified by this catchpad.
381 if (pointsToConstantMemory(Loc))
382 return MRI_NoModRef;
383 }
384
385 // Otherwise, a catchret reads and writes.
386 return MRI_ModRef;
387}
388
Chandler Carruth7b560d42015-09-09 17:55:00 +0000389ModRefInfo AAResults::getModRefInfo(const AtomicCmpXchgInst *CX,
390 const MemoryLocation &Loc) {
Eli Friedman5c918912011-09-26 20:15:28 +0000391 // Acquire/Release cmpxchg has properties that matter for arbitrary addresses.
Tim Northovere94a5182014-03-11 10:48:52 +0000392 if (CX->getSuccessOrdering() > Monotonic)
Chandler Carruth194f59c2015-07-22 23:15:57 +0000393 return MRI_ModRef;
Eli Friedman5c918912011-09-26 20:15:28 +0000394
395 // If the cmpxchg address does not alias the location, it does not access it.
Chandler Carruth70c61c12015-06-04 02:03:15 +0000396 if (Loc.Ptr && !alias(MemoryLocation::get(CX), Loc))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000397 return MRI_NoModRef;
Eli Friedman5c918912011-09-26 20:15:28 +0000398
Chandler Carruth194f59c2015-07-22 23:15:57 +0000399 return MRI_ModRef;
Eli Friedman5c918912011-09-26 20:15:28 +0000400}
401
Chandler Carruth7b560d42015-09-09 17:55:00 +0000402ModRefInfo AAResults::getModRefInfo(const AtomicRMWInst *RMW,
403 const MemoryLocation &Loc) {
Eli Friedman5c918912011-09-26 20:15:28 +0000404 // Acquire/Release atomicrmw has properties that matter for arbitrary addresses.
405 if (RMW->getOrdering() > Monotonic)
Chandler Carruth194f59c2015-07-22 23:15:57 +0000406 return MRI_ModRef;
Eli Friedman5c918912011-09-26 20:15:28 +0000407
408 // If the atomicrmw address does not alias the location, it does not access it.
Chandler Carruth70c61c12015-06-04 02:03:15 +0000409 if (Loc.Ptr && !alias(MemoryLocation::get(RMW), Loc))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000410 return MRI_NoModRef;
Eli Friedman5c918912011-09-26 20:15:28 +0000411
Chandler Carruth194f59c2015-07-22 23:15:57 +0000412 return MRI_ModRef;
Eli Friedman5c918912011-09-26 20:15:28 +0000413}
414
Bruno Cardoso Lopesdfc1d962015-07-31 14:31:35 +0000415/// \brief Return information about whether a particular call site modifies
416/// or reads the specified memory location \p MemLoc before instruction \p I
417/// in a BasicBlock. A ordered basic block \p OBB can be used to speed up
418/// instruction-ordering queries inside the BasicBlock containing \p I.
419/// FIXME: this is really just shoring-up a deficiency in alias analysis.
420/// BasicAA isn't willing to spend linear time determining whether an alloca
421/// was captured before or after this particular call, while we are. However,
422/// with a smarter AA in place, this test is just wasting compile time.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000423ModRefInfo AAResults::callCapturesBefore(const Instruction *I,
424 const MemoryLocation &MemLoc,
425 DominatorTree *DT,
426 OrderedBasicBlock *OBB) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000427 if (!DT)
Chandler Carruth194f59c2015-07-22 23:15:57 +0000428 return MRI_ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000429
Chandler Carruth7b560d42015-09-09 17:55:00 +0000430 const Value *Object =
431 GetUnderlyingObject(MemLoc.Ptr, I->getModule()->getDataLayout());
Chad Rosiera968caf2012-05-14 20:35:04 +0000432 if (!isIdentifiedObject(Object) || isa<GlobalValue>(Object) ||
433 isa<Constant>(Object))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000434 return MRI_ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000435
436 ImmutableCallSite CS(I);
437 if (!CS.getInstruction() || CS.getInstruction() == Object)
Chandler Carruth194f59c2015-07-22 23:15:57 +0000438 return MRI_ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000439
Hal Finkelb0356212014-07-21 13:15:48 +0000440 if (llvm::PointerMayBeCapturedBefore(Object, /* ReturnCaptures */ true,
Hal Finkeld32803b2014-07-21 21:30:22 +0000441 /* StoreCaptures */ true, I, DT,
Bruno Cardoso Lopesdfc1d962015-07-31 14:31:35 +0000442 /* include Object */ true,
443 /* OrderedBasicBlock */ OBB))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000444 return MRI_ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000445
446 unsigned ArgNo = 0;
Chandler Carruth194f59c2015-07-22 23:15:57 +0000447 ModRefInfo R = MRI_NoModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000448 for (ImmutableCallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
449 CI != CE; ++CI, ++ArgNo) {
450 // Only look at the no-capture or byval pointer arguments. If this
451 // pointer were passed to arguments that were neither of these, then it
452 // couldn't be no-capture.
453 if (!(*CI)->getType()->isPointerTy() ||
454 (!CS.doesNotCapture(ArgNo) && !CS.isByValArgument(ArgNo)))
455 continue;
456
457 // If this is a no-capture pointer argument, see if we can tell that it
458 // is impossible to alias the pointer we're checking. If not, we have to
459 // assume that the call could touch the pointer, even though it doesn't
460 // escape.
Chandler Carruthac80dc72015-06-17 07:18:54 +0000461 if (isNoAlias(MemoryLocation(*CI), MemoryLocation(Object)))
Nick Lewyckyc0514622013-07-07 10:15:16 +0000462 continue;
463 if (CS.doesNotAccessMemory(ArgNo))
464 continue;
465 if (CS.onlyReadsMemory(ArgNo)) {
Chandler Carruth194f59c2015-07-22 23:15:57 +0000466 R = MRI_Ref;
Nick Lewyckyc0514622013-07-07 10:15:16 +0000467 continue;
Chad Rosiera968caf2012-05-14 20:35:04 +0000468 }
Chandler Carruth194f59c2015-07-22 23:15:57 +0000469 return MRI_ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000470 }
Nick Lewyckyc0514622013-07-07 10:15:16 +0000471 return R;
Chad Rosiera968caf2012-05-14 20:35:04 +0000472}
Eli Friedman5c918912011-09-26 20:15:28 +0000473
Chris Lattnerd922a842002-08-22 22:46:39 +0000474/// canBasicBlockModify - Return true if it is possible for execution of the
Elena Demikhovskya5599bf2014-12-15 14:09:53 +0000475/// specified basic block to modify the location Loc.
Chris Lattnerd922a842002-08-22 22:46:39 +0000476///
Chandler Carruth7b560d42015-09-09 17:55:00 +0000477bool AAResults::canBasicBlockModify(const BasicBlock &BB,
478 const MemoryLocation &Loc) {
Chandler Carruth194f59c2015-07-22 23:15:57 +0000479 return canInstructionRangeModRef(BB.front(), BB.back(), Loc, MRI_Mod);
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000480}
481
Elena Demikhovskya5599bf2014-12-15 14:09:53 +0000482/// canInstructionRangeModRef - Return true if it is possible for the
483/// execution of the specified instructions to mod\ref (according to the
484/// mode) the location Loc. The instructions to consider are all
485/// of the instructions in the range of [I1,I2] INCLUSIVE.
Teresa Johnsonbbcf75e2015-05-13 15:04:14 +0000486/// I1 and I2 must be in the same basic block.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000487bool AAResults::canInstructionRangeModRef(const Instruction &I1,
488 const Instruction &I2,
489 const MemoryLocation &Loc,
490 const ModRefInfo Mode) {
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000491 assert(I1.getParent() == I2.getParent() &&
492 "Instructions not in same basic block!");
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000493 BasicBlock::const_iterator I = I1.getIterator();
494 BasicBlock::const_iterator E = I2.getIterator();
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000495 ++E; // Convert from inclusive to exclusive range.
496
Chris Lattner3a311832003-02-26 19:26:51 +0000497 for (; I != E; ++I) // Check every instruction in range
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000498 if (getModRefInfo(&*I, Loc) & Mode)
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000499 return true;
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000500 return false;
501}
502
Chandler Carruth7b560d42015-09-09 17:55:00 +0000503// Provide a definition for the root virtual destructor.
504AAResults::Concept::~Concept() {}
505
NAKAMURA Takumidf0cd722016-02-28 17:17:00 +0000506// Provide a definition for the static object used to identify passes.
Chandler Carruthb4faf132016-03-11 10:22:49 +0000507char AAManager::PassID;
NAKAMURA Takumidf0cd722016-02-28 17:17:00 +0000508
Chandler Carruth2be10752015-10-21 12:15:19 +0000509namespace {
510/// A wrapper pass for external alias analyses. This just squirrels away the
511/// callback used to run any analyses and register their results.
512struct ExternalAAWrapperPass : ImmutablePass {
513 typedef std::function<void(Pass &, Function &, AAResults &)> CallbackT;
514
515 CallbackT CB;
516
517 static char ID;
518
519 ExternalAAWrapperPass() : ImmutablePass(ID) {
520 initializeExternalAAWrapperPassPass(*PassRegistry::getPassRegistry());
521 }
522 explicit ExternalAAWrapperPass(CallbackT CB)
523 : ImmutablePass(ID), CB(std::move(CB)) {
524 initializeExternalAAWrapperPassPass(*PassRegistry::getPassRegistry());
525 }
526
527 void getAnalysisUsage(AnalysisUsage &AU) const override {
528 AU.setPreservesAll();
529 }
530};
531}
532
533char ExternalAAWrapperPass::ID = 0;
534INITIALIZE_PASS(ExternalAAWrapperPass, "external-aa", "External Alias Analysis",
535 false, true)
536
537ImmutablePass *
538llvm::createExternalAAWrapperPass(ExternalAAWrapperPass::CallbackT Callback) {
539 return new ExternalAAWrapperPass(std::move(Callback));
540}
541
Chandler Carruth7b560d42015-09-09 17:55:00 +0000542AAResultsWrapperPass::AAResultsWrapperPass() : FunctionPass(ID) {
543 initializeAAResultsWrapperPassPass(*PassRegistry::getPassRegistry());
544}
545
546char AAResultsWrapperPass::ID = 0;
547
548INITIALIZE_PASS_BEGIN(AAResultsWrapperPass, "aa",
549 "Function Alias Analysis Results", false, true)
550INITIALIZE_PASS_DEPENDENCY(BasicAAWrapperPass)
551INITIALIZE_PASS_DEPENDENCY(CFLAAWrapperPass)
Chandler Carruth2be10752015-10-21 12:15:19 +0000552INITIALIZE_PASS_DEPENDENCY(ExternalAAWrapperPass)
Chandler Carruth7b560d42015-09-09 17:55:00 +0000553INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass)
554INITIALIZE_PASS_DEPENDENCY(ObjCARCAAWrapperPass)
555INITIALIZE_PASS_DEPENDENCY(SCEVAAWrapperPass)
556INITIALIZE_PASS_DEPENDENCY(ScopedNoAliasAAWrapperPass)
557INITIALIZE_PASS_DEPENDENCY(TypeBasedAAWrapperPass)
558INITIALIZE_PASS_END(AAResultsWrapperPass, "aa",
559 "Function Alias Analysis Results", false, true)
560
561FunctionPass *llvm::createAAResultsWrapperPass() {
562 return new AAResultsWrapperPass();
563}
564
565/// Run the wrapper pass to rebuild an aggregation over known AA passes.
566///
567/// This is the legacy pass manager's interface to the new-style AA results
568/// aggregation object. Because this is somewhat shoe-horned into the legacy
569/// pass manager, we hard code all the specific alias analyses available into
570/// it. While the particular set enabled is configured via commandline flags,
571/// adding a new alias analysis to LLVM will require adding support for it to
572/// this list.
573bool AAResultsWrapperPass::runOnFunction(Function &F) {
574 // NB! This *must* be reset before adding new AA results to the new
575 // AAResults object because in the legacy pass manager, each instance
576 // of these will refer to the *same* immutable analyses, registering and
577 // unregistering themselves with them. We need to carefully tear down the
578 // previous object first, in this case replacing it with an empty one, before
579 // registering new results.
Chandler Carruth12884f72016-03-02 15:56:53 +0000580 AAR.reset(
581 new AAResults(getAnalysis<TargetLibraryInfoWrapperPass>().getTLI()));
Chandler Carruth7b560d42015-09-09 17:55:00 +0000582
583 // BasicAA is always available for function analyses. Also, we add it first
584 // so that it can trump TBAA results when it proves MustAlias.
585 // FIXME: TBAA should have an explicit mode to support this and then we
586 // should reconsider the ordering here.
587 if (!DisableBasicAA)
588 AAR->addAAResult(getAnalysis<BasicAAWrapperPass>().getResult());
589
590 // Populate the results with the currently available AAs.
591 if (auto *WrapperPass = getAnalysisIfAvailable<ScopedNoAliasAAWrapperPass>())
592 AAR->addAAResult(WrapperPass->getResult());
593 if (auto *WrapperPass = getAnalysisIfAvailable<TypeBasedAAWrapperPass>())
594 AAR->addAAResult(WrapperPass->getResult());
595 if (auto *WrapperPass =
596 getAnalysisIfAvailable<objcarc::ObjCARCAAWrapperPass>())
597 AAR->addAAResult(WrapperPass->getResult());
598 if (auto *WrapperPass = getAnalysisIfAvailable<GlobalsAAWrapperPass>())
599 AAR->addAAResult(WrapperPass->getResult());
600 if (auto *WrapperPass = getAnalysisIfAvailable<SCEVAAWrapperPass>())
601 AAR->addAAResult(WrapperPass->getResult());
602 if (auto *WrapperPass = getAnalysisIfAvailable<CFLAAWrapperPass>())
603 AAR->addAAResult(WrapperPass->getResult());
604
Chandler Carruth2be10752015-10-21 12:15:19 +0000605 // If available, run an external AA providing callback over the results as
606 // well.
607 if (auto *WrapperPass = getAnalysisIfAvailable<ExternalAAWrapperPass>())
608 if (WrapperPass->CB)
609 WrapperPass->CB(*this, F, *AAR);
610
Chandler Carruth7b560d42015-09-09 17:55:00 +0000611 // Analyses don't mutate the IR, so return false.
612 return false;
613}
614
615void AAResultsWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
616 AU.setPreservesAll();
617 AU.addRequired<BasicAAWrapperPass>();
Chandler Carruth12884f72016-03-02 15:56:53 +0000618 AU.addRequired<TargetLibraryInfoWrapperPass>();
Chandler Carruth7b560d42015-09-09 17:55:00 +0000619
620 // We also need to mark all the alias analysis passes we will potentially
621 // probe in runOnFunction as used here to ensure the legacy pass manager
622 // preserves them. This hard coding of lists of alias analyses is specific to
623 // the legacy pass manager.
624 AU.addUsedIfAvailable<ScopedNoAliasAAWrapperPass>();
625 AU.addUsedIfAvailable<TypeBasedAAWrapperPass>();
626 AU.addUsedIfAvailable<objcarc::ObjCARCAAWrapperPass>();
627 AU.addUsedIfAvailable<GlobalsAAWrapperPass>();
628 AU.addUsedIfAvailable<SCEVAAWrapperPass>();
629 AU.addUsedIfAvailable<CFLAAWrapperPass>();
630}
631
632AAResults llvm::createLegacyPMAAResults(Pass &P, Function &F,
633 BasicAAResult &BAR) {
Chandler Carruth12884f72016-03-02 15:56:53 +0000634 AAResults AAR(P.getAnalysis<TargetLibraryInfoWrapperPass>().getTLI());
Chandler Carruth7b560d42015-09-09 17:55:00 +0000635
636 // Add in our explicitly constructed BasicAA results.
637 if (!DisableBasicAA)
638 AAR.addAAResult(BAR);
639
640 // Populate the results with the other currently available AAs.
641 if (auto *WrapperPass =
642 P.getAnalysisIfAvailable<ScopedNoAliasAAWrapperPass>())
643 AAR.addAAResult(WrapperPass->getResult());
644 if (auto *WrapperPass = P.getAnalysisIfAvailable<TypeBasedAAWrapperPass>())
645 AAR.addAAResult(WrapperPass->getResult());
646 if (auto *WrapperPass =
647 P.getAnalysisIfAvailable<objcarc::ObjCARCAAWrapperPass>())
648 AAR.addAAResult(WrapperPass->getResult());
649 if (auto *WrapperPass = P.getAnalysisIfAvailable<GlobalsAAWrapperPass>())
650 AAR.addAAResult(WrapperPass->getResult());
Chandler Carruth7b560d42015-09-09 17:55:00 +0000651 if (auto *WrapperPass = P.getAnalysisIfAvailable<CFLAAWrapperPass>())
652 AAR.addAAResult(WrapperPass->getResult());
653
654 return AAR;
655}
656
Dan Gohmanfb306c02009-02-03 01:28:32 +0000657bool llvm::isNoAliasCall(const Value *V) {
Benjamin Kramer45f39542015-08-05 14:16:44 +0000658 if (auto CS = ImmutableCallSite(V))
659 return CS.paramHasAttr(0, Attribute::NoAlias);
Dan Gohmanfb306c02009-02-03 01:28:32 +0000660 return false;
661}
662
Sanjay Pateld7f613d2016-01-13 22:17:13 +0000663bool llvm::isNoAliasArgument(const Value *V) {
Michael Kupersteinf3e663a2013-05-28 08:17:48 +0000664 if (const Argument *A = dyn_cast<Argument>(V))
665 return A->hasNoAliasAttr();
666 return false;
667}
668
Dan Gohman00ef9322010-07-07 14:27:09 +0000669bool llvm::isIdentifiedObject(const Value *V) {
Dan Gohman0824aff2010-06-29 00:50:39 +0000670 if (isa<AllocaInst>(V))
Dan Gohman1f59f012009-08-27 17:52:56 +0000671 return true;
672 if (isa<GlobalValue>(V) && !isa<GlobalAlias>(V))
Dan Gohmanfb306c02009-02-03 01:28:32 +0000673 return true;
Dan Gohman00ef9322010-07-07 14:27:09 +0000674 if (isNoAliasCall(V))
675 return true;
676 if (const Argument *A = dyn_cast<Argument>(V))
677 return A->hasNoAliasAttr() || A->hasByValAttr();
Dan Gohmanfb306c02009-02-03 01:28:32 +0000678 return false;
679}
Hal Finkelc782aa52014-07-21 12:27:23 +0000680
Sanjay Pateld7f613d2016-01-13 22:17:13 +0000681bool llvm::isIdentifiedFunctionLocal(const Value *V) {
Hal Finkelc782aa52014-07-21 12:27:23 +0000682 return isa<AllocaInst>(V) || isNoAliasCall(V) || isNoAliasArgument(V);
683}
Sanjoy Das1c481f52016-02-09 01:21:57 +0000684
Chandler Carruth12884f72016-03-02 15:56:53 +0000685void llvm::getAAResultsAnalysisUsage(AnalysisUsage &AU) {
Sanjoy Das1c481f52016-02-09 01:21:57 +0000686 // This function needs to be in sync with llvm::createLegacyPMAAResults -- if
687 // more alias analyses are added to llvm::createLegacyPMAAResults, they need
688 // to be added here also.
Chandler Carruth12884f72016-03-02 15:56:53 +0000689 AU.addRequired<TargetLibraryInfoWrapperPass>();
Sanjoy Das1c481f52016-02-09 01:21:57 +0000690 AU.addUsedIfAvailable<ScopedNoAliasAAWrapperPass>();
691 AU.addUsedIfAvailable<TypeBasedAAWrapperPass>();
692 AU.addUsedIfAvailable<objcarc::ObjCARCAAWrapperPass>();
693 AU.addUsedIfAvailable<GlobalsAAWrapperPass>();
694 AU.addUsedIfAvailable<CFLAAWrapperPass>();
695}