blob: 92cff8ea9698f77e97e58074447d9a272e1d831a [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"
Chris Lattner4244bb52004-03-15 03:36:49 +000024#include "llvm/Pass.h"
Chris Lattnerd501c132003-02-26 19:41:54 +000025#include "llvm/Target/TargetData.h"
Chris Lattnera77600e2007-02-10 22:15:31 +000026#include "llvm/ADT/SmallVector.h"
Owen Anderson718cb662007-09-07 04:06:50 +000027#include "llvm/ADT/STLExtras.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000028#include "llvm/Support/Compiler.h"
Chris Lattner90aa8392006-10-04 21:52:35 +000029#include "llvm/Support/GetElementPtrTypeIterator.h"
30#include "llvm/Support/ManagedStatic.h"
Alkis Evlogimenos20aa4742004-09-03 18:19:51 +000031#include <algorithm>
Chris Lattnerec4e8082003-11-25 18:33:40 +000032using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000033
Chris Lattnerdefa1c82008-06-16 06:30:22 +000034//===----------------------------------------------------------------------===//
35// Useful predicates
36//===----------------------------------------------------------------------===//
Devang Patel794fd752007-05-01 21:15:47 +000037
Duncan Sands91c9c3102009-01-05 21:19:53 +000038// Determine if a value escapes from the function it is contained in (being
39// returned by the function does not count as escaping here). If a value local
40// to the function does not escape, there is no way another function can mod/ref
41// it. We do this by looking at its uses and determining if they can escape
42// (recursively).
Chris Lattnerdefa1c82008-06-16 06:30:22 +000043static bool AddressMightEscape(const Value *V) {
44 for (Value::use_const_iterator UI = V->use_begin(), E = V->use_end();
45 UI != E; ++UI) {
46 const Instruction *I = cast<Instruction>(*UI);
47 switch (I->getOpcode()) {
48 case Instruction::Load:
49 break; //next use.
50 case Instruction::Store:
51 if (I->getOperand(0) == V)
52 return true; // Escapes if the pointer is stored.
53 break; // next use.
54 case Instruction::GetElementPtr:
55 if (AddressMightEscape(I))
56 return true;
57 break; // next use.
58 case Instruction::BitCast:
59 if (AddressMightEscape(I))
60 return true;
61 break; // next use
62 case Instruction::Ret:
63 // If returned, the address will escape to calling functions, but no
64 // callees could modify it.
65 break; // next use
66 case Instruction::Call:
Nick Lewyckyf23d0d32008-12-19 06:39:12 +000067 // If the argument to the call has the nocapture attribute, then the call
68 // may store or load to the pointer, but it cannot escape.
69 if (cast<CallInst>(I)->paramHasAttr(UI.getOperandNo(),
70 Attribute::NoCapture))
71 continue;
Nick Lewyckyf23d0d32008-12-19 06:39:12 +000072 return true;
73 case Instruction::Invoke:
74 // If the argument to the call has the nocapture attribute, then the call
75 // may store or load to the pointer, but it cannot escape.
76 if (cast<InvokeInst>(I)->paramHasAttr(UI.getOperandNo()-2,
77 Attribute::NoCapture))
78 continue;
79 return true;
Chris Lattnerdefa1c82008-06-16 06:30:22 +000080 default:
81 return true;
Chris Lattner689835a2004-06-19 08:05:58 +000082 }
Chris Lattnerdefa1c82008-06-16 06:30:22 +000083 }
84 return false;
Jeff Cohen534927d2005-01-08 22:01:16 +000085}
86
Chris Lattnerb307c882003-12-11 22:44:13 +000087static const User *isGEP(const Value *V) {
88 if (isa<GetElementPtrInst>(V) ||
89 (isa<ConstantExpr>(V) &&
90 cast<ConstantExpr>(V)->getOpcode() == Instruction::GetElementPtr))
91 return cast<User>(V);
92 return 0;
93}
Chris Lattnerd501c132003-02-26 19:41:54 +000094
Chris Lattnera77600e2007-02-10 22:15:31 +000095static const Value *GetGEPOperands(const Value *V,
Chris Lattnerb957bda2008-12-10 01:04:47 +000096 SmallVector<Value*, 16> &GEPOps) {
Chris Lattner4a830882003-12-11 23:20:16 +000097 assert(GEPOps.empty() && "Expect empty list to populate!");
98 GEPOps.insert(GEPOps.end(), cast<User>(V)->op_begin()+1,
99 cast<User>(V)->op_end());
100
101 // Accumulate all of the chained indexes into the operand array
102 V = cast<User>(V)->getOperand(0);
103
104 while (const User *G = isGEP(V)) {
Reid Spencere8404342004-07-18 00:18:30 +0000105 if (!isa<Constant>(GEPOps[0]) || isa<GlobalValue>(GEPOps[0]) ||
Chris Lattner4a830882003-12-11 23:20:16 +0000106 !cast<Constant>(GEPOps[0])->isNullValue())
107 break; // Don't handle folding arbitrary pointer offsets yet...
108 GEPOps.erase(GEPOps.begin()); // Drop the zero index
109 GEPOps.insert(GEPOps.begin(), G->op_begin()+1, G->op_end());
110 V = G->getOperand(0);
111 }
112 return V;
113}
114
Nick Lewycky02ff3082008-11-24 03:41:24 +0000115/// isNoAliasCall - Return true if this pointer is returned by a noalias
116/// function.
117static bool isNoAliasCall(const Value *V) {
118 if (isa<CallInst>(V) || isa<InvokeInst>(V))
119 return CallSite(const_cast<Instruction*>(cast<Instruction>(V)))
120 .paramHasAttr(0, Attribute::NoAlias);
121 return false;
122}
123
Chris Lattnera4139602008-06-16 06:10:11 +0000124/// isIdentifiedObject - Return true if this pointer refers to a distinct and
125/// identifiable object. This returns true for:
126/// Global Variables and Functions
127/// Allocas and Mallocs
128/// ByVal and NoAlias Arguments
Nick Lewycky02ff3082008-11-24 03:41:24 +0000129/// NoAlias returns
Chris Lattnera4139602008-06-16 06:10:11 +0000130///
131static bool isIdentifiedObject(const Value *V) {
Nick Lewycky02ff3082008-11-24 03:41:24 +0000132 if (isa<GlobalValue>(V) || isa<AllocationInst>(V) || isNoAliasCall(V))
Chris Lattnera4139602008-06-16 06:10:11 +0000133 return true;
134 if (const Argument *A = dyn_cast<Argument>(V))
135 return A->hasNoAliasAttr() || A->hasByValAttr();
136 return false;
137}
138
139/// isKnownNonNull - Return true if we know that the specified value is never
140/// null.
141static bool isKnownNonNull(const Value *V) {
142 // Alloca never returns null, malloc might.
143 if (isa<AllocaInst>(V)) return true;
144
145 // A byval argument is never null.
146 if (const Argument *A = dyn_cast<Argument>(V))
147 return A->hasByValAttr();
148
149 // Global values are not null unless extern weak.
150 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
151 return !GV->hasExternalWeakLinkage();
152 return false;
153}
154
Chris Lattner845f0d22008-06-16 06:19:11 +0000155/// isNonEscapingLocalObject - Return true if the pointer is to a function-local
156/// object that never escapes from the function.
157static bool isNonEscapingLocalObject(const Value *V) {
Chris Lattnere7275792008-06-16 06:28:01 +0000158 // If this is a local allocation, check to see if it escapes.
Nick Lewycky02ff3082008-11-24 03:41:24 +0000159 if (isa<AllocationInst>(V) || isNoAliasCall(V))
Chris Lattner845f0d22008-06-16 06:19:11 +0000160 return !AddressMightEscape(V);
Duncan Sands91c9c3102009-01-05 21:19:53 +0000161
Chris Lattnere7275792008-06-16 06:28:01 +0000162 // If this is an argument that corresponds to a byval or noalias argument,
Duncan Sands91c9c3102009-01-05 21:19:53 +0000163 // then it has not escaped before entering the function. Check if it escapes
164 // inside the function.
Chris Lattnere7275792008-06-16 06:28:01 +0000165 if (const Argument *A = dyn_cast<Argument>(V))
Duncan Sands91c9c3102009-01-05 21:19:53 +0000166 if (A->hasByValAttr() || A->hasNoAliasAttr()) {
167 // Don't bother analyzing arguments already known not to escape.
168 if (A->hasNoCaptureAttr())
169 return true;
Chris Lattnere7275792008-06-16 06:28:01 +0000170 return !AddressMightEscape(V);
Duncan Sands91c9c3102009-01-05 21:19:53 +0000171 }
Chris Lattner845f0d22008-06-16 06:19:11 +0000172 return false;
173}
174
175
Chris Lattnera4139602008-06-16 06:10:11 +0000176/// isObjectSmallerThan - Return true if we can prove that the object specified
177/// by V is smaller than Size.
178static bool isObjectSmallerThan(const Value *V, unsigned Size,
179 const TargetData &TD) {
Chris Lattner295d4e92008-12-08 06:28:54 +0000180 const Type *AccessTy;
181 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
Chris Lattnera4139602008-06-16 06:10:11 +0000182 AccessTy = GV->getType()->getElementType();
Chris Lattner295d4e92008-12-08 06:28:54 +0000183 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(V)) {
Chris Lattnera4139602008-06-16 06:10:11 +0000184 if (!AI->isArrayAllocation())
185 AccessTy = AI->getType()->getElementType();
Chris Lattner295d4e92008-12-08 06:28:54 +0000186 else
187 return false;
188 } else if (const Argument *A = dyn_cast<Argument>(V)) {
Chris Lattnera4139602008-06-16 06:10:11 +0000189 if (A->hasByValAttr())
190 AccessTy = cast<PointerType>(A->getType())->getElementType();
Chris Lattner295d4e92008-12-08 06:28:54 +0000191 else
192 return false;
193 } else {
194 return false;
195 }
Chris Lattnera4139602008-06-16 06:10:11 +0000196
Chris Lattner295d4e92008-12-08 06:28:54 +0000197 if (AccessTy->isSized())
Chris Lattnera4139602008-06-16 06:10:11 +0000198 return TD.getABITypeSize(AccessTy) < Size;
199 return false;
200}
201
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000202//===----------------------------------------------------------------------===//
203// NoAA Pass
204//===----------------------------------------------------------------------===//
205
206namespace {
207 /// NoAA - This class implements the -no-aa pass, which always returns "I
208 /// don't know" for alias queries. NoAA is unlike other alias analysis
209 /// implementations, in that it does not chain to a previous analysis. As
210 /// such it doesn't follow many of the rules that other alias analyses must.
211 ///
212 struct VISIBILITY_HIDDEN NoAA : public ImmutablePass, public AliasAnalysis {
213 static char ID; // Class identification, replacement for typeinfo
Dan Gohmanae73dc12008-09-04 17:05:41 +0000214 NoAA() : ImmutablePass(&ID) {}
215 explicit NoAA(void *PID) : ImmutablePass(PID) { }
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000216
217 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
218 AU.addRequired<TargetData>();
219 }
220
221 virtual void initializePass() {
222 TD = &getAnalysis<TargetData>();
223 }
224
225 virtual AliasResult alias(const Value *V1, unsigned V1Size,
226 const Value *V2, unsigned V2Size) {
227 return MayAlias;
228 }
229
230 virtual ModRefBehavior getModRefBehavior(Function *F, CallSite CS,
231 std::vector<PointerAccessInfo> *Info) {
232 return UnknownModRefBehavior;
233 }
234
235 virtual void getArgumentAccesses(Function *F, CallSite CS,
236 std::vector<PointerAccessInfo> &Info) {
237 assert(0 && "This method may not be called on this function!");
238 }
239
240 virtual void getMustAliases(Value *P, std::vector<Value*> &RetVals) { }
241 virtual bool pointsToConstantMemory(const Value *P) { return false; }
242 virtual ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size) {
243 return ModRef;
244 }
245 virtual ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) {
246 return ModRef;
247 }
248 virtual bool hasNoModRefInfoForCalls() const { return true; }
249
250 virtual void deleteValue(Value *V) {}
251 virtual void copyValue(Value *From, Value *To) {}
252 };
253} // End of anonymous namespace
254
255// Register this pass...
256char NoAA::ID = 0;
257static RegisterPass<NoAA>
258U("no-aa", "No Alias Analysis (always returns 'may' alias)", true, true);
259
260// Declare that we implement the AliasAnalysis interface
261static RegisterAnalysisGroup<AliasAnalysis> V(U);
262
263ImmutablePass *llvm::createNoAAPass() { return new NoAA(); }
264
265//===----------------------------------------------------------------------===//
266// BasicAA Pass
267//===----------------------------------------------------------------------===//
268
269namespace {
270 /// BasicAliasAnalysis - This is the default alias analysis implementation.
271 /// Because it doesn't chain to a previous alias analysis (like -no-aa), it
272 /// derives from the NoAA class.
273 struct VISIBILITY_HIDDEN BasicAliasAnalysis : public NoAA {
274 static char ID; // Class identification, replacement for typeinfo
Dan Gohmanae73dc12008-09-04 17:05:41 +0000275 BasicAliasAnalysis() : NoAA(&ID) {}
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000276 AliasResult alias(const Value *V1, unsigned V1Size,
277 const Value *V2, unsigned V2Size);
278
279 ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
Chris Lattner20d6f092008-12-09 21:19:42 +0000280 ModRefResult getModRefInfo(CallSite CS1, CallSite CS2);
281
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000282 /// hasNoModRefInfoForCalls - We can provide mod/ref information against
283 /// non-escaping allocations.
284 virtual bool hasNoModRefInfoForCalls() const { return false; }
285
286 /// pointsToConstantMemory - Chase pointers until we find a (constant
287 /// global) or not.
288 bool pointsToConstantMemory(const Value *P);
289
290 private:
291 // CheckGEPInstructions - Check two GEP instructions with known
292 // must-aliasing base pointers. This checks to see if the index expressions
293 // preclude the pointers from aliasing...
294 AliasResult
295 CheckGEPInstructions(const Type* BasePtr1Ty,
296 Value **GEP1Ops, unsigned NumGEP1Ops, unsigned G1Size,
297 const Type *BasePtr2Ty,
298 Value **GEP2Ops, unsigned NumGEP2Ops, unsigned G2Size);
299 };
300} // End of anonymous namespace
301
302// Register this pass...
303char BasicAliasAnalysis::ID = 0;
304static RegisterPass<BasicAliasAnalysis>
305X("basicaa", "Basic Alias Analysis (default AA impl)", false, true);
306
307// Declare that we implement the AliasAnalysis interface
308static RegisterAnalysisGroup<AliasAnalysis, true> Y(X);
309
310ImmutablePass *llvm::createBasicAliasAnalysisPass() {
311 return new BasicAliasAnalysis();
312}
313
314
315/// pointsToConstantMemory - Chase pointers until we find a (constant
316/// global) or not.
317bool BasicAliasAnalysis::pointsToConstantMemory(const Value *P) {
318 if (const GlobalVariable *GV =
Duncan Sands5d0392c2008-10-01 15:25:41 +0000319 dyn_cast<GlobalVariable>(P->getUnderlyingObject()))
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000320 return GV->isConstant();
321 return false;
322}
323
324// getModRefInfo - Check to see if the specified callsite can clobber the
325// specified memory object. Since we only look at local properties of this
326// function, we really can't say much about this query. We do, however, use
327// simple "address taken" analysis on local objects.
328//
329AliasAnalysis::ModRefResult
330BasicAliasAnalysis::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
331 if (!isa<Constant>(P)) {
Duncan Sands5d0392c2008-10-01 15:25:41 +0000332 const Value *Object = P->getUnderlyingObject();
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000333
334 // If this is a tail call and P points to a stack location, we know that
335 // the tail call cannot access or modify the local stack.
336 // We cannot exclude byval arguments here; these belong to the caller of
337 // the current function not to the current function, and a tail callee
338 // may reference them.
339 if (isa<AllocaInst>(Object))
340 if (CallInst *CI = dyn_cast<CallInst>(CS.getInstruction()))
341 if (CI->isTailCall())
342 return NoModRef;
343
Chris Lattner25df20f2008-06-16 06:38:26 +0000344 // If the pointer is to a locally allocated object that does not escape,
345 // then the call can not mod/ref the pointer unless the call takes the
346 // argument without capturing it.
347 if (isNonEscapingLocalObject(Object)) {
348 bool passedAsArg = false;
349 // TODO: Eventually only check 'nocapture' arguments.
350 for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
351 CI != CE; ++CI)
352 if (isa<PointerType>((*CI)->getType()) &&
353 alias(cast<Value>(CI), ~0U, P, ~0U) != NoAlias)
354 passedAsArg = true;
355
356 if (!passedAsArg)
357 return NoModRef;
Chris Lattnerdefa1c82008-06-16 06:30:22 +0000358 }
359 }
360
361 // The AliasAnalysis base class has some smarts, lets use them.
362 return AliasAnalysis::getModRefInfo(CS, P, Size);
363}
364
365
Chris Lattner20d6f092008-12-09 21:19:42 +0000366AliasAnalysis::ModRefResult
367BasicAliasAnalysis::getModRefInfo(CallSite CS1, CallSite CS2) {
368 // If CS1 or CS2 are readnone, they don't interact.
369 ModRefBehavior CS1B = AliasAnalysis::getModRefBehavior(CS1);
370 if (CS1B == DoesNotAccessMemory) return NoModRef;
371
372 ModRefBehavior CS2B = AliasAnalysis::getModRefBehavior(CS2);
373 if (CS2B == DoesNotAccessMemory) return NoModRef;
374
375 // If they both only read from memory, just return ref.
376 if (CS1B == OnlyReadsMemory && CS2B == OnlyReadsMemory)
377 return Ref;
378
379 // Otherwise, fall back to NoAA (mod+ref).
380 return NoAA::getModRefInfo(CS1, CS2);
381}
382
383
Chris Lattnerd501c132003-02-26 19:41:54 +0000384// alias - Provide a bunch of ad-hoc rules to disambiguate in common cases, such
Chris Lattnerb957bda2008-12-10 01:04:47 +0000385// as array references.
Chris Lattnerd501c132003-02-26 19:41:54 +0000386//
387AliasAnalysis::AliasResult
388BasicAliasAnalysis::alias(const Value *V1, unsigned V1Size,
389 const Value *V2, unsigned V2Size) {
Chris Lattnerb307c882003-12-11 22:44:13 +0000390 // Strip off any constant expression casts if they exist
391 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V1))
Reid Spencer3da59db2006-11-27 01:05:10 +0000392 if (CE->isCast() && isa<PointerType>(CE->getOperand(0)->getType()))
Chris Lattnerb307c882003-12-11 22:44:13 +0000393 V1 = CE->getOperand(0);
394 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V2))
Reid Spencer3da59db2006-11-27 01:05:10 +0000395 if (CE->isCast() && isa<PointerType>(CE->getOperand(0)->getType()))
Chris Lattnerb307c882003-12-11 22:44:13 +0000396 V2 = CE->getOperand(0);
397
Chris Lattnerd501c132003-02-26 19:41:54 +0000398 // Are we checking for alias of the same value?
399 if (V1 == V2) return MustAlias;
400
Nick Lewycky02ff3082008-11-24 03:41:24 +0000401 if (!isa<PointerType>(V1->getType()) || !isa<PointerType>(V2->getType()))
Chris Lattnerd501c132003-02-26 19:41:54 +0000402 return NoAlias; // Scalars cannot alias each other
403
Chris Lattnerb957bda2008-12-10 01:04:47 +0000404 // Strip off cast instructions. Since V1 and V2 are pointers, they must be
405 // pointer<->pointer bitcasts.
Reid Spencer3da59db2006-11-27 01:05:10 +0000406 if (const BitCastInst *I = dyn_cast<BitCastInst>(V1))
Chris Lattner241607d2007-01-14 05:57:53 +0000407 return alias(I->getOperand(0), V1Size, V2, V2Size);
Reid Spencer3da59db2006-11-27 01:05:10 +0000408 if (const BitCastInst *I = dyn_cast<BitCastInst>(V2))
Chris Lattner241607d2007-01-14 05:57:53 +0000409 return alias(V1, V1Size, I->getOperand(0), V2Size);
Chris Lattnerd501c132003-02-26 19:41:54 +0000410
Chris Lattnerb957bda2008-12-10 01:04:47 +0000411 // Figure out what objects these things are pointing to if we can.
Duncan Sands5d0392c2008-10-01 15:25:41 +0000412 const Value *O1 = V1->getUnderlyingObject();
413 const Value *O2 = V2->getUnderlyingObject();
Chris Lattnerd501c132003-02-26 19:41:54 +0000414
Chris Lattnera4139602008-06-16 06:10:11 +0000415 if (O1 != O2) {
416 // If V1/V2 point to two different objects we know that we have no alias.
417 if (isIdentifiedObject(O1) && isIdentifiedObject(O2))
418 return NoAlias;
419
Nick Lewyckyb2b32fd2008-11-24 05:00:44 +0000420 // Arguments can't alias with local allocations or noalias calls.
421 if ((isa<Argument>(O1) && (isa<AllocationInst>(O2) || isNoAliasCall(O2))) ||
422 (isa<Argument>(O2) && (isa<AllocationInst>(O1) || isNoAliasCall(O1))))
Chris Lattnera4139602008-06-16 06:10:11 +0000423 return NoAlias;
Nick Lewycky02ff3082008-11-24 03:41:24 +0000424
Chris Lattnera4139602008-06-16 06:10:11 +0000425 // Most objects can't alias null.
426 if ((isa<ConstantPointerNull>(V2) && isKnownNonNull(O1)) ||
427 (isa<ConstantPointerNull>(V1) && isKnownNonNull(O2)))
428 return NoAlias;
Chris Lattner0a1ac902004-11-26 19:20:01 +0000429 }
Chris Lattnera4139602008-06-16 06:10:11 +0000430
431 // If the size of one access is larger than the entire object on the other
432 // side, then we know such behavior is undefined and can assume no alias.
433 const TargetData &TD = getTargetData();
434 if ((V1Size != ~0U && isObjectSmallerThan(O2, V1Size, TD)) ||
435 (V2Size != ~0U && isObjectSmallerThan(O1, V2Size, TD)))
436 return NoAlias;
437
Chris Lattner845f0d22008-06-16 06:19:11 +0000438 // If one pointer is the result of a call/invoke and the other is a
439 // non-escaping local object, then we know the object couldn't escape to a
440 // point where the call could return it.
441 if ((isa<CallInst>(O1) || isa<InvokeInst>(O1)) &&
442 isNonEscapingLocalObject(O2))
443 return NoAlias;
444 if ((isa<CallInst>(O2) || isa<InvokeInst>(O2)) &&
445 isNonEscapingLocalObject(O1))
446 return NoAlias;
Chris Lattnera4139602008-06-16 06:10:11 +0000447
Chris Lattnerb307c882003-12-11 22:44:13 +0000448 // If we have two gep instructions with must-alias'ing base pointers, figure
449 // out if the indexes to the GEP tell us anything about the derived pointer.
450 // Note that we also handle chains of getelementptr instructions as well as
451 // constant expression getelementptrs here.
Chris Lattnerd501c132003-02-26 19:41:54 +0000452 //
Chris Lattnerb307c882003-12-11 22:44:13 +0000453 if (isGEP(V1) && isGEP(V2)) {
Chris Lattnerb957bda2008-12-10 01:04:47 +0000454 const User *GEP1 = cast<User>(V1);
455 const User *GEP2 = cast<User>(V2);
456
457 // If V1 and V2 are identical GEPs, just recurse down on both of them.
458 // This allows us to analyze things like:
459 // P = gep A, 0, i, 1
460 // Q = gep B, 0, i, 1
461 // by just analyzing A and B. This is even safe for variable indices.
462 if (GEP1->getType() == GEP2->getType() &&
463 GEP1->getNumOperands() == GEP2->getNumOperands() &&
464 GEP1->getOperand(0)->getType() == GEP2->getOperand(0)->getType() &&
465 // All operands are the same, ignoring the base.
466 std::equal(GEP1->op_begin()+1, GEP1->op_end(), GEP2->op_begin()+1))
467 return alias(GEP1->getOperand(0), V1Size, GEP2->getOperand(0), V2Size);
468
469
Chris Lattnerb307c882003-12-11 22:44:13 +0000470 // Drill down into the first non-gep value, to test for must-aliasing of
471 // the base pointers.
Chris Lattnerb957bda2008-12-10 01:04:47 +0000472 while (isGEP(GEP1->getOperand(0)) &&
473 GEP1->getOperand(1) ==
474 Constant::getNullValue(GEP1->getOperand(1)->getType()))
475 GEP1 = cast<User>(GEP1->getOperand(0));
476 const Value *BasePtr1 = GEP1->getOperand(0);
Wojciech Matyjewicz4ba8cfc2007-12-13 16:22:58 +0000477
Chris Lattnerb957bda2008-12-10 01:04:47 +0000478 while (isGEP(GEP2->getOperand(0)) &&
479 GEP2->getOperand(1) ==
480 Constant::getNullValue(GEP2->getOperand(1)->getType()))
481 GEP2 = cast<User>(GEP2->getOperand(0));
482 const Value *BasePtr2 = GEP2->getOperand(0);
Chris Lattnerb307c882003-12-11 22:44:13 +0000483
484 // Do the base pointers alias?
Chris Lattner241607d2007-01-14 05:57:53 +0000485 AliasResult BaseAlias = alias(BasePtr1, ~0U, BasePtr2, ~0U);
Chris Lattnerb307c882003-12-11 22:44:13 +0000486 if (BaseAlias == NoAlias) return NoAlias;
487 if (BaseAlias == MustAlias) {
488 // If the base pointers alias each other exactly, check to see if we can
489 // figure out anything about the resultant pointers, to try to prove
490 // non-aliasing.
491
492 // Collect all of the chained GEP operands together into one simple place
Chris Lattnera77600e2007-02-10 22:15:31 +0000493 SmallVector<Value*, 16> GEP1Ops, GEP2Ops;
Chris Lattner4a830882003-12-11 23:20:16 +0000494 BasePtr1 = GetGEPOperands(V1, GEP1Ops);
495 BasePtr2 = GetGEPOperands(V2, GEP2Ops);
Chris Lattnerb307c882003-12-11 22:44:13 +0000496
Chris Lattner730b1ad2004-07-29 07:56:39 +0000497 // If GetGEPOperands were able to fold to the same must-aliased pointer,
498 // do the comparison.
499 if (BasePtr1 == BasePtr2) {
500 AliasResult GAlias =
Chris Lattnerfd1ad3b2007-02-10 22:12:53 +0000501 CheckGEPInstructions(BasePtr1->getType(),
502 &GEP1Ops[0], GEP1Ops.size(), V1Size,
503 BasePtr2->getType(),
504 &GEP2Ops[0], GEP2Ops.size(), V2Size);
Chris Lattner730b1ad2004-07-29 07:56:39 +0000505 if (GAlias != MayAlias)
506 return GAlias;
507 }
Chris Lattnerb307c882003-12-11 22:44:13 +0000508 }
509 }
Chris Lattnerd501c132003-02-26 19:41:54 +0000510
511 // Check to see if these two pointers are related by a getelementptr
512 // instruction. If one pointer is a GEP with a non-zero index of the other
513 // pointer, we know they cannot alias.
514 //
Chris Lattner4a830882003-12-11 23:20:16 +0000515 if (isGEP(V2)) {
Chris Lattnerd501c132003-02-26 19:41:54 +0000516 std::swap(V1, V2);
517 std::swap(V1Size, V2Size);
518 }
519
Chris Lattnerc330ee62003-02-26 21:57:23 +0000520 if (V1Size != ~0U && V2Size != ~0U)
Reid Spencer3ed469c2006-11-02 20:25:50 +0000521 if (isGEP(V1)) {
Chris Lattnera77600e2007-02-10 22:15:31 +0000522 SmallVector<Value*, 16> GEPOperands;
Chris Lattner4a830882003-12-11 23:20:16 +0000523 const Value *BasePtr = GetGEPOperands(V1, GEPOperands);
524
525 AliasResult R = alias(BasePtr, V1Size, V2, V2Size);
Chris Lattnerc330ee62003-02-26 21:57:23 +0000526 if (R == MustAlias) {
527 // If there is at least one non-zero constant index, we know they cannot
528 // alias.
529 bool ConstantFound = false;
Chris Lattner88d3e032003-12-11 06:02:00 +0000530 bool AllZerosFound = true;
Chris Lattner4a830882003-12-11 23:20:16 +0000531 for (unsigned i = 0, e = GEPOperands.size(); i != e; ++i)
532 if (const Constant *C = dyn_cast<Constant>(GEPOperands[i])) {
Chris Lattnerc330ee62003-02-26 21:57:23 +0000533 if (!C->isNullValue()) {
534 ConstantFound = true;
Chris Lattnerc54735e2003-12-11 06:06:28 +0000535 AllZerosFound = false;
Chris Lattnerc330ee62003-02-26 21:57:23 +0000536 break;
Chris Lattner88d3e032003-12-11 06:02:00 +0000537 }
538 } else {
539 AllZerosFound = false;
Chris Lattnerc330ee62003-02-26 21:57:23 +0000540 }
Chris Lattner88d3e032003-12-11 06:02:00 +0000541
542 // If we have getelementptr <ptr>, 0, 0, 0, 0, ... and V2 must aliases
543 // the ptr, the end result is a must alias also.
544 if (AllZerosFound)
545 return MustAlias;
546
Chris Lattnerc330ee62003-02-26 21:57:23 +0000547 if (ConstantFound) {
548 if (V2Size <= 1 && V1Size <= 1) // Just pointer check?
Chris Lattnerd501c132003-02-26 19:41:54 +0000549 return NoAlias;
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000550
Chris Lattnerc330ee62003-02-26 21:57:23 +0000551 // Otherwise we have to check to see that the distance is more than
552 // the size of the argument... build an index vector that is equal to
553 // the arguments provided, except substitute 0's for any variable
554 // indexes we find...
Alkis Evlogimenosa95cf302004-12-08 23:42:11 +0000555 if (cast<PointerType>(
556 BasePtr->getType())->getElementType()->isSized()) {
557 for (unsigned i = 0; i != GEPOperands.size(); ++i)
Chris Lattner0bb38312007-01-12 18:20:48 +0000558 if (!isa<ConstantInt>(GEPOperands[i]))
Alkis Evlogimenosa95cf302004-12-08 23:42:11 +0000559 GEPOperands[i] =
560 Constant::getNullValue(GEPOperands[i]->getType());
561 int64_t Offset =
Chris Lattner829621c2007-02-10 20:35:22 +0000562 getTargetData().getIndexedOffset(BasePtr->getType(),
563 &GEPOperands[0],
564 GEPOperands.size());
Alkis Evlogimenosa95cf302004-12-08 23:42:11 +0000565
566 if (Offset >= (int64_t)V2Size || Offset <= -(int64_t)V1Size)
567 return NoAlias;
568 }
Chris Lattnerc330ee62003-02-26 21:57:23 +0000569 }
570 }
Chris Lattnerd501c132003-02-26 19:41:54 +0000571 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000572
Chris Lattnerd501c132003-02-26 19:41:54 +0000573 return MayAlias;
574}
575
Duncan Sands18327052008-12-08 14:01:59 +0000576// This function is used to determine if the indices of two GEP instructions are
Reid Spencer3da59db2006-11-27 01:05:10 +0000577// equal. V1 and V2 are the indices.
578static bool IndexOperandsEqual(Value *V1, Value *V2) {
Chris Lattner28977af2004-04-05 01:30:19 +0000579 if (V1->getType() == V2->getType())
580 return V1 == V2;
581 if (Constant *C1 = dyn_cast<Constant>(V1))
582 if (Constant *C2 = dyn_cast<Constant>(V2)) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000583 // Sign extend the constants to long types, if necessary
Reid Spencerc5b206b2006-12-31 05:48:39 +0000584 if (C1->getType() != Type::Int64Ty)
585 C1 = ConstantExpr::getSExt(C1, Type::Int64Ty);
586 if (C2->getType() != Type::Int64Ty)
587 C2 = ConstantExpr::getSExt(C2, Type::Int64Ty);
Chris Lattner28977af2004-04-05 01:30:19 +0000588 return C1 == C2;
589 }
590 return false;
591}
592
Chris Lattnerb307c882003-12-11 22:44:13 +0000593/// CheckGEPInstructions - Check two GEP instructions with known must-aliasing
594/// base pointers. This checks to see if the index expressions preclude the
595/// pointers from aliasing...
Reid Spencerb83eb642006-10-20 07:07:24 +0000596AliasAnalysis::AliasResult
597BasicAliasAnalysis::CheckGEPInstructions(
Chris Lattnerfd1ad3b2007-02-10 22:12:53 +0000598 const Type* BasePtr1Ty, Value **GEP1Ops, unsigned NumGEP1Ops, unsigned G1S,
599 const Type *BasePtr2Ty, Value **GEP2Ops, unsigned NumGEP2Ops, unsigned G2S) {
Chris Lattnerb307c882003-12-11 22:44:13 +0000600 // We currently can't handle the case when the base pointers have different
601 // primitive types. Since this is uncommon anyway, we are happy being
602 // extremely conservative.
603 if (BasePtr1Ty != BasePtr2Ty)
604 return MayAlias;
605
Alkis Evlogimenosc49741d2004-12-08 23:56:15 +0000606 const PointerType *GEPPointerTy = cast<PointerType>(BasePtr1Ty);
Chris Lattnerb307c882003-12-11 22:44:13 +0000607
608 // Find the (possibly empty) initial sequence of equal values... which are not
609 // necessarily constants.
Chris Lattnerfd1ad3b2007-02-10 22:12:53 +0000610 unsigned NumGEP1Operands = NumGEP1Ops, NumGEP2Operands = NumGEP2Ops;
Chris Lattnerb307c882003-12-11 22:44:13 +0000611 unsigned MinOperands = std::min(NumGEP1Operands, NumGEP2Operands);
612 unsigned MaxOperands = std::max(NumGEP1Operands, NumGEP2Operands);
613 unsigned UnequalOper = 0;
614 while (UnequalOper != MinOperands &&
Reid Spencer3da59db2006-11-27 01:05:10 +0000615 IndexOperandsEqual(GEP1Ops[UnequalOper], GEP2Ops[UnequalOper])) {
Chris Lattnerb307c882003-12-11 22:44:13 +0000616 // Advance through the type as we go...
617 ++UnequalOper;
618 if (const CompositeType *CT = dyn_cast<CompositeType>(BasePtr1Ty))
619 BasePtr1Ty = CT->getTypeAtIndex(GEP1Ops[UnequalOper-1]);
620 else {
621 // If all operands equal each other, then the derived pointers must
622 // alias each other...
623 BasePtr1Ty = 0;
624 assert(UnequalOper == NumGEP1Operands && UnequalOper == NumGEP2Operands &&
625 "Ran out of type nesting, but not out of operands?");
626 return MustAlias;
Chris Lattner920bd792003-06-02 05:42:39 +0000627 }
628 }
Chris Lattner920bd792003-06-02 05:42:39 +0000629
Chris Lattnerb307c882003-12-11 22:44:13 +0000630 // If we have seen all constant operands, and run out of indexes on one of the
631 // getelementptrs, check to see if the tail of the leftover one is all zeros.
632 // If so, return mustalias.
Chris Lattner4a830882003-12-11 23:20:16 +0000633 if (UnequalOper == MinOperands) {
Chris Lattnerfd1ad3b2007-02-10 22:12:53 +0000634 if (NumGEP1Ops < NumGEP2Ops) {
635 std::swap(GEP1Ops, GEP2Ops);
636 std::swap(NumGEP1Ops, NumGEP2Ops);
637 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000638
Chris Lattnerb307c882003-12-11 22:44:13 +0000639 bool AllAreZeros = true;
640 for (unsigned i = UnequalOper; i != MaxOperands; ++i)
Chris Lattnera35339d2004-10-16 16:07:10 +0000641 if (!isa<Constant>(GEP1Ops[i]) ||
Chris Lattnerb307c882003-12-11 22:44:13 +0000642 !cast<Constant>(GEP1Ops[i])->isNullValue()) {
643 AllAreZeros = false;
644 break;
645 }
646 if (AllAreZeros) return MustAlias;
647 }
648
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000649
Chris Lattnerd501c132003-02-26 19:41:54 +0000650 // So now we know that the indexes derived from the base pointers,
651 // which are known to alias, are different. We can still determine a
652 // no-alias result if there are differing constant pairs in the index
653 // chain. For example:
654 // A[i][0] != A[j][1] iff (&A[0][1]-&A[0][0] >= std::max(G1S, G2S))
655 //
Chris Lattner5a3cf8d2006-03-04 02:06:34 +0000656 // We have to be careful here about array accesses. In particular, consider:
657 // A[1][0] vs A[0][i]
658 // In this case, we don't *know* that the array will be accessed in bounds:
659 // the index could even be negative. Because of this, we have to
660 // conservatively *give up* and return may alias. We disregard differing
661 // array subscripts that are followed by a variable index without going
662 // through a struct.
663 //
Chris Lattnerd501c132003-02-26 19:41:54 +0000664 unsigned SizeMax = std::max(G1S, G2S);
Chris Lattnereaf8f9c2004-11-28 20:30:15 +0000665 if (SizeMax == ~0U) return MayAlias; // Avoid frivolous work.
Chris Lattner920bd792003-06-02 05:42:39 +0000666
Chris Lattnerd501c132003-02-26 19:41:54 +0000667 // Scan for the first operand that is constant and unequal in the
Chris Lattner28977af2004-04-05 01:30:19 +0000668 // two getelementptrs...
Chris Lattnerd501c132003-02-26 19:41:54 +0000669 unsigned FirstConstantOper = UnequalOper;
Chris Lattnerb307c882003-12-11 22:44:13 +0000670 for (; FirstConstantOper != MinOperands; ++FirstConstantOper) {
671 const Value *G1Oper = GEP1Ops[FirstConstantOper];
672 const Value *G2Oper = GEP2Ops[FirstConstantOper];
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000673
Chris Lattner6eb88d42004-01-12 17:57:32 +0000674 if (G1Oper != G2Oper) // Found non-equal constant indexes...
Chris Lattnera35339d2004-10-16 16:07:10 +0000675 if (Constant *G1OC = dyn_cast<ConstantInt>(const_cast<Value*>(G1Oper)))
676 if (Constant *G2OC = dyn_cast<ConstantInt>(const_cast<Value*>(G2Oper))){
Chris Lattner28977af2004-04-05 01:30:19 +0000677 if (G1OC->getType() != G2OC->getType()) {
678 // Sign extend both operands to long.
Reid Spencerc5b206b2006-12-31 05:48:39 +0000679 if (G1OC->getType() != Type::Int64Ty)
680 G1OC = ConstantExpr::getSExt(G1OC, Type::Int64Ty);
681 if (G2OC->getType() != Type::Int64Ty)
682 G2OC = ConstantExpr::getSExt(G2OC, Type::Int64Ty);
Chris Lattner28977af2004-04-05 01:30:19 +0000683 GEP1Ops[FirstConstantOper] = G1OC;
684 GEP2Ops[FirstConstantOper] = G2OC;
685 }
Chris Lattner5a3cf8d2006-03-04 02:06:34 +0000686
Chris Lattner28977af2004-04-05 01:30:19 +0000687 if (G1OC != G2OC) {
Dan Gohman07a96762007-07-16 14:29:03 +0000688 // Handle the "be careful" case above: if this is an array/vector
Chris Lattner5a3cf8d2006-03-04 02:06:34 +0000689 // subscript, scan for a subsequent variable array index.
Chris Lattner7765d712006-11-03 21:58:48 +0000690 if (isa<SequentialType>(BasePtr1Ty)) {
691 const Type *NextTy =
692 cast<SequentialType>(BasePtr1Ty)->getElementType();
Chris Lattner5a3cf8d2006-03-04 02:06:34 +0000693 bool isBadCase = false;
694
695 for (unsigned Idx = FirstConstantOper+1;
Chris Lattner7765d712006-11-03 21:58:48 +0000696 Idx != MinOperands && isa<SequentialType>(NextTy); ++Idx) {
Chris Lattner5a3cf8d2006-03-04 02:06:34 +0000697 const Value *V1 = GEP1Ops[Idx], *V2 = GEP2Ops[Idx];
698 if (!isa<Constant>(V1) || !isa<Constant>(V2)) {
699 isBadCase = true;
700 break;
701 }
Chris Lattner7765d712006-11-03 21:58:48 +0000702 NextTy = cast<SequentialType>(NextTy)->getElementType();
Chris Lattner5a3cf8d2006-03-04 02:06:34 +0000703 }
704
705 if (isBadCase) G1OC = 0;
706 }
707
Chris Lattnera35339d2004-10-16 16:07:10 +0000708 // Make sure they are comparable (ie, not constant expressions), and
709 // make sure the GEP with the smaller leading constant is GEP1.
Chris Lattner5a3cf8d2006-03-04 02:06:34 +0000710 if (G1OC) {
Reid Spencere4d87aa2006-12-23 06:05:41 +0000711 Constant *Compare = ConstantExpr::getICmp(ICmpInst::ICMP_SGT,
712 G1OC, G2OC);
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000713 if (ConstantInt *CV = dyn_cast<ConstantInt>(Compare)) {
Chris Lattnerfd1ad3b2007-02-10 22:12:53 +0000714 if (CV->getZExtValue()) { // If they are comparable and G2 > G1
Chris Lattner5a3cf8d2006-03-04 02:06:34 +0000715 std::swap(GEP1Ops, GEP2Ops); // Make GEP1 < GEP2
Chris Lattnerfd1ad3b2007-02-10 22:12:53 +0000716 std::swap(NumGEP1Ops, NumGEP2Ops);
717 }
Chris Lattner5a3cf8d2006-03-04 02:06:34 +0000718 break;
719 }
Chris Lattner28977af2004-04-05 01:30:19 +0000720 }
Chris Lattner6eb88d42004-01-12 17:57:32 +0000721 }
722 }
Chris Lattnerb307c882003-12-11 22:44:13 +0000723 BasePtr1Ty = cast<CompositeType>(BasePtr1Ty)->getTypeAtIndex(G1Oper);
Chris Lattnerd501c132003-02-26 19:41:54 +0000724 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000725
Chris Lattnerb307c882003-12-11 22:44:13 +0000726 // No shared constant operands, and we ran out of common operands. At this
727 // point, the GEP instructions have run through all of their operands, and we
728 // haven't found evidence that there are any deltas between the GEP's.
729 // However, one GEP may have more operands than the other. If this is the
Chris Lattner28977af2004-04-05 01:30:19 +0000730 // case, there may still be hope. Check this now.
Chris Lattnerb307c882003-12-11 22:44:13 +0000731 if (FirstConstantOper == MinOperands) {
732 // Make GEP1Ops be the longer one if there is a longer one.
Chris Lattnerfd1ad3b2007-02-10 22:12:53 +0000733 if (NumGEP1Ops < NumGEP2Ops) {
Chris Lattnerb307c882003-12-11 22:44:13 +0000734 std::swap(GEP1Ops, GEP2Ops);
Chris Lattnerfd1ad3b2007-02-10 22:12:53 +0000735 std::swap(NumGEP1Ops, NumGEP2Ops);
736 }
Chris Lattnerb307c882003-12-11 22:44:13 +0000737
738 // Is there anything to check?
Chris Lattnerfd1ad3b2007-02-10 22:12:53 +0000739 if (NumGEP1Ops > MinOperands) {
Chris Lattnerb307c882003-12-11 22:44:13 +0000740 for (unsigned i = FirstConstantOper; i != MaxOperands; ++i)
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000741 if (isa<ConstantInt>(GEP1Ops[i]) &&
Zhou Sheng843f07672007-04-19 05:39:12 +0000742 !cast<ConstantInt>(GEP1Ops[i])->isZero()) {
Chris Lattnerb307c882003-12-11 22:44:13 +0000743 // Yup, there's a constant in the tail. Set all variables to
Wojciech Matyjewicz98e3a682008-06-02 17:26:12 +0000744 // constants in the GEP instruction to make it suitable for
Chris Lattnerb307c882003-12-11 22:44:13 +0000745 // TargetData::getIndexedOffset.
746 for (i = 0; i != MaxOperands; ++i)
Chris Lattner0bb38312007-01-12 18:20:48 +0000747 if (!isa<ConstantInt>(GEP1Ops[i]))
Chris Lattnerb307c882003-12-11 22:44:13 +0000748 GEP1Ops[i] = Constant::getNullValue(GEP1Ops[i]->getType());
749 // Okay, now get the offset. This is the relative offset for the full
750 // instruction.
751 const TargetData &TD = getTargetData();
Chris Lattnerfd1ad3b2007-02-10 22:12:53 +0000752 int64_t Offset1 = TD.getIndexedOffset(GEPPointerTy, GEP1Ops,
753 NumGEP1Ops);
Chris Lattnerb307c882003-12-11 22:44:13 +0000754
Chris Lattnerfd1ad3b2007-02-10 22:12:53 +0000755 // Now check without any constants at the end.
756 int64_t Offset2 = TD.getIndexedOffset(GEPPointerTy, GEP1Ops,
757 MinOperands);
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000758
Wojciech Matyjewicz98e3a682008-06-02 17:26:12 +0000759 // Make sure we compare the absolute difference.
760 if (Offset1 > Offset2)
761 std::swap(Offset1, Offset2);
762
Chris Lattnerb307c882003-12-11 22:44:13 +0000763 // If the tail provided a bit enough offset, return noalias!
764 if ((uint64_t)(Offset2-Offset1) >= SizeMax)
765 return NoAlias;
Wojciech Matyjewicz98e3a682008-06-02 17:26:12 +0000766 // Otherwise break - we don't look for another constant in the tail.
767 break;
Chris Lattnerb307c882003-12-11 22:44:13 +0000768 }
769 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000770
Chris Lattnerb307c882003-12-11 22:44:13 +0000771 // Couldn't find anything useful.
772 return MayAlias;
773 }
Chris Lattnerd501c132003-02-26 19:41:54 +0000774
775 // If there are non-equal constants arguments, then we can figure
776 // out a minimum known delta between the two index expressions... at
777 // this point we know that the first constant index of GEP1 is less
778 // than the first constant index of GEP2.
Chris Lattner1af55e12003-11-25 20:10:07 +0000779
Chris Lattnerb307c882003-12-11 22:44:13 +0000780 // Advance BasePtr[12]Ty over this first differing constant operand.
Chris Lattner2cfdd282006-03-04 21:48:01 +0000781 BasePtr2Ty = cast<CompositeType>(BasePtr1Ty)->
782 getTypeAtIndex(GEP2Ops[FirstConstantOper]);
783 BasePtr1Ty = cast<CompositeType>(BasePtr1Ty)->
784 getTypeAtIndex(GEP1Ops[FirstConstantOper]);
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000785
Chris Lattnerb307c882003-12-11 22:44:13 +0000786 // We are going to be using TargetData::getIndexedOffset to determine the
787 // offset that each of the GEP's is reaching. To do this, we have to convert
788 // all variable references to constant references. To do this, we convert the
Chris Lattner2cfdd282006-03-04 21:48:01 +0000789 // initial sequence of array subscripts into constant zeros to start with.
790 const Type *ZeroIdxTy = GEPPointerTy;
791 for (unsigned i = 0; i != FirstConstantOper; ++i) {
792 if (!isa<StructType>(ZeroIdxTy))
Reid Spencerc5b206b2006-12-31 05:48:39 +0000793 GEP1Ops[i] = GEP2Ops[i] = Constant::getNullValue(Type::Int32Ty);
Chris Lattnerb307c882003-12-11 22:44:13 +0000794
Chris Lattner2cfdd282006-03-04 21:48:01 +0000795 if (const CompositeType *CT = dyn_cast<CompositeType>(ZeroIdxTy))
796 ZeroIdxTy = CT->getTypeAtIndex(GEP1Ops[i]);
797 }
798
Chris Lattnerb307c882003-12-11 22:44:13 +0000799 // We know that GEP1Ops[FirstConstantOper] & GEP2Ops[FirstConstantOper] are ok
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000800
Chris Lattnerd501c132003-02-26 19:41:54 +0000801 // Loop over the rest of the operands...
Chris Lattnerb307c882003-12-11 22:44:13 +0000802 for (unsigned i = FirstConstantOper+1; i != MaxOperands; ++i) {
Chris Lattnerfd1ad3b2007-02-10 22:12:53 +0000803 const Value *Op1 = i < NumGEP1Ops ? GEP1Ops[i] : 0;
804 const Value *Op2 = i < NumGEP2Ops ? GEP2Ops[i] : 0;
Chris Lattnerb307c882003-12-11 22:44:13 +0000805 // If they are equal, use a zero index...
806 if (Op1 == Op2 && BasePtr1Ty == BasePtr2Ty) {
Chris Lattner0bb38312007-01-12 18:20:48 +0000807 if (!isa<ConstantInt>(Op1))
Chris Lattnerb307c882003-12-11 22:44:13 +0000808 GEP1Ops[i] = GEP2Ops[i] = Constant::getNullValue(Op1->getType());
809 // Otherwise, just keep the constants we have.
Chris Lattnerd501c132003-02-26 19:41:54 +0000810 } else {
Chris Lattnerb307c882003-12-11 22:44:13 +0000811 if (Op1) {
812 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
813 // If this is an array index, make sure the array element is in range.
Chris Lattner7765d712006-11-03 21:58:48 +0000814 if (const ArrayType *AT = dyn_cast<ArrayType>(BasePtr1Ty)) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000815 if (Op1C->getZExtValue() >= AT->getNumElements())
Chris Lattnerb307c882003-12-11 22:44:13 +0000816 return MayAlias; // Be conservative with out-of-range accesses
Chris Lattnerf88380b2007-12-09 07:35:13 +0000817 } else if (const VectorType *VT = dyn_cast<VectorType>(BasePtr1Ty)) {
818 if (Op1C->getZExtValue() >= VT->getNumElements())
Chris Lattner7765d712006-11-03 21:58:48 +0000819 return MayAlias; // Be conservative with out-of-range accesses
820 }
821
Chris Lattnerb307c882003-12-11 22:44:13 +0000822 } else {
823 // GEP1 is known to produce a value less than GEP2. To be
824 // conservatively correct, we must assume the largest possible
825 // constant is used in this position. This cannot be the initial
826 // index to the GEP instructions (because we know we have at least one
827 // element before this one with the different constant arguments), so
828 // we know that the current index must be into either a struct or
829 // array. Because we know it's not constant, this cannot be a
830 // structure index. Because of this, we can calculate the maximum
831 // value possible.
832 //
833 if (const ArrayType *AT = dyn_cast<ArrayType>(BasePtr1Ty))
Chris Lattner241607d2007-01-14 05:57:53 +0000834 GEP1Ops[i] = ConstantInt::get(Type::Int64Ty,AT->getNumElements()-1);
Chris Lattner9907cb12007-11-06 05:58:42 +0000835 else if (const VectorType *VT = dyn_cast<VectorType>(BasePtr1Ty))
836 GEP1Ops[i] = ConstantInt::get(Type::Int64Ty,VT->getNumElements()-1);
Chris Lattnerb307c882003-12-11 22:44:13 +0000837 }
Chris Lattnerd501c132003-02-26 19:41:54 +0000838 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000839
Chris Lattnerb307c882003-12-11 22:44:13 +0000840 if (Op2) {
841 if (const ConstantInt *Op2C = dyn_cast<ConstantInt>(Op2)) {
842 // If this is an array index, make sure the array element is in range.
Chris Lattnerf88380b2007-12-09 07:35:13 +0000843 if (const ArrayType *AT = dyn_cast<ArrayType>(BasePtr2Ty)) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000844 if (Op2C->getZExtValue() >= AT->getNumElements())
Chris Lattnerb307c882003-12-11 22:44:13 +0000845 return MayAlias; // Be conservative with out-of-range accesses
Chris Lattnerf88380b2007-12-09 07:35:13 +0000846 } else if (const VectorType *VT = dyn_cast<VectorType>(BasePtr2Ty)) {
Chris Lattner9907cb12007-11-06 05:58:42 +0000847 if (Op2C->getZExtValue() >= VT->getNumElements())
Chris Lattner7765d712006-11-03 21:58:48 +0000848 return MayAlias; // Be conservative with out-of-range accesses
849 }
Chris Lattnerb307c882003-12-11 22:44:13 +0000850 } else { // Conservatively assume the minimum value for this index
851 GEP2Ops[i] = Constant::getNullValue(Op2->getType());
852 }
Chris Lattner920bd792003-06-02 05:42:39 +0000853 }
Chris Lattnerb307c882003-12-11 22:44:13 +0000854 }
855
856 if (BasePtr1Ty && Op1) {
857 if (const CompositeType *CT = dyn_cast<CompositeType>(BasePtr1Ty))
858 BasePtr1Ty = CT->getTypeAtIndex(GEP1Ops[i]);
859 else
860 BasePtr1Ty = 0;
861 }
862
863 if (BasePtr2Ty && Op2) {
864 if (const CompositeType *CT = dyn_cast<CompositeType>(BasePtr2Ty))
865 BasePtr2Ty = CT->getTypeAtIndex(GEP2Ops[i]);
866 else
867 BasePtr2Ty = 0;
Chris Lattnerd501c132003-02-26 19:41:54 +0000868 }
869 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000870
Alkis Evlogimenosc49741d2004-12-08 23:56:15 +0000871 if (GEPPointerTy->getElementType()->isSized()) {
Chris Lattner829621c2007-02-10 20:35:22 +0000872 int64_t Offset1 =
Chris Lattnerfd1ad3b2007-02-10 22:12:53 +0000873 getTargetData().getIndexedOffset(GEPPointerTy, GEP1Ops, NumGEP1Ops);
Chris Lattner829621c2007-02-10 20:35:22 +0000874 int64_t Offset2 =
Chris Lattnerfd1ad3b2007-02-10 22:12:53 +0000875 getTargetData().getIndexedOffset(GEPPointerTy, GEP2Ops, NumGEP2Ops);
Chris Lattner9907cb12007-11-06 05:58:42 +0000876 assert(Offset1 != Offset2 &&
877 "There is at least one different constant here!");
878
879 // Make sure we compare the absolute difference.
880 if (Offset1 > Offset2)
881 std::swap(Offset1, Offset2);
882
Alkis Evlogimenosc49741d2004-12-08 23:56:15 +0000883 if ((uint64_t)(Offset2-Offset1) >= SizeMax) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000884 //cerr << "Determined that these two GEP's don't alias ["
885 // << SizeMax << " bytes]: \n" << *GEP1 << *GEP2;
Alkis Evlogimenosc49741d2004-12-08 23:56:15 +0000886 return NoAlias;
887 }
Chris Lattnerd501c132003-02-26 19:41:54 +0000888 }
889 return MayAlias;
890}
891
Reid Spencer4f1bd9e2006-06-07 22:00:26 +0000892// Make sure that anything that uses AliasAnalysis pulls in this file...
893DEFINING_FILE_FOR(BasicAliasAnalysis)