blob: fb6b9df608d1985181a8cab7da84e832f6602b34 [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"
Duncan Sands8556d2a2009-01-18 12:19:30 +000017#include "llvm/Analysis/CaptureTracking.h"
Victor Hernandezf006b182009-10-27 20:05:49 +000018#include "llvm/Analysis/MemoryBuiltins.h"
Jeff Cohen534927d2005-01-08 22:01:16 +000019#include "llvm/Analysis/Passes.h"
Chris Lattner4244bb52004-03-15 03:36:49 +000020#include "llvm/Constants.h"
21#include "llvm/DerivedTypes.h"
22#include "llvm/Function.h"
Chris Lattner23e2a5b2009-11-26 02:14:59 +000023#include "llvm/GlobalAlias.h"
Chris Lattner4244bb52004-03-15 03:36:49 +000024#include "llvm/GlobalVariable.h"
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000025#include "llvm/Instructions.h"
Owen Anderson9b636cb2008-02-17 21:29:08 +000026#include "llvm/IntrinsicInst.h"
Dan Gohman3a7a68c2009-07-17 22:25:10 +000027#include "llvm/Operator.h"
Chris Lattner4244bb52004-03-15 03:36:49 +000028#include "llvm/Pass.h"
Chris Lattnerd501c132003-02-26 19:41:54 +000029#include "llvm/Target/TargetData.h"
Evan Cheng50a59142009-10-13 22:02:20 +000030#include "llvm/ADT/SmallSet.h"
Chris Lattnera77600e2007-02-10 22:15:31 +000031#include "llvm/ADT/SmallVector.h"
Owen Anderson718cb662007-09-07 04:06:50 +000032#include "llvm/ADT/STLExtras.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000033#include "llvm/Support/ErrorHandling.h"
Chris Lattner90aa8392006-10-04 21:52:35 +000034#include "llvm/Support/GetElementPtrTypeIterator.h"
Alkis Evlogimenos20aa4742004-09-03 18:19:51 +000035#include <algorithm>
Chris Lattnerec4e8082003-11-25 18:33:40 +000036using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000037
Chris Lattnerdefa1c82008-06-16 06:30:22 +000038//===----------------------------------------------------------------------===//
39// Useful predicates
40//===----------------------------------------------------------------------===//
Devang Patel794fd752007-05-01 21:15:47 +000041
Chris Lattnera4139602008-06-16 06:10:11 +000042/// isKnownNonNull - Return true if we know that the specified value is never
43/// null.
44static bool isKnownNonNull(const Value *V) {
45 // Alloca never returns null, malloc might.
46 if (isa<AllocaInst>(V)) return true;
47
48 // A byval argument is never null.
49 if (const Argument *A = dyn_cast<Argument>(V))
50 return A->hasByValAttr();
51
52 // Global values are not null unless extern weak.
53 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
54 return !GV->hasExternalWeakLinkage();
55 return false;
56}
57
Chris Lattner845f0d22008-06-16 06:19:11 +000058/// isNonEscapingLocalObject - Return true if the pointer is to a function-local
59/// object that never escapes from the function.
60static bool isNonEscapingLocalObject(const Value *V) {
Chris Lattnere7275792008-06-16 06:28:01 +000061 // If this is a local allocation, check to see if it escapes.
Victor Hernandez7b929da2009-10-23 21:09:37 +000062 if (isa<AllocaInst>(V) || isNoAliasCall(V))
Dan Gohmanf94b5ed2009-11-19 21:57:48 +000063 // Set StoreCaptures to True so that we can assume in our callers that the
64 // pointer is not the result of a load instruction. Currently
65 // PointerMayBeCaptured doesn't have any special analysis for the
66 // StoreCaptures=false case; if it did, our callers could be refined to be
67 // more precise.
68 return !PointerMayBeCaptured(V, false, /*StoreCaptures=*/true);
Duncan Sands91c9c3102009-01-05 21:19:53 +000069
Chris Lattnere7275792008-06-16 06:28:01 +000070 // If this is an argument that corresponds to a byval or noalias argument,
Duncan Sands91c9c3102009-01-05 21:19:53 +000071 // then it has not escaped before entering the function. Check if it escapes
72 // inside the function.
Chris Lattnere7275792008-06-16 06:28:01 +000073 if (const Argument *A = dyn_cast<Argument>(V))
Duncan Sands91c9c3102009-01-05 21:19:53 +000074 if (A->hasByValAttr() || A->hasNoAliasAttr()) {
75 // Don't bother analyzing arguments already known not to escape.
76 if (A->hasNoCaptureAttr())
77 return true;
Dan Gohmanf94b5ed2009-11-19 21:57:48 +000078 return !PointerMayBeCaptured(V, false, /*StoreCaptures=*/true);
Duncan Sands91c9c3102009-01-05 21:19:53 +000079 }
Chris Lattner845f0d22008-06-16 06:19:11 +000080 return false;
81}
82
83
Chris Lattnera4139602008-06-16 06:10:11 +000084/// isObjectSmallerThan - Return true if we can prove that the object specified
85/// by V is smaller than Size.
86static bool isObjectSmallerThan(const Value *V, unsigned Size,
Chris Lattner7b550cc2009-11-06 04:27:31 +000087 const TargetData &TD) {
Chris Lattner295d4e92008-12-08 06:28:54 +000088 const Type *AccessTy;
89 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
Chris Lattnera4139602008-06-16 06:10:11 +000090 AccessTy = GV->getType()->getElementType();
Victor Hernandez7b929da2009-10-23 21:09:37 +000091 } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
Chris Lattnera4139602008-06-16 06:10:11 +000092 if (!AI->isArrayAllocation())
93 AccessTy = AI->getType()->getElementType();
Chris Lattner295d4e92008-12-08 06:28:54 +000094 else
95 return false;
Victor Hernandez46e83122009-09-18 21:34:51 +000096 } else if (const CallInst* CI = extractMallocCall(V)) {
Chris Lattner7b550cc2009-11-06 04:27:31 +000097 if (!isArrayMalloc(V, &TD))
Victor Hernandez46e83122009-09-18 21:34:51 +000098 // The size is the argument to the malloc call.
99 if (const ConstantInt* C = dyn_cast<ConstantInt>(CI->getOperand(1)))
100 return (C->getZExtValue() < Size);
101 return false;
Chris Lattner295d4e92008-12-08 06:28:54 +0000102 } else if (const Argument *A = dyn_cast<Argument>(V)) {
Chris Lattnera4139602008-06-16 06:10:11 +0000103 if (A->hasByValAttr())
104 AccessTy = cast<PointerType>(A->getType())->getElementType();
Chris Lattner295d4e92008-12-08 06:28:54 +0000105 else
106 return false;
107 } else {
108 return false;
109 }
Chris Lattnera4139602008-06-16 06:10:11 +0000110
Chris Lattner295d4e92008-12-08 06:28:54 +0000111 if (AccessTy->isSized())
Duncan Sands777d2302009-05-09 07:06:46 +0000112 return TD.getTypeAllocSize(AccessTy) < Size;
Chris Lattnera4139602008-06-16 06:10:11 +0000113 return false;
114}
115
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000116//===----------------------------------------------------------------------===//
117// NoAA Pass
118//===----------------------------------------------------------------------===//
119
120namespace {
121 /// NoAA - This class implements the -no-aa pass, which always returns "I
122 /// don't know" for alias queries. NoAA is unlike other alias analysis
123 /// implementations, in that it does not chain to a previous analysis. As
124 /// such it doesn't follow many of the rules that other alias analyses must.
125 ///
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000126 struct NoAA : public ImmutablePass, public AliasAnalysis {
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000127 static char ID; // Class identification, replacement for typeinfo
Dan Gohmanae73dc12008-09-04 17:05:41 +0000128 NoAA() : ImmutablePass(&ID) {}
129 explicit NoAA(void *PID) : ImmutablePass(PID) { }
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000130
131 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000132 }
133
134 virtual void initializePass() {
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +0000135 TD = getAnalysisIfAvailable<TargetData>();
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000136 }
137
138 virtual AliasResult alias(const Value *V1, unsigned V1Size,
139 const Value *V2, unsigned V2Size) {
140 return MayAlias;
141 }
142
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000143 virtual void getArgumentAccesses(Function *F, CallSite CS,
144 std::vector<PointerAccessInfo> &Info) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000145 llvm_unreachable("This method may not be called on this function!");
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000146 }
147
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000148 virtual bool pointsToConstantMemory(const Value *P) { return false; }
149 virtual ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size) {
150 return ModRef;
151 }
152 virtual ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) {
153 return ModRef;
154 }
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000155
156 virtual void deleteValue(Value *V) {}
157 virtual void copyValue(Value *From, Value *To) {}
158 };
159} // End of anonymous namespace
160
161// Register this pass...
162char NoAA::ID = 0;
163static RegisterPass<NoAA>
164U("no-aa", "No Alias Analysis (always returns 'may' alias)", true, true);
165
166// Declare that we implement the AliasAnalysis interface
167static RegisterAnalysisGroup<AliasAnalysis> V(U);
168
169ImmutablePass *llvm::createNoAAPass() { return new NoAA(); }
170
171//===----------------------------------------------------------------------===//
172// BasicAA Pass
173//===----------------------------------------------------------------------===//
174
175namespace {
176 /// BasicAliasAnalysis - This is the default alias analysis implementation.
177 /// Because it doesn't chain to a previous alias analysis (like -no-aa), it
178 /// derives from the NoAA class.
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000179 struct BasicAliasAnalysis : public NoAA {
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000180 static char ID; // Class identification, replacement for typeinfo
Dan Gohmanae73dc12008-09-04 17:05:41 +0000181 BasicAliasAnalysis() : NoAA(&ID) {}
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000182 AliasResult alias(const Value *V1, unsigned V1Size,
Evan Cheng50a59142009-10-13 22:02:20 +0000183 const Value *V2, unsigned V2Size) {
Evan Chengf0429fd2009-10-14 06:46:26 +0000184 assert(VisitedPHIs.empty() && "VisitedPHIs must be cleared after use!");
185 AliasResult Alias = aliasCheck(V1, V1Size, V2, V2Size);
Evan Cheng3dbe43b2009-10-14 05:05:02 +0000186 VisitedPHIs.clear();
Evan Chengf0429fd2009-10-14 06:46:26 +0000187 return Alias;
Evan Cheng50a59142009-10-13 22:02:20 +0000188 }
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000189
190 ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
Chris Lattner20d6f092008-12-09 21:19:42 +0000191 ModRefResult getModRefInfo(CallSite CS1, CallSite CS2);
Owen Andersone7942202009-02-05 23:36:27 +0000192
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000193 /// pointsToConstantMemory - Chase pointers until we find a (constant
194 /// global) or not.
195 bool pointsToConstantMemory(const Value *P);
196
197 private:
Evan Cheng3dbe43b2009-10-14 05:05:02 +0000198 // VisitedPHIs - Track PHI nodes visited by a aliasCheck() call.
Dan Gohman6665b0e2009-10-26 21:55:43 +0000199 SmallPtrSet<const Value*, 16> VisitedPHIs;
Evan Cheng3dbe43b2009-10-14 05:05:02 +0000200
Chris Lattner5d56b2d2009-11-23 16:45:27 +0000201 // aliasGEP - Provide a bunch of ad-hoc rules to disambiguate a GEP
202 // instruction against another.
Chris Lattner539c9b92009-11-26 02:11:08 +0000203 AliasResult aliasGEP(const GEPOperator *V1, unsigned V1Size,
Chris Lattner23e2a5b2009-11-26 02:14:59 +0000204 const Value *V2, unsigned V2Size,
205 const Value *UnderlyingV1, const Value *UnderlyingV2);
Evan Cheng50a59142009-10-13 22:02:20 +0000206
Chris Lattner5d56b2d2009-11-23 16:45:27 +0000207 // aliasPHI - Provide a bunch of ad-hoc rules to disambiguate a PHI
208 // instruction against another.
Evan Chengd83c2ca2009-10-14 05:22:03 +0000209 AliasResult aliasPHI(const PHINode *PN, unsigned PNSize,
Evan Cheng3dbe43b2009-10-14 05:05:02 +0000210 const Value *V2, unsigned V2Size);
Evan Cheng50a59142009-10-13 22:02:20 +0000211
Dan Gohman6665b0e2009-10-26 21:55:43 +0000212 /// aliasSelect - Disambiguate a Select instruction against another value.
213 AliasResult aliasSelect(const SelectInst *SI, unsigned SISize,
214 const Value *V2, unsigned V2Size);
215
Evan Cheng50a59142009-10-13 22:02:20 +0000216 AliasResult aliasCheck(const Value *V1, unsigned V1Size,
Evan Cheng3dbe43b2009-10-14 05:05:02 +0000217 const Value *V2, unsigned V2Size);
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000218 };
219} // End of anonymous namespace
220
221// Register this pass...
222char BasicAliasAnalysis::ID = 0;
223static RegisterPass<BasicAliasAnalysis>
224X("basicaa", "Basic Alias Analysis (default AA impl)", false, true);
225
226// Declare that we implement the AliasAnalysis interface
227static RegisterAnalysisGroup<AliasAnalysis, true> Y(X);
228
229ImmutablePass *llvm::createBasicAliasAnalysisPass() {
230 return new BasicAliasAnalysis();
231}
232
233
234/// pointsToConstantMemory - Chase pointers until we find a (constant
235/// global) or not.
236bool BasicAliasAnalysis::pointsToConstantMemory(const Value *P) {
237 if (const GlobalVariable *GV =
Duncan Sands5d0392c2008-10-01 15:25:41 +0000238 dyn_cast<GlobalVariable>(P->getUnderlyingObject()))
Chris Lattnerb27db372009-11-23 17:07:35 +0000239 // Note: this doesn't require GV to be "ODR" because it isn't legal for a
240 // global to be marked constant in some modules and non-constant in others.
241 // GV may even be a declaration, not a definition.
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000242 return GV->isConstant();
243 return false;
244}
245
Owen Andersone7942202009-02-05 23:36:27 +0000246
Chris Lattner5d56b2d2009-11-23 16:45:27 +0000247/// getModRefInfo - Check to see if the specified callsite can clobber the
248/// specified memory object. Since we only look at local properties of this
249/// function, we really can't say much about this query. We do, however, use
250/// simple "address taken" analysis on local objects.
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000251AliasAnalysis::ModRefResult
252BasicAliasAnalysis::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
Chris Lattner92e803c2009-11-22 16:05:05 +0000253 const Value *Object = P->getUnderlyingObject();
254
255 // If this is a tail call and P points to a stack location, we know that
256 // the tail call cannot access or modify the local stack.
257 // We cannot exclude byval arguments here; these belong to the caller of
258 // the current function not to the current function, and a tail callee
259 // may reference them.
260 if (isa<AllocaInst>(Object))
261 if (CallInst *CI = dyn_cast<CallInst>(CS.getInstruction()))
262 if (CI->isTailCall())
263 return NoModRef;
264
265 // If the pointer is to a locally allocated object that does not escape,
Chris Lattnerb34b82e2009-11-23 16:44:43 +0000266 // then the call can not mod/ref the pointer unless the call takes the pointer
267 // as an argument, and itself doesn't capture it.
Chris Lattner403ac2e2009-11-23 16:46:41 +0000268 if (!isa<Constant>(Object) && CS.getInstruction() != Object &&
269 isNonEscapingLocalObject(Object)) {
Chris Lattnerb34b82e2009-11-23 16:44:43 +0000270 bool PassedAsArg = false;
271 unsigned ArgNo = 0;
Chris Lattner92e803c2009-11-22 16:05:05 +0000272 for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
Chris Lattnerb34b82e2009-11-23 16:44:43 +0000273 CI != CE; ++CI, ++ArgNo) {
274 // Only look at the no-capture pointer arguments.
275 if (!isa<PointerType>((*CI)->getType()) ||
276 !CS.paramHasAttr(ArgNo+1, Attribute::NoCapture))
277 continue;
278
279 // If this is a no-capture pointer argument, see if we can tell that it
280 // is impossible to alias the pointer we're checking. If not, we have to
281 // assume that the call could touch the pointer, even though it doesn't
282 // escape.
Chris Lattner403ac2e2009-11-23 16:46:41 +0000283 if (!isNoAlias(cast<Value>(CI), ~0U, P, ~0U)) {
Chris Lattnerb34b82e2009-11-23 16:44:43 +0000284 PassedAsArg = true;
285 break;
286 }
287 }
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000288
Chris Lattnerb34b82e2009-11-23 16:44:43 +0000289 if (!PassedAsArg)
Chris Lattner92e803c2009-11-22 16:05:05 +0000290 return NoModRef;
291 }
292
293 // Finally, handle specific knowledge of intrinsics.
294 IntrinsicInst *II = dyn_cast<IntrinsicInst>(CS.getInstruction());
295 if (II == 0)
296 return AliasAnalysis::getModRefInfo(CS, P, Size);
297
298 switch (II->getIntrinsicID()) {
299 default: break;
300 case Intrinsic::memcpy:
301 case Intrinsic::memmove: {
302 unsigned Len = ~0U;
303 if (ConstantInt *LenCI = dyn_cast<ConstantInt>(II->getOperand(3)))
304 Len = LenCI->getZExtValue();
305 Value *Dest = II->getOperand(1);
306 Value *Src = II->getOperand(2);
Chris Lattner403ac2e2009-11-23 16:46:41 +0000307 if (isNoAlias(Dest, Len, P, Size)) {
308 if (isNoAlias(Src, Len, P, Size))
Chris Lattner92e803c2009-11-22 16:05:05 +0000309 return NoModRef;
310 return Ref;
311 }
312 break;
313 }
314 case Intrinsic::memset:
Chris Lattner403ac2e2009-11-23 16:46:41 +0000315 // Since memset is 'accesses arguments' only, the AliasAnalysis base class
316 // will handle it for the variable length case.
Chris Lattner92e803c2009-11-22 16:05:05 +0000317 if (ConstantInt *LenCI = dyn_cast<ConstantInt>(II->getOperand(3))) {
318 unsigned Len = LenCI->getZExtValue();
319 Value *Dest = II->getOperand(1);
Chris Lattner403ac2e2009-11-23 16:46:41 +0000320 if (isNoAlias(Dest, Len, P, Size))
Chris Lattner25df20f2008-06-16 06:38:26 +0000321 return NoModRef;
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000322 }
Chris Lattner92e803c2009-11-22 16:05:05 +0000323 break;
324 case Intrinsic::atomic_cmp_swap:
325 case Intrinsic::atomic_swap:
326 case Intrinsic::atomic_load_add:
327 case Intrinsic::atomic_load_sub:
328 case Intrinsic::atomic_load_and:
329 case Intrinsic::atomic_load_nand:
330 case Intrinsic::atomic_load_or:
331 case Intrinsic::atomic_load_xor:
332 case Intrinsic::atomic_load_max:
333 case Intrinsic::atomic_load_min:
334 case Intrinsic::atomic_load_umax:
335 case Intrinsic::atomic_load_umin:
336 if (TD) {
337 Value *Op1 = II->getOperand(1);
338 unsigned Op1Size = TD->getTypeStoreSize(Op1->getType());
Chris Lattner403ac2e2009-11-23 16:46:41 +0000339 if (isNoAlias(Op1, Op1Size, P, Size))
Chris Lattner92e803c2009-11-22 16:05:05 +0000340 return NoModRef;
Nick Lewycky5c9be672009-10-13 07:48:38 +0000341 }
Chris Lattner92e803c2009-11-22 16:05:05 +0000342 break;
343 case Intrinsic::lifetime_start:
344 case Intrinsic::lifetime_end:
345 case Intrinsic::invariant_start: {
346 unsigned PtrSize = cast<ConstantInt>(II->getOperand(1))->getZExtValue();
Chris Lattner403ac2e2009-11-23 16:46:41 +0000347 if (isNoAlias(II->getOperand(2), PtrSize, P, Size))
Chris Lattner92e803c2009-11-22 16:05:05 +0000348 return NoModRef;
349 break;
350 }
351 case Intrinsic::invariant_end: {
352 unsigned PtrSize = cast<ConstantInt>(II->getOperand(2))->getZExtValue();
Chris Lattner403ac2e2009-11-23 16:46:41 +0000353 if (isNoAlias(II->getOperand(3), PtrSize, P, Size))
Chris Lattner92e803c2009-11-22 16:05:05 +0000354 return NoModRef;
355 break;
356 }
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000357 }
358
359 // The AliasAnalysis base class has some smarts, lets use them.
360 return AliasAnalysis::getModRefInfo(CS, P, Size);
361}
362
363
Chris Lattner20d6f092008-12-09 21:19:42 +0000364AliasAnalysis::ModRefResult
365BasicAliasAnalysis::getModRefInfo(CallSite CS1, CallSite CS2) {
366 // If CS1 or CS2 are readnone, they don't interact.
367 ModRefBehavior CS1B = AliasAnalysis::getModRefBehavior(CS1);
368 if (CS1B == DoesNotAccessMemory) return NoModRef;
369
370 ModRefBehavior CS2B = AliasAnalysis::getModRefBehavior(CS2);
371 if (CS2B == DoesNotAccessMemory) return NoModRef;
372
373 // If they both only read from memory, just return ref.
374 if (CS1B == OnlyReadsMemory && CS2B == OnlyReadsMemory)
375 return Ref;
376
377 // Otherwise, fall back to NoAA (mod+ref).
378 return NoAA::getModRefInfo(CS1, CS2);
379}
380
Chris Lattnerf6ac4d92009-11-26 16:18:10 +0000381/// GetLinearExpression - Analyze the specified value as a linear expression:
382/// "A*V + B". Return the scale and offset values as APInts and return V as a
383/// Value*. The incoming Value is known to be a scalar integer.
384static Value *GetLinearExpression(Value *V, APInt &Scale, APInt &Offset) {
385 assert(isa<IntegerType>(V->getType()) && "Not an integer value");
386
387 if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(V)) {
388 if (ConstantInt *RHSC = dyn_cast<ConstantInt>(BOp->getOperand(1))) {
389 switch (BOp->getOpcode()) {
390 default: break;
391 case Instruction::Add:
392 V = GetLinearExpression(BOp->getOperand(0), Scale, Offset);
393 Offset += RHSC->getValue();
394 return V;
395 // TODO: SHL, MUL, OR.
396 }
397 }
398 }
399
400 Scale = 1;
401 Offset = 0;
402 return V;
403}
404
Chris Lattner4e91ee72009-11-26 02:13:03 +0000405/// DecomposeGEPExpression - If V is a symbolic pointer expression, decompose it
406/// into a base pointer with a constant offset and a number of scaled symbolic
407/// offsets.
Chris Lattner23e2a5b2009-11-26 02:14:59 +0000408///
409/// When TargetData is around, this function is capable of analyzing everything
410/// that Value::getUnderlyingObject() can look through. When not, it just looks
411/// through pointer casts.
412///
413/// FIXME: Move this out to ValueTracking.cpp
414///
Chris Lattner4e91ee72009-11-26 02:13:03 +0000415static const Value *DecomposeGEPExpression(const Value *V, int64_t &BaseOffs,
Chris Lattnerd84eb912009-11-26 02:17:34 +0000416 SmallVectorImpl<std::pair<const Value*, int64_t> > &VarIndices,
Chris Lattner4e91ee72009-11-26 02:13:03 +0000417 const TargetData *TD) {
Chris Lattner23e2a5b2009-11-26 02:14:59 +0000418 // FIXME: Should limit depth like getUnderlyingObject?
Chris Lattner4e91ee72009-11-26 02:13:03 +0000419 BaseOffs = 0;
420 while (1) {
421 // See if this is a bitcast or GEP.
422 const Operator *Op = dyn_cast<Operator>(V);
Chris Lattner23e2a5b2009-11-26 02:14:59 +0000423 if (Op == 0) {
424 // The only non-operator case we can handle are GlobalAliases.
425 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
426 if (!GA->mayBeOverridden()) {
427 V = GA->getAliasee();
428 continue;
429 }
430 }
431 return V;
432 }
Chris Lattner4e91ee72009-11-26 02:13:03 +0000433
434 if (Op->getOpcode() == Instruction::BitCast) {
435 V = Op->getOperand(0);
436 continue;
437 }
438
Chris Lattner23e2a5b2009-11-26 02:14:59 +0000439 const GEPOperator *GEPOp = dyn_cast<GEPOperator>(Op);
440 if (GEPOp == 0)
Chris Lattner4e91ee72009-11-26 02:13:03 +0000441 return V;
442
443 // Don't attempt to analyze GEPs over unsized objects.
Chris Lattner23e2a5b2009-11-26 02:14:59 +0000444 if (!cast<PointerType>(GEPOp->getOperand(0)->getType())
Chris Lattner4e91ee72009-11-26 02:13:03 +0000445 ->getElementType()->isSized())
446 return V;
Chris Lattner23e2a5b2009-11-26 02:14:59 +0000447
448 // If we are lacking TargetData information, we can't compute the offets of
449 // elements computed by GEPs. However, we can handle bitcast equivalent
450 // GEPs.
451 if (!TD) {
452 if (!GEPOp->hasAllZeroIndices())
453 return V;
454 V = GEPOp->getOperand(0);
455 continue;
456 }
Chris Lattner4e91ee72009-11-26 02:13:03 +0000457
458 // Walk the indices of the GEP, accumulating them into BaseOff/VarIndices.
Chris Lattner23e2a5b2009-11-26 02:14:59 +0000459 gep_type_iterator GTI = gep_type_begin(GEPOp);
460 for (User::const_op_iterator I = next(GEPOp->op_begin()),
461 E = GEPOp->op_end(); I != E; ++I) {
Chris Lattner4e91ee72009-11-26 02:13:03 +0000462 Value *Index = *I;
463 // Compute the (potentially symbolic) offset in bytes for this index.
464 if (const StructType *STy = dyn_cast<StructType>(*GTI++)) {
465 // For a struct, add the member offset.
466 unsigned FieldNo = cast<ConstantInt>(Index)->getZExtValue();
467 if (FieldNo == 0) continue;
Chris Lattner4e91ee72009-11-26 02:13:03 +0000468
469 BaseOffs += TD->getStructLayout(STy)->getElementOffset(FieldNo);
470 continue;
471 }
472
473 // For an array/pointer, add the element offset, explicitly scaled.
474 if (ConstantInt *CIdx = dyn_cast<ConstantInt>(Index)) {
475 if (CIdx->isZero()) continue;
Chris Lattner4e91ee72009-11-26 02:13:03 +0000476 BaseOffs += TD->getTypeAllocSize(*GTI)*CIdx->getSExtValue();
477 continue;
478 }
479
Chris Lattner4e91ee72009-11-26 02:13:03 +0000480 // TODO: Could handle linear expressions here like A[X+1], also A[X*4|1].
481 uint64_t Scale = TD->getTypeAllocSize(*GTI);
482
Chris Lattnerf6ac4d92009-11-26 16:18:10 +0000483 unsigned Width = cast<IntegerType>(Index->getType())->getBitWidth();
484 APInt IndexScale(Width, 0), IndexOffset(Width, 0);
485 Index = GetLinearExpression(Index, IndexScale, IndexOffset);
486
487 Scale *= IndexScale.getZExtValue();
488 BaseOffs += IndexOffset.getZExtValue()*Scale;
489
490
Chris Lattner4e91ee72009-11-26 02:13:03 +0000491 // If we already had an occurrance of this index variable, merge this
492 // scale into it. For example, we want to handle:
493 // A[x][x] -> x*16 + x*4 -> x*20
Chris Lattnerd84eb912009-11-26 02:17:34 +0000494 // This also ensures that 'x' only appears in the index list once.
Chris Lattner4e91ee72009-11-26 02:13:03 +0000495 for (unsigned i = 0, e = VarIndices.size(); i != e; ++i) {
496 if (VarIndices[i].first == Index) {
497 Scale += VarIndices[i].second;
498 VarIndices.erase(VarIndices.begin()+i);
499 break;
500 }
501 }
502
503 // Make sure that we have a scale that makes sense for this target's
504 // pointer size.
505 if (unsigned ShiftBits = 64-TD->getPointerSizeInBits()) {
506 Scale <<= ShiftBits;
507 Scale >>= ShiftBits;
508 }
509
510 if (Scale)
511 VarIndices.push_back(std::make_pair(Index, Scale));
512 }
513
514 // Analyze the base pointer next.
Chris Lattner23e2a5b2009-11-26 02:14:59 +0000515 V = GEPOp->getOperand(0);
Chris Lattner4e91ee72009-11-26 02:13:03 +0000516 }
Chris Lattner4e91ee72009-11-26 02:13:03 +0000517}
518
Chris Lattnerd84eb912009-11-26 02:17:34 +0000519/// GetIndiceDifference - Dest and Src are the variable indices from two
520/// decomposed GetElementPtr instructions GEP1 and GEP2 which have common base
521/// pointers. Subtract the GEP2 indices from GEP1 to find the symbolic
522/// difference between the two pointers.
523static void GetIndiceDifference(
524 SmallVectorImpl<std::pair<const Value*, int64_t> > &Dest,
525 const SmallVectorImpl<std::pair<const Value*, int64_t> > &Src) {
526 if (Src.empty()) return;
527
528 for (unsigned i = 0, e = Src.size(); i != e; ++i) {
529 const Value *V = Src[i].first;
530 int64_t Scale = Src[i].second;
531
532 // Find V in Dest. This is N^2, but pointer indices almost never have more
533 // than a few variable indexes.
534 for (unsigned j = 0, e = Dest.size(); j != e; ++j) {
535 if (Dest[j].first != V) continue;
536
537 // If we found it, subtract off Scale V's from the entry in Dest. If it
538 // goes to zero, remove the entry.
539 if (Dest[j].second != Scale)
540 Dest[j].second -= Scale;
541 else
542 Dest.erase(Dest.begin()+j);
543 Scale = 0;
544 break;
545 }
546
547 // If we didn't consume this entry, add it to the end of the Dest list.
548 if (Scale)
549 Dest.push_back(std::make_pair(V, -Scale));
550 }
551}
Chris Lattner4e91ee72009-11-26 02:13:03 +0000552
Chris Lattner539c9b92009-11-26 02:11:08 +0000553/// aliasGEP - Provide a bunch of ad-hoc rules to disambiguate a GEP instruction
554/// against another pointer. We know that V1 is a GEP, but we don't know
Chris Lattner23e2a5b2009-11-26 02:14:59 +0000555/// anything about V2. UnderlyingV1 is GEP1->getUnderlyingObject(),
556/// UnderlyingV2 is the same for V2.
Chris Lattner539c9b92009-11-26 02:11:08 +0000557///
Chris Lattnerd501c132003-02-26 19:41:54 +0000558AliasAnalysis::AliasResult
Chris Lattner539c9b92009-11-26 02:11:08 +0000559BasicAliasAnalysis::aliasGEP(const GEPOperator *GEP1, unsigned V1Size,
Chris Lattner23e2a5b2009-11-26 02:14:59 +0000560 const Value *V2, unsigned V2Size,
561 const Value *UnderlyingV1,
562 const Value *UnderlyingV2) {
Chris Lattnerd84eb912009-11-26 02:17:34 +0000563 int64_t GEP1BaseOffset;
564 SmallVector<std::pair<const Value*, int64_t>, 4> GEP1VariableIndices;
565
Chris Lattnerb307c882003-12-11 22:44:13 +0000566 // If we have two gep instructions with must-alias'ing base pointers, figure
567 // out if the indexes to the GEP tell us anything about the derived pointer.
Chris Lattner539c9b92009-11-26 02:11:08 +0000568 if (const GEPOperator *GEP2 = dyn_cast<GEPOperator>(V2)) {
Chris Lattnerb307c882003-12-11 22:44:13 +0000569 // Do the base pointers alias?
Chris Lattnerd84eb912009-11-26 02:17:34 +0000570 AliasResult BaseAlias = aliasCheck(UnderlyingV1, ~0U, UnderlyingV2, ~0U);
571
572 // If we get a No or May, then return it immediately, no amount of analysis
573 // will improve this situation.
574 if (BaseAlias != MustAlias) return BaseAlias;
575
576 // Otherwise, we have a MustAlias. Since the base pointers alias each other
577 // exactly, see if the computed offset from the common pointer tells us
578 // about the relation of the resulting pointer.
579 const Value *GEP1BasePtr =
580 DecomposeGEPExpression(GEP1, GEP1BaseOffset, GEP1VariableIndices, TD);
581
582 int64_t GEP2BaseOffset;
583 SmallVector<std::pair<const Value*, int64_t>, 4> GEP2VariableIndices;
584 const Value *GEP2BasePtr =
585 DecomposeGEPExpression(GEP2, GEP2BaseOffset, GEP2VariableIndices, TD);
586
587 // If DecomposeGEPExpression isn't able to look all the way through the
588 // addressing operation, we must not have TD and this is too complex for us
589 // to handle without it.
590 if (GEP1BasePtr != UnderlyingV1 || GEP2BasePtr != UnderlyingV2) {
591 assert(TD == 0 &&
592 "DecomposeGEPExpression and getUnderlyingObject disagree!");
593 return MayAlias;
594 }
595
596 // Subtract the GEP2 pointer from the GEP1 pointer to find out their
597 // symbolic difference.
598 GEP1BaseOffset -= GEP2BaseOffset;
599 GetIndiceDifference(GEP1VariableIndices, GEP2VariableIndices);
600
601 } else {
602 // Check to see if these two pointers are related by the getelementptr
603 // instruction. If one pointer is a GEP with a non-zero index of the other
604 // pointer, we know they cannot alias.
605 //
606 // FIXME: The check below only looks at the size of one of the pointers, not
607 // both, this may cause us to miss things.
608 if (V1Size == ~0U || V2Size == ~0U)
609 return MayAlias;
Chris Lattnerb307c882003-12-11 22:44:13 +0000610
Chris Lattnerd84eb912009-11-26 02:17:34 +0000611 AliasResult R = aliasCheck(UnderlyingV1, ~0U, V2, V2Size);
612 if (R != MustAlias)
613 // If V2 may alias GEP base pointer, conservatively returns MayAlias.
614 // If V2 is known not to alias GEP base pointer, then the two values
615 // cannot alias per GEP semantics: "A pointer value formed from a
616 // getelementptr instruction is associated with the addresses associated
617 // with the first operand of the getelementptr".
618 return R;
Chris Lattnerb307c882003-12-11 22:44:13 +0000619
Chris Lattnerd84eb912009-11-26 02:17:34 +0000620 const Value *GEP1BasePtr =
621 DecomposeGEPExpression(GEP1, GEP1BaseOffset, GEP1VariableIndices, TD);
622
623 // If DecomposeGEPExpression isn't able to look all the way through the
624 // addressing operation, we must not have TD and this is too complex for us
625 // to handle without it.
626 if (GEP1BasePtr != UnderlyingV1) {
627 assert(TD == 0 &&
628 "DecomposeGEPExpression and getUnderlyingObject disagree!");
629 return MayAlias;
Chris Lattnerb307c882003-12-11 22:44:13 +0000630 }
631 }
Chris Lattnerd84eb912009-11-26 02:17:34 +0000632
633 // In the two GEP Case, if there is no difference in the offsets of the
634 // computed pointers, the resultant pointers are a must alias. This
635 // hapens when we have two lexically identical GEP's (for example).
Chris Lattnerd501c132003-02-26 19:41:54 +0000636 //
Chris Lattnerd84eb912009-11-26 02:17:34 +0000637 // In the other case, if we have getelementptr <ptr>, 0, 0, 0, 0, ... and V2
638 // must aliases the GEP, the end result is a must alias also.
639 if (GEP1BaseOffset == 0 && GEP1VariableIndices.empty())
Evan Cheng681a33e2009-10-14 06:41:49 +0000640 return MustAlias;
Evan Cheng094f04b2009-10-13 18:42:04 +0000641
Chris Lattner4e91ee72009-11-26 02:13:03 +0000642 // If we have a known constant offset, see if this offset is larger than the
643 // access size being queried. If so, and if no variable indices can remove
644 // pieces of this constant, then we know we have a no-alias. For example,
645 // &A[100] != &A.
646
647 // In order to handle cases like &A[100][i] where i is an out of range
648 // subscript, we have to ignore all constant offset pieces that are a multiple
649 // of a scaled index. Do this by removing constant offsets that are a
650 // multiple of any of our variable indices. This allows us to transform
651 // things like &A[i][1] because i has a stride of (e.g.) 8 bytes but the 1
652 // provides an offset of 4 bytes (assuming a <= 4 byte access).
Chris Lattnerd84eb912009-11-26 02:17:34 +0000653 for (unsigned i = 0, e = GEP1VariableIndices.size();
654 i != e && GEP1BaseOffset;++i)
655 if (int64_t RemovedOffset = GEP1BaseOffset/GEP1VariableIndices[i].second)
656 GEP1BaseOffset -= RemovedOffset*GEP1VariableIndices[i].second;
Chris Lattner4e91ee72009-11-26 02:13:03 +0000657
658 // If our known offset is bigger than the access size, we know we don't have
659 // an alias.
660 if (GEP1BaseOffset) {
661 if (GEP1BaseOffset >= (int64_t)V2Size ||
662 GEP1BaseOffset <= -(int64_t)V1Size)
Evan Cheng681a33e2009-10-14 06:41:49 +0000663 return NoAlias;
Evan Cheng094f04b2009-10-13 18:42:04 +0000664 }
Chris Lattner4e91ee72009-11-26 02:13:03 +0000665
Evan Cheng094f04b2009-10-13 18:42:04 +0000666 return MayAlias;
667}
668
Chris Lattner5d56b2d2009-11-23 16:45:27 +0000669/// aliasSelect - Provide a bunch of ad-hoc rules to disambiguate a Select
670/// instruction against another.
Dan Gohman6665b0e2009-10-26 21:55:43 +0000671AliasAnalysis::AliasResult
672BasicAliasAnalysis::aliasSelect(const SelectInst *SI, unsigned SISize,
673 const Value *V2, unsigned V2Size) {
674 // If the values are Selects with the same condition, we can do a more precise
675 // check: just check for aliases between the values on corresponding arms.
676 if (const SelectInst *SI2 = dyn_cast<SelectInst>(V2))
677 if (SI->getCondition() == SI2->getCondition()) {
678 AliasResult Alias =
679 aliasCheck(SI->getTrueValue(), SISize,
680 SI2->getTrueValue(), V2Size);
681 if (Alias == MayAlias)
682 return MayAlias;
683 AliasResult ThisAlias =
684 aliasCheck(SI->getFalseValue(), SISize,
685 SI2->getFalseValue(), V2Size);
686 if (ThisAlias != Alias)
687 return MayAlias;
688 return Alias;
689 }
690
691 // If both arms of the Select node NoAlias or MustAlias V2, then returns
692 // NoAlias / MustAlias. Otherwise, returns MayAlias.
693 AliasResult Alias =
694 aliasCheck(SI->getTrueValue(), SISize, V2, V2Size);
695 if (Alias == MayAlias)
696 return MayAlias;
697 AliasResult ThisAlias =
698 aliasCheck(SI->getFalseValue(), SISize, V2, V2Size);
699 if (ThisAlias != Alias)
700 return MayAlias;
701 return Alias;
702}
703
Evan Chengd83c2ca2009-10-14 05:22:03 +0000704// aliasPHI - Provide a bunch of ad-hoc rules to disambiguate a PHI instruction
Evan Cheng3dbe43b2009-10-14 05:05:02 +0000705// against another.
Evan Cheng50a59142009-10-13 22:02:20 +0000706AliasAnalysis::AliasResult
Evan Chengd83c2ca2009-10-14 05:22:03 +0000707BasicAliasAnalysis::aliasPHI(const PHINode *PN, unsigned PNSize,
Evan Cheng3dbe43b2009-10-14 05:05:02 +0000708 const Value *V2, unsigned V2Size) {
Evan Cheng50a59142009-10-13 22:02:20 +0000709 // The PHI node has already been visited, avoid recursion any further.
Evan Chengd83c2ca2009-10-14 05:22:03 +0000710 if (!VisitedPHIs.insert(PN))
Evan Cheng50a59142009-10-13 22:02:20 +0000711 return MayAlias;
712
Dan Gohman6665b0e2009-10-26 21:55:43 +0000713 // If the values are PHIs in the same block, we can do a more precise
714 // as well as efficient check: just check for aliases between the values
715 // on corresponding edges.
716 if (const PHINode *PN2 = dyn_cast<PHINode>(V2))
717 if (PN2->getParent() == PN->getParent()) {
718 AliasResult Alias =
719 aliasCheck(PN->getIncomingValue(0), PNSize,
720 PN2->getIncomingValueForBlock(PN->getIncomingBlock(0)),
721 V2Size);
722 if (Alias == MayAlias)
723 return MayAlias;
724 for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) {
725 AliasResult ThisAlias =
726 aliasCheck(PN->getIncomingValue(i), PNSize,
727 PN2->getIncomingValueForBlock(PN->getIncomingBlock(i)),
728 V2Size);
729 if (ThisAlias != Alias)
730 return MayAlias;
731 }
732 return Alias;
733 }
734
Evan Chenga846a8a2009-10-16 00:33:09 +0000735 SmallPtrSet<Value*, 4> UniqueSrc;
Evan Cheng50a59142009-10-13 22:02:20 +0000736 SmallVector<Value*, 4> V1Srcs;
Evan Cheng50a59142009-10-13 22:02:20 +0000737 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
738 Value *PV1 = PN->getIncomingValue(i);
739 if (isa<PHINode>(PV1))
740 // If any of the source itself is a PHI, return MayAlias conservatively
Evan Cheng681a33e2009-10-14 06:41:49 +0000741 // to avoid compile time explosion. The worst possible case is if both
742 // sides are PHI nodes. In which case, this is O(m x n) time where 'm'
743 // and 'n' are the number of PHI sources.
Evan Cheng50a59142009-10-13 22:02:20 +0000744 return MayAlias;
745 if (UniqueSrc.insert(PV1))
746 V1Srcs.push_back(PV1);
747 }
748
Dan Gohman6665b0e2009-10-26 21:55:43 +0000749 AliasResult Alias = aliasCheck(V2, V2Size, V1Srcs[0], PNSize);
Evan Chengd83c2ca2009-10-14 05:22:03 +0000750 // Early exit if the check of the first PHI source against V2 is MayAlias.
751 // Other results are not possible.
752 if (Alias == MayAlias)
753 return MayAlias;
754
Evan Cheng50a59142009-10-13 22:02:20 +0000755 // If all sources of the PHI node NoAlias or MustAlias V2, then returns
756 // NoAlias / MustAlias. Otherwise, returns MayAlias.
Evan Cheng50a59142009-10-13 22:02:20 +0000757 for (unsigned i = 1, e = V1Srcs.size(); i != e; ++i) {
758 Value *V = V1Srcs[i];
Dan Gohman6665b0e2009-10-26 21:55:43 +0000759
760 // If V2 is a PHI, the recursive case will have been caught in the
761 // above aliasCheck call, so these subsequent calls to aliasCheck
762 // don't need to assume that V2 is being visited recursively.
763 VisitedPHIs.erase(V2);
764
Evan Chenga846a8a2009-10-16 00:33:09 +0000765 AliasResult ThisAlias = aliasCheck(V2, V2Size, V, PNSize);
Evan Chengd83c2ca2009-10-14 05:22:03 +0000766 if (ThisAlias != Alias || ThisAlias == MayAlias)
Evan Cheng50a59142009-10-13 22:02:20 +0000767 return MayAlias;
768 }
769
770 return Alias;
771}
772
773// aliasCheck - Provide a bunch of ad-hoc rules to disambiguate in common cases,
774// such as array references.
Evan Cheng094f04b2009-10-13 18:42:04 +0000775//
776AliasAnalysis::AliasResult
Evan Cheng50a59142009-10-13 22:02:20 +0000777BasicAliasAnalysis::aliasCheck(const Value *V1, unsigned V1Size,
Evan Cheng3dbe43b2009-10-14 05:05:02 +0000778 const Value *V2, unsigned V2Size) {
Evan Cheng094f04b2009-10-13 18:42:04 +0000779 // Strip off any casts if they exist.
780 V1 = V1->stripPointerCasts();
781 V2 = V2->stripPointerCasts();
782
783 // Are we checking for alias of the same value?
784 if (V1 == V2) return MustAlias;
785
786 if (!isa<PointerType>(V1->getType()) || !isa<PointerType>(V2->getType()))
787 return NoAlias; // Scalars cannot alias each other
788
789 // Figure out what objects these things are pointing to if we can.
790 const Value *O1 = V1->getUnderlyingObject();
791 const Value *O2 = V2->getUnderlyingObject();
792
Dan Gohmanf75ef662009-11-09 19:29:11 +0000793 // Null values in the default address space don't point to any object, so they
794 // don't alias any other pointer.
795 if (const ConstantPointerNull *CPN = dyn_cast<ConstantPointerNull>(O1))
796 if (CPN->getType()->getAddressSpace() == 0)
797 return NoAlias;
798 if (const ConstantPointerNull *CPN = dyn_cast<ConstantPointerNull>(O2))
799 if (CPN->getType()->getAddressSpace() == 0)
800 return NoAlias;
801
Evan Cheng094f04b2009-10-13 18:42:04 +0000802 if (O1 != O2) {
803 // If V1/V2 point to two different objects we know that we have no alias.
804 if (isIdentifiedObject(O1) && isIdentifiedObject(O2))
805 return NoAlias;
Nick Lewycky20162ac2009-11-14 06:15:14 +0000806
807 // Constant pointers can't alias with non-const isIdentifiedObject objects.
808 if ((isa<Constant>(O1) && isIdentifiedObject(O2) && !isa<Constant>(O2)) ||
809 (isa<Constant>(O2) && isIdentifiedObject(O1) && !isa<Constant>(O1)))
810 return NoAlias;
811
Evan Cheng094f04b2009-10-13 18:42:04 +0000812 // Arguments can't alias with local allocations or noalias calls.
Victor Hernandez7b929da2009-10-23 21:09:37 +0000813 if ((isa<Argument>(O1) && (isa<AllocaInst>(O2) || isNoAliasCall(O2))) ||
814 (isa<Argument>(O2) && (isa<AllocaInst>(O1) || isNoAliasCall(O1))))
Evan Cheng094f04b2009-10-13 18:42:04 +0000815 return NoAlias;
816
817 // Most objects can't alias null.
818 if ((isa<ConstantPointerNull>(V2) && isKnownNonNull(O1)) ||
819 (isa<ConstantPointerNull>(V1) && isKnownNonNull(O2)))
820 return NoAlias;
821 }
822
823 // If the size of one access is larger than the entire object on the other
824 // side, then we know such behavior is undefined and can assume no alias.
Evan Cheng094f04b2009-10-13 18:42:04 +0000825 if (TD)
Chris Lattner7b550cc2009-11-06 04:27:31 +0000826 if ((V1Size != ~0U && isObjectSmallerThan(O2, V1Size, *TD)) ||
827 (V2Size != ~0U && isObjectSmallerThan(O1, V2Size, *TD)))
Evan Cheng094f04b2009-10-13 18:42:04 +0000828 return NoAlias;
829
Dan Gohmanf94b5ed2009-11-19 21:57:48 +0000830 // If one pointer is the result of a call/invoke or load and the other is a
Evan Cheng094f04b2009-10-13 18:42:04 +0000831 // non-escaping local object, then we know the object couldn't escape to a
Dan Gohmanf94b5ed2009-11-19 21:57:48 +0000832 // point where the call could return it. The load case works because
833 // isNonEscapingLocalObject considers all stores to be escapes (it
834 // passes true for the StoreCaptures argument to PointerMayBeCaptured).
835 if (O1 != O2) {
Chris Lattner5d56b2d2009-11-23 16:45:27 +0000836 if ((isa<CallInst>(O1) || isa<InvokeInst>(O1) || isa<LoadInst>(O1) ||
Chris Lattner403ac2e2009-11-23 16:46:41 +0000837 isa<Argument>(O1)) &&
Dan Gohmanf94b5ed2009-11-19 21:57:48 +0000838 isNonEscapingLocalObject(O2))
839 return NoAlias;
Chris Lattner5d56b2d2009-11-23 16:45:27 +0000840 if ((isa<CallInst>(O2) || isa<InvokeInst>(O2) || isa<LoadInst>(O2) ||
Chris Lattner403ac2e2009-11-23 16:46:41 +0000841 isa<Argument>(O2)) &&
Dan Gohmanf94b5ed2009-11-19 21:57:48 +0000842 isNonEscapingLocalObject(O1))
843 return NoAlias;
844 }
Evan Cheng094f04b2009-10-13 18:42:04 +0000845
Chris Lattner4e91ee72009-11-26 02:13:03 +0000846 // FIXME: This isn't aggressively handling alias(GEP, PHI) for example: if the
847 // GEP can't simplify, we don't even look at the PHI cases.
Chris Lattner391d23b2009-10-17 23:48:54 +0000848 if (!isa<GEPOperator>(V1) && isa<GEPOperator>(V2)) {
Chris Lattnerd501c132003-02-26 19:41:54 +0000849 std::swap(V1, V2);
850 std::swap(V1Size, V2Size);
Chris Lattner23e2a5b2009-11-26 02:14:59 +0000851 std::swap(O1, O2);
Chris Lattnerd501c132003-02-26 19:41:54 +0000852 }
Chris Lattner539c9b92009-11-26 02:11:08 +0000853 if (const GEPOperator *GV1 = dyn_cast<GEPOperator>(V1))
Chris Lattner23e2a5b2009-11-26 02:14:59 +0000854 return aliasGEP(GV1, V1Size, V2, V2Size, O1, O2);
Evan Cheng50a59142009-10-13 22:02:20 +0000855
856 if (isa<PHINode>(V2) && !isa<PHINode>(V1)) {
857 std::swap(V1, V2);
858 std::swap(V1Size, V2Size);
859 }
Evan Chengd83c2ca2009-10-14 05:22:03 +0000860 if (const PHINode *PN = dyn_cast<PHINode>(V1))
861 return aliasPHI(PN, V1Size, V2, V2Size);
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000862
Dan Gohman6665b0e2009-10-26 21:55:43 +0000863 if (isa<SelectInst>(V2) && !isa<SelectInst>(V1)) {
864 std::swap(V1, V2);
865 std::swap(V1Size, V2Size);
866 }
867 if (const SelectInst *S1 = dyn_cast<SelectInst>(V1))
868 return aliasSelect(S1, V1Size, V2, V2Size);
869
Chris Lattnerd501c132003-02-26 19:41:54 +0000870 return MayAlias;
871}
872
Chris Lattner5d56b2d2009-11-23 16:45:27 +0000873// Make sure that anything that uses AliasAnalysis pulls in this file.
Reid Spencer4f1bd9e2006-06-07 22:00:26 +0000874DEFINING_FILE_FOR(BasicAliasAnalysis)