LLVM API Documentation

BasicAliasAnalysis.cpp
Go to the documentation of this file.
00001 //===- BasicAliasAnalysis.cpp - Stateless Alias Analysis Impl -------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the primary stateless implementation of the
00011 // Alias Analysis interface that implements identities (two different
00012 // globals cannot alias, etc), but does no stateful analysis.
00013 //
00014 //===----------------------------------------------------------------------===//
00015 
00016 #include "llvm/Analysis/Passes.h"
00017 #include "llvm/ADT/SmallPtrSet.h"
00018 #include "llvm/ADT/SmallVector.h"
00019 #include "llvm/Analysis/AliasAnalysis.h"
00020 #include "llvm/Analysis/CaptureTracking.h"
00021 #include "llvm/Analysis/InstructionSimplify.h"
00022 #include "llvm/Analysis/MemoryBuiltins.h"
00023 #include "llvm/Analysis/ValueTracking.h"
00024 #include "llvm/IR/Constants.h"
00025 #include "llvm/IR/DataLayout.h"
00026 #include "llvm/IR/DerivedTypes.h"
00027 #include "llvm/IR/Function.h"
00028 #include "llvm/IR/GlobalAlias.h"
00029 #include "llvm/IR/GlobalVariable.h"
00030 #include "llvm/IR/Instructions.h"
00031 #include "llvm/IR/IntrinsicInst.h"
00032 #include "llvm/IR/LLVMContext.h"
00033 #include "llvm/IR/Operator.h"
00034 #include "llvm/Pass.h"
00035 #include "llvm/Support/ErrorHandling.h"
00036 #include "llvm/Support/GetElementPtrTypeIterator.h"
00037 #include "llvm/Target/TargetLibraryInfo.h"
00038 #include <algorithm>
00039 using namespace llvm;
00040 
00041 //===----------------------------------------------------------------------===//
00042 // Useful predicates
00043 //===----------------------------------------------------------------------===//
00044 
00045 /// isNonEscapingLocalObject - Return true if the pointer is to a function-local
00046 /// object that never escapes from the function.
00047 static bool isNonEscapingLocalObject(const Value *V) {
00048   // If this is a local allocation, check to see if it escapes.
00049   if (isa<AllocaInst>(V) || isNoAliasCall(V))
00050     // Set StoreCaptures to True so that we can assume in our callers that the
00051     // pointer is not the result of a load instruction. Currently
00052     // PointerMayBeCaptured doesn't have any special analysis for the
00053     // StoreCaptures=false case; if it did, our callers could be refined to be
00054     // more precise.
00055     return !PointerMayBeCaptured(V, false, /*StoreCaptures=*/true);
00056 
00057   // If this is an argument that corresponds to a byval or noalias argument,
00058   // then it has not escaped before entering the function.  Check if it escapes
00059   // inside the function.
00060   if (const Argument *A = dyn_cast<Argument>(V))
00061     if (A->hasByValAttr() || A->hasNoAliasAttr())
00062       // Note even if the argument is marked nocapture we still need to check
00063       // for copies made inside the function. The nocapture attribute only
00064       // specifies that there are no copies made that outlive the function.
00065       return !PointerMayBeCaptured(V, false, /*StoreCaptures=*/true);
00066 
00067   return false;
00068 }
00069 
00070 /// isEscapeSource - Return true if the pointer is one which would have
00071 /// been considered an escape by isNonEscapingLocalObject.
00072 static bool isEscapeSource(const Value *V) {
00073   if (isa<CallInst>(V) || isa<InvokeInst>(V) || isa<Argument>(V))
00074     return true;
00075 
00076   // The load case works because isNonEscapingLocalObject considers all
00077   // stores to be escapes (it passes true for the StoreCaptures argument
00078   // to PointerMayBeCaptured).
00079   if (isa<LoadInst>(V))
00080     return true;
00081 
00082   return false;
00083 }
00084 
00085 /// getObjectSize - Return the size of the object specified by V, or
00086 /// UnknownSize if unknown.
00087 static uint64_t getObjectSize(const Value *V, const DataLayout &TD,
00088                               const TargetLibraryInfo &TLI,
00089                               bool RoundToAlign = false) {
00090   uint64_t Size;
00091   if (getObjectSize(V, Size, &TD, &TLI, RoundToAlign))
00092     return Size;
00093   return AliasAnalysis::UnknownSize;
00094 }
00095 
00096 /// isObjectSmallerThan - Return true if we can prove that the object specified
00097 /// by V is smaller than Size.
00098 static bool isObjectSmallerThan(const Value *V, uint64_t Size,
00099                                 const DataLayout &TD,
00100                                 const TargetLibraryInfo &TLI) {
00101   // Note that the meanings of the "object" are slightly different in the
00102   // following contexts:
00103   //    c1: llvm::getObjectSize()
00104   //    c2: llvm.objectsize() intrinsic
00105   //    c3: isObjectSmallerThan()
00106   // c1 and c2 share the same meaning; however, the meaning of "object" in c3
00107   // refers to the "entire object".
00108   //
00109   //  Consider this example:
00110   //     char *p = (char*)malloc(100)
00111   //     char *q = p+80;
00112   //
00113   //  In the context of c1 and c2, the "object" pointed by q refers to the
00114   // stretch of memory of q[0:19]. So, getObjectSize(q) should return 20.
00115   //
00116   //  However, in the context of c3, the "object" refers to the chunk of memory
00117   // being allocated. So, the "object" has 100 bytes, and q points to the middle
00118   // the "object". In case q is passed to isObjectSmallerThan() as the 1st
00119   // parameter, before the llvm::getObjectSize() is called to get the size of
00120   // entire object, we should:
00121   //    - either rewind the pointer q to the base-address of the object in
00122   //      question (in this case rewind to p), or
00123   //    - just give up. It is up to caller to make sure the pointer is pointing
00124   //      to the base address the object.
00125   // 
00126   // We go for 2nd option for simplicity.
00127   if (!isIdentifiedObject(V))
00128     return false;
00129 
00130   // This function needs to use the aligned object size because we allow
00131   // reads a bit past the end given sufficient alignment.
00132   uint64_t ObjectSize = getObjectSize(V, TD, TLI, /*RoundToAlign*/true);
00133   
00134   return ObjectSize != AliasAnalysis::UnknownSize && ObjectSize < Size;
00135 }
00136 
00137 /// isObjectSize - Return true if we can prove that the object specified
00138 /// by V has size Size.
00139 static bool isObjectSize(const Value *V, uint64_t Size,
00140                          const DataLayout &TD, const TargetLibraryInfo &TLI) {
00141   uint64_t ObjectSize = getObjectSize(V, TD, TLI);
00142   return ObjectSize != AliasAnalysis::UnknownSize && ObjectSize == Size;
00143 }
00144 
00145 //===----------------------------------------------------------------------===//
00146 // GetElementPtr Instruction Decomposition and Analysis
00147 //===----------------------------------------------------------------------===//
00148 
00149 namespace {
00150   enum ExtensionKind {
00151     EK_NotExtended,
00152     EK_SignExt,
00153     EK_ZeroExt
00154   };
00155   
00156   struct VariableGEPIndex {
00157     const Value *V;
00158     ExtensionKind Extension;
00159     int64_t Scale;
00160 
00161     bool operator==(const VariableGEPIndex &Other) const {
00162       return V == Other.V && Extension == Other.Extension &&
00163         Scale == Other.Scale;
00164     }
00165 
00166     bool operator!=(const VariableGEPIndex &Other) const {
00167       return !operator==(Other);
00168     }
00169   };
00170 }
00171 
00172 
00173 /// GetLinearExpression - Analyze the specified value as a linear expression:
00174 /// "A*V + B", where A and B are constant integers.  Return the scale and offset
00175 /// values as APInts and return V as a Value*, and return whether we looked
00176 /// through any sign or zero extends.  The incoming Value is known to have
00177 /// IntegerType and it may already be sign or zero extended.
00178 ///
00179 /// Note that this looks through extends, so the high bits may not be
00180 /// represented in the result.
00181 static Value *GetLinearExpression(Value *V, APInt &Scale, APInt &Offset,
00182                                   ExtensionKind &Extension,
00183                                   const DataLayout &TD, unsigned Depth) {
00184   assert(V->getType()->isIntegerTy() && "Not an integer value");
00185 
00186   // Limit our recursion depth.
00187   if (Depth == 6) {
00188     Scale = 1;
00189     Offset = 0;
00190     return V;
00191   }
00192   
00193   if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(V)) {
00194     if (ConstantInt *RHSC = dyn_cast<ConstantInt>(BOp->getOperand(1))) {
00195       switch (BOp->getOpcode()) {
00196       default: break;
00197       case Instruction::Or:
00198         // X|C == X+C if all the bits in C are unset in X.  Otherwise we can't
00199         // analyze it.
00200         if (!MaskedValueIsZero(BOp->getOperand(0), RHSC->getValue(), &TD))
00201           break;
00202         // FALL THROUGH.
00203       case Instruction::Add:
00204         V = GetLinearExpression(BOp->getOperand(0), Scale, Offset, Extension,
00205                                 TD, Depth+1);
00206         Offset += RHSC->getValue();
00207         return V;
00208       case Instruction::Mul:
00209         V = GetLinearExpression(BOp->getOperand(0), Scale, Offset, Extension,
00210                                 TD, Depth+1);
00211         Offset *= RHSC->getValue();
00212         Scale *= RHSC->getValue();
00213         return V;
00214       case Instruction::Shl:
00215         V = GetLinearExpression(BOp->getOperand(0), Scale, Offset, Extension,
00216                                 TD, Depth+1);
00217         Offset <<= RHSC->getValue().getLimitedValue();
00218         Scale <<= RHSC->getValue().getLimitedValue();
00219         return V;
00220       }
00221     }
00222   }
00223   
00224   // Since GEP indices are sign extended anyway, we don't care about the high
00225   // bits of a sign or zero extended value - just scales and offsets.  The
00226   // extensions have to be consistent though.
00227   if ((isa<SExtInst>(V) && Extension != EK_ZeroExt) ||
00228       (isa<ZExtInst>(V) && Extension != EK_SignExt)) {
00229     Value *CastOp = cast<CastInst>(V)->getOperand(0);
00230     unsigned OldWidth = Scale.getBitWidth();
00231     unsigned SmallWidth = CastOp->getType()->getPrimitiveSizeInBits();
00232     Scale = Scale.trunc(SmallWidth);
00233     Offset = Offset.trunc(SmallWidth);
00234     Extension = isa<SExtInst>(V) ? EK_SignExt : EK_ZeroExt;
00235 
00236     Value *Result = GetLinearExpression(CastOp, Scale, Offset, Extension,
00237                                         TD, Depth+1);
00238     Scale = Scale.zext(OldWidth);
00239     Offset = Offset.zext(OldWidth);
00240     
00241     return Result;
00242   }
00243   
00244   Scale = 1;
00245   Offset = 0;
00246   return V;
00247 }
00248 
00249 /// DecomposeGEPExpression - If V is a symbolic pointer expression, decompose it
00250 /// into a base pointer with a constant offset and a number of scaled symbolic
00251 /// offsets.
00252 ///
00253 /// The scaled symbolic offsets (represented by pairs of a Value* and a scale in
00254 /// the VarIndices vector) are Value*'s that are known to be scaled by the
00255 /// specified amount, but which may have other unrepresented high bits. As such,
00256 /// the gep cannot necessarily be reconstructed from its decomposed form.
00257 ///
00258 /// When DataLayout is around, this function is capable of analyzing everything
00259 /// that GetUnderlyingObject can look through.  When not, it just looks
00260 /// through pointer casts.
00261 ///
00262 static const Value *
00263 DecomposeGEPExpression(const Value *V, int64_t &BaseOffs,
00264                        SmallVectorImpl<VariableGEPIndex> &VarIndices,
00265                        const DataLayout *TD) {
00266   // Limit recursion depth to limit compile time in crazy cases.
00267   unsigned MaxLookup = 6;
00268   
00269   BaseOffs = 0;
00270   do {
00271     // See if this is a bitcast or GEP.
00272     const Operator *Op = dyn_cast<Operator>(V);
00273     if (Op == 0) {
00274       // The only non-operator case we can handle are GlobalAliases.
00275       if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
00276         if (!GA->mayBeOverridden()) {
00277           V = GA->getAliasee();
00278           continue;
00279         }
00280       }
00281       return V;
00282     }
00283     
00284     if (Op->getOpcode() == Instruction::BitCast) {
00285       V = Op->getOperand(0);
00286       continue;
00287     }
00288 
00289     const GEPOperator *GEPOp = dyn_cast<GEPOperator>(Op);
00290     if (GEPOp == 0) {
00291       // If it's not a GEP, hand it off to SimplifyInstruction to see if it
00292       // can come up with something. This matches what GetUnderlyingObject does.
00293       if (const Instruction *I = dyn_cast<Instruction>(V))
00294         // TODO: Get a DominatorTree and use it here.
00295         if (const Value *Simplified =
00296               SimplifyInstruction(const_cast<Instruction *>(I), TD)) {
00297           V = Simplified;
00298           continue;
00299         }
00300     
00301       return V;
00302     }
00303     
00304     // Don't attempt to analyze GEPs over unsized objects.
00305     if (!cast<PointerType>(GEPOp->getOperand(0)->getType())
00306         ->getElementType()->isSized())
00307       return V;
00308     
00309     // If we are lacking DataLayout information, we can't compute the offets of
00310     // elements computed by GEPs.  However, we can handle bitcast equivalent
00311     // GEPs.
00312     if (TD == 0) {
00313       if (!GEPOp->hasAllZeroIndices())
00314         return V;
00315       V = GEPOp->getOperand(0);
00316       continue;
00317     }
00318     
00319     // Walk the indices of the GEP, accumulating them into BaseOff/VarIndices.
00320     gep_type_iterator GTI = gep_type_begin(GEPOp);
00321     for (User::const_op_iterator I = GEPOp->op_begin()+1,
00322          E = GEPOp->op_end(); I != E; ++I) {
00323       Value *Index = *I;
00324       // Compute the (potentially symbolic) offset in bytes for this index.
00325       if (StructType *STy = dyn_cast<StructType>(*GTI++)) {
00326         // For a struct, add the member offset.
00327         unsigned FieldNo = cast<ConstantInt>(Index)->getZExtValue();
00328         if (FieldNo == 0) continue;
00329         
00330         BaseOffs += TD->getStructLayout(STy)->getElementOffset(FieldNo);
00331         continue;
00332       }
00333       
00334       // For an array/pointer, add the element offset, explicitly scaled.
00335       if (ConstantInt *CIdx = dyn_cast<ConstantInt>(Index)) {
00336         if (CIdx->isZero()) continue;
00337         BaseOffs += TD->getTypeAllocSize(*GTI)*CIdx->getSExtValue();
00338         continue;
00339       }
00340       
00341       uint64_t Scale = TD->getTypeAllocSize(*GTI);
00342       ExtensionKind Extension = EK_NotExtended;
00343       
00344       // If the integer type is smaller than the pointer size, it is implicitly
00345       // sign extended to pointer size.
00346       unsigned Width = cast<IntegerType>(Index->getType())->getBitWidth();
00347       if (TD->getPointerSizeInBits() > Width)
00348         Extension = EK_SignExt;
00349       
00350       // Use GetLinearExpression to decompose the index into a C1*V+C2 form.
00351       APInt IndexScale(Width, 0), IndexOffset(Width, 0);
00352       Index = GetLinearExpression(Index, IndexScale, IndexOffset, Extension,
00353                                   *TD, 0);
00354       
00355       // The GEP index scale ("Scale") scales C1*V+C2, yielding (C1*V+C2)*Scale.
00356       // This gives us an aggregate computation of (C1*Scale)*V + C2*Scale.
00357       BaseOffs += IndexOffset.getSExtValue()*Scale;
00358       Scale *= IndexScale.getSExtValue();
00359       
00360       
00361       // If we already had an occurrence of this index variable, merge this
00362       // scale into it.  For example, we want to handle:
00363       //   A[x][x] -> x*16 + x*4 -> x*20
00364       // This also ensures that 'x' only appears in the index list once.
00365       for (unsigned i = 0, e = VarIndices.size(); i != e; ++i) {
00366         if (VarIndices[i].V == Index &&
00367             VarIndices[i].Extension == Extension) {
00368           Scale += VarIndices[i].Scale;
00369           VarIndices.erase(VarIndices.begin()+i);
00370           break;
00371         }
00372       }
00373       
00374       // Make sure that we have a scale that makes sense for this target's
00375       // pointer size.
00376       if (unsigned ShiftBits = 64-TD->getPointerSizeInBits()) {
00377         Scale <<= ShiftBits;
00378         Scale = (int64_t)Scale >> ShiftBits;
00379       }
00380       
00381       if (Scale) {
00382         VariableGEPIndex Entry = {Index, Extension,
00383                                   static_cast<int64_t>(Scale)};
00384         VarIndices.push_back(Entry);
00385       }
00386     }
00387     
00388     // Analyze the base pointer next.
00389     V = GEPOp->getOperand(0);
00390   } while (--MaxLookup);
00391   
00392   // If the chain of expressions is too deep, just return early.
00393   return V;
00394 }
00395 
00396 /// GetIndexDifference - Dest and Src are the variable indices from two
00397 /// decomposed GetElementPtr instructions GEP1 and GEP2 which have common base
00398 /// pointers.  Subtract the GEP2 indices from GEP1 to find the symbolic
00399 /// difference between the two pointers. 
00400 static void GetIndexDifference(SmallVectorImpl<VariableGEPIndex> &Dest,
00401                                const SmallVectorImpl<VariableGEPIndex> &Src) {
00402   if (Src.empty()) return;
00403 
00404   for (unsigned i = 0, e = Src.size(); i != e; ++i) {
00405     const Value *V = Src[i].V;
00406     ExtensionKind Extension = Src[i].Extension;
00407     int64_t Scale = Src[i].Scale;
00408     
00409     // Find V in Dest.  This is N^2, but pointer indices almost never have more
00410     // than a few variable indexes.
00411     for (unsigned j = 0, e = Dest.size(); j != e; ++j) {
00412       if (Dest[j].V != V || Dest[j].Extension != Extension) continue;
00413       
00414       // If we found it, subtract off Scale V's from the entry in Dest.  If it
00415       // goes to zero, remove the entry.
00416       if (Dest[j].Scale != Scale)
00417         Dest[j].Scale -= Scale;
00418       else
00419         Dest.erase(Dest.begin()+j);
00420       Scale = 0;
00421       break;
00422     }
00423     
00424     // If we didn't consume this entry, add it to the end of the Dest list.
00425     if (Scale) {
00426       VariableGEPIndex Entry = { V, Extension, -Scale };
00427       Dest.push_back(Entry);
00428     }
00429   }
00430 }
00431 
00432 //===----------------------------------------------------------------------===//
00433 // BasicAliasAnalysis Pass
00434 //===----------------------------------------------------------------------===//
00435 
00436 #ifndef NDEBUG
00437 static const Function *getParent(const Value *V) {
00438   if (const Instruction *inst = dyn_cast<Instruction>(V))
00439     return inst->getParent()->getParent();
00440 
00441   if (const Argument *arg = dyn_cast<Argument>(V))
00442     return arg->getParent();
00443 
00444   return NULL;
00445 }
00446 
00447 static bool notDifferentParent(const Value *O1, const Value *O2) {
00448 
00449   const Function *F1 = getParent(O1);
00450   const Function *F2 = getParent(O2);
00451 
00452   return !F1 || !F2 || F1 == F2;
00453 }
00454 #endif
00455 
00456 namespace {
00457   /// BasicAliasAnalysis - This is the primary alias analysis implementation.
00458   struct BasicAliasAnalysis : public ImmutablePass, public AliasAnalysis {
00459     static char ID; // Class identification, replacement for typeinfo
00460     BasicAliasAnalysis() : ImmutablePass(ID) {
00461       initializeBasicAliasAnalysisPass(*PassRegistry::getPassRegistry());
00462     }
00463 
00464     virtual void initializePass() {
00465       InitializeAliasAnalysis(this);
00466     }
00467 
00468     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
00469       AU.addRequired<AliasAnalysis>();
00470       AU.addRequired<TargetLibraryInfo>();
00471     }
00472 
00473     virtual AliasResult alias(const Location &LocA,
00474                               const Location &LocB) {
00475       assert(AliasCache.empty() && "AliasCache must be cleared after use!");
00476       assert(notDifferentParent(LocA.Ptr, LocB.Ptr) &&
00477              "BasicAliasAnalysis doesn't support interprocedural queries.");
00478       AliasResult Alias = aliasCheck(LocA.Ptr, LocA.Size, LocA.TBAATag,
00479                                      LocB.Ptr, LocB.Size, LocB.TBAATag);
00480       // AliasCache rarely has more than 1 or 2 elements, always use
00481       // shrink_and_clear so it quickly returns to the inline capacity of the
00482       // SmallDenseMap if it ever grows larger.
00483       // FIXME: This should really be shrink_to_inline_capacity_and_clear().
00484       AliasCache.shrink_and_clear();
00485       return Alias;
00486     }
00487 
00488     virtual ModRefResult getModRefInfo(ImmutableCallSite CS,
00489                                        const Location &Loc);
00490 
00491     virtual ModRefResult getModRefInfo(ImmutableCallSite CS1,
00492                                        ImmutableCallSite CS2) {
00493       // The AliasAnalysis base class has some smarts, lets use them.
00494       return AliasAnalysis::getModRefInfo(CS1, CS2);
00495     }
00496 
00497     /// pointsToConstantMemory - Chase pointers until we find a (constant
00498     /// global) or not.
00499     virtual bool pointsToConstantMemory(const Location &Loc, bool OrLocal);
00500 
00501     /// getModRefBehavior - Return the behavior when calling the given
00502     /// call site.
00503     virtual ModRefBehavior getModRefBehavior(ImmutableCallSite CS);
00504 
00505     /// getModRefBehavior - Return the behavior when calling the given function.
00506     /// For use when the call site is not known.
00507     virtual ModRefBehavior getModRefBehavior(const Function *F);
00508 
00509     /// getAdjustedAnalysisPointer - This method is used when a pass implements
00510     /// an analysis interface through multiple inheritance.  If needed, it
00511     /// should override this to adjust the this pointer as needed for the
00512     /// specified pass info.
00513     virtual void *getAdjustedAnalysisPointer(const void *ID) {
00514       if (ID == &AliasAnalysis::ID)
00515         return (AliasAnalysis*)this;
00516       return this;
00517     }
00518     
00519   private:
00520     // AliasCache - Track alias queries to guard against recursion.
00521     typedef std::pair<Location, Location> LocPair;
00522     typedef SmallDenseMap<LocPair, AliasResult, 8> AliasCacheTy;
00523     AliasCacheTy AliasCache;
00524 
00525     // Visited - Track instructions visited by pointsToConstantMemory.
00526     SmallPtrSet<const Value*, 16> Visited;
00527 
00528     // aliasGEP - Provide a bunch of ad-hoc rules to disambiguate a GEP
00529     // instruction against another.
00530     AliasResult aliasGEP(const GEPOperator *V1, uint64_t V1Size,
00531                          const MDNode *V1TBAAInfo,
00532                          const Value *V2, uint64_t V2Size,
00533                          const MDNode *V2TBAAInfo,
00534                          const Value *UnderlyingV1, const Value *UnderlyingV2);
00535 
00536     // aliasPHI - Provide a bunch of ad-hoc rules to disambiguate a PHI
00537     // instruction against another.
00538     AliasResult aliasPHI(const PHINode *PN, uint64_t PNSize,
00539                          const MDNode *PNTBAAInfo,
00540                          const Value *V2, uint64_t V2Size,
00541                          const MDNode *V2TBAAInfo);
00542 
00543     /// aliasSelect - Disambiguate a Select instruction against another value.
00544     AliasResult aliasSelect(const SelectInst *SI, uint64_t SISize,
00545                             const MDNode *SITBAAInfo,
00546                             const Value *V2, uint64_t V2Size,
00547                             const MDNode *V2TBAAInfo);
00548 
00549     AliasResult aliasCheck(const Value *V1, uint64_t V1Size,
00550                            const MDNode *V1TBAATag,
00551                            const Value *V2, uint64_t V2Size,
00552                            const MDNode *V2TBAATag);
00553   };
00554 }  // End of anonymous namespace
00555 
00556 // Register this pass...
00557 char BasicAliasAnalysis::ID = 0;
00558 INITIALIZE_AG_PASS_BEGIN(BasicAliasAnalysis, AliasAnalysis, "basicaa",
00559                    "Basic Alias Analysis (stateless AA impl)",
00560                    false, true, false)
00561 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
00562 INITIALIZE_AG_PASS_END(BasicAliasAnalysis, AliasAnalysis, "basicaa",
00563                    "Basic Alias Analysis (stateless AA impl)",
00564                    false, true, false)
00565 
00566 
00567 ImmutablePass *llvm::createBasicAliasAnalysisPass() {
00568   return new BasicAliasAnalysis();
00569 }
00570 
00571 /// pointsToConstantMemory - Returns whether the given pointer value
00572 /// points to memory that is local to the function, with global constants being
00573 /// considered local to all functions.
00574 bool
00575 BasicAliasAnalysis::pointsToConstantMemory(const Location &Loc, bool OrLocal) {
00576   assert(Visited.empty() && "Visited must be cleared after use!");
00577 
00578   unsigned MaxLookup = 8;
00579   SmallVector<const Value *, 16> Worklist;
00580   Worklist.push_back(Loc.Ptr);
00581   do {
00582     const Value *V = GetUnderlyingObject(Worklist.pop_back_val(), TD);
00583     if (!Visited.insert(V)) {
00584       Visited.clear();
00585       return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
00586     }
00587 
00588     // An alloca instruction defines local memory.
00589     if (OrLocal && isa<AllocaInst>(V))
00590       continue;
00591 
00592     // A global constant counts as local memory for our purposes.
00593     if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
00594       // Note: this doesn't require GV to be "ODR" because it isn't legal for a
00595       // global to be marked constant in some modules and non-constant in
00596       // others.  GV may even be a declaration, not a definition.
00597       if (!GV->isConstant()) {
00598         Visited.clear();
00599         return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
00600       }
00601       continue;
00602     }
00603 
00604     // If both select values point to local memory, then so does the select.
00605     if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {
00606       Worklist.push_back(SI->getTrueValue());
00607       Worklist.push_back(SI->getFalseValue());
00608       continue;
00609     }
00610 
00611     // If all values incoming to a phi node point to local memory, then so does
00612     // the phi.
00613     if (const PHINode *PN = dyn_cast<PHINode>(V)) {
00614       // Don't bother inspecting phi nodes with many operands.
00615       if (PN->getNumIncomingValues() > MaxLookup) {
00616         Visited.clear();
00617         return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
00618       }
00619       for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
00620         Worklist.push_back(PN->getIncomingValue(i));
00621       continue;
00622     }
00623 
00624     // Otherwise be conservative.
00625     Visited.clear();
00626     return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
00627 
00628   } while (!Worklist.empty() && --MaxLookup);
00629 
00630   Visited.clear();
00631   return Worklist.empty();
00632 }
00633 
00634 /// getModRefBehavior - Return the behavior when calling the given call site.
00635 AliasAnalysis::ModRefBehavior
00636 BasicAliasAnalysis::getModRefBehavior(ImmutableCallSite CS) {
00637   if (CS.doesNotAccessMemory())
00638     // Can't do better than this.
00639     return DoesNotAccessMemory;
00640 
00641   ModRefBehavior Min = UnknownModRefBehavior;
00642 
00643   // If the callsite knows it only reads memory, don't return worse
00644   // than that.
00645   if (CS.onlyReadsMemory())
00646     Min = OnlyReadsMemory;
00647 
00648   // The AliasAnalysis base class has some smarts, lets use them.
00649   return ModRefBehavior(AliasAnalysis::getModRefBehavior(CS) & Min);
00650 }
00651 
00652 /// getModRefBehavior - Return the behavior when calling the given function.
00653 /// For use when the call site is not known.
00654 AliasAnalysis::ModRefBehavior
00655 BasicAliasAnalysis::getModRefBehavior(const Function *F) {
00656   // If the function declares it doesn't access memory, we can't do better.
00657   if (F->doesNotAccessMemory())
00658     return DoesNotAccessMemory;
00659 
00660   // For intrinsics, we can check the table.
00661   if (unsigned iid = F->getIntrinsicID()) {
00662 #define GET_INTRINSIC_MODREF_BEHAVIOR
00663 #include "llvm/IR/Intrinsics.gen"
00664 #undef GET_INTRINSIC_MODREF_BEHAVIOR
00665   }
00666 
00667   ModRefBehavior Min = UnknownModRefBehavior;
00668 
00669   // If the function declares it only reads memory, go with that.
00670   if (F->onlyReadsMemory())
00671     Min = OnlyReadsMemory;
00672 
00673   // Otherwise be conservative.
00674   return ModRefBehavior(AliasAnalysis::getModRefBehavior(F) & Min);
00675 }
00676 
00677 /// getModRefInfo - Check to see if the specified callsite can clobber the
00678 /// specified memory object.  Since we only look at local properties of this
00679 /// function, we really can't say much about this query.  We do, however, use
00680 /// simple "address taken" analysis on local objects.
00681 AliasAnalysis::ModRefResult
00682 BasicAliasAnalysis::getModRefInfo(ImmutableCallSite CS,
00683                                   const Location &Loc) {
00684   assert(notDifferentParent(CS.getInstruction(), Loc.Ptr) &&
00685          "AliasAnalysis query involving multiple functions!");
00686 
00687   const Value *Object = GetUnderlyingObject(Loc.Ptr, TD);
00688   
00689   // If this is a tail call and Loc.Ptr points to a stack location, we know that
00690   // the tail call cannot access or modify the local stack.
00691   // We cannot exclude byval arguments here; these belong to the caller of
00692   // the current function not to the current function, and a tail callee
00693   // may reference them.
00694   if (isa<AllocaInst>(Object))
00695     if (const CallInst *CI = dyn_cast<CallInst>(CS.getInstruction()))
00696       if (CI->isTailCall())
00697         return NoModRef;
00698   
00699   // If the pointer is to a locally allocated object that does not escape,
00700   // then the call can not mod/ref the pointer unless the call takes the pointer
00701   // as an argument, and itself doesn't capture it.
00702   if (!isa<Constant>(Object) && CS.getInstruction() != Object &&
00703       isNonEscapingLocalObject(Object)) {
00704     bool PassedAsArg = false;
00705     unsigned ArgNo = 0;
00706     for (ImmutableCallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
00707          CI != CE; ++CI, ++ArgNo) {
00708       // Only look at the no-capture or byval pointer arguments.  If this
00709       // pointer were passed to arguments that were neither of these, then it
00710       // couldn't be no-capture.
00711       if (!(*CI)->getType()->isPointerTy() ||
00712           (!CS.doesNotCapture(ArgNo) && !CS.isByValArgument(ArgNo)))
00713         continue;
00714       
00715       // If this is a no-capture pointer argument, see if we can tell that it
00716       // is impossible to alias the pointer we're checking.  If not, we have to
00717       // assume that the call could touch the pointer, even though it doesn't
00718       // escape.
00719       if (!isNoAlias(Location(*CI), Location(Object))) {
00720         PassedAsArg = true;
00721         break;
00722       }
00723     }
00724     
00725     if (!PassedAsArg)
00726       return NoModRef;
00727   }
00728 
00729   const TargetLibraryInfo &TLI = getAnalysis<TargetLibraryInfo>();
00730   ModRefResult Min = ModRef;
00731 
00732   // Finally, handle specific knowledge of intrinsics.
00733   const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CS.getInstruction());
00734   if (II != 0)
00735     switch (II->getIntrinsicID()) {
00736     default: break;
00737     case Intrinsic::memcpy:
00738     case Intrinsic::memmove: {
00739       uint64_t Len = UnknownSize;
00740       if (ConstantInt *LenCI = dyn_cast<ConstantInt>(II->getArgOperand(2)))
00741         Len = LenCI->getZExtValue();
00742       Value *Dest = II->getArgOperand(0);
00743       Value *Src = II->getArgOperand(1);
00744       // If it can't overlap the source dest, then it doesn't modref the loc.
00745       if (isNoAlias(Location(Dest, Len), Loc)) {
00746         if (isNoAlias(Location(Src, Len), Loc))
00747           return NoModRef;
00748         // If it can't overlap the dest, then worst case it reads the loc.
00749         Min = Ref;
00750       } else if (isNoAlias(Location(Src, Len), Loc)) {
00751         // If it can't overlap the source, then worst case it mutates the loc.
00752         Min = Mod;
00753       }
00754       break;
00755     }
00756     case Intrinsic::memset:
00757       // Since memset is 'accesses arguments' only, the AliasAnalysis base class
00758       // will handle it for the variable length case.
00759       if (ConstantInt *LenCI = dyn_cast<ConstantInt>(II->getArgOperand(2))) {
00760         uint64_t Len = LenCI->getZExtValue();
00761         Value *Dest = II->getArgOperand(0);
00762         if (isNoAlias(Location(Dest, Len), Loc))
00763           return NoModRef;
00764       }
00765       // We know that memset doesn't load anything.
00766       Min = Mod;
00767       break;
00768     case Intrinsic::lifetime_start:
00769     case Intrinsic::lifetime_end:
00770     case Intrinsic::invariant_start: {
00771       uint64_t PtrSize =
00772         cast<ConstantInt>(II->getArgOperand(0))->getZExtValue();
00773       if (isNoAlias(Location(II->getArgOperand(1),
00774                              PtrSize,
00775                              II->getMetadata(LLVMContext::MD_tbaa)),
00776                     Loc))
00777         return NoModRef;
00778       break;
00779     }
00780     case Intrinsic::invariant_end: {
00781       uint64_t PtrSize =
00782         cast<ConstantInt>(II->getArgOperand(1))->getZExtValue();
00783       if (isNoAlias(Location(II->getArgOperand(2),
00784                              PtrSize,
00785                              II->getMetadata(LLVMContext::MD_tbaa)),
00786                     Loc))
00787         return NoModRef;
00788       break;
00789     }
00790     case Intrinsic::arm_neon_vld1: {
00791       // LLVM's vld1 and vst1 intrinsics currently only support a single
00792       // vector register.
00793       uint64_t Size =
00794         TD ? TD->getTypeStoreSize(II->getType()) : UnknownSize;
00795       if (isNoAlias(Location(II->getArgOperand(0), Size,
00796                              II->getMetadata(LLVMContext::MD_tbaa)),
00797                     Loc))
00798         return NoModRef;
00799       break;
00800     }
00801     case Intrinsic::arm_neon_vst1: {
00802       uint64_t Size =
00803         TD ? TD->getTypeStoreSize(II->getArgOperand(1)->getType()) : UnknownSize;
00804       if (isNoAlias(Location(II->getArgOperand(0), Size,
00805                              II->getMetadata(LLVMContext::MD_tbaa)),
00806                     Loc))
00807         return NoModRef;
00808       break;
00809     }
00810     }
00811 
00812   // We can bound the aliasing properties of memset_pattern16 just as we can
00813   // for memcpy/memset.  This is particularly important because the 
00814   // LoopIdiomRecognizer likes to turn loops into calls to memset_pattern16
00815   // whenever possible.
00816   else if (TLI.has(LibFunc::memset_pattern16) &&
00817            CS.getCalledFunction() &&
00818            CS.getCalledFunction()->getName() == "memset_pattern16") {
00819     const Function *MS = CS.getCalledFunction();
00820     FunctionType *MemsetType = MS->getFunctionType();
00821     if (!MemsetType->isVarArg() && MemsetType->getNumParams() == 3 &&
00822         isa<PointerType>(MemsetType->getParamType(0)) &&
00823         isa<PointerType>(MemsetType->getParamType(1)) &&
00824         isa<IntegerType>(MemsetType->getParamType(2))) {
00825       uint64_t Len = UnknownSize;
00826       if (const ConstantInt *LenCI = dyn_cast<ConstantInt>(CS.getArgument(2)))
00827         Len = LenCI->getZExtValue();
00828       const Value *Dest = CS.getArgument(0);
00829       const Value *Src = CS.getArgument(1);
00830       // If it can't overlap the source dest, then it doesn't modref the loc.
00831       if (isNoAlias(Location(Dest, Len), Loc)) {
00832         // Always reads 16 bytes of the source.
00833         if (isNoAlias(Location(Src, 16), Loc))
00834           return NoModRef;
00835         // If it can't overlap the dest, then worst case it reads the loc.
00836         Min = Ref;
00837       // Always reads 16 bytes of the source.
00838       } else if (isNoAlias(Location(Src, 16), Loc)) {
00839         // If it can't overlap the source, then worst case it mutates the loc.
00840         Min = Mod;
00841       }
00842     }
00843   }
00844 
00845   // The AliasAnalysis base class has some smarts, lets use them.
00846   return ModRefResult(AliasAnalysis::getModRefInfo(CS, Loc) & Min);
00847 }
00848 
00849 static bool areVarIndicesEqual(SmallVector<VariableGEPIndex, 4> &Indices1,
00850                                SmallVector<VariableGEPIndex, 4> &Indices2) {
00851   unsigned Size1 = Indices1.size();
00852   unsigned Size2 = Indices2.size();
00853 
00854   if (Size1 != Size2)
00855     return false;
00856 
00857   for (unsigned I = 0; I != Size1; ++I)
00858     if (Indices1[I] != Indices2[I])
00859       return false;
00860 
00861   return true;
00862 }
00863 
00864 /// aliasGEP - Provide a bunch of ad-hoc rules to disambiguate a GEP instruction
00865 /// against another pointer.  We know that V1 is a GEP, but we don't know
00866 /// anything about V2.  UnderlyingV1 is GetUnderlyingObject(GEP1, TD),
00867 /// UnderlyingV2 is the same for V2.
00868 ///
00869 AliasAnalysis::AliasResult
00870 BasicAliasAnalysis::aliasGEP(const GEPOperator *GEP1, uint64_t V1Size,
00871                              const MDNode *V1TBAAInfo,
00872                              const Value *V2, uint64_t V2Size,
00873                              const MDNode *V2TBAAInfo,
00874                              const Value *UnderlyingV1,
00875                              const Value *UnderlyingV2) {
00876   int64_t GEP1BaseOffset;
00877   SmallVector<VariableGEPIndex, 4> GEP1VariableIndices;
00878 
00879   // If we have two gep instructions with must-alias or not-alias'ing base
00880   // pointers, figure out if the indexes to the GEP tell us anything about the
00881   // derived pointer.
00882   if (const GEPOperator *GEP2 = dyn_cast<GEPOperator>(V2)) {
00883     // Do the base pointers alias?
00884     AliasResult BaseAlias = aliasCheck(UnderlyingV1, UnknownSize, 0,
00885                                        UnderlyingV2, UnknownSize, 0);
00886 
00887     // Check for geps of non-aliasing underlying pointers where the offsets are
00888     // identical.
00889     if ((BaseAlias == MayAlias) && V1Size == V2Size) {
00890       // Do the base pointers alias assuming type and size.
00891       AliasResult PreciseBaseAlias = aliasCheck(UnderlyingV1, V1Size,
00892                                                 V1TBAAInfo, UnderlyingV2,
00893                                                 V2Size, V2TBAAInfo);
00894       if (PreciseBaseAlias == NoAlias) {
00895         // See if the computed offset from the common pointer tells us about the
00896         // relation of the resulting pointer.
00897         int64_t GEP2BaseOffset;
00898         SmallVector<VariableGEPIndex, 4> GEP2VariableIndices;
00899         const Value *GEP2BasePtr =
00900           DecomposeGEPExpression(GEP2, GEP2BaseOffset, GEP2VariableIndices, TD);
00901         const Value *GEP1BasePtr =
00902           DecomposeGEPExpression(GEP1, GEP1BaseOffset, GEP1VariableIndices, TD);
00903         // DecomposeGEPExpression and GetUnderlyingObject should return the
00904         // same result except when DecomposeGEPExpression has no DataLayout.
00905         if (GEP1BasePtr != UnderlyingV1 || GEP2BasePtr != UnderlyingV2) {
00906           assert(TD == 0 &&
00907              "DecomposeGEPExpression and GetUnderlyingObject disagree!");
00908           return MayAlias;
00909         }
00910         // Same offsets.
00911         if (GEP1BaseOffset == GEP2BaseOffset &&
00912             areVarIndicesEqual(GEP1VariableIndices, GEP2VariableIndices))
00913           return NoAlias;
00914         GEP1VariableIndices.clear();
00915       }
00916     }
00917     
00918     // If we get a No or May, then return it immediately, no amount of analysis
00919     // will improve this situation.
00920     if (BaseAlias != MustAlias) return BaseAlias;
00921     
00922     // Otherwise, we have a MustAlias.  Since the base pointers alias each other
00923     // exactly, see if the computed offset from the common pointer tells us
00924     // about the relation of the resulting pointer.
00925     const Value *GEP1BasePtr =
00926       DecomposeGEPExpression(GEP1, GEP1BaseOffset, GEP1VariableIndices, TD);
00927     
00928     int64_t GEP2BaseOffset;
00929     SmallVector<VariableGEPIndex, 4> GEP2VariableIndices;
00930     const Value *GEP2BasePtr =
00931       DecomposeGEPExpression(GEP2, GEP2BaseOffset, GEP2VariableIndices, TD);
00932     
00933     // DecomposeGEPExpression and GetUnderlyingObject should return the
00934     // same result except when DecomposeGEPExpression has no DataLayout.
00935     if (GEP1BasePtr != UnderlyingV1 || GEP2BasePtr != UnderlyingV2) {
00936       assert(TD == 0 &&
00937              "DecomposeGEPExpression and GetUnderlyingObject disagree!");
00938       return MayAlias;
00939     }
00940     
00941     // Subtract the GEP2 pointer from the GEP1 pointer to find out their
00942     // symbolic difference.
00943     GEP1BaseOffset -= GEP2BaseOffset;
00944     GetIndexDifference(GEP1VariableIndices, GEP2VariableIndices);
00945     
00946   } else {
00947     // Check to see if these two pointers are related by the getelementptr
00948     // instruction.  If one pointer is a GEP with a non-zero index of the other
00949     // pointer, we know they cannot alias.
00950 
00951     // If both accesses are unknown size, we can't do anything useful here.
00952     if (V1Size == UnknownSize && V2Size == UnknownSize)
00953       return MayAlias;
00954 
00955     AliasResult R = aliasCheck(UnderlyingV1, UnknownSize, 0,
00956                                V2, V2Size, V2TBAAInfo);
00957     if (R != MustAlias)
00958       // If V2 may alias GEP base pointer, conservatively returns MayAlias.
00959       // If V2 is known not to alias GEP base pointer, then the two values
00960       // cannot alias per GEP semantics: "A pointer value formed from a
00961       // getelementptr instruction is associated with the addresses associated
00962       // with the first operand of the getelementptr".
00963       return R;
00964 
00965     const Value *GEP1BasePtr =
00966       DecomposeGEPExpression(GEP1, GEP1BaseOffset, GEP1VariableIndices, TD);
00967     
00968     // DecomposeGEPExpression and GetUnderlyingObject should return the
00969     // same result except when DecomposeGEPExpression has no DataLayout.
00970     if (GEP1BasePtr != UnderlyingV1) {
00971       assert(TD == 0 &&
00972              "DecomposeGEPExpression and GetUnderlyingObject disagree!");
00973       return MayAlias;
00974     }
00975   }
00976   
00977   // In the two GEP Case, if there is no difference in the offsets of the
00978   // computed pointers, the resultant pointers are a must alias.  This
00979   // hapens when we have two lexically identical GEP's (for example).
00980   //
00981   // In the other case, if we have getelementptr <ptr>, 0, 0, 0, 0, ... and V2
00982   // must aliases the GEP, the end result is a must alias also.
00983   if (GEP1BaseOffset == 0 && GEP1VariableIndices.empty())
00984     return MustAlias;
00985 
00986   // If there is a constant difference between the pointers, but the difference
00987   // is less than the size of the associated memory object, then we know
00988   // that the objects are partially overlapping.  If the difference is
00989   // greater, we know they do not overlap.
00990   if (GEP1BaseOffset != 0 && GEP1VariableIndices.empty()) {
00991     if (GEP1BaseOffset >= 0) {
00992       if (V2Size != UnknownSize) {
00993         if ((uint64_t)GEP1BaseOffset < V2Size)
00994           return PartialAlias;
00995         return NoAlias;
00996       }
00997     } else {
00998       if (V1Size != UnknownSize) {
00999         if (-(uint64_t)GEP1BaseOffset < V1Size)
01000           return PartialAlias;
01001         return NoAlias;
01002       }
01003     }
01004   }
01005 
01006   // Try to distinguish something like &A[i][1] against &A[42][0].
01007   // Grab the least significant bit set in any of the scales.
01008   if (!GEP1VariableIndices.empty()) {
01009     uint64_t Modulo = 0;
01010     for (unsigned i = 0, e = GEP1VariableIndices.size(); i != e; ++i)
01011       Modulo |= (uint64_t)GEP1VariableIndices[i].Scale;
01012     Modulo = Modulo ^ (Modulo & (Modulo - 1));
01013 
01014     // We can compute the difference between the two addresses
01015     // mod Modulo. Check whether that difference guarantees that the
01016     // two locations do not alias.
01017     uint64_t ModOffset = (uint64_t)GEP1BaseOffset & (Modulo - 1);
01018     if (V1Size != UnknownSize && V2Size != UnknownSize &&
01019         ModOffset >= V2Size && V1Size <= Modulo - ModOffset)
01020       return NoAlias;
01021   }
01022 
01023   // Statically, we can see that the base objects are the same, but the
01024   // pointers have dynamic offsets which we can't resolve. And none of our
01025   // little tricks above worked.
01026   //
01027   // TODO: Returning PartialAlias instead of MayAlias is a mild hack; the
01028   // practical effect of this is protecting TBAA in the case of dynamic
01029   // indices into arrays of unions or malloc'd memory.
01030   return PartialAlias;
01031 }
01032 
01033 static AliasAnalysis::AliasResult
01034 MergeAliasResults(AliasAnalysis::AliasResult A, AliasAnalysis::AliasResult B) {
01035   // If the results agree, take it.
01036   if (A == B)
01037     return A;
01038   // A mix of PartialAlias and MustAlias is PartialAlias.
01039   if ((A == AliasAnalysis::PartialAlias && B == AliasAnalysis::MustAlias) ||
01040       (B == AliasAnalysis::PartialAlias && A == AliasAnalysis::MustAlias))
01041     return AliasAnalysis::PartialAlias;
01042   // Otherwise, we don't know anything.
01043   return AliasAnalysis::MayAlias;
01044 }
01045 
01046 /// aliasSelect - Provide a bunch of ad-hoc rules to disambiguate a Select
01047 /// instruction against another.
01048 AliasAnalysis::AliasResult
01049 BasicAliasAnalysis::aliasSelect(const SelectInst *SI, uint64_t SISize,
01050                                 const MDNode *SITBAAInfo,
01051                                 const Value *V2, uint64_t V2Size,
01052                                 const MDNode *V2TBAAInfo) {
01053   // If the values are Selects with the same condition, we can do a more precise
01054   // check: just check for aliases between the values on corresponding arms.
01055   if (const SelectInst *SI2 = dyn_cast<SelectInst>(V2))
01056     if (SI->getCondition() == SI2->getCondition()) {
01057       AliasResult Alias =
01058         aliasCheck(SI->getTrueValue(), SISize, SITBAAInfo,
01059                    SI2->getTrueValue(), V2Size, V2TBAAInfo);
01060       if (Alias == MayAlias)
01061         return MayAlias;
01062       AliasResult ThisAlias =
01063         aliasCheck(SI->getFalseValue(), SISize, SITBAAInfo,
01064                    SI2->getFalseValue(), V2Size, V2TBAAInfo);
01065       return MergeAliasResults(ThisAlias, Alias);
01066     }
01067 
01068   // If both arms of the Select node NoAlias or MustAlias V2, then returns
01069   // NoAlias / MustAlias. Otherwise, returns MayAlias.
01070   AliasResult Alias =
01071     aliasCheck(V2, V2Size, V2TBAAInfo, SI->getTrueValue(), SISize, SITBAAInfo);
01072   if (Alias == MayAlias)
01073     return MayAlias;
01074 
01075   AliasResult ThisAlias =
01076     aliasCheck(V2, V2Size, V2TBAAInfo, SI->getFalseValue(), SISize, SITBAAInfo);
01077   return MergeAliasResults(ThisAlias, Alias);
01078 }
01079 
01080 // aliasPHI - Provide a bunch of ad-hoc rules to disambiguate a PHI instruction
01081 // against another.
01082 AliasAnalysis::AliasResult
01083 BasicAliasAnalysis::aliasPHI(const PHINode *PN, uint64_t PNSize,
01084                              const MDNode *PNTBAAInfo,
01085                              const Value *V2, uint64_t V2Size,
01086                              const MDNode *V2TBAAInfo) {
01087   // If the values are PHIs in the same block, we can do a more precise
01088   // as well as efficient check: just check for aliases between the values
01089   // on corresponding edges.
01090   if (const PHINode *PN2 = dyn_cast<PHINode>(V2))
01091     if (PN2->getParent() == PN->getParent()) {
01092       LocPair Locs(Location(PN, PNSize, PNTBAAInfo),
01093                    Location(V2, V2Size, V2TBAAInfo));
01094       if (PN > V2)
01095         std::swap(Locs.first, Locs.second);
01096       // Analyse the PHIs' inputs under the assumption that the PHIs are
01097       // NoAlias.
01098       // If the PHIs are May/MustAlias there must be (recursively) an input
01099       // operand from outside the PHIs' cycle that is MayAlias/MustAlias or
01100       // there must be an operation on the PHIs within the PHIs' value cycle
01101       // that causes a MayAlias.
01102       // Pretend the phis do not alias.
01103       AliasResult Alias = NoAlias;
01104       assert(AliasCache.count(Locs) &&
01105              "There must exist an entry for the phi node");
01106       AliasResult OrigAliasResult = AliasCache[Locs];
01107       AliasCache[Locs] = NoAlias;
01108 
01109       for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
01110         AliasResult ThisAlias =
01111           aliasCheck(PN->getIncomingValue(i), PNSize, PNTBAAInfo,
01112                      PN2->getIncomingValueForBlock(PN->getIncomingBlock(i)),
01113                      V2Size, V2TBAAInfo);
01114         Alias = MergeAliasResults(ThisAlias, Alias);
01115         if (Alias == MayAlias)
01116           break;
01117       }
01118 
01119       // Reset if speculation failed.
01120       if (Alias != NoAlias)
01121         AliasCache[Locs] = OrigAliasResult;
01122 
01123       return Alias;
01124     }
01125 
01126   SmallPtrSet<Value*, 4> UniqueSrc;
01127   SmallVector<Value*, 4> V1Srcs;
01128   for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
01129     Value *PV1 = PN->getIncomingValue(i);
01130     if (isa<PHINode>(PV1))
01131       // If any of the source itself is a PHI, return MayAlias conservatively
01132       // to avoid compile time explosion. The worst possible case is if both
01133       // sides are PHI nodes. In which case, this is O(m x n) time where 'm'
01134       // and 'n' are the number of PHI sources.
01135       return MayAlias;
01136     if (UniqueSrc.insert(PV1))
01137       V1Srcs.push_back(PV1);
01138   }
01139 
01140   AliasResult Alias = aliasCheck(V2, V2Size, V2TBAAInfo,
01141                                  V1Srcs[0], PNSize, PNTBAAInfo);
01142   // Early exit if the check of the first PHI source against V2 is MayAlias.
01143   // Other results are not possible.
01144   if (Alias == MayAlias)
01145     return MayAlias;
01146 
01147   // If all sources of the PHI node NoAlias or MustAlias V2, then returns
01148   // NoAlias / MustAlias. Otherwise, returns MayAlias.
01149   for (unsigned i = 1, e = V1Srcs.size(); i != e; ++i) {
01150     Value *V = V1Srcs[i];
01151 
01152     AliasResult ThisAlias = aliasCheck(V2, V2Size, V2TBAAInfo,
01153                                        V, PNSize, PNTBAAInfo);
01154     Alias = MergeAliasResults(ThisAlias, Alias);
01155     if (Alias == MayAlias)
01156       break;
01157   }
01158 
01159   return Alias;
01160 }
01161 
01162 // aliasCheck - Provide a bunch of ad-hoc rules to disambiguate in common cases,
01163 // such as array references.
01164 //
01165 AliasAnalysis::AliasResult
01166 BasicAliasAnalysis::aliasCheck(const Value *V1, uint64_t V1Size,
01167                                const MDNode *V1TBAAInfo,
01168                                const Value *V2, uint64_t V2Size,
01169                                const MDNode *V2TBAAInfo) {
01170   // If either of the memory references is empty, it doesn't matter what the
01171   // pointer values are.
01172   if (V1Size == 0 || V2Size == 0)
01173     return NoAlias;
01174 
01175   // Strip off any casts if they exist.
01176   V1 = V1->stripPointerCasts();
01177   V2 = V2->stripPointerCasts();
01178 
01179   // Are we checking for alias of the same value?
01180   if (V1 == V2) return MustAlias;
01181 
01182   if (!V1->getType()->isPointerTy() || !V2->getType()->isPointerTy())
01183     return NoAlias;  // Scalars cannot alias each other
01184 
01185   // Figure out what objects these things are pointing to if we can.
01186   const Value *O1 = GetUnderlyingObject(V1, TD);
01187   const Value *O2 = GetUnderlyingObject(V2, TD);
01188 
01189   // Null values in the default address space don't point to any object, so they
01190   // don't alias any other pointer.
01191   if (const ConstantPointerNull *CPN = dyn_cast<ConstantPointerNull>(O1))
01192     if (CPN->getType()->getAddressSpace() == 0)
01193       return NoAlias;
01194   if (const ConstantPointerNull *CPN = dyn_cast<ConstantPointerNull>(O2))
01195     if (CPN->getType()->getAddressSpace() == 0)
01196       return NoAlias;
01197 
01198   if (O1 != O2) {
01199     // If V1/V2 point to two different objects we know that we have no alias.
01200     if (isIdentifiedObject(O1) && isIdentifiedObject(O2))
01201       return NoAlias;
01202 
01203     // Constant pointers can't alias with non-const isIdentifiedObject objects.
01204     if ((isa<Constant>(O1) && isIdentifiedObject(O2) && !isa<Constant>(O2)) ||
01205         (isa<Constant>(O2) && isIdentifiedObject(O1) && !isa<Constant>(O1)))
01206       return NoAlias;
01207 
01208     // Arguments can't alias with local allocations or noalias calls
01209     // in the same function.
01210     if (((isa<Argument>(O1) && (isa<AllocaInst>(O2) || isNoAliasCall(O2))) ||
01211          (isa<Argument>(O2) && (isa<AllocaInst>(O1) || isNoAliasCall(O1)))))
01212       return NoAlias;
01213 
01214     // Most objects can't alias null.
01215     if ((isa<ConstantPointerNull>(O2) && isKnownNonNull(O1)) ||
01216         (isa<ConstantPointerNull>(O1) && isKnownNonNull(O2)))
01217       return NoAlias;
01218   
01219     // If one pointer is the result of a call/invoke or load and the other is a
01220     // non-escaping local object within the same function, then we know the
01221     // object couldn't escape to a point where the call could return it.
01222     //
01223     // Note that if the pointers are in different functions, there are a
01224     // variety of complications. A call with a nocapture argument may still
01225     // temporary store the nocapture argument's value in a temporary memory
01226     // location if that memory location doesn't escape. Or it may pass a
01227     // nocapture value to other functions as long as they don't capture it.
01228     if (isEscapeSource(O1) && isNonEscapingLocalObject(O2))
01229       return NoAlias;
01230     if (isEscapeSource(O2) && isNonEscapingLocalObject(O1))
01231       return NoAlias;
01232   }
01233 
01234   // If the size of one access is larger than the entire object on the other
01235   // side, then we know such behavior is undefined and can assume no alias.
01236   if (TD)
01237     if ((V1Size != UnknownSize && isObjectSmallerThan(O2, V1Size, *TD, *TLI)) ||
01238         (V2Size != UnknownSize && isObjectSmallerThan(O1, V2Size, *TD, *TLI)))
01239       return NoAlias;
01240   
01241   // Check the cache before climbing up use-def chains. This also terminates
01242   // otherwise infinitely recursive queries.
01243   LocPair Locs(Location(V1, V1Size, V1TBAAInfo),
01244                Location(V2, V2Size, V2TBAAInfo));
01245   if (V1 > V2)
01246     std::swap(Locs.first, Locs.second);
01247   std::pair<AliasCacheTy::iterator, bool> Pair =
01248     AliasCache.insert(std::make_pair(Locs, MayAlias));
01249   if (!Pair.second)
01250     return Pair.first->second;
01251 
01252   // FIXME: This isn't aggressively handling alias(GEP, PHI) for example: if the
01253   // GEP can't simplify, we don't even look at the PHI cases.
01254   if (!isa<GEPOperator>(V1) && isa<GEPOperator>(V2)) {
01255     std::swap(V1, V2);
01256     std::swap(V1Size, V2Size);
01257     std::swap(O1, O2);
01258     std::swap(V1TBAAInfo, V2TBAAInfo);
01259   }
01260   if (const GEPOperator *GV1 = dyn_cast<GEPOperator>(V1)) {
01261     AliasResult Result = aliasGEP(GV1, V1Size, V1TBAAInfo, V2, V2Size, V2TBAAInfo, O1, O2);
01262     if (Result != MayAlias) return AliasCache[Locs] = Result;
01263   }
01264 
01265   if (isa<PHINode>(V2) && !isa<PHINode>(V1)) {
01266     std::swap(V1, V2);
01267     std::swap(V1Size, V2Size);
01268     std::swap(V1TBAAInfo, V2TBAAInfo);
01269   }
01270   if (const PHINode *PN = dyn_cast<PHINode>(V1)) {
01271     AliasResult Result = aliasPHI(PN, V1Size, V1TBAAInfo,
01272                                   V2, V2Size, V2TBAAInfo);
01273     if (Result != MayAlias) return AliasCache[Locs] = Result;
01274   }
01275 
01276   if (isa<SelectInst>(V2) && !isa<SelectInst>(V1)) {
01277     std::swap(V1, V2);
01278     std::swap(V1Size, V2Size);
01279     std::swap(V1TBAAInfo, V2TBAAInfo);
01280   }
01281   if (const SelectInst *S1 = dyn_cast<SelectInst>(V1)) {
01282     AliasResult Result = aliasSelect(S1, V1Size, V1TBAAInfo,
01283                                      V2, V2Size, V2TBAAInfo);
01284     if (Result != MayAlias) return AliasCache[Locs] = Result;
01285   }
01286 
01287   // If both pointers are pointing into the same object and one of them
01288   // accesses is accessing the entire object, then the accesses must
01289   // overlap in some way.
01290   if (TD && O1 == O2)
01291     if ((V1Size != UnknownSize && isObjectSize(O1, V1Size, *TD, *TLI)) ||
01292         (V2Size != UnknownSize && isObjectSize(O2, V2Size, *TD, *TLI)))
01293       return AliasCache[Locs] = PartialAlias;
01294 
01295   AliasResult Result =
01296     AliasAnalysis::alias(Location(V1, V1Size, V1TBAAInfo),
01297                          Location(V2, V2Size, V2TBAAInfo));
01298   return AliasCache[Locs] = Result;
01299 }