blob: c2ae32f0132b357ecac9b7f21a9c20be03846f94 [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 Gohman6be2bd52010-06-29 00:50:39 +000058static bool isNonEscapingLocalObject(const Value *V, bool Interprocedural) {
Chris Lattnere7275792008-06-16 06:28:01 +000059 // If this is a local allocation, check to see if it escapes.
Dan Gohman6be2bd52010-06-29 00:50:39 +000060 if (isa<AllocaInst>(V) ||
61 (!Interprocedural && isNoAliasCall(V)))
Dan Gohmanf94b5ed2009-11-19 21:57:48 +000062 // Set StoreCaptures to True so that we can assume in our callers that the
63 // pointer is not the result of a load instruction. Currently
64 // PointerMayBeCaptured doesn't have any special analysis for the
65 // StoreCaptures=false case; if it did, our callers could be refined to be
66 // more precise.
67 return !PointerMayBeCaptured(V, false, /*StoreCaptures=*/true);
Duncan Sands91c9c3102009-01-05 21:19:53 +000068
Chris Lattnere7275792008-06-16 06:28:01 +000069 // If this is an argument that corresponds to a byval or noalias argument,
Duncan Sands91c9c3102009-01-05 21:19:53 +000070 // then it has not escaped before entering the function. Check if it escapes
71 // inside the function.
Dan Gohman6be2bd52010-06-29 00:50:39 +000072 if (!Interprocedural)
73 if (const Argument *A = dyn_cast<Argument>(V))
74 if (A->hasByValAttr() || A->hasNoAliasAttr()) {
75 // Don't bother analyzing arguments already known not to escape.
76 if (A->hasNoCaptureAttr())
77 return true;
78 return !PointerMayBeCaptured(V, false, /*StoreCaptures=*/true);
79 }
Chris Lattner845f0d22008-06-16 06:19:11 +000080 return false;
81}
82
Dan Gohman6be2bd52010-06-29 00:50:39 +000083/// isEscapeSource - Return true if the pointer is one which would have
84/// been considered an escape by isNonEscapingLocalObject.
85static bool isEscapeSource(const Value *V, bool Interprocedural) {
86 if (!Interprocedural)
87 if (isa<CallInst>(V) || isa<InvokeInst>(V) || isa<Argument>(V))
88 return true;
89
90 // The load case works because isNonEscapingLocalObject considers all
91 // stores to be escapes (it passes true for the StoreCaptures argument
92 // to PointerMayBeCaptured).
93 if (isa<LoadInst>(V))
94 return true;
95
96 return false;
97}
Chris Lattner845f0d22008-06-16 06:19:11 +000098
Chris Lattnera4139602008-06-16 06:10:11 +000099/// isObjectSmallerThan - Return true if we can prove that the object specified
100/// by V is smaller than Size.
101static bool isObjectSmallerThan(const Value *V, unsigned Size,
Chris Lattner7b550cc2009-11-06 04:27:31 +0000102 const TargetData &TD) {
Chris Lattner295d4e92008-12-08 06:28:54 +0000103 const Type *AccessTy;
104 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
Chris Lattnera4139602008-06-16 06:10:11 +0000105 AccessTy = GV->getType()->getElementType();
Victor Hernandez7b929da2009-10-23 21:09:37 +0000106 } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
Chris Lattnera4139602008-06-16 06:10:11 +0000107 if (!AI->isArrayAllocation())
108 AccessTy = AI->getType()->getElementType();
Chris Lattner295d4e92008-12-08 06:28:54 +0000109 else
110 return false;
Victor Hernandez46e83122009-09-18 21:34:51 +0000111 } else if (const CallInst* CI = extractMallocCall(V)) {
Chris Lattner7b550cc2009-11-06 04:27:31 +0000112 if (!isArrayMalloc(V, &TD))
Victor Hernandez46e83122009-09-18 21:34:51 +0000113 // The size is the argument to the malloc call.
Gabor Greif71339c92010-06-23 23:38:07 +0000114 if (const ConstantInt* C = dyn_cast<ConstantInt>(CI->getArgOperand(0)))
Victor Hernandez46e83122009-09-18 21:34:51 +0000115 return (C->getZExtValue() < Size);
116 return false;
Chris Lattner295d4e92008-12-08 06:28:54 +0000117 } else if (const Argument *A = dyn_cast<Argument>(V)) {
Chris Lattnera4139602008-06-16 06:10:11 +0000118 if (A->hasByValAttr())
119 AccessTy = cast<PointerType>(A->getType())->getElementType();
Chris Lattner295d4e92008-12-08 06:28:54 +0000120 else
121 return false;
122 } else {
123 return false;
124 }
Chris Lattnera4139602008-06-16 06:10:11 +0000125
Chris Lattner295d4e92008-12-08 06:28:54 +0000126 if (AccessTy->isSized())
Duncan Sands777d2302009-05-09 07:06:46 +0000127 return TD.getTypeAllocSize(AccessTy) < Size;
Chris Lattnera4139602008-06-16 06:10:11 +0000128 return false;
129}
130
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000131//===----------------------------------------------------------------------===//
132// NoAA Pass
133//===----------------------------------------------------------------------===//
134
135namespace {
136 /// NoAA - This class implements the -no-aa pass, which always returns "I
137 /// don't know" for alias queries. NoAA is unlike other alias analysis
138 /// implementations, in that it does not chain to a previous analysis. As
139 /// such it doesn't follow many of the rules that other alias analyses must.
140 ///
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000141 struct NoAA : public ImmutablePass, public AliasAnalysis {
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000142 static char ID; // Class identification, replacement for typeinfo
Dan Gohmanae73dc12008-09-04 17:05:41 +0000143 NoAA() : ImmutablePass(&ID) {}
144 explicit NoAA(void *PID) : ImmutablePass(PID) { }
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000145
146 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000147 }
148
149 virtual void initializePass() {
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +0000150 TD = getAnalysisIfAvailable<TargetData>();
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000151 }
152
153 virtual AliasResult alias(const Value *V1, unsigned V1Size,
154 const Value *V2, unsigned V2Size) {
155 return MayAlias;
156 }
157
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000158 virtual void getArgumentAccesses(Function *F, CallSite CS,
159 std::vector<PointerAccessInfo> &Info) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000160 llvm_unreachable("This method may not be called on this function!");
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000161 }
162
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000163 virtual bool pointsToConstantMemory(const Value *P) { return false; }
164 virtual ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size) {
165 return ModRef;
166 }
167 virtual ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) {
168 return ModRef;
169 }
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000170
171 virtual void deleteValue(Value *V) {}
172 virtual void copyValue(Value *From, Value *To) {}
Chris Lattner20330972010-01-20 19:26:14 +0000173
174 /// getAdjustedAnalysisPointer - This method is used when a pass implements
175 /// an analysis interface through multiple inheritance. If needed, it should
176 /// override this to adjust the this pointer as needed for the specified pass
177 /// info.
178 virtual void *getAdjustedAnalysisPointer(const PassInfo *PI) {
179 if (PI->isPassID(&AliasAnalysis::ID))
180 return (AliasAnalysis*)this;
181 return this;
182 }
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000183 };
184} // End of anonymous namespace
185
186// Register this pass...
187char NoAA::ID = 0;
188static RegisterPass<NoAA>
189U("no-aa", "No Alias Analysis (always returns 'may' alias)", true, true);
190
191// Declare that we implement the AliasAnalysis interface
192static RegisterAnalysisGroup<AliasAnalysis> V(U);
193
194ImmutablePass *llvm::createNoAAPass() { return new NoAA(); }
195
196//===----------------------------------------------------------------------===//
Dan Gohman6be2bd52010-06-29 00:50:39 +0000197// BasicAliasAnalysis Pass
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000198//===----------------------------------------------------------------------===//
199
Dan Gohman6be2bd52010-06-29 00:50:39 +0000200static const Function *getParent(const Value *V) {
201 if(const Instruction *inst = dyn_cast<Instruction>(V))
202 return inst->getParent()->getParent();
203
204 if(const Argument *arg = dyn_cast<Argument>(V))
205 return arg->getParent();
206
207 return NULL;
208}
209
210static bool sameParent(const Value *O1, const Value *O2) {
211
212 const Function *F1 = getParent(O1);
213 const Function *F2 = getParent(O2);
214
215 return !F1 || !F2 || F1 == F2;
216}
217
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000218namespace {
219 /// BasicAliasAnalysis - This is the default alias analysis implementation.
220 /// Because it doesn't chain to a previous alias analysis (like -no-aa), it
221 /// derives from the NoAA class.
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000222 struct BasicAliasAnalysis : public NoAA {
Dan Gohman6be2bd52010-06-29 00:50:39 +0000223 /// Interprocedural - Flag for "interprocedural" mode, where we must
224 /// support queries of values which live in different functions.
225 bool Interprocedural;
226
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000227 static char ID; // Class identification, replacement for typeinfo
Dan Gohman6be2bd52010-06-29 00:50:39 +0000228 BasicAliasAnalysis()
229 : NoAA(&ID), Interprocedural(false) {}
230 BasicAliasAnalysis(void *PID, bool interprocedural)
231 : NoAA(PID), Interprocedural(interprocedural) {}
232
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000233 AliasResult alias(const Value *V1, unsigned V1Size,
Evan Cheng50a59142009-10-13 22:02:20 +0000234 const Value *V2, unsigned V2Size) {
Dan Gohman50f424c2010-06-28 21:16:52 +0000235 assert(Visited.empty() && "Visited must be cleared after use!");
Dan Gohman6be2bd52010-06-29 00:50:39 +0000236#ifdef XDEBUG
237 assert((Interprocedural || sameParent(V1, V2)) &&
238 "BasicAliasAnalysis (-basicaa) doesn't support interprocedural "
239 "queries; use InterproceduralAliasAnalysis "
240 "(-interprocedural-basic-aa) instead.");
241#endif
Evan Chengf0429fd2009-10-14 06:46:26 +0000242 AliasResult Alias = aliasCheck(V1, V1Size, V2, V2Size);
Dan Gohman50f424c2010-06-28 21:16:52 +0000243 Visited.clear();
Evan Chengf0429fd2009-10-14 06:46:26 +0000244 return Alias;
Evan Cheng50a59142009-10-13 22:02:20 +0000245 }
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000246
247 ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
Chris Lattner20d6f092008-12-09 21:19:42 +0000248 ModRefResult getModRefInfo(CallSite CS1, CallSite CS2);
Owen Andersone7942202009-02-05 23:36:27 +0000249
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000250 /// pointsToConstantMemory - Chase pointers until we find a (constant
251 /// global) or not.
252 bool pointsToConstantMemory(const Value *P);
253
Chris Lattner20330972010-01-20 19:26:14 +0000254 /// getAdjustedAnalysisPointer - This method is used when a pass implements
255 /// an analysis interface through multiple inheritance. If needed, it should
256 /// override this to adjust the this pointer as needed for the specified pass
257 /// info.
258 virtual void *getAdjustedAnalysisPointer(const PassInfo *PI) {
259 if (PI->isPassID(&AliasAnalysis::ID))
260 return (AliasAnalysis*)this;
261 return this;
262 }
263
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000264 private:
Dan Gohman50f424c2010-06-28 21:16:52 +0000265 // Visited - Track instructions visited by a aliasPHI, aliasSelect(), and aliasGEP().
266 SmallPtrSet<const Value*, 16> Visited;
Evan Cheng3dbe43b2009-10-14 05:05:02 +0000267
Chris Lattner5d56b2d2009-11-23 16:45:27 +0000268 // aliasGEP - Provide a bunch of ad-hoc rules to disambiguate a GEP
269 // instruction against another.
Chris Lattner539c9b92009-11-26 02:11:08 +0000270 AliasResult aliasGEP(const GEPOperator *V1, unsigned V1Size,
Chris Lattner23e2a5b2009-11-26 02:14:59 +0000271 const Value *V2, unsigned V2Size,
272 const Value *UnderlyingV1, const Value *UnderlyingV2);
Evan Cheng50a59142009-10-13 22:02:20 +0000273
Chris Lattner5d56b2d2009-11-23 16:45:27 +0000274 // aliasPHI - Provide a bunch of ad-hoc rules to disambiguate a PHI
275 // instruction against another.
Evan Chengd83c2ca2009-10-14 05:22:03 +0000276 AliasResult aliasPHI(const PHINode *PN, unsigned PNSize,
Evan Cheng3dbe43b2009-10-14 05:05:02 +0000277 const Value *V2, unsigned V2Size);
Evan Cheng50a59142009-10-13 22:02:20 +0000278
Dan Gohman6665b0e2009-10-26 21:55:43 +0000279 /// aliasSelect - Disambiguate a Select instruction against another value.
280 AliasResult aliasSelect(const SelectInst *SI, unsigned SISize,
281 const Value *V2, unsigned V2Size);
282
Evan Cheng50a59142009-10-13 22:02:20 +0000283 AliasResult aliasCheck(const Value *V1, unsigned V1Size,
Evan Cheng3dbe43b2009-10-14 05:05:02 +0000284 const Value *V2, unsigned V2Size);
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000285 };
286} // End of anonymous namespace
287
288// Register this pass...
289char BasicAliasAnalysis::ID = 0;
290static RegisterPass<BasicAliasAnalysis>
291X("basicaa", "Basic Alias Analysis (default AA impl)", false, true);
292
293// Declare that we implement the AliasAnalysis interface
294static RegisterAnalysisGroup<AliasAnalysis, true> Y(X);
295
296ImmutablePass *llvm::createBasicAliasAnalysisPass() {
297 return new BasicAliasAnalysis();
298}
299
300
301/// pointsToConstantMemory - Chase pointers until we find a (constant
302/// global) or not.
303bool BasicAliasAnalysis::pointsToConstantMemory(const Value *P) {
304 if (const GlobalVariable *GV =
Duncan Sands5d0392c2008-10-01 15:25:41 +0000305 dyn_cast<GlobalVariable>(P->getUnderlyingObject()))
Chris Lattnerb27db372009-11-23 17:07:35 +0000306 // Note: this doesn't require GV to be "ODR" because it isn't legal for a
307 // global to be marked constant in some modules and non-constant in others.
308 // GV may even be a declaration, not a definition.
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000309 return GV->isConstant();
310 return false;
311}
312
Owen Andersone7942202009-02-05 23:36:27 +0000313
Chris Lattner5d56b2d2009-11-23 16:45:27 +0000314/// getModRefInfo - Check to see if the specified callsite can clobber the
315/// specified memory object. Since we only look at local properties of this
316/// function, we really can't say much about this query. We do, however, use
317/// simple "address taken" analysis on local objects.
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000318AliasAnalysis::ModRefResult
319BasicAliasAnalysis::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
Chris Lattner92e803c2009-11-22 16:05:05 +0000320 const Value *Object = P->getUnderlyingObject();
321
322 // If this is a tail call and P points to a stack location, we know that
323 // the tail call cannot access or modify the local stack.
324 // We cannot exclude byval arguments here; these belong to the caller of
325 // the current function not to the current function, and a tail callee
326 // may reference them.
327 if (isa<AllocaInst>(Object))
328 if (CallInst *CI = dyn_cast<CallInst>(CS.getInstruction()))
329 if (CI->isTailCall())
330 return NoModRef;
331
332 // If the pointer is to a locally allocated object that does not escape,
Chris Lattnerb34b82e2009-11-23 16:44:43 +0000333 // then the call can not mod/ref the pointer unless the call takes the pointer
334 // as an argument, and itself doesn't capture it.
Chris Lattner403ac2e2009-11-23 16:46:41 +0000335 if (!isa<Constant>(Object) && CS.getInstruction() != Object &&
Dan Gohman6be2bd52010-06-29 00:50:39 +0000336 isNonEscapingLocalObject(Object, Interprocedural)) {
Chris Lattnerb34b82e2009-11-23 16:44:43 +0000337 bool PassedAsArg = false;
338 unsigned ArgNo = 0;
Chris Lattner92e803c2009-11-22 16:05:05 +0000339 for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
Chris Lattnerb34b82e2009-11-23 16:44:43 +0000340 CI != CE; ++CI, ++ArgNo) {
341 // Only look at the no-capture pointer arguments.
Duncan Sands1df98592010-02-16 11:11:14 +0000342 if (!(*CI)->getType()->isPointerTy() ||
Chris Lattnerb34b82e2009-11-23 16:44:43 +0000343 !CS.paramHasAttr(ArgNo+1, Attribute::NoCapture))
344 continue;
345
346 // If this is a no-capture pointer argument, see if we can tell that it
347 // is impossible to alias the pointer we're checking. If not, we have to
348 // assume that the call could touch the pointer, even though it doesn't
349 // escape.
Chris Lattner403ac2e2009-11-23 16:46:41 +0000350 if (!isNoAlias(cast<Value>(CI), ~0U, P, ~0U)) {
Chris Lattnerb34b82e2009-11-23 16:44:43 +0000351 PassedAsArg = true;
352 break;
353 }
354 }
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000355
Chris Lattnerb34b82e2009-11-23 16:44:43 +0000356 if (!PassedAsArg)
Chris Lattner92e803c2009-11-22 16:05:05 +0000357 return NoModRef;
358 }
359
360 // Finally, handle specific knowledge of intrinsics.
361 IntrinsicInst *II = dyn_cast<IntrinsicInst>(CS.getInstruction());
362 if (II == 0)
363 return AliasAnalysis::getModRefInfo(CS, P, Size);
364
365 switch (II->getIntrinsicID()) {
366 default: break;
367 case Intrinsic::memcpy:
368 case Intrinsic::memmove: {
369 unsigned Len = ~0U;
Gabor Greif71339c92010-06-23 23:38:07 +0000370 if (ConstantInt *LenCI = dyn_cast<ConstantInt>(II->getArgOperand(2)))
Chris Lattner92e803c2009-11-22 16:05:05 +0000371 Len = LenCI->getZExtValue();
Gabor Greif71339c92010-06-23 23:38:07 +0000372 Value *Dest = II->getArgOperand(0);
373 Value *Src = II->getArgOperand(1);
Chris Lattner403ac2e2009-11-23 16:46:41 +0000374 if (isNoAlias(Dest, Len, P, Size)) {
375 if (isNoAlias(Src, Len, P, Size))
Chris Lattner92e803c2009-11-22 16:05:05 +0000376 return NoModRef;
377 return Ref;
378 }
379 break;
380 }
381 case Intrinsic::memset:
Chris Lattner403ac2e2009-11-23 16:46:41 +0000382 // Since memset is 'accesses arguments' only, the AliasAnalysis base class
383 // will handle it for the variable length case.
Gabor Greif71339c92010-06-23 23:38:07 +0000384 if (ConstantInt *LenCI = dyn_cast<ConstantInt>(II->getArgOperand(2))) {
Chris Lattner92e803c2009-11-22 16:05:05 +0000385 unsigned Len = LenCI->getZExtValue();
Gabor Greif71339c92010-06-23 23:38:07 +0000386 Value *Dest = II->getArgOperand(0);
Chris Lattner403ac2e2009-11-23 16:46:41 +0000387 if (isNoAlias(Dest, Len, P, Size))
Chris Lattner25df20f2008-06-16 06:38:26 +0000388 return NoModRef;
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000389 }
Chris Lattner92e803c2009-11-22 16:05:05 +0000390 break;
391 case Intrinsic::atomic_cmp_swap:
392 case Intrinsic::atomic_swap:
393 case Intrinsic::atomic_load_add:
394 case Intrinsic::atomic_load_sub:
395 case Intrinsic::atomic_load_and:
396 case Intrinsic::atomic_load_nand:
397 case Intrinsic::atomic_load_or:
398 case Intrinsic::atomic_load_xor:
399 case Intrinsic::atomic_load_max:
400 case Intrinsic::atomic_load_min:
401 case Intrinsic::atomic_load_umax:
402 case Intrinsic::atomic_load_umin:
403 if (TD) {
Gabor Greif71339c92010-06-23 23:38:07 +0000404 Value *Op1 = II->getArgOperand(0);
Chris Lattner92e803c2009-11-22 16:05:05 +0000405 unsigned Op1Size = TD->getTypeStoreSize(Op1->getType());
Chris Lattner403ac2e2009-11-23 16:46:41 +0000406 if (isNoAlias(Op1, Op1Size, P, Size))
Chris Lattner92e803c2009-11-22 16:05:05 +0000407 return NoModRef;
Nick Lewycky5c9be672009-10-13 07:48:38 +0000408 }
Chris Lattner92e803c2009-11-22 16:05:05 +0000409 break;
410 case Intrinsic::lifetime_start:
411 case Intrinsic::lifetime_end:
412 case Intrinsic::invariant_start: {
Gabor Greif71339c92010-06-23 23:38:07 +0000413 unsigned PtrSize = cast<ConstantInt>(II->getArgOperand(0))->getZExtValue();
414 if (isNoAlias(II->getArgOperand(1), PtrSize, P, Size))
Chris Lattner92e803c2009-11-22 16:05:05 +0000415 return NoModRef;
416 break;
417 }
418 case Intrinsic::invariant_end: {
Gabor Greif71339c92010-06-23 23:38:07 +0000419 unsigned PtrSize = cast<ConstantInt>(II->getArgOperand(1))->getZExtValue();
420 if (isNoAlias(II->getArgOperand(2), PtrSize, P, Size))
Chris Lattner92e803c2009-11-22 16:05:05 +0000421 return NoModRef;
422 break;
423 }
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000424 }
425
426 // The AliasAnalysis base class has some smarts, lets use them.
427 return AliasAnalysis::getModRefInfo(CS, P, Size);
428}
429
430
Chris Lattner20d6f092008-12-09 21:19:42 +0000431AliasAnalysis::ModRefResult
432BasicAliasAnalysis::getModRefInfo(CallSite CS1, CallSite CS2) {
433 // If CS1 or CS2 are readnone, they don't interact.
434 ModRefBehavior CS1B = AliasAnalysis::getModRefBehavior(CS1);
435 if (CS1B == DoesNotAccessMemory) return NoModRef;
436
437 ModRefBehavior CS2B = AliasAnalysis::getModRefBehavior(CS2);
438 if (CS2B == DoesNotAccessMemory) return NoModRef;
439
440 // If they both only read from memory, just return ref.
441 if (CS1B == OnlyReadsMemory && CS2B == OnlyReadsMemory)
442 return Ref;
443
444 // Otherwise, fall back to NoAA (mod+ref).
445 return NoAA::getModRefInfo(CS1, CS2);
446}
447
Chris Lattnerd84eb912009-11-26 02:17:34 +0000448/// GetIndiceDifference - Dest and Src are the variable indices from two
449/// decomposed GetElementPtr instructions GEP1 and GEP2 which have common base
450/// pointers. Subtract the GEP2 indices from GEP1 to find the symbolic
451/// difference between the two pointers.
452static void GetIndiceDifference(
453 SmallVectorImpl<std::pair<const Value*, int64_t> > &Dest,
454 const SmallVectorImpl<std::pair<const Value*, int64_t> > &Src) {
455 if (Src.empty()) return;
456
457 for (unsigned i = 0, e = Src.size(); i != e; ++i) {
458 const Value *V = Src[i].first;
459 int64_t Scale = Src[i].second;
460
461 // Find V in Dest. This is N^2, but pointer indices almost never have more
462 // than a few variable indexes.
463 for (unsigned j = 0, e = Dest.size(); j != e; ++j) {
464 if (Dest[j].first != V) continue;
465
466 // If we found it, subtract off Scale V's from the entry in Dest. If it
467 // goes to zero, remove the entry.
468 if (Dest[j].second != Scale)
469 Dest[j].second -= Scale;
470 else
471 Dest.erase(Dest.begin()+j);
472 Scale = 0;
473 break;
474 }
475
476 // If we didn't consume this entry, add it to the end of the Dest list.
477 if (Scale)
478 Dest.push_back(std::make_pair(V, -Scale));
479 }
480}
Chris Lattner4e91ee72009-11-26 02:13:03 +0000481
Chris Lattner539c9b92009-11-26 02:11:08 +0000482/// aliasGEP - Provide a bunch of ad-hoc rules to disambiguate a GEP instruction
483/// against another pointer. We know that V1 is a GEP, but we don't know
Chris Lattner23e2a5b2009-11-26 02:14:59 +0000484/// anything about V2. UnderlyingV1 is GEP1->getUnderlyingObject(),
485/// UnderlyingV2 is the same for V2.
Chris Lattner539c9b92009-11-26 02:11:08 +0000486///
Chris Lattnerd501c132003-02-26 19:41:54 +0000487AliasAnalysis::AliasResult
Chris Lattner539c9b92009-11-26 02:11:08 +0000488BasicAliasAnalysis::aliasGEP(const GEPOperator *GEP1, unsigned V1Size,
Chris Lattner23e2a5b2009-11-26 02:14:59 +0000489 const Value *V2, unsigned V2Size,
490 const Value *UnderlyingV1,
491 const Value *UnderlyingV2) {
Dan Gohman50f424c2010-06-28 21:16:52 +0000492 // If this GEP has been visited before, we're on a use-def cycle.
493 // Such cycles are only valid when PHI nodes are involved or in unreachable
494 // code. The visitPHI function catches cycles containing PHIs, but there
495 // could still be a cycle without PHIs in unreachable code.
496 if (!Visited.insert(GEP1))
497 return MayAlias;
498
Chris Lattnerd84eb912009-11-26 02:17:34 +0000499 int64_t GEP1BaseOffset;
500 SmallVector<std::pair<const Value*, int64_t>, 4> GEP1VariableIndices;
501
Chris Lattnerb307c882003-12-11 22:44:13 +0000502 // If we have two gep instructions with must-alias'ing base pointers, figure
503 // out if the indexes to the GEP tell us anything about the derived pointer.
Chris Lattner539c9b92009-11-26 02:11:08 +0000504 if (const GEPOperator *GEP2 = dyn_cast<GEPOperator>(V2)) {
Chris Lattnerb307c882003-12-11 22:44:13 +0000505 // Do the base pointers alias?
Chris Lattnerd84eb912009-11-26 02:17:34 +0000506 AliasResult BaseAlias = aliasCheck(UnderlyingV1, ~0U, UnderlyingV2, ~0U);
507
508 // If we get a No or May, then return it immediately, no amount of analysis
509 // will improve this situation.
510 if (BaseAlias != MustAlias) return BaseAlias;
511
512 // Otherwise, we have a MustAlias. Since the base pointers alias each other
513 // exactly, see if the computed offset from the common pointer tells us
514 // about the relation of the resulting pointer.
515 const Value *GEP1BasePtr =
516 DecomposeGEPExpression(GEP1, GEP1BaseOffset, GEP1VariableIndices, TD);
517
518 int64_t GEP2BaseOffset;
519 SmallVector<std::pair<const Value*, int64_t>, 4> GEP2VariableIndices;
520 const Value *GEP2BasePtr =
521 DecomposeGEPExpression(GEP2, GEP2BaseOffset, GEP2VariableIndices, TD);
522
523 // If DecomposeGEPExpression isn't able to look all the way through the
524 // addressing operation, we must not have TD and this is too complex for us
525 // to handle without it.
526 if (GEP1BasePtr != UnderlyingV1 || GEP2BasePtr != UnderlyingV2) {
527 assert(TD == 0 &&
528 "DecomposeGEPExpression and getUnderlyingObject disagree!");
529 return MayAlias;
530 }
531
532 // Subtract the GEP2 pointer from the GEP1 pointer to find out their
533 // symbolic difference.
534 GEP1BaseOffset -= GEP2BaseOffset;
535 GetIndiceDifference(GEP1VariableIndices, GEP2VariableIndices);
536
537 } else {
538 // Check to see if these two pointers are related by the getelementptr
539 // instruction. If one pointer is a GEP with a non-zero index of the other
540 // pointer, we know they cannot alias.
Chris Lattner53692502009-11-26 16:52:32 +0000541
542 // If both accesses are unknown size, we can't do anything useful here.
543 if (V1Size == ~0U && V2Size == ~0U)
Chris Lattnerd84eb912009-11-26 02:17:34 +0000544 return MayAlias;
Chris Lattnerb307c882003-12-11 22:44:13 +0000545
Chris Lattnerd84eb912009-11-26 02:17:34 +0000546 AliasResult R = aliasCheck(UnderlyingV1, ~0U, V2, V2Size);
547 if (R != MustAlias)
548 // If V2 may alias GEP base pointer, conservatively returns MayAlias.
549 // If V2 is known not to alias GEP base pointer, then the two values
550 // cannot alias per GEP semantics: "A pointer value formed from a
551 // getelementptr instruction is associated with the addresses associated
552 // with the first operand of the getelementptr".
553 return R;
Chris Lattnerb307c882003-12-11 22:44:13 +0000554
Chris Lattnerd84eb912009-11-26 02:17:34 +0000555 const Value *GEP1BasePtr =
556 DecomposeGEPExpression(GEP1, GEP1BaseOffset, GEP1VariableIndices, TD);
557
558 // If DecomposeGEPExpression isn't able to look all the way through the
559 // addressing operation, we must not have TD and this is too complex for us
560 // to handle without it.
561 if (GEP1BasePtr != UnderlyingV1) {
562 assert(TD == 0 &&
563 "DecomposeGEPExpression and getUnderlyingObject disagree!");
564 return MayAlias;
Chris Lattnerb307c882003-12-11 22:44:13 +0000565 }
566 }
Chris Lattnerd84eb912009-11-26 02:17:34 +0000567
568 // In the two GEP Case, if there is no difference in the offsets of the
569 // computed pointers, the resultant pointers are a must alias. This
570 // hapens when we have two lexically identical GEP's (for example).
Chris Lattnerd501c132003-02-26 19:41:54 +0000571 //
Chris Lattnerd84eb912009-11-26 02:17:34 +0000572 // In the other case, if we have getelementptr <ptr>, 0, 0, 0, 0, ... and V2
573 // must aliases the GEP, the end result is a must alias also.
574 if (GEP1BaseOffset == 0 && GEP1VariableIndices.empty())
Evan Cheng681a33e2009-10-14 06:41:49 +0000575 return MustAlias;
Evan Cheng094f04b2009-10-13 18:42:04 +0000576
Chris Lattner4e91ee72009-11-26 02:13:03 +0000577 // If we have a known constant offset, see if this offset is larger than the
578 // access size being queried. If so, and if no variable indices can remove
579 // pieces of this constant, then we know we have a no-alias. For example,
580 // &A[100] != &A.
581
582 // In order to handle cases like &A[100][i] where i is an out of range
583 // subscript, we have to ignore all constant offset pieces that are a multiple
584 // of a scaled index. Do this by removing constant offsets that are a
585 // multiple of any of our variable indices. This allows us to transform
586 // things like &A[i][1] because i has a stride of (e.g.) 8 bytes but the 1
587 // provides an offset of 4 bytes (assuming a <= 4 byte access).
Chris Lattnerd84eb912009-11-26 02:17:34 +0000588 for (unsigned i = 0, e = GEP1VariableIndices.size();
589 i != e && GEP1BaseOffset;++i)
590 if (int64_t RemovedOffset = GEP1BaseOffset/GEP1VariableIndices[i].second)
591 GEP1BaseOffset -= RemovedOffset*GEP1VariableIndices[i].second;
Chris Lattner4e91ee72009-11-26 02:13:03 +0000592
593 // If our known offset is bigger than the access size, we know we don't have
594 // an alias.
595 if (GEP1BaseOffset) {
596 if (GEP1BaseOffset >= (int64_t)V2Size ||
597 GEP1BaseOffset <= -(int64_t)V1Size)
Evan Cheng681a33e2009-10-14 06:41:49 +0000598 return NoAlias;
Evan Cheng094f04b2009-10-13 18:42:04 +0000599 }
Chris Lattner4e91ee72009-11-26 02:13:03 +0000600
Evan Cheng094f04b2009-10-13 18:42:04 +0000601 return MayAlias;
602}
603
Chris Lattner5d56b2d2009-11-23 16:45:27 +0000604/// aliasSelect - Provide a bunch of ad-hoc rules to disambiguate a Select
605/// instruction against another.
Dan Gohman6665b0e2009-10-26 21:55:43 +0000606AliasAnalysis::AliasResult
607BasicAliasAnalysis::aliasSelect(const SelectInst *SI, unsigned SISize,
608 const Value *V2, unsigned V2Size) {
Dan Gohman50f424c2010-06-28 21:16:52 +0000609 // If this select has been visited before, we're on a use-def cycle.
610 // Such cycles are only valid when PHI nodes are involved or in unreachable
611 // code. The visitPHI function catches cycles containing PHIs, but there
612 // could still be a cycle without PHIs in unreachable code.
613 if (!Visited.insert(SI))
614 return MayAlias;
615
Dan Gohman6665b0e2009-10-26 21:55:43 +0000616 // If the values are Selects with the same condition, we can do a more precise
617 // check: just check for aliases between the values on corresponding arms.
618 if (const SelectInst *SI2 = dyn_cast<SelectInst>(V2))
619 if (SI->getCondition() == SI2->getCondition()) {
620 AliasResult Alias =
621 aliasCheck(SI->getTrueValue(), SISize,
622 SI2->getTrueValue(), V2Size);
623 if (Alias == MayAlias)
624 return MayAlias;
625 AliasResult ThisAlias =
626 aliasCheck(SI->getFalseValue(), SISize,
627 SI2->getFalseValue(), V2Size);
628 if (ThisAlias != Alias)
629 return MayAlias;
630 return Alias;
631 }
632
633 // If both arms of the Select node NoAlias or MustAlias V2, then returns
634 // NoAlias / MustAlias. Otherwise, returns MayAlias.
635 AliasResult Alias =
Dan Gohman50f424c2010-06-28 21:16:52 +0000636 aliasCheck(V2, V2Size, SI->getTrueValue(), SISize);
Dan Gohman6665b0e2009-10-26 21:55:43 +0000637 if (Alias == MayAlias)
638 return MayAlias;
Dan Gohman50f424c2010-06-28 21:16:52 +0000639
640 // If V2 is visited, the recursive case will have been caught in the
641 // above aliasCheck call, so these subsequent calls to aliasCheck
642 // don't need to assume that V2 is being visited recursively.
643 Visited.erase(V2);
644
Dan Gohman6665b0e2009-10-26 21:55:43 +0000645 AliasResult ThisAlias =
Dan Gohman50f424c2010-06-28 21:16:52 +0000646 aliasCheck(V2, V2Size, SI->getFalseValue(), SISize);
Dan Gohman6665b0e2009-10-26 21:55:43 +0000647 if (ThisAlias != Alias)
648 return MayAlias;
649 return Alias;
650}
651
Evan Chengd83c2ca2009-10-14 05:22:03 +0000652// aliasPHI - Provide a bunch of ad-hoc rules to disambiguate a PHI instruction
Evan Cheng3dbe43b2009-10-14 05:05:02 +0000653// against another.
Evan Cheng50a59142009-10-13 22:02:20 +0000654AliasAnalysis::AliasResult
Evan Chengd83c2ca2009-10-14 05:22:03 +0000655BasicAliasAnalysis::aliasPHI(const PHINode *PN, unsigned PNSize,
Evan Cheng3dbe43b2009-10-14 05:05:02 +0000656 const Value *V2, unsigned V2Size) {
Evan Cheng50a59142009-10-13 22:02:20 +0000657 // The PHI node has already been visited, avoid recursion any further.
Dan Gohman50f424c2010-06-28 21:16:52 +0000658 if (!Visited.insert(PN))
Evan Cheng50a59142009-10-13 22:02:20 +0000659 return MayAlias;
660
Dan Gohman6665b0e2009-10-26 21:55:43 +0000661 // If the values are PHIs in the same block, we can do a more precise
662 // as well as efficient check: just check for aliases between the values
663 // on corresponding edges.
664 if (const PHINode *PN2 = dyn_cast<PHINode>(V2))
665 if (PN2->getParent() == PN->getParent()) {
666 AliasResult Alias =
667 aliasCheck(PN->getIncomingValue(0), PNSize,
668 PN2->getIncomingValueForBlock(PN->getIncomingBlock(0)),
669 V2Size);
670 if (Alias == MayAlias)
671 return MayAlias;
672 for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) {
673 AliasResult ThisAlias =
674 aliasCheck(PN->getIncomingValue(i), PNSize,
675 PN2->getIncomingValueForBlock(PN->getIncomingBlock(i)),
676 V2Size);
677 if (ThisAlias != Alias)
678 return MayAlias;
679 }
680 return Alias;
681 }
682
Evan Chenga846a8a2009-10-16 00:33:09 +0000683 SmallPtrSet<Value*, 4> UniqueSrc;
Evan Cheng50a59142009-10-13 22:02:20 +0000684 SmallVector<Value*, 4> V1Srcs;
Evan Cheng50a59142009-10-13 22:02:20 +0000685 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
686 Value *PV1 = PN->getIncomingValue(i);
687 if (isa<PHINode>(PV1))
688 // If any of the source itself is a PHI, return MayAlias conservatively
Evan Cheng681a33e2009-10-14 06:41:49 +0000689 // to avoid compile time explosion. The worst possible case is if both
690 // sides are PHI nodes. In which case, this is O(m x n) time where 'm'
691 // and 'n' are the number of PHI sources.
Evan Cheng50a59142009-10-13 22:02:20 +0000692 return MayAlias;
693 if (UniqueSrc.insert(PV1))
694 V1Srcs.push_back(PV1);
695 }
696
Dan Gohman6665b0e2009-10-26 21:55:43 +0000697 AliasResult Alias = aliasCheck(V2, V2Size, V1Srcs[0], PNSize);
Evan Chengd83c2ca2009-10-14 05:22:03 +0000698 // Early exit if the check of the first PHI source against V2 is MayAlias.
699 // Other results are not possible.
700 if (Alias == MayAlias)
701 return MayAlias;
702
Evan Cheng50a59142009-10-13 22:02:20 +0000703 // If all sources of the PHI node NoAlias or MustAlias V2, then returns
704 // NoAlias / MustAlias. Otherwise, returns MayAlias.
Evan Cheng50a59142009-10-13 22:02:20 +0000705 for (unsigned i = 1, e = V1Srcs.size(); i != e; ++i) {
706 Value *V = V1Srcs[i];
Dan Gohman6665b0e2009-10-26 21:55:43 +0000707
Dan Gohman50f424c2010-06-28 21:16:52 +0000708 // If V2 is visited, the recursive case will have been caught in the
Dan Gohman6665b0e2009-10-26 21:55:43 +0000709 // above aliasCheck call, so these subsequent calls to aliasCheck
710 // don't need to assume that V2 is being visited recursively.
Dan Gohman50f424c2010-06-28 21:16:52 +0000711 Visited.erase(V2);
Dan Gohman6665b0e2009-10-26 21:55:43 +0000712
Evan Chenga846a8a2009-10-16 00:33:09 +0000713 AliasResult ThisAlias = aliasCheck(V2, V2Size, V, PNSize);
Evan Chengd83c2ca2009-10-14 05:22:03 +0000714 if (ThisAlias != Alias || ThisAlias == MayAlias)
Evan Cheng50a59142009-10-13 22:02:20 +0000715 return MayAlias;
716 }
717
718 return Alias;
719}
720
721// aliasCheck - Provide a bunch of ad-hoc rules to disambiguate in common cases,
722// such as array references.
Evan Cheng094f04b2009-10-13 18:42:04 +0000723//
724AliasAnalysis::AliasResult
Evan Cheng50a59142009-10-13 22:02:20 +0000725BasicAliasAnalysis::aliasCheck(const Value *V1, unsigned V1Size,
Evan Cheng3dbe43b2009-10-14 05:05:02 +0000726 const Value *V2, unsigned V2Size) {
Dan Gohmanb57b6f12010-04-08 18:11:50 +0000727 // If either of the memory references is empty, it doesn't matter what the
728 // pointer values are.
729 if (V1Size == 0 || V2Size == 0)
730 return NoAlias;
731
Evan Cheng094f04b2009-10-13 18:42:04 +0000732 // Strip off any casts if they exist.
733 V1 = V1->stripPointerCasts();
734 V2 = V2->stripPointerCasts();
735
736 // Are we checking for alias of the same value?
737 if (V1 == V2) return MustAlias;
738
Duncan Sands1df98592010-02-16 11:11:14 +0000739 if (!V1->getType()->isPointerTy() || !V2->getType()->isPointerTy())
Evan Cheng094f04b2009-10-13 18:42:04 +0000740 return NoAlias; // Scalars cannot alias each other
741
742 // Figure out what objects these things are pointing to if we can.
743 const Value *O1 = V1->getUnderlyingObject();
744 const Value *O2 = V2->getUnderlyingObject();
745
Dan Gohmanf75ef662009-11-09 19:29:11 +0000746 // Null values in the default address space don't point to any object, so they
747 // don't alias any other pointer.
748 if (const ConstantPointerNull *CPN = dyn_cast<ConstantPointerNull>(O1))
749 if (CPN->getType()->getAddressSpace() == 0)
750 return NoAlias;
751 if (const ConstantPointerNull *CPN = dyn_cast<ConstantPointerNull>(O2))
752 if (CPN->getType()->getAddressSpace() == 0)
753 return NoAlias;
754
Evan Cheng094f04b2009-10-13 18:42:04 +0000755 if (O1 != O2) {
756 // If V1/V2 point to two different objects we know that we have no alias.
Dan Gohman6be2bd52010-06-29 00:50:39 +0000757 if (isIdentifiedObject(O1, Interprocedural) &&
758 isIdentifiedObject(O2, Interprocedural))
Evan Cheng094f04b2009-10-13 18:42:04 +0000759 return NoAlias;
Nick Lewycky20162ac2009-11-14 06:15:14 +0000760
761 // Constant pointers can't alias with non-const isIdentifiedObject objects.
Dan Gohman6be2bd52010-06-29 00:50:39 +0000762 if ((isa<Constant>(O1) &&
763 isIdentifiedObject(O2, Interprocedural) &&
764 !isa<Constant>(O2)) ||
765 (isa<Constant>(O2) &&
766 isIdentifiedObject(O1, Interprocedural) &&
767 !isa<Constant>(O1)))
Nick Lewycky20162ac2009-11-14 06:15:14 +0000768 return NoAlias;
769
Dan Gohman6be2bd52010-06-29 00:50:39 +0000770 // Arguments can't alias with local allocations or noalias calls, unless
771 // we have to consider interprocedural aliasing.
772 if (!Interprocedural)
773 if ((isa<Argument>(O1) && (isa<AllocaInst>(O2) || isNoAliasCall(O2))) ||
774 (isa<Argument>(O2) && (isa<AllocaInst>(O1) || isNoAliasCall(O1))))
775 return NoAlias;
Evan Cheng094f04b2009-10-13 18:42:04 +0000776
777 // Most objects can't alias null.
778 if ((isa<ConstantPointerNull>(V2) && isKnownNonNull(O1)) ||
779 (isa<ConstantPointerNull>(V1) && isKnownNonNull(O2)))
780 return NoAlias;
781 }
782
783 // If the size of one access is larger than the entire object on the other
784 // side, then we know such behavior is undefined and can assume no alias.
Evan Cheng094f04b2009-10-13 18:42:04 +0000785 if (TD)
Chris Lattner7b550cc2009-11-06 04:27:31 +0000786 if ((V1Size != ~0U && isObjectSmallerThan(O2, V1Size, *TD)) ||
787 (V2Size != ~0U && isObjectSmallerThan(O1, V2Size, *TD)))
Evan Cheng094f04b2009-10-13 18:42:04 +0000788 return NoAlias;
789
Dan Gohmanf94b5ed2009-11-19 21:57:48 +0000790 // If one pointer is the result of a call/invoke or load and the other is a
Evan Cheng094f04b2009-10-13 18:42:04 +0000791 // non-escaping local object, then we know the object couldn't escape to a
Dan Gohman6be2bd52010-06-29 00:50:39 +0000792 // point where the call could return it.
Dan Gohmanf94b5ed2009-11-19 21:57:48 +0000793 if (O1 != O2) {
Dan Gohman6be2bd52010-06-29 00:50:39 +0000794 if (isEscapeSource(O1, Interprocedural) &&
795 isNonEscapingLocalObject(O2, Interprocedural))
Dan Gohmanf94b5ed2009-11-19 21:57:48 +0000796 return NoAlias;
Dan Gohman6be2bd52010-06-29 00:50:39 +0000797 if (isEscapeSource(O2, Interprocedural) &&
798 isNonEscapingLocalObject(O1, Interprocedural))
Dan Gohmanf94b5ed2009-11-19 21:57:48 +0000799 return NoAlias;
800 }
Evan Cheng094f04b2009-10-13 18:42:04 +0000801
Chris Lattner4e91ee72009-11-26 02:13:03 +0000802 // FIXME: This isn't aggressively handling alias(GEP, PHI) for example: if the
803 // GEP can't simplify, we don't even look at the PHI cases.
Chris Lattner391d23b2009-10-17 23:48:54 +0000804 if (!isa<GEPOperator>(V1) && isa<GEPOperator>(V2)) {
Chris Lattnerd501c132003-02-26 19:41:54 +0000805 std::swap(V1, V2);
806 std::swap(V1Size, V2Size);
Chris Lattner23e2a5b2009-11-26 02:14:59 +0000807 std::swap(O1, O2);
Chris Lattnerd501c132003-02-26 19:41:54 +0000808 }
Chris Lattner539c9b92009-11-26 02:11:08 +0000809 if (const GEPOperator *GV1 = dyn_cast<GEPOperator>(V1))
Chris Lattner23e2a5b2009-11-26 02:14:59 +0000810 return aliasGEP(GV1, V1Size, V2, V2Size, O1, O2);
Evan Cheng50a59142009-10-13 22:02:20 +0000811
812 if (isa<PHINode>(V2) && !isa<PHINode>(V1)) {
813 std::swap(V1, V2);
814 std::swap(V1Size, V2Size);
815 }
Evan Chengd83c2ca2009-10-14 05:22:03 +0000816 if (const PHINode *PN = dyn_cast<PHINode>(V1))
817 return aliasPHI(PN, V1Size, V2, V2Size);
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000818
Dan Gohman6665b0e2009-10-26 21:55:43 +0000819 if (isa<SelectInst>(V2) && !isa<SelectInst>(V1)) {
820 std::swap(V1, V2);
821 std::swap(V1Size, V2Size);
822 }
823 if (const SelectInst *S1 = dyn_cast<SelectInst>(V1))
824 return aliasSelect(S1, V1Size, V2, V2Size);
825
Chris Lattnerd501c132003-02-26 19:41:54 +0000826 return MayAlias;
827}
828
Chris Lattner5d56b2d2009-11-23 16:45:27 +0000829// Make sure that anything that uses AliasAnalysis pulls in this file.
Reid Spencer4f1bd9e2006-06-07 22:00:26 +0000830DEFINING_FILE_FOR(BasicAliasAnalysis)
Dan Gohman6be2bd52010-06-29 00:50:39 +0000831
832//===----------------------------------------------------------------------===//
833// InterproceduralBasicAliasAnalysis Pass
834//===----------------------------------------------------------------------===//
835
836namespace {
837 /// InterproceduralBasicAliasAnalysis - This is similar to basicaa, except
838 /// that it properly supports queries to values which live in different
839 /// functions.
840 ///
841 /// Note that we don't currently take this to the extreme, analyzing all
842 /// call sites of a function to answer a query about an Argument.
843 ///
844 struct InterproceduralBasicAliasAnalysis : public BasicAliasAnalysis {
845 static char ID; // Class identification, replacement for typeinfo
846 InterproceduralBasicAliasAnalysis() : BasicAliasAnalysis(&ID, true) {}
847 };
848}
849
850// Register this pass...
851char InterproceduralBasicAliasAnalysis::ID = 0;
852static RegisterPass<InterproceduralBasicAliasAnalysis>
853W("interprocedural-basic-aa", "Interprocedural Basic Alias Analysis", false, true);
854
855// Declare that we implement the AliasAnalysis interface
856static RegisterAnalysisGroup<AliasAnalysis> Z(W);
857
858ImmutablePass *llvm::createInterproceduralBasicAliasAnalysisPass() {
859 return new InterproceduralBasicAliasAnalysis();
860}