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