blob: f88e40adb90dc148d1c996255bc7704d0d34219b [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) {
75 if (PA.areAllPreserved())
76 return false; // Nothing to do, everything is still valid.
77
78 // Check if the AA manager itself has been invalidated.
79 auto PAC = PA.getChecker<AAManager>();
80 if (!PAC.preserved() && !PAC.preservedSet<AllAnalysesOn<Function>>())
81 return true; // The manager needs to be blown away, clear everything.
82
83 // Check all of the dependencies registered.
84 for (AnalysisKey *ID : AADeps)
85 if (Inv.invalidate(ID, F, PA))
86 return true;
87
88 // Everything we depend on is still fine, so are we. Nothing to invalidate.
89 return false;
90}
91
Chris Lattner62c37002004-05-23 21:15:48 +000092//===----------------------------------------------------------------------===//
93// Default chaining methods
94//===----------------------------------------------------------------------===//
95
Chandler Carruth7b560d42015-09-09 17:55:00 +000096AliasResult AAResults::alias(const MemoryLocation &LocA,
97 const MemoryLocation &LocB) {
98 for (const auto &AA : AAs) {
99 auto Result = AA->alias(LocA, LocB);
100 if (Result != MayAlias)
101 return Result;
102 }
103 return MayAlias;
Chris Lattner62c37002004-05-23 21:15:48 +0000104}
105
Chandler Carruth7b560d42015-09-09 17:55:00 +0000106bool AAResults::pointsToConstantMemory(const MemoryLocation &Loc,
107 bool OrLocal) {
108 for (const auto &AA : AAs)
109 if (AA->pointsToConstantMemory(Loc, OrLocal))
110 return true;
111
112 return false;
Chris Lattner62c37002004-05-23 21:15:48 +0000113}
114
Chandler Carruth7b560d42015-09-09 17:55:00 +0000115ModRefInfo AAResults::getArgModRefInfo(ImmutableCallSite CS, unsigned ArgIdx) {
116 ModRefInfo Result = MRI_ModRef;
117
118 for (const auto &AA : AAs) {
119 Result = ModRefInfo(Result & AA->getArgModRefInfo(CS, ArgIdx));
120
121 // Early-exit the moment we reach the bottom of the lattice.
122 if (Result == MRI_NoModRef)
123 return Result;
124 }
125
126 return Result;
Hal Finkel354e23b2014-07-17 01:28:25 +0000127}
128
Chandler Carruth7b560d42015-09-09 17:55:00 +0000129ModRefInfo AAResults::getModRefInfo(Instruction *I, ImmutableCallSite Call) {
Daniel Berlin8de312d2015-04-13 23:25:41 +0000130 // We may have two calls
131 if (auto CS = ImmutableCallSite(I)) {
132 // Check if the two calls modify the same memory
Nicolai Haehnle9343f362016-07-11 14:11:45 +0000133 return getModRefInfo(CS, Call);
David Majnemera940f362016-07-15 17:19:24 +0000134 } else if (I->isFenceLike()) {
135 // If this is a fence, just return MRI_ModRef.
136 return MRI_ModRef;
Daniel Berlin8de312d2015-04-13 23:25:41 +0000137 } else {
138 // Otherwise, check if the call modifies or references the
139 // location this memory access defines. The best we can say
140 // is that if the call references what this instruction
141 // defines, it must be clobbered by this location.
Chandler Carruthac80dc72015-06-17 07:18:54 +0000142 const MemoryLocation DefLoc = MemoryLocation::get(I);
Chandler Carruth194f59c2015-07-22 23:15:57 +0000143 if (getModRefInfo(Call, DefLoc) != MRI_NoModRef)
144 return MRI_ModRef;
Daniel Berlin8de312d2015-04-13 23:25:41 +0000145 }
Chandler Carruth194f59c2015-07-22 23:15:57 +0000146 return MRI_NoModRef;
Daniel Berlin8de312d2015-04-13 23:25:41 +0000147}
Owen Andersonb6e4ff02011-01-03 21:38:41 +0000148
Chandler Carruth7b560d42015-09-09 17:55:00 +0000149ModRefInfo AAResults::getModRefInfo(ImmutableCallSite CS,
150 const MemoryLocation &Loc) {
151 ModRefInfo Result = MRI_ModRef;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000152
Chandler Carruth7b560d42015-09-09 17:55:00 +0000153 for (const auto &AA : AAs) {
154 Result = ModRefInfo(Result & AA->getModRefInfo(CS, Loc));
Dan Gohman5f1702e2010-08-06 01:25:49 +0000155
Chandler Carruth7b560d42015-09-09 17:55:00 +0000156 // Early-exit the moment we reach the bottom of the lattice.
157 if (Result == MRI_NoModRef)
158 return Result;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000159 }
160
Chandler Carruth12884f72016-03-02 15:56:53 +0000161 // Try to refine the mod-ref info further using other API entry points to the
162 // aggregate set of AA results.
163 auto MRB = getModRefBehavior(CS);
Andrew Kaylor9604f342016-11-08 21:07:42 +0000164 if (MRB == FMRB_DoesNotAccessMemory ||
165 MRB == FMRB_OnlyAccessesInaccessibleMem)
Chandler Carruth12884f72016-03-02 15:56:53 +0000166 return MRI_NoModRef;
167
168 if (onlyReadsMemory(MRB))
169 Result = ModRefInfo(Result & MRI_Ref);
Nicolai Haehnle84c9f992016-07-04 08:01:29 +0000170 else if (doesNotReadMemory(MRB))
171 Result = ModRefInfo(Result & MRI_Mod);
Chandler Carruth12884f72016-03-02 15:56:53 +0000172
Andrew Kaylor9604f342016-11-08 21:07:42 +0000173 if (onlyAccessesArgPointees(MRB) || onlyAccessesInaccessibleOrArgMem(MRB)) {
Chandler Carruth12884f72016-03-02 15:56:53 +0000174 bool DoesAlias = false;
175 ModRefInfo AllArgsMask = MRI_NoModRef;
176 if (doesAccessArgPointees(MRB)) {
177 for (auto AI = CS.arg_begin(), AE = CS.arg_end(); AI != AE; ++AI) {
178 const Value *Arg = *AI;
179 if (!Arg->getType()->isPointerTy())
180 continue;
181 unsigned ArgIdx = std::distance(CS.arg_begin(), AI);
182 MemoryLocation ArgLoc = MemoryLocation::getForArgument(CS, ArgIdx, TLI);
183 AliasResult ArgAlias = alias(ArgLoc, Loc);
184 if (ArgAlias != NoAlias) {
185 ModRefInfo ArgMask = getArgModRefInfo(CS, ArgIdx);
186 DoesAlias = true;
187 AllArgsMask = ModRefInfo(AllArgsMask | ArgMask);
188 }
189 }
190 }
191 if (!DoesAlias)
192 return MRI_NoModRef;
193 Result = ModRefInfo(Result & AllArgsMask);
194 }
195
196 // If Loc is a constant memory location, the call definitely could not
197 // modify the memory location.
198 if ((Result & MRI_Mod) &&
199 pointsToConstantMemory(Loc, /*OrLocal*/ false))
200 Result = ModRefInfo(Result & ~MRI_Mod);
201
Chandler Carruth7b560d42015-09-09 17:55:00 +0000202 return Result;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000203}
204
Chandler Carruth7b560d42015-09-09 17:55:00 +0000205ModRefInfo AAResults::getModRefInfo(ImmutableCallSite CS1,
206 ImmutableCallSite CS2) {
207 ModRefInfo Result = MRI_ModRef;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000208
Chandler Carruth7b560d42015-09-09 17:55:00 +0000209 for (const auto &AA : AAs) {
210 Result = ModRefInfo(Result & AA->getModRefInfo(CS1, CS2));
Dan Gohman5f1702e2010-08-06 01:25:49 +0000211
Chandler Carruth7b560d42015-09-09 17:55:00 +0000212 // Early-exit the moment we reach the bottom of the lattice.
213 if (Result == MRI_NoModRef)
214 return Result;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000215 }
216
Chandler Carruth12884f72016-03-02 15:56:53 +0000217 // Try to refine the mod-ref info further using other API entry points to the
218 // aggregate set of AA results.
219
220 // If CS1 or CS2 are readnone, they don't interact.
221 auto CS1B = getModRefBehavior(CS1);
222 if (CS1B == FMRB_DoesNotAccessMemory)
223 return MRI_NoModRef;
224
225 auto CS2B = getModRefBehavior(CS2);
226 if (CS2B == FMRB_DoesNotAccessMemory)
227 return MRI_NoModRef;
228
229 // If they both only read from memory, there is no dependence.
230 if (onlyReadsMemory(CS1B) && onlyReadsMemory(CS2B))
231 return MRI_NoModRef;
232
233 // If CS1 only reads memory, the only dependence on CS2 can be
234 // from CS1 reading memory written by CS2.
235 if (onlyReadsMemory(CS1B))
236 Result = ModRefInfo(Result & MRI_Ref);
Nicolai Haehnle84c9f992016-07-04 08:01:29 +0000237 else if (doesNotReadMemory(CS1B))
238 Result = ModRefInfo(Result & MRI_Mod);
Chandler Carruth12884f72016-03-02 15:56:53 +0000239
240 // If CS2 only access memory through arguments, accumulate the mod/ref
241 // information from CS1's references to the memory referenced by
242 // CS2's arguments.
243 if (onlyAccessesArgPointees(CS2B)) {
244 ModRefInfo R = MRI_NoModRef;
245 if (doesAccessArgPointees(CS2B)) {
246 for (auto I = CS2.arg_begin(), E = CS2.arg_end(); I != E; ++I) {
247 const Value *Arg = *I;
248 if (!Arg->getType()->isPointerTy())
249 continue;
250 unsigned CS2ArgIdx = std::distance(CS2.arg_begin(), I);
251 auto CS2ArgLoc = MemoryLocation::getForArgument(CS2, CS2ArgIdx, TLI);
252
253 // ArgMask indicates what CS2 might do to CS2ArgLoc, and the dependence
254 // of CS1 on that location is the inverse.
255 ModRefInfo ArgMask = getArgModRefInfo(CS2, CS2ArgIdx);
256 if (ArgMask == MRI_Mod)
257 ArgMask = MRI_ModRef;
258 else if (ArgMask == MRI_Ref)
259 ArgMask = MRI_Mod;
260
261 ArgMask = ModRefInfo(ArgMask & getModRefInfo(CS1, CS2ArgLoc));
262
263 R = ModRefInfo((R | ArgMask) & Result);
264 if (R == Result)
265 break;
266 }
267 }
268 return R;
269 }
270
271 // If CS1 only accesses memory through arguments, check if CS2 references
272 // any of the memory referenced by CS1's arguments. If not, return NoModRef.
273 if (onlyAccessesArgPointees(CS1B)) {
274 ModRefInfo R = MRI_NoModRef;
275 if (doesAccessArgPointees(CS1B)) {
276 for (auto I = CS1.arg_begin(), E = CS1.arg_end(); I != E; ++I) {
277 const Value *Arg = *I;
278 if (!Arg->getType()->isPointerTy())
279 continue;
280 unsigned CS1ArgIdx = std::distance(CS1.arg_begin(), I);
281 auto CS1ArgLoc = MemoryLocation::getForArgument(CS1, CS1ArgIdx, TLI);
282
283 // ArgMask indicates what CS1 might do to CS1ArgLoc; if CS1 might Mod
284 // CS1ArgLoc, then we care about either a Mod or a Ref by CS2. If CS1
285 // might Ref, then we care only about a Mod by CS2.
286 ModRefInfo ArgMask = getArgModRefInfo(CS1, CS1ArgIdx);
287 ModRefInfo ArgR = getModRefInfo(CS2, CS1ArgLoc);
288 if (((ArgMask & MRI_Mod) != MRI_NoModRef &&
289 (ArgR & MRI_ModRef) != MRI_NoModRef) ||
290 ((ArgMask & MRI_Ref) != MRI_NoModRef &&
291 (ArgR & MRI_Mod) != MRI_NoModRef))
292 R = ModRefInfo((R | ArgMask) & Result);
293
294 if (R == Result)
295 break;
296 }
297 }
298 return R;
299 }
300
Chandler Carruth7b560d42015-09-09 17:55:00 +0000301 return Result;
302}
Chandler Carruthc41404a2015-06-17 07:12:40 +0000303
Chandler Carruth7b560d42015-09-09 17:55:00 +0000304FunctionModRefBehavior AAResults::getModRefBehavior(ImmutableCallSite CS) {
305 FunctionModRefBehavior Result = FMRB_UnknownModRefBehavior;
Hal Finkel354e23b2014-07-17 01:28:25 +0000306
Chandler Carruth7b560d42015-09-09 17:55:00 +0000307 for (const auto &AA : AAs) {
308 Result = FunctionModRefBehavior(Result & AA->getModRefBehavior(CS));
309
310 // Early-exit the moment we reach the bottom of the lattice.
311 if (Result == FMRB_DoesNotAccessMemory)
312 return Result;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000313 }
314
Chandler Carruth7b560d42015-09-09 17:55:00 +0000315 return Result;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000316}
317
Chandler Carruth7b560d42015-09-09 17:55:00 +0000318FunctionModRefBehavior AAResults::getModRefBehavior(const Function *F) {
319 FunctionModRefBehavior Result = FMRB_UnknownModRefBehavior;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000320
Chandler Carruth7b560d42015-09-09 17:55:00 +0000321 for (const auto &AA : AAs) {
322 Result = FunctionModRefBehavior(Result & AA->getModRefBehavior(F));
Dan Gohman5f1702e2010-08-06 01:25:49 +0000323
Chandler Carruth7b560d42015-09-09 17:55:00 +0000324 // Early-exit the moment we reach the bottom of the lattice.
325 if (Result == FMRB_DoesNotAccessMemory)
326 return Result;
327 }
Dan Gohman5f1702e2010-08-06 01:25:49 +0000328
Chandler Carruth7b560d42015-09-09 17:55:00 +0000329 return Result;
Chris Lattner62c37002004-05-23 21:15:48 +0000330}
331
Chris Lattner62c37002004-05-23 21:15:48 +0000332//===----------------------------------------------------------------------===//
Chandler Carruth7b560d42015-09-09 17:55:00 +0000333// Helper method implementation
Chris Lattner62c37002004-05-23 21:15:48 +0000334//===----------------------------------------------------------------------===//
335
Chandler Carruth7b560d42015-09-09 17:55:00 +0000336ModRefInfo AAResults::getModRefInfo(const LoadInst *L,
337 const MemoryLocation &Loc) {
Eli Friedman5494ada2011-08-15 20:54:19 +0000338 // Be conservative in the face of volatile/atomic.
339 if (!L->isUnordered())
Chandler Carruth194f59c2015-07-22 23:15:57 +0000340 return MRI_ModRef;
Dan Gohman6b4671b2010-08-06 18:11:28 +0000341
Dan Gohman52f9d7d2010-08-03 17:27:43 +0000342 // If the load address doesn't alias the given address, it doesn't read
343 // or write the specified memory.
Chandler Carruth70c61c12015-06-04 02:03:15 +0000344 if (Loc.Ptr && !alias(MemoryLocation::get(L), Loc))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000345 return MRI_NoModRef;
Dan Gohman52f9d7d2010-08-03 17:27:43 +0000346
Dan Gohman52f9d7d2010-08-03 17:27:43 +0000347 // Otherwise, a load just reads.
Chandler Carruth194f59c2015-07-22 23:15:57 +0000348 return MRI_Ref;
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000349}
350
Chandler Carruth7b560d42015-09-09 17:55:00 +0000351ModRefInfo AAResults::getModRefInfo(const StoreInst *S,
352 const MemoryLocation &Loc) {
Eli Friedman5494ada2011-08-15 20:54:19 +0000353 // Be conservative in the face of volatile/atomic.
354 if (!S->isUnordered())
Chandler Carruth194f59c2015-07-22 23:15:57 +0000355 return MRI_ModRef;
Dan Gohman6b4671b2010-08-06 18:11:28 +0000356
Daniel Berlinb2d22762015-04-13 23:05:45 +0000357 if (Loc.Ptr) {
358 // If the store address cannot alias the pointer in question, then the
359 // specified memory cannot be modified by the store.
Chandler Carruth70c61c12015-06-04 02:03:15 +0000360 if (!alias(MemoryLocation::get(S), Loc))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000361 return MRI_NoModRef;
Chris Lattner96055762004-01-30 22:16:42 +0000362
Daniel Berlinb2d22762015-04-13 23:05:45 +0000363 // If the pointer is a pointer to constant memory, then it could not have
364 // been modified by this store.
365 if (pointsToConstantMemory(Loc))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000366 return MRI_NoModRef;
Daniel Berlinb2d22762015-04-13 23:05:45 +0000367 }
Dan Gohman52f9d7d2010-08-03 17:27:43 +0000368
369 // Otherwise, a store just writes.
Chandler Carruth194f59c2015-07-22 23:15:57 +0000370 return MRI_Mod;
Chris Lattner3a311832003-02-26 19:26:51 +0000371}
372
Chandler Carruth7b560d42015-09-09 17:55:00 +0000373ModRefInfo AAResults::getModRefInfo(const VAArgInst *V,
374 const MemoryLocation &Loc) {
Dan Gohmane68958f2010-08-06 18:24:38 +0000375
Daniel Berlinec1de3f2015-04-28 19:19:14 +0000376 if (Loc.Ptr) {
377 // If the va_arg address cannot alias the pointer in question, then the
378 // specified memory cannot be accessed by the va_arg.
Chandler Carruth70c61c12015-06-04 02:03:15 +0000379 if (!alias(MemoryLocation::get(V), Loc))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000380 return MRI_NoModRef;
Daniel Berlinec1de3f2015-04-28 19:19:14 +0000381
382 // If the pointer is a pointer to constant memory, then it could not have
383 // been modified by this va_arg.
384 if (pointsToConstantMemory(Loc))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000385 return MRI_NoModRef;
Daniel Berlinec1de3f2015-04-28 19:19:14 +0000386 }
Dan Gohmane68958f2010-08-06 18:24:38 +0000387
388 // Otherwise, a va_arg reads and writes.
Chandler Carruth194f59c2015-07-22 23:15:57 +0000389 return MRI_ModRef;
Dan Gohmane68958f2010-08-06 18:24:38 +0000390}
391
David Majnemer6727c012015-11-17 08:15:14 +0000392ModRefInfo AAResults::getModRefInfo(const CatchPadInst *CatchPad,
393 const MemoryLocation &Loc) {
394 if (Loc.Ptr) {
395 // If the pointer is a pointer to constant memory,
396 // then it could not have been modified by this catchpad.
397 if (pointsToConstantMemory(Loc))
398 return MRI_NoModRef;
399 }
400
401 // Otherwise, a catchpad reads and writes.
402 return MRI_ModRef;
403}
404
405ModRefInfo AAResults::getModRefInfo(const CatchReturnInst *CatchRet,
406 const MemoryLocation &Loc) {
407 if (Loc.Ptr) {
408 // If the pointer is a pointer to constant memory,
409 // then it could not have been modified by this catchpad.
410 if (pointsToConstantMemory(Loc))
411 return MRI_NoModRef;
412 }
413
414 // Otherwise, a catchret reads and writes.
415 return MRI_ModRef;
416}
417
Chandler Carruth7b560d42015-09-09 17:55:00 +0000418ModRefInfo AAResults::getModRefInfo(const AtomicCmpXchgInst *CX,
419 const MemoryLocation &Loc) {
Eli Friedman5c918912011-09-26 20:15:28 +0000420 // Acquire/Release cmpxchg has properties that matter for arbitrary addresses.
JF Bastien800f87a2016-04-06 21:19:33 +0000421 if (isStrongerThanMonotonic(CX->getSuccessOrdering()))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000422 return MRI_ModRef;
Eli Friedman5c918912011-09-26 20:15:28 +0000423
424 // If the cmpxchg address does not alias the location, it does not access it.
Chandler Carruth70c61c12015-06-04 02:03:15 +0000425 if (Loc.Ptr && !alias(MemoryLocation::get(CX), Loc))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000426 return MRI_NoModRef;
Eli Friedman5c918912011-09-26 20:15:28 +0000427
Chandler Carruth194f59c2015-07-22 23:15:57 +0000428 return MRI_ModRef;
Eli Friedman5c918912011-09-26 20:15:28 +0000429}
430
Chandler Carruth7b560d42015-09-09 17:55:00 +0000431ModRefInfo AAResults::getModRefInfo(const AtomicRMWInst *RMW,
432 const MemoryLocation &Loc) {
Eli Friedman5c918912011-09-26 20:15:28 +0000433 // Acquire/Release atomicrmw has properties that matter for arbitrary addresses.
JF Bastien800f87a2016-04-06 21:19:33 +0000434 if (isStrongerThanMonotonic(RMW->getOrdering()))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000435 return MRI_ModRef;
Eli Friedman5c918912011-09-26 20:15:28 +0000436
437 // If the atomicrmw address does not alias the location, it does not access it.
Chandler Carruth70c61c12015-06-04 02:03:15 +0000438 if (Loc.Ptr && !alias(MemoryLocation::get(RMW), Loc))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000439 return MRI_NoModRef;
Eli Friedman5c918912011-09-26 20:15:28 +0000440
Chandler Carruth194f59c2015-07-22 23:15:57 +0000441 return MRI_ModRef;
Eli Friedman5c918912011-09-26 20:15:28 +0000442}
443
Bruno Cardoso Lopesdfc1d962015-07-31 14:31:35 +0000444/// \brief Return information about whether a particular call site modifies
445/// or reads the specified memory location \p MemLoc before instruction \p I
446/// in a BasicBlock. A ordered basic block \p OBB can be used to speed up
447/// instruction-ordering queries inside the BasicBlock containing \p I.
448/// FIXME: this is really just shoring-up a deficiency in alias analysis.
449/// BasicAA isn't willing to spend linear time determining whether an alloca
450/// was captured before or after this particular call, while we are. However,
451/// with a smarter AA in place, this test is just wasting compile time.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000452ModRefInfo AAResults::callCapturesBefore(const Instruction *I,
453 const MemoryLocation &MemLoc,
454 DominatorTree *DT,
455 OrderedBasicBlock *OBB) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000456 if (!DT)
Chandler Carruth194f59c2015-07-22 23:15:57 +0000457 return MRI_ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000458
Chandler Carruth7b560d42015-09-09 17:55:00 +0000459 const Value *Object =
460 GetUnderlyingObject(MemLoc.Ptr, I->getModule()->getDataLayout());
Chad Rosiera968caf2012-05-14 20:35:04 +0000461 if (!isIdentifiedObject(Object) || isa<GlobalValue>(Object) ||
462 isa<Constant>(Object))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000463 return MRI_ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000464
465 ImmutableCallSite CS(I);
466 if (!CS.getInstruction() || CS.getInstruction() == Object)
Chandler Carruth194f59c2015-07-22 23:15:57 +0000467 return MRI_ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000468
Hal Finkelb0356212014-07-21 13:15:48 +0000469 if (llvm::PointerMayBeCapturedBefore(Object, /* ReturnCaptures */ true,
Hal Finkeld32803b2014-07-21 21:30:22 +0000470 /* StoreCaptures */ true, I, DT,
Bruno Cardoso Lopesdfc1d962015-07-31 14:31:35 +0000471 /* include Object */ true,
472 /* OrderedBasicBlock */ OBB))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000473 return MRI_ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000474
475 unsigned ArgNo = 0;
Chandler Carruth194f59c2015-07-22 23:15:57 +0000476 ModRefInfo R = MRI_NoModRef;
Sanjoy Dasd0bdf3e2016-06-13 19:55:04 +0000477 for (auto CI = CS.data_operands_begin(), CE = CS.data_operands_end();
Chad Rosiera968caf2012-05-14 20:35:04 +0000478 CI != CE; ++CI, ++ArgNo) {
479 // Only look at the no-capture or byval pointer arguments. If this
480 // pointer were passed to arguments that were neither of these, then it
481 // couldn't be no-capture.
482 if (!(*CI)->getType()->isPointerTy() ||
Hal Finkel39fed392016-12-15 05:09:15 +0000483 (!CS.doesNotCapture(ArgNo) &&
484 ArgNo < CS.getNumArgOperands() && !CS.isByValArgument(ArgNo)))
Chad Rosiera968caf2012-05-14 20:35:04 +0000485 continue;
486
487 // If this is a no-capture pointer argument, see if we can tell that it
488 // is impossible to alias the pointer we're checking. If not, we have to
489 // assume that the call could touch the pointer, even though it doesn't
490 // escape.
Chandler Carruthac80dc72015-06-17 07:18:54 +0000491 if (isNoAlias(MemoryLocation(*CI), MemoryLocation(Object)))
Nick Lewyckyc0514622013-07-07 10:15:16 +0000492 continue;
Hal Finkelf19e1142016-12-15 05:50:45 +0000493 if (CS.doesNotAccessMemory(ArgNo))
Nick Lewyckyc0514622013-07-07 10:15:16 +0000494 continue;
Hal Finkelf19e1142016-12-15 05:50:45 +0000495 if (CS.onlyReadsMemory(ArgNo)) {
Chandler Carruth194f59c2015-07-22 23:15:57 +0000496 R = MRI_Ref;
Nick Lewyckyc0514622013-07-07 10:15:16 +0000497 continue;
Chad Rosiera968caf2012-05-14 20:35:04 +0000498 }
Chandler Carruth194f59c2015-07-22 23:15:57 +0000499 return MRI_ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000500 }
Nick Lewyckyc0514622013-07-07 10:15:16 +0000501 return R;
Chad Rosiera968caf2012-05-14 20:35:04 +0000502}
Eli Friedman5c918912011-09-26 20:15:28 +0000503
Chris Lattnerd922a842002-08-22 22:46:39 +0000504/// canBasicBlockModify - Return true if it is possible for execution of the
Elena Demikhovskya5599bf2014-12-15 14:09:53 +0000505/// specified basic block to modify the location Loc.
Chris Lattnerd922a842002-08-22 22:46:39 +0000506///
Chandler Carruth7b560d42015-09-09 17:55:00 +0000507bool AAResults::canBasicBlockModify(const BasicBlock &BB,
508 const MemoryLocation &Loc) {
Chandler Carruth194f59c2015-07-22 23:15:57 +0000509 return canInstructionRangeModRef(BB.front(), BB.back(), Loc, MRI_Mod);
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000510}
511
Elena Demikhovskya5599bf2014-12-15 14:09:53 +0000512/// canInstructionRangeModRef - Return true if it is possible for the
513/// execution of the specified instructions to mod\ref (according to the
514/// mode) the location Loc. The instructions to consider are all
515/// of the instructions in the range of [I1,I2] INCLUSIVE.
Teresa Johnsonbbcf75e2015-05-13 15:04:14 +0000516/// I1 and I2 must be in the same basic block.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000517bool AAResults::canInstructionRangeModRef(const Instruction &I1,
518 const Instruction &I2,
519 const MemoryLocation &Loc,
520 const ModRefInfo Mode) {
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000521 assert(I1.getParent() == I2.getParent() &&
522 "Instructions not in same basic block!");
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000523 BasicBlock::const_iterator I = I1.getIterator();
524 BasicBlock::const_iterator E = I2.getIterator();
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000525 ++E; // Convert from inclusive to exclusive range.
526
Chris Lattner3a311832003-02-26 19:26:51 +0000527 for (; I != E; ++I) // Check every instruction in range
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000528 if (getModRefInfo(&*I, Loc) & Mode)
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000529 return true;
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000530 return false;
531}
532
Chandler Carruth7b560d42015-09-09 17:55:00 +0000533// Provide a definition for the root virtual destructor.
534AAResults::Concept::~Concept() {}
535
NAKAMURA Takumidf0cd722016-02-28 17:17:00 +0000536// Provide a definition for the static object used to identify passes.
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000537AnalysisKey AAManager::Key;
NAKAMURA Takumidf0cd722016-02-28 17:17:00 +0000538
Chandler Carruth2be10752015-10-21 12:15:19 +0000539namespace {
540/// A wrapper pass for external alias analyses. This just squirrels away the
541/// callback used to run any analyses and register their results.
542struct ExternalAAWrapperPass : ImmutablePass {
543 typedef std::function<void(Pass &, Function &, AAResults &)> CallbackT;
544
545 CallbackT CB;
546
547 static char ID;
548
549 ExternalAAWrapperPass() : ImmutablePass(ID) {
550 initializeExternalAAWrapperPassPass(*PassRegistry::getPassRegistry());
551 }
552 explicit ExternalAAWrapperPass(CallbackT CB)
553 : ImmutablePass(ID), CB(std::move(CB)) {
554 initializeExternalAAWrapperPassPass(*PassRegistry::getPassRegistry());
555 }
556
557 void getAnalysisUsage(AnalysisUsage &AU) const override {
558 AU.setPreservesAll();
559 }
560};
561}
562
563char ExternalAAWrapperPass::ID = 0;
564INITIALIZE_PASS(ExternalAAWrapperPass, "external-aa", "External Alias Analysis",
565 false, true)
566
567ImmutablePass *
568llvm::createExternalAAWrapperPass(ExternalAAWrapperPass::CallbackT Callback) {
569 return new ExternalAAWrapperPass(std::move(Callback));
570}
571
Chandler Carruth7b560d42015-09-09 17:55:00 +0000572AAResultsWrapperPass::AAResultsWrapperPass() : FunctionPass(ID) {
573 initializeAAResultsWrapperPassPass(*PassRegistry::getPassRegistry());
574}
575
576char AAResultsWrapperPass::ID = 0;
577
578INITIALIZE_PASS_BEGIN(AAResultsWrapperPass, "aa",
579 "Function Alias Analysis Results", false, true)
580INITIALIZE_PASS_DEPENDENCY(BasicAAWrapperPass)
George Burgess IVbfa401e2016-07-06 00:26:41 +0000581INITIALIZE_PASS_DEPENDENCY(CFLAndersAAWrapperPass)
582INITIALIZE_PASS_DEPENDENCY(CFLSteensAAWrapperPass)
Chandler Carruth2be10752015-10-21 12:15:19 +0000583INITIALIZE_PASS_DEPENDENCY(ExternalAAWrapperPass)
Chandler Carruth7b560d42015-09-09 17:55:00 +0000584INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass)
585INITIALIZE_PASS_DEPENDENCY(ObjCARCAAWrapperPass)
586INITIALIZE_PASS_DEPENDENCY(SCEVAAWrapperPass)
587INITIALIZE_PASS_DEPENDENCY(ScopedNoAliasAAWrapperPass)
588INITIALIZE_PASS_DEPENDENCY(TypeBasedAAWrapperPass)
589INITIALIZE_PASS_END(AAResultsWrapperPass, "aa",
590 "Function Alias Analysis Results", false, true)
591
592FunctionPass *llvm::createAAResultsWrapperPass() {
593 return new AAResultsWrapperPass();
594}
595
596/// Run the wrapper pass to rebuild an aggregation over known AA passes.
597///
598/// This is the legacy pass manager's interface to the new-style AA results
599/// aggregation object. Because this is somewhat shoe-horned into the legacy
600/// pass manager, we hard code all the specific alias analyses available into
601/// it. While the particular set enabled is configured via commandline flags,
602/// adding a new alias analysis to LLVM will require adding support for it to
603/// this list.
604bool AAResultsWrapperPass::runOnFunction(Function &F) {
605 // NB! This *must* be reset before adding new AA results to the new
606 // AAResults object because in the legacy pass manager, each instance
607 // of these will refer to the *same* immutable analyses, registering and
608 // unregistering themselves with them. We need to carefully tear down the
609 // previous object first, in this case replacing it with an empty one, before
610 // registering new results.
Chandler Carruth12884f72016-03-02 15:56:53 +0000611 AAR.reset(
612 new AAResults(getAnalysis<TargetLibraryInfoWrapperPass>().getTLI()));
Chandler Carruth7b560d42015-09-09 17:55:00 +0000613
614 // BasicAA is always available for function analyses. Also, we add it first
615 // so that it can trump TBAA results when it proves MustAlias.
616 // FIXME: TBAA should have an explicit mode to support this and then we
617 // should reconsider the ordering here.
618 if (!DisableBasicAA)
619 AAR->addAAResult(getAnalysis<BasicAAWrapperPass>().getResult());
620
621 // Populate the results with the currently available AAs.
622 if (auto *WrapperPass = getAnalysisIfAvailable<ScopedNoAliasAAWrapperPass>())
623 AAR->addAAResult(WrapperPass->getResult());
624 if (auto *WrapperPass = getAnalysisIfAvailable<TypeBasedAAWrapperPass>())
625 AAR->addAAResult(WrapperPass->getResult());
626 if (auto *WrapperPass =
627 getAnalysisIfAvailable<objcarc::ObjCARCAAWrapperPass>())
628 AAR->addAAResult(WrapperPass->getResult());
629 if (auto *WrapperPass = getAnalysisIfAvailable<GlobalsAAWrapperPass>())
630 AAR->addAAResult(WrapperPass->getResult());
631 if (auto *WrapperPass = getAnalysisIfAvailable<SCEVAAWrapperPass>())
632 AAR->addAAResult(WrapperPass->getResult());
George Burgess IVbfa401e2016-07-06 00:26:41 +0000633 if (auto *WrapperPass = getAnalysisIfAvailable<CFLAndersAAWrapperPass>())
634 AAR->addAAResult(WrapperPass->getResult());
635 if (auto *WrapperPass = getAnalysisIfAvailable<CFLSteensAAWrapperPass>())
Chandler Carruth7b560d42015-09-09 17:55:00 +0000636 AAR->addAAResult(WrapperPass->getResult());
637
Chandler Carruth2be10752015-10-21 12:15:19 +0000638 // If available, run an external AA providing callback over the results as
639 // well.
640 if (auto *WrapperPass = getAnalysisIfAvailable<ExternalAAWrapperPass>())
641 if (WrapperPass->CB)
642 WrapperPass->CB(*this, F, *AAR);
643
Chandler Carruth7b560d42015-09-09 17:55:00 +0000644 // Analyses don't mutate the IR, so return false.
645 return false;
646}
647
648void AAResultsWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
649 AU.setPreservesAll();
650 AU.addRequired<BasicAAWrapperPass>();
Chandler Carruth12884f72016-03-02 15:56:53 +0000651 AU.addRequired<TargetLibraryInfoWrapperPass>();
Chandler Carruth7b560d42015-09-09 17:55:00 +0000652
653 // We also need to mark all the alias analysis passes we will potentially
654 // probe in runOnFunction as used here to ensure the legacy pass manager
655 // preserves them. This hard coding of lists of alias analyses is specific to
656 // the legacy pass manager.
657 AU.addUsedIfAvailable<ScopedNoAliasAAWrapperPass>();
658 AU.addUsedIfAvailable<TypeBasedAAWrapperPass>();
659 AU.addUsedIfAvailable<objcarc::ObjCARCAAWrapperPass>();
660 AU.addUsedIfAvailable<GlobalsAAWrapperPass>();
661 AU.addUsedIfAvailable<SCEVAAWrapperPass>();
George Burgess IVbfa401e2016-07-06 00:26:41 +0000662 AU.addUsedIfAvailable<CFLAndersAAWrapperPass>();
663 AU.addUsedIfAvailable<CFLSteensAAWrapperPass>();
Chandler Carruth7b560d42015-09-09 17:55:00 +0000664}
665
666AAResults llvm::createLegacyPMAAResults(Pass &P, Function &F,
667 BasicAAResult &BAR) {
Chandler Carruth12884f72016-03-02 15:56:53 +0000668 AAResults AAR(P.getAnalysis<TargetLibraryInfoWrapperPass>().getTLI());
Chandler Carruth7b560d42015-09-09 17:55:00 +0000669
670 // Add in our explicitly constructed BasicAA results.
671 if (!DisableBasicAA)
672 AAR.addAAResult(BAR);
673
674 // Populate the results with the other currently available AAs.
675 if (auto *WrapperPass =
676 P.getAnalysisIfAvailable<ScopedNoAliasAAWrapperPass>())
677 AAR.addAAResult(WrapperPass->getResult());
678 if (auto *WrapperPass = P.getAnalysisIfAvailable<TypeBasedAAWrapperPass>())
679 AAR.addAAResult(WrapperPass->getResult());
680 if (auto *WrapperPass =
681 P.getAnalysisIfAvailable<objcarc::ObjCARCAAWrapperPass>())
682 AAR.addAAResult(WrapperPass->getResult());
683 if (auto *WrapperPass = P.getAnalysisIfAvailable<GlobalsAAWrapperPass>())
684 AAR.addAAResult(WrapperPass->getResult());
George Burgess IVbfa401e2016-07-06 00:26:41 +0000685 if (auto *WrapperPass = P.getAnalysisIfAvailable<CFLAndersAAWrapperPass>())
686 AAR.addAAResult(WrapperPass->getResult());
687 if (auto *WrapperPass = P.getAnalysisIfAvailable<CFLSteensAAWrapperPass>())
Chandler Carruth7b560d42015-09-09 17:55:00 +0000688 AAR.addAAResult(WrapperPass->getResult());
689
690 return AAR;
691}
692
Dan Gohmanfb306c02009-02-03 01:28:32 +0000693bool llvm::isNoAliasCall(const Value *V) {
Benjamin Kramer45f39542015-08-05 14:16:44 +0000694 if (auto CS = ImmutableCallSite(V))
695 return CS.paramHasAttr(0, Attribute::NoAlias);
Dan Gohmanfb306c02009-02-03 01:28:32 +0000696 return false;
697}
698
Sanjay Pateld7f613d2016-01-13 22:17:13 +0000699bool llvm::isNoAliasArgument(const Value *V) {
Michael Kupersteinf3e663a2013-05-28 08:17:48 +0000700 if (const Argument *A = dyn_cast<Argument>(V))
701 return A->hasNoAliasAttr();
702 return false;
703}
704
Dan Gohman00ef9322010-07-07 14:27:09 +0000705bool llvm::isIdentifiedObject(const Value *V) {
Dan Gohman0824aff2010-06-29 00:50:39 +0000706 if (isa<AllocaInst>(V))
Dan Gohman1f59f012009-08-27 17:52:56 +0000707 return true;
708 if (isa<GlobalValue>(V) && !isa<GlobalAlias>(V))
Dan Gohmanfb306c02009-02-03 01:28:32 +0000709 return true;
Dan Gohman00ef9322010-07-07 14:27:09 +0000710 if (isNoAliasCall(V))
711 return true;
712 if (const Argument *A = dyn_cast<Argument>(V))
713 return A->hasNoAliasAttr() || A->hasByValAttr();
Dan Gohmanfb306c02009-02-03 01:28:32 +0000714 return false;
715}
Hal Finkelc782aa52014-07-21 12:27:23 +0000716
Sanjay Pateld7f613d2016-01-13 22:17:13 +0000717bool llvm::isIdentifiedFunctionLocal(const Value *V) {
Hal Finkelc782aa52014-07-21 12:27:23 +0000718 return isa<AllocaInst>(V) || isNoAliasCall(V) || isNoAliasArgument(V);
719}
Sanjoy Das1c481f52016-02-09 01:21:57 +0000720
Chandler Carruth12884f72016-03-02 15:56:53 +0000721void llvm::getAAResultsAnalysisUsage(AnalysisUsage &AU) {
Sanjoy Das1c481f52016-02-09 01:21:57 +0000722 // This function needs to be in sync with llvm::createLegacyPMAAResults -- if
723 // more alias analyses are added to llvm::createLegacyPMAAResults, they need
724 // to be added here also.
Chandler Carruth12884f72016-03-02 15:56:53 +0000725 AU.addRequired<TargetLibraryInfoWrapperPass>();
Sanjoy Das1c481f52016-02-09 01:21:57 +0000726 AU.addUsedIfAvailable<ScopedNoAliasAAWrapperPass>();
727 AU.addUsedIfAvailable<TypeBasedAAWrapperPass>();
728 AU.addUsedIfAvailable<objcarc::ObjCARCAAWrapperPass>();
729 AU.addUsedIfAvailable<GlobalsAAWrapperPass>();
George Burgess IVbfa401e2016-07-06 00:26:41 +0000730 AU.addUsedIfAvailable<CFLAndersAAWrapperPass>();
731 AU.addUsedIfAvailable<CFLSteensAAWrapperPass>();
Sanjoy Das1c481f52016-02-09 01:21:57 +0000732}