blob: 84da76be98bbc5fe497a6e3e225d6043a71c3fd3 [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"
George Burgess IVbfa401e2016-07-06 00:26:41 +000030#include "llvm/Analysis/CFLAndersAliasAnalysis.h"
31#include "llvm/Analysis/CFLSteensAliasAnalysis.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000032#include "llvm/Analysis/CaptureTracking.h"
Chandler Carruth7b560d42015-09-09 17:55:00 +000033#include "llvm/Analysis/GlobalsModRef.h"
34#include "llvm/Analysis/ObjCARCAliasAnalysis.h"
35#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
36#include "llvm/Analysis/ScopedNoAliasAA.h"
Chandler Carruth62d42152015-01-15 02:16:27 +000037#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruth7b560d42015-09-09 17:55:00 +000038#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
Chad Rosiera968caf2012-05-14 20:35:04 +000039#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000040#include "llvm/IR/BasicBlock.h"
41#include "llvm/IR/DataLayout.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000042#include "llvm/IR/Dominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000043#include "llvm/IR/Function.h"
44#include "llvm/IR/Instructions.h"
45#include "llvm/IR/IntrinsicInst.h"
46#include "llvm/IR/LLVMContext.h"
47#include "llvm/IR/Type.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000048#include "llvm/Pass.h"
Chris Lattnera67dbd02004-03-15 04:07:29 +000049using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000050
Chandler Carruth7b560d42015-09-09 17:55:00 +000051/// Allow disabling BasicAA from the AA results. This is particularly useful
52/// when testing to isolate a single AA implementation.
53static cl::opt<bool> DisableBasicAA("disable-basicaa", cl::Hidden,
54 cl::init(false));
55
Chandler Carruth17c630a2016-12-27 08:44:39 +000056AAResults::AAResults(AAResults &&Arg)
57 : TLI(Arg.TLI), AAs(std::move(Arg.AAs)), AADeps(std::move(Arg.AADeps)) {
Chandler Carruth7b560d42015-09-09 17:55:00 +000058 for (auto &AA : AAs)
59 AA->setAAResults(this);
60}
61
Chandler Carruth7b560d42015-09-09 17:55:00 +000062AAResults::~AAResults() {
63// FIXME; It would be nice to at least clear out the pointers back to this
64// aggregation here, but we end up with non-nesting lifetimes in the legacy
65// pass manager that prevent this from working. In the legacy pass manager
66// we'll end up with dangling references here in some cases.
67#if 0
68 for (auto &AA : AAs)
69 AA->setAAResults(nullptr);
70#endif
71}
Chris Lattner7d58f8d2002-08-22 18:25:32 +000072
Chandler Carruth17c630a2016-12-27 08:44:39 +000073bool AAResults::invalidate(Function &F, const PreservedAnalyses &PA,
74 FunctionAnalysisManager::Invalidator &Inv) {
Chandler Carruth17c630a2016-12-27 08:44:39 +000075 // Check if the AA manager itself has been invalidated.
76 auto PAC = PA.getChecker<AAManager>();
77 if (!PAC.preserved() && !PAC.preservedSet<AllAnalysesOn<Function>>())
78 return true; // The manager needs to be blown away, clear everything.
79
80 // Check all of the dependencies registered.
81 for (AnalysisKey *ID : AADeps)
82 if (Inv.invalidate(ID, F, PA))
83 return true;
84
85 // Everything we depend on is still fine, so are we. Nothing to invalidate.
86 return false;
87}
88
Chris Lattner62c37002004-05-23 21:15:48 +000089//===----------------------------------------------------------------------===//
90// Default chaining methods
91//===----------------------------------------------------------------------===//
92
Chandler Carruth7b560d42015-09-09 17:55:00 +000093AliasResult AAResults::alias(const MemoryLocation &LocA,
94 const MemoryLocation &LocB) {
95 for (const auto &AA : AAs) {
96 auto Result = AA->alias(LocA, LocB);
97 if (Result != MayAlias)
98 return Result;
99 }
100 return MayAlias;
Chris Lattner62c37002004-05-23 21:15:48 +0000101}
102
Chandler Carruth7b560d42015-09-09 17:55:00 +0000103bool AAResults::pointsToConstantMemory(const MemoryLocation &Loc,
104 bool OrLocal) {
105 for (const auto &AA : AAs)
106 if (AA->pointsToConstantMemory(Loc, OrLocal))
107 return true;
108
109 return false;
Chris Lattner62c37002004-05-23 21:15:48 +0000110}
111
Chandler Carruth7b560d42015-09-09 17:55:00 +0000112ModRefInfo AAResults::getArgModRefInfo(ImmutableCallSite CS, unsigned ArgIdx) {
113 ModRefInfo Result = MRI_ModRef;
114
115 for (const auto &AA : AAs) {
116 Result = ModRefInfo(Result & AA->getArgModRefInfo(CS, ArgIdx));
117
118 // Early-exit the moment we reach the bottom of the lattice.
119 if (Result == MRI_NoModRef)
120 return Result;
121 }
122
123 return Result;
Hal Finkel354e23b2014-07-17 01:28:25 +0000124}
125
Chandler Carruth7b560d42015-09-09 17:55:00 +0000126ModRefInfo AAResults::getModRefInfo(Instruction *I, ImmutableCallSite Call) {
Daniel Berlin8de312d2015-04-13 23:25:41 +0000127 // We may have two calls
128 if (auto CS = ImmutableCallSite(I)) {
129 // Check if the two calls modify the same memory
Nicolai Haehnle9343f362016-07-11 14:11:45 +0000130 return getModRefInfo(CS, Call);
David Majnemera940f362016-07-15 17:19:24 +0000131 } else if (I->isFenceLike()) {
132 // If this is a fence, just return MRI_ModRef.
133 return MRI_ModRef;
Daniel Berlin8de312d2015-04-13 23:25:41 +0000134 } else {
135 // Otherwise, check if the call modifies or references the
136 // location this memory access defines. The best we can say
137 // is that if the call references what this instruction
138 // defines, it must be clobbered by this location.
Chandler Carruthac80dc72015-06-17 07:18:54 +0000139 const MemoryLocation DefLoc = MemoryLocation::get(I);
Chandler Carruth194f59c2015-07-22 23:15:57 +0000140 if (getModRefInfo(Call, DefLoc) != MRI_NoModRef)
141 return MRI_ModRef;
Daniel Berlin8de312d2015-04-13 23:25:41 +0000142 }
Chandler Carruth194f59c2015-07-22 23:15:57 +0000143 return MRI_NoModRef;
Daniel Berlin8de312d2015-04-13 23:25:41 +0000144}
Owen Andersonb6e4ff02011-01-03 21:38:41 +0000145
Chandler Carruth7b560d42015-09-09 17:55:00 +0000146ModRefInfo AAResults::getModRefInfo(ImmutableCallSite CS,
147 const MemoryLocation &Loc) {
148 ModRefInfo Result = MRI_ModRef;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000149
Chandler Carruth7b560d42015-09-09 17:55:00 +0000150 for (const auto &AA : AAs) {
151 Result = ModRefInfo(Result & AA->getModRefInfo(CS, Loc));
Dan Gohman5f1702e2010-08-06 01:25:49 +0000152
Chandler Carruth7b560d42015-09-09 17:55:00 +0000153 // Early-exit the moment we reach the bottom of the lattice.
154 if (Result == MRI_NoModRef)
155 return Result;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000156 }
157
Chandler Carruth12884f72016-03-02 15:56:53 +0000158 // Try to refine the mod-ref info further using other API entry points to the
159 // aggregate set of AA results.
160 auto MRB = getModRefBehavior(CS);
Andrew Kaylor9604f342016-11-08 21:07:42 +0000161 if (MRB == FMRB_DoesNotAccessMemory ||
162 MRB == FMRB_OnlyAccessesInaccessibleMem)
Chandler Carruth12884f72016-03-02 15:56:53 +0000163 return MRI_NoModRef;
164
165 if (onlyReadsMemory(MRB))
166 Result = ModRefInfo(Result & MRI_Ref);
Nicolai Haehnle84c9f992016-07-04 08:01:29 +0000167 else if (doesNotReadMemory(MRB))
168 Result = ModRefInfo(Result & MRI_Mod);
Chandler Carruth12884f72016-03-02 15:56:53 +0000169
Andrew Kaylor9604f342016-11-08 21:07:42 +0000170 if (onlyAccessesArgPointees(MRB) || onlyAccessesInaccessibleOrArgMem(MRB)) {
Chandler Carruth12884f72016-03-02 15:56:53 +0000171 bool DoesAlias = false;
172 ModRefInfo AllArgsMask = MRI_NoModRef;
173 if (doesAccessArgPointees(MRB)) {
174 for (auto AI = CS.arg_begin(), AE = CS.arg_end(); AI != AE; ++AI) {
175 const Value *Arg = *AI;
176 if (!Arg->getType()->isPointerTy())
177 continue;
178 unsigned ArgIdx = std::distance(CS.arg_begin(), AI);
179 MemoryLocation ArgLoc = MemoryLocation::getForArgument(CS, ArgIdx, TLI);
180 AliasResult ArgAlias = alias(ArgLoc, Loc);
181 if (ArgAlias != NoAlias) {
182 ModRefInfo ArgMask = getArgModRefInfo(CS, ArgIdx);
183 DoesAlias = true;
184 AllArgsMask = ModRefInfo(AllArgsMask | ArgMask);
185 }
186 }
187 }
188 if (!DoesAlias)
189 return MRI_NoModRef;
190 Result = ModRefInfo(Result & AllArgsMask);
191 }
192
193 // If Loc is a constant memory location, the call definitely could not
194 // modify the memory location.
195 if ((Result & MRI_Mod) &&
196 pointsToConstantMemory(Loc, /*OrLocal*/ false))
197 Result = ModRefInfo(Result & ~MRI_Mod);
198
Chandler Carruth7b560d42015-09-09 17:55:00 +0000199 return Result;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000200}
201
Chandler Carruth7b560d42015-09-09 17:55:00 +0000202ModRefInfo AAResults::getModRefInfo(ImmutableCallSite CS1,
203 ImmutableCallSite CS2) {
204 ModRefInfo Result = MRI_ModRef;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000205
Chandler Carruth7b560d42015-09-09 17:55:00 +0000206 for (const auto &AA : AAs) {
207 Result = ModRefInfo(Result & AA->getModRefInfo(CS1, CS2));
Dan Gohman5f1702e2010-08-06 01:25:49 +0000208
Chandler Carruth7b560d42015-09-09 17:55:00 +0000209 // Early-exit the moment we reach the bottom of the lattice.
210 if (Result == MRI_NoModRef)
211 return Result;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000212 }
213
Chandler Carruth12884f72016-03-02 15:56:53 +0000214 // Try to refine the mod-ref info further using other API entry points to the
215 // aggregate set of AA results.
216
217 // If CS1 or CS2 are readnone, they don't interact.
218 auto CS1B = getModRefBehavior(CS1);
219 if (CS1B == FMRB_DoesNotAccessMemory)
220 return MRI_NoModRef;
221
222 auto CS2B = getModRefBehavior(CS2);
223 if (CS2B == FMRB_DoesNotAccessMemory)
224 return MRI_NoModRef;
225
226 // If they both only read from memory, there is no dependence.
227 if (onlyReadsMemory(CS1B) && onlyReadsMemory(CS2B))
228 return MRI_NoModRef;
229
230 // If CS1 only reads memory, the only dependence on CS2 can be
231 // from CS1 reading memory written by CS2.
232 if (onlyReadsMemory(CS1B))
233 Result = ModRefInfo(Result & MRI_Ref);
Nicolai Haehnle84c9f992016-07-04 08:01:29 +0000234 else if (doesNotReadMemory(CS1B))
235 Result = ModRefInfo(Result & MRI_Mod);
Chandler Carruth12884f72016-03-02 15:56:53 +0000236
237 // If CS2 only access memory through arguments, accumulate the mod/ref
238 // information from CS1's references to the memory referenced by
239 // CS2's arguments.
240 if (onlyAccessesArgPointees(CS2B)) {
241 ModRefInfo R = MRI_NoModRef;
242 if (doesAccessArgPointees(CS2B)) {
243 for (auto I = CS2.arg_begin(), E = CS2.arg_end(); I != E; ++I) {
244 const Value *Arg = *I;
245 if (!Arg->getType()->isPointerTy())
246 continue;
247 unsigned CS2ArgIdx = std::distance(CS2.arg_begin(), I);
248 auto CS2ArgLoc = MemoryLocation::getForArgument(CS2, CS2ArgIdx, TLI);
249
250 // ArgMask indicates what CS2 might do to CS2ArgLoc, and the dependence
251 // of CS1 on that location is the inverse.
252 ModRefInfo ArgMask = getArgModRefInfo(CS2, CS2ArgIdx);
253 if (ArgMask == MRI_Mod)
254 ArgMask = MRI_ModRef;
255 else if (ArgMask == MRI_Ref)
256 ArgMask = MRI_Mod;
257
258 ArgMask = ModRefInfo(ArgMask & getModRefInfo(CS1, CS2ArgLoc));
259
260 R = ModRefInfo((R | ArgMask) & Result);
261 if (R == Result)
262 break;
263 }
264 }
265 return R;
266 }
267
268 // If CS1 only accesses memory through arguments, check if CS2 references
269 // any of the memory referenced by CS1's arguments. If not, return NoModRef.
270 if (onlyAccessesArgPointees(CS1B)) {
271 ModRefInfo R = MRI_NoModRef;
272 if (doesAccessArgPointees(CS1B)) {
273 for (auto I = CS1.arg_begin(), E = CS1.arg_end(); I != E; ++I) {
274 const Value *Arg = *I;
275 if (!Arg->getType()->isPointerTy())
276 continue;
277 unsigned CS1ArgIdx = std::distance(CS1.arg_begin(), I);
278 auto CS1ArgLoc = MemoryLocation::getForArgument(CS1, CS1ArgIdx, TLI);
279
280 // ArgMask indicates what CS1 might do to CS1ArgLoc; if CS1 might Mod
281 // CS1ArgLoc, then we care about either a Mod or a Ref by CS2. If CS1
282 // might Ref, then we care only about a Mod by CS2.
283 ModRefInfo ArgMask = getArgModRefInfo(CS1, CS1ArgIdx);
284 ModRefInfo ArgR = getModRefInfo(CS2, CS1ArgLoc);
285 if (((ArgMask & MRI_Mod) != MRI_NoModRef &&
286 (ArgR & MRI_ModRef) != MRI_NoModRef) ||
287 ((ArgMask & MRI_Ref) != MRI_NoModRef &&
288 (ArgR & MRI_Mod) != MRI_NoModRef))
289 R = ModRefInfo((R | ArgMask) & Result);
290
291 if (R == Result)
292 break;
293 }
294 }
295 return R;
296 }
297
Chandler Carruth7b560d42015-09-09 17:55:00 +0000298 return Result;
299}
Chandler Carruthc41404a2015-06-17 07:12:40 +0000300
Chandler Carruth7b560d42015-09-09 17:55:00 +0000301FunctionModRefBehavior AAResults::getModRefBehavior(ImmutableCallSite CS) {
302 FunctionModRefBehavior Result = FMRB_UnknownModRefBehavior;
Hal Finkel354e23b2014-07-17 01:28:25 +0000303
Chandler Carruth7b560d42015-09-09 17:55:00 +0000304 for (const auto &AA : AAs) {
305 Result = FunctionModRefBehavior(Result & AA->getModRefBehavior(CS));
306
307 // Early-exit the moment we reach the bottom of the lattice.
308 if (Result == FMRB_DoesNotAccessMemory)
309 return Result;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000310 }
311
Chandler Carruth7b560d42015-09-09 17:55:00 +0000312 return Result;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000313}
314
Chandler Carruth7b560d42015-09-09 17:55:00 +0000315FunctionModRefBehavior AAResults::getModRefBehavior(const Function *F) {
316 FunctionModRefBehavior Result = FMRB_UnknownModRefBehavior;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000317
Chandler Carruth7b560d42015-09-09 17:55:00 +0000318 for (const auto &AA : AAs) {
319 Result = FunctionModRefBehavior(Result & AA->getModRefBehavior(F));
Dan Gohman5f1702e2010-08-06 01:25:49 +0000320
Chandler Carruth7b560d42015-09-09 17:55:00 +0000321 // Early-exit the moment we reach the bottom of the lattice.
322 if (Result == FMRB_DoesNotAccessMemory)
323 return Result;
324 }
Dan Gohman5f1702e2010-08-06 01:25:49 +0000325
Chandler Carruth7b560d42015-09-09 17:55:00 +0000326 return Result;
Chris Lattner62c37002004-05-23 21:15:48 +0000327}
328
Chris Lattner62c37002004-05-23 21:15:48 +0000329//===----------------------------------------------------------------------===//
Chandler Carruth7b560d42015-09-09 17:55:00 +0000330// Helper method implementation
Chris Lattner62c37002004-05-23 21:15:48 +0000331//===----------------------------------------------------------------------===//
332
Chandler Carruth7b560d42015-09-09 17:55:00 +0000333ModRefInfo AAResults::getModRefInfo(const LoadInst *L,
334 const MemoryLocation &Loc) {
Eli Friedman5494ada2011-08-15 20:54:19 +0000335 // Be conservative in the face of volatile/atomic.
336 if (!L->isUnordered())
Chandler Carruth194f59c2015-07-22 23:15:57 +0000337 return MRI_ModRef;
Dan Gohman6b4671b2010-08-06 18:11:28 +0000338
Dan Gohman52f9d7d2010-08-03 17:27:43 +0000339 // If the load address doesn't alias the given address, it doesn't read
340 // or write the specified memory.
Chandler Carruth70c61c12015-06-04 02:03:15 +0000341 if (Loc.Ptr && !alias(MemoryLocation::get(L), Loc))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000342 return MRI_NoModRef;
Dan Gohman52f9d7d2010-08-03 17:27:43 +0000343
Dan Gohman52f9d7d2010-08-03 17:27:43 +0000344 // Otherwise, a load just reads.
Chandler Carruth194f59c2015-07-22 23:15:57 +0000345 return MRI_Ref;
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000346}
347
Chandler Carruth7b560d42015-09-09 17:55:00 +0000348ModRefInfo AAResults::getModRefInfo(const StoreInst *S,
349 const MemoryLocation &Loc) {
Eli Friedman5494ada2011-08-15 20:54:19 +0000350 // Be conservative in the face of volatile/atomic.
351 if (!S->isUnordered())
Chandler Carruth194f59c2015-07-22 23:15:57 +0000352 return MRI_ModRef;
Dan Gohman6b4671b2010-08-06 18:11:28 +0000353
Daniel Berlinb2d22762015-04-13 23:05:45 +0000354 if (Loc.Ptr) {
355 // If the store address cannot alias the pointer in question, then the
356 // specified memory cannot be modified by the store.
Chandler Carruth70c61c12015-06-04 02:03:15 +0000357 if (!alias(MemoryLocation::get(S), Loc))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000358 return MRI_NoModRef;
Chris Lattner96055762004-01-30 22:16:42 +0000359
Daniel Berlinb2d22762015-04-13 23:05:45 +0000360 // If the pointer is a pointer to constant memory, then it could not have
361 // been modified by this store.
362 if (pointsToConstantMemory(Loc))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000363 return MRI_NoModRef;
Daniel Berlinb2d22762015-04-13 23:05:45 +0000364 }
Dan Gohman52f9d7d2010-08-03 17:27:43 +0000365
366 // Otherwise, a store just writes.
Chandler Carruth194f59c2015-07-22 23:15:57 +0000367 return MRI_Mod;
Chris Lattner3a311832003-02-26 19:26:51 +0000368}
369
Chandler Carruth7b560d42015-09-09 17:55:00 +0000370ModRefInfo AAResults::getModRefInfo(const VAArgInst *V,
371 const MemoryLocation &Loc) {
Dan Gohmane68958f2010-08-06 18:24:38 +0000372
Daniel Berlinec1de3f2015-04-28 19:19:14 +0000373 if (Loc.Ptr) {
374 // If the va_arg address cannot alias the pointer in question, then the
375 // specified memory cannot be accessed by the va_arg.
Chandler Carruth70c61c12015-06-04 02:03:15 +0000376 if (!alias(MemoryLocation::get(V), Loc))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000377 return MRI_NoModRef;
Daniel Berlinec1de3f2015-04-28 19:19:14 +0000378
379 // If the pointer is a pointer to constant memory, then it could not have
380 // been modified by this va_arg.
381 if (pointsToConstantMemory(Loc))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000382 return MRI_NoModRef;
Daniel Berlinec1de3f2015-04-28 19:19:14 +0000383 }
Dan Gohmane68958f2010-08-06 18:24:38 +0000384
385 // Otherwise, a va_arg reads and writes.
Chandler Carruth194f59c2015-07-22 23:15:57 +0000386 return MRI_ModRef;
Dan Gohmane68958f2010-08-06 18:24:38 +0000387}
388
David Majnemer6727c012015-11-17 08:15:14 +0000389ModRefInfo AAResults::getModRefInfo(const CatchPadInst *CatchPad,
390 const MemoryLocation &Loc) {
391 if (Loc.Ptr) {
392 // If the pointer is a pointer to constant memory,
393 // then it could not have been modified by this catchpad.
394 if (pointsToConstantMemory(Loc))
395 return MRI_NoModRef;
396 }
397
398 // Otherwise, a catchpad reads and writes.
399 return MRI_ModRef;
400}
401
402ModRefInfo AAResults::getModRefInfo(const CatchReturnInst *CatchRet,
403 const MemoryLocation &Loc) {
404 if (Loc.Ptr) {
405 // If the pointer is a pointer to constant memory,
406 // then it could not have been modified by this catchpad.
407 if (pointsToConstantMemory(Loc))
408 return MRI_NoModRef;
409 }
410
411 // Otherwise, a catchret reads and writes.
412 return MRI_ModRef;
413}
414
Chandler Carruth7b560d42015-09-09 17:55:00 +0000415ModRefInfo AAResults::getModRefInfo(const AtomicCmpXchgInst *CX,
416 const MemoryLocation &Loc) {
Eli Friedman5c918912011-09-26 20:15:28 +0000417 // Acquire/Release cmpxchg has properties that matter for arbitrary addresses.
JF Bastien800f87a2016-04-06 21:19:33 +0000418 if (isStrongerThanMonotonic(CX->getSuccessOrdering()))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000419 return MRI_ModRef;
Eli Friedman5c918912011-09-26 20:15:28 +0000420
421 // If the cmpxchg address does not alias the location, it does not access it.
Chandler Carruth70c61c12015-06-04 02:03:15 +0000422 if (Loc.Ptr && !alias(MemoryLocation::get(CX), Loc))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000423 return MRI_NoModRef;
Eli Friedman5c918912011-09-26 20:15:28 +0000424
Chandler Carruth194f59c2015-07-22 23:15:57 +0000425 return MRI_ModRef;
Eli Friedman5c918912011-09-26 20:15:28 +0000426}
427
Chandler Carruth7b560d42015-09-09 17:55:00 +0000428ModRefInfo AAResults::getModRefInfo(const AtomicRMWInst *RMW,
429 const MemoryLocation &Loc) {
Eli Friedman5c918912011-09-26 20:15:28 +0000430 // Acquire/Release atomicrmw has properties that matter for arbitrary addresses.
JF Bastien800f87a2016-04-06 21:19:33 +0000431 if (isStrongerThanMonotonic(RMW->getOrdering()))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000432 return MRI_ModRef;
Eli Friedman5c918912011-09-26 20:15:28 +0000433
434 // If the atomicrmw address does not alias the location, it does not access it.
Chandler Carruth70c61c12015-06-04 02:03:15 +0000435 if (Loc.Ptr && !alias(MemoryLocation::get(RMW), Loc))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000436 return MRI_NoModRef;
Eli Friedman5c918912011-09-26 20:15:28 +0000437
Chandler Carruth194f59c2015-07-22 23:15:57 +0000438 return MRI_ModRef;
Eli Friedman5c918912011-09-26 20:15:28 +0000439}
440
Bruno Cardoso Lopesdfc1d962015-07-31 14:31:35 +0000441/// \brief Return information about whether a particular call site modifies
442/// or reads the specified memory location \p MemLoc before instruction \p I
443/// in a BasicBlock. A ordered basic block \p OBB can be used to speed up
444/// instruction-ordering queries inside the BasicBlock containing \p I.
445/// FIXME: this is really just shoring-up a deficiency in alias analysis.
446/// BasicAA isn't willing to spend linear time determining whether an alloca
447/// was captured before or after this particular call, while we are. However,
448/// with a smarter AA in place, this test is just wasting compile time.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000449ModRefInfo AAResults::callCapturesBefore(const Instruction *I,
450 const MemoryLocation &MemLoc,
451 DominatorTree *DT,
452 OrderedBasicBlock *OBB) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000453 if (!DT)
Chandler Carruth194f59c2015-07-22 23:15:57 +0000454 return MRI_ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000455
Chandler Carruth7b560d42015-09-09 17:55:00 +0000456 const Value *Object =
457 GetUnderlyingObject(MemLoc.Ptr, I->getModule()->getDataLayout());
Chad Rosiera968caf2012-05-14 20:35:04 +0000458 if (!isIdentifiedObject(Object) || isa<GlobalValue>(Object) ||
459 isa<Constant>(Object))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000460 return MRI_ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000461
462 ImmutableCallSite CS(I);
463 if (!CS.getInstruction() || CS.getInstruction() == Object)
Chandler Carruth194f59c2015-07-22 23:15:57 +0000464 return MRI_ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000465
Hal Finkelb0356212014-07-21 13:15:48 +0000466 if (llvm::PointerMayBeCapturedBefore(Object, /* ReturnCaptures */ true,
Hal Finkeld32803b2014-07-21 21:30:22 +0000467 /* StoreCaptures */ true, I, DT,
Bruno Cardoso Lopesdfc1d962015-07-31 14:31:35 +0000468 /* include Object */ true,
469 /* OrderedBasicBlock */ OBB))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000470 return MRI_ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000471
472 unsigned ArgNo = 0;
Chandler Carruth194f59c2015-07-22 23:15:57 +0000473 ModRefInfo R = MRI_NoModRef;
Sanjoy Dasd0bdf3e2016-06-13 19:55:04 +0000474 for (auto CI = CS.data_operands_begin(), CE = CS.data_operands_end();
Chad Rosiera968caf2012-05-14 20:35:04 +0000475 CI != CE; ++CI, ++ArgNo) {
476 // Only look at the no-capture or byval pointer arguments. If this
477 // pointer were passed to arguments that were neither of these, then it
478 // couldn't be no-capture.
479 if (!(*CI)->getType()->isPointerTy() ||
Hal Finkel39fed392016-12-15 05:09:15 +0000480 (!CS.doesNotCapture(ArgNo) &&
481 ArgNo < CS.getNumArgOperands() && !CS.isByValArgument(ArgNo)))
Chad Rosiera968caf2012-05-14 20:35:04 +0000482 continue;
483
484 // If this is a no-capture pointer argument, see if we can tell that it
485 // is impossible to alias the pointer we're checking. If not, we have to
486 // assume that the call could touch the pointer, even though it doesn't
487 // escape.
Chandler Carruthac80dc72015-06-17 07:18:54 +0000488 if (isNoAlias(MemoryLocation(*CI), MemoryLocation(Object)))
Nick Lewyckyc0514622013-07-07 10:15:16 +0000489 continue;
Hal Finkelf19e1142016-12-15 05:50:45 +0000490 if (CS.doesNotAccessMemory(ArgNo))
Nick Lewyckyc0514622013-07-07 10:15:16 +0000491 continue;
Hal Finkelf19e1142016-12-15 05:50:45 +0000492 if (CS.onlyReadsMemory(ArgNo)) {
Chandler Carruth194f59c2015-07-22 23:15:57 +0000493 R = MRI_Ref;
Nick Lewyckyc0514622013-07-07 10:15:16 +0000494 continue;
Chad Rosiera968caf2012-05-14 20:35:04 +0000495 }
Chandler Carruth194f59c2015-07-22 23:15:57 +0000496 return MRI_ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000497 }
Nick Lewyckyc0514622013-07-07 10:15:16 +0000498 return R;
Chad Rosiera968caf2012-05-14 20:35:04 +0000499}
Eli Friedman5c918912011-09-26 20:15:28 +0000500
Chris Lattnerd922a842002-08-22 22:46:39 +0000501/// canBasicBlockModify - Return true if it is possible for execution of the
Elena Demikhovskya5599bf2014-12-15 14:09:53 +0000502/// specified basic block to modify the location Loc.
Chris Lattnerd922a842002-08-22 22:46:39 +0000503///
Chandler Carruth7b560d42015-09-09 17:55:00 +0000504bool AAResults::canBasicBlockModify(const BasicBlock &BB,
505 const MemoryLocation &Loc) {
Chandler Carruth194f59c2015-07-22 23:15:57 +0000506 return canInstructionRangeModRef(BB.front(), BB.back(), Loc, MRI_Mod);
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000507}
508
Elena Demikhovskya5599bf2014-12-15 14:09:53 +0000509/// canInstructionRangeModRef - Return true if it is possible for the
510/// execution of the specified instructions to mod\ref (according to the
511/// mode) the location Loc. The instructions to consider are all
512/// of the instructions in the range of [I1,I2] INCLUSIVE.
Teresa Johnsonbbcf75e2015-05-13 15:04:14 +0000513/// I1 and I2 must be in the same basic block.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000514bool AAResults::canInstructionRangeModRef(const Instruction &I1,
515 const Instruction &I2,
516 const MemoryLocation &Loc,
517 const ModRefInfo Mode) {
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000518 assert(I1.getParent() == I2.getParent() &&
519 "Instructions not in same basic block!");
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000520 BasicBlock::const_iterator I = I1.getIterator();
521 BasicBlock::const_iterator E = I2.getIterator();
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000522 ++E; // Convert from inclusive to exclusive range.
523
Chris Lattner3a311832003-02-26 19:26:51 +0000524 for (; I != E; ++I) // Check every instruction in range
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000525 if (getModRefInfo(&*I, Loc) & Mode)
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000526 return true;
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000527 return false;
528}
529
Chandler Carruth7b560d42015-09-09 17:55:00 +0000530// Provide a definition for the root virtual destructor.
531AAResults::Concept::~Concept() {}
532
NAKAMURA Takumidf0cd722016-02-28 17:17:00 +0000533// Provide a definition for the static object used to identify passes.
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000534AnalysisKey AAManager::Key;
NAKAMURA Takumidf0cd722016-02-28 17:17:00 +0000535
Chandler Carruth2be10752015-10-21 12:15:19 +0000536namespace {
537/// A wrapper pass for external alias analyses. This just squirrels away the
538/// callback used to run any analyses and register their results.
539struct ExternalAAWrapperPass : ImmutablePass {
540 typedef std::function<void(Pass &, Function &, AAResults &)> CallbackT;
541
542 CallbackT CB;
543
544 static char ID;
545
546 ExternalAAWrapperPass() : ImmutablePass(ID) {
547 initializeExternalAAWrapperPassPass(*PassRegistry::getPassRegistry());
548 }
549 explicit ExternalAAWrapperPass(CallbackT CB)
550 : ImmutablePass(ID), CB(std::move(CB)) {
551 initializeExternalAAWrapperPassPass(*PassRegistry::getPassRegistry());
552 }
553
554 void getAnalysisUsage(AnalysisUsage &AU) const override {
555 AU.setPreservesAll();
556 }
557};
558}
559
560char ExternalAAWrapperPass::ID = 0;
561INITIALIZE_PASS(ExternalAAWrapperPass, "external-aa", "External Alias Analysis",
562 false, true)
563
564ImmutablePass *
565llvm::createExternalAAWrapperPass(ExternalAAWrapperPass::CallbackT Callback) {
566 return new ExternalAAWrapperPass(std::move(Callback));
567}
568
Chandler Carruth7b560d42015-09-09 17:55:00 +0000569AAResultsWrapperPass::AAResultsWrapperPass() : FunctionPass(ID) {
570 initializeAAResultsWrapperPassPass(*PassRegistry::getPassRegistry());
571}
572
573char AAResultsWrapperPass::ID = 0;
574
575INITIALIZE_PASS_BEGIN(AAResultsWrapperPass, "aa",
576 "Function Alias Analysis Results", false, true)
577INITIALIZE_PASS_DEPENDENCY(BasicAAWrapperPass)
George Burgess IVbfa401e2016-07-06 00:26:41 +0000578INITIALIZE_PASS_DEPENDENCY(CFLAndersAAWrapperPass)
579INITIALIZE_PASS_DEPENDENCY(CFLSteensAAWrapperPass)
Chandler Carruth2be10752015-10-21 12:15:19 +0000580INITIALIZE_PASS_DEPENDENCY(ExternalAAWrapperPass)
Chandler Carruth7b560d42015-09-09 17:55:00 +0000581INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass)
582INITIALIZE_PASS_DEPENDENCY(ObjCARCAAWrapperPass)
583INITIALIZE_PASS_DEPENDENCY(SCEVAAWrapperPass)
584INITIALIZE_PASS_DEPENDENCY(ScopedNoAliasAAWrapperPass)
585INITIALIZE_PASS_DEPENDENCY(TypeBasedAAWrapperPass)
586INITIALIZE_PASS_END(AAResultsWrapperPass, "aa",
587 "Function Alias Analysis Results", false, true)
588
589FunctionPass *llvm::createAAResultsWrapperPass() {
590 return new AAResultsWrapperPass();
591}
592
593/// Run the wrapper pass to rebuild an aggregation over known AA passes.
594///
595/// This is the legacy pass manager's interface to the new-style AA results
596/// aggregation object. Because this is somewhat shoe-horned into the legacy
597/// pass manager, we hard code all the specific alias analyses available into
598/// it. While the particular set enabled is configured via commandline flags,
599/// adding a new alias analysis to LLVM will require adding support for it to
600/// this list.
601bool AAResultsWrapperPass::runOnFunction(Function &F) {
602 // NB! This *must* be reset before adding new AA results to the new
603 // AAResults object because in the legacy pass manager, each instance
604 // of these will refer to the *same* immutable analyses, registering and
605 // unregistering themselves with them. We need to carefully tear down the
606 // previous object first, in this case replacing it with an empty one, before
607 // registering new results.
Chandler Carruth12884f72016-03-02 15:56:53 +0000608 AAR.reset(
609 new AAResults(getAnalysis<TargetLibraryInfoWrapperPass>().getTLI()));
Chandler Carruth7b560d42015-09-09 17:55:00 +0000610
611 // BasicAA is always available for function analyses. Also, we add it first
612 // so that it can trump TBAA results when it proves MustAlias.
613 // FIXME: TBAA should have an explicit mode to support this and then we
614 // should reconsider the ordering here.
615 if (!DisableBasicAA)
616 AAR->addAAResult(getAnalysis<BasicAAWrapperPass>().getResult());
617
618 // Populate the results with the currently available AAs.
619 if (auto *WrapperPass = getAnalysisIfAvailable<ScopedNoAliasAAWrapperPass>())
620 AAR->addAAResult(WrapperPass->getResult());
621 if (auto *WrapperPass = getAnalysisIfAvailable<TypeBasedAAWrapperPass>())
622 AAR->addAAResult(WrapperPass->getResult());
623 if (auto *WrapperPass =
624 getAnalysisIfAvailable<objcarc::ObjCARCAAWrapperPass>())
625 AAR->addAAResult(WrapperPass->getResult());
626 if (auto *WrapperPass = getAnalysisIfAvailable<GlobalsAAWrapperPass>())
627 AAR->addAAResult(WrapperPass->getResult());
628 if (auto *WrapperPass = getAnalysisIfAvailable<SCEVAAWrapperPass>())
629 AAR->addAAResult(WrapperPass->getResult());
George Burgess IVbfa401e2016-07-06 00:26:41 +0000630 if (auto *WrapperPass = getAnalysisIfAvailable<CFLAndersAAWrapperPass>())
631 AAR->addAAResult(WrapperPass->getResult());
632 if (auto *WrapperPass = getAnalysisIfAvailable<CFLSteensAAWrapperPass>())
Chandler Carruth7b560d42015-09-09 17:55:00 +0000633 AAR->addAAResult(WrapperPass->getResult());
634
Chandler Carruth2be10752015-10-21 12:15:19 +0000635 // If available, run an external AA providing callback over the results as
636 // well.
637 if (auto *WrapperPass = getAnalysisIfAvailable<ExternalAAWrapperPass>())
638 if (WrapperPass->CB)
639 WrapperPass->CB(*this, F, *AAR);
640
Chandler Carruth7b560d42015-09-09 17:55:00 +0000641 // Analyses don't mutate the IR, so return false.
642 return false;
643}
644
645void AAResultsWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
646 AU.setPreservesAll();
647 AU.addRequired<BasicAAWrapperPass>();
Chandler Carruth12884f72016-03-02 15:56:53 +0000648 AU.addRequired<TargetLibraryInfoWrapperPass>();
Chandler Carruth7b560d42015-09-09 17:55:00 +0000649
650 // We also need to mark all the alias analysis passes we will potentially
651 // probe in runOnFunction as used here to ensure the legacy pass manager
652 // preserves them. This hard coding of lists of alias analyses is specific to
653 // the legacy pass manager.
654 AU.addUsedIfAvailable<ScopedNoAliasAAWrapperPass>();
655 AU.addUsedIfAvailable<TypeBasedAAWrapperPass>();
656 AU.addUsedIfAvailable<objcarc::ObjCARCAAWrapperPass>();
657 AU.addUsedIfAvailable<GlobalsAAWrapperPass>();
658 AU.addUsedIfAvailable<SCEVAAWrapperPass>();
George Burgess IVbfa401e2016-07-06 00:26:41 +0000659 AU.addUsedIfAvailable<CFLAndersAAWrapperPass>();
660 AU.addUsedIfAvailable<CFLSteensAAWrapperPass>();
Chandler Carruth7b560d42015-09-09 17:55:00 +0000661}
662
663AAResults llvm::createLegacyPMAAResults(Pass &P, Function &F,
664 BasicAAResult &BAR) {
Chandler Carruth12884f72016-03-02 15:56:53 +0000665 AAResults AAR(P.getAnalysis<TargetLibraryInfoWrapperPass>().getTLI());
Chandler Carruth7b560d42015-09-09 17:55:00 +0000666
667 // Add in our explicitly constructed BasicAA results.
668 if (!DisableBasicAA)
669 AAR.addAAResult(BAR);
670
671 // Populate the results with the other currently available AAs.
672 if (auto *WrapperPass =
673 P.getAnalysisIfAvailable<ScopedNoAliasAAWrapperPass>())
674 AAR.addAAResult(WrapperPass->getResult());
675 if (auto *WrapperPass = P.getAnalysisIfAvailable<TypeBasedAAWrapperPass>())
676 AAR.addAAResult(WrapperPass->getResult());
677 if (auto *WrapperPass =
678 P.getAnalysisIfAvailable<objcarc::ObjCARCAAWrapperPass>())
679 AAR.addAAResult(WrapperPass->getResult());
680 if (auto *WrapperPass = P.getAnalysisIfAvailable<GlobalsAAWrapperPass>())
681 AAR.addAAResult(WrapperPass->getResult());
George Burgess IVbfa401e2016-07-06 00:26:41 +0000682 if (auto *WrapperPass = P.getAnalysisIfAvailable<CFLAndersAAWrapperPass>())
683 AAR.addAAResult(WrapperPass->getResult());
684 if (auto *WrapperPass = P.getAnalysisIfAvailable<CFLSteensAAWrapperPass>())
Chandler Carruth7b560d42015-09-09 17:55:00 +0000685 AAR.addAAResult(WrapperPass->getResult());
686
687 return AAR;
688}
689
Dan Gohmanfb306c02009-02-03 01:28:32 +0000690bool llvm::isNoAliasCall(const Value *V) {
Benjamin Kramer45f39542015-08-05 14:16:44 +0000691 if (auto CS = ImmutableCallSite(V))
692 return CS.paramHasAttr(0, Attribute::NoAlias);
Dan Gohmanfb306c02009-02-03 01:28:32 +0000693 return false;
694}
695
Sanjay Pateld7f613d2016-01-13 22:17:13 +0000696bool llvm::isNoAliasArgument(const Value *V) {
Michael Kupersteinf3e663a2013-05-28 08:17:48 +0000697 if (const Argument *A = dyn_cast<Argument>(V))
698 return A->hasNoAliasAttr();
699 return false;
700}
701
Dan Gohman00ef9322010-07-07 14:27:09 +0000702bool llvm::isIdentifiedObject(const Value *V) {
Dan Gohman0824aff2010-06-29 00:50:39 +0000703 if (isa<AllocaInst>(V))
Dan Gohman1f59f012009-08-27 17:52:56 +0000704 return true;
705 if (isa<GlobalValue>(V) && !isa<GlobalAlias>(V))
Dan Gohmanfb306c02009-02-03 01:28:32 +0000706 return true;
Dan Gohman00ef9322010-07-07 14:27:09 +0000707 if (isNoAliasCall(V))
708 return true;
709 if (const Argument *A = dyn_cast<Argument>(V))
710 return A->hasNoAliasAttr() || A->hasByValAttr();
Dan Gohmanfb306c02009-02-03 01:28:32 +0000711 return false;
712}
Hal Finkelc782aa52014-07-21 12:27:23 +0000713
Sanjay Pateld7f613d2016-01-13 22:17:13 +0000714bool llvm::isIdentifiedFunctionLocal(const Value *V) {
Hal Finkelc782aa52014-07-21 12:27:23 +0000715 return isa<AllocaInst>(V) || isNoAliasCall(V) || isNoAliasArgument(V);
716}
Sanjoy Das1c481f52016-02-09 01:21:57 +0000717
Chandler Carruth12884f72016-03-02 15:56:53 +0000718void llvm::getAAResultsAnalysisUsage(AnalysisUsage &AU) {
Sanjoy Das1c481f52016-02-09 01:21:57 +0000719 // This function needs to be in sync with llvm::createLegacyPMAAResults -- if
720 // more alias analyses are added to llvm::createLegacyPMAAResults, they need
721 // to be added here also.
Chandler Carruth12884f72016-03-02 15:56:53 +0000722 AU.addRequired<TargetLibraryInfoWrapperPass>();
Sanjoy Das1c481f52016-02-09 01:21:57 +0000723 AU.addUsedIfAvailable<ScopedNoAliasAAWrapperPass>();
724 AU.addUsedIfAvailable<TypeBasedAAWrapperPass>();
725 AU.addUsedIfAvailable<objcarc::ObjCARCAAWrapperPass>();
726 AU.addUsedIfAvailable<GlobalsAAWrapperPass>();
George Burgess IVbfa401e2016-07-06 00:26:41 +0000727 AU.addUsedIfAvailable<CFLAndersAAWrapperPass>();
728 AU.addUsedIfAvailable<CFLSteensAAWrapperPass>();
Sanjoy Das1c481f52016-02-09 01:21:57 +0000729}