blob: e928a265ff091da20634fd413205ee51f10996a3 [file] [log] [blame]
Chris Lattner9d7c9ea2003-11-25 20:11:47 +00001//===- BasicAliasAnalysis.cpp - Local Alias Analysis Impl -----------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
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//
Chris Lattner6cdc42b2003-12-28 04:03:49 +000014// FIXME: This could be extended for a very simple form of mod/ref information.
15// If a pointer is locally allocated (either malloc or alloca) and never passed
16// into a call or stored to memory, then we know that calls will not mod/ref the
Chris Lattner2d6a6aa2004-03-01 02:44:44 +000017// memory. This can be important for tailcallelim, and can support CSE of loads
18// and dead store elimination across calls. This is particularly important for
19// stack allocated arrays.
Chris Lattner6cdc42b2003-12-28 04:03:49 +000020//
Chris Lattnerd501c132003-02-26 19:41:54 +000021//===----------------------------------------------------------------------===//
22
23#include "llvm/Analysis/AliasAnalysis.h"
24#include "llvm/Pass.h"
Chris Lattnerc1820032003-09-20 03:08:47 +000025#include "llvm/Argument.h"
Chris Lattnerd501c132003-02-26 19:41:54 +000026#include "llvm/iOther.h"
Chris Lattnere735b2d2004-02-22 06:26:17 +000027#include "llvm/iMemory.h"
Chris Lattner6eb88d42004-01-12 17:57:32 +000028#include "llvm/Constants.h"
Chris Lattnerbc1daaa2004-01-30 22:17:24 +000029#include "llvm/GlobalVariable.h"
Chris Lattnerd501c132003-02-26 19:41:54 +000030#include "llvm/DerivedTypes.h"
31#include "llvm/Target/TargetData.h"
Chris Lattner1af55e12003-11-25 20:10:07 +000032#include "llvm/Support/GetElementPtrTypeIterator.h"
Chris Lattnerec4e8082003-11-25 18:33:40 +000033using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000034
Chris Lattnerd501c132003-02-26 19:41:54 +000035// Make sure that anything that uses AliasAnalysis pulls in this file...
Chris Lattner86391452003-12-11 05:44:59 +000036void llvm::BasicAAStub() {}
Chris Lattnerd501c132003-02-26 19:41:54 +000037
Chris Lattnerd501c132003-02-26 19:41:54 +000038namespace {
39 struct BasicAliasAnalysis : public ImmutablePass, public AliasAnalysis {
40
41 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
42 AliasAnalysis::getAnalysisUsage(AU);
43 }
44
45 virtual void initializePass();
46
Chris Lattnerd501c132003-02-26 19:41:54 +000047 AliasResult alias(const Value *V1, unsigned V1Size,
48 const Value *V2, unsigned V2Size);
Chris Lattnerbc1daaa2004-01-30 22:17:24 +000049
Chris Lattner04b75932004-03-12 22:39:00 +000050 ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
51
Chris Lattnerbc1daaa2004-01-30 22:17:24 +000052 /// pointsToConstantMemory - Chase pointers until we find a (constant
53 /// global) or not.
54 bool pointsToConstantMemory(const Value *P);
55
Chris Lattnerd501c132003-02-26 19:41:54 +000056 private:
Chris Lattnerb307c882003-12-11 22:44:13 +000057 // CheckGEPInstructions - Check two GEP instructions with known
58 // must-aliasing base pointers. This checks to see if the index expressions
Chris Lattnerd501c132003-02-26 19:41:54 +000059 // preclude the pointers from aliasing...
Chris Lattnerb307c882003-12-11 22:44:13 +000060 AliasResult
61 CheckGEPInstructions(const Type* BasePtr1Ty, std::vector<Value*> &GEP1Ops,
62 unsigned G1Size,
63 const Type *BasePtr2Ty, std::vector<Value*> &GEP2Ops,
64 unsigned G2Size);
Chris Lattnerd501c132003-02-26 19:41:54 +000065 };
66
67 // Register this pass...
68 RegisterOpt<BasicAliasAnalysis>
69 X("basicaa", "Basic Alias Analysis (default AA impl)");
70
71 // Declare that we implement the AliasAnalysis interface
72 RegisterAnalysisGroup<AliasAnalysis, BasicAliasAnalysis, true> Y;
73} // End of anonymous namespace
74
75void BasicAliasAnalysis::initializePass() {
76 InitializeAliasAnalysis(this);
77}
78
Chris Lattnerc1820032003-09-20 03:08:47 +000079// hasUniqueAddress - Return true if the specified value points to something
80// with a unique, discernable, address.
Chris Lattnerd501c132003-02-26 19:41:54 +000081static inline bool hasUniqueAddress(const Value *V) {
Chris Lattnerc1820032003-09-20 03:08:47 +000082 return isa<GlobalValue>(V) || isa<AllocationInst>(V);
Chris Lattnerd501c132003-02-26 19:41:54 +000083}
84
Chris Lattnerc1820032003-09-20 03:08:47 +000085// getUnderlyingObject - This traverses the use chain to figure out what object
86// the specified value points to. If the value points to, or is derived from, a
87// unique object or an argument, return it.
Chris Lattnerd501c132003-02-26 19:41:54 +000088static const Value *getUnderlyingObject(const Value *V) {
89 if (!isa<PointerType>(V->getType())) return 0;
90
91 // If we are at some type of object... return it.
Chris Lattnerc1820032003-09-20 03:08:47 +000092 if (hasUniqueAddress(V) || isa<Argument>(V)) return V;
Chris Lattnerd501c132003-02-26 19:41:54 +000093
94 // Traverse through different addressing mechanisms...
95 if (const Instruction *I = dyn_cast<Instruction>(V)) {
96 if (isa<CastInst>(I) || isa<GetElementPtrInst>(I))
97 return getUnderlyingObject(I->getOperand(0));
Chris Lattner388f6692003-06-17 15:25:37 +000098 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
99 if (CE->getOpcode() == Instruction::Cast ||
100 CE->getOpcode() == Instruction::GetElementPtr)
101 return getUnderlyingObject(CE->getOperand(0));
102 } else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(V)) {
103 return CPR->getValue();
Chris Lattnerd501c132003-02-26 19:41:54 +0000104 }
105 return 0;
106}
107
Chris Lattnerb307c882003-12-11 22:44:13 +0000108static const User *isGEP(const Value *V) {
109 if (isa<GetElementPtrInst>(V) ||
110 (isa<ConstantExpr>(V) &&
111 cast<ConstantExpr>(V)->getOpcode() == Instruction::GetElementPtr))
112 return cast<User>(V);
113 return 0;
114}
Chris Lattnerd501c132003-02-26 19:41:54 +0000115
Chris Lattner4a830882003-12-11 23:20:16 +0000116static const Value *GetGEPOperands(const Value *V, std::vector<Value*> &GEPOps){
117 assert(GEPOps.empty() && "Expect empty list to populate!");
118 GEPOps.insert(GEPOps.end(), cast<User>(V)->op_begin()+1,
119 cast<User>(V)->op_end());
120
121 // Accumulate all of the chained indexes into the operand array
122 V = cast<User>(V)->getOperand(0);
123
124 while (const User *G = isGEP(V)) {
125 if (!isa<Constant>(GEPOps[0]) ||
126 !cast<Constant>(GEPOps[0])->isNullValue())
127 break; // Don't handle folding arbitrary pointer offsets yet...
128 GEPOps.erase(GEPOps.begin()); // Drop the zero index
129 GEPOps.insert(GEPOps.begin(), G->op_begin()+1, G->op_end());
130 V = G->getOperand(0);
131 }
132 return V;
133}
134
Chris Lattnerbc1daaa2004-01-30 22:17:24 +0000135/// pointsToConstantMemory - Chase pointers until we find a (constant
136/// global) or not.
137bool BasicAliasAnalysis::pointsToConstantMemory(const Value *P) {
Chris Lattnera4dd6742004-01-30 22:48:02 +0000138 if (const Value *V = getUnderlyingObject(P))
139 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
140 return GV->isConstant();
Chris Lattnerbc1daaa2004-01-30 22:17:24 +0000141 return false;
142}
Chris Lattner4a830882003-12-11 23:20:16 +0000143
Chris Lattner04b75932004-03-12 22:39:00 +0000144static bool AddressMightEscape(const Value *V) {
145 for (Value::use_const_iterator UI = V->use_begin(), E = V->use_end();
146 UI != E; ++UI) {
147 const Instruction *I = cast<Instruction>(*UI);
148 switch (I->getOpcode()) {
149 case Instruction::Load: break;
150 case Instruction::Store:
151 if (I->getOperand(0) == V)
152 return true; // Escapes if the pointer is stored.
153 break;
154 case Instruction::GetElementPtr:
155 if (AddressMightEscape(I)) return true;
156 break;
157 case Instruction::Cast:
158 if (!isa<PointerType>(I->getType()))
159 return true;
160 if (AddressMightEscape(I)) return true;
161 break;
Chris Lattner04b75932004-03-12 22:39:00 +0000162 default:
163 return true;
164 }
165 }
166 return false;
167}
168
169// getModRefInfo - Check to see if the specified callsite can clobber the
170// specified memory object. Since we only look at local properties of this
171// function, we really can't say much about this query. We do, however, use
172// simple "address taken" analysis on local objects.
173//
174AliasAnalysis::ModRefResult
175BasicAliasAnalysis::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
176 if (!isa<Constant>(P) && !isa<GlobalValue>(P))
177 if (const AllocationInst *AI =
Chris Lattner7a82ba02004-03-12 23:12:55 +0000178 dyn_cast_or_null<AllocationInst>(getUnderlyingObject(P))) {
Chris Lattner04b75932004-03-12 22:39:00 +0000179 // Okay, the pointer is to a stack allocated object. If we can prove that
180 // the pointer never "escapes", then we know the call cannot clobber it,
181 // because it simply can't get its address.
182 if (!AddressMightEscape(AI))
183 return NoModRef;
184 }
185
186 // If P points to a constant memory location, the call definitely could not
187 // modify the memory location.
188 return pointsToConstantMemory(P) ? Ref : ModRef;
189}
190
Chris Lattnerd501c132003-02-26 19:41:54 +0000191// alias - Provide a bunch of ad-hoc rules to disambiguate in common cases, such
192// as array references. Note that this function is heavily tail recursive.
193// Hopefully we have a smart C++ compiler. :)
194//
195AliasAnalysis::AliasResult
196BasicAliasAnalysis::alias(const Value *V1, unsigned V1Size,
197 const Value *V2, unsigned V2Size) {
Chris Lattnerb307c882003-12-11 22:44:13 +0000198 // Strip off any constant expression casts if they exist
199 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V1))
200 if (CE->getOpcode() == Instruction::Cast)
201 V1 = CE->getOperand(0);
202 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V2))
203 if (CE->getOpcode() == Instruction::Cast)
204 V2 = CE->getOperand(0);
205
Chris Lattnerd501c132003-02-26 19:41:54 +0000206 // Strip off constant pointer refs if they exist
207 if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(V1))
208 V1 = CPR->getValue();
209 if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(V2))
210 V2 = CPR->getValue();
211
212 // Are we checking for alias of the same value?
213 if (V1 == V2) return MustAlias;
214
215 if ((!isa<PointerType>(V1->getType()) || !isa<PointerType>(V2->getType())) &&
216 V1->getType() != Type::LongTy && V2->getType() != Type::LongTy)
217 return NoAlias; // Scalars cannot alias each other
218
219 // Strip off cast instructions...
220 if (const Instruction *I = dyn_cast<CastInst>(V1))
221 return alias(I->getOperand(0), V1Size, V2, V2Size);
222 if (const Instruction *I = dyn_cast<CastInst>(V2))
223 return alias(V1, V1Size, I->getOperand(0), V2Size);
224
225 // Figure out what objects these things are pointing to if we can...
226 const Value *O1 = getUnderlyingObject(V1);
227 const Value *O2 = getUnderlyingObject(V2);
228
Misha Brukman2f2d0652003-09-11 18:14:24 +0000229 // Pointing at a discernible object?
Chris Lattnerd501c132003-02-26 19:41:54 +0000230 if (O1 && O2) {
Chris Lattnerc1820032003-09-20 03:08:47 +0000231 if (isa<Argument>(O1)) {
232 // Incoming argument cannot alias locally allocated object!
233 if (isa<AllocationInst>(O2)) return NoAlias;
234 // Otherwise, nothing is known...
235 } else if (isa<Argument>(O2)) {
236 // Incoming argument cannot alias locally allocated object!
237 if (isa<AllocationInst>(O1)) return NoAlias;
238 // Otherwise, nothing is known...
239 } else {
240 // If they are two different objects, we know that we have no alias...
241 if (O1 != O2) return NoAlias;
242 }
Chris Lattnerd501c132003-02-26 19:41:54 +0000243
244 // If they are the same object, they we can look at the indexes. If they
245 // index off of the object is the same for both pointers, they must alias.
246 // If they are provably different, they must not alias. Otherwise, we can't
247 // tell anything.
Chris Lattnerc1820032003-09-20 03:08:47 +0000248 } else if (O1 && !isa<Argument>(O1) && isa<ConstantPointerNull>(V2)) {
Chris Lattnerd501c132003-02-26 19:41:54 +0000249 return NoAlias; // Unique values don't alias null
Chris Lattnerc1820032003-09-20 03:08:47 +0000250 } else if (O2 && !isa<Argument>(O2) && isa<ConstantPointerNull>(V1)) {
Chris Lattnerd501c132003-02-26 19:41:54 +0000251 return NoAlias; // Unique values don't alias null
252 }
253
Chris Lattnerb307c882003-12-11 22:44:13 +0000254 // If we have two gep instructions with must-alias'ing base pointers, figure
255 // out if the indexes to the GEP tell us anything about the derived pointer.
256 // Note that we also handle chains of getelementptr instructions as well as
257 // constant expression getelementptrs here.
Chris Lattnerd501c132003-02-26 19:41:54 +0000258 //
Chris Lattnerb307c882003-12-11 22:44:13 +0000259 if (isGEP(V1) && isGEP(V2)) {
260 // Drill down into the first non-gep value, to test for must-aliasing of
261 // the base pointers.
262 const Value *BasePtr1 = V1, *BasePtr2 = V2;
263 do {
264 BasePtr1 = cast<User>(BasePtr1)->getOperand(0);
265 } while (isGEP(BasePtr1) &&
266 cast<User>(BasePtr1)->getOperand(1) ==
267 Constant::getNullValue(cast<User>(BasePtr1)->getOperand(1)->getType()));
268 do {
269 BasePtr2 = cast<User>(BasePtr2)->getOperand(0);
270 } while (isGEP(BasePtr2) &&
271 cast<User>(BasePtr2)->getOperand(1) ==
272 Constant::getNullValue(cast<User>(BasePtr2)->getOperand(1)->getType()));
273
274 // Do the base pointers alias?
275 AliasResult BaseAlias = alias(BasePtr1, V1Size, BasePtr2, V2Size);
276 if (BaseAlias == NoAlias) return NoAlias;
277 if (BaseAlias == MustAlias) {
278 // If the base pointers alias each other exactly, check to see if we can
279 // figure out anything about the resultant pointers, to try to prove
280 // non-aliasing.
281
282 // Collect all of the chained GEP operands together into one simple place
Chris Lattner4a830882003-12-11 23:20:16 +0000283 std::vector<Value*> GEP1Ops, GEP2Ops;
284 BasePtr1 = GetGEPOperands(V1, GEP1Ops);
285 BasePtr2 = GetGEPOperands(V2, GEP2Ops);
Chris Lattnerb307c882003-12-11 22:44:13 +0000286
Chris Lattnerb307c882003-12-11 22:44:13 +0000287 AliasResult GAlias =
288 CheckGEPInstructions(BasePtr1->getType(), GEP1Ops, V1Size,
289 BasePtr2->getType(), GEP2Ops, V2Size);
290 if (GAlias != MayAlias)
291 return GAlias;
292 }
293 }
Chris Lattnerd501c132003-02-26 19:41:54 +0000294
295 // Check to see if these two pointers are related by a getelementptr
296 // instruction. If one pointer is a GEP with a non-zero index of the other
297 // pointer, we know they cannot alias.
298 //
Chris Lattner4a830882003-12-11 23:20:16 +0000299 if (isGEP(V2)) {
Chris Lattnerd501c132003-02-26 19:41:54 +0000300 std::swap(V1, V2);
301 std::swap(V1Size, V2Size);
302 }
303
Chris Lattnerc330ee62003-02-26 21:57:23 +0000304 if (V1Size != ~0U && V2Size != ~0U)
Chris Lattner4a830882003-12-11 23:20:16 +0000305 if (const User *GEP = isGEP(V1)) {
306 std::vector<Value*> GEPOperands;
307 const Value *BasePtr = GetGEPOperands(V1, GEPOperands);
308
309 AliasResult R = alias(BasePtr, V1Size, V2, V2Size);
Chris Lattnerc330ee62003-02-26 21:57:23 +0000310 if (R == MustAlias) {
311 // If there is at least one non-zero constant index, we know they cannot
312 // alias.
313 bool ConstantFound = false;
Chris Lattner88d3e032003-12-11 06:02:00 +0000314 bool AllZerosFound = true;
Chris Lattner4a830882003-12-11 23:20:16 +0000315 for (unsigned i = 0, e = GEPOperands.size(); i != e; ++i)
316 if (const Constant *C = dyn_cast<Constant>(GEPOperands[i])) {
Chris Lattnerc330ee62003-02-26 21:57:23 +0000317 if (!C->isNullValue()) {
318 ConstantFound = true;
Chris Lattnerc54735e2003-12-11 06:06:28 +0000319 AllZerosFound = false;
Chris Lattnerc330ee62003-02-26 21:57:23 +0000320 break;
Chris Lattner88d3e032003-12-11 06:02:00 +0000321 }
322 } else {
323 AllZerosFound = false;
Chris Lattnerc330ee62003-02-26 21:57:23 +0000324 }
Chris Lattner88d3e032003-12-11 06:02:00 +0000325
326 // If we have getelementptr <ptr>, 0, 0, 0, 0, ... and V2 must aliases
327 // the ptr, the end result is a must alias also.
328 if (AllZerosFound)
329 return MustAlias;
330
Chris Lattnerc330ee62003-02-26 21:57:23 +0000331 if (ConstantFound) {
332 if (V2Size <= 1 && V1Size <= 1) // Just pointer check?
Chris Lattnerd501c132003-02-26 19:41:54 +0000333 return NoAlias;
Chris Lattnerc330ee62003-02-26 21:57:23 +0000334
335 // Otherwise we have to check to see that the distance is more than
336 // the size of the argument... build an index vector that is equal to
337 // the arguments provided, except substitute 0's for any variable
338 // indexes we find...
Chris Lattner4a830882003-12-11 23:20:16 +0000339 for (unsigned i = 0; i != GEPOperands.size(); ++i)
340 if (!isa<Constant>(GEPOperands[i]) ||
341 isa<ConstantExpr>(GEPOperands[i]))
342 GEPOperands[i] =Constant::getNullValue(GEPOperands[i]->getType());
343 int64_t Offset = getTargetData().getIndexedOffset(BasePtr->getType(),
344 GEPOperands);
345 if (Offset >= (int64_t)V2Size || Offset <= -(int64_t)V1Size)
Chris Lattnerc330ee62003-02-26 21:57:23 +0000346 return NoAlias;
347 }
348 }
Chris Lattnerd501c132003-02-26 19:41:54 +0000349 }
Chris Lattnerc330ee62003-02-26 21:57:23 +0000350
Chris Lattnerd501c132003-02-26 19:41:54 +0000351 return MayAlias;
352}
353
Chris Lattnerb307c882003-12-11 22:44:13 +0000354/// CheckGEPInstructions - Check two GEP instructions with known must-aliasing
355/// base pointers. This checks to see if the index expressions preclude the
356/// pointers from aliasing...
357AliasAnalysis::AliasResult BasicAliasAnalysis::
358CheckGEPInstructions(const Type* BasePtr1Ty, std::vector<Value*> &GEP1Ops,
359 unsigned G1S,
360 const Type *BasePtr2Ty, std::vector<Value*> &GEP2Ops,
361 unsigned G2S) {
362 // We currently can't handle the case when the base pointers have different
363 // primitive types. Since this is uncommon anyway, we are happy being
364 // extremely conservative.
365 if (BasePtr1Ty != BasePtr2Ty)
366 return MayAlias;
367
368 const Type *GEPPointerTy = BasePtr1Ty;
369
370 // Find the (possibly empty) initial sequence of equal values... which are not
371 // necessarily constants.
372 unsigned NumGEP1Operands = GEP1Ops.size(), NumGEP2Operands = GEP2Ops.size();
373 unsigned MinOperands = std::min(NumGEP1Operands, NumGEP2Operands);
374 unsigned MaxOperands = std::max(NumGEP1Operands, NumGEP2Operands);
375 unsigned UnequalOper = 0;
376 while (UnequalOper != MinOperands &&
377 GEP1Ops[UnequalOper] == GEP2Ops[UnequalOper]) {
378 // Advance through the type as we go...
379 ++UnequalOper;
380 if (const CompositeType *CT = dyn_cast<CompositeType>(BasePtr1Ty))
381 BasePtr1Ty = CT->getTypeAtIndex(GEP1Ops[UnequalOper-1]);
382 else {
383 // If all operands equal each other, then the derived pointers must
384 // alias each other...
385 BasePtr1Ty = 0;
386 assert(UnequalOper == NumGEP1Operands && UnequalOper == NumGEP2Operands &&
387 "Ran out of type nesting, but not out of operands?");
388 return MustAlias;
Chris Lattner920bd792003-06-02 05:42:39 +0000389 }
390 }
Chris Lattner920bd792003-06-02 05:42:39 +0000391
Chris Lattnerb307c882003-12-11 22:44:13 +0000392 // If we have seen all constant operands, and run out of indexes on one of the
393 // getelementptrs, check to see if the tail of the leftover one is all zeros.
394 // If so, return mustalias.
Chris Lattner4a830882003-12-11 23:20:16 +0000395 if (UnequalOper == MinOperands) {
Chris Lattnerb307c882003-12-11 22:44:13 +0000396 if (GEP1Ops.size() < GEP2Ops.size()) std::swap(GEP1Ops, GEP2Ops);
Chris Lattnerd501c132003-02-26 19:41:54 +0000397
Chris Lattnerb307c882003-12-11 22:44:13 +0000398 bool AllAreZeros = true;
399 for (unsigned i = UnequalOper; i != MaxOperands; ++i)
400 if (!isa<Constant>(GEP1Ops[i]) ||
401 !cast<Constant>(GEP1Ops[i])->isNullValue()) {
402 AllAreZeros = false;
403 break;
404 }
405 if (AllAreZeros) return MustAlias;
406 }
407
Chris Lattnerd501c132003-02-26 19:41:54 +0000408
409 // So now we know that the indexes derived from the base pointers,
410 // which are known to alias, are different. We can still determine a
411 // no-alias result if there are differing constant pairs in the index
412 // chain. For example:
413 // A[i][0] != A[j][1] iff (&A[0][1]-&A[0][0] >= std::max(G1S, G2S))
414 //
415 unsigned SizeMax = std::max(G1S, G2S);
416 if (SizeMax == ~0U) return MayAlias; // Avoid frivolous work...
Chris Lattner920bd792003-06-02 05:42:39 +0000417
Chris Lattnerd501c132003-02-26 19:41:54 +0000418 // Scan for the first operand that is constant and unequal in the
419 // two getelemenptrs...
420 unsigned FirstConstantOper = UnequalOper;
Chris Lattnerb307c882003-12-11 22:44:13 +0000421 for (; FirstConstantOper != MinOperands; ++FirstConstantOper) {
422 const Value *G1Oper = GEP1Ops[FirstConstantOper];
423 const Value *G2Oper = GEP2Ops[FirstConstantOper];
424
Chris Lattner6eb88d42004-01-12 17:57:32 +0000425 if (G1Oper != G2Oper) // Found non-equal constant indexes...
426 if (Constant *G1OC = dyn_cast<Constant>(const_cast<Value*>(G1Oper)))
427 if (Constant *G2OC = dyn_cast<Constant>(const_cast<Value*>(G2Oper))) {
428 // Make sure they are comparable (ie, not constant expressions)...
429 // and make sure the GEP with the smaller leading constant is GEP1.
430 Constant *Compare = ConstantExpr::get(Instruction::SetGT, G1OC, G2OC);
431 if (ConstantBool *CV = dyn_cast<ConstantBool>(Compare)) {
432 if (CV->getValue()) // If they are comparable and G2 > G1
433 std::swap(GEP1Ops, GEP2Ops); // Make GEP1 < GEP2
434 break;
435 }
436 }
Chris Lattnerb307c882003-12-11 22:44:13 +0000437 BasePtr1Ty = cast<CompositeType>(BasePtr1Ty)->getTypeAtIndex(G1Oper);
Chris Lattnerd501c132003-02-26 19:41:54 +0000438 }
439
Chris Lattnerb307c882003-12-11 22:44:13 +0000440 // No shared constant operands, and we ran out of common operands. At this
441 // point, the GEP instructions have run through all of their operands, and we
442 // haven't found evidence that there are any deltas between the GEP's.
443 // However, one GEP may have more operands than the other. If this is the
444 // case, there may still be hope. This this now.
445 if (FirstConstantOper == MinOperands) {
446 // Make GEP1Ops be the longer one if there is a longer one.
447 if (GEP1Ops.size() < GEP2Ops.size())
448 std::swap(GEP1Ops, GEP2Ops);
449
450 // Is there anything to check?
451 if (GEP1Ops.size() > MinOperands) {
452 for (unsigned i = FirstConstantOper; i != MaxOperands; ++i)
453 if (isa<Constant>(GEP1Ops[i]) && !isa<ConstantExpr>(GEP1Ops[i]) &&
454 !cast<Constant>(GEP1Ops[i])->isNullValue()) {
455 // Yup, there's a constant in the tail. Set all variables to
456 // constants in the GEP instruction to make it suiteable for
457 // TargetData::getIndexedOffset.
458 for (i = 0; i != MaxOperands; ++i)
459 if (!isa<Constant>(GEP1Ops[i]) || isa<ConstantExpr>(GEP1Ops[i]))
460 GEP1Ops[i] = Constant::getNullValue(GEP1Ops[i]->getType());
461 // Okay, now get the offset. This is the relative offset for the full
462 // instruction.
463 const TargetData &TD = getTargetData();
464 int64_t Offset1 = TD.getIndexedOffset(GEPPointerTy, GEP1Ops);
465
466 // Now crop off any constants from the end...
467 GEP1Ops.resize(MinOperands);
468 int64_t Offset2 = TD.getIndexedOffset(GEPPointerTy, GEP1Ops);
469
470 // If the tail provided a bit enough offset, return noalias!
471 if ((uint64_t)(Offset2-Offset1) >= SizeMax)
472 return NoAlias;
473 }
474 }
475
476 // Couldn't find anything useful.
477 return MayAlias;
478 }
Chris Lattnerd501c132003-02-26 19:41:54 +0000479
480 // If there are non-equal constants arguments, then we can figure
481 // out a minimum known delta between the two index expressions... at
482 // this point we know that the first constant index of GEP1 is less
483 // than the first constant index of GEP2.
Chris Lattner1af55e12003-11-25 20:10:07 +0000484
Chris Lattnerb307c882003-12-11 22:44:13 +0000485 // Advance BasePtr[12]Ty over this first differing constant operand.
486 BasePtr2Ty = cast<CompositeType>(BasePtr1Ty)->getTypeAtIndex(GEP2Ops[FirstConstantOper]);
487 BasePtr1Ty = cast<CompositeType>(BasePtr1Ty)->getTypeAtIndex(GEP1Ops[FirstConstantOper]);
Chris Lattnerd501c132003-02-26 19:41:54 +0000488
Chris Lattnerb307c882003-12-11 22:44:13 +0000489 // We are going to be using TargetData::getIndexedOffset to determine the
490 // offset that each of the GEP's is reaching. To do this, we have to convert
491 // all variable references to constant references. To do this, we convert the
492 // initial equal sequence of variables into constant zeros to start with.
493 for (unsigned i = 0; i != FirstConstantOper; ++i) {
494 if (!isa<Constant>(GEP1Ops[i]) || isa<ConstantExpr>(GEP1Ops[i]) ||
495 !isa<Constant>(GEP2Ops[i]) || isa<ConstantExpr>(GEP2Ops[i])) {
496 GEP1Ops[i] = Constant::getNullValue(GEP1Ops[i]->getType());
497 GEP2Ops[i] = Constant::getNullValue(GEP2Ops[i]->getType());
498 }
499 }
500
501 // We know that GEP1Ops[FirstConstantOper] & GEP2Ops[FirstConstantOper] are ok
Chris Lattnerd501c132003-02-26 19:41:54 +0000502
503 // Loop over the rest of the operands...
Chris Lattnerb307c882003-12-11 22:44:13 +0000504 for (unsigned i = FirstConstantOper+1; i != MaxOperands; ++i) {
505 const Value *Op1 = i < GEP1Ops.size() ? GEP1Ops[i] : 0;
506 const Value *Op2 = i < GEP2Ops.size() ? GEP2Ops[i] : 0;
507 // If they are equal, use a zero index...
508 if (Op1 == Op2 && BasePtr1Ty == BasePtr2Ty) {
509 if (!isa<Constant>(Op1) || isa<ConstantExpr>(Op1))
510 GEP1Ops[i] = GEP2Ops[i] = Constant::getNullValue(Op1->getType());
511 // Otherwise, just keep the constants we have.
Chris Lattnerd501c132003-02-26 19:41:54 +0000512 } else {
Chris Lattnerb307c882003-12-11 22:44:13 +0000513 if (Op1) {
514 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
515 // If this is an array index, make sure the array element is in range.
516 if (const ArrayType *AT = dyn_cast<ArrayType>(BasePtr1Ty))
517 if (Op1C->getRawValue() >= AT->getNumElements())
518 return MayAlias; // Be conservative with out-of-range accesses
519
520 } else {
521 // GEP1 is known to produce a value less than GEP2. To be
522 // conservatively correct, we must assume the largest possible
523 // constant is used in this position. This cannot be the initial
524 // index to the GEP instructions (because we know we have at least one
525 // element before this one with the different constant arguments), so
526 // we know that the current index must be into either a struct or
527 // array. Because we know it's not constant, this cannot be a
528 // structure index. Because of this, we can calculate the maximum
529 // value possible.
530 //
531 if (const ArrayType *AT = dyn_cast<ArrayType>(BasePtr1Ty))
532 GEP1Ops[i] = ConstantSInt::get(Type::LongTy,AT->getNumElements()-1);
533 }
Chris Lattnerd501c132003-02-26 19:41:54 +0000534 }
535
Chris Lattnerb307c882003-12-11 22:44:13 +0000536 if (Op2) {
537 if (const ConstantInt *Op2C = dyn_cast<ConstantInt>(Op2)) {
538 // If this is an array index, make sure the array element is in range.
539 if (const ArrayType *AT = dyn_cast<ArrayType>(BasePtr1Ty))
540 if (Op2C->getRawValue() >= AT->getNumElements())
541 return MayAlias; // Be conservative with out-of-range accesses
542 } else { // Conservatively assume the minimum value for this index
543 GEP2Ops[i] = Constant::getNullValue(Op2->getType());
544 }
Chris Lattner920bd792003-06-02 05:42:39 +0000545 }
Chris Lattnerb307c882003-12-11 22:44:13 +0000546 }
547
548 if (BasePtr1Ty && Op1) {
549 if (const CompositeType *CT = dyn_cast<CompositeType>(BasePtr1Ty))
550 BasePtr1Ty = CT->getTypeAtIndex(GEP1Ops[i]);
551 else
552 BasePtr1Ty = 0;
553 }
554
555 if (BasePtr2Ty && Op2) {
556 if (const CompositeType *CT = dyn_cast<CompositeType>(BasePtr2Ty))
557 BasePtr2Ty = CT->getTypeAtIndex(GEP2Ops[i]);
558 else
559 BasePtr2Ty = 0;
Chris Lattnerd501c132003-02-26 19:41:54 +0000560 }
561 }
562
Chris Lattnerb307c882003-12-11 22:44:13 +0000563 int64_t Offset1 = getTargetData().getIndexedOffset(GEPPointerTy, GEP1Ops);
564 int64_t Offset2 = getTargetData().getIndexedOffset(GEPPointerTy, GEP2Ops);
Chris Lattnerd501c132003-02-26 19:41:54 +0000565 assert(Offset1 < Offset2 &&"There is at least one different constant here!");
566
Chris Lattner807b7052003-04-25 18:03:06 +0000567 if ((uint64_t)(Offset2-Offset1) >= SizeMax) {
Chris Lattnerd501c132003-02-26 19:41:54 +0000568 //std::cerr << "Determined that these two GEP's don't alias ["
569 // << SizeMax << " bytes]: \n" << *GEP1 << *GEP2;
570 return NoAlias;
571 }
572 return MayAlias;
573}
574