blob: a9d4610ed67ef50cde769b502909732041304abf [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);
Nicolai Haehnle84c9f992016-07-04 08:01:29 +0000145 else if (doesNotReadMemory(MRB))
146 Result = ModRefInfo(Result & MRI_Mod);
Chandler Carruth12884f72016-03-02 15:56:53 +0000147
148 if (onlyAccessesArgPointees(MRB)) {
149 bool DoesAlias = false;
150 ModRefInfo AllArgsMask = MRI_NoModRef;
151 if (doesAccessArgPointees(MRB)) {
152 for (auto AI = CS.arg_begin(), AE = CS.arg_end(); AI != AE; ++AI) {
153 const Value *Arg = *AI;
154 if (!Arg->getType()->isPointerTy())
155 continue;
156 unsigned ArgIdx = std::distance(CS.arg_begin(), AI);
157 MemoryLocation ArgLoc = MemoryLocation::getForArgument(CS, ArgIdx, TLI);
158 AliasResult ArgAlias = alias(ArgLoc, Loc);
159 if (ArgAlias != NoAlias) {
160 ModRefInfo ArgMask = getArgModRefInfo(CS, ArgIdx);
161 DoesAlias = true;
162 AllArgsMask = ModRefInfo(AllArgsMask | ArgMask);
163 }
164 }
165 }
166 if (!DoesAlias)
167 return MRI_NoModRef;
168 Result = ModRefInfo(Result & AllArgsMask);
169 }
170
171 // If Loc is a constant memory location, the call definitely could not
172 // modify the memory location.
173 if ((Result & MRI_Mod) &&
174 pointsToConstantMemory(Loc, /*OrLocal*/ false))
175 Result = ModRefInfo(Result & ~MRI_Mod);
176
Chandler Carruth7b560d42015-09-09 17:55:00 +0000177 return Result;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000178}
179
Chandler Carruth7b560d42015-09-09 17:55:00 +0000180ModRefInfo AAResults::getModRefInfo(ImmutableCallSite CS1,
181 ImmutableCallSite CS2) {
182 ModRefInfo Result = MRI_ModRef;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000183
Chandler Carruth7b560d42015-09-09 17:55:00 +0000184 for (const auto &AA : AAs) {
185 Result = ModRefInfo(Result & AA->getModRefInfo(CS1, CS2));
Dan Gohman5f1702e2010-08-06 01:25:49 +0000186
Chandler Carruth7b560d42015-09-09 17:55:00 +0000187 // Early-exit the moment we reach the bottom of the lattice.
188 if (Result == MRI_NoModRef)
189 return Result;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000190 }
191
Chandler Carruth12884f72016-03-02 15:56:53 +0000192 // Try to refine the mod-ref info further using other API entry points to the
193 // aggregate set of AA results.
194
195 // If CS1 or CS2 are readnone, they don't interact.
196 auto CS1B = getModRefBehavior(CS1);
197 if (CS1B == FMRB_DoesNotAccessMemory)
198 return MRI_NoModRef;
199
200 auto CS2B = getModRefBehavior(CS2);
201 if (CS2B == FMRB_DoesNotAccessMemory)
202 return MRI_NoModRef;
203
204 // If they both only read from memory, there is no dependence.
205 if (onlyReadsMemory(CS1B) && onlyReadsMemory(CS2B))
206 return MRI_NoModRef;
207
208 // If CS1 only reads memory, the only dependence on CS2 can be
209 // from CS1 reading memory written by CS2.
210 if (onlyReadsMemory(CS1B))
211 Result = ModRefInfo(Result & MRI_Ref);
Nicolai Haehnle84c9f992016-07-04 08:01:29 +0000212 else if (doesNotReadMemory(CS1B))
213 Result = ModRefInfo(Result & MRI_Mod);
Chandler Carruth12884f72016-03-02 15:56:53 +0000214
215 // If CS2 only access memory through arguments, accumulate the mod/ref
216 // information from CS1's references to the memory referenced by
217 // CS2's arguments.
218 if (onlyAccessesArgPointees(CS2B)) {
219 ModRefInfo R = MRI_NoModRef;
220 if (doesAccessArgPointees(CS2B)) {
221 for (auto I = CS2.arg_begin(), E = CS2.arg_end(); I != E; ++I) {
222 const Value *Arg = *I;
223 if (!Arg->getType()->isPointerTy())
224 continue;
225 unsigned CS2ArgIdx = std::distance(CS2.arg_begin(), I);
226 auto CS2ArgLoc = MemoryLocation::getForArgument(CS2, CS2ArgIdx, TLI);
227
228 // ArgMask indicates what CS2 might do to CS2ArgLoc, and the dependence
229 // of CS1 on that location is the inverse.
230 ModRefInfo ArgMask = getArgModRefInfo(CS2, CS2ArgIdx);
231 if (ArgMask == MRI_Mod)
232 ArgMask = MRI_ModRef;
233 else if (ArgMask == MRI_Ref)
234 ArgMask = MRI_Mod;
235
236 ArgMask = ModRefInfo(ArgMask & getModRefInfo(CS1, CS2ArgLoc));
237
238 R = ModRefInfo((R | ArgMask) & Result);
239 if (R == Result)
240 break;
241 }
242 }
243 return R;
244 }
245
246 // If CS1 only accesses memory through arguments, check if CS2 references
247 // any of the memory referenced by CS1's arguments. If not, return NoModRef.
248 if (onlyAccessesArgPointees(CS1B)) {
249 ModRefInfo R = MRI_NoModRef;
250 if (doesAccessArgPointees(CS1B)) {
251 for (auto I = CS1.arg_begin(), E = CS1.arg_end(); I != E; ++I) {
252 const Value *Arg = *I;
253 if (!Arg->getType()->isPointerTy())
254 continue;
255 unsigned CS1ArgIdx = std::distance(CS1.arg_begin(), I);
256 auto CS1ArgLoc = MemoryLocation::getForArgument(CS1, CS1ArgIdx, TLI);
257
258 // ArgMask indicates what CS1 might do to CS1ArgLoc; if CS1 might Mod
259 // CS1ArgLoc, then we care about either a Mod or a Ref by CS2. If CS1
260 // might Ref, then we care only about a Mod by CS2.
261 ModRefInfo ArgMask = getArgModRefInfo(CS1, CS1ArgIdx);
262 ModRefInfo ArgR = getModRefInfo(CS2, CS1ArgLoc);
263 if (((ArgMask & MRI_Mod) != MRI_NoModRef &&
264 (ArgR & MRI_ModRef) != MRI_NoModRef) ||
265 ((ArgMask & MRI_Ref) != MRI_NoModRef &&
266 (ArgR & MRI_Mod) != MRI_NoModRef))
267 R = ModRefInfo((R | ArgMask) & Result);
268
269 if (R == Result)
270 break;
271 }
272 }
273 return R;
274 }
275
Chandler Carruth7b560d42015-09-09 17:55:00 +0000276 return Result;
277}
Chandler Carruthc41404a2015-06-17 07:12:40 +0000278
Chandler Carruth7b560d42015-09-09 17:55:00 +0000279FunctionModRefBehavior AAResults::getModRefBehavior(ImmutableCallSite CS) {
280 FunctionModRefBehavior Result = FMRB_UnknownModRefBehavior;
Hal Finkel354e23b2014-07-17 01:28:25 +0000281
Chandler Carruth7b560d42015-09-09 17:55:00 +0000282 for (const auto &AA : AAs) {
283 Result = FunctionModRefBehavior(Result & AA->getModRefBehavior(CS));
284
285 // Early-exit the moment we reach the bottom of the lattice.
286 if (Result == FMRB_DoesNotAccessMemory)
287 return Result;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000288 }
289
Chandler Carruth7b560d42015-09-09 17:55:00 +0000290 return Result;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000291}
292
Chandler Carruth7b560d42015-09-09 17:55:00 +0000293FunctionModRefBehavior AAResults::getModRefBehavior(const Function *F) {
294 FunctionModRefBehavior Result = FMRB_UnknownModRefBehavior;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000295
Chandler Carruth7b560d42015-09-09 17:55:00 +0000296 for (const auto &AA : AAs) {
297 Result = FunctionModRefBehavior(Result & AA->getModRefBehavior(F));
Dan Gohman5f1702e2010-08-06 01:25:49 +0000298
Chandler Carruth7b560d42015-09-09 17:55:00 +0000299 // Early-exit the moment we reach the bottom of the lattice.
300 if (Result == FMRB_DoesNotAccessMemory)
301 return Result;
302 }
Dan Gohman5f1702e2010-08-06 01:25:49 +0000303
Chandler Carruth7b560d42015-09-09 17:55:00 +0000304 return Result;
Chris Lattner62c37002004-05-23 21:15:48 +0000305}
306
Chris Lattner62c37002004-05-23 21:15:48 +0000307//===----------------------------------------------------------------------===//
Chandler Carruth7b560d42015-09-09 17:55:00 +0000308// Helper method implementation
Chris Lattner62c37002004-05-23 21:15:48 +0000309//===----------------------------------------------------------------------===//
310
Chandler Carruth7b560d42015-09-09 17:55:00 +0000311ModRefInfo AAResults::getModRefInfo(const LoadInst *L,
312 const MemoryLocation &Loc) {
Eli Friedman5494ada2011-08-15 20:54:19 +0000313 // Be conservative in the face of volatile/atomic.
314 if (!L->isUnordered())
Chandler Carruth194f59c2015-07-22 23:15:57 +0000315 return MRI_ModRef;
Dan Gohman6b4671b2010-08-06 18:11:28 +0000316
Dan Gohman52f9d7d2010-08-03 17:27:43 +0000317 // If the load address doesn't alias the given address, it doesn't read
318 // or write the specified memory.
Chandler Carruth70c61c12015-06-04 02:03:15 +0000319 if (Loc.Ptr && !alias(MemoryLocation::get(L), Loc))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000320 return MRI_NoModRef;
Dan Gohman52f9d7d2010-08-03 17:27:43 +0000321
Dan Gohman52f9d7d2010-08-03 17:27:43 +0000322 // Otherwise, a load just reads.
Chandler Carruth194f59c2015-07-22 23:15:57 +0000323 return MRI_Ref;
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000324}
325
Chandler Carruth7b560d42015-09-09 17:55:00 +0000326ModRefInfo AAResults::getModRefInfo(const StoreInst *S,
327 const MemoryLocation &Loc) {
Eli Friedman5494ada2011-08-15 20:54:19 +0000328 // Be conservative in the face of volatile/atomic.
329 if (!S->isUnordered())
Chandler Carruth194f59c2015-07-22 23:15:57 +0000330 return MRI_ModRef;
Dan Gohman6b4671b2010-08-06 18:11:28 +0000331
Daniel Berlinb2d22762015-04-13 23:05:45 +0000332 if (Loc.Ptr) {
333 // If the store address cannot alias the pointer in question, then the
334 // specified memory cannot be modified by the store.
Chandler Carruth70c61c12015-06-04 02:03:15 +0000335 if (!alias(MemoryLocation::get(S), Loc))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000336 return MRI_NoModRef;
Chris Lattner96055762004-01-30 22:16:42 +0000337
Daniel Berlinb2d22762015-04-13 23:05:45 +0000338 // If the pointer is a pointer to constant memory, then it could not have
339 // been modified by this store.
340 if (pointsToConstantMemory(Loc))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000341 return MRI_NoModRef;
Daniel Berlinb2d22762015-04-13 23:05:45 +0000342 }
Dan Gohman52f9d7d2010-08-03 17:27:43 +0000343
344 // Otherwise, a store just writes.
Chandler Carruth194f59c2015-07-22 23:15:57 +0000345 return MRI_Mod;
Chris Lattner3a311832003-02-26 19:26:51 +0000346}
347
Chandler Carruth7b560d42015-09-09 17:55:00 +0000348ModRefInfo AAResults::getModRefInfo(const VAArgInst *V,
349 const MemoryLocation &Loc) {
Dan Gohmane68958f2010-08-06 18:24:38 +0000350
Daniel Berlinec1de3f2015-04-28 19:19:14 +0000351 if (Loc.Ptr) {
352 // If the va_arg address cannot alias the pointer in question, then the
353 // specified memory cannot be accessed by the va_arg.
Chandler Carruth70c61c12015-06-04 02:03:15 +0000354 if (!alias(MemoryLocation::get(V), Loc))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000355 return MRI_NoModRef;
Daniel Berlinec1de3f2015-04-28 19:19:14 +0000356
357 // If the pointer is a pointer to constant memory, then it could not have
358 // been modified by this va_arg.
359 if (pointsToConstantMemory(Loc))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000360 return MRI_NoModRef;
Daniel Berlinec1de3f2015-04-28 19:19:14 +0000361 }
Dan Gohmane68958f2010-08-06 18:24:38 +0000362
363 // Otherwise, a va_arg reads and writes.
Chandler Carruth194f59c2015-07-22 23:15:57 +0000364 return MRI_ModRef;
Dan Gohmane68958f2010-08-06 18:24:38 +0000365}
366
David Majnemer6727c012015-11-17 08:15:14 +0000367ModRefInfo AAResults::getModRefInfo(const CatchPadInst *CatchPad,
368 const MemoryLocation &Loc) {
369 if (Loc.Ptr) {
370 // If the pointer is a pointer to constant memory,
371 // then it could not have been modified by this catchpad.
372 if (pointsToConstantMemory(Loc))
373 return MRI_NoModRef;
374 }
375
376 // Otherwise, a catchpad reads and writes.
377 return MRI_ModRef;
378}
379
380ModRefInfo AAResults::getModRefInfo(const CatchReturnInst *CatchRet,
381 const MemoryLocation &Loc) {
382 if (Loc.Ptr) {
383 // If the pointer is a pointer to constant memory,
384 // then it could not have been modified by this catchpad.
385 if (pointsToConstantMemory(Loc))
386 return MRI_NoModRef;
387 }
388
389 // Otherwise, a catchret reads and writes.
390 return MRI_ModRef;
391}
392
Chandler Carruth7b560d42015-09-09 17:55:00 +0000393ModRefInfo AAResults::getModRefInfo(const AtomicCmpXchgInst *CX,
394 const MemoryLocation &Loc) {
Eli Friedman5c918912011-09-26 20:15:28 +0000395 // Acquire/Release cmpxchg has properties that matter for arbitrary addresses.
JF Bastien800f87a2016-04-06 21:19:33 +0000396 if (isStrongerThanMonotonic(CX->getSuccessOrdering()))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000397 return MRI_ModRef;
Eli Friedman5c918912011-09-26 20:15:28 +0000398
399 // If the cmpxchg address does not alias the location, it does not access it.
Chandler Carruth70c61c12015-06-04 02:03:15 +0000400 if (Loc.Ptr && !alias(MemoryLocation::get(CX), Loc))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000401 return MRI_NoModRef;
Eli Friedman5c918912011-09-26 20:15:28 +0000402
Chandler Carruth194f59c2015-07-22 23:15:57 +0000403 return MRI_ModRef;
Eli Friedman5c918912011-09-26 20:15:28 +0000404}
405
Chandler Carruth7b560d42015-09-09 17:55:00 +0000406ModRefInfo AAResults::getModRefInfo(const AtomicRMWInst *RMW,
407 const MemoryLocation &Loc) {
Eli Friedman5c918912011-09-26 20:15:28 +0000408 // Acquire/Release atomicrmw has properties that matter for arbitrary addresses.
JF Bastien800f87a2016-04-06 21:19:33 +0000409 if (isStrongerThanMonotonic(RMW->getOrdering()))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000410 return MRI_ModRef;
Eli Friedman5c918912011-09-26 20:15:28 +0000411
412 // If the atomicrmw address does not alias the location, it does not access it.
Chandler Carruth70c61c12015-06-04 02:03:15 +0000413 if (Loc.Ptr && !alias(MemoryLocation::get(RMW), Loc))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000414 return MRI_NoModRef;
Eli Friedman5c918912011-09-26 20:15:28 +0000415
Chandler Carruth194f59c2015-07-22 23:15:57 +0000416 return MRI_ModRef;
Eli Friedman5c918912011-09-26 20:15:28 +0000417}
418
Bruno Cardoso Lopesdfc1d962015-07-31 14:31:35 +0000419/// \brief Return information about whether a particular call site modifies
420/// or reads the specified memory location \p MemLoc before instruction \p I
421/// in a BasicBlock. A ordered basic block \p OBB can be used to speed up
422/// instruction-ordering queries inside the BasicBlock containing \p I.
423/// FIXME: this is really just shoring-up a deficiency in alias analysis.
424/// BasicAA isn't willing to spend linear time determining whether an alloca
425/// was captured before or after this particular call, while we are. However,
426/// with a smarter AA in place, this test is just wasting compile time.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000427ModRefInfo AAResults::callCapturesBefore(const Instruction *I,
428 const MemoryLocation &MemLoc,
429 DominatorTree *DT,
430 OrderedBasicBlock *OBB) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000431 if (!DT)
Chandler Carruth194f59c2015-07-22 23:15:57 +0000432 return MRI_ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000433
Chandler Carruth7b560d42015-09-09 17:55:00 +0000434 const Value *Object =
435 GetUnderlyingObject(MemLoc.Ptr, I->getModule()->getDataLayout());
Chad Rosiera968caf2012-05-14 20:35:04 +0000436 if (!isIdentifiedObject(Object) || isa<GlobalValue>(Object) ||
437 isa<Constant>(Object))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000438 return MRI_ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000439
440 ImmutableCallSite CS(I);
441 if (!CS.getInstruction() || CS.getInstruction() == Object)
Chandler Carruth194f59c2015-07-22 23:15:57 +0000442 return MRI_ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000443
Hal Finkelb0356212014-07-21 13:15:48 +0000444 if (llvm::PointerMayBeCapturedBefore(Object, /* ReturnCaptures */ true,
Hal Finkeld32803b2014-07-21 21:30:22 +0000445 /* StoreCaptures */ true, I, DT,
Bruno Cardoso Lopesdfc1d962015-07-31 14:31:35 +0000446 /* include Object */ true,
447 /* OrderedBasicBlock */ OBB))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000448 return MRI_ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000449
450 unsigned ArgNo = 0;
Chandler Carruth194f59c2015-07-22 23:15:57 +0000451 ModRefInfo R = MRI_NoModRef;
Sanjoy Dasd0bdf3e2016-06-13 19:55:04 +0000452 for (auto CI = CS.data_operands_begin(), CE = CS.data_operands_end();
Chad Rosiera968caf2012-05-14 20:35:04 +0000453 CI != CE; ++CI, ++ArgNo) {
454 // Only look at the no-capture or byval pointer arguments. If this
455 // pointer were passed to arguments that were neither of these, then it
456 // couldn't be no-capture.
457 if (!(*CI)->getType()->isPointerTy() ||
458 (!CS.doesNotCapture(ArgNo) && !CS.isByValArgument(ArgNo)))
459 continue;
460
461 // If this is a no-capture pointer argument, see if we can tell that it
462 // is impossible to alias the pointer we're checking. If not, we have to
463 // assume that the call could touch the pointer, even though it doesn't
464 // escape.
Chandler Carruthac80dc72015-06-17 07:18:54 +0000465 if (isNoAlias(MemoryLocation(*CI), MemoryLocation(Object)))
Nick Lewyckyc0514622013-07-07 10:15:16 +0000466 continue;
467 if (CS.doesNotAccessMemory(ArgNo))
468 continue;
469 if (CS.onlyReadsMemory(ArgNo)) {
Chandler Carruth194f59c2015-07-22 23:15:57 +0000470 R = MRI_Ref;
Nick Lewyckyc0514622013-07-07 10:15:16 +0000471 continue;
Chad Rosiera968caf2012-05-14 20:35:04 +0000472 }
Chandler Carruth194f59c2015-07-22 23:15:57 +0000473 return MRI_ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000474 }
Nick Lewyckyc0514622013-07-07 10:15:16 +0000475 return R;
Chad Rosiera968caf2012-05-14 20:35:04 +0000476}
Eli Friedman5c918912011-09-26 20:15:28 +0000477
Chris Lattnerd922a842002-08-22 22:46:39 +0000478/// canBasicBlockModify - Return true if it is possible for execution of the
Elena Demikhovskya5599bf2014-12-15 14:09:53 +0000479/// specified basic block to modify the location Loc.
Chris Lattnerd922a842002-08-22 22:46:39 +0000480///
Chandler Carruth7b560d42015-09-09 17:55:00 +0000481bool AAResults::canBasicBlockModify(const BasicBlock &BB,
482 const MemoryLocation &Loc) {
Chandler Carruth194f59c2015-07-22 23:15:57 +0000483 return canInstructionRangeModRef(BB.front(), BB.back(), Loc, MRI_Mod);
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000484}
485
Elena Demikhovskya5599bf2014-12-15 14:09:53 +0000486/// canInstructionRangeModRef - Return true if it is possible for the
487/// execution of the specified instructions to mod\ref (according to the
488/// mode) the location Loc. The instructions to consider are all
489/// of the instructions in the range of [I1,I2] INCLUSIVE.
Teresa Johnsonbbcf75e2015-05-13 15:04:14 +0000490/// I1 and I2 must be in the same basic block.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000491bool AAResults::canInstructionRangeModRef(const Instruction &I1,
492 const Instruction &I2,
493 const MemoryLocation &Loc,
494 const ModRefInfo Mode) {
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000495 assert(I1.getParent() == I2.getParent() &&
496 "Instructions not in same basic block!");
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000497 BasicBlock::const_iterator I = I1.getIterator();
498 BasicBlock::const_iterator E = I2.getIterator();
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000499 ++E; // Convert from inclusive to exclusive range.
500
Chris Lattner3a311832003-02-26 19:26:51 +0000501 for (; I != E; ++I) // Check every instruction in range
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000502 if (getModRefInfo(&*I, Loc) & Mode)
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000503 return true;
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000504 return false;
505}
506
Chandler Carruth7b560d42015-09-09 17:55:00 +0000507// Provide a definition for the root virtual destructor.
508AAResults::Concept::~Concept() {}
509
NAKAMURA Takumidf0cd722016-02-28 17:17:00 +0000510// Provide a definition for the static object used to identify passes.
Chandler Carruthb4faf132016-03-11 10:22:49 +0000511char AAManager::PassID;
NAKAMURA Takumidf0cd722016-02-28 17:17:00 +0000512
Chandler Carruth2be10752015-10-21 12:15:19 +0000513namespace {
514/// A wrapper pass for external alias analyses. This just squirrels away the
515/// callback used to run any analyses and register their results.
516struct ExternalAAWrapperPass : ImmutablePass {
517 typedef std::function<void(Pass &, Function &, AAResults &)> CallbackT;
518
519 CallbackT CB;
520
521 static char ID;
522
523 ExternalAAWrapperPass() : ImmutablePass(ID) {
524 initializeExternalAAWrapperPassPass(*PassRegistry::getPassRegistry());
525 }
526 explicit ExternalAAWrapperPass(CallbackT CB)
527 : ImmutablePass(ID), CB(std::move(CB)) {
528 initializeExternalAAWrapperPassPass(*PassRegistry::getPassRegistry());
529 }
530
531 void getAnalysisUsage(AnalysisUsage &AU) const override {
532 AU.setPreservesAll();
533 }
534};
535}
536
537char ExternalAAWrapperPass::ID = 0;
538INITIALIZE_PASS(ExternalAAWrapperPass, "external-aa", "External Alias Analysis",
539 false, true)
540
541ImmutablePass *
542llvm::createExternalAAWrapperPass(ExternalAAWrapperPass::CallbackT Callback) {
543 return new ExternalAAWrapperPass(std::move(Callback));
544}
545
Chandler Carruth7b560d42015-09-09 17:55:00 +0000546AAResultsWrapperPass::AAResultsWrapperPass() : FunctionPass(ID) {
547 initializeAAResultsWrapperPassPass(*PassRegistry::getPassRegistry());
548}
549
550char AAResultsWrapperPass::ID = 0;
551
552INITIALIZE_PASS_BEGIN(AAResultsWrapperPass, "aa",
553 "Function Alias Analysis Results", false, true)
554INITIALIZE_PASS_DEPENDENCY(BasicAAWrapperPass)
555INITIALIZE_PASS_DEPENDENCY(CFLAAWrapperPass)
Chandler Carruth2be10752015-10-21 12:15:19 +0000556INITIALIZE_PASS_DEPENDENCY(ExternalAAWrapperPass)
Chandler Carruth7b560d42015-09-09 17:55:00 +0000557INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass)
558INITIALIZE_PASS_DEPENDENCY(ObjCARCAAWrapperPass)
559INITIALIZE_PASS_DEPENDENCY(SCEVAAWrapperPass)
560INITIALIZE_PASS_DEPENDENCY(ScopedNoAliasAAWrapperPass)
561INITIALIZE_PASS_DEPENDENCY(TypeBasedAAWrapperPass)
562INITIALIZE_PASS_END(AAResultsWrapperPass, "aa",
563 "Function Alias Analysis Results", false, true)
564
565FunctionPass *llvm::createAAResultsWrapperPass() {
566 return new AAResultsWrapperPass();
567}
568
569/// Run the wrapper pass to rebuild an aggregation over known AA passes.
570///
571/// This is the legacy pass manager's interface to the new-style AA results
572/// aggregation object. Because this is somewhat shoe-horned into the legacy
573/// pass manager, we hard code all the specific alias analyses available into
574/// it. While the particular set enabled is configured via commandline flags,
575/// adding a new alias analysis to LLVM will require adding support for it to
576/// this list.
577bool AAResultsWrapperPass::runOnFunction(Function &F) {
578 // NB! This *must* be reset before adding new AA results to the new
579 // AAResults object because in the legacy pass manager, each instance
580 // of these will refer to the *same* immutable analyses, registering and
581 // unregistering themselves with them. We need to carefully tear down the
582 // previous object first, in this case replacing it with an empty one, before
583 // registering new results.
Chandler Carruth12884f72016-03-02 15:56:53 +0000584 AAR.reset(
585 new AAResults(getAnalysis<TargetLibraryInfoWrapperPass>().getTLI()));
Chandler Carruth7b560d42015-09-09 17:55:00 +0000586
587 // BasicAA is always available for function analyses. Also, we add it first
588 // so that it can trump TBAA results when it proves MustAlias.
589 // FIXME: TBAA should have an explicit mode to support this and then we
590 // should reconsider the ordering here.
591 if (!DisableBasicAA)
592 AAR->addAAResult(getAnalysis<BasicAAWrapperPass>().getResult());
593
594 // Populate the results with the currently available AAs.
595 if (auto *WrapperPass = getAnalysisIfAvailable<ScopedNoAliasAAWrapperPass>())
596 AAR->addAAResult(WrapperPass->getResult());
597 if (auto *WrapperPass = getAnalysisIfAvailable<TypeBasedAAWrapperPass>())
598 AAR->addAAResult(WrapperPass->getResult());
599 if (auto *WrapperPass =
600 getAnalysisIfAvailable<objcarc::ObjCARCAAWrapperPass>())
601 AAR->addAAResult(WrapperPass->getResult());
602 if (auto *WrapperPass = getAnalysisIfAvailable<GlobalsAAWrapperPass>())
603 AAR->addAAResult(WrapperPass->getResult());
604 if (auto *WrapperPass = getAnalysisIfAvailable<SCEVAAWrapperPass>())
605 AAR->addAAResult(WrapperPass->getResult());
606 if (auto *WrapperPass = getAnalysisIfAvailable<CFLAAWrapperPass>())
607 AAR->addAAResult(WrapperPass->getResult());
608
Chandler Carruth2be10752015-10-21 12:15:19 +0000609 // If available, run an external AA providing callback over the results as
610 // well.
611 if (auto *WrapperPass = getAnalysisIfAvailable<ExternalAAWrapperPass>())
612 if (WrapperPass->CB)
613 WrapperPass->CB(*this, F, *AAR);
614
Chandler Carruth7b560d42015-09-09 17:55:00 +0000615 // Analyses don't mutate the IR, so return false.
616 return false;
617}
618
619void AAResultsWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
620 AU.setPreservesAll();
621 AU.addRequired<BasicAAWrapperPass>();
Chandler Carruth12884f72016-03-02 15:56:53 +0000622 AU.addRequired<TargetLibraryInfoWrapperPass>();
Chandler Carruth7b560d42015-09-09 17:55:00 +0000623
624 // We also need to mark all the alias analysis passes we will potentially
625 // probe in runOnFunction as used here to ensure the legacy pass manager
626 // preserves them. This hard coding of lists of alias analyses is specific to
627 // the legacy pass manager.
628 AU.addUsedIfAvailable<ScopedNoAliasAAWrapperPass>();
629 AU.addUsedIfAvailable<TypeBasedAAWrapperPass>();
630 AU.addUsedIfAvailable<objcarc::ObjCARCAAWrapperPass>();
631 AU.addUsedIfAvailable<GlobalsAAWrapperPass>();
632 AU.addUsedIfAvailable<SCEVAAWrapperPass>();
633 AU.addUsedIfAvailable<CFLAAWrapperPass>();
634}
635
636AAResults llvm::createLegacyPMAAResults(Pass &P, Function &F,
637 BasicAAResult &BAR) {
Chandler Carruth12884f72016-03-02 15:56:53 +0000638 AAResults AAR(P.getAnalysis<TargetLibraryInfoWrapperPass>().getTLI());
Chandler Carruth7b560d42015-09-09 17:55:00 +0000639
640 // Add in our explicitly constructed BasicAA results.
641 if (!DisableBasicAA)
642 AAR.addAAResult(BAR);
643
644 // Populate the results with the other currently available AAs.
645 if (auto *WrapperPass =
646 P.getAnalysisIfAvailable<ScopedNoAliasAAWrapperPass>())
647 AAR.addAAResult(WrapperPass->getResult());
648 if (auto *WrapperPass = P.getAnalysisIfAvailable<TypeBasedAAWrapperPass>())
649 AAR.addAAResult(WrapperPass->getResult());
650 if (auto *WrapperPass =
651 P.getAnalysisIfAvailable<objcarc::ObjCARCAAWrapperPass>())
652 AAR.addAAResult(WrapperPass->getResult());
653 if (auto *WrapperPass = P.getAnalysisIfAvailable<GlobalsAAWrapperPass>())
654 AAR.addAAResult(WrapperPass->getResult());
Chandler Carruth7b560d42015-09-09 17:55:00 +0000655 if (auto *WrapperPass = P.getAnalysisIfAvailable<CFLAAWrapperPass>())
656 AAR.addAAResult(WrapperPass->getResult());
657
658 return AAR;
659}
660
Dan Gohmanfb306c02009-02-03 01:28:32 +0000661bool llvm::isNoAliasCall(const Value *V) {
Benjamin Kramer45f39542015-08-05 14:16:44 +0000662 if (auto CS = ImmutableCallSite(V))
663 return CS.paramHasAttr(0, Attribute::NoAlias);
Dan Gohmanfb306c02009-02-03 01:28:32 +0000664 return false;
665}
666
Sanjay Pateld7f613d2016-01-13 22:17:13 +0000667bool llvm::isNoAliasArgument(const Value *V) {
Michael Kupersteinf3e663a2013-05-28 08:17:48 +0000668 if (const Argument *A = dyn_cast<Argument>(V))
669 return A->hasNoAliasAttr();
670 return false;
671}
672
Dan Gohman00ef9322010-07-07 14:27:09 +0000673bool llvm::isIdentifiedObject(const Value *V) {
Dan Gohman0824aff2010-06-29 00:50:39 +0000674 if (isa<AllocaInst>(V))
Dan Gohman1f59f012009-08-27 17:52:56 +0000675 return true;
676 if (isa<GlobalValue>(V) && !isa<GlobalAlias>(V))
Dan Gohmanfb306c02009-02-03 01:28:32 +0000677 return true;
Dan Gohman00ef9322010-07-07 14:27:09 +0000678 if (isNoAliasCall(V))
679 return true;
680 if (const Argument *A = dyn_cast<Argument>(V))
681 return A->hasNoAliasAttr() || A->hasByValAttr();
Dan Gohmanfb306c02009-02-03 01:28:32 +0000682 return false;
683}
Hal Finkelc782aa52014-07-21 12:27:23 +0000684
Sanjay Pateld7f613d2016-01-13 22:17:13 +0000685bool llvm::isIdentifiedFunctionLocal(const Value *V) {
Hal Finkelc782aa52014-07-21 12:27:23 +0000686 return isa<AllocaInst>(V) || isNoAliasCall(V) || isNoAliasArgument(V);
687}
Sanjoy Das1c481f52016-02-09 01:21:57 +0000688
Chandler Carruth12884f72016-03-02 15:56:53 +0000689void llvm::getAAResultsAnalysisUsage(AnalysisUsage &AU) {
Sanjoy Das1c481f52016-02-09 01:21:57 +0000690 // This function needs to be in sync with llvm::createLegacyPMAAResults -- if
691 // more alias analyses are added to llvm::createLegacyPMAAResults, they need
692 // to be added here also.
Chandler Carruth12884f72016-03-02 15:56:53 +0000693 AU.addRequired<TargetLibraryInfoWrapperPass>();
Sanjoy Das1c481f52016-02-09 01:21:57 +0000694 AU.addUsedIfAvailable<ScopedNoAliasAAWrapperPass>();
695 AU.addUsedIfAvailable<TypeBasedAAWrapperPass>();
696 AU.addUsedIfAvailable<objcarc::ObjCARCAAWrapperPass>();
697 AU.addUsedIfAvailable<GlobalsAAWrapperPass>();
698 AU.addUsedIfAvailable<CFLAAWrapperPass>();
699}