blob: 229e9e509bc1570dad1ebd2a3c4a59d5265b0f34 [file] [log] [blame]
Chris Lattner9d7c9ea2003-11-25 20:11:47 +00001//===- BasicAliasAnalysis.cpp - Local Alias Analysis Impl -----------------===//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Brukman2b37d7c2005-04-21 21:13:18 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerd501c132003-02-26 19:41:54 +00009//
10// This file defines the default implementation of the Alias Analysis interface
11// that simply implements a few identities (two different globals cannot alias,
12// etc), but otherwise does no analysis.
13//
14//===----------------------------------------------------------------------===//
15
16#include "llvm/Analysis/AliasAnalysis.h"
Jeff Cohen534927d2005-01-08 22:01:16 +000017#include "llvm/Analysis/Passes.h"
Chris Lattner4244bb52004-03-15 03:36:49 +000018#include "llvm/Constants.h"
19#include "llvm/DerivedTypes.h"
20#include "llvm/Function.h"
21#include "llvm/GlobalVariable.h"
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000022#include "llvm/Instructions.h"
Owen Anderson9b636cb2008-02-17 21:29:08 +000023#include "llvm/IntrinsicInst.h"
Dan Gohman3a7a68c2009-07-17 22:25:10 +000024#include "llvm/Operator.h"
Chris Lattner4244bb52004-03-15 03:36:49 +000025#include "llvm/Pass.h"
Chris Lattner5d5261c2009-11-26 16:26:43 +000026#include "llvm/Analysis/CaptureTracking.h"
27#include "llvm/Analysis/MemoryBuiltins.h"
28#include "llvm/Analysis/ValueTracking.h"
Chris Lattnerd501c132003-02-26 19:41:54 +000029#include "llvm/Target/TargetData.h"
Chris Lattnere405c642009-11-26 17:12:50 +000030#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnera77600e2007-02-10 22:15:31 +000031#include "llvm/ADT/SmallVector.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000032#include "llvm/Support/ErrorHandling.h"
Alkis Evlogimenos20aa4742004-09-03 18:19:51 +000033#include <algorithm>
Chris Lattnerec4e8082003-11-25 18:33:40 +000034using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000035
Chris Lattnerdefa1c82008-06-16 06:30:22 +000036//===----------------------------------------------------------------------===//
37// Useful predicates
38//===----------------------------------------------------------------------===//
Devang Patel794fd752007-05-01 21:15:47 +000039
Chris Lattnera4139602008-06-16 06:10:11 +000040/// isKnownNonNull - Return true if we know that the specified value is never
41/// null.
42static bool isKnownNonNull(const Value *V) {
43 // Alloca never returns null, malloc might.
44 if (isa<AllocaInst>(V)) return true;
45
46 // A byval argument is never null.
47 if (const Argument *A = dyn_cast<Argument>(V))
48 return A->hasByValAttr();
49
50 // Global values are not null unless extern weak.
51 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
52 return !GV->hasExternalWeakLinkage();
53 return false;
54}
55
Chris Lattner845f0d22008-06-16 06:19:11 +000056/// isNonEscapingLocalObject - Return true if the pointer is to a function-local
57/// object that never escapes from the function.
Dan Gohman21de4c02010-07-01 20:08:40 +000058static bool isNonEscapingLocalObject(const Value *V) {
Chris Lattnere7275792008-06-16 06:28:01 +000059 // If this is a local allocation, check to see if it escapes.
Dan Gohman21de4c02010-07-01 20:08:40 +000060 if (isa<AllocaInst>(V) || isNoAliasCall(V))
Dan Gohmanf94b5ed2009-11-19 21:57:48 +000061 // Set StoreCaptures to True so that we can assume in our callers that the
62 // pointer is not the result of a load instruction. Currently
63 // PointerMayBeCaptured doesn't have any special analysis for the
64 // StoreCaptures=false case; if it did, our callers could be refined to be
65 // more precise.
66 return !PointerMayBeCaptured(V, false, /*StoreCaptures=*/true);
Duncan Sands91c9c3102009-01-05 21:19:53 +000067
Chris Lattnere7275792008-06-16 06:28:01 +000068 // If this is an argument that corresponds to a byval or noalias argument,
Duncan Sands91c9c3102009-01-05 21:19:53 +000069 // then it has not escaped before entering the function. Check if it escapes
70 // inside the function.
Dan Gohman21de4c02010-07-01 20:08:40 +000071 if (const Argument *A = dyn_cast<Argument>(V))
72 if (A->hasByValAttr() || A->hasNoAliasAttr()) {
73 // Don't bother analyzing arguments already known not to escape.
74 if (A->hasNoCaptureAttr())
75 return true;
76 return !PointerMayBeCaptured(V, false, /*StoreCaptures=*/true);
77 }
Chris Lattner845f0d22008-06-16 06:19:11 +000078 return false;
79}
80
Dan Gohman6be2bd52010-06-29 00:50:39 +000081/// isEscapeSource - Return true if the pointer is one which would have
82/// been considered an escape by isNonEscapingLocalObject.
Dan Gohman21de4c02010-07-01 20:08:40 +000083static bool isEscapeSource(const Value *V) {
84 if (isa<CallInst>(V) || isa<InvokeInst>(V) || isa<Argument>(V))
85 return true;
Dan Gohman6be2bd52010-06-29 00:50:39 +000086
87 // The load case works because isNonEscapingLocalObject considers all
88 // stores to be escapes (it passes true for the StoreCaptures argument
89 // to PointerMayBeCaptured).
90 if (isa<LoadInst>(V))
91 return true;
92
93 return false;
94}
Chris Lattner845f0d22008-06-16 06:19:11 +000095
Chris Lattnera4139602008-06-16 06:10:11 +000096/// isObjectSmallerThan - Return true if we can prove that the object specified
97/// by V is smaller than Size.
98static bool isObjectSmallerThan(const Value *V, unsigned Size,
Chris Lattner7b550cc2009-11-06 04:27:31 +000099 const TargetData &TD) {
Chris Lattner295d4e92008-12-08 06:28:54 +0000100 const Type *AccessTy;
101 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
Chris Lattnera4139602008-06-16 06:10:11 +0000102 AccessTy = GV->getType()->getElementType();
Victor Hernandez7b929da2009-10-23 21:09:37 +0000103 } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
Chris Lattnera4139602008-06-16 06:10:11 +0000104 if (!AI->isArrayAllocation())
105 AccessTy = AI->getType()->getElementType();
Chris Lattner295d4e92008-12-08 06:28:54 +0000106 else
107 return false;
Victor Hernandez46e83122009-09-18 21:34:51 +0000108 } else if (const CallInst* CI = extractMallocCall(V)) {
Chris Lattner7b550cc2009-11-06 04:27:31 +0000109 if (!isArrayMalloc(V, &TD))
Victor Hernandez46e83122009-09-18 21:34:51 +0000110 // The size is the argument to the malloc call.
Gabor Greif71339c92010-06-23 23:38:07 +0000111 if (const ConstantInt* C = dyn_cast<ConstantInt>(CI->getArgOperand(0)))
Victor Hernandez46e83122009-09-18 21:34:51 +0000112 return (C->getZExtValue() < Size);
113 return false;
Chris Lattner295d4e92008-12-08 06:28:54 +0000114 } else if (const Argument *A = dyn_cast<Argument>(V)) {
Chris Lattnera4139602008-06-16 06:10:11 +0000115 if (A->hasByValAttr())
116 AccessTy = cast<PointerType>(A->getType())->getElementType();
Chris Lattner295d4e92008-12-08 06:28:54 +0000117 else
118 return false;
119 } else {
120 return false;
121 }
Chris Lattnera4139602008-06-16 06:10:11 +0000122
Chris Lattner295d4e92008-12-08 06:28:54 +0000123 if (AccessTy->isSized())
Duncan Sands777d2302009-05-09 07:06:46 +0000124 return TD.getTypeAllocSize(AccessTy) < Size;
Chris Lattnera4139602008-06-16 06:10:11 +0000125 return false;
126}
127
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000128//===----------------------------------------------------------------------===//
129// NoAA Pass
130//===----------------------------------------------------------------------===//
131
132namespace {
133 /// NoAA - This class implements the -no-aa pass, which always returns "I
134 /// don't know" for alias queries. NoAA is unlike other alias analysis
135 /// implementations, in that it does not chain to a previous analysis. As
136 /// such it doesn't follow many of the rules that other alias analyses must.
137 ///
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000138 struct NoAA : public ImmutablePass, public AliasAnalysis {
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000139 static char ID; // Class identification, replacement for typeinfo
Dan Gohmanae73dc12008-09-04 17:05:41 +0000140 NoAA() : ImmutablePass(&ID) {}
141 explicit NoAA(void *PID) : ImmutablePass(PID) { }
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000142
143 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000144 }
145
146 virtual void initializePass() {
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +0000147 TD = getAnalysisIfAvailable<TargetData>();
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000148 }
149
150 virtual AliasResult alias(const Value *V1, unsigned V1Size,
151 const Value *V2, unsigned V2Size) {
152 return MayAlias;
153 }
154
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000155 virtual void getArgumentAccesses(Function *F, CallSite CS,
156 std::vector<PointerAccessInfo> &Info) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000157 llvm_unreachable("This method may not be called on this function!");
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000158 }
159
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000160 virtual bool pointsToConstantMemory(const Value *P) { return false; }
161 virtual ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size) {
162 return ModRef;
163 }
164 virtual ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) {
165 return ModRef;
166 }
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000167
168 virtual void deleteValue(Value *V) {}
169 virtual void copyValue(Value *From, Value *To) {}
Chris Lattner20330972010-01-20 19:26:14 +0000170
171 /// getAdjustedAnalysisPointer - This method is used when a pass implements
172 /// an analysis interface through multiple inheritance. If needed, it should
173 /// override this to adjust the this pointer as needed for the specified pass
174 /// info.
Owen Anderson8be32912010-07-20 08:26:15 +0000175 virtual void *getAdjustedAnalysisPointer(const PassInfo *PI) {
Chris Lattner20330972010-01-20 19:26:14 +0000176 if (PI->isPassID(&AliasAnalysis::ID))
177 return (AliasAnalysis*)this;
178 return this;
179 }
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000180 };
181} // End of anonymous namespace
182
183// Register this pass...
184char NoAA::ID = 0;
Owen Andersond8cc7be2010-07-21 23:07:00 +0000185INITIALIZE_AG_PASS(NoAA, AliasAnalysis, "no-aa",
186 "No Alias Analysis (always returns 'may' alias)",
187 true, true, false);
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000188
189ImmutablePass *llvm::createNoAAPass() { return new NoAA(); }
190
191//===----------------------------------------------------------------------===//
Dan Gohman6be2bd52010-06-29 00:50:39 +0000192// BasicAliasAnalysis Pass
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000193//===----------------------------------------------------------------------===//
194
Dan Gohman9e86f432010-07-07 14:27:09 +0000195#ifndef NDEBUG
Dan Gohman6be2bd52010-06-29 00:50:39 +0000196static const Function *getParent(const Value *V) {
Dan Gohman6f205cb2010-06-29 18:12:34 +0000197 if (const Instruction *inst = dyn_cast<Instruction>(V))
Dan Gohman6be2bd52010-06-29 00:50:39 +0000198 return inst->getParent()->getParent();
199
Dan Gohman6f205cb2010-06-29 18:12:34 +0000200 if (const Argument *arg = dyn_cast<Argument>(V))
Dan Gohman6be2bd52010-06-29 00:50:39 +0000201 return arg->getParent();
202
203 return NULL;
204}
205
Dan Gohman21de4c02010-07-01 20:08:40 +0000206static bool notDifferentParent(const Value *O1, const Value *O2) {
207
208 const Function *F1 = getParent(O1);
209 const Function *F2 = getParent(O2);
210
Dan Gohman6be2bd52010-06-29 00:50:39 +0000211 return !F1 || !F2 || F1 == F2;
212}
Benjamin Kramer3432d702010-06-29 10:03:11 +0000213#endif
Dan Gohman6be2bd52010-06-29 00:50:39 +0000214
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000215namespace {
216 /// BasicAliasAnalysis - This is the default alias analysis implementation.
217 /// Because it doesn't chain to a previous alias analysis (like -no-aa), it
218 /// derives from the NoAA class.
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000219 struct BasicAliasAnalysis : public NoAA {
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000220 static char ID; // Class identification, replacement for typeinfo
Dan Gohman9e86f432010-07-07 14:27:09 +0000221 BasicAliasAnalysis() : NoAA(&ID) {}
Dan Gohman6be2bd52010-06-29 00:50:39 +0000222
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000223 AliasResult alias(const Value *V1, unsigned V1Size,
Evan Cheng50a59142009-10-13 22:02:20 +0000224 const Value *V2, unsigned V2Size) {
Dan Gohman50f424c2010-06-28 21:16:52 +0000225 assert(Visited.empty() && "Visited must be cleared after use!");
Dan Gohman9e86f432010-07-07 14:27:09 +0000226 assert(notDifferentParent(V1, V2) &&
227 "BasicAliasAnalysis doesn't support interprocedural queries.");
Evan Chengf0429fd2009-10-14 06:46:26 +0000228 AliasResult Alias = aliasCheck(V1, V1Size, V2, V2Size);
Dan Gohman50f424c2010-06-28 21:16:52 +0000229 Visited.clear();
Evan Chengf0429fd2009-10-14 06:46:26 +0000230 return Alias;
Evan Cheng50a59142009-10-13 22:02:20 +0000231 }
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000232
233 ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
Chris Lattner20d6f092008-12-09 21:19:42 +0000234 ModRefResult getModRefInfo(CallSite CS1, CallSite CS2);
Owen Andersone7942202009-02-05 23:36:27 +0000235
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000236 /// pointsToConstantMemory - Chase pointers until we find a (constant
237 /// global) or not.
238 bool pointsToConstantMemory(const Value *P);
239
Chris Lattner20330972010-01-20 19:26:14 +0000240 /// getAdjustedAnalysisPointer - This method is used when a pass implements
241 /// an analysis interface through multiple inheritance. If needed, it should
242 /// override this to adjust the this pointer as needed for the specified pass
243 /// info.
Owen Anderson8be32912010-07-20 08:26:15 +0000244 virtual void *getAdjustedAnalysisPointer(const PassInfo *PI) {
Chris Lattner20330972010-01-20 19:26:14 +0000245 if (PI->isPassID(&AliasAnalysis::ID))
246 return (AliasAnalysis*)this;
247 return this;
248 }
249
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000250 private:
Dan Gohman50f424c2010-06-28 21:16:52 +0000251 // Visited - Track instructions visited by a aliasPHI, aliasSelect(), and aliasGEP().
252 SmallPtrSet<const Value*, 16> Visited;
Evan Cheng3dbe43b2009-10-14 05:05:02 +0000253
Chris Lattner5d56b2d2009-11-23 16:45:27 +0000254 // aliasGEP - Provide a bunch of ad-hoc rules to disambiguate a GEP
255 // instruction against another.
Chris Lattner539c9b92009-11-26 02:11:08 +0000256 AliasResult aliasGEP(const GEPOperator *V1, unsigned V1Size,
Chris Lattner23e2a5b2009-11-26 02:14:59 +0000257 const Value *V2, unsigned V2Size,
258 const Value *UnderlyingV1, const Value *UnderlyingV2);
Evan Cheng50a59142009-10-13 22:02:20 +0000259
Chris Lattner5d56b2d2009-11-23 16:45:27 +0000260 // aliasPHI - Provide a bunch of ad-hoc rules to disambiguate a PHI
261 // instruction against another.
Evan Chengd83c2ca2009-10-14 05:22:03 +0000262 AliasResult aliasPHI(const PHINode *PN, unsigned PNSize,
Evan Cheng3dbe43b2009-10-14 05:05:02 +0000263 const Value *V2, unsigned V2Size);
Evan Cheng50a59142009-10-13 22:02:20 +0000264
Dan Gohman6665b0e2009-10-26 21:55:43 +0000265 /// aliasSelect - Disambiguate a Select instruction against another value.
266 AliasResult aliasSelect(const SelectInst *SI, unsigned SISize,
267 const Value *V2, unsigned V2Size);
268
Evan Cheng50a59142009-10-13 22:02:20 +0000269 AliasResult aliasCheck(const Value *V1, unsigned V1Size,
Evan Cheng3dbe43b2009-10-14 05:05:02 +0000270 const Value *V2, unsigned V2Size);
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000271 };
272} // End of anonymous namespace
273
274// Register this pass...
275char BasicAliasAnalysis::ID = 0;
Owen Andersond8cc7be2010-07-21 23:07:00 +0000276INITIALIZE_AG_PASS(BasicAliasAnalysis, AliasAnalysis, "basicaa",
277 "Basic Alias Analysis (default AA impl)",
278 false, true, true);
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000279
280ImmutablePass *llvm::createBasicAliasAnalysisPass() {
281 return new BasicAliasAnalysis();
282}
283
284
285/// pointsToConstantMemory - Chase pointers until we find a (constant
286/// global) or not.
287bool BasicAliasAnalysis::pointsToConstantMemory(const Value *P) {
288 if (const GlobalVariable *GV =
Duncan Sands5d0392c2008-10-01 15:25:41 +0000289 dyn_cast<GlobalVariable>(P->getUnderlyingObject()))
Chris Lattnerb27db372009-11-23 17:07:35 +0000290 // Note: this doesn't require GV to be "ODR" because it isn't legal for a
291 // global to be marked constant in some modules and non-constant in others.
292 // GV may even be a declaration, not a definition.
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000293 return GV->isConstant();
294 return false;
295}
296
Owen Andersone7942202009-02-05 23:36:27 +0000297
Chris Lattner5d56b2d2009-11-23 16:45:27 +0000298/// getModRefInfo - Check to see if the specified callsite can clobber the
299/// specified memory object. Since we only look at local properties of this
300/// function, we really can't say much about this query. We do, however, use
301/// simple "address taken" analysis on local objects.
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000302AliasAnalysis::ModRefResult
303BasicAliasAnalysis::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
Dan Gohman9e86f432010-07-07 14:27:09 +0000304 assert(notDifferentParent(CS.getInstruction(), P) &&
305 "AliasAnalysis query involving multiple functions!");
306
Chris Lattner92e803c2009-11-22 16:05:05 +0000307 const Value *Object = P->getUnderlyingObject();
308
309 // If this is a tail call and P points to a stack location, we know that
310 // the tail call cannot access or modify the local stack.
311 // We cannot exclude byval arguments here; these belong to the caller of
312 // the current function not to the current function, and a tail callee
313 // may reference them.
314 if (isa<AllocaInst>(Object))
315 if (CallInst *CI = dyn_cast<CallInst>(CS.getInstruction()))
316 if (CI->isTailCall())
317 return NoModRef;
318
319 // If the pointer is to a locally allocated object that does not escape,
Chris Lattnerb34b82e2009-11-23 16:44:43 +0000320 // then the call can not mod/ref the pointer unless the call takes the pointer
321 // as an argument, and itself doesn't capture it.
Chris Lattner403ac2e2009-11-23 16:46:41 +0000322 if (!isa<Constant>(Object) && CS.getInstruction() != Object &&
Dan Gohman21de4c02010-07-01 20:08:40 +0000323 isNonEscapingLocalObject(Object)) {
Chris Lattnerb34b82e2009-11-23 16:44:43 +0000324 bool PassedAsArg = false;
325 unsigned ArgNo = 0;
Chris Lattner92e803c2009-11-22 16:05:05 +0000326 for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
Chris Lattnerb34b82e2009-11-23 16:44:43 +0000327 CI != CE; ++CI, ++ArgNo) {
328 // Only look at the no-capture pointer arguments.
Duncan Sands1df98592010-02-16 11:11:14 +0000329 if (!(*CI)->getType()->isPointerTy() ||
Chris Lattnerb34b82e2009-11-23 16:44:43 +0000330 !CS.paramHasAttr(ArgNo+1, Attribute::NoCapture))
331 continue;
332
333 // If this is a no-capture pointer argument, see if we can tell that it
334 // is impossible to alias the pointer we're checking. If not, we have to
335 // assume that the call could touch the pointer, even though it doesn't
336 // escape.
Dan Gohmanef1cfac2010-08-03 01:03:11 +0000337 if (!isNoAlias(cast<Value>(CI), UnknownSize, P, UnknownSize)) {
Chris Lattnerb34b82e2009-11-23 16:44:43 +0000338 PassedAsArg = true;
339 break;
340 }
341 }
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000342
Chris Lattnerb34b82e2009-11-23 16:44:43 +0000343 if (!PassedAsArg)
Chris Lattner92e803c2009-11-22 16:05:05 +0000344 return NoModRef;
345 }
346
347 // Finally, handle specific knowledge of intrinsics.
348 IntrinsicInst *II = dyn_cast<IntrinsicInst>(CS.getInstruction());
349 if (II == 0)
350 return AliasAnalysis::getModRefInfo(CS, P, Size);
351
352 switch (II->getIntrinsicID()) {
353 default: break;
354 case Intrinsic::memcpy:
355 case Intrinsic::memmove: {
Dan Gohmanef1cfac2010-08-03 01:03:11 +0000356 unsigned Len = UnknownSize;
Gabor Greif71339c92010-06-23 23:38:07 +0000357 if (ConstantInt *LenCI = dyn_cast<ConstantInt>(II->getArgOperand(2)))
Chris Lattner92e803c2009-11-22 16:05:05 +0000358 Len = LenCI->getZExtValue();
Gabor Greif71339c92010-06-23 23:38:07 +0000359 Value *Dest = II->getArgOperand(0);
360 Value *Src = II->getArgOperand(1);
Chris Lattner403ac2e2009-11-23 16:46:41 +0000361 if (isNoAlias(Dest, Len, P, Size)) {
362 if (isNoAlias(Src, Len, P, Size))
Chris Lattner92e803c2009-11-22 16:05:05 +0000363 return NoModRef;
364 return Ref;
365 }
366 break;
367 }
368 case Intrinsic::memset:
Chris Lattner403ac2e2009-11-23 16:46:41 +0000369 // Since memset is 'accesses arguments' only, the AliasAnalysis base class
370 // will handle it for the variable length case.
Gabor Greif71339c92010-06-23 23:38:07 +0000371 if (ConstantInt *LenCI = dyn_cast<ConstantInt>(II->getArgOperand(2))) {
Chris Lattner92e803c2009-11-22 16:05:05 +0000372 unsigned Len = LenCI->getZExtValue();
Gabor Greif71339c92010-06-23 23:38:07 +0000373 Value *Dest = II->getArgOperand(0);
Chris Lattner403ac2e2009-11-23 16:46:41 +0000374 if (isNoAlias(Dest, Len, P, Size))
Chris Lattner25df20f2008-06-16 06:38:26 +0000375 return NoModRef;
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000376 }
Chris Lattner92e803c2009-11-22 16:05:05 +0000377 break;
378 case Intrinsic::atomic_cmp_swap:
379 case Intrinsic::atomic_swap:
380 case Intrinsic::atomic_load_add:
381 case Intrinsic::atomic_load_sub:
382 case Intrinsic::atomic_load_and:
383 case Intrinsic::atomic_load_nand:
384 case Intrinsic::atomic_load_or:
385 case Intrinsic::atomic_load_xor:
386 case Intrinsic::atomic_load_max:
387 case Intrinsic::atomic_load_min:
388 case Intrinsic::atomic_load_umax:
389 case Intrinsic::atomic_load_umin:
390 if (TD) {
Gabor Greif71339c92010-06-23 23:38:07 +0000391 Value *Op1 = II->getArgOperand(0);
Chris Lattner92e803c2009-11-22 16:05:05 +0000392 unsigned Op1Size = TD->getTypeStoreSize(Op1->getType());
Chris Lattner403ac2e2009-11-23 16:46:41 +0000393 if (isNoAlias(Op1, Op1Size, P, Size))
Chris Lattner92e803c2009-11-22 16:05:05 +0000394 return NoModRef;
Nick Lewycky5c9be672009-10-13 07:48:38 +0000395 }
Chris Lattner92e803c2009-11-22 16:05:05 +0000396 break;
397 case Intrinsic::lifetime_start:
398 case Intrinsic::lifetime_end:
399 case Intrinsic::invariant_start: {
Gabor Greif71339c92010-06-23 23:38:07 +0000400 unsigned PtrSize = cast<ConstantInt>(II->getArgOperand(0))->getZExtValue();
401 if (isNoAlias(II->getArgOperand(1), PtrSize, P, Size))
Chris Lattner92e803c2009-11-22 16:05:05 +0000402 return NoModRef;
403 break;
404 }
405 case Intrinsic::invariant_end: {
Gabor Greif71339c92010-06-23 23:38:07 +0000406 unsigned PtrSize = cast<ConstantInt>(II->getArgOperand(1))->getZExtValue();
407 if (isNoAlias(II->getArgOperand(2), PtrSize, P, Size))
Chris Lattner92e803c2009-11-22 16:05:05 +0000408 return NoModRef;
409 break;
410 }
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000411 }
412
413 // The AliasAnalysis base class has some smarts, lets use them.
414 return AliasAnalysis::getModRefInfo(CS, P, Size);
415}
416
417
Chris Lattner20d6f092008-12-09 21:19:42 +0000418AliasAnalysis::ModRefResult
419BasicAliasAnalysis::getModRefInfo(CallSite CS1, CallSite CS2) {
420 // If CS1 or CS2 are readnone, they don't interact.
421 ModRefBehavior CS1B = AliasAnalysis::getModRefBehavior(CS1);
422 if (CS1B == DoesNotAccessMemory) return NoModRef;
423
424 ModRefBehavior CS2B = AliasAnalysis::getModRefBehavior(CS2);
425 if (CS2B == DoesNotAccessMemory) return NoModRef;
426
427 // If they both only read from memory, just return ref.
428 if (CS1B == OnlyReadsMemory && CS2B == OnlyReadsMemory)
429 return Ref;
430
431 // Otherwise, fall back to NoAA (mod+ref).
432 return NoAA::getModRefInfo(CS1, CS2);
433}
434
Chris Lattnerd84eb912009-11-26 02:17:34 +0000435/// GetIndiceDifference - Dest and Src are the variable indices from two
436/// decomposed GetElementPtr instructions GEP1 and GEP2 which have common base
437/// pointers. Subtract the GEP2 indices from GEP1 to find the symbolic
438/// difference between the two pointers.
439static void GetIndiceDifference(
440 SmallVectorImpl<std::pair<const Value*, int64_t> > &Dest,
441 const SmallVectorImpl<std::pair<const Value*, int64_t> > &Src) {
442 if (Src.empty()) return;
443
444 for (unsigned i = 0, e = Src.size(); i != e; ++i) {
445 const Value *V = Src[i].first;
446 int64_t Scale = Src[i].second;
447
448 // Find V in Dest. This is N^2, but pointer indices almost never have more
449 // than a few variable indexes.
450 for (unsigned j = 0, e = Dest.size(); j != e; ++j) {
451 if (Dest[j].first != V) continue;
452
453 // If we found it, subtract off Scale V's from the entry in Dest. If it
454 // goes to zero, remove the entry.
455 if (Dest[j].second != Scale)
456 Dest[j].second -= Scale;
457 else
458 Dest.erase(Dest.begin()+j);
459 Scale = 0;
460 break;
461 }
462
463 // If we didn't consume this entry, add it to the end of the Dest list.
464 if (Scale)
465 Dest.push_back(std::make_pair(V, -Scale));
466 }
467}
Chris Lattner4e91ee72009-11-26 02:13:03 +0000468
Chris Lattner539c9b92009-11-26 02:11:08 +0000469/// aliasGEP - Provide a bunch of ad-hoc rules to disambiguate a GEP instruction
470/// against another pointer. We know that V1 is a GEP, but we don't know
Chris Lattner23e2a5b2009-11-26 02:14:59 +0000471/// anything about V2. UnderlyingV1 is GEP1->getUnderlyingObject(),
472/// UnderlyingV2 is the same for V2.
Chris Lattner539c9b92009-11-26 02:11:08 +0000473///
Chris Lattnerd501c132003-02-26 19:41:54 +0000474AliasAnalysis::AliasResult
Chris Lattner539c9b92009-11-26 02:11:08 +0000475BasicAliasAnalysis::aliasGEP(const GEPOperator *GEP1, unsigned V1Size,
Chris Lattner23e2a5b2009-11-26 02:14:59 +0000476 const Value *V2, unsigned V2Size,
477 const Value *UnderlyingV1,
478 const Value *UnderlyingV2) {
Dan Gohman50f424c2010-06-28 21:16:52 +0000479 // If this GEP has been visited before, we're on a use-def cycle.
480 // Such cycles are only valid when PHI nodes are involved or in unreachable
481 // code. The visitPHI function catches cycles containing PHIs, but there
482 // could still be a cycle without PHIs in unreachable code.
483 if (!Visited.insert(GEP1))
484 return MayAlias;
485
Chris Lattnerd84eb912009-11-26 02:17:34 +0000486 int64_t GEP1BaseOffset;
487 SmallVector<std::pair<const Value*, int64_t>, 4> GEP1VariableIndices;
488
Chris Lattnerb307c882003-12-11 22:44:13 +0000489 // If we have two gep instructions with must-alias'ing base pointers, figure
490 // out if the indexes to the GEP tell us anything about the derived pointer.
Chris Lattner539c9b92009-11-26 02:11:08 +0000491 if (const GEPOperator *GEP2 = dyn_cast<GEPOperator>(V2)) {
Chris Lattnerb307c882003-12-11 22:44:13 +0000492 // Do the base pointers alias?
Dan Gohmanef1cfac2010-08-03 01:03:11 +0000493 AliasResult BaseAlias = aliasCheck(UnderlyingV1, UnknownSize,
494 UnderlyingV2, UnknownSize);
Chris Lattnerd84eb912009-11-26 02:17:34 +0000495
496 // If we get a No or May, then return it immediately, no amount of analysis
497 // will improve this situation.
498 if (BaseAlias != MustAlias) return BaseAlias;
499
500 // Otherwise, we have a MustAlias. Since the base pointers alias each other
501 // exactly, see if the computed offset from the common pointer tells us
502 // about the relation of the resulting pointer.
503 const Value *GEP1BasePtr =
504 DecomposeGEPExpression(GEP1, GEP1BaseOffset, GEP1VariableIndices, TD);
505
506 int64_t GEP2BaseOffset;
507 SmallVector<std::pair<const Value*, int64_t>, 4> GEP2VariableIndices;
508 const Value *GEP2BasePtr =
509 DecomposeGEPExpression(GEP2, GEP2BaseOffset, GEP2VariableIndices, TD);
510
511 // If DecomposeGEPExpression isn't able to look all the way through the
512 // addressing operation, we must not have TD and this is too complex for us
513 // to handle without it.
514 if (GEP1BasePtr != UnderlyingV1 || GEP2BasePtr != UnderlyingV2) {
515 assert(TD == 0 &&
516 "DecomposeGEPExpression and getUnderlyingObject disagree!");
517 return MayAlias;
518 }
519
520 // Subtract the GEP2 pointer from the GEP1 pointer to find out their
521 // symbolic difference.
522 GEP1BaseOffset -= GEP2BaseOffset;
523 GetIndiceDifference(GEP1VariableIndices, GEP2VariableIndices);
524
525 } else {
526 // Check to see if these two pointers are related by the getelementptr
527 // instruction. If one pointer is a GEP with a non-zero index of the other
528 // pointer, we know they cannot alias.
Chris Lattner53692502009-11-26 16:52:32 +0000529
530 // If both accesses are unknown size, we can't do anything useful here.
Dan Gohmanef1cfac2010-08-03 01:03:11 +0000531 if (V1Size == UnknownSize && V2Size == UnknownSize)
Chris Lattnerd84eb912009-11-26 02:17:34 +0000532 return MayAlias;
Chris Lattnerb307c882003-12-11 22:44:13 +0000533
Dan Gohmanef1cfac2010-08-03 01:03:11 +0000534 AliasResult R = aliasCheck(UnderlyingV1, UnknownSize, V2, V2Size);
Chris Lattnerd84eb912009-11-26 02:17:34 +0000535 if (R != MustAlias)
536 // If V2 may alias GEP base pointer, conservatively returns MayAlias.
537 // If V2 is known not to alias GEP base pointer, then the two values
538 // cannot alias per GEP semantics: "A pointer value formed from a
539 // getelementptr instruction is associated with the addresses associated
540 // with the first operand of the getelementptr".
541 return R;
Chris Lattnerb307c882003-12-11 22:44:13 +0000542
Chris Lattnerd84eb912009-11-26 02:17:34 +0000543 const Value *GEP1BasePtr =
544 DecomposeGEPExpression(GEP1, GEP1BaseOffset, GEP1VariableIndices, TD);
545
546 // If DecomposeGEPExpression isn't able to look all the way through the
547 // addressing operation, we must not have TD and this is too complex for us
548 // to handle without it.
549 if (GEP1BasePtr != UnderlyingV1) {
550 assert(TD == 0 &&
551 "DecomposeGEPExpression and getUnderlyingObject disagree!");
552 return MayAlias;
Chris Lattnerb307c882003-12-11 22:44:13 +0000553 }
554 }
Chris Lattnerd84eb912009-11-26 02:17:34 +0000555
556 // In the two GEP Case, if there is no difference in the offsets of the
557 // computed pointers, the resultant pointers are a must alias. This
558 // hapens when we have two lexically identical GEP's (for example).
Chris Lattnerd501c132003-02-26 19:41:54 +0000559 //
Chris Lattnerd84eb912009-11-26 02:17:34 +0000560 // In the other case, if we have getelementptr <ptr>, 0, 0, 0, 0, ... and V2
561 // must aliases the GEP, the end result is a must alias also.
562 if (GEP1BaseOffset == 0 && GEP1VariableIndices.empty())
Evan Cheng681a33e2009-10-14 06:41:49 +0000563 return MustAlias;
Evan Cheng094f04b2009-10-13 18:42:04 +0000564
Chris Lattner4e91ee72009-11-26 02:13:03 +0000565 // If we have a known constant offset, see if this offset is larger than the
566 // access size being queried. If so, and if no variable indices can remove
567 // pieces of this constant, then we know we have a no-alias. For example,
568 // &A[100] != &A.
569
570 // In order to handle cases like &A[100][i] where i is an out of range
571 // subscript, we have to ignore all constant offset pieces that are a multiple
572 // of a scaled index. Do this by removing constant offsets that are a
573 // multiple of any of our variable indices. This allows us to transform
574 // things like &A[i][1] because i has a stride of (e.g.) 8 bytes but the 1
575 // provides an offset of 4 bytes (assuming a <= 4 byte access).
Chris Lattnerd84eb912009-11-26 02:17:34 +0000576 for (unsigned i = 0, e = GEP1VariableIndices.size();
577 i != e && GEP1BaseOffset;++i)
578 if (int64_t RemovedOffset = GEP1BaseOffset/GEP1VariableIndices[i].second)
579 GEP1BaseOffset -= RemovedOffset*GEP1VariableIndices[i].second;
Chris Lattner4e91ee72009-11-26 02:13:03 +0000580
581 // If our known offset is bigger than the access size, we know we don't have
582 // an alias.
583 if (GEP1BaseOffset) {
584 if (GEP1BaseOffset >= (int64_t)V2Size ||
585 GEP1BaseOffset <= -(int64_t)V1Size)
Evan Cheng681a33e2009-10-14 06:41:49 +0000586 return NoAlias;
Evan Cheng094f04b2009-10-13 18:42:04 +0000587 }
Chris Lattner4e91ee72009-11-26 02:13:03 +0000588
Evan Cheng094f04b2009-10-13 18:42:04 +0000589 return MayAlias;
590}
591
Chris Lattner5d56b2d2009-11-23 16:45:27 +0000592/// aliasSelect - Provide a bunch of ad-hoc rules to disambiguate a Select
593/// instruction against another.
Dan Gohman6665b0e2009-10-26 21:55:43 +0000594AliasAnalysis::AliasResult
595BasicAliasAnalysis::aliasSelect(const SelectInst *SI, unsigned SISize,
596 const Value *V2, unsigned V2Size) {
Dan Gohman50f424c2010-06-28 21:16:52 +0000597 // If this select has been visited before, we're on a use-def cycle.
598 // Such cycles are only valid when PHI nodes are involved or in unreachable
599 // code. The visitPHI function catches cycles containing PHIs, but there
600 // could still be a cycle without PHIs in unreachable code.
601 if (!Visited.insert(SI))
602 return MayAlias;
603
Dan Gohman6665b0e2009-10-26 21:55:43 +0000604 // If the values are Selects with the same condition, we can do a more precise
605 // check: just check for aliases between the values on corresponding arms.
606 if (const SelectInst *SI2 = dyn_cast<SelectInst>(V2))
607 if (SI->getCondition() == SI2->getCondition()) {
608 AliasResult Alias =
609 aliasCheck(SI->getTrueValue(), SISize,
610 SI2->getTrueValue(), V2Size);
611 if (Alias == MayAlias)
612 return MayAlias;
613 AliasResult ThisAlias =
614 aliasCheck(SI->getFalseValue(), SISize,
615 SI2->getFalseValue(), V2Size);
616 if (ThisAlias != Alias)
617 return MayAlias;
618 return Alias;
619 }
620
621 // If both arms of the Select node NoAlias or MustAlias V2, then returns
622 // NoAlias / MustAlias. Otherwise, returns MayAlias.
623 AliasResult Alias =
Dan Gohman50f424c2010-06-28 21:16:52 +0000624 aliasCheck(V2, V2Size, SI->getTrueValue(), SISize);
Dan Gohman6665b0e2009-10-26 21:55:43 +0000625 if (Alias == MayAlias)
626 return MayAlias;
Dan Gohman50f424c2010-06-28 21:16:52 +0000627
628 // If V2 is visited, the recursive case will have been caught in the
629 // above aliasCheck call, so these subsequent calls to aliasCheck
630 // don't need to assume that V2 is being visited recursively.
631 Visited.erase(V2);
632
Dan Gohman6665b0e2009-10-26 21:55:43 +0000633 AliasResult ThisAlias =
Dan Gohman50f424c2010-06-28 21:16:52 +0000634 aliasCheck(V2, V2Size, SI->getFalseValue(), SISize);
Dan Gohman6665b0e2009-10-26 21:55:43 +0000635 if (ThisAlias != Alias)
636 return MayAlias;
637 return Alias;
638}
639
Evan Chengd83c2ca2009-10-14 05:22:03 +0000640// aliasPHI - Provide a bunch of ad-hoc rules to disambiguate a PHI instruction
Evan Cheng3dbe43b2009-10-14 05:05:02 +0000641// against another.
Evan Cheng50a59142009-10-13 22:02:20 +0000642AliasAnalysis::AliasResult
Evan Chengd83c2ca2009-10-14 05:22:03 +0000643BasicAliasAnalysis::aliasPHI(const PHINode *PN, unsigned PNSize,
Evan Cheng3dbe43b2009-10-14 05:05:02 +0000644 const Value *V2, unsigned V2Size) {
Evan Cheng50a59142009-10-13 22:02:20 +0000645 // The PHI node has already been visited, avoid recursion any further.
Dan Gohman50f424c2010-06-28 21:16:52 +0000646 if (!Visited.insert(PN))
Evan Cheng50a59142009-10-13 22:02:20 +0000647 return MayAlias;
648
Dan Gohman6665b0e2009-10-26 21:55:43 +0000649 // If the values are PHIs in the same block, we can do a more precise
650 // as well as efficient check: just check for aliases between the values
651 // on corresponding edges.
652 if (const PHINode *PN2 = dyn_cast<PHINode>(V2))
653 if (PN2->getParent() == PN->getParent()) {
654 AliasResult Alias =
655 aliasCheck(PN->getIncomingValue(0), PNSize,
656 PN2->getIncomingValueForBlock(PN->getIncomingBlock(0)),
657 V2Size);
658 if (Alias == MayAlias)
659 return MayAlias;
660 for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) {
661 AliasResult ThisAlias =
662 aliasCheck(PN->getIncomingValue(i), PNSize,
663 PN2->getIncomingValueForBlock(PN->getIncomingBlock(i)),
664 V2Size);
665 if (ThisAlias != Alias)
666 return MayAlias;
667 }
668 return Alias;
669 }
670
Evan Chenga846a8a2009-10-16 00:33:09 +0000671 SmallPtrSet<Value*, 4> UniqueSrc;
Evan Cheng50a59142009-10-13 22:02:20 +0000672 SmallVector<Value*, 4> V1Srcs;
Evan Cheng50a59142009-10-13 22:02:20 +0000673 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
674 Value *PV1 = PN->getIncomingValue(i);
675 if (isa<PHINode>(PV1))
676 // If any of the source itself is a PHI, return MayAlias conservatively
Evan Cheng681a33e2009-10-14 06:41:49 +0000677 // to avoid compile time explosion. The worst possible case is if both
678 // sides are PHI nodes. In which case, this is O(m x n) time where 'm'
679 // and 'n' are the number of PHI sources.
Evan Cheng50a59142009-10-13 22:02:20 +0000680 return MayAlias;
681 if (UniqueSrc.insert(PV1))
682 V1Srcs.push_back(PV1);
683 }
684
Dan Gohman6665b0e2009-10-26 21:55:43 +0000685 AliasResult Alias = aliasCheck(V2, V2Size, V1Srcs[0], PNSize);
Evan Chengd83c2ca2009-10-14 05:22:03 +0000686 // Early exit if the check of the first PHI source against V2 is MayAlias.
687 // Other results are not possible.
688 if (Alias == MayAlias)
689 return MayAlias;
690
Evan Cheng50a59142009-10-13 22:02:20 +0000691 // If all sources of the PHI node NoAlias or MustAlias V2, then returns
692 // NoAlias / MustAlias. Otherwise, returns MayAlias.
Evan Cheng50a59142009-10-13 22:02:20 +0000693 for (unsigned i = 1, e = V1Srcs.size(); i != e; ++i) {
694 Value *V = V1Srcs[i];
Dan Gohman6665b0e2009-10-26 21:55:43 +0000695
Dan Gohman50f424c2010-06-28 21:16:52 +0000696 // If V2 is visited, the recursive case will have been caught in the
Dan Gohman6665b0e2009-10-26 21:55:43 +0000697 // above aliasCheck call, so these subsequent calls to aliasCheck
698 // don't need to assume that V2 is being visited recursively.
Dan Gohman50f424c2010-06-28 21:16:52 +0000699 Visited.erase(V2);
Dan Gohman6665b0e2009-10-26 21:55:43 +0000700
Evan Chenga846a8a2009-10-16 00:33:09 +0000701 AliasResult ThisAlias = aliasCheck(V2, V2Size, V, PNSize);
Evan Chengd83c2ca2009-10-14 05:22:03 +0000702 if (ThisAlias != Alias || ThisAlias == MayAlias)
Evan Cheng50a59142009-10-13 22:02:20 +0000703 return MayAlias;
704 }
705
706 return Alias;
707}
708
709// aliasCheck - Provide a bunch of ad-hoc rules to disambiguate in common cases,
710// such as array references.
Evan Cheng094f04b2009-10-13 18:42:04 +0000711//
712AliasAnalysis::AliasResult
Evan Cheng50a59142009-10-13 22:02:20 +0000713BasicAliasAnalysis::aliasCheck(const Value *V1, unsigned V1Size,
Evan Cheng3dbe43b2009-10-14 05:05:02 +0000714 const Value *V2, unsigned V2Size) {
Dan Gohmanb57b6f12010-04-08 18:11:50 +0000715 // If either of the memory references is empty, it doesn't matter what the
716 // pointer values are.
717 if (V1Size == 0 || V2Size == 0)
718 return NoAlias;
719
Evan Cheng094f04b2009-10-13 18:42:04 +0000720 // Strip off any casts if they exist.
721 V1 = V1->stripPointerCasts();
722 V2 = V2->stripPointerCasts();
723
724 // Are we checking for alias of the same value?
725 if (V1 == V2) return MustAlias;
726
Duncan Sands1df98592010-02-16 11:11:14 +0000727 if (!V1->getType()->isPointerTy() || !V2->getType()->isPointerTy())
Evan Cheng094f04b2009-10-13 18:42:04 +0000728 return NoAlias; // Scalars cannot alias each other
729
730 // Figure out what objects these things are pointing to if we can.
731 const Value *O1 = V1->getUnderlyingObject();
732 const Value *O2 = V2->getUnderlyingObject();
733
Dan Gohmanf75ef662009-11-09 19:29:11 +0000734 // Null values in the default address space don't point to any object, so they
735 // don't alias any other pointer.
736 if (const ConstantPointerNull *CPN = dyn_cast<ConstantPointerNull>(O1))
737 if (CPN->getType()->getAddressSpace() == 0)
738 return NoAlias;
739 if (const ConstantPointerNull *CPN = dyn_cast<ConstantPointerNull>(O2))
740 if (CPN->getType()->getAddressSpace() == 0)
741 return NoAlias;
742
Evan Cheng094f04b2009-10-13 18:42:04 +0000743 if (O1 != O2) {
744 // If V1/V2 point to two different objects we know that we have no alias.
Dan Gohman9e86f432010-07-07 14:27:09 +0000745 if (isIdentifiedObject(O1) && isIdentifiedObject(O2))
Evan Cheng094f04b2009-10-13 18:42:04 +0000746 return NoAlias;
Nick Lewycky20162ac2009-11-14 06:15:14 +0000747
748 // Constant pointers can't alias with non-const isIdentifiedObject objects.
Dan Gohman9e86f432010-07-07 14:27:09 +0000749 if ((isa<Constant>(O1) && isIdentifiedObject(O2) && !isa<Constant>(O2)) ||
750 (isa<Constant>(O2) && isIdentifiedObject(O1) && !isa<Constant>(O1)))
Nick Lewycky20162ac2009-11-14 06:15:14 +0000751 return NoAlias;
752
Dan Gohman21de4c02010-07-01 20:08:40 +0000753 // Arguments can't alias with local allocations or noalias calls
754 // in the same function.
Dan Gohman9e86f432010-07-07 14:27:09 +0000755 if (((isa<Argument>(O1) && (isa<AllocaInst>(O2) || isNoAliasCall(O2))) ||
Dan Gohman21de4c02010-07-01 20:08:40 +0000756 (isa<Argument>(O2) && (isa<AllocaInst>(O1) || isNoAliasCall(O1)))))
757 return NoAlias;
Evan Cheng094f04b2009-10-13 18:42:04 +0000758
759 // Most objects can't alias null.
Dan Gohman9e86f432010-07-07 14:27:09 +0000760 if ((isa<ConstantPointerNull>(O2) && isKnownNonNull(O1)) ||
761 (isa<ConstantPointerNull>(O1) && isKnownNonNull(O2)))
Evan Cheng094f04b2009-10-13 18:42:04 +0000762 return NoAlias;
Evan Cheng094f04b2009-10-13 18:42:04 +0000763
Dan Gohmanb8c86a02010-07-07 14:30:04 +0000764 // If one pointer is the result of a call/invoke or load and the other is a
765 // non-escaping local object within the same function, then we know the
766 // object couldn't escape to a point where the call could return it.
767 //
768 // Note that if the pointers are in different functions, there are a
769 // variety of complications. A call with a nocapture argument may still
770 // temporary store the nocapture argument's value in a temporary memory
771 // location if that memory location doesn't escape. Or it may pass a
772 // nocapture value to other functions as long as they don't capture it.
773 if (isEscapeSource(O1) && isNonEscapingLocalObject(O2))
774 return NoAlias;
775 if (isEscapeSource(O2) && isNonEscapingLocalObject(O1))
776 return NoAlias;
777 }
778
Evan Cheng094f04b2009-10-13 18:42:04 +0000779 // If the size of one access is larger than the entire object on the other
780 // side, then we know such behavior is undefined and can assume no alias.
Evan Cheng094f04b2009-10-13 18:42:04 +0000781 if (TD)
Dan Gohmanef1cfac2010-08-03 01:03:11 +0000782 if ((V1Size != UnknownSize && isObjectSmallerThan(O2, V1Size, *TD)) ||
783 (V2Size != UnknownSize && isObjectSmallerThan(O1, V2Size, *TD)))
Evan Cheng094f04b2009-10-13 18:42:04 +0000784 return NoAlias;
785
Chris Lattner4e91ee72009-11-26 02:13:03 +0000786 // FIXME: This isn't aggressively handling alias(GEP, PHI) for example: if the
787 // GEP can't simplify, we don't even look at the PHI cases.
Chris Lattner391d23b2009-10-17 23:48:54 +0000788 if (!isa<GEPOperator>(V1) && isa<GEPOperator>(V2)) {
Chris Lattnerd501c132003-02-26 19:41:54 +0000789 std::swap(V1, V2);
790 std::swap(V1Size, V2Size);
Chris Lattner23e2a5b2009-11-26 02:14:59 +0000791 std::swap(O1, O2);
Chris Lattnerd501c132003-02-26 19:41:54 +0000792 }
Chris Lattner539c9b92009-11-26 02:11:08 +0000793 if (const GEPOperator *GV1 = dyn_cast<GEPOperator>(V1))
Chris Lattner23e2a5b2009-11-26 02:14:59 +0000794 return aliasGEP(GV1, V1Size, V2, V2Size, O1, O2);
Evan Cheng50a59142009-10-13 22:02:20 +0000795
796 if (isa<PHINode>(V2) && !isa<PHINode>(V1)) {
797 std::swap(V1, V2);
798 std::swap(V1Size, V2Size);
799 }
Evan Chengd83c2ca2009-10-14 05:22:03 +0000800 if (const PHINode *PN = dyn_cast<PHINode>(V1))
801 return aliasPHI(PN, V1Size, V2, V2Size);
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000802
Dan Gohman6665b0e2009-10-26 21:55:43 +0000803 if (isa<SelectInst>(V2) && !isa<SelectInst>(V1)) {
804 std::swap(V1, V2);
805 std::swap(V1Size, V2Size);
806 }
807 if (const SelectInst *S1 = dyn_cast<SelectInst>(V1))
808 return aliasSelect(S1, V1Size, V2, V2Size);
809
Chris Lattnerd501c132003-02-26 19:41:54 +0000810 return MayAlias;
811}
812
Chris Lattner5d56b2d2009-11-23 16:45:27 +0000813// Make sure that anything that uses AliasAnalysis pulls in this file.
Reid Spencer4f1bd9e2006-06-07 22:00:26 +0000814DEFINING_FILE_FOR(BasicAliasAnalysis)