blob: 39f232e3e5603daee456885a3572e176e9258c12 [file] [log] [blame]
Chris Lattnera346e642003-11-25 20:11:47 +00001//===- BasicAliasAnalysis.cpp - Local Alias Analysis Impl -----------------===//
Misha Brukman01808ca2005-04-21 21:13:18 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// 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.
Misha Brukman01808ca2005-04-21 21:13:18 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerd6a2a992003-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 Cohencede1ce2005-01-08 22:01:16 +000017#include "llvm/Analysis/Passes.h"
Chris Lattnerd82256a2004-03-15 03:36:49 +000018#include "llvm/Constants.h"
19#include "llvm/DerivedTypes.h"
20#include "llvm/Function.h"
Christopher Lamb1a802012007-08-02 01:18:14 +000021#include "llvm/ParameterAttributes.h"
Chris Lattnerd82256a2004-03-15 03:36:49 +000022#include "llvm/GlobalVariable.h"
Alkis Evlogimenosfd7a2d42004-07-29 12:17:34 +000023#include "llvm/Instructions.h"
Chris Lattnerd82256a2004-03-15 03:36:49 +000024#include "llvm/Pass.h"
Chris Lattnerd6a2a992003-02-26 19:41:54 +000025#include "llvm/Target/TargetData.h"
Chris Lattnerd3182e42007-02-10 22:15:31 +000026#include "llvm/ADT/SmallVector.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000027#include "llvm/Support/Compiler.h"
Chris Lattner8111c592006-10-04 21:52:35 +000028#include "llvm/Support/GetElementPtrTypeIterator.h"
29#include "llvm/Support/ManagedStatic.h"
Alkis Evlogimenosa5c04ee2004-09-03 18:19:51 +000030#include <algorithm>
Chris Lattner35997482003-11-25 18:33:40 +000031using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000032
Chris Lattnerd6a2a992003-02-26 19:41:54 +000033namespace {
Chris Lattner59c8ed82004-05-23 21:15:12 +000034 /// NoAA - This class implements the -no-aa pass, which always returns "I
35 /// don't know" for alias queries. NoAA is unlike other alias analysis
36 /// implementations, in that it does not chain to a previous analysis. As
37 /// such it doesn't follow many of the rules that other alias analyses must.
38 ///
Chris Lattner996795b2006-06-28 23:17:24 +000039 struct VISIBILITY_HIDDEN NoAA : public ImmutablePass, public AliasAnalysis {
Devang Patel8c78a0b2007-05-03 01:11:54 +000040 static char ID; // Class identification, replacement for typeinfo
Devang Patel09f162c2007-05-01 21:15:47 +000041 NoAA() : ImmutablePass((intptr_t)&ID) {}
Dan Gohman1eb8ed42007-07-02 14:53:37 +000042 explicit NoAA(intptr_t PID) : ImmutablePass(PID) { }
Devang Patel09f162c2007-05-01 21:15:47 +000043
Chris Lattnerfeda9d02004-06-19 08:05:58 +000044 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
45 AU.addRequired<TargetData>();
46 }
Misha Brukman01808ca2005-04-21 21:13:18 +000047
Chris Lattnerfeda9d02004-06-19 08:05:58 +000048 virtual void initializePass() {
49 TD = &getAnalysis<TargetData>();
50 }
51
Chris Lattner59c8ed82004-05-23 21:15:12 +000052 virtual AliasResult alias(const Value *V1, unsigned V1Size,
53 const Value *V2, unsigned V2Size) {
54 return MayAlias;
55 }
56
Chris Lattner869d6a42004-12-15 17:13:24 +000057 virtual ModRefBehavior getModRefBehavior(Function *F, CallSite CS,
58 std::vector<PointerAccessInfo> *Info) {
Chris Lattner71d04bc2004-12-15 07:22:13 +000059 return UnknownModRefBehavior;
60 }
Misha Brukman01808ca2005-04-21 21:13:18 +000061
Chris Lattner71d04bc2004-12-15 07:22:13 +000062 virtual void getArgumentAccesses(Function *F, CallSite CS,
63 std::vector<PointerAccessInfo> &Info) {
64 assert(0 && "This method may not be called on this function!");
65 }
66
Chris Lattner59c8ed82004-05-23 21:15:12 +000067 virtual void getMustAliases(Value *P, std::vector<Value*> &RetVals) { }
68 virtual bool pointsToConstantMemory(const Value *P) { return false; }
Chris Lattner59c8ed82004-05-23 21:15:12 +000069 virtual ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size) {
70 return ModRef;
71 }
72 virtual ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) {
73 return ModRef;
74 }
75 virtual bool hasNoModRefInfoForCalls() const { return true; }
76
77 virtual void deleteValue(Value *V) {}
78 virtual void copyValue(Value *From, Value *To) {}
Chris Lattner59c8ed82004-05-23 21:15:12 +000079 };
Misha Brukman01808ca2005-04-21 21:13:18 +000080
Chris Lattner59c8ed82004-05-23 21:15:12 +000081 // Register this pass...
Devang Patel8c78a0b2007-05-03 01:11:54 +000082 char NoAA::ID = 0;
Chris Lattnerc2d3d312006-08-27 22:42:52 +000083 RegisterPass<NoAA>
Chris Lattner59c8ed82004-05-23 21:15:12 +000084 U("no-aa", "No Alias Analysis (always returns 'may' alias)");
85
86 // Declare that we implement the AliasAnalysis interface
Chris Lattner97c9f202006-08-28 00:42:29 +000087 RegisterAnalysisGroup<AliasAnalysis> V(U);
Chris Lattner59c8ed82004-05-23 21:15:12 +000088} // End of anonymous namespace
89
Jeff Cohencede1ce2005-01-08 22:01:16 +000090ImmutablePass *llvm::createNoAAPass() { return new NoAA(); }
Chris Lattner59c8ed82004-05-23 21:15:12 +000091
92namespace {
93 /// BasicAliasAnalysis - This is the default alias analysis implementation.
94 /// Because it doesn't chain to a previous alias analysis (like -no-aa), it
95 /// derives from the NoAA class.
Chris Lattner996795b2006-06-28 23:17:24 +000096 struct VISIBILITY_HIDDEN BasicAliasAnalysis : public NoAA {
Devang Patel8c78a0b2007-05-03 01:11:54 +000097 static char ID; // Class identification, replacement for typeinfo
Anton Korobeynikov02b09972007-06-18 17:13:29 +000098 BasicAliasAnalysis() : NoAA((intptr_t)&ID) { }
Chris Lattnerd6a2a992003-02-26 19:41:54 +000099 AliasResult alias(const Value *V1, unsigned V1Size,
100 const Value *V2, unsigned V2Size);
Chris Lattnerf0eac5d2004-01-30 22:17:24 +0000101
Chris Lattnera0362532004-03-12 22:39:00 +0000102 ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
Reid Spencer5c132bc2004-12-07 08:11:24 +0000103 ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) {
104 return NoAA::getModRefInfo(CS1,CS2);
105 }
Chris Lattnera0362532004-03-12 22:39:00 +0000106
Chris Lattner6b570262004-07-27 02:13:55 +0000107 /// hasNoModRefInfoForCalls - We can provide mod/ref information against
108 /// non-escaping allocations.
109 virtual bool hasNoModRefInfoForCalls() const { return false; }
Chris Lattnerc5fad352004-04-11 16:43:07 +0000110
Chris Lattnerf0eac5d2004-01-30 22:17:24 +0000111 /// pointsToConstantMemory - Chase pointers until we find a (constant
112 /// global) or not.
113 bool pointsToConstantMemory(const Value *P);
114
Chris Lattner71d04bc2004-12-15 07:22:13 +0000115 virtual ModRefBehavior getModRefBehavior(Function *F, CallSite CS,
Chris Lattner875d7b22007-01-14 05:57:53 +0000116 std::vector<PointerAccessInfo> *Info);
Misha Brukman01808ca2005-04-21 21:13:18 +0000117
Chris Lattnerd6a2a992003-02-26 19:41:54 +0000118 private:
Chris Lattner6ea17f77f2003-12-11 22:44:13 +0000119 // CheckGEPInstructions - Check two GEP instructions with known
120 // must-aliasing base pointers. This checks to see if the index expressions
Chris Lattnerd6a2a992003-02-26 19:41:54 +0000121 // preclude the pointers from aliasing...
Chris Lattner6ea17f77f2003-12-11 22:44:13 +0000122 AliasResult
Chris Lattner237b5b62007-02-10 22:12:53 +0000123 CheckGEPInstructions(const Type* BasePtr1Ty,
124 Value **GEP1Ops, unsigned NumGEP1Ops, unsigned G1Size,
125 const Type *BasePtr2Ty,
126 Value **GEP2Ops, unsigned NumGEP2Ops, unsigned G2Size);
Chris Lattnerd6a2a992003-02-26 19:41:54 +0000127 };
Misha Brukman01808ca2005-04-21 21:13:18 +0000128
Chris Lattnerd6a2a992003-02-26 19:41:54 +0000129 // Register this pass...
Devang Patel8c78a0b2007-05-03 01:11:54 +0000130 char BasicAliasAnalysis::ID = 0;
Chris Lattnerc2d3d312006-08-27 22:42:52 +0000131 RegisterPass<BasicAliasAnalysis>
Chris Lattnerd6a2a992003-02-26 19:41:54 +0000132 X("basicaa", "Basic Alias Analysis (default AA impl)");
133
134 // Declare that we implement the AliasAnalysis interface
Chris Lattner97c9f202006-08-28 00:42:29 +0000135 RegisterAnalysisGroup<AliasAnalysis, true> Y(X);
Chris Lattnerd6a2a992003-02-26 19:41:54 +0000136} // End of anonymous namespace
137
Jeff Cohencede1ce2005-01-08 22:01:16 +0000138ImmutablePass *llvm::createBasicAliasAnalysisPass() {
139 return new BasicAliasAnalysis();
140}
141
Chris Lattner092af3f2003-09-20 03:08:47 +0000142// getUnderlyingObject - This traverses the use chain to figure out what object
143// the specified value points to. If the value points to, or is derived from, a
144// unique object or an argument, return it.
Chris Lattnerd6a2a992003-02-26 19:41:54 +0000145static const Value *getUnderlyingObject(const Value *V) {
146 if (!isa<PointerType>(V->getType())) return 0;
147
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000148 // If we are at some type of object, return it. GlobalValues and Allocations
149 // have unique addresses.
150 if (isa<GlobalValue>(V) || isa<AllocationInst>(V) || isa<Argument>(V))
151 return V;
Misha Brukman01808ca2005-04-21 21:13:18 +0000152
Chris Lattnerd6a2a992003-02-26 19:41:54 +0000153 // Traverse through different addressing mechanisms...
154 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000155 if (isa<BitCastInst>(I) || isa<GetElementPtrInst>(I))
Chris Lattnerd6a2a992003-02-26 19:41:54 +0000156 return getUnderlyingObject(I->getOperand(0));
Chris Lattner1bec75e2003-06-17 15:25:37 +0000157 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000158 if (CE->getOpcode() == Instruction::BitCast ||
Chris Lattner1bec75e2003-06-17 15:25:37 +0000159 CE->getOpcode() == Instruction::GetElementPtr)
160 return getUnderlyingObject(CE->getOperand(0));
Chris Lattnerd6a2a992003-02-26 19:41:54 +0000161 }
162 return 0;
163}
164
Chris Lattner6ea17f77f2003-12-11 22:44:13 +0000165static const User *isGEP(const Value *V) {
166 if (isa<GetElementPtrInst>(V) ||
167 (isa<ConstantExpr>(V) &&
168 cast<ConstantExpr>(V)->getOpcode() == Instruction::GetElementPtr))
169 return cast<User>(V);
170 return 0;
171}
Chris Lattnerd6a2a992003-02-26 19:41:54 +0000172
Chris Lattnerd3182e42007-02-10 22:15:31 +0000173static const Value *GetGEPOperands(const Value *V,
174 SmallVector<Value*, 16> &GEPOps){
Chris Lattner1eed55d2003-12-11 23:20:16 +0000175 assert(GEPOps.empty() && "Expect empty list to populate!");
176 GEPOps.insert(GEPOps.end(), cast<User>(V)->op_begin()+1,
177 cast<User>(V)->op_end());
178
179 // Accumulate all of the chained indexes into the operand array
180 V = cast<User>(V)->getOperand(0);
181
182 while (const User *G = isGEP(V)) {
Reid Spencer30d69a52004-07-18 00:18:30 +0000183 if (!isa<Constant>(GEPOps[0]) || isa<GlobalValue>(GEPOps[0]) ||
Chris Lattner1eed55d2003-12-11 23:20:16 +0000184 !cast<Constant>(GEPOps[0])->isNullValue())
185 break; // Don't handle folding arbitrary pointer offsets yet...
186 GEPOps.erase(GEPOps.begin()); // Drop the zero index
187 GEPOps.insert(GEPOps.begin(), G->op_begin()+1, G->op_end());
188 V = G->getOperand(0);
189 }
190 return V;
191}
192
Chris Lattnerf0eac5d2004-01-30 22:17:24 +0000193/// pointsToConstantMemory - Chase pointers until we find a (constant
194/// global) or not.
195bool BasicAliasAnalysis::pointsToConstantMemory(const Value *P) {
Chris Lattner729ea9e2004-01-30 22:48:02 +0000196 if (const Value *V = getUnderlyingObject(P))
197 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
198 return GV->isConstant();
Chris Lattnerf0eac5d2004-01-30 22:17:24 +0000199 return false;
200}
Chris Lattner1eed55d2003-12-11 23:20:16 +0000201
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000202// Determine if an AllocationInst instruction escapes from the function it is
203// contained in. If it does not escape, there is no way for another function to
204// mod/ref it. We do this by looking at its uses and determining if the uses
205// can escape (recursively).
Chris Lattnera0362532004-03-12 22:39:00 +0000206static bool AddressMightEscape(const Value *V) {
207 for (Value::use_const_iterator UI = V->use_begin(), E = V->use_end();
208 UI != E; ++UI) {
209 const Instruction *I = cast<Instruction>(*UI);
210 switch (I->getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000211 case Instruction::Load:
212 break; //next use.
Chris Lattnera0362532004-03-12 22:39:00 +0000213 case Instruction::Store:
214 if (I->getOperand(0) == V)
215 return true; // Escapes if the pointer is stored.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000216 break; // next use.
Chris Lattnera0362532004-03-12 22:39:00 +0000217 case Instruction::GetElementPtr:
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000218 if (AddressMightEscape(I))
219 return true;
220 case Instruction::BitCast:
Chris Lattnera0362532004-03-12 22:39:00 +0000221 if (!isa<PointerType>(I->getType()))
222 return true;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000223 if (AddressMightEscape(I))
224 return true;
225 break; // next use
Chris Lattneraa05a6e2004-07-27 02:18:52 +0000226 case Instruction::Ret:
227 // If returned, the address will escape to calling functions, but no
228 // callees could modify it.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000229 break; // next use
Chris Lattnera0362532004-03-12 22:39:00 +0000230 default:
231 return true;
232 }
233 }
234 return false;
235}
236
237// getModRefInfo - Check to see if the specified callsite can clobber the
238// specified memory object. Since we only look at local properties of this
239// function, we really can't say much about this query. We do, however, use
240// simple "address taken" analysis on local objects.
241//
242AliasAnalysis::ModRefResult
243BasicAliasAnalysis::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
Reid Spencer30d69a52004-07-18 00:18:30 +0000244 if (!isa<Constant>(P))
Chris Lattnera0362532004-03-12 22:39:00 +0000245 if (const AllocationInst *AI =
Chris Lattnerf9e69b42004-03-12 23:12:55 +0000246 dyn_cast_or_null<AllocationInst>(getUnderlyingObject(P))) {
Chris Lattnera0362532004-03-12 22:39:00 +0000247 // Okay, the pointer is to a stack allocated object. If we can prove that
248 // the pointer never "escapes", then we know the call cannot clobber it,
249 // because it simply can't get its address.
250 if (!AddressMightEscape(AI))
251 return NoModRef;
Chris Lattnerbb0bfc42005-05-08 23:58:12 +0000252
253 // If this is a tail call and P points to a stack location, we know that
254 // the tail call cannot access or modify the local stack.
255 if (CallInst *CI = dyn_cast<CallInst>(CS.getInstruction()))
256 if (CI->isTailCall() && isa<AllocaInst>(AI))
257 return NoModRef;
Chris Lattnera0362532004-03-12 22:39:00 +0000258 }
259
Chris Lattnerea42c852004-03-15 04:18:28 +0000260 // The AliasAnalysis base class has some smarts, lets use them.
261 return AliasAnalysis::getModRefInfo(CS, P, Size);
Chris Lattnera0362532004-03-12 22:39:00 +0000262}
263
Christopher Lamb1a802012007-08-02 01:18:14 +0000264static bool isNoAliasArgument(const Argument *Arg) {
265 const Function *Func = Arg->getParent();
266 const ParamAttrsList *Attr = Func->getFunctionType()->getParamAttrs();
267 if (Attr) {
268 unsigned Idx = 1;
269 for (Function::const_arg_iterator I = Func->arg_begin(),
270 E = Func->arg_end(); I != E; ++I, ++Idx) {
271 if (&(*I) == Arg &&
272 Attr->paramHasAttr(Idx, ParamAttr::NoAlias))
273 return true;
274 }
275 }
276 return false;
277}
278
Chris Lattnerd6a2a992003-02-26 19:41:54 +0000279// alias - Provide a bunch of ad-hoc rules to disambiguate in common cases, such
280// as array references. Note that this function is heavily tail recursive.
281// Hopefully we have a smart C++ compiler. :)
282//
283AliasAnalysis::AliasResult
284BasicAliasAnalysis::alias(const Value *V1, unsigned V1Size,
285 const Value *V2, unsigned V2Size) {
Chris Lattner6ea17f77f2003-12-11 22:44:13 +0000286 // Strip off any constant expression casts if they exist
287 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V1))
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000288 if (CE->isCast() && isa<PointerType>(CE->getOperand(0)->getType()))
Chris Lattner6ea17f77f2003-12-11 22:44:13 +0000289 V1 = CE->getOperand(0);
290 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V2))
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000291 if (CE->isCast() && isa<PointerType>(CE->getOperand(0)->getType()))
Chris Lattner6ea17f77f2003-12-11 22:44:13 +0000292 V2 = CE->getOperand(0);
293
Chris Lattnerd6a2a992003-02-26 19:41:54 +0000294 // Are we checking for alias of the same value?
295 if (V1 == V2) return MustAlias;
296
297 if ((!isa<PointerType>(V1->getType()) || !isa<PointerType>(V2->getType())) &&
Reid Spencerc635f472006-12-31 05:48:39 +0000298 V1->getType() != Type::Int64Ty && V2->getType() != Type::Int64Ty)
Chris Lattnerd6a2a992003-02-26 19:41:54 +0000299 return NoAlias; // Scalars cannot alias each other
300
301 // Strip off cast instructions...
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000302 if (const BitCastInst *I = dyn_cast<BitCastInst>(V1))
Chris Lattner875d7b22007-01-14 05:57:53 +0000303 return alias(I->getOperand(0), V1Size, V2, V2Size);
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000304 if (const BitCastInst *I = dyn_cast<BitCastInst>(V2))
Chris Lattner875d7b22007-01-14 05:57:53 +0000305 return alias(V1, V1Size, I->getOperand(0), V2Size);
Chris Lattnerd6a2a992003-02-26 19:41:54 +0000306
307 // Figure out what objects these things are pointing to if we can...
308 const Value *O1 = getUnderlyingObject(V1);
309 const Value *O2 = getUnderlyingObject(V2);
310
Misha Brukman32998322003-09-11 18:14:24 +0000311 // Pointing at a discernible object?
Chris Lattner75819a82004-11-26 19:20:01 +0000312 if (O1) {
313 if (O2) {
314 if (isa<Argument>(O1)) {
315 // Incoming argument cannot alias locally allocated object!
316 if (isa<AllocationInst>(O2)) return NoAlias;
Christopher Lamb1a802012007-08-02 01:18:14 +0000317
318 // If they are two different objects, and one is a noalias argument
319 // then they do not alias.
320 if (O1 != O2 && isNoAliasArgument(cast<Argument>(O1)))
321 return NoAlias;
322
Chris Lattner75819a82004-11-26 19:20:01 +0000323 // Otherwise, nothing is known...
Christopher Lamb1a802012007-08-02 01:18:14 +0000324 }
325
326 if (isa<Argument>(O2)) {
Chris Lattner75819a82004-11-26 19:20:01 +0000327 // Incoming argument cannot alias locally allocated object!
328 if (isa<AllocationInst>(O1)) return NoAlias;
Christopher Lamb1a802012007-08-02 01:18:14 +0000329
330 // If they are two different objects, and one is a noalias argument
331 // then they do not alias.
332 if (O1 != O2 && isNoAliasArgument(cast<Argument>(O2)))
333 return NoAlias;
334
Chris Lattner75819a82004-11-26 19:20:01 +0000335 // Otherwise, nothing is known...
336 } else if (O1 != O2) {
337 // If they are two different objects, we know that we have no alias...
338 return NoAlias;
339 }
Christopher Lamb28315242007-07-31 16:18:07 +0000340
Chris Lattner75819a82004-11-26 19:20:01 +0000341 // If they are the same object, they we can look at the indexes. If they
342 // index off of the object is the same for both pointers, they must alias.
343 // If they are provably different, they must not alias. Otherwise, we
344 // can't tell anything.
Chris Lattner092af3f2003-09-20 03:08:47 +0000345 }
Chris Lattnerd6a2a992003-02-26 19:41:54 +0000346
Chris Lattner75819a82004-11-26 19:20:01 +0000347
348 if (!isa<Argument>(O1) && isa<ConstantPointerNull>(V2))
349 return NoAlias; // Unique values don't alias null
350
Misha Brukman01808ca2005-04-21 21:13:18 +0000351 if (isa<GlobalVariable>(O1) ||
Chris Lattner562c1802005-03-09 16:29:52 +0000352 (isa<AllocationInst>(O1) &&
353 !cast<AllocationInst>(O1)->isArrayAllocation()))
Chris Lattner63b45b42004-11-26 20:01:48 +0000354 if (cast<PointerType>(O1->getType())->getElementType()->isSized()) {
Chris Lattner75819a82004-11-26 19:20:01 +0000355 // If the size of the other access is larger than the total size of the
Chris Lattner63b45b42004-11-26 20:01:48 +0000356 // global/alloca/malloc, it cannot be accessing the global (it's
357 // undefined to load or store bytes before or after an object).
358 const Type *ElTy = cast<PointerType>(O1->getType())->getElementType();
359 unsigned GlobalSize = getTargetData().getTypeSize(ElTy);
Chris Lattner1b784b12004-11-28 20:30:15 +0000360 if (GlobalSize < V2Size && V2Size != ~0U)
Chris Lattner75819a82004-11-26 19:20:01 +0000361 return NoAlias;
362 }
363 }
364
365 if (O2) {
366 if (!isa<Argument>(O2) && isa<ConstantPointerNull>(V1))
367 return NoAlias; // Unique values don't alias null
368
Chris Lattner562c1802005-03-09 16:29:52 +0000369 if (isa<GlobalVariable>(O2) ||
370 (isa<AllocationInst>(O2) &&
371 !cast<AllocationInst>(O2)->isArrayAllocation()))
Chris Lattner63b45b42004-11-26 20:01:48 +0000372 if (cast<PointerType>(O2->getType())->getElementType()->isSized()) {
Chris Lattner75819a82004-11-26 19:20:01 +0000373 // If the size of the other access is larger than the total size of the
Chris Lattner63b45b42004-11-26 20:01:48 +0000374 // global/alloca/malloc, it cannot be accessing the object (it's
375 // undefined to load or store bytes before or after an object).
376 const Type *ElTy = cast<PointerType>(O2->getType())->getElementType();
377 unsigned GlobalSize = getTargetData().getTypeSize(ElTy);
Chris Lattner1b784b12004-11-28 20:30:15 +0000378 if (GlobalSize < V1Size && V1Size != ~0U)
Chris Lattner75819a82004-11-26 19:20:01 +0000379 return NoAlias;
380 }
Chris Lattnerd6a2a992003-02-26 19:41:54 +0000381 }
382
Chris Lattner6ea17f77f2003-12-11 22:44:13 +0000383 // If we have two gep instructions with must-alias'ing base pointers, figure
384 // out if the indexes to the GEP tell us anything about the derived pointer.
385 // Note that we also handle chains of getelementptr instructions as well as
386 // constant expression getelementptrs here.
Chris Lattnerd6a2a992003-02-26 19:41:54 +0000387 //
Chris Lattner6ea17f77f2003-12-11 22:44:13 +0000388 if (isGEP(V1) && isGEP(V2)) {
389 // Drill down into the first non-gep value, to test for must-aliasing of
390 // the base pointers.
391 const Value *BasePtr1 = V1, *BasePtr2 = V2;
392 do {
393 BasePtr1 = cast<User>(BasePtr1)->getOperand(0);
394 } while (isGEP(BasePtr1) &&
Misha Brukman01808ca2005-04-21 21:13:18 +0000395 cast<User>(BasePtr1)->getOperand(1) ==
Chris Lattner6ea17f77f2003-12-11 22:44:13 +0000396 Constant::getNullValue(cast<User>(BasePtr1)->getOperand(1)->getType()));
397 do {
398 BasePtr2 = cast<User>(BasePtr2)->getOperand(0);
399 } while (isGEP(BasePtr2) &&
Misha Brukman01808ca2005-04-21 21:13:18 +0000400 cast<User>(BasePtr2)->getOperand(1) ==
Chris Lattner6ea17f77f2003-12-11 22:44:13 +0000401 Constant::getNullValue(cast<User>(BasePtr2)->getOperand(1)->getType()));
402
403 // Do the base pointers alias?
Chris Lattner875d7b22007-01-14 05:57:53 +0000404 AliasResult BaseAlias = alias(BasePtr1, ~0U, BasePtr2, ~0U);
Chris Lattner6ea17f77f2003-12-11 22:44:13 +0000405 if (BaseAlias == NoAlias) return NoAlias;
406 if (BaseAlias == MustAlias) {
407 // If the base pointers alias each other exactly, check to see if we can
408 // figure out anything about the resultant pointers, to try to prove
409 // non-aliasing.
410
411 // Collect all of the chained GEP operands together into one simple place
Chris Lattnerd3182e42007-02-10 22:15:31 +0000412 SmallVector<Value*, 16> GEP1Ops, GEP2Ops;
Chris Lattner1eed55d2003-12-11 23:20:16 +0000413 BasePtr1 = GetGEPOperands(V1, GEP1Ops);
414 BasePtr2 = GetGEPOperands(V2, GEP2Ops);
Chris Lattner6ea17f77f2003-12-11 22:44:13 +0000415
Chris Lattnerc21acbf2004-07-29 07:56:39 +0000416 // If GetGEPOperands were able to fold to the same must-aliased pointer,
417 // do the comparison.
418 if (BasePtr1 == BasePtr2) {
419 AliasResult GAlias =
Chris Lattner237b5b62007-02-10 22:12:53 +0000420 CheckGEPInstructions(BasePtr1->getType(),
421 &GEP1Ops[0], GEP1Ops.size(), V1Size,
422 BasePtr2->getType(),
423 &GEP2Ops[0], GEP2Ops.size(), V2Size);
Chris Lattnerc21acbf2004-07-29 07:56:39 +0000424 if (GAlias != MayAlias)
425 return GAlias;
426 }
Chris Lattner6ea17f77f2003-12-11 22:44:13 +0000427 }
428 }
Chris Lattnerd6a2a992003-02-26 19:41:54 +0000429
430 // Check to see if these two pointers are related by a getelementptr
431 // instruction. If one pointer is a GEP with a non-zero index of the other
432 // pointer, we know they cannot alias.
433 //
Chris Lattner1eed55d2003-12-11 23:20:16 +0000434 if (isGEP(V2)) {
Chris Lattnerd6a2a992003-02-26 19:41:54 +0000435 std::swap(V1, V2);
436 std::swap(V1Size, V2Size);
437 }
438
Chris Lattner053994f2003-02-26 21:57:23 +0000439 if (V1Size != ~0U && V2Size != ~0U)
Reid Spencerde46e482006-11-02 20:25:50 +0000440 if (isGEP(V1)) {
Chris Lattnerd3182e42007-02-10 22:15:31 +0000441 SmallVector<Value*, 16> GEPOperands;
Chris Lattner1eed55d2003-12-11 23:20:16 +0000442 const Value *BasePtr = GetGEPOperands(V1, GEPOperands);
443
444 AliasResult R = alias(BasePtr, V1Size, V2, V2Size);
Chris Lattner053994f2003-02-26 21:57:23 +0000445 if (R == MustAlias) {
446 // If there is at least one non-zero constant index, we know they cannot
447 // alias.
448 bool ConstantFound = false;
Chris Lattnerbc1197f2003-12-11 06:02:00 +0000449 bool AllZerosFound = true;
Chris Lattner1eed55d2003-12-11 23:20:16 +0000450 for (unsigned i = 0, e = GEPOperands.size(); i != e; ++i)
451 if (const Constant *C = dyn_cast<Constant>(GEPOperands[i])) {
Chris Lattner053994f2003-02-26 21:57:23 +0000452 if (!C->isNullValue()) {
453 ConstantFound = true;
Chris Lattner17790fb2003-12-11 06:06:28 +0000454 AllZerosFound = false;
Chris Lattner053994f2003-02-26 21:57:23 +0000455 break;
Chris Lattnerbc1197f2003-12-11 06:02:00 +0000456 }
457 } else {
458 AllZerosFound = false;
Chris Lattner053994f2003-02-26 21:57:23 +0000459 }
Chris Lattnerbc1197f2003-12-11 06:02:00 +0000460
461 // If we have getelementptr <ptr>, 0, 0, 0, 0, ... and V2 must aliases
462 // the ptr, the end result is a must alias also.
463 if (AllZerosFound)
464 return MustAlias;
465
Chris Lattner053994f2003-02-26 21:57:23 +0000466 if (ConstantFound) {
467 if (V2Size <= 1 && V1Size <= 1) // Just pointer check?
Chris Lattnerd6a2a992003-02-26 19:41:54 +0000468 return NoAlias;
Misha Brukman01808ca2005-04-21 21:13:18 +0000469
Chris Lattner053994f2003-02-26 21:57:23 +0000470 // Otherwise we have to check to see that the distance is more than
471 // the size of the argument... build an index vector that is equal to
472 // the arguments provided, except substitute 0's for any variable
473 // indexes we find...
Alkis Evlogimenosb1ff6d72004-12-08 23:42:11 +0000474 if (cast<PointerType>(
475 BasePtr->getType())->getElementType()->isSized()) {
476 for (unsigned i = 0; i != GEPOperands.size(); ++i)
Chris Lattner0fd2b9f2007-01-12 18:20:48 +0000477 if (!isa<ConstantInt>(GEPOperands[i]))
Alkis Evlogimenosb1ff6d72004-12-08 23:42:11 +0000478 GEPOperands[i] =
479 Constant::getNullValue(GEPOperands[i]->getType());
480 int64_t Offset =
Chris Lattnerc44bd782007-02-10 20:35:22 +0000481 getTargetData().getIndexedOffset(BasePtr->getType(),
482 &GEPOperands[0],
483 GEPOperands.size());
Alkis Evlogimenosb1ff6d72004-12-08 23:42:11 +0000484
485 if (Offset >= (int64_t)V2Size || Offset <= -(int64_t)V1Size)
486 return NoAlias;
487 }
Chris Lattner053994f2003-02-26 21:57:23 +0000488 }
489 }
Chris Lattnerd6a2a992003-02-26 19:41:54 +0000490 }
Misha Brukman01808ca2005-04-21 21:13:18 +0000491
Chris Lattnerd6a2a992003-02-26 19:41:54 +0000492 return MayAlias;
493}
494
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000495// This function is used to determin if the indices of two GEP instructions are
496// equal. V1 and V2 are the indices.
497static bool IndexOperandsEqual(Value *V1, Value *V2) {
Chris Lattner69193f92004-04-05 01:30:19 +0000498 if (V1->getType() == V2->getType())
499 return V1 == V2;
500 if (Constant *C1 = dyn_cast<Constant>(V1))
501 if (Constant *C2 = dyn_cast<Constant>(V2)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000502 // Sign extend the constants to long types, if necessary
Reid Spencerc635f472006-12-31 05:48:39 +0000503 if (C1->getType() != Type::Int64Ty)
504 C1 = ConstantExpr::getSExt(C1, Type::Int64Ty);
505 if (C2->getType() != Type::Int64Ty)
506 C2 = ConstantExpr::getSExt(C2, Type::Int64Ty);
Chris Lattner69193f92004-04-05 01:30:19 +0000507 return C1 == C2;
508 }
509 return false;
510}
511
Chris Lattner6ea17f77f2003-12-11 22:44:13 +0000512/// CheckGEPInstructions - Check two GEP instructions with known must-aliasing
513/// base pointers. This checks to see if the index expressions preclude the
514/// pointers from aliasing...
Reid Spencere0fc4df2006-10-20 07:07:24 +0000515AliasAnalysis::AliasResult
516BasicAliasAnalysis::CheckGEPInstructions(
Chris Lattner237b5b62007-02-10 22:12:53 +0000517 const Type* BasePtr1Ty, Value **GEP1Ops, unsigned NumGEP1Ops, unsigned G1S,
518 const Type *BasePtr2Ty, Value **GEP2Ops, unsigned NumGEP2Ops, unsigned G2S) {
Chris Lattner6ea17f77f2003-12-11 22:44:13 +0000519 // We currently can't handle the case when the base pointers have different
520 // primitive types. Since this is uncommon anyway, we are happy being
521 // extremely conservative.
522 if (BasePtr1Ty != BasePtr2Ty)
523 return MayAlias;
524
Alkis Evlogimenos346ee4c2004-12-08 23:56:15 +0000525 const PointerType *GEPPointerTy = cast<PointerType>(BasePtr1Ty);
Chris Lattner6ea17f77f2003-12-11 22:44:13 +0000526
527 // Find the (possibly empty) initial sequence of equal values... which are not
528 // necessarily constants.
Chris Lattner237b5b62007-02-10 22:12:53 +0000529 unsigned NumGEP1Operands = NumGEP1Ops, NumGEP2Operands = NumGEP2Ops;
Chris Lattner6ea17f77f2003-12-11 22:44:13 +0000530 unsigned MinOperands = std::min(NumGEP1Operands, NumGEP2Operands);
531 unsigned MaxOperands = std::max(NumGEP1Operands, NumGEP2Operands);
532 unsigned UnequalOper = 0;
533 while (UnequalOper != MinOperands &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000534 IndexOperandsEqual(GEP1Ops[UnequalOper], GEP2Ops[UnequalOper])) {
Chris Lattner6ea17f77f2003-12-11 22:44:13 +0000535 // Advance through the type as we go...
536 ++UnequalOper;
537 if (const CompositeType *CT = dyn_cast<CompositeType>(BasePtr1Ty))
538 BasePtr1Ty = CT->getTypeAtIndex(GEP1Ops[UnequalOper-1]);
539 else {
540 // If all operands equal each other, then the derived pointers must
541 // alias each other...
542 BasePtr1Ty = 0;
543 assert(UnequalOper == NumGEP1Operands && UnequalOper == NumGEP2Operands &&
544 "Ran out of type nesting, but not out of operands?");
545 return MustAlias;
Chris Lattner78dd4322003-06-02 05:42:39 +0000546 }
547 }
Chris Lattner78dd4322003-06-02 05:42:39 +0000548
Chris Lattner6ea17f77f2003-12-11 22:44:13 +0000549 // If we have seen all constant operands, and run out of indexes on one of the
550 // getelementptrs, check to see if the tail of the leftover one is all zeros.
551 // If so, return mustalias.
Chris Lattner1eed55d2003-12-11 23:20:16 +0000552 if (UnequalOper == MinOperands) {
Chris Lattner237b5b62007-02-10 22:12:53 +0000553 if (NumGEP1Ops < NumGEP2Ops) {
554 std::swap(GEP1Ops, GEP2Ops);
555 std::swap(NumGEP1Ops, NumGEP2Ops);
556 }
Misha Brukman01808ca2005-04-21 21:13:18 +0000557
Chris Lattner6ea17f77f2003-12-11 22:44:13 +0000558 bool AllAreZeros = true;
559 for (unsigned i = UnequalOper; i != MaxOperands; ++i)
Chris Lattner3c3e0582004-10-16 16:07:10 +0000560 if (!isa<Constant>(GEP1Ops[i]) ||
Chris Lattner6ea17f77f2003-12-11 22:44:13 +0000561 !cast<Constant>(GEP1Ops[i])->isNullValue()) {
562 AllAreZeros = false;
563 break;
564 }
565 if (AllAreZeros) return MustAlias;
566 }
567
Misha Brukman01808ca2005-04-21 21:13:18 +0000568
Chris Lattnerd6a2a992003-02-26 19:41:54 +0000569 // So now we know that the indexes derived from the base pointers,
570 // which are known to alias, are different. We can still determine a
571 // no-alias result if there are differing constant pairs in the index
572 // chain. For example:
573 // A[i][0] != A[j][1] iff (&A[0][1]-&A[0][0] >= std::max(G1S, G2S))
574 //
Chris Lattner071faf22006-03-04 02:06:34 +0000575 // We have to be careful here about array accesses. In particular, consider:
576 // A[1][0] vs A[0][i]
577 // In this case, we don't *know* that the array will be accessed in bounds:
578 // the index could even be negative. Because of this, we have to
579 // conservatively *give up* and return may alias. We disregard differing
580 // array subscripts that are followed by a variable index without going
581 // through a struct.
582 //
Chris Lattnerd6a2a992003-02-26 19:41:54 +0000583 unsigned SizeMax = std::max(G1S, G2S);
Chris Lattner1b784b12004-11-28 20:30:15 +0000584 if (SizeMax == ~0U) return MayAlias; // Avoid frivolous work.
Chris Lattner78dd4322003-06-02 05:42:39 +0000585
Chris Lattnerd6a2a992003-02-26 19:41:54 +0000586 // Scan for the first operand that is constant and unequal in the
Chris Lattner69193f92004-04-05 01:30:19 +0000587 // two getelementptrs...
Chris Lattnerd6a2a992003-02-26 19:41:54 +0000588 unsigned FirstConstantOper = UnequalOper;
Chris Lattner6ea17f77f2003-12-11 22:44:13 +0000589 for (; FirstConstantOper != MinOperands; ++FirstConstantOper) {
590 const Value *G1Oper = GEP1Ops[FirstConstantOper];
591 const Value *G2Oper = GEP2Ops[FirstConstantOper];
Misha Brukman01808ca2005-04-21 21:13:18 +0000592
Chris Lattnerc99dd892004-01-12 17:57:32 +0000593 if (G1Oper != G2Oper) // Found non-equal constant indexes...
Chris Lattner3c3e0582004-10-16 16:07:10 +0000594 if (Constant *G1OC = dyn_cast<ConstantInt>(const_cast<Value*>(G1Oper)))
595 if (Constant *G2OC = dyn_cast<ConstantInt>(const_cast<Value*>(G2Oper))){
Chris Lattner69193f92004-04-05 01:30:19 +0000596 if (G1OC->getType() != G2OC->getType()) {
597 // Sign extend both operands to long.
Reid Spencerc635f472006-12-31 05:48:39 +0000598 if (G1OC->getType() != Type::Int64Ty)
599 G1OC = ConstantExpr::getSExt(G1OC, Type::Int64Ty);
600 if (G2OC->getType() != Type::Int64Ty)
601 G2OC = ConstantExpr::getSExt(G2OC, Type::Int64Ty);
Chris Lattner69193f92004-04-05 01:30:19 +0000602 GEP1Ops[FirstConstantOper] = G1OC;
603 GEP2Ops[FirstConstantOper] = G2OC;
604 }
Chris Lattner071faf22006-03-04 02:06:34 +0000605
Chris Lattner69193f92004-04-05 01:30:19 +0000606 if (G1OC != G2OC) {
Dan Gohman06c60b62007-07-16 14:29:03 +0000607 // Handle the "be careful" case above: if this is an array/vector
Chris Lattner071faf22006-03-04 02:06:34 +0000608 // subscript, scan for a subsequent variable array index.
Chris Lattnerea2abe22006-11-03 21:58:48 +0000609 if (isa<SequentialType>(BasePtr1Ty)) {
610 const Type *NextTy =
611 cast<SequentialType>(BasePtr1Ty)->getElementType();
Chris Lattner071faf22006-03-04 02:06:34 +0000612 bool isBadCase = false;
613
614 for (unsigned Idx = FirstConstantOper+1;
Chris Lattnerea2abe22006-11-03 21:58:48 +0000615 Idx != MinOperands && isa<SequentialType>(NextTy); ++Idx) {
Chris Lattner071faf22006-03-04 02:06:34 +0000616 const Value *V1 = GEP1Ops[Idx], *V2 = GEP2Ops[Idx];
617 if (!isa<Constant>(V1) || !isa<Constant>(V2)) {
618 isBadCase = true;
619 break;
620 }
Chris Lattnerea2abe22006-11-03 21:58:48 +0000621 NextTy = cast<SequentialType>(NextTy)->getElementType();
Chris Lattner071faf22006-03-04 02:06:34 +0000622 }
623
624 if (isBadCase) G1OC = 0;
625 }
626
Chris Lattner3c3e0582004-10-16 16:07:10 +0000627 // Make sure they are comparable (ie, not constant expressions), and
628 // make sure the GEP with the smaller leading constant is GEP1.
Chris Lattner071faf22006-03-04 02:06:34 +0000629 if (G1OC) {
Reid Spencer266e42b2006-12-23 06:05:41 +0000630 Constant *Compare = ConstantExpr::getICmp(ICmpInst::ICMP_SGT,
631 G1OC, G2OC);
Zhou Sheng75b871f2007-01-11 12:24:14 +0000632 if (ConstantInt *CV = dyn_cast<ConstantInt>(Compare)) {
Chris Lattner237b5b62007-02-10 22:12:53 +0000633 if (CV->getZExtValue()) { // If they are comparable and G2 > G1
Chris Lattner071faf22006-03-04 02:06:34 +0000634 std::swap(GEP1Ops, GEP2Ops); // Make GEP1 < GEP2
Chris Lattner237b5b62007-02-10 22:12:53 +0000635 std::swap(NumGEP1Ops, NumGEP2Ops);
636 }
Chris Lattner071faf22006-03-04 02:06:34 +0000637 break;
638 }
Chris Lattner69193f92004-04-05 01:30:19 +0000639 }
Chris Lattnerc99dd892004-01-12 17:57:32 +0000640 }
641 }
Chris Lattner6ea17f77f2003-12-11 22:44:13 +0000642 BasePtr1Ty = cast<CompositeType>(BasePtr1Ty)->getTypeAtIndex(G1Oper);
Chris Lattnerd6a2a992003-02-26 19:41:54 +0000643 }
Misha Brukman01808ca2005-04-21 21:13:18 +0000644
Chris Lattner6ea17f77f2003-12-11 22:44:13 +0000645 // No shared constant operands, and we ran out of common operands. At this
646 // point, the GEP instructions have run through all of their operands, and we
647 // haven't found evidence that there are any deltas between the GEP's.
648 // However, one GEP may have more operands than the other. If this is the
Chris Lattner69193f92004-04-05 01:30:19 +0000649 // case, there may still be hope. Check this now.
Chris Lattner6ea17f77f2003-12-11 22:44:13 +0000650 if (FirstConstantOper == MinOperands) {
651 // Make GEP1Ops be the longer one if there is a longer one.
Chris Lattner237b5b62007-02-10 22:12:53 +0000652 if (NumGEP1Ops < NumGEP2Ops) {
Chris Lattner6ea17f77f2003-12-11 22:44:13 +0000653 std::swap(GEP1Ops, GEP2Ops);
Chris Lattner237b5b62007-02-10 22:12:53 +0000654 std::swap(NumGEP1Ops, NumGEP2Ops);
655 }
Chris Lattner6ea17f77f2003-12-11 22:44:13 +0000656
657 // Is there anything to check?
Chris Lattner237b5b62007-02-10 22:12:53 +0000658 if (NumGEP1Ops > MinOperands) {
Chris Lattner6ea17f77f2003-12-11 22:44:13 +0000659 for (unsigned i = FirstConstantOper; i != MaxOperands; ++i)
Zhou Sheng75b871f2007-01-11 12:24:14 +0000660 if (isa<ConstantInt>(GEP1Ops[i]) &&
Zhou Shengaafe4e22007-04-19 05:39:12 +0000661 !cast<ConstantInt>(GEP1Ops[i])->isZero()) {
Chris Lattner6ea17f77f2003-12-11 22:44:13 +0000662 // Yup, there's a constant in the tail. Set all variables to
663 // constants in the GEP instruction to make it suiteable for
664 // TargetData::getIndexedOffset.
665 for (i = 0; i != MaxOperands; ++i)
Chris Lattner0fd2b9f2007-01-12 18:20:48 +0000666 if (!isa<ConstantInt>(GEP1Ops[i]))
Chris Lattner6ea17f77f2003-12-11 22:44:13 +0000667 GEP1Ops[i] = Constant::getNullValue(GEP1Ops[i]->getType());
668 // Okay, now get the offset. This is the relative offset for the full
669 // instruction.
670 const TargetData &TD = getTargetData();
Chris Lattner237b5b62007-02-10 22:12:53 +0000671 int64_t Offset1 = TD.getIndexedOffset(GEPPointerTy, GEP1Ops,
672 NumGEP1Ops);
Chris Lattner6ea17f77f2003-12-11 22:44:13 +0000673
Chris Lattner237b5b62007-02-10 22:12:53 +0000674 // Now check without any constants at the end.
675 int64_t Offset2 = TD.getIndexedOffset(GEPPointerTy, GEP1Ops,
676 MinOperands);
Misha Brukman01808ca2005-04-21 21:13:18 +0000677
Chris Lattner6ea17f77f2003-12-11 22:44:13 +0000678 // If the tail provided a bit enough offset, return noalias!
679 if ((uint64_t)(Offset2-Offset1) >= SizeMax)
680 return NoAlias;
681 }
682 }
Misha Brukman01808ca2005-04-21 21:13:18 +0000683
Chris Lattner6ea17f77f2003-12-11 22:44:13 +0000684 // Couldn't find anything useful.
685 return MayAlias;
686 }
Chris Lattnerd6a2a992003-02-26 19:41:54 +0000687
688 // If there are non-equal constants arguments, then we can figure
689 // out a minimum known delta between the two index expressions... at
690 // this point we know that the first constant index of GEP1 is less
691 // than the first constant index of GEP2.
Chris Lattner388bc982003-11-25 20:10:07 +0000692
Chris Lattner6ea17f77f2003-12-11 22:44:13 +0000693 // Advance BasePtr[12]Ty over this first differing constant operand.
Chris Lattner3e19c232006-03-04 21:48:01 +0000694 BasePtr2Ty = cast<CompositeType>(BasePtr1Ty)->
695 getTypeAtIndex(GEP2Ops[FirstConstantOper]);
696 BasePtr1Ty = cast<CompositeType>(BasePtr1Ty)->
697 getTypeAtIndex(GEP1Ops[FirstConstantOper]);
Misha Brukman01808ca2005-04-21 21:13:18 +0000698
Chris Lattner6ea17f77f2003-12-11 22:44:13 +0000699 // We are going to be using TargetData::getIndexedOffset to determine the
700 // offset that each of the GEP's is reaching. To do this, we have to convert
701 // all variable references to constant references. To do this, we convert the
Chris Lattner3e19c232006-03-04 21:48:01 +0000702 // initial sequence of array subscripts into constant zeros to start with.
703 const Type *ZeroIdxTy = GEPPointerTy;
704 for (unsigned i = 0; i != FirstConstantOper; ++i) {
705 if (!isa<StructType>(ZeroIdxTy))
Reid Spencerc635f472006-12-31 05:48:39 +0000706 GEP1Ops[i] = GEP2Ops[i] = Constant::getNullValue(Type::Int32Ty);
Chris Lattner6ea17f77f2003-12-11 22:44:13 +0000707
Chris Lattner3e19c232006-03-04 21:48:01 +0000708 if (const CompositeType *CT = dyn_cast<CompositeType>(ZeroIdxTy))
709 ZeroIdxTy = CT->getTypeAtIndex(GEP1Ops[i]);
710 }
711
Chris Lattner6ea17f77f2003-12-11 22:44:13 +0000712 // We know that GEP1Ops[FirstConstantOper] & GEP2Ops[FirstConstantOper] are ok
Misha Brukman01808ca2005-04-21 21:13:18 +0000713
Chris Lattnerd6a2a992003-02-26 19:41:54 +0000714 // Loop over the rest of the operands...
Chris Lattner6ea17f77f2003-12-11 22:44:13 +0000715 for (unsigned i = FirstConstantOper+1; i != MaxOperands; ++i) {
Chris Lattner237b5b62007-02-10 22:12:53 +0000716 const Value *Op1 = i < NumGEP1Ops ? GEP1Ops[i] : 0;
717 const Value *Op2 = i < NumGEP2Ops ? GEP2Ops[i] : 0;
Chris Lattner6ea17f77f2003-12-11 22:44:13 +0000718 // If they are equal, use a zero index...
719 if (Op1 == Op2 && BasePtr1Ty == BasePtr2Ty) {
Chris Lattner0fd2b9f2007-01-12 18:20:48 +0000720 if (!isa<ConstantInt>(Op1))
Chris Lattner6ea17f77f2003-12-11 22:44:13 +0000721 GEP1Ops[i] = GEP2Ops[i] = Constant::getNullValue(Op1->getType());
722 // Otherwise, just keep the constants we have.
Chris Lattnerd6a2a992003-02-26 19:41:54 +0000723 } else {
Chris Lattner6ea17f77f2003-12-11 22:44:13 +0000724 if (Op1) {
725 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
726 // If this is an array index, make sure the array element is in range.
Chris Lattnerea2abe22006-11-03 21:58:48 +0000727 if (const ArrayType *AT = dyn_cast<ArrayType>(BasePtr1Ty)) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000728 if (Op1C->getZExtValue() >= AT->getNumElements())
Chris Lattner6ea17f77f2003-12-11 22:44:13 +0000729 return MayAlias; // Be conservative with out-of-range accesses
Reid Spencerd84d35b2007-02-15 02:26:10 +0000730 } else if (const VectorType *PT = dyn_cast<VectorType>(BasePtr1Ty)) {
Chris Lattnerea2abe22006-11-03 21:58:48 +0000731 if (Op1C->getZExtValue() >= PT->getNumElements())
732 return MayAlias; // Be conservative with out-of-range accesses
733 }
734
Chris Lattner6ea17f77f2003-12-11 22:44:13 +0000735 } else {
736 // GEP1 is known to produce a value less than GEP2. To be
737 // conservatively correct, we must assume the largest possible
738 // constant is used in this position. This cannot be the initial
739 // index to the GEP instructions (because we know we have at least one
740 // element before this one with the different constant arguments), so
741 // we know that the current index must be into either a struct or
742 // array. Because we know it's not constant, this cannot be a
743 // structure index. Because of this, we can calculate the maximum
744 // value possible.
745 //
746 if (const ArrayType *AT = dyn_cast<ArrayType>(BasePtr1Ty))
Chris Lattner875d7b22007-01-14 05:57:53 +0000747 GEP1Ops[i] = ConstantInt::get(Type::Int64Ty,AT->getNumElements()-1);
Reid Spencerd84d35b2007-02-15 02:26:10 +0000748 else if (const VectorType *PT = dyn_cast<VectorType>(BasePtr1Ty))
Chris Lattner875d7b22007-01-14 05:57:53 +0000749 GEP1Ops[i] = ConstantInt::get(Type::Int64Ty,PT->getNumElements()-1);
Chris Lattnerea2abe22006-11-03 21:58:48 +0000750
Chris Lattner6ea17f77f2003-12-11 22:44:13 +0000751 }
Chris Lattnerd6a2a992003-02-26 19:41:54 +0000752 }
Misha Brukman01808ca2005-04-21 21:13:18 +0000753
Chris Lattner6ea17f77f2003-12-11 22:44:13 +0000754 if (Op2) {
755 if (const ConstantInt *Op2C = dyn_cast<ConstantInt>(Op2)) {
756 // If this is an array index, make sure the array element is in range.
Chris Lattnerea2abe22006-11-03 21:58:48 +0000757 if (const ArrayType *AT = dyn_cast<ArrayType>(BasePtr1Ty)) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000758 if (Op2C->getZExtValue() >= AT->getNumElements())
Chris Lattner6ea17f77f2003-12-11 22:44:13 +0000759 return MayAlias; // Be conservative with out-of-range accesses
Reid Spencerd84d35b2007-02-15 02:26:10 +0000760 } else if (const VectorType *PT = dyn_cast<VectorType>(BasePtr1Ty)) {
Chris Lattnerea2abe22006-11-03 21:58:48 +0000761 if (Op2C->getZExtValue() >= PT->getNumElements())
762 return MayAlias; // Be conservative with out-of-range accesses
763 }
Chris Lattner6ea17f77f2003-12-11 22:44:13 +0000764 } else { // Conservatively assume the minimum value for this index
765 GEP2Ops[i] = Constant::getNullValue(Op2->getType());
766 }
Chris Lattner78dd4322003-06-02 05:42:39 +0000767 }
Chris Lattner6ea17f77f2003-12-11 22:44:13 +0000768 }
769
770 if (BasePtr1Ty && Op1) {
771 if (const CompositeType *CT = dyn_cast<CompositeType>(BasePtr1Ty))
772 BasePtr1Ty = CT->getTypeAtIndex(GEP1Ops[i]);
773 else
774 BasePtr1Ty = 0;
775 }
776
777 if (BasePtr2Ty && Op2) {
778 if (const CompositeType *CT = dyn_cast<CompositeType>(BasePtr2Ty))
779 BasePtr2Ty = CT->getTypeAtIndex(GEP2Ops[i]);
780 else
781 BasePtr2Ty = 0;
Chris Lattnerd6a2a992003-02-26 19:41:54 +0000782 }
783 }
Misha Brukman01808ca2005-04-21 21:13:18 +0000784
Alkis Evlogimenos346ee4c2004-12-08 23:56:15 +0000785 if (GEPPointerTy->getElementType()->isSized()) {
Chris Lattnerc44bd782007-02-10 20:35:22 +0000786 int64_t Offset1 =
Chris Lattner237b5b62007-02-10 22:12:53 +0000787 getTargetData().getIndexedOffset(GEPPointerTy, GEP1Ops, NumGEP1Ops);
Chris Lattnerc44bd782007-02-10 20:35:22 +0000788 int64_t Offset2 =
Chris Lattner237b5b62007-02-10 22:12:53 +0000789 getTargetData().getIndexedOffset(GEPPointerTy, GEP2Ops, NumGEP2Ops);
Alkis Evlogimenos346ee4c2004-12-08 23:56:15 +0000790 assert(Offset1<Offset2 && "There is at least one different constant here!");
Chris Lattnerd6a2a992003-02-26 19:41:54 +0000791
Alkis Evlogimenos346ee4c2004-12-08 23:56:15 +0000792 if ((uint64_t)(Offset2-Offset1) >= SizeMax) {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000793 //cerr << "Determined that these two GEP's don't alias ["
794 // << SizeMax << " bytes]: \n" << *GEP1 << *GEP2;
Alkis Evlogimenos346ee4c2004-12-08 23:56:15 +0000795 return NoAlias;
796 }
Chris Lattnerd6a2a992003-02-26 19:41:54 +0000797 }
798 return MayAlias;
799}
800
Chris Lattnerd82256a2004-03-15 03:36:49 +0000801namespace {
Reid Spencerf75727a2007-02-05 23:42:17 +0000802 struct VISIBILITY_HIDDEN StringCompare {
Chris Lattnerd82256a2004-03-15 03:36:49 +0000803 bool operator()(const char *LHS, const char *RHS) {
804 return strcmp(LHS, RHS) < 0;
805 }
806 };
807}
808
809// Note that this list cannot contain libm functions (such as acos and sqrt)
810// that set errno on a domain or other error.
Chris Lattnerbb40a9f2006-03-09 22:31:29 +0000811static const char *DoesntAccessMemoryFns[] = {
Chris Lattnerd82256a2004-03-15 03:36:49 +0000812 "abs", "labs", "llabs", "imaxabs", "fabs", "fabsf", "fabsl",
813 "trunc", "truncf", "truncl", "ldexp",
Misha Brukman01808ca2005-04-21 21:13:18 +0000814
Chris Lattnerd82256a2004-03-15 03:36:49 +0000815 "atan", "atanf", "atanl", "atan2", "atan2f", "atan2l",
816 "cbrt",
Chris Lattner59ecb572005-04-28 21:52:31 +0000817 "cos", "cosf", "cosl",
Misha Brukman01808ca2005-04-21 21:13:18 +0000818 "exp", "expf", "expl",
Chris Lattnerd82256a2004-03-15 03:36:49 +0000819 "hypot",
Chris Lattner59ecb572005-04-28 21:52:31 +0000820 "sin", "sinf", "sinl",
Chris Lattnerd82256a2004-03-15 03:36:49 +0000821 "tan", "tanf", "tanl", "tanh", "tanhf", "tanhl",
Chris Lattner898e50e2005-08-24 16:58:56 +0000822
823 "floor", "floorf", "floorl", "ceil", "ceilf", "ceill",
Chris Lattnerd82256a2004-03-15 03:36:49 +0000824
Chris Lattnerea42c852004-03-15 04:18:28 +0000825 // ctype.h
Chris Lattnerd82256a2004-03-15 03:36:49 +0000826 "isalnum", "isalpha", "iscntrl", "isdigit", "isgraph", "islower", "isprint"
827 "ispunct", "isspace", "isupper", "isxdigit", "tolower", "toupper",
828
Chris Lattnerea42c852004-03-15 04:18:28 +0000829 // wctype.h"
Chris Lattnerd82256a2004-03-15 03:36:49 +0000830 "iswalnum", "iswalpha", "iswcntrl", "iswdigit", "iswgraph", "iswlower",
831 "iswprint", "iswpunct", "iswspace", "iswupper", "iswxdigit",
832
Misha Brukman01808ca2005-04-21 21:13:18 +0000833 "iswctype", "towctrans", "towlower", "towupper",
Chris Lattnerea42c852004-03-15 04:18:28 +0000834
Misha Brukman01808ca2005-04-21 21:13:18 +0000835 "btowc", "wctob",
Chris Lattner8ad948d2004-03-16 03:41:35 +0000836
837 "isinf", "isnan", "finite",
838
839 // C99 math functions
840 "copysign", "copysignf", "copysignd",
841 "nexttoward", "nexttowardf", "nexttowardd",
842 "nextafter", "nextafterf", "nextafterd",
843
Andrew Lenharth23167c32005-07-11 20:35:20 +0000844 // ISO C99:
Chris Lattner8ad948d2004-03-16 03:41:35 +0000845 "__signbit", "__signbitf", "__signbitl",
Chris Lattnerd82256a2004-03-15 03:36:49 +0000846};
847
Chris Lattnerd82256a2004-03-15 03:36:49 +0000848
Chris Lattnerbb40a9f2006-03-09 22:31:29 +0000849static const char *OnlyReadsMemoryFns[] = {
Chris Lattner8ad948d2004-03-16 03:41:35 +0000850 "atoi", "atol", "atof", "atoll", "atoq", "a64l",
Misha Brukman01808ca2005-04-21 21:13:18 +0000851 "bcmp", "memcmp", "memchr", "memrchr", "wmemcmp", "wmemchr",
Chris Lattnerd82256a2004-03-15 03:36:49 +0000852
853 // Strings
854 "strcmp", "strcasecmp", "strcoll", "strncmp", "strncasecmp",
Misha Brukman01808ca2005-04-21 21:13:18 +0000855 "strchr", "strcspn", "strlen", "strpbrk", "strrchr", "strspn", "strstr",
Chris Lattner8ad948d2004-03-16 03:41:35 +0000856 "index", "rindex",
Chris Lattnerd82256a2004-03-15 03:36:49 +0000857
858 // Wide char strings
859 "wcschr", "wcscmp", "wcscoll", "wcscspn", "wcslen", "wcsncmp", "wcspbrk",
Misha Brukman01808ca2005-04-21 21:13:18 +0000860 "wcsrchr", "wcsspn", "wcsstr",
Chris Lattner8ad948d2004-03-16 03:41:35 +0000861
862 // glibc
863 "alphasort", "alphasort64", "versionsort", "versionsort64",
864
865 // C99
866 "nan", "nanf", "nand",
Chris Lattner4a1b03c2004-04-10 06:55:27 +0000867
868 // File I/O
869 "feof", "ferror", "fileno",
870 "feof_unlocked", "ferror_unlocked", "fileno_unlocked"
Chris Lattnerd82256a2004-03-15 03:36:49 +0000871};
872
Chris Lattner8111c592006-10-04 21:52:35 +0000873static ManagedStatic<std::vector<const char*> > NoMemoryTable;
874static ManagedStatic<std::vector<const char*> > OnlyReadsMemoryTable;
875
876
Misha Brukman01808ca2005-04-21 21:13:18 +0000877AliasAnalysis::ModRefBehavior
Chris Lattner71d04bc2004-12-15 07:22:13 +0000878BasicAliasAnalysis::getModRefBehavior(Function *F, CallSite CS,
879 std::vector<PointerAccessInfo> *Info) {
Reid Spencer5301e7c2007-01-30 20:08:39 +0000880 if (!F->isDeclaration()) return UnknownModRefBehavior;
Chris Lattnerd82256a2004-03-15 03:36:49 +0000881
882 static bool Initialized = false;
883 if (!Initialized) {
Chris Lattner8111c592006-10-04 21:52:35 +0000884 NoMemoryTable->insert(NoMemoryTable->end(),
885 DoesntAccessMemoryFns,
886 DoesntAccessMemoryFns+
Chris Lattnerbb40a9f2006-03-09 22:31:29 +0000887 sizeof(DoesntAccessMemoryFns)/sizeof(DoesntAccessMemoryFns[0]));
888
Chris Lattner8111c592006-10-04 21:52:35 +0000889 OnlyReadsMemoryTable->insert(OnlyReadsMemoryTable->end(),
Chris Lattnerbb40a9f2006-03-09 22:31:29 +0000890 OnlyReadsMemoryFns,
891 OnlyReadsMemoryFns+
892 sizeof(OnlyReadsMemoryFns)/sizeof(OnlyReadsMemoryFns[0]));
893#define GET_MODREF_BEHAVIOR
894#include "llvm/Intrinsics.gen"
895#undef GET_MODREF_BEHAVIOR
896
Chris Lattnerd82256a2004-03-15 03:36:49 +0000897 // Sort the table the first time through.
Chris Lattner8111c592006-10-04 21:52:35 +0000898 std::sort(NoMemoryTable->begin(), NoMemoryTable->end(), StringCompare());
899 std::sort(OnlyReadsMemoryTable->begin(), OnlyReadsMemoryTable->end(),
Chris Lattnerd82256a2004-03-15 03:36:49 +0000900 StringCompare());
901 Initialized = true;
902 }
903
Chris Lattnerbb40a9f2006-03-09 22:31:29 +0000904 std::vector<const char*>::iterator Ptr =
Chris Lattner8111c592006-10-04 21:52:35 +0000905 std::lower_bound(NoMemoryTable->begin(), NoMemoryTable->end(),
Chris Lattnerbb40a9f2006-03-09 22:31:29 +0000906 F->getName().c_str(), StringCompare());
Chris Lattner8111c592006-10-04 21:52:35 +0000907 if (Ptr != NoMemoryTable->end() && *Ptr == F->getName())
Chris Lattner71d04bc2004-12-15 07:22:13 +0000908 return DoesNotAccessMemory;
Misha Brukman01808ca2005-04-21 21:13:18 +0000909
Chris Lattner8111c592006-10-04 21:52:35 +0000910 Ptr = std::lower_bound(OnlyReadsMemoryTable->begin(),
911 OnlyReadsMemoryTable->end(),
Chris Lattner71d04bc2004-12-15 07:22:13 +0000912 F->getName().c_str(), StringCompare());
Chris Lattner8111c592006-10-04 21:52:35 +0000913 if (Ptr != OnlyReadsMemoryTable->end() && *Ptr == F->getName())
Chris Lattner71d04bc2004-12-15 07:22:13 +0000914 return OnlyReadsMemory;
915
916 return UnknownModRefBehavior;
Chris Lattnerd82256a2004-03-15 03:36:49 +0000917}
Reid Spencerbe535662006-06-07 22:00:26 +0000918
919// Make sure that anything that uses AliasAnalysis pulls in this file...
920DEFINING_FILE_FOR(BasicAliasAnalysis)