BasicAliasAnalysis.cpp revision 193323
1193323Sed//===- BasicAliasAnalysis.cpp - Local Alias Analysis Impl -----------------===//
2193323Sed//
3193323Sed//                     The LLVM Compiler Infrastructure
4193323Sed//
5193323Sed// This file is distributed under the University of Illinois Open Source
6193323Sed// License. See LICENSE.TXT for details.
7193323Sed//
8193323Sed//===----------------------------------------------------------------------===//
9193323Sed//
10193323Sed// This file defines the default implementation of the Alias Analysis interface
11193323Sed// that simply implements a few identities (two different globals cannot alias,
12193323Sed// etc), but otherwise does no analysis.
13193323Sed//
14193323Sed//===----------------------------------------------------------------------===//
15193323Sed
16193323Sed#include "llvm/Analysis/AliasAnalysis.h"
17193323Sed#include "llvm/Analysis/CaptureTracking.h"
18193323Sed#include "llvm/Analysis/Passes.h"
19193323Sed#include "llvm/Constants.h"
20193323Sed#include "llvm/DerivedTypes.h"
21193323Sed#include "llvm/Function.h"
22193323Sed#include "llvm/GlobalVariable.h"
23193323Sed#include "llvm/Instructions.h"
24193323Sed#include "llvm/IntrinsicInst.h"
25193323Sed#include "llvm/Pass.h"
26193323Sed#include "llvm/Target/TargetData.h"
27193323Sed#include "llvm/ADT/SmallVector.h"
28193323Sed#include "llvm/ADT/STLExtras.h"
29193323Sed#include "llvm/Support/Compiler.h"
30193323Sed#include "llvm/Support/GetElementPtrTypeIterator.h"
31193323Sed#include "llvm/Support/ManagedStatic.h"
32193323Sed#include <algorithm>
33193323Sedusing namespace llvm;
34193323Sed
35193323Sed//===----------------------------------------------------------------------===//
36193323Sed// Useful predicates
37193323Sed//===----------------------------------------------------------------------===//
38193323Sed
39193323Sedstatic const User *isGEP(const Value *V) {
40193323Sed  if (isa<GetElementPtrInst>(V) ||
41193323Sed      (isa<ConstantExpr>(V) &&
42193323Sed       cast<ConstantExpr>(V)->getOpcode() == Instruction::GetElementPtr))
43193323Sed    return cast<User>(V);
44193323Sed  return 0;
45193323Sed}
46193323Sed
47193323Sedstatic const Value *GetGEPOperands(const Value *V,
48193323Sed                                   SmallVector<Value*, 16> &GEPOps) {
49193323Sed  assert(GEPOps.empty() && "Expect empty list to populate!");
50193323Sed  GEPOps.insert(GEPOps.end(), cast<User>(V)->op_begin()+1,
51193323Sed                cast<User>(V)->op_end());
52193323Sed
53193323Sed  // Accumulate all of the chained indexes into the operand array
54193323Sed  V = cast<User>(V)->getOperand(0);
55193323Sed
56193323Sed  while (const User *G = isGEP(V)) {
57193323Sed    if (!isa<Constant>(GEPOps[0]) || isa<GlobalValue>(GEPOps[0]) ||
58193323Sed        !cast<Constant>(GEPOps[0])->isNullValue())
59193323Sed      break;  // Don't handle folding arbitrary pointer offsets yet...
60193323Sed    GEPOps.erase(GEPOps.begin());   // Drop the zero index
61193323Sed    GEPOps.insert(GEPOps.begin(), G->op_begin()+1, G->op_end());
62193323Sed    V = G->getOperand(0);
63193323Sed  }
64193323Sed  return V;
65193323Sed}
66193323Sed
67193323Sed/// isKnownNonNull - Return true if we know that the specified value is never
68193323Sed/// null.
69193323Sedstatic bool isKnownNonNull(const Value *V) {
70193323Sed  // Alloca never returns null, malloc might.
71193323Sed  if (isa<AllocaInst>(V)) return true;
72193323Sed
73193323Sed  // A byval argument is never null.
74193323Sed  if (const Argument *A = dyn_cast<Argument>(V))
75193323Sed    return A->hasByValAttr();
76193323Sed
77193323Sed  // Global values are not null unless extern weak.
78193323Sed  if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
79193323Sed    return !GV->hasExternalWeakLinkage();
80193323Sed  return false;
81193323Sed}
82193323Sed
83193323Sed/// isNonEscapingLocalObject - Return true if the pointer is to a function-local
84193323Sed/// object that never escapes from the function.
85193323Sedstatic bool isNonEscapingLocalObject(const Value *V) {
86193323Sed  // If this is a local allocation, check to see if it escapes.
87193323Sed  if (isa<AllocationInst>(V) || isNoAliasCall(V))
88193323Sed    return !PointerMayBeCaptured(V, false);
89193323Sed
90193323Sed  // If this is an argument that corresponds to a byval or noalias argument,
91193323Sed  // then it has not escaped before entering the function.  Check if it escapes
92193323Sed  // inside the function.
93193323Sed  if (const Argument *A = dyn_cast<Argument>(V))
94193323Sed    if (A->hasByValAttr() || A->hasNoAliasAttr()) {
95193323Sed      // Don't bother analyzing arguments already known not to escape.
96193323Sed      if (A->hasNoCaptureAttr())
97193323Sed        return true;
98193323Sed      return !PointerMayBeCaptured(V, false);
99193323Sed    }
100193323Sed  return false;
101193323Sed}
102193323Sed
103193323Sed
104193323Sed/// isObjectSmallerThan - Return true if we can prove that the object specified
105193323Sed/// by V is smaller than Size.
106193323Sedstatic bool isObjectSmallerThan(const Value *V, unsigned Size,
107193323Sed                                const TargetData &TD) {
108193323Sed  const Type *AccessTy;
109193323Sed  if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
110193323Sed    AccessTy = GV->getType()->getElementType();
111193323Sed  } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(V)) {
112193323Sed    if (!AI->isArrayAllocation())
113193323Sed      AccessTy = AI->getType()->getElementType();
114193323Sed    else
115193323Sed      return false;
116193323Sed  } else if (const Argument *A = dyn_cast<Argument>(V)) {
117193323Sed    if (A->hasByValAttr())
118193323Sed      AccessTy = cast<PointerType>(A->getType())->getElementType();
119193323Sed    else
120193323Sed      return false;
121193323Sed  } else {
122193323Sed    return false;
123193323Sed  }
124193323Sed
125193323Sed  if (AccessTy->isSized())
126193323Sed    return TD.getTypeAllocSize(AccessTy) < Size;
127193323Sed  return false;
128193323Sed}
129193323Sed
130193323Sed//===----------------------------------------------------------------------===//
131193323Sed// NoAA Pass
132193323Sed//===----------------------------------------------------------------------===//
133193323Sed
134193323Sednamespace {
135193323Sed  /// NoAA - This class implements the -no-aa pass, which always returns "I
136193323Sed  /// don't know" for alias queries.  NoAA is unlike other alias analysis
137193323Sed  /// implementations, in that it does not chain to a previous analysis.  As
138193323Sed  /// such it doesn't follow many of the rules that other alias analyses must.
139193323Sed  ///
140193323Sed  struct VISIBILITY_HIDDEN NoAA : public ImmutablePass, public AliasAnalysis {
141193323Sed    static char ID; // Class identification, replacement for typeinfo
142193323Sed    NoAA() : ImmutablePass(&ID) {}
143193323Sed    explicit NoAA(void *PID) : ImmutablePass(PID) { }
144193323Sed
145193323Sed    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
146193323Sed      AU.addRequired<TargetData>();
147193323Sed    }
148193323Sed
149193323Sed    virtual void initializePass() {
150193323Sed      TD = &getAnalysis<TargetData>();
151193323Sed    }
152193323Sed
153193323Sed    virtual AliasResult alias(const Value *V1, unsigned V1Size,
154193323Sed                              const Value *V2, unsigned V2Size) {
155193323Sed      return MayAlias;
156193323Sed    }
157193323Sed
158193323Sed    virtual void getArgumentAccesses(Function *F, CallSite CS,
159193323Sed                                     std::vector<PointerAccessInfo> &Info) {
160193323Sed      assert(0 && "This method may not be called on this function!");
161193323Sed    }
162193323Sed
163193323Sed    virtual void getMustAliases(Value *P, std::vector<Value*> &RetVals) { }
164193323Sed    virtual bool pointsToConstantMemory(const Value *P) { return false; }
165193323Sed    virtual ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size) {
166193323Sed      return ModRef;
167193323Sed    }
168193323Sed    virtual ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) {
169193323Sed      return ModRef;
170193323Sed    }
171193323Sed    virtual bool hasNoModRefInfoForCalls() const { return true; }
172193323Sed
173193323Sed    virtual void deleteValue(Value *V) {}
174193323Sed    virtual void copyValue(Value *From, Value *To) {}
175193323Sed  };
176193323Sed}  // End of anonymous namespace
177193323Sed
178193323Sed// Register this pass...
179193323Sedchar NoAA::ID = 0;
180193323Sedstatic RegisterPass<NoAA>
181193323SedU("no-aa", "No Alias Analysis (always returns 'may' alias)", true, true);
182193323Sed
183193323Sed// Declare that we implement the AliasAnalysis interface
184193323Sedstatic RegisterAnalysisGroup<AliasAnalysis> V(U);
185193323Sed
186193323SedImmutablePass *llvm::createNoAAPass() { return new NoAA(); }
187193323Sed
188193323Sed//===----------------------------------------------------------------------===//
189193323Sed// BasicAA Pass
190193323Sed//===----------------------------------------------------------------------===//
191193323Sed
192193323Sednamespace {
193193323Sed  /// BasicAliasAnalysis - This is the default alias analysis implementation.
194193323Sed  /// Because it doesn't chain to a previous alias analysis (like -no-aa), it
195193323Sed  /// derives from the NoAA class.
196193323Sed  struct VISIBILITY_HIDDEN BasicAliasAnalysis : public NoAA {
197193323Sed    static char ID; // Class identification, replacement for typeinfo
198193323Sed    BasicAliasAnalysis() : NoAA(&ID) {}
199193323Sed    AliasResult alias(const Value *V1, unsigned V1Size,
200193323Sed                      const Value *V2, unsigned V2Size);
201193323Sed
202193323Sed    ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
203193323Sed    ModRefResult getModRefInfo(CallSite CS1, CallSite CS2);
204193323Sed
205193323Sed    /// hasNoModRefInfoForCalls - We can provide mod/ref information against
206193323Sed    /// non-escaping allocations.
207193323Sed    virtual bool hasNoModRefInfoForCalls() const { return false; }
208193323Sed
209193323Sed    /// pointsToConstantMemory - Chase pointers until we find a (constant
210193323Sed    /// global) or not.
211193323Sed    bool pointsToConstantMemory(const Value *P);
212193323Sed
213193323Sed  private:
214193323Sed    // CheckGEPInstructions - Check two GEP instructions with known
215193323Sed    // must-aliasing base pointers.  This checks to see if the index expressions
216193323Sed    // preclude the pointers from aliasing...
217193323Sed    AliasResult
218193323Sed    CheckGEPInstructions(const Type* BasePtr1Ty,
219193323Sed                         Value **GEP1Ops, unsigned NumGEP1Ops, unsigned G1Size,
220193323Sed                         const Type *BasePtr2Ty,
221193323Sed                         Value **GEP2Ops, unsigned NumGEP2Ops, unsigned G2Size);
222193323Sed  };
223193323Sed}  // End of anonymous namespace
224193323Sed
225193323Sed// Register this pass...
226193323Sedchar BasicAliasAnalysis::ID = 0;
227193323Sedstatic RegisterPass<BasicAliasAnalysis>
228193323SedX("basicaa", "Basic Alias Analysis (default AA impl)", false, true);
229193323Sed
230193323Sed// Declare that we implement the AliasAnalysis interface
231193323Sedstatic RegisterAnalysisGroup<AliasAnalysis, true> Y(X);
232193323Sed
233193323SedImmutablePass *llvm::createBasicAliasAnalysisPass() {
234193323Sed  return new BasicAliasAnalysis();
235193323Sed}
236193323Sed
237193323Sed
238193323Sed/// pointsToConstantMemory - Chase pointers until we find a (constant
239193323Sed/// global) or not.
240193323Sedbool BasicAliasAnalysis::pointsToConstantMemory(const Value *P) {
241193323Sed  if (const GlobalVariable *GV =
242193323Sed        dyn_cast<GlobalVariable>(P->getUnderlyingObject()))
243193323Sed    return GV->isConstant();
244193323Sed  return false;
245193323Sed}
246193323Sed
247193323Sed
248193323Sed// getModRefInfo - Check to see if the specified callsite can clobber the
249193323Sed// specified memory object.  Since we only look at local properties of this
250193323Sed// function, we really can't say much about this query.  We do, however, use
251193323Sed// simple "address taken" analysis on local objects.
252193323Sed//
253193323SedAliasAnalysis::ModRefResult
254193323SedBasicAliasAnalysis::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
255193323Sed  if (!isa<Constant>(P)) {
256193323Sed    const Value *Object = P->getUnderlyingObject();
257193323Sed
258193323Sed    // If this is a tail call and P points to a stack location, we know that
259193323Sed    // the tail call cannot access or modify the local stack.
260193323Sed    // We cannot exclude byval arguments here; these belong to the caller of
261193323Sed    // the current function not to the current function, and a tail callee
262193323Sed    // may reference them.
263193323Sed    if (isa<AllocaInst>(Object))
264193323Sed      if (CallInst *CI = dyn_cast<CallInst>(CS.getInstruction()))
265193323Sed        if (CI->isTailCall())
266193323Sed          return NoModRef;
267193323Sed
268193323Sed    // If the pointer is to a locally allocated object that does not escape,
269193323Sed    // then the call can not mod/ref the pointer unless the call takes the
270193323Sed    // argument without capturing it.
271193323Sed    if (isNonEscapingLocalObject(Object) && CS.getInstruction() != Object) {
272193323Sed      bool passedAsArg = false;
273193323Sed      // TODO: Eventually only check 'nocapture' arguments.
274193323Sed      for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
275193323Sed           CI != CE; ++CI)
276193323Sed        if (isa<PointerType>((*CI)->getType()) &&
277193323Sed            alias(cast<Value>(CI), ~0U, P, ~0U) != NoAlias)
278193323Sed          passedAsArg = true;
279193323Sed
280193323Sed      if (!passedAsArg)
281193323Sed        return NoModRef;
282193323Sed    }
283193323Sed  }
284193323Sed
285193323Sed  // The AliasAnalysis base class has some smarts, lets use them.
286193323Sed  return AliasAnalysis::getModRefInfo(CS, P, Size);
287193323Sed}
288193323Sed
289193323Sed
290193323SedAliasAnalysis::ModRefResult
291193323SedBasicAliasAnalysis::getModRefInfo(CallSite CS1, CallSite CS2) {
292193323Sed  // If CS1 or CS2 are readnone, they don't interact.
293193323Sed  ModRefBehavior CS1B = AliasAnalysis::getModRefBehavior(CS1);
294193323Sed  if (CS1B == DoesNotAccessMemory) return NoModRef;
295193323Sed
296193323Sed  ModRefBehavior CS2B = AliasAnalysis::getModRefBehavior(CS2);
297193323Sed  if (CS2B == DoesNotAccessMemory) return NoModRef;
298193323Sed
299193323Sed  // If they both only read from memory, just return ref.
300193323Sed  if (CS1B == OnlyReadsMemory && CS2B == OnlyReadsMemory)
301193323Sed    return Ref;
302193323Sed
303193323Sed  // Otherwise, fall back to NoAA (mod+ref).
304193323Sed  return NoAA::getModRefInfo(CS1, CS2);
305193323Sed}
306193323Sed
307193323Sed
308193323Sed// alias - Provide a bunch of ad-hoc rules to disambiguate in common cases, such
309193323Sed// as array references.
310193323Sed//
311193323SedAliasAnalysis::AliasResult
312193323SedBasicAliasAnalysis::alias(const Value *V1, unsigned V1Size,
313193323Sed                          const Value *V2, unsigned V2Size) {
314193323Sed  // Strip off any constant expression casts if they exist
315193323Sed  if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V1))
316193323Sed    if (CE->isCast() && isa<PointerType>(CE->getOperand(0)->getType()))
317193323Sed      V1 = CE->getOperand(0);
318193323Sed  if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V2))
319193323Sed    if (CE->isCast() && isa<PointerType>(CE->getOperand(0)->getType()))
320193323Sed      V2 = CE->getOperand(0);
321193323Sed
322193323Sed  // Are we checking for alias of the same value?
323193323Sed  if (V1 == V2) return MustAlias;
324193323Sed
325193323Sed  if (!isa<PointerType>(V1->getType()) || !isa<PointerType>(V2->getType()))
326193323Sed    return NoAlias;  // Scalars cannot alias each other
327193323Sed
328193323Sed  // Strip off cast instructions.   Since V1 and V2 are pointers, they must be
329193323Sed  // pointer<->pointer bitcasts.
330193323Sed  if (const BitCastInst *I = dyn_cast<BitCastInst>(V1))
331193323Sed    return alias(I->getOperand(0), V1Size, V2, V2Size);
332193323Sed  if (const BitCastInst *I = dyn_cast<BitCastInst>(V2))
333193323Sed    return alias(V1, V1Size, I->getOperand(0), V2Size);
334193323Sed
335193323Sed  // Figure out what objects these things are pointing to if we can.
336193323Sed  const Value *O1 = V1->getUnderlyingObject();
337193323Sed  const Value *O2 = V2->getUnderlyingObject();
338193323Sed
339193323Sed  if (O1 != O2) {
340193323Sed    // If V1/V2 point to two different objects we know that we have no alias.
341193323Sed    if (isIdentifiedObject(O1) && isIdentifiedObject(O2))
342193323Sed      return NoAlias;
343193323Sed
344193323Sed    // Arguments can't alias with local allocations or noalias calls.
345193323Sed    if ((isa<Argument>(O1) && (isa<AllocationInst>(O2) || isNoAliasCall(O2))) ||
346193323Sed        (isa<Argument>(O2) && (isa<AllocationInst>(O1) || isNoAliasCall(O1))))
347193323Sed      return NoAlias;
348193323Sed
349193323Sed    // Most objects can't alias null.
350193323Sed    if ((isa<ConstantPointerNull>(V2) && isKnownNonNull(O1)) ||
351193323Sed        (isa<ConstantPointerNull>(V1) && isKnownNonNull(O2)))
352193323Sed      return NoAlias;
353193323Sed  }
354193323Sed
355193323Sed  // If the size of one access is larger than the entire object on the other
356193323Sed  // side, then we know such behavior is undefined and can assume no alias.
357193323Sed  const TargetData &TD = getTargetData();
358193323Sed  if ((V1Size != ~0U && isObjectSmallerThan(O2, V1Size, TD)) ||
359193323Sed      (V2Size != ~0U && isObjectSmallerThan(O1, V2Size, TD)))
360193323Sed    return NoAlias;
361193323Sed
362193323Sed  // If one pointer is the result of a call/invoke and the other is a
363193323Sed  // non-escaping local object, then we know the object couldn't escape to a
364193323Sed  // point where the call could return it.
365193323Sed  if ((isa<CallInst>(O1) || isa<InvokeInst>(O1)) &&
366193323Sed      isNonEscapingLocalObject(O2) && O1 != O2)
367193323Sed    return NoAlias;
368193323Sed  if ((isa<CallInst>(O2) || isa<InvokeInst>(O2)) &&
369193323Sed      isNonEscapingLocalObject(O1) && O1 != O2)
370193323Sed    return NoAlias;
371193323Sed
372193323Sed  // If we have two gep instructions with must-alias'ing base pointers, figure
373193323Sed  // out if the indexes to the GEP tell us anything about the derived pointer.
374193323Sed  // Note that we also handle chains of getelementptr instructions as well as
375193323Sed  // constant expression getelementptrs here.
376193323Sed  //
377193323Sed  if (isGEP(V1) && isGEP(V2)) {
378193323Sed    const User *GEP1 = cast<User>(V1);
379193323Sed    const User *GEP2 = cast<User>(V2);
380193323Sed
381193323Sed    // If V1 and V2 are identical GEPs, just recurse down on both of them.
382193323Sed    // This allows us to analyze things like:
383193323Sed    //   P = gep A, 0, i, 1
384193323Sed    //   Q = gep B, 0, i, 1
385193323Sed    // by just analyzing A and B.  This is even safe for variable indices.
386193323Sed    if (GEP1->getType() == GEP2->getType() &&
387193323Sed        GEP1->getNumOperands() == GEP2->getNumOperands() &&
388193323Sed        GEP1->getOperand(0)->getType() == GEP2->getOperand(0)->getType() &&
389193323Sed        // All operands are the same, ignoring the base.
390193323Sed        std::equal(GEP1->op_begin()+1, GEP1->op_end(), GEP2->op_begin()+1))
391193323Sed      return alias(GEP1->getOperand(0), V1Size, GEP2->getOperand(0), V2Size);
392193323Sed
393193323Sed
394193323Sed    // Drill down into the first non-gep value, to test for must-aliasing of
395193323Sed    // the base pointers.
396193323Sed    while (isGEP(GEP1->getOperand(0)) &&
397193323Sed           GEP1->getOperand(1) ==
398193323Sed           Constant::getNullValue(GEP1->getOperand(1)->getType()))
399193323Sed      GEP1 = cast<User>(GEP1->getOperand(0));
400193323Sed    const Value *BasePtr1 = GEP1->getOperand(0);
401193323Sed
402193323Sed    while (isGEP(GEP2->getOperand(0)) &&
403193323Sed           GEP2->getOperand(1) ==
404193323Sed           Constant::getNullValue(GEP2->getOperand(1)->getType()))
405193323Sed      GEP2 = cast<User>(GEP2->getOperand(0));
406193323Sed    const Value *BasePtr2 = GEP2->getOperand(0);
407193323Sed
408193323Sed    // Do the base pointers alias?
409193323Sed    AliasResult BaseAlias = alias(BasePtr1, ~0U, BasePtr2, ~0U);
410193323Sed    if (BaseAlias == NoAlias) return NoAlias;
411193323Sed    if (BaseAlias == MustAlias) {
412193323Sed      // If the base pointers alias each other exactly, check to see if we can
413193323Sed      // figure out anything about the resultant pointers, to try to prove
414193323Sed      // non-aliasing.
415193323Sed
416193323Sed      // Collect all of the chained GEP operands together into one simple place
417193323Sed      SmallVector<Value*, 16> GEP1Ops, GEP2Ops;
418193323Sed      BasePtr1 = GetGEPOperands(V1, GEP1Ops);
419193323Sed      BasePtr2 = GetGEPOperands(V2, GEP2Ops);
420193323Sed
421193323Sed      // If GetGEPOperands were able to fold to the same must-aliased pointer,
422193323Sed      // do the comparison.
423193323Sed      if (BasePtr1 == BasePtr2) {
424193323Sed        AliasResult GAlias =
425193323Sed          CheckGEPInstructions(BasePtr1->getType(),
426193323Sed                               &GEP1Ops[0], GEP1Ops.size(), V1Size,
427193323Sed                               BasePtr2->getType(),
428193323Sed                               &GEP2Ops[0], GEP2Ops.size(), V2Size);
429193323Sed        if (GAlias != MayAlias)
430193323Sed          return GAlias;
431193323Sed      }
432193323Sed    }
433193323Sed  }
434193323Sed
435193323Sed  // Check to see if these two pointers are related by a getelementptr
436193323Sed  // instruction.  If one pointer is a GEP with a non-zero index of the other
437193323Sed  // pointer, we know they cannot alias.
438193323Sed  //
439193323Sed  if (isGEP(V2)) {
440193323Sed    std::swap(V1, V2);
441193323Sed    std::swap(V1Size, V2Size);
442193323Sed  }
443193323Sed
444193323Sed  if (V1Size != ~0U && V2Size != ~0U)
445193323Sed    if (isGEP(V1)) {
446193323Sed      SmallVector<Value*, 16> GEPOperands;
447193323Sed      const Value *BasePtr = GetGEPOperands(V1, GEPOperands);
448193323Sed
449193323Sed      AliasResult R = alias(BasePtr, V1Size, V2, V2Size);
450193323Sed      if (R == MustAlias) {
451193323Sed        // If there is at least one non-zero constant index, we know they cannot
452193323Sed        // alias.
453193323Sed        bool ConstantFound = false;
454193323Sed        bool AllZerosFound = true;
455193323Sed        for (unsigned i = 0, e = GEPOperands.size(); i != e; ++i)
456193323Sed          if (const Constant *C = dyn_cast<Constant>(GEPOperands[i])) {
457193323Sed            if (!C->isNullValue()) {
458193323Sed              ConstantFound = true;
459193323Sed              AllZerosFound = false;
460193323Sed              break;
461193323Sed            }
462193323Sed          } else {
463193323Sed            AllZerosFound = false;
464193323Sed          }
465193323Sed
466193323Sed        // If we have getelementptr <ptr>, 0, 0, 0, 0, ... and V2 must aliases
467193323Sed        // the ptr, the end result is a must alias also.
468193323Sed        if (AllZerosFound)
469193323Sed          return MustAlias;
470193323Sed
471193323Sed        if (ConstantFound) {
472193323Sed          if (V2Size <= 1 && V1Size <= 1)  // Just pointer check?
473193323Sed            return NoAlias;
474193323Sed
475193323Sed          // Otherwise we have to check to see that the distance is more than
476193323Sed          // the size of the argument... build an index vector that is equal to
477193323Sed          // the arguments provided, except substitute 0's for any variable
478193323Sed          // indexes we find...
479193323Sed          if (cast<PointerType>(
480193323Sed                BasePtr->getType())->getElementType()->isSized()) {
481193323Sed            for (unsigned i = 0; i != GEPOperands.size(); ++i)
482193323Sed              if (!isa<ConstantInt>(GEPOperands[i]))
483193323Sed                GEPOperands[i] =
484193323Sed                  Constant::getNullValue(GEPOperands[i]->getType());
485193323Sed            int64_t Offset =
486193323Sed              getTargetData().getIndexedOffset(BasePtr->getType(),
487193323Sed                                               &GEPOperands[0],
488193323Sed                                               GEPOperands.size());
489193323Sed
490193323Sed            if (Offset >= (int64_t)V2Size || Offset <= -(int64_t)V1Size)
491193323Sed              return NoAlias;
492193323Sed          }
493193323Sed        }
494193323Sed      }
495193323Sed    }
496193323Sed
497193323Sed  return MayAlias;
498193323Sed}
499193323Sed
500193323Sed// This function is used to determine if the indices of two GEP instructions are
501193323Sed// equal. V1 and V2 are the indices.
502193323Sedstatic bool IndexOperandsEqual(Value *V1, Value *V2) {
503193323Sed  if (V1->getType() == V2->getType())
504193323Sed    return V1 == V2;
505193323Sed  if (Constant *C1 = dyn_cast<Constant>(V1))
506193323Sed    if (Constant *C2 = dyn_cast<Constant>(V2)) {
507193323Sed      // Sign extend the constants to long types, if necessary
508193323Sed      if (C1->getType() != Type::Int64Ty)
509193323Sed        C1 = ConstantExpr::getSExt(C1, Type::Int64Ty);
510193323Sed      if (C2->getType() != Type::Int64Ty)
511193323Sed        C2 = ConstantExpr::getSExt(C2, Type::Int64Ty);
512193323Sed      return C1 == C2;
513193323Sed    }
514193323Sed  return false;
515193323Sed}
516193323Sed
517193323Sed/// CheckGEPInstructions - Check two GEP instructions with known must-aliasing
518193323Sed/// base pointers.  This checks to see if the index expressions preclude the
519193323Sed/// pointers from aliasing...
520193323SedAliasAnalysis::AliasResult
521193323SedBasicAliasAnalysis::CheckGEPInstructions(
522193323Sed  const Type* BasePtr1Ty, Value **GEP1Ops, unsigned NumGEP1Ops, unsigned G1S,
523193323Sed  const Type *BasePtr2Ty, Value **GEP2Ops, unsigned NumGEP2Ops, unsigned G2S) {
524193323Sed  // We currently can't handle the case when the base pointers have different
525193323Sed  // primitive types.  Since this is uncommon anyway, we are happy being
526193323Sed  // extremely conservative.
527193323Sed  if (BasePtr1Ty != BasePtr2Ty)
528193323Sed    return MayAlias;
529193323Sed
530193323Sed  const PointerType *GEPPointerTy = cast<PointerType>(BasePtr1Ty);
531193323Sed
532193323Sed  // Find the (possibly empty) initial sequence of equal values... which are not
533193323Sed  // necessarily constants.
534193323Sed  unsigned NumGEP1Operands = NumGEP1Ops, NumGEP2Operands = NumGEP2Ops;
535193323Sed  unsigned MinOperands = std::min(NumGEP1Operands, NumGEP2Operands);
536193323Sed  unsigned MaxOperands = std::max(NumGEP1Operands, NumGEP2Operands);
537193323Sed  unsigned UnequalOper = 0;
538193323Sed  while (UnequalOper != MinOperands &&
539193323Sed         IndexOperandsEqual(GEP1Ops[UnequalOper], GEP2Ops[UnequalOper])) {
540193323Sed    // Advance through the type as we go...
541193323Sed    ++UnequalOper;
542193323Sed    if (const CompositeType *CT = dyn_cast<CompositeType>(BasePtr1Ty))
543193323Sed      BasePtr1Ty = CT->getTypeAtIndex(GEP1Ops[UnequalOper-1]);
544193323Sed    else {
545193323Sed      // If all operands equal each other, then the derived pointers must
546193323Sed      // alias each other...
547193323Sed      BasePtr1Ty = 0;
548193323Sed      assert(UnequalOper == NumGEP1Operands && UnequalOper == NumGEP2Operands &&
549193323Sed             "Ran out of type nesting, but not out of operands?");
550193323Sed      return MustAlias;
551193323Sed    }
552193323Sed  }
553193323Sed
554193323Sed  // If we have seen all constant operands, and run out of indexes on one of the
555193323Sed  // getelementptrs, check to see if the tail of the leftover one is all zeros.
556193323Sed  // If so, return mustalias.
557193323Sed  if (UnequalOper == MinOperands) {
558193323Sed    if (NumGEP1Ops < NumGEP2Ops) {
559193323Sed      std::swap(GEP1Ops, GEP2Ops);
560193323Sed      std::swap(NumGEP1Ops, NumGEP2Ops);
561193323Sed    }
562193323Sed
563193323Sed    bool AllAreZeros = true;
564193323Sed    for (unsigned i = UnequalOper; i != MaxOperands; ++i)
565193323Sed      if (!isa<Constant>(GEP1Ops[i]) ||
566193323Sed          !cast<Constant>(GEP1Ops[i])->isNullValue()) {
567193323Sed        AllAreZeros = false;
568193323Sed        break;
569193323Sed      }
570193323Sed    if (AllAreZeros) return MustAlias;
571193323Sed  }
572193323Sed
573193323Sed
574193323Sed  // So now we know that the indexes derived from the base pointers,
575193323Sed  // which are known to alias, are different.  We can still determine a
576193323Sed  // no-alias result if there are differing constant pairs in the index
577193323Sed  // chain.  For example:
578193323Sed  //        A[i][0] != A[j][1] iff (&A[0][1]-&A[0][0] >= std::max(G1S, G2S))
579193323Sed  //
580193323Sed  // We have to be careful here about array accesses.  In particular, consider:
581193323Sed  //        A[1][0] vs A[0][i]
582193323Sed  // In this case, we don't *know* that the array will be accessed in bounds:
583193323Sed  // the index could even be negative.  Because of this, we have to
584193323Sed  // conservatively *give up* and return may alias.  We disregard differing
585193323Sed  // array subscripts that are followed by a variable index without going
586193323Sed  // through a struct.
587193323Sed  //
588193323Sed  unsigned SizeMax = std::max(G1S, G2S);
589193323Sed  if (SizeMax == ~0U) return MayAlias; // Avoid frivolous work.
590193323Sed
591193323Sed  // Scan for the first operand that is constant and unequal in the
592193323Sed  // two getelementptrs...
593193323Sed  unsigned FirstConstantOper = UnequalOper;
594193323Sed  for (; FirstConstantOper != MinOperands; ++FirstConstantOper) {
595193323Sed    const Value *G1Oper = GEP1Ops[FirstConstantOper];
596193323Sed    const Value *G2Oper = GEP2Ops[FirstConstantOper];
597193323Sed
598193323Sed    if (G1Oper != G2Oper)   // Found non-equal constant indexes...
599193323Sed      if (Constant *G1OC = dyn_cast<ConstantInt>(const_cast<Value*>(G1Oper)))
600193323Sed        if (Constant *G2OC = dyn_cast<ConstantInt>(const_cast<Value*>(G2Oper))){
601193323Sed          if (G1OC->getType() != G2OC->getType()) {
602193323Sed            // Sign extend both operands to long.
603193323Sed            if (G1OC->getType() != Type::Int64Ty)
604193323Sed              G1OC = ConstantExpr::getSExt(G1OC, Type::Int64Ty);
605193323Sed            if (G2OC->getType() != Type::Int64Ty)
606193323Sed              G2OC = ConstantExpr::getSExt(G2OC, Type::Int64Ty);
607193323Sed            GEP1Ops[FirstConstantOper] = G1OC;
608193323Sed            GEP2Ops[FirstConstantOper] = G2OC;
609193323Sed          }
610193323Sed
611193323Sed          if (G1OC != G2OC) {
612193323Sed            // Handle the "be careful" case above: if this is an array/vector
613193323Sed            // subscript, scan for a subsequent variable array index.
614193323Sed            if (const SequentialType *STy =
615193323Sed                  dyn_cast<SequentialType>(BasePtr1Ty)) {
616193323Sed              const Type *NextTy = STy;
617193323Sed              bool isBadCase = false;
618193323Sed
619193323Sed              for (unsigned Idx = FirstConstantOper;
620193323Sed                   Idx != MinOperands && isa<SequentialType>(NextTy); ++Idx) {
621193323Sed                const Value *V1 = GEP1Ops[Idx], *V2 = GEP2Ops[Idx];
622193323Sed                if (!isa<Constant>(V1) || !isa<Constant>(V2)) {
623193323Sed                  isBadCase = true;
624193323Sed                  break;
625193323Sed                }
626193323Sed                // If the array is indexed beyond the bounds of the static type
627193323Sed                // at this level, it will also fall into the "be careful" case.
628193323Sed                // It would theoretically be possible to analyze these cases,
629193323Sed                // but for now just be conservatively correct.
630193323Sed                if (const ArrayType *ATy = dyn_cast<ArrayType>(STy))
631193323Sed                  if (cast<ConstantInt>(G1OC)->getZExtValue() >=
632193323Sed                        ATy->getNumElements() ||
633193323Sed                      cast<ConstantInt>(G2OC)->getZExtValue() >=
634193323Sed                        ATy->getNumElements()) {
635193323Sed                    isBadCase = true;
636193323Sed                    break;
637193323Sed                  }
638193323Sed                if (const VectorType *VTy = dyn_cast<VectorType>(STy))
639193323Sed                  if (cast<ConstantInt>(G1OC)->getZExtValue() >=
640193323Sed                        VTy->getNumElements() ||
641193323Sed                      cast<ConstantInt>(G2OC)->getZExtValue() >=
642193323Sed                        VTy->getNumElements()) {
643193323Sed                    isBadCase = true;
644193323Sed                    break;
645193323Sed                  }
646193323Sed                STy = cast<SequentialType>(NextTy);
647193323Sed                NextTy = cast<SequentialType>(NextTy)->getElementType();
648193323Sed              }
649193323Sed
650193323Sed              if (isBadCase) G1OC = 0;
651193323Sed            }
652193323Sed
653193323Sed            // Make sure they are comparable (ie, not constant expressions), and
654193323Sed            // make sure the GEP with the smaller leading constant is GEP1.
655193323Sed            if (G1OC) {
656193323Sed              Constant *Compare = ConstantExpr::getICmp(ICmpInst::ICMP_SGT,
657193323Sed                                                        G1OC, G2OC);
658193323Sed              if (ConstantInt *CV = dyn_cast<ConstantInt>(Compare)) {
659193323Sed                if (CV->getZExtValue()) {  // If they are comparable and G2 > G1
660193323Sed                  std::swap(GEP1Ops, GEP2Ops);  // Make GEP1 < GEP2
661193323Sed                  std::swap(NumGEP1Ops, NumGEP2Ops);
662193323Sed                }
663193323Sed                break;
664193323Sed              }
665193323Sed            }
666193323Sed          }
667193323Sed        }
668193323Sed    BasePtr1Ty = cast<CompositeType>(BasePtr1Ty)->getTypeAtIndex(G1Oper);
669193323Sed  }
670193323Sed
671193323Sed  // No shared constant operands, and we ran out of common operands.  At this
672193323Sed  // point, the GEP instructions have run through all of their operands, and we
673193323Sed  // haven't found evidence that there are any deltas between the GEP's.
674193323Sed  // However, one GEP may have more operands than the other.  If this is the
675193323Sed  // case, there may still be hope.  Check this now.
676193323Sed  if (FirstConstantOper == MinOperands) {
677193323Sed    // Make GEP1Ops be the longer one if there is a longer one.
678193323Sed    if (NumGEP1Ops < NumGEP2Ops) {
679193323Sed      std::swap(GEP1Ops, GEP2Ops);
680193323Sed      std::swap(NumGEP1Ops, NumGEP2Ops);
681193323Sed    }
682193323Sed
683193323Sed    // Is there anything to check?
684193323Sed    if (NumGEP1Ops > MinOperands) {
685193323Sed      for (unsigned i = FirstConstantOper; i != MaxOperands; ++i)
686193323Sed        if (isa<ConstantInt>(GEP1Ops[i]) &&
687193323Sed            !cast<ConstantInt>(GEP1Ops[i])->isZero()) {
688193323Sed          // Yup, there's a constant in the tail.  Set all variables to
689193323Sed          // constants in the GEP instruction to make it suitable for
690193323Sed          // TargetData::getIndexedOffset.
691193323Sed          for (i = 0; i != MaxOperands; ++i)
692193323Sed            if (!isa<ConstantInt>(GEP1Ops[i]))
693193323Sed              GEP1Ops[i] = Constant::getNullValue(GEP1Ops[i]->getType());
694193323Sed          // Okay, now get the offset.  This is the relative offset for the full
695193323Sed          // instruction.
696193323Sed          const TargetData &TD = getTargetData();
697193323Sed          int64_t Offset1 = TD.getIndexedOffset(GEPPointerTy, GEP1Ops,
698193323Sed                                                NumGEP1Ops);
699193323Sed
700193323Sed          // Now check without any constants at the end.
701193323Sed          int64_t Offset2 = TD.getIndexedOffset(GEPPointerTy, GEP1Ops,
702193323Sed                                                MinOperands);
703193323Sed
704193323Sed          // Make sure we compare the absolute difference.
705193323Sed          if (Offset1 > Offset2)
706193323Sed            std::swap(Offset1, Offset2);
707193323Sed
708193323Sed          // If the tail provided a bit enough offset, return noalias!
709193323Sed          if ((uint64_t)(Offset2-Offset1) >= SizeMax)
710193323Sed            return NoAlias;
711193323Sed          // Otherwise break - we don't look for another constant in the tail.
712193323Sed          break;
713193323Sed        }
714193323Sed    }
715193323Sed
716193323Sed    // Couldn't find anything useful.
717193323Sed    return MayAlias;
718193323Sed  }
719193323Sed
720193323Sed  // If there are non-equal constants arguments, then we can figure
721193323Sed  // out a minimum known delta between the two index expressions... at
722193323Sed  // this point we know that the first constant index of GEP1 is less
723193323Sed  // than the first constant index of GEP2.
724193323Sed
725193323Sed  // Advance BasePtr[12]Ty over this first differing constant operand.
726193323Sed  BasePtr2Ty = cast<CompositeType>(BasePtr1Ty)->
727193323Sed      getTypeAtIndex(GEP2Ops[FirstConstantOper]);
728193323Sed  BasePtr1Ty = cast<CompositeType>(BasePtr1Ty)->
729193323Sed      getTypeAtIndex(GEP1Ops[FirstConstantOper]);
730193323Sed
731193323Sed  // We are going to be using TargetData::getIndexedOffset to determine the
732193323Sed  // offset that each of the GEP's is reaching.  To do this, we have to convert
733193323Sed  // all variable references to constant references.  To do this, we convert the
734193323Sed  // initial sequence of array subscripts into constant zeros to start with.
735193323Sed  const Type *ZeroIdxTy = GEPPointerTy;
736193323Sed  for (unsigned i = 0; i != FirstConstantOper; ++i) {
737193323Sed    if (!isa<StructType>(ZeroIdxTy))
738193323Sed      GEP1Ops[i] = GEP2Ops[i] = Constant::getNullValue(Type::Int32Ty);
739193323Sed
740193323Sed    if (const CompositeType *CT = dyn_cast<CompositeType>(ZeroIdxTy))
741193323Sed      ZeroIdxTy = CT->getTypeAtIndex(GEP1Ops[i]);
742193323Sed  }
743193323Sed
744193323Sed  // We know that GEP1Ops[FirstConstantOper] & GEP2Ops[FirstConstantOper] are ok
745193323Sed
746193323Sed  // Loop over the rest of the operands...
747193323Sed  for (unsigned i = FirstConstantOper+1; i != MaxOperands; ++i) {
748193323Sed    const Value *Op1 = i < NumGEP1Ops ? GEP1Ops[i] : 0;
749193323Sed    const Value *Op2 = i < NumGEP2Ops ? GEP2Ops[i] : 0;
750193323Sed    // If they are equal, use a zero index...
751193323Sed    if (Op1 == Op2 && BasePtr1Ty == BasePtr2Ty) {
752193323Sed      if (!isa<ConstantInt>(Op1))
753193323Sed        GEP1Ops[i] = GEP2Ops[i] = Constant::getNullValue(Op1->getType());
754193323Sed      // Otherwise, just keep the constants we have.
755193323Sed    } else {
756193323Sed      if (Op1) {
757193323Sed        if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
758193323Sed          // If this is an array index, make sure the array element is in range.
759193323Sed          if (const ArrayType *AT = dyn_cast<ArrayType>(BasePtr1Ty)) {
760193323Sed            if (Op1C->getZExtValue() >= AT->getNumElements())
761193323Sed              return MayAlias;  // Be conservative with out-of-range accesses
762193323Sed          } else if (const VectorType *VT = dyn_cast<VectorType>(BasePtr1Ty)) {
763193323Sed            if (Op1C->getZExtValue() >= VT->getNumElements())
764193323Sed              return MayAlias;  // Be conservative with out-of-range accesses
765193323Sed          }
766193323Sed
767193323Sed        } else {
768193323Sed          // GEP1 is known to produce a value less than GEP2.  To be
769193323Sed          // conservatively correct, we must assume the largest possible
770193323Sed          // constant is used in this position.  This cannot be the initial
771193323Sed          // index to the GEP instructions (because we know we have at least one
772193323Sed          // element before this one with the different constant arguments), so
773193323Sed          // we know that the current index must be into either a struct or
774193323Sed          // array.  Because we know it's not constant, this cannot be a
775193323Sed          // structure index.  Because of this, we can calculate the maximum
776193323Sed          // value possible.
777193323Sed          //
778193323Sed          if (const ArrayType *AT = dyn_cast<ArrayType>(BasePtr1Ty))
779193323Sed            GEP1Ops[i] = ConstantInt::get(Type::Int64Ty,AT->getNumElements()-1);
780193323Sed          else if (const VectorType *VT = dyn_cast<VectorType>(BasePtr1Ty))
781193323Sed            GEP1Ops[i] = ConstantInt::get(Type::Int64Ty,VT->getNumElements()-1);
782193323Sed        }
783193323Sed      }
784193323Sed
785193323Sed      if (Op2) {
786193323Sed        if (const ConstantInt *Op2C = dyn_cast<ConstantInt>(Op2)) {
787193323Sed          // If this is an array index, make sure the array element is in range.
788193323Sed          if (const ArrayType *AT = dyn_cast<ArrayType>(BasePtr2Ty)) {
789193323Sed            if (Op2C->getZExtValue() >= AT->getNumElements())
790193323Sed              return MayAlias;  // Be conservative with out-of-range accesses
791193323Sed          } else if (const VectorType *VT = dyn_cast<VectorType>(BasePtr2Ty)) {
792193323Sed            if (Op2C->getZExtValue() >= VT->getNumElements())
793193323Sed              return MayAlias;  // Be conservative with out-of-range accesses
794193323Sed          }
795193323Sed        } else {  // Conservatively assume the minimum value for this index
796193323Sed          GEP2Ops[i] = Constant::getNullValue(Op2->getType());
797193323Sed        }
798193323Sed      }
799193323Sed    }
800193323Sed
801193323Sed    if (BasePtr1Ty && Op1) {
802193323Sed      if (const CompositeType *CT = dyn_cast<CompositeType>(BasePtr1Ty))
803193323Sed        BasePtr1Ty = CT->getTypeAtIndex(GEP1Ops[i]);
804193323Sed      else
805193323Sed        BasePtr1Ty = 0;
806193323Sed    }
807193323Sed
808193323Sed    if (BasePtr2Ty && Op2) {
809193323Sed      if (const CompositeType *CT = dyn_cast<CompositeType>(BasePtr2Ty))
810193323Sed        BasePtr2Ty = CT->getTypeAtIndex(GEP2Ops[i]);
811193323Sed      else
812193323Sed        BasePtr2Ty = 0;
813193323Sed    }
814193323Sed  }
815193323Sed
816193323Sed  if (GEPPointerTy->getElementType()->isSized()) {
817193323Sed    int64_t Offset1 =
818193323Sed      getTargetData().getIndexedOffset(GEPPointerTy, GEP1Ops, NumGEP1Ops);
819193323Sed    int64_t Offset2 =
820193323Sed      getTargetData().getIndexedOffset(GEPPointerTy, GEP2Ops, NumGEP2Ops);
821193323Sed    assert(Offset1 != Offset2 &&
822193323Sed           "There is at least one different constant here!");
823193323Sed
824193323Sed    // Make sure we compare the absolute difference.
825193323Sed    if (Offset1 > Offset2)
826193323Sed      std::swap(Offset1, Offset2);
827193323Sed
828193323Sed    if ((uint64_t)(Offset2-Offset1) >= SizeMax) {
829193323Sed      //cerr << "Determined that these two GEP's don't alias ["
830193323Sed      //     << SizeMax << " bytes]: \n" << *GEP1 << *GEP2;
831193323Sed      return NoAlias;
832193323Sed    }
833193323Sed  }
834193323Sed  return MayAlias;
835193323Sed}
836193323Sed
837193323Sed// Make sure that anything that uses AliasAnalysis pulls in this file...
838193323SedDEFINING_FILE_FOR(BasicAliasAnalysis)
839