blob: 97ea0fc40bd3cfc4cf9090b5e90efc6db3c72fc3 [file] [log] [blame]
Eugene Zelenko530851c2017-08-11 21:30:02 +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"
George Burgess IVbfa401e2016-07-06 00:26:41 +000029#include "llvm/Analysis/CFLAndersAliasAnalysis.h"
30#include "llvm/Analysis/CFLSteensAliasAnalysis.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"
Eugene Zelenko530851c2017-08-11 21:30:02 +000033#include "llvm/Analysis/MemoryLocation.h"
Chandler Carruth7b560d42015-09-09 17:55:00 +000034#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"
Eugene Zelenko530851c2017-08-11 21:30:02 +000040#include "llvm/IR/Argument.h"
41#include "llvm/IR/Attributes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000042#include "llvm/IR/BasicBlock.h"
Eugene Zelenko530851c2017-08-11 21:30:02 +000043#include "llvm/IR/CallSite.h"
44#include "llvm/IR/Instruction.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000045#include "llvm/IR/Instructions.h"
Eugene Zelenko530851c2017-08-11 21:30:02 +000046#include "llvm/IR/Module.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000047#include "llvm/IR/Type.h"
Eugene Zelenko530851c2017-08-11 21:30:02 +000048#include "llvm/IR/Value.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000049#include "llvm/Pass.h"
Eugene Zelenko530851c2017-08-11 21:30:02 +000050#include "llvm/Support/AtomicOrdering.h"
51#include "llvm/Support/Casting.h"
52#include "llvm/Support/CommandLine.h"
53#include <algorithm>
54#include <cassert>
55#include <functional>
56#include <iterator>
57
Chris Lattnera67dbd02004-03-15 04:07:29 +000058using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000059
Chandler Carruth7b560d42015-09-09 17:55:00 +000060/// Allow disabling BasicAA from the AA results. This is particularly useful
61/// when testing to isolate a single AA implementation.
62static cl::opt<bool> DisableBasicAA("disable-basicaa", cl::Hidden,
63 cl::init(false));
64
Chandler Carruth17c630a2016-12-27 08:44:39 +000065AAResults::AAResults(AAResults &&Arg)
66 : TLI(Arg.TLI), AAs(std::move(Arg.AAs)), AADeps(std::move(Arg.AADeps)) {
Chandler Carruth7b560d42015-09-09 17:55:00 +000067 for (auto &AA : AAs)
68 AA->setAAResults(this);
69}
70
Chandler Carruth7b560d42015-09-09 17:55:00 +000071AAResults::~AAResults() {
72// FIXME; It would be nice to at least clear out the pointers back to this
73// aggregation here, but we end up with non-nesting lifetimes in the legacy
74// pass manager that prevent this from working. In the legacy pass manager
75// we'll end up with dangling references here in some cases.
76#if 0
77 for (auto &AA : AAs)
78 AA->setAAResults(nullptr);
79#endif
80}
Chris Lattner7d58f8d2002-08-22 18:25:32 +000081
Chandler Carruth17c630a2016-12-27 08:44:39 +000082bool AAResults::invalidate(Function &F, const PreservedAnalyses &PA,
83 FunctionAnalysisManager::Invalidator &Inv) {
Chandler Carruth17c630a2016-12-27 08:44:39 +000084 // Check if the AA manager itself has been invalidated.
85 auto PAC = PA.getChecker<AAManager>();
86 if (!PAC.preserved() && !PAC.preservedSet<AllAnalysesOn<Function>>())
87 return true; // The manager needs to be blown away, clear everything.
88
89 // Check all of the dependencies registered.
90 for (AnalysisKey *ID : AADeps)
91 if (Inv.invalidate(ID, F, PA))
92 return true;
93
94 // Everything we depend on is still fine, so are we. Nothing to invalidate.
95 return false;
96}
97
Chris Lattner62c37002004-05-23 21:15:48 +000098//===----------------------------------------------------------------------===//
99// Default chaining methods
100//===----------------------------------------------------------------------===//
101
Chandler Carruth7b560d42015-09-09 17:55:00 +0000102AliasResult AAResults::alias(const MemoryLocation &LocA,
103 const MemoryLocation &LocB) {
104 for (const auto &AA : AAs) {
105 auto Result = AA->alias(LocA, LocB);
106 if (Result != MayAlias)
107 return Result;
108 }
109 return MayAlias;
Chris Lattner62c37002004-05-23 21:15:48 +0000110}
111
Chandler Carruth7b560d42015-09-09 17:55:00 +0000112bool AAResults::pointsToConstantMemory(const MemoryLocation &Loc,
113 bool OrLocal) {
114 for (const auto &AA : AAs)
115 if (AA->pointsToConstantMemory(Loc, OrLocal))
116 return true;
117
118 return false;
Chris Lattner62c37002004-05-23 21:15:48 +0000119}
120
Chandler Carruth7b560d42015-09-09 17:55:00 +0000121ModRefInfo AAResults::getArgModRefInfo(ImmutableCallSite CS, unsigned ArgIdx) {
122 ModRefInfo Result = MRI_ModRef;
123
124 for (const auto &AA : AAs) {
Alina Sbirlea63d22502017-12-05 20:12:23 +0000125 Result = intersectModRef(Result, AA->getArgModRefInfo(CS, ArgIdx));
Chandler Carruth7b560d42015-09-09 17:55:00 +0000126
127 // Early-exit the moment we reach the bottom of the lattice.
Alina Sbirlea63d22502017-12-05 20:12:23 +0000128 if (isNoModRef(Result))
Chandler Carruth7b560d42015-09-09 17:55:00 +0000129 return Result;
130 }
131
132 return Result;
Hal Finkel354e23b2014-07-17 01:28:25 +0000133}
134
Chandler Carruth7b560d42015-09-09 17:55:00 +0000135ModRefInfo AAResults::getModRefInfo(Instruction *I, ImmutableCallSite Call) {
Daniel Berlin8de312d2015-04-13 23:25:41 +0000136 // We may have two calls
137 if (auto CS = ImmutableCallSite(I)) {
138 // Check if the two calls modify the same memory
Nicolai Haehnle9343f362016-07-11 14:11:45 +0000139 return getModRefInfo(CS, Call);
David Majnemera940f362016-07-15 17:19:24 +0000140 } else if (I->isFenceLike()) {
141 // If this is a fence, just return MRI_ModRef.
142 return MRI_ModRef;
Daniel Berlin8de312d2015-04-13 23:25:41 +0000143 } else {
144 // Otherwise, check if the call modifies or references the
145 // location this memory access defines. The best we can say
146 // is that if the call references what this instruction
147 // defines, it must be clobbered by this location.
Chandler Carruthac80dc72015-06-17 07:18:54 +0000148 const MemoryLocation DefLoc = MemoryLocation::get(I);
Alina Sbirlea63d22502017-12-05 20:12:23 +0000149 ModRefInfo MR = getModRefInfo(Call, DefLoc);
150 if (isModOrRefSet(MR))
151 return setModAndRef(MR);
Daniel Berlin8de312d2015-04-13 23:25:41 +0000152 }
Chandler Carruth194f59c2015-07-22 23:15:57 +0000153 return MRI_NoModRef;
Daniel Berlin8de312d2015-04-13 23:25:41 +0000154}
Owen Andersonb6e4ff02011-01-03 21:38:41 +0000155
Chandler Carruth7b560d42015-09-09 17:55:00 +0000156ModRefInfo AAResults::getModRefInfo(ImmutableCallSite CS,
157 const MemoryLocation &Loc) {
158 ModRefInfo Result = MRI_ModRef;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000159
Chandler Carruth7b560d42015-09-09 17:55:00 +0000160 for (const auto &AA : AAs) {
Alina Sbirlea63d22502017-12-05 20:12:23 +0000161 Result = intersectModRef(Result, AA->getModRefInfo(CS, Loc));
Dan Gohman5f1702e2010-08-06 01:25:49 +0000162
Chandler Carruth7b560d42015-09-09 17:55:00 +0000163 // Early-exit the moment we reach the bottom of the lattice.
Alina Sbirlea63d22502017-12-05 20:12:23 +0000164 if (isNoModRef(Result))
Chandler Carruth7b560d42015-09-09 17:55:00 +0000165 return Result;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000166 }
167
Chandler Carruth12884f72016-03-02 15:56:53 +0000168 // Try to refine the mod-ref info further using other API entry points to the
169 // aggregate set of AA results.
170 auto MRB = getModRefBehavior(CS);
Andrew Kaylor9604f342016-11-08 21:07:42 +0000171 if (MRB == FMRB_DoesNotAccessMemory ||
172 MRB == FMRB_OnlyAccessesInaccessibleMem)
Chandler Carruth12884f72016-03-02 15:56:53 +0000173 return MRI_NoModRef;
174
175 if (onlyReadsMemory(MRB))
Alina Sbirlea63d22502017-12-05 20:12:23 +0000176 Result = clearMod(Result);
Nicolai Haehnle84c9f992016-07-04 08:01:29 +0000177 else if (doesNotReadMemory(MRB))
Alina Sbirlea63d22502017-12-05 20:12:23 +0000178 Result = clearRef(Result);
Chandler Carruth12884f72016-03-02 15:56:53 +0000179
Andrew Kaylor9604f342016-11-08 21:07:42 +0000180 if (onlyAccessesArgPointees(MRB) || onlyAccessesInaccessibleOrArgMem(MRB)) {
Chandler Carruth12884f72016-03-02 15:56:53 +0000181 bool DoesAlias = false;
182 ModRefInfo AllArgsMask = MRI_NoModRef;
183 if (doesAccessArgPointees(MRB)) {
184 for (auto AI = CS.arg_begin(), AE = CS.arg_end(); AI != AE; ++AI) {
185 const Value *Arg = *AI;
186 if (!Arg->getType()->isPointerTy())
187 continue;
188 unsigned ArgIdx = std::distance(CS.arg_begin(), AI);
189 MemoryLocation ArgLoc = MemoryLocation::getForArgument(CS, ArgIdx, TLI);
190 AliasResult ArgAlias = alias(ArgLoc, Loc);
191 if (ArgAlias != NoAlias) {
192 ModRefInfo ArgMask = getArgModRefInfo(CS, ArgIdx);
193 DoesAlias = true;
Alina Sbirlea63d22502017-12-05 20:12:23 +0000194 AllArgsMask = unionModRef(AllArgsMask, ArgMask);
Chandler Carruth12884f72016-03-02 15:56:53 +0000195 }
196 }
197 }
Alina Sbirlea63d22502017-12-05 20:12:23 +0000198 // Return MRI_NoModRef if no alias found with any argument.
Chandler Carruth12884f72016-03-02 15:56:53 +0000199 if (!DoesAlias)
200 return MRI_NoModRef;
Alina Sbirlea63d22502017-12-05 20:12:23 +0000201 // Logical & between other AA analyses and argument analysis.
202 Result = intersectModRef(Result, AllArgsMask);
Chandler Carruth12884f72016-03-02 15:56:53 +0000203 }
204
205 // If Loc is a constant memory location, the call definitely could not
206 // modify the memory location.
Alina Sbirlea63d22502017-12-05 20:12:23 +0000207 if (isModSet(Result) && pointsToConstantMemory(Loc, /*OrLocal*/ false))
208 Result = clearMod(Result);
Chandler Carruth12884f72016-03-02 15:56:53 +0000209
Chandler Carruth7b560d42015-09-09 17:55:00 +0000210 return Result;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000211}
212
Chandler Carruth7b560d42015-09-09 17:55:00 +0000213ModRefInfo AAResults::getModRefInfo(ImmutableCallSite CS1,
214 ImmutableCallSite CS2) {
215 ModRefInfo Result = MRI_ModRef;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000216
Chandler Carruth7b560d42015-09-09 17:55:00 +0000217 for (const auto &AA : AAs) {
Alina Sbirlea63d22502017-12-05 20:12:23 +0000218 Result = intersectModRef(Result, AA->getModRefInfo(CS1, CS2));
Dan Gohman5f1702e2010-08-06 01:25:49 +0000219
Chandler Carruth7b560d42015-09-09 17:55:00 +0000220 // Early-exit the moment we reach the bottom of the lattice.
Alina Sbirlea63d22502017-12-05 20:12:23 +0000221 if (isNoModRef(Result))
Chandler Carruth7b560d42015-09-09 17:55:00 +0000222 return Result;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000223 }
224
Chandler Carruth12884f72016-03-02 15:56:53 +0000225 // Try to refine the mod-ref info further using other API entry points to the
226 // aggregate set of AA results.
227
228 // If CS1 or CS2 are readnone, they don't interact.
229 auto CS1B = getModRefBehavior(CS1);
230 if (CS1B == FMRB_DoesNotAccessMemory)
231 return MRI_NoModRef;
232
233 auto CS2B = getModRefBehavior(CS2);
234 if (CS2B == FMRB_DoesNotAccessMemory)
235 return MRI_NoModRef;
236
237 // If they both only read from memory, there is no dependence.
238 if (onlyReadsMemory(CS1B) && onlyReadsMemory(CS2B))
239 return MRI_NoModRef;
240
241 // If CS1 only reads memory, the only dependence on CS2 can be
242 // from CS1 reading memory written by CS2.
243 if (onlyReadsMemory(CS1B))
Alina Sbirlea63d22502017-12-05 20:12:23 +0000244 Result = clearMod(Result);
Nicolai Haehnle84c9f992016-07-04 08:01:29 +0000245 else if (doesNotReadMemory(CS1B))
Alina Sbirlea63d22502017-12-05 20:12:23 +0000246 Result = clearRef(Result);
Chandler Carruth12884f72016-03-02 15:56:53 +0000247
248 // If CS2 only access memory through arguments, accumulate the mod/ref
249 // information from CS1's references to the memory referenced by
250 // CS2's arguments.
251 if (onlyAccessesArgPointees(CS2B)) {
252 ModRefInfo R = MRI_NoModRef;
253 if (doesAccessArgPointees(CS2B)) {
254 for (auto I = CS2.arg_begin(), E = CS2.arg_end(); I != E; ++I) {
255 const Value *Arg = *I;
256 if (!Arg->getType()->isPointerTy())
257 continue;
258 unsigned CS2ArgIdx = std::distance(CS2.arg_begin(), I);
259 auto CS2ArgLoc = MemoryLocation::getForArgument(CS2, CS2ArgIdx, TLI);
260
Alina Sbirlea63d22502017-12-05 20:12:23 +0000261 // ArgModRefCS2 indicates what CS2 might do to CS2ArgLoc, and the
262 // dependence of CS1 on that location is the inverse:
263 // - If CS2 modifies location, dependence exists if CS1 reads or writes.
264 // - If CS2 only reads location, dependence exists if CS1 writes.
265 ModRefInfo ArgModRefCS2 = getArgModRefInfo(CS2, CS2ArgIdx);
Alina Sbirlea1e7440d2017-12-05 20:51:20 +0000266 ModRefInfo ArgMask = MRI_NoModRef;
Alina Sbirlea63d22502017-12-05 20:12:23 +0000267 if (isModSet(ArgModRefCS2))
Chandler Carruth12884f72016-03-02 15:56:53 +0000268 ArgMask = MRI_ModRef;
Alina Sbirlea63d22502017-12-05 20:12:23 +0000269 else if (isRefSet(ArgModRefCS2))
Chandler Carruth12884f72016-03-02 15:56:53 +0000270 ArgMask = MRI_Mod;
271
Alina Sbirlea63d22502017-12-05 20:12:23 +0000272 // ModRefCS1 indicates what CS1 might do to CS2ArgLoc, and we use
273 // above ArgMask to update dependence info.
274 ModRefInfo ModRefCS1 = getModRefInfo(CS1, CS2ArgLoc);
275 ArgMask = intersectModRef(ArgMask, ModRefCS1);
Chandler Carruth12884f72016-03-02 15:56:53 +0000276
Alina Sbirlea63d22502017-12-05 20:12:23 +0000277 R = intersectModRef(unionModRef(R, ArgMask), Result);
Chandler Carruth12884f72016-03-02 15:56:53 +0000278 if (R == Result)
279 break;
280 }
281 }
282 return R;
283 }
284
285 // If CS1 only accesses memory through arguments, check if CS2 references
286 // any of the memory referenced by CS1's arguments. If not, return NoModRef.
287 if (onlyAccessesArgPointees(CS1B)) {
288 ModRefInfo R = MRI_NoModRef;
289 if (doesAccessArgPointees(CS1B)) {
290 for (auto I = CS1.arg_begin(), E = CS1.arg_end(); I != E; ++I) {
291 const Value *Arg = *I;
292 if (!Arg->getType()->isPointerTy())
293 continue;
294 unsigned CS1ArgIdx = std::distance(CS1.arg_begin(), I);
295 auto CS1ArgLoc = MemoryLocation::getForArgument(CS1, CS1ArgIdx, TLI);
296
Alina Sbirlea63d22502017-12-05 20:12:23 +0000297 // ArgModRefCS1 indicates what CS1 might do to CS1ArgLoc; if CS1 might
298 // Mod CS1ArgLoc, then we care about either a Mod or a Ref by CS2. If
299 // CS1 might Ref, then we care only about a Mod by CS2.
300 ModRefInfo ArgModRefCS1 = getArgModRefInfo(CS1, CS1ArgIdx);
301 ModRefInfo ModRefCS2 = getModRefInfo(CS2, CS1ArgLoc);
302 if ((isModSet(ArgModRefCS1) && isModOrRefSet(ModRefCS2)) ||
303 (isRefSet(ArgModRefCS1) && isModSet(ModRefCS2)))
304 R = intersectModRef(unionModRef(R, ArgModRefCS1), Result);
Chandler Carruth12884f72016-03-02 15:56:53 +0000305
306 if (R == Result)
307 break;
308 }
309 }
310 return R;
311 }
312
Chandler Carruth7b560d42015-09-09 17:55:00 +0000313 return Result;
314}
Chandler Carruthc41404a2015-06-17 07:12:40 +0000315
Chandler Carruth7b560d42015-09-09 17:55:00 +0000316FunctionModRefBehavior AAResults::getModRefBehavior(ImmutableCallSite CS) {
317 FunctionModRefBehavior Result = FMRB_UnknownModRefBehavior;
Hal Finkel354e23b2014-07-17 01:28:25 +0000318
Chandler Carruth7b560d42015-09-09 17:55:00 +0000319 for (const auto &AA : AAs) {
320 Result = FunctionModRefBehavior(Result & AA->getModRefBehavior(CS));
321
322 // Early-exit the moment we reach the bottom of the lattice.
323 if (Result == FMRB_DoesNotAccessMemory)
324 return Result;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000325 }
326
Chandler Carruth7b560d42015-09-09 17:55:00 +0000327 return Result;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000328}
329
Chandler Carruth7b560d42015-09-09 17:55:00 +0000330FunctionModRefBehavior AAResults::getModRefBehavior(const Function *F) {
331 FunctionModRefBehavior Result = FMRB_UnknownModRefBehavior;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000332
Chandler Carruth7b560d42015-09-09 17:55:00 +0000333 for (const auto &AA : AAs) {
334 Result = FunctionModRefBehavior(Result & AA->getModRefBehavior(F));
Dan Gohman5f1702e2010-08-06 01:25:49 +0000335
Chandler Carruth7b560d42015-09-09 17:55:00 +0000336 // Early-exit the moment we reach the bottom of the lattice.
337 if (Result == FMRB_DoesNotAccessMemory)
338 return Result;
339 }
Dan Gohman5f1702e2010-08-06 01:25:49 +0000340
Chandler Carruth7b560d42015-09-09 17:55:00 +0000341 return Result;
Chris Lattner62c37002004-05-23 21:15:48 +0000342}
343
Chris Lattner62c37002004-05-23 21:15:48 +0000344//===----------------------------------------------------------------------===//
Chandler Carruth7b560d42015-09-09 17:55:00 +0000345// Helper method implementation
Chris Lattner62c37002004-05-23 21:15:48 +0000346//===----------------------------------------------------------------------===//
347
Chandler Carruth7b560d42015-09-09 17:55:00 +0000348ModRefInfo AAResults::getModRefInfo(const LoadInst *L,
349 const MemoryLocation &Loc) {
Daniel Berlind952cea2017-04-07 01:28:36 +0000350 // Be conservative in the face of atomic.
351 if (isStrongerThan(L->getOrdering(), AtomicOrdering::Unordered))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000352 return MRI_ModRef;
Dan Gohman6b4671b2010-08-06 18:11:28 +0000353
Dan Gohman52f9d7d2010-08-03 17:27:43 +0000354 // If the load address doesn't alias the given address, it doesn't read
355 // or write the specified memory.
Chandler Carruth70c61c12015-06-04 02:03:15 +0000356 if (Loc.Ptr && !alias(MemoryLocation::get(L), Loc))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000357 return MRI_NoModRef;
Dan Gohman52f9d7d2010-08-03 17:27:43 +0000358
Dan Gohman52f9d7d2010-08-03 17:27:43 +0000359 // Otherwise, a load just reads.
Chandler Carruth194f59c2015-07-22 23:15:57 +0000360 return MRI_Ref;
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000361}
362
Chandler Carruth7b560d42015-09-09 17:55:00 +0000363ModRefInfo AAResults::getModRefInfo(const StoreInst *S,
364 const MemoryLocation &Loc) {
Daniel Berlind952cea2017-04-07 01:28:36 +0000365 // Be conservative in the face of atomic.
366 if (isStrongerThan(S->getOrdering(), AtomicOrdering::Unordered))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000367 return MRI_ModRef;
Dan Gohman6b4671b2010-08-06 18:11:28 +0000368
Daniel Berlinb2d22762015-04-13 23:05:45 +0000369 if (Loc.Ptr) {
370 // If the store address cannot alias the pointer in question, then the
371 // specified memory cannot be modified by the store.
Chandler Carruth70c61c12015-06-04 02:03:15 +0000372 if (!alias(MemoryLocation::get(S), Loc))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000373 return MRI_NoModRef;
Chris Lattner96055762004-01-30 22:16:42 +0000374
Daniel Berlinb2d22762015-04-13 23:05:45 +0000375 // If the pointer is a pointer to constant memory, then it could not have
376 // been modified by this store.
377 if (pointsToConstantMemory(Loc))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000378 return MRI_NoModRef;
Daniel Berlinb2d22762015-04-13 23:05:45 +0000379 }
Dan Gohman52f9d7d2010-08-03 17:27:43 +0000380
381 // Otherwise, a store just writes.
Chandler Carruth194f59c2015-07-22 23:15:57 +0000382 return MRI_Mod;
Chris Lattner3a311832003-02-26 19:26:51 +0000383}
384
Anna Thomas698f0de2017-01-20 00:21:33 +0000385ModRefInfo AAResults::getModRefInfo(const FenceInst *S, const MemoryLocation &Loc) {
386 // If we know that the location is a constant memory location, the fence
387 // cannot modify this location.
388 if (Loc.Ptr && pointsToConstantMemory(Loc))
389 return MRI_Ref;
390 return MRI_ModRef;
391}
392
Chandler Carruth7b560d42015-09-09 17:55:00 +0000393ModRefInfo AAResults::getModRefInfo(const VAArgInst *V,
394 const MemoryLocation &Loc) {
Daniel Berlinec1de3f2015-04-28 19:19:14 +0000395 if (Loc.Ptr) {
396 // If the va_arg address cannot alias the pointer in question, then the
397 // specified memory cannot be accessed by the va_arg.
Chandler Carruth70c61c12015-06-04 02:03:15 +0000398 if (!alias(MemoryLocation::get(V), Loc))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000399 return MRI_NoModRef;
Daniel Berlinec1de3f2015-04-28 19:19:14 +0000400
401 // If the pointer is a pointer to constant memory, then it could not have
402 // been modified by this va_arg.
403 if (pointsToConstantMemory(Loc))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000404 return MRI_NoModRef;
Daniel Berlinec1de3f2015-04-28 19:19:14 +0000405 }
Dan Gohmane68958f2010-08-06 18:24:38 +0000406
407 // Otherwise, a va_arg reads and writes.
Chandler Carruth194f59c2015-07-22 23:15:57 +0000408 return MRI_ModRef;
Dan Gohmane68958f2010-08-06 18:24:38 +0000409}
410
David Majnemer6727c012015-11-17 08:15:14 +0000411ModRefInfo AAResults::getModRefInfo(const CatchPadInst *CatchPad,
412 const MemoryLocation &Loc) {
413 if (Loc.Ptr) {
414 // If the pointer is a pointer to constant memory,
415 // then it could not have been modified by this catchpad.
416 if (pointsToConstantMemory(Loc))
417 return MRI_NoModRef;
418 }
419
420 // Otherwise, a catchpad reads and writes.
421 return MRI_ModRef;
422}
423
424ModRefInfo AAResults::getModRefInfo(const CatchReturnInst *CatchRet,
425 const MemoryLocation &Loc) {
426 if (Loc.Ptr) {
427 // If the pointer is a pointer to constant memory,
428 // then it could not have been modified by this catchpad.
429 if (pointsToConstantMemory(Loc))
430 return MRI_NoModRef;
431 }
432
433 // Otherwise, a catchret reads and writes.
434 return MRI_ModRef;
435}
436
Chandler Carruth7b560d42015-09-09 17:55:00 +0000437ModRefInfo AAResults::getModRefInfo(const AtomicCmpXchgInst *CX,
438 const MemoryLocation &Loc) {
Eli Friedman5c918912011-09-26 20:15:28 +0000439 // Acquire/Release cmpxchg has properties that matter for arbitrary addresses.
JF Bastien800f87a2016-04-06 21:19:33 +0000440 if (isStrongerThanMonotonic(CX->getSuccessOrdering()))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000441 return MRI_ModRef;
Eli Friedman5c918912011-09-26 20:15:28 +0000442
443 // If the cmpxchg address does not alias the location, it does not access it.
Chandler Carruth70c61c12015-06-04 02:03:15 +0000444 if (Loc.Ptr && !alias(MemoryLocation::get(CX), Loc))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000445 return MRI_NoModRef;
Eli Friedman5c918912011-09-26 20:15:28 +0000446
Chandler Carruth194f59c2015-07-22 23:15:57 +0000447 return MRI_ModRef;
Eli Friedman5c918912011-09-26 20:15:28 +0000448}
449
Chandler Carruth7b560d42015-09-09 17:55:00 +0000450ModRefInfo AAResults::getModRefInfo(const AtomicRMWInst *RMW,
451 const MemoryLocation &Loc) {
Eli Friedman5c918912011-09-26 20:15:28 +0000452 // Acquire/Release atomicrmw has properties that matter for arbitrary addresses.
JF Bastien800f87a2016-04-06 21:19:33 +0000453 if (isStrongerThanMonotonic(RMW->getOrdering()))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000454 return MRI_ModRef;
Eli Friedman5c918912011-09-26 20:15:28 +0000455
456 // If the atomicrmw address does not alias the location, it does not access it.
Chandler Carruth70c61c12015-06-04 02:03:15 +0000457 if (Loc.Ptr && !alias(MemoryLocation::get(RMW), Loc))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000458 return MRI_NoModRef;
Eli Friedman5c918912011-09-26 20:15:28 +0000459
Chandler Carruth194f59c2015-07-22 23:15:57 +0000460 return MRI_ModRef;
Eli Friedman5c918912011-09-26 20:15:28 +0000461}
462
Bruno Cardoso Lopesdfc1d962015-07-31 14:31:35 +0000463/// \brief Return information about whether a particular call site modifies
464/// or reads the specified memory location \p MemLoc before instruction \p I
Alina Sbirlea63d22502017-12-05 20:12:23 +0000465/// in a BasicBlock. An ordered basic block \p OBB can be used to speed up
Bruno Cardoso Lopesdfc1d962015-07-31 14:31:35 +0000466/// instruction-ordering queries inside the BasicBlock containing \p I.
467/// FIXME: this is really just shoring-up a deficiency in alias analysis.
468/// BasicAA isn't willing to spend linear time determining whether an alloca
469/// was captured before or after this particular call, while we are. However,
470/// with a smarter AA in place, this test is just wasting compile time.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000471ModRefInfo AAResults::callCapturesBefore(const Instruction *I,
472 const MemoryLocation &MemLoc,
473 DominatorTree *DT,
474 OrderedBasicBlock *OBB) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000475 if (!DT)
Chandler Carruth194f59c2015-07-22 23:15:57 +0000476 return MRI_ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000477
Chandler Carruth7b560d42015-09-09 17:55:00 +0000478 const Value *Object =
479 GetUnderlyingObject(MemLoc.Ptr, I->getModule()->getDataLayout());
Chad Rosiera968caf2012-05-14 20:35:04 +0000480 if (!isIdentifiedObject(Object) || isa<GlobalValue>(Object) ||
481 isa<Constant>(Object))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000482 return MRI_ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000483
484 ImmutableCallSite CS(I);
485 if (!CS.getInstruction() || CS.getInstruction() == Object)
Chandler Carruth194f59c2015-07-22 23:15:57 +0000486 return MRI_ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000487
Eugene Zelenko530851c2017-08-11 21:30:02 +0000488 if (PointerMayBeCapturedBefore(Object, /* ReturnCaptures */ true,
489 /* StoreCaptures */ true, I, DT,
490 /* include Object */ true,
491 /* OrderedBasicBlock */ OBB))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000492 return MRI_ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000493
494 unsigned ArgNo = 0;
Chandler Carruth194f59c2015-07-22 23:15:57 +0000495 ModRefInfo R = MRI_NoModRef;
Sanjoy Dasd0bdf3e2016-06-13 19:55:04 +0000496 for (auto CI = CS.data_operands_begin(), CE = CS.data_operands_end();
Chad Rosiera968caf2012-05-14 20:35:04 +0000497 CI != CE; ++CI, ++ArgNo) {
498 // Only look at the no-capture or byval pointer arguments. If this
499 // pointer were passed to arguments that were neither of these, then it
500 // couldn't be no-capture.
501 if (!(*CI)->getType()->isPointerTy() ||
Hal Finkel39fed392016-12-15 05:09:15 +0000502 (!CS.doesNotCapture(ArgNo) &&
503 ArgNo < CS.getNumArgOperands() && !CS.isByValArgument(ArgNo)))
Chad Rosiera968caf2012-05-14 20:35:04 +0000504 continue;
505
506 // If this is a no-capture pointer argument, see if we can tell that it
507 // is impossible to alias the pointer we're checking. If not, we have to
508 // assume that the call could touch the pointer, even though it doesn't
509 // escape.
Chandler Carruthac80dc72015-06-17 07:18:54 +0000510 if (isNoAlias(MemoryLocation(*CI), MemoryLocation(Object)))
Nick Lewyckyc0514622013-07-07 10:15:16 +0000511 continue;
Hal Finkelf19e1142016-12-15 05:50:45 +0000512 if (CS.doesNotAccessMemory(ArgNo))
Nick Lewyckyc0514622013-07-07 10:15:16 +0000513 continue;
Hal Finkelf19e1142016-12-15 05:50:45 +0000514 if (CS.onlyReadsMemory(ArgNo)) {
Chandler Carruth194f59c2015-07-22 23:15:57 +0000515 R = MRI_Ref;
Nick Lewyckyc0514622013-07-07 10:15:16 +0000516 continue;
Chad Rosiera968caf2012-05-14 20:35:04 +0000517 }
Chandler Carruth194f59c2015-07-22 23:15:57 +0000518 return MRI_ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000519 }
Nick Lewyckyc0514622013-07-07 10:15:16 +0000520 return R;
Chad Rosiera968caf2012-05-14 20:35:04 +0000521}
Eli Friedman5c918912011-09-26 20:15:28 +0000522
Chris Lattnerd922a842002-08-22 22:46:39 +0000523/// canBasicBlockModify - Return true if it is possible for execution of the
Elena Demikhovskya5599bf2014-12-15 14:09:53 +0000524/// specified basic block to modify the location Loc.
Chris Lattnerd922a842002-08-22 22:46:39 +0000525///
Chandler Carruth7b560d42015-09-09 17:55:00 +0000526bool AAResults::canBasicBlockModify(const BasicBlock &BB,
527 const MemoryLocation &Loc) {
Chandler Carruth194f59c2015-07-22 23:15:57 +0000528 return canInstructionRangeModRef(BB.front(), BB.back(), Loc, MRI_Mod);
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000529}
530
Elena Demikhovskya5599bf2014-12-15 14:09:53 +0000531/// canInstructionRangeModRef - Return true if it is possible for the
532/// execution of the specified instructions to mod\ref (according to the
533/// mode) the location Loc. The instructions to consider are all
534/// of the instructions in the range of [I1,I2] INCLUSIVE.
Teresa Johnsonbbcf75e2015-05-13 15:04:14 +0000535/// I1 and I2 must be in the same basic block.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000536bool AAResults::canInstructionRangeModRef(const Instruction &I1,
537 const Instruction &I2,
538 const MemoryLocation &Loc,
539 const ModRefInfo Mode) {
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000540 assert(I1.getParent() == I2.getParent() &&
541 "Instructions not in same basic block!");
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000542 BasicBlock::const_iterator I = I1.getIterator();
543 BasicBlock::const_iterator E = I2.getIterator();
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000544 ++E; // Convert from inclusive to exclusive range.
545
Chris Lattner3a311832003-02-26 19:26:51 +0000546 for (; I != E; ++I) // Check every instruction in range
Alina Sbirlea63d22502017-12-05 20:12:23 +0000547 if (intersectModRef(getModRefInfo(&*I, Loc), Mode))
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000548 return true;
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000549 return false;
550}
551
Chandler Carruth7b560d42015-09-09 17:55:00 +0000552// Provide a definition for the root virtual destructor.
Eugene Zelenko530851c2017-08-11 21:30:02 +0000553AAResults::Concept::~Concept() = default;
Chandler Carruth7b560d42015-09-09 17:55:00 +0000554
NAKAMURA Takumidf0cd722016-02-28 17:17:00 +0000555// Provide a definition for the static object used to identify passes.
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000556AnalysisKey AAManager::Key;
NAKAMURA Takumidf0cd722016-02-28 17:17:00 +0000557
Chandler Carruth2be10752015-10-21 12:15:19 +0000558namespace {
Eugene Zelenko530851c2017-08-11 21:30:02 +0000559
Chandler Carruth2be10752015-10-21 12:15:19 +0000560/// A wrapper pass for external alias analyses. This just squirrels away the
561/// callback used to run any analyses and register their results.
562struct ExternalAAWrapperPass : ImmutablePass {
Eugene Zelenko530851c2017-08-11 21:30:02 +0000563 using CallbackT = std::function<void(Pass &, Function &, AAResults &)>;
Chandler Carruth2be10752015-10-21 12:15:19 +0000564
565 CallbackT CB;
566
567 static char ID;
568
569 ExternalAAWrapperPass() : ImmutablePass(ID) {
570 initializeExternalAAWrapperPassPass(*PassRegistry::getPassRegistry());
571 }
Eugene Zelenko530851c2017-08-11 21:30:02 +0000572
Chandler Carruth2be10752015-10-21 12:15:19 +0000573 explicit ExternalAAWrapperPass(CallbackT CB)
574 : ImmutablePass(ID), CB(std::move(CB)) {
575 initializeExternalAAWrapperPassPass(*PassRegistry::getPassRegistry());
576 }
577
578 void getAnalysisUsage(AnalysisUsage &AU) const override {
579 AU.setPreservesAll();
580 }
581};
Eugene Zelenko530851c2017-08-11 21:30:02 +0000582
583} // end anonymous namespace
Chandler Carruth2be10752015-10-21 12:15:19 +0000584
585char ExternalAAWrapperPass::ID = 0;
Eugene Zelenko530851c2017-08-11 21:30:02 +0000586
Chandler Carruth2be10752015-10-21 12:15:19 +0000587INITIALIZE_PASS(ExternalAAWrapperPass, "external-aa", "External Alias Analysis",
588 false, true)
589
590ImmutablePass *
591llvm::createExternalAAWrapperPass(ExternalAAWrapperPass::CallbackT Callback) {
592 return new ExternalAAWrapperPass(std::move(Callback));
593}
594
Chandler Carruth7b560d42015-09-09 17:55:00 +0000595AAResultsWrapperPass::AAResultsWrapperPass() : FunctionPass(ID) {
596 initializeAAResultsWrapperPassPass(*PassRegistry::getPassRegistry());
597}
598
599char AAResultsWrapperPass::ID = 0;
600
601INITIALIZE_PASS_BEGIN(AAResultsWrapperPass, "aa",
602 "Function Alias Analysis Results", false, true)
603INITIALIZE_PASS_DEPENDENCY(BasicAAWrapperPass)
George Burgess IVbfa401e2016-07-06 00:26:41 +0000604INITIALIZE_PASS_DEPENDENCY(CFLAndersAAWrapperPass)
605INITIALIZE_PASS_DEPENDENCY(CFLSteensAAWrapperPass)
Chandler Carruth2be10752015-10-21 12:15:19 +0000606INITIALIZE_PASS_DEPENDENCY(ExternalAAWrapperPass)
Chandler Carruth7b560d42015-09-09 17:55:00 +0000607INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass)
608INITIALIZE_PASS_DEPENDENCY(ObjCARCAAWrapperPass)
609INITIALIZE_PASS_DEPENDENCY(SCEVAAWrapperPass)
610INITIALIZE_PASS_DEPENDENCY(ScopedNoAliasAAWrapperPass)
611INITIALIZE_PASS_DEPENDENCY(TypeBasedAAWrapperPass)
612INITIALIZE_PASS_END(AAResultsWrapperPass, "aa",
613 "Function Alias Analysis Results", false, true)
614
615FunctionPass *llvm::createAAResultsWrapperPass() {
616 return new AAResultsWrapperPass();
617}
618
619/// Run the wrapper pass to rebuild an aggregation over known AA passes.
620///
621/// This is the legacy pass manager's interface to the new-style AA results
622/// aggregation object. Because this is somewhat shoe-horned into the legacy
623/// pass manager, we hard code all the specific alias analyses available into
624/// it. While the particular set enabled is configured via commandline flags,
625/// adding a new alias analysis to LLVM will require adding support for it to
626/// this list.
627bool AAResultsWrapperPass::runOnFunction(Function &F) {
628 // NB! This *must* be reset before adding new AA results to the new
629 // AAResults object because in the legacy pass manager, each instance
630 // of these will refer to the *same* immutable analyses, registering and
631 // unregistering themselves with them. We need to carefully tear down the
632 // previous object first, in this case replacing it with an empty one, before
633 // registering new results.
Chandler Carruth12884f72016-03-02 15:56:53 +0000634 AAR.reset(
635 new AAResults(getAnalysis<TargetLibraryInfoWrapperPass>().getTLI()));
Chandler Carruth7b560d42015-09-09 17:55:00 +0000636
637 // BasicAA is always available for function analyses. Also, we add it first
638 // so that it can trump TBAA results when it proves MustAlias.
639 // FIXME: TBAA should have an explicit mode to support this and then we
640 // should reconsider the ordering here.
641 if (!DisableBasicAA)
642 AAR->addAAResult(getAnalysis<BasicAAWrapperPass>().getResult());
643
644 // Populate the results with the currently available AAs.
645 if (auto *WrapperPass = getAnalysisIfAvailable<ScopedNoAliasAAWrapperPass>())
646 AAR->addAAResult(WrapperPass->getResult());
647 if (auto *WrapperPass = getAnalysisIfAvailable<TypeBasedAAWrapperPass>())
648 AAR->addAAResult(WrapperPass->getResult());
649 if (auto *WrapperPass =
650 getAnalysisIfAvailable<objcarc::ObjCARCAAWrapperPass>())
651 AAR->addAAResult(WrapperPass->getResult());
652 if (auto *WrapperPass = getAnalysisIfAvailable<GlobalsAAWrapperPass>())
653 AAR->addAAResult(WrapperPass->getResult());
654 if (auto *WrapperPass = getAnalysisIfAvailable<SCEVAAWrapperPass>())
655 AAR->addAAResult(WrapperPass->getResult());
George Burgess IVbfa401e2016-07-06 00:26:41 +0000656 if (auto *WrapperPass = getAnalysisIfAvailable<CFLAndersAAWrapperPass>())
657 AAR->addAAResult(WrapperPass->getResult());
658 if (auto *WrapperPass = getAnalysisIfAvailable<CFLSteensAAWrapperPass>())
Chandler Carruth7b560d42015-09-09 17:55:00 +0000659 AAR->addAAResult(WrapperPass->getResult());
660
Chandler Carruth2be10752015-10-21 12:15:19 +0000661 // If available, run an external AA providing callback over the results as
662 // well.
663 if (auto *WrapperPass = getAnalysisIfAvailable<ExternalAAWrapperPass>())
664 if (WrapperPass->CB)
665 WrapperPass->CB(*this, F, *AAR);
666
Chandler Carruth7b560d42015-09-09 17:55:00 +0000667 // Analyses don't mutate the IR, so return false.
668 return false;
669}
670
671void AAResultsWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
672 AU.setPreservesAll();
673 AU.addRequired<BasicAAWrapperPass>();
Chandler Carruth12884f72016-03-02 15:56:53 +0000674 AU.addRequired<TargetLibraryInfoWrapperPass>();
Chandler Carruth7b560d42015-09-09 17:55:00 +0000675
676 // We also need to mark all the alias analysis passes we will potentially
677 // probe in runOnFunction as used here to ensure the legacy pass manager
678 // preserves them. This hard coding of lists of alias analyses is specific to
679 // the legacy pass manager.
680 AU.addUsedIfAvailable<ScopedNoAliasAAWrapperPass>();
681 AU.addUsedIfAvailable<TypeBasedAAWrapperPass>();
682 AU.addUsedIfAvailable<objcarc::ObjCARCAAWrapperPass>();
683 AU.addUsedIfAvailable<GlobalsAAWrapperPass>();
684 AU.addUsedIfAvailable<SCEVAAWrapperPass>();
George Burgess IVbfa401e2016-07-06 00:26:41 +0000685 AU.addUsedIfAvailable<CFLAndersAAWrapperPass>();
686 AU.addUsedIfAvailable<CFLSteensAAWrapperPass>();
Chandler Carruth7b560d42015-09-09 17:55:00 +0000687}
688
689AAResults llvm::createLegacyPMAAResults(Pass &P, Function &F,
690 BasicAAResult &BAR) {
Chandler Carruth12884f72016-03-02 15:56:53 +0000691 AAResults AAR(P.getAnalysis<TargetLibraryInfoWrapperPass>().getTLI());
Chandler Carruth7b560d42015-09-09 17:55:00 +0000692
693 // Add in our explicitly constructed BasicAA results.
694 if (!DisableBasicAA)
695 AAR.addAAResult(BAR);
696
697 // Populate the results with the other currently available AAs.
698 if (auto *WrapperPass =
699 P.getAnalysisIfAvailable<ScopedNoAliasAAWrapperPass>())
700 AAR.addAAResult(WrapperPass->getResult());
701 if (auto *WrapperPass = P.getAnalysisIfAvailable<TypeBasedAAWrapperPass>())
702 AAR.addAAResult(WrapperPass->getResult());
703 if (auto *WrapperPass =
704 P.getAnalysisIfAvailable<objcarc::ObjCARCAAWrapperPass>())
705 AAR.addAAResult(WrapperPass->getResult());
706 if (auto *WrapperPass = P.getAnalysisIfAvailable<GlobalsAAWrapperPass>())
707 AAR.addAAResult(WrapperPass->getResult());
George Burgess IVbfa401e2016-07-06 00:26:41 +0000708 if (auto *WrapperPass = P.getAnalysisIfAvailable<CFLAndersAAWrapperPass>())
709 AAR.addAAResult(WrapperPass->getResult());
710 if (auto *WrapperPass = P.getAnalysisIfAvailable<CFLSteensAAWrapperPass>())
Chandler Carruth7b560d42015-09-09 17:55:00 +0000711 AAR.addAAResult(WrapperPass->getResult());
712
713 return AAR;
714}
715
Dan Gohmanfb306c02009-02-03 01:28:32 +0000716bool llvm::isNoAliasCall(const Value *V) {
Benjamin Kramer45f39542015-08-05 14:16:44 +0000717 if (auto CS = ImmutableCallSite(V))
Reid Klecknerfb502d22017-04-14 20:19:02 +0000718 return CS.hasRetAttr(Attribute::NoAlias);
Dan Gohmanfb306c02009-02-03 01:28:32 +0000719 return false;
720}
721
Sanjay Pateld7f613d2016-01-13 22:17:13 +0000722bool llvm::isNoAliasArgument(const Value *V) {
Michael Kupersteinf3e663a2013-05-28 08:17:48 +0000723 if (const Argument *A = dyn_cast<Argument>(V))
724 return A->hasNoAliasAttr();
725 return false;
726}
727
Dan Gohman00ef9322010-07-07 14:27:09 +0000728bool llvm::isIdentifiedObject(const Value *V) {
Dan Gohman0824aff2010-06-29 00:50:39 +0000729 if (isa<AllocaInst>(V))
Dan Gohman1f59f012009-08-27 17:52:56 +0000730 return true;
731 if (isa<GlobalValue>(V) && !isa<GlobalAlias>(V))
Dan Gohmanfb306c02009-02-03 01:28:32 +0000732 return true;
Dan Gohman00ef9322010-07-07 14:27:09 +0000733 if (isNoAliasCall(V))
734 return true;
735 if (const Argument *A = dyn_cast<Argument>(V))
736 return A->hasNoAliasAttr() || A->hasByValAttr();
Dan Gohmanfb306c02009-02-03 01:28:32 +0000737 return false;
738}
Hal Finkelc782aa52014-07-21 12:27:23 +0000739
Sanjay Pateld7f613d2016-01-13 22:17:13 +0000740bool llvm::isIdentifiedFunctionLocal(const Value *V) {
Hal Finkelc782aa52014-07-21 12:27:23 +0000741 return isa<AllocaInst>(V) || isNoAliasCall(V) || isNoAliasArgument(V);
742}
Sanjoy Das1c481f52016-02-09 01:21:57 +0000743
Chandler Carruth12884f72016-03-02 15:56:53 +0000744void llvm::getAAResultsAnalysisUsage(AnalysisUsage &AU) {
Sanjoy Das1c481f52016-02-09 01:21:57 +0000745 // This function needs to be in sync with llvm::createLegacyPMAAResults -- if
746 // more alias analyses are added to llvm::createLegacyPMAAResults, they need
747 // to be added here also.
Chandler Carruth12884f72016-03-02 15:56:53 +0000748 AU.addRequired<TargetLibraryInfoWrapperPass>();
Sanjoy Das1c481f52016-02-09 01:21:57 +0000749 AU.addUsedIfAvailable<ScopedNoAliasAAWrapperPass>();
750 AU.addUsedIfAvailable<TypeBasedAAWrapperPass>();
751 AU.addUsedIfAvailable<objcarc::ObjCARCAAWrapperPass>();
752 AU.addUsedIfAvailable<GlobalsAAWrapperPass>();
George Burgess IVbfa401e2016-07-06 00:26:41 +0000753 AU.addUsedIfAvailable<CFLAndersAAWrapperPass>();
754 AU.addUsedIfAvailable<CFLSteensAAWrapperPass>();
Sanjoy Das1c481f52016-02-09 01:21:57 +0000755}