blob: 16bb53f63b7c7004a6c3046c16ceadd658f2a70c [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
50 /// pointsToConstantMemory - Chase pointers until we find a (constant
51 /// global) or not.
52 bool pointsToConstantMemory(const Value *P);
53
Chris Lattnerd501c132003-02-26 19:41:54 +000054 private:
Chris Lattnerb307c882003-12-11 22:44:13 +000055 // CheckGEPInstructions - Check two GEP instructions with known
56 // must-aliasing base pointers. This checks to see if the index expressions
Chris Lattnerd501c132003-02-26 19:41:54 +000057 // preclude the pointers from aliasing...
Chris Lattnerb307c882003-12-11 22:44:13 +000058 AliasResult
59 CheckGEPInstructions(const Type* BasePtr1Ty, std::vector<Value*> &GEP1Ops,
60 unsigned G1Size,
61 const Type *BasePtr2Ty, std::vector<Value*> &GEP2Ops,
62 unsigned G2Size);
Chris Lattnerd501c132003-02-26 19:41:54 +000063 };
64
65 // Register this pass...
66 RegisterOpt<BasicAliasAnalysis>
67 X("basicaa", "Basic Alias Analysis (default AA impl)");
68
69 // Declare that we implement the AliasAnalysis interface
70 RegisterAnalysisGroup<AliasAnalysis, BasicAliasAnalysis, true> Y;
71} // End of anonymous namespace
72
73void BasicAliasAnalysis::initializePass() {
74 InitializeAliasAnalysis(this);
75}
76
Chris Lattnerc1820032003-09-20 03:08:47 +000077// hasUniqueAddress - Return true if the specified value points to something
78// with a unique, discernable, address.
Chris Lattnerd501c132003-02-26 19:41:54 +000079static inline bool hasUniqueAddress(const Value *V) {
Chris Lattnerc1820032003-09-20 03:08:47 +000080 return isa<GlobalValue>(V) || isa<AllocationInst>(V);
Chris Lattnerd501c132003-02-26 19:41:54 +000081}
82
Chris Lattnerc1820032003-09-20 03:08:47 +000083// getUnderlyingObject - This traverses the use chain to figure out what object
84// the specified value points to. If the value points to, or is derived from, a
85// unique object or an argument, return it.
Chris Lattnerd501c132003-02-26 19:41:54 +000086static const Value *getUnderlyingObject(const Value *V) {
87 if (!isa<PointerType>(V->getType())) return 0;
88
89 // If we are at some type of object... return it.
Chris Lattnerc1820032003-09-20 03:08:47 +000090 if (hasUniqueAddress(V) || isa<Argument>(V)) return V;
Chris Lattnerd501c132003-02-26 19:41:54 +000091
92 // Traverse through different addressing mechanisms...
93 if (const Instruction *I = dyn_cast<Instruction>(V)) {
94 if (isa<CastInst>(I) || isa<GetElementPtrInst>(I))
95 return getUnderlyingObject(I->getOperand(0));
Chris Lattner388f6692003-06-17 15:25:37 +000096 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
97 if (CE->getOpcode() == Instruction::Cast ||
98 CE->getOpcode() == Instruction::GetElementPtr)
99 return getUnderlyingObject(CE->getOperand(0));
100 } else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(V)) {
101 return CPR->getValue();
Chris Lattnerd501c132003-02-26 19:41:54 +0000102 }
103 return 0;
104}
105
Chris Lattnerb307c882003-12-11 22:44:13 +0000106static const User *isGEP(const Value *V) {
107 if (isa<GetElementPtrInst>(V) ||
108 (isa<ConstantExpr>(V) &&
109 cast<ConstantExpr>(V)->getOpcode() == Instruction::GetElementPtr))
110 return cast<User>(V);
111 return 0;
112}
Chris Lattnerd501c132003-02-26 19:41:54 +0000113
Chris Lattner4a830882003-12-11 23:20:16 +0000114static const Value *GetGEPOperands(const Value *V, std::vector<Value*> &GEPOps){
115 assert(GEPOps.empty() && "Expect empty list to populate!");
116 GEPOps.insert(GEPOps.end(), cast<User>(V)->op_begin()+1,
117 cast<User>(V)->op_end());
118
119 // Accumulate all of the chained indexes into the operand array
120 V = cast<User>(V)->getOperand(0);
121
122 while (const User *G = isGEP(V)) {
123 if (!isa<Constant>(GEPOps[0]) ||
124 !cast<Constant>(GEPOps[0])->isNullValue())
125 break; // Don't handle folding arbitrary pointer offsets yet...
126 GEPOps.erase(GEPOps.begin()); // Drop the zero index
127 GEPOps.insert(GEPOps.begin(), G->op_begin()+1, G->op_end());
128 V = G->getOperand(0);
129 }
130 return V;
131}
132
Chris Lattnerbc1daaa2004-01-30 22:17:24 +0000133/// pointsToConstantMemory - Chase pointers until we find a (constant
134/// global) or not.
135bool BasicAliasAnalysis::pointsToConstantMemory(const Value *P) {
Chris Lattnera4dd6742004-01-30 22:48:02 +0000136 if (const Value *V = getUnderlyingObject(P))
137 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
138 return GV->isConstant();
Chris Lattnerbc1daaa2004-01-30 22:17:24 +0000139 return false;
140}
Chris Lattner4a830882003-12-11 23:20:16 +0000141
Chris Lattnerd501c132003-02-26 19:41:54 +0000142// alias - Provide a bunch of ad-hoc rules to disambiguate in common cases, such
143// as array references. Note that this function is heavily tail recursive.
144// Hopefully we have a smart C++ compiler. :)
145//
146AliasAnalysis::AliasResult
147BasicAliasAnalysis::alias(const Value *V1, unsigned V1Size,
148 const Value *V2, unsigned V2Size) {
Chris Lattnerb307c882003-12-11 22:44:13 +0000149 // Strip off any constant expression casts if they exist
150 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V1))
151 if (CE->getOpcode() == Instruction::Cast)
152 V1 = CE->getOperand(0);
153 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V2))
154 if (CE->getOpcode() == Instruction::Cast)
155 V2 = CE->getOperand(0);
156
Chris Lattnerd501c132003-02-26 19:41:54 +0000157 // Strip off constant pointer refs if they exist
158 if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(V1))
159 V1 = CPR->getValue();
160 if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(V2))
161 V2 = CPR->getValue();
162
163 // Are we checking for alias of the same value?
164 if (V1 == V2) return MustAlias;
165
166 if ((!isa<PointerType>(V1->getType()) || !isa<PointerType>(V2->getType())) &&
167 V1->getType() != Type::LongTy && V2->getType() != Type::LongTy)
168 return NoAlias; // Scalars cannot alias each other
169
170 // Strip off cast instructions...
171 if (const Instruction *I = dyn_cast<CastInst>(V1))
172 return alias(I->getOperand(0), V1Size, V2, V2Size);
173 if (const Instruction *I = dyn_cast<CastInst>(V2))
174 return alias(V1, V1Size, I->getOperand(0), V2Size);
175
176 // Figure out what objects these things are pointing to if we can...
177 const Value *O1 = getUnderlyingObject(V1);
178 const Value *O2 = getUnderlyingObject(V2);
179
Misha Brukman2f2d0652003-09-11 18:14:24 +0000180 // Pointing at a discernible object?
Chris Lattnerd501c132003-02-26 19:41:54 +0000181 if (O1 && O2) {
Chris Lattnerc1820032003-09-20 03:08:47 +0000182 if (isa<Argument>(O1)) {
183 // Incoming argument cannot alias locally allocated object!
184 if (isa<AllocationInst>(O2)) return NoAlias;
185 // Otherwise, nothing is known...
186 } else if (isa<Argument>(O2)) {
187 // Incoming argument cannot alias locally allocated object!
188 if (isa<AllocationInst>(O1)) return NoAlias;
189 // Otherwise, nothing is known...
190 } else {
191 // If they are two different objects, we know that we have no alias...
192 if (O1 != O2) return NoAlias;
193 }
Chris Lattnerd501c132003-02-26 19:41:54 +0000194
195 // If they are the same object, they we can look at the indexes. If they
196 // index off of the object is the same for both pointers, they must alias.
197 // If they are provably different, they must not alias. Otherwise, we can't
198 // tell anything.
Chris Lattnerc1820032003-09-20 03:08:47 +0000199 } else if (O1 && !isa<Argument>(O1) && isa<ConstantPointerNull>(V2)) {
Chris Lattnerd501c132003-02-26 19:41:54 +0000200 return NoAlias; // Unique values don't alias null
Chris Lattnerc1820032003-09-20 03:08:47 +0000201 } else if (O2 && !isa<Argument>(O2) && isa<ConstantPointerNull>(V1)) {
Chris Lattnerd501c132003-02-26 19:41:54 +0000202 return NoAlias; // Unique values don't alias null
203 }
204
Chris Lattnerb307c882003-12-11 22:44:13 +0000205 // If we have two gep instructions with must-alias'ing base pointers, figure
206 // out if the indexes to the GEP tell us anything about the derived pointer.
207 // Note that we also handle chains of getelementptr instructions as well as
208 // constant expression getelementptrs here.
Chris Lattnerd501c132003-02-26 19:41:54 +0000209 //
Chris Lattnerb307c882003-12-11 22:44:13 +0000210 if (isGEP(V1) && isGEP(V2)) {
211 // Drill down into the first non-gep value, to test for must-aliasing of
212 // the base pointers.
213 const Value *BasePtr1 = V1, *BasePtr2 = V2;
214 do {
215 BasePtr1 = cast<User>(BasePtr1)->getOperand(0);
216 } while (isGEP(BasePtr1) &&
217 cast<User>(BasePtr1)->getOperand(1) ==
218 Constant::getNullValue(cast<User>(BasePtr1)->getOperand(1)->getType()));
219 do {
220 BasePtr2 = cast<User>(BasePtr2)->getOperand(0);
221 } while (isGEP(BasePtr2) &&
222 cast<User>(BasePtr2)->getOperand(1) ==
223 Constant::getNullValue(cast<User>(BasePtr2)->getOperand(1)->getType()));
224
225 // Do the base pointers alias?
226 AliasResult BaseAlias = alias(BasePtr1, V1Size, BasePtr2, V2Size);
227 if (BaseAlias == NoAlias) return NoAlias;
228 if (BaseAlias == MustAlias) {
229 // If the base pointers alias each other exactly, check to see if we can
230 // figure out anything about the resultant pointers, to try to prove
231 // non-aliasing.
232
233 // Collect all of the chained GEP operands together into one simple place
Chris Lattner4a830882003-12-11 23:20:16 +0000234 std::vector<Value*> GEP1Ops, GEP2Ops;
235 BasePtr1 = GetGEPOperands(V1, GEP1Ops);
236 BasePtr2 = GetGEPOperands(V2, GEP2Ops);
Chris Lattnerb307c882003-12-11 22:44:13 +0000237
Chris Lattnerb307c882003-12-11 22:44:13 +0000238 AliasResult GAlias =
239 CheckGEPInstructions(BasePtr1->getType(), GEP1Ops, V1Size,
240 BasePtr2->getType(), GEP2Ops, V2Size);
241 if (GAlias != MayAlias)
242 return GAlias;
243 }
244 }
Chris Lattnerd501c132003-02-26 19:41:54 +0000245
246 // Check to see if these two pointers are related by a getelementptr
247 // instruction. If one pointer is a GEP with a non-zero index of the other
248 // pointer, we know they cannot alias.
249 //
Chris Lattner4a830882003-12-11 23:20:16 +0000250 if (isGEP(V2)) {
Chris Lattnerd501c132003-02-26 19:41:54 +0000251 std::swap(V1, V2);
252 std::swap(V1Size, V2Size);
253 }
254
Chris Lattnerc330ee62003-02-26 21:57:23 +0000255 if (V1Size != ~0U && V2Size != ~0U)
Chris Lattner4a830882003-12-11 23:20:16 +0000256 if (const User *GEP = isGEP(V1)) {
257 std::vector<Value*> GEPOperands;
258 const Value *BasePtr = GetGEPOperands(V1, GEPOperands);
259
260 AliasResult R = alias(BasePtr, V1Size, V2, V2Size);
Chris Lattnerc330ee62003-02-26 21:57:23 +0000261 if (R == MustAlias) {
262 // If there is at least one non-zero constant index, we know they cannot
263 // alias.
264 bool ConstantFound = false;
Chris Lattner88d3e032003-12-11 06:02:00 +0000265 bool AllZerosFound = true;
Chris Lattner4a830882003-12-11 23:20:16 +0000266 for (unsigned i = 0, e = GEPOperands.size(); i != e; ++i)
267 if (const Constant *C = dyn_cast<Constant>(GEPOperands[i])) {
Chris Lattnerc330ee62003-02-26 21:57:23 +0000268 if (!C->isNullValue()) {
269 ConstantFound = true;
Chris Lattnerc54735e2003-12-11 06:06:28 +0000270 AllZerosFound = false;
Chris Lattnerc330ee62003-02-26 21:57:23 +0000271 break;
Chris Lattner88d3e032003-12-11 06:02:00 +0000272 }
273 } else {
274 AllZerosFound = false;
Chris Lattnerc330ee62003-02-26 21:57:23 +0000275 }
Chris Lattner88d3e032003-12-11 06:02:00 +0000276
277 // If we have getelementptr <ptr>, 0, 0, 0, 0, ... and V2 must aliases
278 // the ptr, the end result is a must alias also.
279 if (AllZerosFound)
280 return MustAlias;
281
Chris Lattnerc330ee62003-02-26 21:57:23 +0000282 if (ConstantFound) {
283 if (V2Size <= 1 && V1Size <= 1) // Just pointer check?
Chris Lattnerd501c132003-02-26 19:41:54 +0000284 return NoAlias;
Chris Lattnerc330ee62003-02-26 21:57:23 +0000285
286 // Otherwise we have to check to see that the distance is more than
287 // the size of the argument... build an index vector that is equal to
288 // the arguments provided, except substitute 0's for any variable
289 // indexes we find...
Chris Lattner4a830882003-12-11 23:20:16 +0000290 for (unsigned i = 0; i != GEPOperands.size(); ++i)
291 if (!isa<Constant>(GEPOperands[i]) ||
292 isa<ConstantExpr>(GEPOperands[i]))
293 GEPOperands[i] =Constant::getNullValue(GEPOperands[i]->getType());
294 int64_t Offset = getTargetData().getIndexedOffset(BasePtr->getType(),
295 GEPOperands);
296 if (Offset >= (int64_t)V2Size || Offset <= -(int64_t)V1Size)
Chris Lattnerc330ee62003-02-26 21:57:23 +0000297 return NoAlias;
298 }
299 }
Chris Lattnerd501c132003-02-26 19:41:54 +0000300 }
Chris Lattnerc330ee62003-02-26 21:57:23 +0000301
Chris Lattnerd501c132003-02-26 19:41:54 +0000302 return MayAlias;
303}
304
Chris Lattnerb307c882003-12-11 22:44:13 +0000305/// CheckGEPInstructions - Check two GEP instructions with known must-aliasing
306/// base pointers. This checks to see if the index expressions preclude the
307/// pointers from aliasing...
308AliasAnalysis::AliasResult BasicAliasAnalysis::
309CheckGEPInstructions(const Type* BasePtr1Ty, std::vector<Value*> &GEP1Ops,
310 unsigned G1S,
311 const Type *BasePtr2Ty, std::vector<Value*> &GEP2Ops,
312 unsigned G2S) {
313 // We currently can't handle the case when the base pointers have different
314 // primitive types. Since this is uncommon anyway, we are happy being
315 // extremely conservative.
316 if (BasePtr1Ty != BasePtr2Ty)
317 return MayAlias;
318
319 const Type *GEPPointerTy = BasePtr1Ty;
320
321 // Find the (possibly empty) initial sequence of equal values... which are not
322 // necessarily constants.
323 unsigned NumGEP1Operands = GEP1Ops.size(), NumGEP2Operands = GEP2Ops.size();
324 unsigned MinOperands = std::min(NumGEP1Operands, NumGEP2Operands);
325 unsigned MaxOperands = std::max(NumGEP1Operands, NumGEP2Operands);
326 unsigned UnequalOper = 0;
327 while (UnequalOper != MinOperands &&
328 GEP1Ops[UnequalOper] == GEP2Ops[UnequalOper]) {
329 // Advance through the type as we go...
330 ++UnequalOper;
331 if (const CompositeType *CT = dyn_cast<CompositeType>(BasePtr1Ty))
332 BasePtr1Ty = CT->getTypeAtIndex(GEP1Ops[UnequalOper-1]);
333 else {
334 // If all operands equal each other, then the derived pointers must
335 // alias each other...
336 BasePtr1Ty = 0;
337 assert(UnequalOper == NumGEP1Operands && UnequalOper == NumGEP2Operands &&
338 "Ran out of type nesting, but not out of operands?");
339 return MustAlias;
Chris Lattner920bd792003-06-02 05:42:39 +0000340 }
341 }
Chris Lattner920bd792003-06-02 05:42:39 +0000342
Chris Lattnerb307c882003-12-11 22:44:13 +0000343 // If we have seen all constant operands, and run out of indexes on one of the
344 // getelementptrs, check to see if the tail of the leftover one is all zeros.
345 // If so, return mustalias.
Chris Lattner4a830882003-12-11 23:20:16 +0000346 if (UnequalOper == MinOperands) {
Chris Lattnerb307c882003-12-11 22:44:13 +0000347 if (GEP1Ops.size() < GEP2Ops.size()) std::swap(GEP1Ops, GEP2Ops);
Chris Lattnerd501c132003-02-26 19:41:54 +0000348
Chris Lattnerb307c882003-12-11 22:44:13 +0000349 bool AllAreZeros = true;
350 for (unsigned i = UnequalOper; i != MaxOperands; ++i)
351 if (!isa<Constant>(GEP1Ops[i]) ||
352 !cast<Constant>(GEP1Ops[i])->isNullValue()) {
353 AllAreZeros = false;
354 break;
355 }
356 if (AllAreZeros) return MustAlias;
357 }
358
Chris Lattnerd501c132003-02-26 19:41:54 +0000359
360 // So now we know that the indexes derived from the base pointers,
361 // which are known to alias, are different. We can still determine a
362 // no-alias result if there are differing constant pairs in the index
363 // chain. For example:
364 // A[i][0] != A[j][1] iff (&A[0][1]-&A[0][0] >= std::max(G1S, G2S))
365 //
366 unsigned SizeMax = std::max(G1S, G2S);
367 if (SizeMax == ~0U) return MayAlias; // Avoid frivolous work...
Chris Lattner920bd792003-06-02 05:42:39 +0000368
Chris Lattnerd501c132003-02-26 19:41:54 +0000369 // Scan for the first operand that is constant and unequal in the
370 // two getelemenptrs...
371 unsigned FirstConstantOper = UnequalOper;
Chris Lattnerb307c882003-12-11 22:44:13 +0000372 for (; FirstConstantOper != MinOperands; ++FirstConstantOper) {
373 const Value *G1Oper = GEP1Ops[FirstConstantOper];
374 const Value *G2Oper = GEP2Ops[FirstConstantOper];
375
Chris Lattner6eb88d42004-01-12 17:57:32 +0000376 if (G1Oper != G2Oper) // Found non-equal constant indexes...
377 if (Constant *G1OC = dyn_cast<Constant>(const_cast<Value*>(G1Oper)))
378 if (Constant *G2OC = dyn_cast<Constant>(const_cast<Value*>(G2Oper))) {
379 // Make sure they are comparable (ie, not constant expressions)...
380 // and make sure the GEP with the smaller leading constant is GEP1.
381 Constant *Compare = ConstantExpr::get(Instruction::SetGT, G1OC, G2OC);
382 if (ConstantBool *CV = dyn_cast<ConstantBool>(Compare)) {
383 if (CV->getValue()) // If they are comparable and G2 > G1
384 std::swap(GEP1Ops, GEP2Ops); // Make GEP1 < GEP2
385 break;
386 }
387 }
Chris Lattnerb307c882003-12-11 22:44:13 +0000388 BasePtr1Ty = cast<CompositeType>(BasePtr1Ty)->getTypeAtIndex(G1Oper);
Chris Lattnerd501c132003-02-26 19:41:54 +0000389 }
390
Chris Lattnerb307c882003-12-11 22:44:13 +0000391 // No shared constant operands, and we ran out of common operands. At this
392 // point, the GEP instructions have run through all of their operands, and we
393 // haven't found evidence that there are any deltas between the GEP's.
394 // However, one GEP may have more operands than the other. If this is the
395 // case, there may still be hope. This this now.
396 if (FirstConstantOper == MinOperands) {
397 // Make GEP1Ops be the longer one if there is a longer one.
398 if (GEP1Ops.size() < GEP2Ops.size())
399 std::swap(GEP1Ops, GEP2Ops);
400
401 // Is there anything to check?
402 if (GEP1Ops.size() > MinOperands) {
403 for (unsigned i = FirstConstantOper; i != MaxOperands; ++i)
404 if (isa<Constant>(GEP1Ops[i]) && !isa<ConstantExpr>(GEP1Ops[i]) &&
405 !cast<Constant>(GEP1Ops[i])->isNullValue()) {
406 // Yup, there's a constant in the tail. Set all variables to
407 // constants in the GEP instruction to make it suiteable for
408 // TargetData::getIndexedOffset.
409 for (i = 0; i != MaxOperands; ++i)
410 if (!isa<Constant>(GEP1Ops[i]) || isa<ConstantExpr>(GEP1Ops[i]))
411 GEP1Ops[i] = Constant::getNullValue(GEP1Ops[i]->getType());
412 // Okay, now get the offset. This is the relative offset for the full
413 // instruction.
414 const TargetData &TD = getTargetData();
415 int64_t Offset1 = TD.getIndexedOffset(GEPPointerTy, GEP1Ops);
416
417 // Now crop off any constants from the end...
418 GEP1Ops.resize(MinOperands);
419 int64_t Offset2 = TD.getIndexedOffset(GEPPointerTy, GEP1Ops);
420
421 // If the tail provided a bit enough offset, return noalias!
422 if ((uint64_t)(Offset2-Offset1) >= SizeMax)
423 return NoAlias;
424 }
425 }
426
427 // Couldn't find anything useful.
428 return MayAlias;
429 }
Chris Lattnerd501c132003-02-26 19:41:54 +0000430
431 // If there are non-equal constants arguments, then we can figure
432 // out a minimum known delta between the two index expressions... at
433 // this point we know that the first constant index of GEP1 is less
434 // than the first constant index of GEP2.
Chris Lattner1af55e12003-11-25 20:10:07 +0000435
Chris Lattnerb307c882003-12-11 22:44:13 +0000436 // Advance BasePtr[12]Ty over this first differing constant operand.
437 BasePtr2Ty = cast<CompositeType>(BasePtr1Ty)->getTypeAtIndex(GEP2Ops[FirstConstantOper]);
438 BasePtr1Ty = cast<CompositeType>(BasePtr1Ty)->getTypeAtIndex(GEP1Ops[FirstConstantOper]);
Chris Lattnerd501c132003-02-26 19:41:54 +0000439
Chris Lattnerb307c882003-12-11 22:44:13 +0000440 // We are going to be using TargetData::getIndexedOffset to determine the
441 // offset that each of the GEP's is reaching. To do this, we have to convert
442 // all variable references to constant references. To do this, we convert the
443 // initial equal sequence of variables into constant zeros to start with.
444 for (unsigned i = 0; i != FirstConstantOper; ++i) {
445 if (!isa<Constant>(GEP1Ops[i]) || isa<ConstantExpr>(GEP1Ops[i]) ||
446 !isa<Constant>(GEP2Ops[i]) || isa<ConstantExpr>(GEP2Ops[i])) {
447 GEP1Ops[i] = Constant::getNullValue(GEP1Ops[i]->getType());
448 GEP2Ops[i] = Constant::getNullValue(GEP2Ops[i]->getType());
449 }
450 }
451
452 // We know that GEP1Ops[FirstConstantOper] & GEP2Ops[FirstConstantOper] are ok
Chris Lattnerd501c132003-02-26 19:41:54 +0000453
454 // Loop over the rest of the operands...
Chris Lattnerb307c882003-12-11 22:44:13 +0000455 for (unsigned i = FirstConstantOper+1; i != MaxOperands; ++i) {
456 const Value *Op1 = i < GEP1Ops.size() ? GEP1Ops[i] : 0;
457 const Value *Op2 = i < GEP2Ops.size() ? GEP2Ops[i] : 0;
458 // If they are equal, use a zero index...
459 if (Op1 == Op2 && BasePtr1Ty == BasePtr2Ty) {
460 if (!isa<Constant>(Op1) || isa<ConstantExpr>(Op1))
461 GEP1Ops[i] = GEP2Ops[i] = Constant::getNullValue(Op1->getType());
462 // Otherwise, just keep the constants we have.
Chris Lattnerd501c132003-02-26 19:41:54 +0000463 } else {
Chris Lattnerb307c882003-12-11 22:44:13 +0000464 if (Op1) {
465 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
466 // If this is an array index, make sure the array element is in range.
467 if (const ArrayType *AT = dyn_cast<ArrayType>(BasePtr1Ty))
468 if (Op1C->getRawValue() >= AT->getNumElements())
469 return MayAlias; // Be conservative with out-of-range accesses
470
471 } else {
472 // GEP1 is known to produce a value less than GEP2. To be
473 // conservatively correct, we must assume the largest possible
474 // constant is used in this position. This cannot be the initial
475 // index to the GEP instructions (because we know we have at least one
476 // element before this one with the different constant arguments), so
477 // we know that the current index must be into either a struct or
478 // array. Because we know it's not constant, this cannot be a
479 // structure index. Because of this, we can calculate the maximum
480 // value possible.
481 //
482 if (const ArrayType *AT = dyn_cast<ArrayType>(BasePtr1Ty))
483 GEP1Ops[i] = ConstantSInt::get(Type::LongTy,AT->getNumElements()-1);
484 }
Chris Lattnerd501c132003-02-26 19:41:54 +0000485 }
486
Chris Lattnerb307c882003-12-11 22:44:13 +0000487 if (Op2) {
488 if (const ConstantInt *Op2C = dyn_cast<ConstantInt>(Op2)) {
489 // If this is an array index, make sure the array element is in range.
490 if (const ArrayType *AT = dyn_cast<ArrayType>(BasePtr1Ty))
491 if (Op2C->getRawValue() >= AT->getNumElements())
492 return MayAlias; // Be conservative with out-of-range accesses
493 } else { // Conservatively assume the minimum value for this index
494 GEP2Ops[i] = Constant::getNullValue(Op2->getType());
495 }
Chris Lattner920bd792003-06-02 05:42:39 +0000496 }
Chris Lattnerb307c882003-12-11 22:44:13 +0000497 }
498
499 if (BasePtr1Ty && Op1) {
500 if (const CompositeType *CT = dyn_cast<CompositeType>(BasePtr1Ty))
501 BasePtr1Ty = CT->getTypeAtIndex(GEP1Ops[i]);
502 else
503 BasePtr1Ty = 0;
504 }
505
506 if (BasePtr2Ty && Op2) {
507 if (const CompositeType *CT = dyn_cast<CompositeType>(BasePtr2Ty))
508 BasePtr2Ty = CT->getTypeAtIndex(GEP2Ops[i]);
509 else
510 BasePtr2Ty = 0;
Chris Lattnerd501c132003-02-26 19:41:54 +0000511 }
512 }
513
Chris Lattnerb307c882003-12-11 22:44:13 +0000514 int64_t Offset1 = getTargetData().getIndexedOffset(GEPPointerTy, GEP1Ops);
515 int64_t Offset2 = getTargetData().getIndexedOffset(GEPPointerTy, GEP2Ops);
Chris Lattnerd501c132003-02-26 19:41:54 +0000516 assert(Offset1 < Offset2 &&"There is at least one different constant here!");
517
Chris Lattner807b7052003-04-25 18:03:06 +0000518 if ((uint64_t)(Offset2-Offset1) >= SizeMax) {
Chris Lattnerd501c132003-02-26 19:41:54 +0000519 //std::cerr << "Determined that these two GEP's don't alias ["
520 // << SizeMax << " bytes]: \n" << *GEP1 << *GEP2;
521 return NoAlias;
522 }
523 return MayAlias;
524}
525