blob: 26ab74bbb4e43cfe85d285019330a4a5019fcd1d [file] [log] [blame]
Chris Lattner7d58f8d2002-08-22 18:25:32 +00001//===- AliasAnalysis.cpp - Generic Alias Analysis Interface Implementation -==//
Misha Brukman01808ca2005-04-21 21:13:18 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman01808ca2005-04-21 21:13:18 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner7d58f8d2002-08-22 18:25:32 +00009//
10// This file implements the generic AliasAnalysis interface which is used as the
11// common interface used by all clients and implementations of alias analysis.
12//
13// This file also implements the default version of the AliasAnalysis interface
14// that is to be used when no other implementation is specified. This does some
15// simple tests that detect obvious cases: two different global pointers cannot
16// alias, a global cannot alias a malloc, two different mallocs cannot alias,
17// etc.
18//
19// This alias analysis implementation really isn't very good for anything, but
20// it is very fast, and makes a nice clean default implementation. Because it
21// handles lots of little corner cases, other, more complex, alias analysis
22// implementations may choose to rely on this pass to resolve these simple and
23// easy cases.
24//
25//===----------------------------------------------------------------------===//
26
Chris Lattnerd6a2a992003-02-26 19:41:54 +000027#include "llvm/Analysis/AliasAnalysis.h"
Chandler Carruth7b560d42015-09-09 17:55:00 +000028#include "llvm/Analysis/BasicAliasAnalysis.h"
Nick Lewycky0b682452013-07-27 01:24:00 +000029#include "llvm/Analysis/CFG.h"
Chandler Carruth7b560d42015-09-09 17:55:00 +000030#include "llvm/Analysis/CFLAliasAnalysis.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000031#include "llvm/Analysis/CaptureTracking.h"
Chandler Carruth7b560d42015-09-09 17:55:00 +000032#include "llvm/Analysis/GlobalsModRef.h"
33#include "llvm/Analysis/ObjCARCAliasAnalysis.h"
34#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
35#include "llvm/Analysis/ScopedNoAliasAA.h"
Chandler Carruth62d42152015-01-15 02:16:27 +000036#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruth7b560d42015-09-09 17:55:00 +000037#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
Chad Rosiera968caf2012-05-14 20:35:04 +000038#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000039#include "llvm/IR/BasicBlock.h"
40#include "llvm/IR/DataLayout.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000041#include "llvm/IR/Dominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000042#include "llvm/IR/Function.h"
43#include "llvm/IR/Instructions.h"
44#include "llvm/IR/IntrinsicInst.h"
45#include "llvm/IR/LLVMContext.h"
46#include "llvm/IR/Type.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000047#include "llvm/Pass.h"
Chris Lattnera67dbd02004-03-15 04:07:29 +000048using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000049
Chandler Carruth7b560d42015-09-09 17:55:00 +000050/// Allow disabling BasicAA from the AA results. This is particularly useful
51/// when testing to isolate a single AA implementation.
52static cl::opt<bool> DisableBasicAA("disable-basicaa", cl::Hidden,
53 cl::init(false));
54
55AAResults::AAResults(AAResults &&Arg) : AAs(std::move(Arg.AAs)) {
56 for (auto &AA : AAs)
57 AA->setAAResults(this);
58}
59
60AAResults &AAResults::operator=(AAResults &&Arg) {
61 AAs = std::move(Arg.AAs);
62 for (auto &AA : AAs)
63 AA->setAAResults(this);
64 return *this;
65}
66
67AAResults::~AAResults() {
68// FIXME; It would be nice to at least clear out the pointers back to this
69// aggregation here, but we end up with non-nesting lifetimes in the legacy
70// pass manager that prevent this from working. In the legacy pass manager
71// we'll end up with dangling references here in some cases.
72#if 0
73 for (auto &AA : AAs)
74 AA->setAAResults(nullptr);
75#endif
76}
Chris Lattner7d58f8d2002-08-22 18:25:32 +000077
Chris Lattner62c37002004-05-23 21:15:48 +000078//===----------------------------------------------------------------------===//
79// Default chaining methods
80//===----------------------------------------------------------------------===//
81
Chandler Carruth7b560d42015-09-09 17:55:00 +000082AliasResult AAResults::alias(const MemoryLocation &LocA,
83 const MemoryLocation &LocB) {
84 for (const auto &AA : AAs) {
85 auto Result = AA->alias(LocA, LocB);
86 if (Result != MayAlias)
87 return Result;
88 }
89 return MayAlias;
Chris Lattner62c37002004-05-23 21:15:48 +000090}
91
Chandler Carruth7b560d42015-09-09 17:55:00 +000092bool AAResults::pointsToConstantMemory(const MemoryLocation &Loc,
93 bool OrLocal) {
94 for (const auto &AA : AAs)
95 if (AA->pointsToConstantMemory(Loc, OrLocal))
96 return true;
97
98 return false;
Chris Lattner62c37002004-05-23 21:15:48 +000099}
100
Chandler Carruth7b560d42015-09-09 17:55:00 +0000101ModRefInfo AAResults::getArgModRefInfo(ImmutableCallSite CS, unsigned ArgIdx) {
102 ModRefInfo Result = MRI_ModRef;
103
104 for (const auto &AA : AAs) {
105 Result = ModRefInfo(Result & AA->getArgModRefInfo(CS, ArgIdx));
106
107 // Early-exit the moment we reach the bottom of the lattice.
108 if (Result == MRI_NoModRef)
109 return Result;
110 }
111
112 return Result;
Hal Finkel354e23b2014-07-17 01:28:25 +0000113}
114
Chandler Carruth7b560d42015-09-09 17:55:00 +0000115ModRefInfo AAResults::getModRefInfo(Instruction *I, ImmutableCallSite Call) {
Daniel Berlin8de312d2015-04-13 23:25:41 +0000116 // We may have two calls
117 if (auto CS = ImmutableCallSite(I)) {
118 // Check if the two calls modify the same memory
119 return getModRefInfo(Call, CS);
120 } else {
121 // Otherwise, check if the call modifies or references the
122 // location this memory access defines. The best we can say
123 // is that if the call references what this instruction
124 // defines, it must be clobbered by this location.
Chandler Carruthac80dc72015-06-17 07:18:54 +0000125 const MemoryLocation DefLoc = MemoryLocation::get(I);
Chandler Carruth194f59c2015-07-22 23:15:57 +0000126 if (getModRefInfo(Call, DefLoc) != MRI_NoModRef)
127 return MRI_ModRef;
Daniel Berlin8de312d2015-04-13 23:25:41 +0000128 }
Chandler Carruth194f59c2015-07-22 23:15:57 +0000129 return MRI_NoModRef;
Daniel Berlin8de312d2015-04-13 23:25:41 +0000130}
Owen Andersonb6e4ff02011-01-03 21:38:41 +0000131
Chandler Carruth7b560d42015-09-09 17:55:00 +0000132ModRefInfo AAResults::getModRefInfo(ImmutableCallSite CS,
133 const MemoryLocation &Loc) {
134 ModRefInfo Result = MRI_ModRef;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000135
Chandler Carruth7b560d42015-09-09 17:55:00 +0000136 for (const auto &AA : AAs) {
137 Result = ModRefInfo(Result & AA->getModRefInfo(CS, Loc));
Dan Gohman5f1702e2010-08-06 01:25:49 +0000138
Chandler Carruth7b560d42015-09-09 17:55:00 +0000139 // Early-exit the moment we reach the bottom of the lattice.
140 if (Result == MRI_NoModRef)
141 return Result;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000142 }
143
Chandler Carruth7b560d42015-09-09 17:55:00 +0000144 return Result;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000145}
146
Chandler Carruth7b560d42015-09-09 17:55:00 +0000147ModRefInfo AAResults::getModRefInfo(ImmutableCallSite CS1,
148 ImmutableCallSite CS2) {
149 ModRefInfo Result = MRI_ModRef;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000150
Chandler Carruth7b560d42015-09-09 17:55:00 +0000151 for (const auto &AA : AAs) {
152 Result = ModRefInfo(Result & AA->getModRefInfo(CS1, CS2));
Dan Gohman5f1702e2010-08-06 01:25:49 +0000153
Chandler Carruth7b560d42015-09-09 17:55:00 +0000154 // Early-exit the moment we reach the bottom of the lattice.
155 if (Result == MRI_NoModRef)
156 return Result;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000157 }
158
Chandler Carruth7b560d42015-09-09 17:55:00 +0000159 return Result;
160}
Chandler Carruthc41404a2015-06-17 07:12:40 +0000161
Chandler Carruth7b560d42015-09-09 17:55:00 +0000162FunctionModRefBehavior AAResults::getModRefBehavior(ImmutableCallSite CS) {
163 FunctionModRefBehavior Result = FMRB_UnknownModRefBehavior;
Hal Finkel354e23b2014-07-17 01:28:25 +0000164
Chandler Carruth7b560d42015-09-09 17:55:00 +0000165 for (const auto &AA : AAs) {
166 Result = FunctionModRefBehavior(Result & AA->getModRefBehavior(CS));
167
168 // Early-exit the moment we reach the bottom of the lattice.
169 if (Result == FMRB_DoesNotAccessMemory)
170 return Result;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000171 }
172
Chandler Carruth7b560d42015-09-09 17:55:00 +0000173 return Result;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000174}
175
Chandler Carruth7b560d42015-09-09 17:55:00 +0000176FunctionModRefBehavior AAResults::getModRefBehavior(const Function *F) {
177 FunctionModRefBehavior Result = FMRB_UnknownModRefBehavior;
Dan Gohman5f1702e2010-08-06 01:25:49 +0000178
Chandler Carruth7b560d42015-09-09 17:55:00 +0000179 for (const auto &AA : AAs) {
180 Result = FunctionModRefBehavior(Result & AA->getModRefBehavior(F));
Dan Gohman5f1702e2010-08-06 01:25:49 +0000181
Chandler Carruth7b560d42015-09-09 17:55:00 +0000182 // Early-exit the moment we reach the bottom of the lattice.
183 if (Result == FMRB_DoesNotAccessMemory)
184 return Result;
185 }
Dan Gohman5f1702e2010-08-06 01:25:49 +0000186
Chandler Carruth7b560d42015-09-09 17:55:00 +0000187 return Result;
Chris Lattner62c37002004-05-23 21:15:48 +0000188}
189
Chris Lattner62c37002004-05-23 21:15:48 +0000190//===----------------------------------------------------------------------===//
Chandler Carruth7b560d42015-09-09 17:55:00 +0000191// Helper method implementation
Chris Lattner62c37002004-05-23 21:15:48 +0000192//===----------------------------------------------------------------------===//
193
Chandler Carruth7b560d42015-09-09 17:55:00 +0000194ModRefInfo AAResults::getModRefInfo(const LoadInst *L,
195 const MemoryLocation &Loc) {
Eli Friedman5494ada2011-08-15 20:54:19 +0000196 // Be conservative in the face of volatile/atomic.
197 if (!L->isUnordered())
Chandler Carruth194f59c2015-07-22 23:15:57 +0000198 return MRI_ModRef;
Dan Gohman6b4671b2010-08-06 18:11:28 +0000199
Dan Gohman52f9d7d2010-08-03 17:27:43 +0000200 // If the load address doesn't alias the given address, it doesn't read
201 // or write the specified memory.
Chandler Carruth70c61c12015-06-04 02:03:15 +0000202 if (Loc.Ptr && !alias(MemoryLocation::get(L), Loc))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000203 return MRI_NoModRef;
Dan Gohman52f9d7d2010-08-03 17:27:43 +0000204
Dan Gohman52f9d7d2010-08-03 17:27:43 +0000205 // Otherwise, a load just reads.
Chandler Carruth194f59c2015-07-22 23:15:57 +0000206 return MRI_Ref;
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000207}
208
Chandler Carruth7b560d42015-09-09 17:55:00 +0000209ModRefInfo AAResults::getModRefInfo(const StoreInst *S,
210 const MemoryLocation &Loc) {
Eli Friedman5494ada2011-08-15 20:54:19 +0000211 // Be conservative in the face of volatile/atomic.
212 if (!S->isUnordered())
Chandler Carruth194f59c2015-07-22 23:15:57 +0000213 return MRI_ModRef;
Dan Gohman6b4671b2010-08-06 18:11:28 +0000214
Daniel Berlinb2d22762015-04-13 23:05:45 +0000215 if (Loc.Ptr) {
216 // If the store address cannot alias the pointer in question, then the
217 // specified memory cannot be modified by the store.
Chandler Carruth70c61c12015-06-04 02:03:15 +0000218 if (!alias(MemoryLocation::get(S), Loc))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000219 return MRI_NoModRef;
Chris Lattner96055762004-01-30 22:16:42 +0000220
Daniel Berlinb2d22762015-04-13 23:05:45 +0000221 // If the pointer is a pointer to constant memory, then it could not have
222 // been modified by this store.
223 if (pointsToConstantMemory(Loc))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000224 return MRI_NoModRef;
Daniel Berlinb2d22762015-04-13 23:05:45 +0000225 }
Dan Gohman52f9d7d2010-08-03 17:27:43 +0000226
227 // Otherwise, a store just writes.
Chandler Carruth194f59c2015-07-22 23:15:57 +0000228 return MRI_Mod;
Chris Lattner3a311832003-02-26 19:26:51 +0000229}
230
Chandler Carruth7b560d42015-09-09 17:55:00 +0000231ModRefInfo AAResults::getModRefInfo(const VAArgInst *V,
232 const MemoryLocation &Loc) {
Dan Gohmane68958f2010-08-06 18:24:38 +0000233
Daniel Berlinec1de3f2015-04-28 19:19:14 +0000234 if (Loc.Ptr) {
235 // If the va_arg address cannot alias the pointer in question, then the
236 // specified memory cannot be accessed by the va_arg.
Chandler Carruth70c61c12015-06-04 02:03:15 +0000237 if (!alias(MemoryLocation::get(V), Loc))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000238 return MRI_NoModRef;
Daniel Berlinec1de3f2015-04-28 19:19:14 +0000239
240 // If the pointer is a pointer to constant memory, then it could not have
241 // been modified by this va_arg.
242 if (pointsToConstantMemory(Loc))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000243 return MRI_NoModRef;
Daniel Berlinec1de3f2015-04-28 19:19:14 +0000244 }
Dan Gohmane68958f2010-08-06 18:24:38 +0000245
246 // Otherwise, a va_arg reads and writes.
Chandler Carruth194f59c2015-07-22 23:15:57 +0000247 return MRI_ModRef;
Dan Gohmane68958f2010-08-06 18:24:38 +0000248}
249
Chandler Carruth7b560d42015-09-09 17:55:00 +0000250ModRefInfo AAResults::getModRefInfo(const AtomicCmpXchgInst *CX,
251 const MemoryLocation &Loc) {
Eli Friedman5c918912011-09-26 20:15:28 +0000252 // Acquire/Release cmpxchg has properties that matter for arbitrary addresses.
Tim Northovere94a5182014-03-11 10:48:52 +0000253 if (CX->getSuccessOrdering() > Monotonic)
Chandler Carruth194f59c2015-07-22 23:15:57 +0000254 return MRI_ModRef;
Eli Friedman5c918912011-09-26 20:15:28 +0000255
256 // If the cmpxchg address does not alias the location, it does not access it.
Chandler Carruth70c61c12015-06-04 02:03:15 +0000257 if (Loc.Ptr && !alias(MemoryLocation::get(CX), Loc))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000258 return MRI_NoModRef;
Eli Friedman5c918912011-09-26 20:15:28 +0000259
Chandler Carruth194f59c2015-07-22 23:15:57 +0000260 return MRI_ModRef;
Eli Friedman5c918912011-09-26 20:15:28 +0000261}
262
Chandler Carruth7b560d42015-09-09 17:55:00 +0000263ModRefInfo AAResults::getModRefInfo(const AtomicRMWInst *RMW,
264 const MemoryLocation &Loc) {
Eli Friedman5c918912011-09-26 20:15:28 +0000265 // Acquire/Release atomicrmw has properties that matter for arbitrary addresses.
266 if (RMW->getOrdering() > Monotonic)
Chandler Carruth194f59c2015-07-22 23:15:57 +0000267 return MRI_ModRef;
Eli Friedman5c918912011-09-26 20:15:28 +0000268
269 // If the atomicrmw address does not alias the location, it does not access it.
Chandler Carruth70c61c12015-06-04 02:03:15 +0000270 if (Loc.Ptr && !alias(MemoryLocation::get(RMW), Loc))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000271 return MRI_NoModRef;
Eli Friedman5c918912011-09-26 20:15:28 +0000272
Chandler Carruth194f59c2015-07-22 23:15:57 +0000273 return MRI_ModRef;
Eli Friedman5c918912011-09-26 20:15:28 +0000274}
275
Bruno Cardoso Lopesdfc1d962015-07-31 14:31:35 +0000276/// \brief Return information about whether a particular call site modifies
277/// or reads the specified memory location \p MemLoc before instruction \p I
278/// in a BasicBlock. A ordered basic block \p OBB can be used to speed up
279/// instruction-ordering queries inside the BasicBlock containing \p I.
280/// FIXME: this is really just shoring-up a deficiency in alias analysis.
281/// BasicAA isn't willing to spend linear time determining whether an alloca
282/// was captured before or after this particular call, while we are. However,
283/// with a smarter AA in place, this test is just wasting compile time.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000284ModRefInfo AAResults::callCapturesBefore(const Instruction *I,
285 const MemoryLocation &MemLoc,
286 DominatorTree *DT,
287 OrderedBasicBlock *OBB) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000288 if (!DT)
Chandler Carruth194f59c2015-07-22 23:15:57 +0000289 return MRI_ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000290
Chandler Carruth7b560d42015-09-09 17:55:00 +0000291 const Value *Object =
292 GetUnderlyingObject(MemLoc.Ptr, I->getModule()->getDataLayout());
Chad Rosiera968caf2012-05-14 20:35:04 +0000293 if (!isIdentifiedObject(Object) || isa<GlobalValue>(Object) ||
294 isa<Constant>(Object))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000295 return MRI_ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000296
297 ImmutableCallSite CS(I);
298 if (!CS.getInstruction() || CS.getInstruction() == Object)
Chandler Carruth194f59c2015-07-22 23:15:57 +0000299 return MRI_ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000300
Hal Finkelb0356212014-07-21 13:15:48 +0000301 if (llvm::PointerMayBeCapturedBefore(Object, /* ReturnCaptures */ true,
Hal Finkeld32803b2014-07-21 21:30:22 +0000302 /* StoreCaptures */ true, I, DT,
Bruno Cardoso Lopesdfc1d962015-07-31 14:31:35 +0000303 /* include Object */ true,
304 /* OrderedBasicBlock */ OBB))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000305 return MRI_ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000306
307 unsigned ArgNo = 0;
Chandler Carruth194f59c2015-07-22 23:15:57 +0000308 ModRefInfo R = MRI_NoModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000309 for (ImmutableCallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
310 CI != CE; ++CI, ++ArgNo) {
311 // Only look at the no-capture or byval pointer arguments. If this
312 // pointer were passed to arguments that were neither of these, then it
313 // couldn't be no-capture.
314 if (!(*CI)->getType()->isPointerTy() ||
315 (!CS.doesNotCapture(ArgNo) && !CS.isByValArgument(ArgNo)))
316 continue;
317
318 // If this is a no-capture pointer argument, see if we can tell that it
319 // is impossible to alias the pointer we're checking. If not, we have to
320 // assume that the call could touch the pointer, even though it doesn't
321 // escape.
Chandler Carruthac80dc72015-06-17 07:18:54 +0000322 if (isNoAlias(MemoryLocation(*CI), MemoryLocation(Object)))
Nick Lewyckyc0514622013-07-07 10:15:16 +0000323 continue;
324 if (CS.doesNotAccessMemory(ArgNo))
325 continue;
326 if (CS.onlyReadsMemory(ArgNo)) {
Chandler Carruth194f59c2015-07-22 23:15:57 +0000327 R = MRI_Ref;
Nick Lewyckyc0514622013-07-07 10:15:16 +0000328 continue;
Chad Rosiera968caf2012-05-14 20:35:04 +0000329 }
Chandler Carruth194f59c2015-07-22 23:15:57 +0000330 return MRI_ModRef;
Chad Rosiera968caf2012-05-14 20:35:04 +0000331 }
Nick Lewyckyc0514622013-07-07 10:15:16 +0000332 return R;
Chad Rosiera968caf2012-05-14 20:35:04 +0000333}
Eli Friedman5c918912011-09-26 20:15:28 +0000334
Chris Lattnerd922a842002-08-22 22:46:39 +0000335/// canBasicBlockModify - Return true if it is possible for execution of the
Elena Demikhovskya5599bf2014-12-15 14:09:53 +0000336/// specified basic block to modify the location Loc.
Chris Lattnerd922a842002-08-22 22:46:39 +0000337///
Chandler Carruth7b560d42015-09-09 17:55:00 +0000338bool AAResults::canBasicBlockModify(const BasicBlock &BB,
339 const MemoryLocation &Loc) {
Chandler Carruth194f59c2015-07-22 23:15:57 +0000340 return canInstructionRangeModRef(BB.front(), BB.back(), Loc, MRI_Mod);
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000341}
342
Elena Demikhovskya5599bf2014-12-15 14:09:53 +0000343/// canInstructionRangeModRef - Return true if it is possible for the
344/// execution of the specified instructions to mod\ref (according to the
345/// mode) the location Loc. The instructions to consider are all
346/// of the instructions in the range of [I1,I2] INCLUSIVE.
Teresa Johnsonbbcf75e2015-05-13 15:04:14 +0000347/// I1 and I2 must be in the same basic block.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000348bool AAResults::canInstructionRangeModRef(const Instruction &I1,
349 const Instruction &I2,
350 const MemoryLocation &Loc,
351 const ModRefInfo Mode) {
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000352 assert(I1.getParent() == I2.getParent() &&
353 "Instructions not in same basic block!");
Dan Gohman5442c712010-08-03 21:48:53 +0000354 BasicBlock::const_iterator I = &I1;
355 BasicBlock::const_iterator E = &I2;
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000356 ++E; // Convert from inclusive to exclusive range.
357
Chris Lattner3a311832003-02-26 19:26:51 +0000358 for (; I != E; ++I) // Check every instruction in range
Elena Demikhovskya5599bf2014-12-15 14:09:53 +0000359 if (getModRefInfo(I, Loc) & Mode)
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000360 return true;
Chris Lattner7d58f8d2002-08-22 18:25:32 +0000361 return false;
362}
363
Chandler Carruth7b560d42015-09-09 17:55:00 +0000364// Provide a definition for the root virtual destructor.
365AAResults::Concept::~Concept() {}
366
367AAResultsWrapperPass::AAResultsWrapperPass() : FunctionPass(ID) {
368 initializeAAResultsWrapperPassPass(*PassRegistry::getPassRegistry());
369}
370
371char AAResultsWrapperPass::ID = 0;
372
373INITIALIZE_PASS_BEGIN(AAResultsWrapperPass, "aa",
374 "Function Alias Analysis Results", false, true)
375INITIALIZE_PASS_DEPENDENCY(BasicAAWrapperPass)
376INITIALIZE_PASS_DEPENDENCY(CFLAAWrapperPass)
377INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass)
378INITIALIZE_PASS_DEPENDENCY(ObjCARCAAWrapperPass)
379INITIALIZE_PASS_DEPENDENCY(SCEVAAWrapperPass)
380INITIALIZE_PASS_DEPENDENCY(ScopedNoAliasAAWrapperPass)
381INITIALIZE_PASS_DEPENDENCY(TypeBasedAAWrapperPass)
382INITIALIZE_PASS_END(AAResultsWrapperPass, "aa",
383 "Function Alias Analysis Results", false, true)
384
385FunctionPass *llvm::createAAResultsWrapperPass() {
386 return new AAResultsWrapperPass();
387}
388
389/// Run the wrapper pass to rebuild an aggregation over known AA passes.
390///
391/// This is the legacy pass manager's interface to the new-style AA results
392/// aggregation object. Because this is somewhat shoe-horned into the legacy
393/// pass manager, we hard code all the specific alias analyses available into
394/// it. While the particular set enabled is configured via commandline flags,
395/// adding a new alias analysis to LLVM will require adding support for it to
396/// this list.
397bool AAResultsWrapperPass::runOnFunction(Function &F) {
398 // NB! This *must* be reset before adding new AA results to the new
399 // AAResults object because in the legacy pass manager, each instance
400 // of these will refer to the *same* immutable analyses, registering and
401 // unregistering themselves with them. We need to carefully tear down the
402 // previous object first, in this case replacing it with an empty one, before
403 // registering new results.
404 AAR.reset(new AAResults());
405
406 // BasicAA is always available for function analyses. Also, we add it first
407 // so that it can trump TBAA results when it proves MustAlias.
408 // FIXME: TBAA should have an explicit mode to support this and then we
409 // should reconsider the ordering here.
410 if (!DisableBasicAA)
411 AAR->addAAResult(getAnalysis<BasicAAWrapperPass>().getResult());
412
413 // Populate the results with the currently available AAs.
414 if (auto *WrapperPass = getAnalysisIfAvailable<ScopedNoAliasAAWrapperPass>())
415 AAR->addAAResult(WrapperPass->getResult());
416 if (auto *WrapperPass = getAnalysisIfAvailable<TypeBasedAAWrapperPass>())
417 AAR->addAAResult(WrapperPass->getResult());
418 if (auto *WrapperPass =
419 getAnalysisIfAvailable<objcarc::ObjCARCAAWrapperPass>())
420 AAR->addAAResult(WrapperPass->getResult());
421 if (auto *WrapperPass = getAnalysisIfAvailable<GlobalsAAWrapperPass>())
422 AAR->addAAResult(WrapperPass->getResult());
423 if (auto *WrapperPass = getAnalysisIfAvailable<SCEVAAWrapperPass>())
424 AAR->addAAResult(WrapperPass->getResult());
425 if (auto *WrapperPass = getAnalysisIfAvailable<CFLAAWrapperPass>())
426 AAR->addAAResult(WrapperPass->getResult());
427
428 // Analyses don't mutate the IR, so return false.
429 return false;
430}
431
432void AAResultsWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
433 AU.setPreservesAll();
434 AU.addRequired<BasicAAWrapperPass>();
435
436 // We also need to mark all the alias analysis passes we will potentially
437 // probe in runOnFunction as used here to ensure the legacy pass manager
438 // preserves them. This hard coding of lists of alias analyses is specific to
439 // the legacy pass manager.
440 AU.addUsedIfAvailable<ScopedNoAliasAAWrapperPass>();
441 AU.addUsedIfAvailable<TypeBasedAAWrapperPass>();
442 AU.addUsedIfAvailable<objcarc::ObjCARCAAWrapperPass>();
443 AU.addUsedIfAvailable<GlobalsAAWrapperPass>();
444 AU.addUsedIfAvailable<SCEVAAWrapperPass>();
445 AU.addUsedIfAvailable<CFLAAWrapperPass>();
446}
447
448AAResults llvm::createLegacyPMAAResults(Pass &P, Function &F,
449 BasicAAResult &BAR) {
450 AAResults AAR;
451
452 // Add in our explicitly constructed BasicAA results.
453 if (!DisableBasicAA)
454 AAR.addAAResult(BAR);
455
456 // Populate the results with the other currently available AAs.
457 if (auto *WrapperPass =
458 P.getAnalysisIfAvailable<ScopedNoAliasAAWrapperPass>())
459 AAR.addAAResult(WrapperPass->getResult());
460 if (auto *WrapperPass = P.getAnalysisIfAvailable<TypeBasedAAWrapperPass>())
461 AAR.addAAResult(WrapperPass->getResult());
462 if (auto *WrapperPass =
463 P.getAnalysisIfAvailable<objcarc::ObjCARCAAWrapperPass>())
464 AAR.addAAResult(WrapperPass->getResult());
465 if (auto *WrapperPass = P.getAnalysisIfAvailable<GlobalsAAWrapperPass>())
466 AAR.addAAResult(WrapperPass->getResult());
467 if (auto *WrapperPass = P.getAnalysisIfAvailable<SCEVAAWrapperPass>())
468 AAR.addAAResult(WrapperPass->getResult());
469 if (auto *WrapperPass = P.getAnalysisIfAvailable<CFLAAWrapperPass>())
470 AAR.addAAResult(WrapperPass->getResult());
471
472 return AAR;
473}
474
Dan Gohmanfb306c02009-02-03 01:28:32 +0000475/// isNoAliasCall - Return true if this pointer is returned by a noalias
476/// function.
477bool llvm::isNoAliasCall(const Value *V) {
Benjamin Kramer45f39542015-08-05 14:16:44 +0000478 if (auto CS = ImmutableCallSite(V))
479 return CS.paramHasAttr(0, Attribute::NoAlias);
Dan Gohmanfb306c02009-02-03 01:28:32 +0000480 return false;
481}
482
Michael Kupersteinf3e663a2013-05-28 08:17:48 +0000483/// isNoAliasArgument - Return true if this is an argument with the noalias
484/// attribute.
485bool llvm::isNoAliasArgument(const Value *V)
486{
487 if (const Argument *A = dyn_cast<Argument>(V))
488 return A->hasNoAliasAttr();
489 return false;
490}
491
Dan Gohmanfb306c02009-02-03 01:28:32 +0000492/// isIdentifiedObject - Return true if this pointer refers to a distinct and
493/// identifiable object. This returns true for:
Dan Gohman1f59f012009-08-27 17:52:56 +0000494/// Global Variables and Functions (but not Global Aliases)
Dan Gohmanfb306c02009-02-03 01:28:32 +0000495/// Allocas and Mallocs
Dan Gohman00ef9322010-07-07 14:27:09 +0000496/// ByVal and NoAlias Arguments
497/// NoAlias returns
Dan Gohmanfb306c02009-02-03 01:28:32 +0000498///
Dan Gohman00ef9322010-07-07 14:27:09 +0000499bool llvm::isIdentifiedObject(const Value *V) {
Dan Gohman0824aff2010-06-29 00:50:39 +0000500 if (isa<AllocaInst>(V))
Dan Gohman1f59f012009-08-27 17:52:56 +0000501 return true;
502 if (isa<GlobalValue>(V) && !isa<GlobalAlias>(V))
Dan Gohmanfb306c02009-02-03 01:28:32 +0000503 return true;
Dan Gohman00ef9322010-07-07 14:27:09 +0000504 if (isNoAliasCall(V))
505 return true;
506 if (const Argument *A = dyn_cast<Argument>(V))
507 return A->hasNoAliasAttr() || A->hasByValAttr();
Dan Gohmanfb306c02009-02-03 01:28:32 +0000508 return false;
509}
Hal Finkelc782aa52014-07-21 12:27:23 +0000510
511/// isIdentifiedFunctionLocal - Return true if V is umabigously identified
512/// at the function-level. Different IdentifiedFunctionLocals can't alias.
513/// Further, an IdentifiedFunctionLocal can not alias with any function
514/// arguments other than itself, which is not necessarily true for
515/// IdentifiedObjects.
516bool llvm::isIdentifiedFunctionLocal(const Value *V)
517{
518 return isa<AllocaInst>(V) || isNoAliasCall(V) || isNoAliasArgument(V);
519}