1 //===- BasicAliasAnalysis.cpp - Stateless Alias Analysis Impl -------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the primary stateless implementation of the
10 // Alias Analysis interface that implements identities (two different
11 // globals cannot alias, etc), but does no stateful analysis.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/Analysis/BasicAliasAnalysis.h"
16 #include "llvm/ADT/APInt.h"
17 #include "llvm/ADT/ScopeExit.h"
18 #include "llvm/ADT/SmallPtrSet.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/Statistic.h"
21 #include "llvm/Analysis/AliasAnalysis.h"
22 #include "llvm/Analysis/AssumptionCache.h"
23 #include "llvm/Analysis/CFG.h"
24 #include "llvm/Analysis/CaptureTracking.h"
25 #include "llvm/Analysis/MemoryBuiltins.h"
26 #include "llvm/Analysis/MemoryLocation.h"
27 #include "llvm/Analysis/PhiValues.h"
28 #include "llvm/Analysis/TargetLibraryInfo.h"
29 #include "llvm/Analysis/ValueTracking.h"
30 #include "llvm/IR/Argument.h"
31 #include "llvm/IR/Attributes.h"
32 #include "llvm/IR/Constant.h"
33 #include "llvm/IR/ConstantRange.h"
34 #include "llvm/IR/Constants.h"
35 #include "llvm/IR/DataLayout.h"
36 #include "llvm/IR/DerivedTypes.h"
37 #include "llvm/IR/Dominators.h"
38 #include "llvm/IR/Function.h"
39 #include "llvm/IR/GetElementPtrTypeIterator.h"
40 #include "llvm/IR/GlobalAlias.h"
41 #include "llvm/IR/GlobalVariable.h"
42 #include "llvm/IR/InstrTypes.h"
43 #include "llvm/IR/Instruction.h"
44 #include "llvm/IR/Instructions.h"
45 #include "llvm/IR/IntrinsicInst.h"
46 #include "llvm/IR/Intrinsics.h"
47 #include "llvm/IR/Operator.h"
48 #include "llvm/IR/Type.h"
49 #include "llvm/IR/User.h"
50 #include "llvm/IR/Value.h"
51 #include "llvm/InitializePasses.h"
52 #include "llvm/Pass.h"
53 #include "llvm/Support/Casting.h"
54 #include "llvm/Support/CommandLine.h"
55 #include "llvm/Support/Compiler.h"
56 #include "llvm/Support/KnownBits.h"
57 #include <cassert>
58 #include <cstdint>
59 #include <cstdlib>
60 #include <utility>
61 
62 #define DEBUG_TYPE "basicaa"
63 
64 using namespace llvm;
65 
66 /// Enable analysis of recursive PHI nodes.
67 static cl::opt<bool> EnableRecPhiAnalysis("basic-aa-recphi", cl::Hidden,
68                                           cl::init(true));
69 
70 /// SearchLimitReached / SearchTimes shows how often the limit of
71 /// to decompose GEPs is reached. It will affect the precision
72 /// of basic alias analysis.
73 STATISTIC(SearchLimitReached, "Number of times the limit to "
74                               "decompose GEPs is reached");
75 STATISTIC(SearchTimes, "Number of times a GEP is decomposed");
76 
77 /// Cutoff after which to stop analysing a set of phi nodes potentially involved
78 /// in a cycle. Because we are analysing 'through' phi nodes, we need to be
79 /// careful with value equivalence. We use reachability to make sure a value
80 /// cannot be involved in a cycle.
81 const unsigned MaxNumPhiBBsValueReachabilityCheck = 20;
82 
83 // The max limit of the search depth in DecomposeGEPExpression() and
84 // getUnderlyingObject().
85 static const unsigned MaxLookupSearchDepth = 6;
86 
87 bool BasicAAResult::invalidate(Function &Fn, const PreservedAnalyses &PA,
88                                FunctionAnalysisManager::Invalidator &Inv) {
89   // We don't care if this analysis itself is preserved, it has no state. But
90   // we need to check that the analyses it depends on have been. Note that we
91   // may be created without handles to some analyses and in that case don't
92   // depend on them.
93   if (Inv.invalidate<AssumptionAnalysis>(Fn, PA) ||
94       (DT && Inv.invalidate<DominatorTreeAnalysis>(Fn, PA)) ||
95       (PV && Inv.invalidate<PhiValuesAnalysis>(Fn, PA)))
96     return true;
97 
98   // Otherwise this analysis result remains valid.
99   return false;
100 }
101 
102 //===----------------------------------------------------------------------===//
103 // Useful predicates
104 //===----------------------------------------------------------------------===//
105 
106 /// Returns true if the pointer is one which would have been considered an
107 /// escape by isNonEscapingLocalObject.
108 static bool isEscapeSource(const Value *V) {
109   if (isa<CallBase>(V))
110     return true;
111 
112   // The load case works because isNonEscapingLocalObject considers all
113   // stores to be escapes (it passes true for the StoreCaptures argument
114   // to PointerMayBeCaptured).
115   if (isa<LoadInst>(V))
116     return true;
117 
118   // The inttoptr case works because isNonEscapingLocalObject considers all
119   // means of converting or equating a pointer to an int (ptrtoint, ptr store
120   // which could be followed by an integer load, ptr<->int compare) as
121   // escaping, and objects located at well-known addresses via platform-specific
122   // means cannot be considered non-escaping local objects.
123   if (isa<IntToPtrInst>(V))
124     return true;
125 
126   return false;
127 }
128 
129 /// Returns the size of the object specified by V or UnknownSize if unknown.
130 static uint64_t getObjectSize(const Value *V, const DataLayout &DL,
131                               const TargetLibraryInfo &TLI,
132                               bool NullIsValidLoc,
133                               bool RoundToAlign = false) {
134   uint64_t Size;
135   ObjectSizeOpts Opts;
136   Opts.RoundToAlign = RoundToAlign;
137   Opts.NullIsUnknownSize = NullIsValidLoc;
138   if (getObjectSize(V, Size, DL, &TLI, Opts))
139     return Size;
140   return MemoryLocation::UnknownSize;
141 }
142 
143 /// Returns true if we can prove that the object specified by V is smaller than
144 /// Size.
145 static bool isObjectSmallerThan(const Value *V, uint64_t Size,
146                                 const DataLayout &DL,
147                                 const TargetLibraryInfo &TLI,
148                                 bool NullIsValidLoc) {
149   // Note that the meanings of the "object" are slightly different in the
150   // following contexts:
151   //    c1: llvm::getObjectSize()
152   //    c2: llvm.objectsize() intrinsic
153   //    c3: isObjectSmallerThan()
154   // c1 and c2 share the same meaning; however, the meaning of "object" in c3
155   // refers to the "entire object".
156   //
157   //  Consider this example:
158   //     char *p = (char*)malloc(100)
159   //     char *q = p+80;
160   //
161   //  In the context of c1 and c2, the "object" pointed by q refers to the
162   // stretch of memory of q[0:19]. So, getObjectSize(q) should return 20.
163   //
164   //  However, in the context of c3, the "object" refers to the chunk of memory
165   // being allocated. So, the "object" has 100 bytes, and q points to the middle
166   // the "object". In case q is passed to isObjectSmallerThan() as the 1st
167   // parameter, before the llvm::getObjectSize() is called to get the size of
168   // entire object, we should:
169   //    - either rewind the pointer q to the base-address of the object in
170   //      question (in this case rewind to p), or
171   //    - just give up. It is up to caller to make sure the pointer is pointing
172   //      to the base address the object.
173   //
174   // We go for 2nd option for simplicity.
175   if (!isIdentifiedObject(V))
176     return false;
177 
178   // This function needs to use the aligned object size because we allow
179   // reads a bit past the end given sufficient alignment.
180   uint64_t ObjectSize = getObjectSize(V, DL, TLI, NullIsValidLoc,
181                                       /*RoundToAlign*/ true);
182 
183   return ObjectSize != MemoryLocation::UnknownSize && ObjectSize < Size;
184 }
185 
186 /// Return the minimal extent from \p V to the end of the underlying object,
187 /// assuming the result is used in an aliasing query. E.g., we do use the query
188 /// location size and the fact that null pointers cannot alias here.
189 static uint64_t getMinimalExtentFrom(const Value &V,
190                                      const LocationSize &LocSize,
191                                      const DataLayout &DL,
192                                      bool NullIsValidLoc) {
193   // If we have dereferenceability information we know a lower bound for the
194   // extent as accesses for a lower offset would be valid. We need to exclude
195   // the "or null" part if null is a valid pointer. We can ignore frees, as an
196   // access after free would be undefined behavior.
197   bool CanBeNull, CanBeFreed;
198   uint64_t DerefBytes =
199     V.getPointerDereferenceableBytes(DL, CanBeNull, CanBeFreed);
200   DerefBytes = (CanBeNull && NullIsValidLoc) ? 0 : DerefBytes;
201   // If queried with a precise location size, we assume that location size to be
202   // accessed, thus valid.
203   if (LocSize.isPrecise())
204     DerefBytes = std::max(DerefBytes, LocSize.getValue());
205   return DerefBytes;
206 }
207 
208 /// Returns true if we can prove that the object specified by V has size Size.
209 static bool isObjectSize(const Value *V, uint64_t Size, const DataLayout &DL,
210                          const TargetLibraryInfo &TLI, bool NullIsValidLoc) {
211   uint64_t ObjectSize = getObjectSize(V, DL, TLI, NullIsValidLoc);
212   return ObjectSize != MemoryLocation::UnknownSize && ObjectSize == Size;
213 }
214 
215 //===----------------------------------------------------------------------===//
216 // CaptureInfo implementations
217 //===----------------------------------------------------------------------===//
218 
219 CaptureInfo::~CaptureInfo() = default;
220 
221 bool SimpleCaptureInfo::isNotCapturedBeforeOrAt(const Value *Object,
222                                                 const Instruction *I) {
223   return isNonEscapingLocalObject(Object, &IsCapturedCache);
224 }
225 
226 bool EarliestEscapeInfo::isNotCapturedBeforeOrAt(const Value *Object,
227                                                  const Instruction *I) {
228   if (!isIdentifiedFunctionLocal(Object))
229     return false;
230 
231   auto Iter = EarliestEscapes.insert({Object, nullptr});
232   if (Iter.second) {
233     Instruction *EarliestCapture = FindEarliestCapture(
234         Object, *const_cast<Function *>(I->getFunction()),
235         /*ReturnCaptures=*/false, /*StoreCaptures=*/true, DT, EphValues);
236     if (EarliestCapture) {
237       auto Ins = Inst2Obj.insert({EarliestCapture, {}});
238       Ins.first->second.push_back(Object);
239     }
240     Iter.first->second = EarliestCapture;
241   }
242 
243   // No capturing instruction.
244   if (!Iter.first->second)
245     return true;
246 
247   return I != Iter.first->second &&
248          !isPotentiallyReachable(Iter.first->second, I, nullptr, &DT, &LI);
249 }
250 
251 void EarliestEscapeInfo::removeInstruction(Instruction *I) {
252   auto Iter = Inst2Obj.find(I);
253   if (Iter != Inst2Obj.end()) {
254     for (const Value *Obj : Iter->second)
255       EarliestEscapes.erase(Obj);
256     Inst2Obj.erase(I);
257   }
258 }
259 
260 //===----------------------------------------------------------------------===//
261 // GetElementPtr Instruction Decomposition and Analysis
262 //===----------------------------------------------------------------------===//
263 
264 namespace {
265 /// Represents zext(sext(trunc(V))).
266 struct CastedValue {
267   const Value *V;
268   unsigned ZExtBits = 0;
269   unsigned SExtBits = 0;
270   unsigned TruncBits = 0;
271 
272   explicit CastedValue(const Value *V) : V(V) {}
273   explicit CastedValue(const Value *V, unsigned ZExtBits, unsigned SExtBits,
274                        unsigned TruncBits)
275       : V(V), ZExtBits(ZExtBits), SExtBits(SExtBits), TruncBits(TruncBits) {}
276 
277   unsigned getBitWidth() const {
278     return V->getType()->getPrimitiveSizeInBits() - TruncBits + ZExtBits +
279            SExtBits;
280   }
281 
282   CastedValue withValue(const Value *NewV) const {
283     return CastedValue(NewV, ZExtBits, SExtBits, TruncBits);
284   }
285 
286   /// Replace V with zext(NewV)
287   CastedValue withZExtOfValue(const Value *NewV) const {
288     unsigned ExtendBy = V->getType()->getPrimitiveSizeInBits() -
289                         NewV->getType()->getPrimitiveSizeInBits();
290     if (ExtendBy <= TruncBits)
291       return CastedValue(NewV, ZExtBits, SExtBits, TruncBits - ExtendBy);
292 
293     // zext(sext(zext(NewV))) == zext(zext(zext(NewV)))
294     ExtendBy -= TruncBits;
295     return CastedValue(NewV, ZExtBits + SExtBits + ExtendBy, 0, 0);
296   }
297 
298   /// Replace V with sext(NewV)
299   CastedValue withSExtOfValue(const Value *NewV) const {
300     unsigned ExtendBy = V->getType()->getPrimitiveSizeInBits() -
301                         NewV->getType()->getPrimitiveSizeInBits();
302     if (ExtendBy <= TruncBits)
303       return CastedValue(NewV, ZExtBits, SExtBits, TruncBits - ExtendBy);
304 
305     // zext(sext(sext(NewV)))
306     ExtendBy -= TruncBits;
307     return CastedValue(NewV, ZExtBits, SExtBits + ExtendBy, 0);
308   }
309 
310   APInt evaluateWith(APInt N) const {
311     assert(N.getBitWidth() == V->getType()->getPrimitiveSizeInBits() &&
312            "Incompatible bit width");
313     if (TruncBits) N = N.trunc(N.getBitWidth() - TruncBits);
314     if (SExtBits) N = N.sext(N.getBitWidth() + SExtBits);
315     if (ZExtBits) N = N.zext(N.getBitWidth() + ZExtBits);
316     return N;
317   }
318 
319   ConstantRange evaluateWith(ConstantRange N) const {
320     assert(N.getBitWidth() == V->getType()->getPrimitiveSizeInBits() &&
321            "Incompatible bit width");
322     if (TruncBits) N = N.truncate(N.getBitWidth() - TruncBits);
323     if (SExtBits) N = N.signExtend(N.getBitWidth() + SExtBits);
324     if (ZExtBits) N = N.zeroExtend(N.getBitWidth() + ZExtBits);
325     return N;
326   }
327 
328   bool canDistributeOver(bool NUW, bool NSW) const {
329     // zext(x op<nuw> y) == zext(x) op<nuw> zext(y)
330     // sext(x op<nsw> y) == sext(x) op<nsw> sext(y)
331     // trunc(x op y) == trunc(x) op trunc(y)
332     return (!ZExtBits || NUW) && (!SExtBits || NSW);
333   }
334 
335   bool hasSameCastsAs(const CastedValue &Other) const {
336     return ZExtBits == Other.ZExtBits && SExtBits == Other.SExtBits &&
337            TruncBits == Other.TruncBits;
338   }
339 };
340 
341 /// Represents zext(sext(trunc(V))) * Scale + Offset.
342 struct LinearExpression {
343   CastedValue Val;
344   APInt Scale;
345   APInt Offset;
346 
347   /// True if all operations in this expression are NSW.
348   bool IsNSW;
349 
350   LinearExpression(const CastedValue &Val, const APInt &Scale,
351                    const APInt &Offset, bool IsNSW)
352       : Val(Val), Scale(Scale), Offset(Offset), IsNSW(IsNSW) {}
353 
354   LinearExpression(const CastedValue &Val) : Val(Val), IsNSW(true) {
355     unsigned BitWidth = Val.getBitWidth();
356     Scale = APInt(BitWidth, 1);
357     Offset = APInt(BitWidth, 0);
358   }
359 
360   LinearExpression mul(const APInt &Other, bool MulIsNSW) const {
361     // The check for zero offset is necessary, because generally
362     // (X +nsw Y) *nsw Z does not imply (X *nsw Z) +nsw (Y *nsw Z).
363     bool NSW = IsNSW && (Other.isOne() || (MulIsNSW && Offset.isZero()));
364     return LinearExpression(Val, Scale * Other, Offset * Other, NSW);
365   }
366 };
367 }
368 
369 /// Analyzes the specified value as a linear expression: "A*V + B", where A and
370 /// B are constant integers.
371 static LinearExpression GetLinearExpression(
372     const CastedValue &Val,  const DataLayout &DL, unsigned Depth,
373     AssumptionCache *AC, DominatorTree *DT) {
374   // Limit our recursion depth.
375   if (Depth == 6)
376     return Val;
377 
378   if (const ConstantInt *Const = dyn_cast<ConstantInt>(Val.V))
379     return LinearExpression(Val, APInt(Val.getBitWidth(), 0),
380                             Val.evaluateWith(Const->getValue()), true);
381 
382   if (const BinaryOperator *BOp = dyn_cast<BinaryOperator>(Val.V)) {
383     if (ConstantInt *RHSC = dyn_cast<ConstantInt>(BOp->getOperand(1))) {
384       APInt RHS = Val.evaluateWith(RHSC->getValue());
385       // The only non-OBO case we deal with is or, and only limited to the
386       // case where it is both nuw and nsw.
387       bool NUW = true, NSW = true;
388       if (isa<OverflowingBinaryOperator>(BOp)) {
389         NUW &= BOp->hasNoUnsignedWrap();
390         NSW &= BOp->hasNoSignedWrap();
391       }
392       if (!Val.canDistributeOver(NUW, NSW))
393         return Val;
394 
395       // While we can distribute over trunc, we cannot preserve nowrap flags
396       // in that case.
397       if (Val.TruncBits)
398         NUW = NSW = false;
399 
400       LinearExpression E(Val);
401       switch (BOp->getOpcode()) {
402       default:
403         // We don't understand this instruction, so we can't decompose it any
404         // further.
405         return Val;
406       case Instruction::Or:
407         // X|C == X+C if all the bits in C are unset in X.  Otherwise we can't
408         // analyze it.
409         if (!MaskedValueIsZero(BOp->getOperand(0), RHSC->getValue(), DL, 0, AC,
410                                BOp, DT))
411           return Val;
412 
413         LLVM_FALLTHROUGH;
414       case Instruction::Add: {
415         E = GetLinearExpression(Val.withValue(BOp->getOperand(0)), DL,
416                                 Depth + 1, AC, DT);
417         E.Offset += RHS;
418         E.IsNSW &= NSW;
419         break;
420       }
421       case Instruction::Sub: {
422         E = GetLinearExpression(Val.withValue(BOp->getOperand(0)), DL,
423                                 Depth + 1, AC, DT);
424         E.Offset -= RHS;
425         E.IsNSW &= NSW;
426         break;
427       }
428       case Instruction::Mul:
429         E = GetLinearExpression(Val.withValue(BOp->getOperand(0)), DL,
430                                 Depth + 1, AC, DT)
431                 .mul(RHS, NSW);
432         break;
433       case Instruction::Shl:
434         // We're trying to linearize an expression of the kind:
435         //   shl i8 -128, 36
436         // where the shift count exceeds the bitwidth of the type.
437         // We can't decompose this further (the expression would return
438         // a poison value).
439         if (RHS.getLimitedValue() > Val.getBitWidth())
440           return Val;
441 
442         E = GetLinearExpression(Val.withValue(BOp->getOperand(0)), DL,
443                                 Depth + 1, AC, DT);
444         E.Offset <<= RHS.getLimitedValue();
445         E.Scale <<= RHS.getLimitedValue();
446         E.IsNSW &= NSW;
447         break;
448       }
449       return E;
450     }
451   }
452 
453   if (isa<ZExtInst>(Val.V))
454     return GetLinearExpression(
455         Val.withZExtOfValue(cast<CastInst>(Val.V)->getOperand(0)),
456         DL, Depth + 1, AC, DT);
457 
458   if (isa<SExtInst>(Val.V))
459     return GetLinearExpression(
460         Val.withSExtOfValue(cast<CastInst>(Val.V)->getOperand(0)),
461         DL, Depth + 1, AC, DT);
462 
463   return Val;
464 }
465 
466 /// To ensure a pointer offset fits in an integer of size IndexSize
467 /// (in bits) when that size is smaller than the maximum index size. This is
468 /// an issue, for example, in particular for 32b pointers with negative indices
469 /// that rely on two's complement wrap-arounds for precise alias information
470 /// where the maximum index size is 64b.
471 static APInt adjustToIndexSize(const APInt &Offset, unsigned IndexSize) {
472   assert(IndexSize <= Offset.getBitWidth() && "Invalid IndexSize!");
473   unsigned ShiftBits = Offset.getBitWidth() - IndexSize;
474   return (Offset << ShiftBits).ashr(ShiftBits);
475 }
476 
477 namespace {
478 // A linear transformation of a Value; this class represents
479 // ZExt(SExt(Trunc(V, TruncBits), SExtBits), ZExtBits) * Scale.
480 struct VariableGEPIndex {
481   CastedValue Val;
482   APInt Scale;
483 
484   // Context instruction to use when querying information about this index.
485   const Instruction *CxtI;
486 
487   /// True if all operations in this expression are NSW.
488   bool IsNSW;
489 
490   void dump() const {
491     print(dbgs());
492     dbgs() << "\n";
493   }
494   void print(raw_ostream &OS) const {
495     OS << "(V=" << Val.V->getName()
496        << ", zextbits=" << Val.ZExtBits
497        << ", sextbits=" << Val.SExtBits
498        << ", truncbits=" << Val.TruncBits
499        << ", scale=" << Scale << ")";
500   }
501 };
502 }
503 
504 // Represents the internal structure of a GEP, decomposed into a base pointer,
505 // constant offsets, and variable scaled indices.
506 struct BasicAAResult::DecomposedGEP {
507   // Base pointer of the GEP
508   const Value *Base;
509   // Total constant offset from base.
510   APInt Offset;
511   // Scaled variable (non-constant) indices.
512   SmallVector<VariableGEPIndex, 4> VarIndices;
513   // Are all operations inbounds GEPs or non-indexing operations?
514   // (None iff expression doesn't involve any geps)
515   Optional<bool> InBounds;
516 
517   void dump() const {
518     print(dbgs());
519     dbgs() << "\n";
520   }
521   void print(raw_ostream &OS) const {
522     OS << "(DecomposedGEP Base=" << Base->getName()
523        << ", Offset=" << Offset
524        << ", VarIndices=[";
525     for (size_t i = 0; i < VarIndices.size(); i++) {
526       if (i != 0)
527         OS << ", ";
528       VarIndices[i].print(OS);
529     }
530     OS << "])";
531   }
532 };
533 
534 
535 /// If V is a symbolic pointer expression, decompose it into a base pointer
536 /// with a constant offset and a number of scaled symbolic offsets.
537 ///
538 /// The scaled symbolic offsets (represented by pairs of a Value* and a scale
539 /// in the VarIndices vector) are Value*'s that are known to be scaled by the
540 /// specified amount, but which may have other unrepresented high bits. As
541 /// such, the gep cannot necessarily be reconstructed from its decomposed form.
542 BasicAAResult::DecomposedGEP
543 BasicAAResult::DecomposeGEPExpression(const Value *V, const DataLayout &DL,
544                                       AssumptionCache *AC, DominatorTree *DT) {
545   // Limit recursion depth to limit compile time in crazy cases.
546   unsigned MaxLookup = MaxLookupSearchDepth;
547   SearchTimes++;
548   const Instruction *CxtI = dyn_cast<Instruction>(V);
549 
550   unsigned MaxIndexSize = DL.getMaxIndexSizeInBits();
551   DecomposedGEP Decomposed;
552   Decomposed.Offset = APInt(MaxIndexSize, 0);
553   do {
554     // See if this is a bitcast or GEP.
555     const Operator *Op = dyn_cast<Operator>(V);
556     if (!Op) {
557       // The only non-operator case we can handle are GlobalAliases.
558       if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
559         if (!GA->isInterposable()) {
560           V = GA->getAliasee();
561           continue;
562         }
563       }
564       Decomposed.Base = V;
565       return Decomposed;
566     }
567 
568     if (Op->getOpcode() == Instruction::BitCast ||
569         Op->getOpcode() == Instruction::AddrSpaceCast) {
570       V = Op->getOperand(0);
571       continue;
572     }
573 
574     const GEPOperator *GEPOp = dyn_cast<GEPOperator>(Op);
575     if (!GEPOp) {
576       if (const auto *PHI = dyn_cast<PHINode>(V)) {
577         // Look through single-arg phi nodes created by LCSSA.
578         if (PHI->getNumIncomingValues() == 1) {
579           V = PHI->getIncomingValue(0);
580           continue;
581         }
582       } else if (const auto *Call = dyn_cast<CallBase>(V)) {
583         // CaptureTracking can know about special capturing properties of some
584         // intrinsics like launder.invariant.group, that can't be expressed with
585         // the attributes, but have properties like returning aliasing pointer.
586         // Because some analysis may assume that nocaptured pointer is not
587         // returned from some special intrinsic (because function would have to
588         // be marked with returns attribute), it is crucial to use this function
589         // because it should be in sync with CaptureTracking. Not using it may
590         // cause weird miscompilations where 2 aliasing pointers are assumed to
591         // noalias.
592         if (auto *RP = getArgumentAliasingToReturnedPointer(Call, false)) {
593           V = RP;
594           continue;
595         }
596       }
597 
598       Decomposed.Base = V;
599       return Decomposed;
600     }
601 
602     // Track whether we've seen at least one in bounds gep, and if so, whether
603     // all geps parsed were in bounds.
604     if (Decomposed.InBounds == None)
605       Decomposed.InBounds = GEPOp->isInBounds();
606     else if (!GEPOp->isInBounds())
607       Decomposed.InBounds = false;
608 
609     assert(GEPOp->getSourceElementType()->isSized() && "GEP must be sized");
610 
611     // Don't attempt to analyze GEPs if index scale is not a compile-time
612     // constant.
613     if (isa<ScalableVectorType>(GEPOp->getSourceElementType())) {
614       Decomposed.Base = V;
615       return Decomposed;
616     }
617 
618     unsigned AS = GEPOp->getPointerAddressSpace();
619     // Walk the indices of the GEP, accumulating them into BaseOff/VarIndices.
620     gep_type_iterator GTI = gep_type_begin(GEPOp);
621     unsigned IndexSize = DL.getIndexSizeInBits(AS);
622     // Assume all GEP operands are constants until proven otherwise.
623     bool GepHasConstantOffset = true;
624     for (User::const_op_iterator I = GEPOp->op_begin() + 1, E = GEPOp->op_end();
625          I != E; ++I, ++GTI) {
626       const Value *Index = *I;
627       // Compute the (potentially symbolic) offset in bytes for this index.
628       if (StructType *STy = GTI.getStructTypeOrNull()) {
629         // For a struct, add the member offset.
630         unsigned FieldNo = cast<ConstantInt>(Index)->getZExtValue();
631         if (FieldNo == 0)
632           continue;
633 
634         Decomposed.Offset += DL.getStructLayout(STy)->getElementOffset(FieldNo);
635         continue;
636       }
637 
638       // For an array/pointer, add the element offset, explicitly scaled.
639       if (const ConstantInt *CIdx = dyn_cast<ConstantInt>(Index)) {
640         if (CIdx->isZero())
641           continue;
642         Decomposed.Offset +=
643             DL.getTypeAllocSize(GTI.getIndexedType()).getFixedSize() *
644             CIdx->getValue().sextOrTrunc(MaxIndexSize);
645         continue;
646       }
647 
648       GepHasConstantOffset = false;
649 
650       // If the integer type is smaller than the index size, it is implicitly
651       // sign extended or truncated to index size.
652       unsigned Width = Index->getType()->getIntegerBitWidth();
653       unsigned SExtBits = IndexSize > Width ? IndexSize - Width : 0;
654       unsigned TruncBits = IndexSize < Width ? Width - IndexSize : 0;
655       LinearExpression LE = GetLinearExpression(
656           CastedValue(Index, 0, SExtBits, TruncBits), DL, 0, AC, DT);
657 
658       // Scale by the type size.
659       unsigned TypeSize =
660           DL.getTypeAllocSize(GTI.getIndexedType()).getFixedSize();
661       LE = LE.mul(APInt(IndexSize, TypeSize), GEPOp->isInBounds());
662       Decomposed.Offset += LE.Offset.sextOrSelf(MaxIndexSize);
663       APInt Scale = LE.Scale.sextOrSelf(MaxIndexSize);
664 
665       // If we already had an occurrence of this index variable, merge this
666       // scale into it.  For example, we want to handle:
667       //   A[x][x] -> x*16 + x*4 -> x*20
668       // This also ensures that 'x' only appears in the index list once.
669       for (unsigned i = 0, e = Decomposed.VarIndices.size(); i != e; ++i) {
670         if (Decomposed.VarIndices[i].Val.V == LE.Val.V &&
671             Decomposed.VarIndices[i].Val.hasSameCastsAs(LE.Val)) {
672           Scale += Decomposed.VarIndices[i].Scale;
673           Decomposed.VarIndices.erase(Decomposed.VarIndices.begin() + i);
674           break;
675         }
676       }
677 
678       // Make sure that we have a scale that makes sense for this target's
679       // index size.
680       Scale = adjustToIndexSize(Scale, IndexSize);
681 
682       if (!!Scale) {
683         VariableGEPIndex Entry = {LE.Val, Scale, CxtI, LE.IsNSW};
684         Decomposed.VarIndices.push_back(Entry);
685       }
686     }
687 
688     // Take care of wrap-arounds
689     if (GepHasConstantOffset)
690       Decomposed.Offset = adjustToIndexSize(Decomposed.Offset, IndexSize);
691 
692     // Analyze the base pointer next.
693     V = GEPOp->getOperand(0);
694   } while (--MaxLookup);
695 
696   // If the chain of expressions is too deep, just return early.
697   Decomposed.Base = V;
698   SearchLimitReached++;
699   return Decomposed;
700 }
701 
702 /// Returns whether the given pointer value points to memory that is local to
703 /// the function, with global constants being considered local to all
704 /// functions.
705 bool BasicAAResult::pointsToConstantMemory(const MemoryLocation &Loc,
706                                            AAQueryInfo &AAQI, bool OrLocal) {
707   assert(Visited.empty() && "Visited must be cleared after use!");
708 
709   unsigned MaxLookup = 8;
710   SmallVector<const Value *, 16> Worklist;
711   Worklist.push_back(Loc.Ptr);
712   do {
713     const Value *V = getUnderlyingObject(Worklist.pop_back_val());
714     if (!Visited.insert(V).second) {
715       Visited.clear();
716       return AAResultBase::pointsToConstantMemory(Loc, AAQI, OrLocal);
717     }
718 
719     // An alloca instruction defines local memory.
720     if (OrLocal && isa<AllocaInst>(V))
721       continue;
722 
723     // A global constant counts as local memory for our purposes.
724     if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
725       // Note: this doesn't require GV to be "ODR" because it isn't legal for a
726       // global to be marked constant in some modules and non-constant in
727       // others.  GV may even be a declaration, not a definition.
728       if (!GV->isConstant()) {
729         Visited.clear();
730         return AAResultBase::pointsToConstantMemory(Loc, AAQI, OrLocal);
731       }
732       continue;
733     }
734 
735     // If both select values point to local memory, then so does the select.
736     if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {
737       Worklist.push_back(SI->getTrueValue());
738       Worklist.push_back(SI->getFalseValue());
739       continue;
740     }
741 
742     // If all values incoming to a phi node point to local memory, then so does
743     // the phi.
744     if (const PHINode *PN = dyn_cast<PHINode>(V)) {
745       // Don't bother inspecting phi nodes with many operands.
746       if (PN->getNumIncomingValues() > MaxLookup) {
747         Visited.clear();
748         return AAResultBase::pointsToConstantMemory(Loc, AAQI, OrLocal);
749       }
750       append_range(Worklist, PN->incoming_values());
751       continue;
752     }
753 
754     // Otherwise be conservative.
755     Visited.clear();
756     return AAResultBase::pointsToConstantMemory(Loc, AAQI, OrLocal);
757   } while (!Worklist.empty() && --MaxLookup);
758 
759   Visited.clear();
760   return Worklist.empty();
761 }
762 
763 static bool isIntrinsicCall(const CallBase *Call, Intrinsic::ID IID) {
764   const IntrinsicInst *II = dyn_cast<IntrinsicInst>(Call);
765   return II && II->getIntrinsicID() == IID;
766 }
767 
768 /// Returns the behavior when calling the given call site.
769 FunctionModRefBehavior BasicAAResult::getModRefBehavior(const CallBase *Call) {
770   if (Call->doesNotAccessMemory())
771     // Can't do better than this.
772     return FMRB_DoesNotAccessMemory;
773 
774   FunctionModRefBehavior Min = FMRB_UnknownModRefBehavior;
775 
776   // If the callsite knows it only reads memory, don't return worse
777   // than that.
778   if (Call->onlyReadsMemory())
779     Min = FMRB_OnlyReadsMemory;
780   else if (Call->onlyWritesMemory())
781     Min = FMRB_OnlyWritesMemory;
782 
783   if (Call->onlyAccessesArgMemory())
784     Min = FunctionModRefBehavior(Min & FMRB_OnlyAccessesArgumentPointees);
785   else if (Call->onlyAccessesInaccessibleMemory())
786     Min = FunctionModRefBehavior(Min & FMRB_OnlyAccessesInaccessibleMem);
787   else if (Call->onlyAccessesInaccessibleMemOrArgMem())
788     Min = FunctionModRefBehavior(Min & FMRB_OnlyAccessesInaccessibleOrArgMem);
789 
790   // If the call has operand bundles then aliasing attributes from the function
791   // it calls do not directly apply to the call.  This can be made more precise
792   // in the future.
793   if (!Call->hasOperandBundles())
794     if (const Function *F = Call->getCalledFunction())
795       Min =
796           FunctionModRefBehavior(Min & getBestAAResults().getModRefBehavior(F));
797 
798   return Min;
799 }
800 
801 /// Returns the behavior when calling the given function. For use when the call
802 /// site is not known.
803 FunctionModRefBehavior BasicAAResult::getModRefBehavior(const Function *F) {
804   // If the function declares it doesn't access memory, we can't do better.
805   if (F->doesNotAccessMemory())
806     return FMRB_DoesNotAccessMemory;
807 
808   FunctionModRefBehavior Min = FMRB_UnknownModRefBehavior;
809 
810   // If the function declares it only reads memory, go with that.
811   if (F->onlyReadsMemory())
812     Min = FMRB_OnlyReadsMemory;
813   else if (F->onlyWritesMemory())
814     Min = FMRB_OnlyWritesMemory;
815 
816   if (F->onlyAccessesArgMemory())
817     Min = FunctionModRefBehavior(Min & FMRB_OnlyAccessesArgumentPointees);
818   else if (F->onlyAccessesInaccessibleMemory())
819     Min = FunctionModRefBehavior(Min & FMRB_OnlyAccessesInaccessibleMem);
820   else if (F->onlyAccessesInaccessibleMemOrArgMem())
821     Min = FunctionModRefBehavior(Min & FMRB_OnlyAccessesInaccessibleOrArgMem);
822 
823   return Min;
824 }
825 
826 /// Returns true if this is a writeonly (i.e Mod only) parameter.
827 static bool isWriteOnlyParam(const CallBase *Call, unsigned ArgIdx,
828                              const TargetLibraryInfo &TLI) {
829   if (Call->paramHasAttr(ArgIdx, Attribute::WriteOnly))
830     return true;
831 
832   // We can bound the aliasing properties of memset_pattern16 just as we can
833   // for memcpy/memset.  This is particularly important because the
834   // LoopIdiomRecognizer likes to turn loops into calls to memset_pattern16
835   // whenever possible.
836   // FIXME Consider handling this in InferFunctionAttr.cpp together with other
837   // attributes.
838   LibFunc F;
839   if (Call->getCalledFunction() &&
840       TLI.getLibFunc(*Call->getCalledFunction(), F) &&
841       F == LibFunc_memset_pattern16 && TLI.has(F))
842     if (ArgIdx == 0)
843       return true;
844 
845   // TODO: memset_pattern4, memset_pattern8
846   // TODO: _chk variants
847   // TODO: strcmp, strcpy
848 
849   return false;
850 }
851 
852 ModRefInfo BasicAAResult::getArgModRefInfo(const CallBase *Call,
853                                            unsigned ArgIdx) {
854   // Checking for known builtin intrinsics and target library functions.
855   if (isWriteOnlyParam(Call, ArgIdx, TLI))
856     return ModRefInfo::Mod;
857 
858   if (Call->paramHasAttr(ArgIdx, Attribute::ReadOnly))
859     return ModRefInfo::Ref;
860 
861   if (Call->paramHasAttr(ArgIdx, Attribute::ReadNone))
862     return ModRefInfo::NoModRef;
863 
864   return AAResultBase::getArgModRefInfo(Call, ArgIdx);
865 }
866 
867 #ifndef NDEBUG
868 static const Function *getParent(const Value *V) {
869   if (const Instruction *inst = dyn_cast<Instruction>(V)) {
870     if (!inst->getParent())
871       return nullptr;
872     return inst->getParent()->getParent();
873   }
874 
875   if (const Argument *arg = dyn_cast<Argument>(V))
876     return arg->getParent();
877 
878   return nullptr;
879 }
880 
881 static bool notDifferentParent(const Value *O1, const Value *O2) {
882 
883   const Function *F1 = getParent(O1);
884   const Function *F2 = getParent(O2);
885 
886   return !F1 || !F2 || F1 == F2;
887 }
888 #endif
889 
890 AliasResult BasicAAResult::alias(const MemoryLocation &LocA,
891                                  const MemoryLocation &LocB,
892                                  AAQueryInfo &AAQI) {
893   assert(notDifferentParent(LocA.Ptr, LocB.Ptr) &&
894          "BasicAliasAnalysis doesn't support interprocedural queries.");
895   return aliasCheck(LocA.Ptr, LocA.Size, LocB.Ptr, LocB.Size, AAQI);
896 }
897 
898 /// Checks to see if the specified callsite can clobber the specified memory
899 /// object.
900 ///
901 /// Since we only look at local properties of this function, we really can't
902 /// say much about this query.  We do, however, use simple "address taken"
903 /// analysis on local objects.
904 ModRefInfo BasicAAResult::getModRefInfo(const CallBase *Call,
905                                         const MemoryLocation &Loc,
906                                         AAQueryInfo &AAQI) {
907   assert(notDifferentParent(Call, Loc.Ptr) &&
908          "AliasAnalysis query involving multiple functions!");
909 
910   const Value *Object = getUnderlyingObject(Loc.Ptr);
911 
912   // Calls marked 'tail' cannot read or write allocas from the current frame
913   // because the current frame might be destroyed by the time they run. However,
914   // a tail call may use an alloca with byval. Calling with byval copies the
915   // contents of the alloca into argument registers or stack slots, so there is
916   // no lifetime issue.
917   if (isa<AllocaInst>(Object))
918     if (const CallInst *CI = dyn_cast<CallInst>(Call))
919       if (CI->isTailCall() &&
920           !CI->getAttributes().hasAttrSomewhere(Attribute::ByVal))
921         return ModRefInfo::NoModRef;
922 
923   // Stack restore is able to modify unescaped dynamic allocas. Assume it may
924   // modify them even though the alloca is not escaped.
925   if (auto *AI = dyn_cast<AllocaInst>(Object))
926     if (!AI->isStaticAlloca() && isIntrinsicCall(Call, Intrinsic::stackrestore))
927       return ModRefInfo::Mod;
928 
929   // If the pointer is to a locally allocated object that does not escape,
930   // then the call can not mod/ref the pointer unless the call takes the pointer
931   // as an argument, and itself doesn't capture it.
932   if (!isa<Constant>(Object) && Call != Object &&
933       AAQI.CI->isNotCapturedBeforeOrAt(Object, Call)) {
934 
935     // Optimistically assume that call doesn't touch Object and check this
936     // assumption in the following loop.
937     ModRefInfo Result = ModRefInfo::NoModRef;
938     bool IsMustAlias = true;
939 
940     unsigned OperandNo = 0;
941     for (auto CI = Call->data_operands_begin(), CE = Call->data_operands_end();
942          CI != CE; ++CI, ++OperandNo) {
943       // Only look at the no-capture or byval pointer arguments.  If this
944       // pointer were passed to arguments that were neither of these, then it
945       // couldn't be no-capture.
946       if (!(*CI)->getType()->isPointerTy() ||
947           (!Call->doesNotCapture(OperandNo) && OperandNo < Call->arg_size() &&
948            !Call->isByValArgument(OperandNo)))
949         continue;
950 
951       // Call doesn't access memory through this operand, so we don't care
952       // if it aliases with Object.
953       if (Call->doesNotAccessMemory(OperandNo))
954         continue;
955 
956       // If this is a no-capture pointer argument, see if we can tell that it
957       // is impossible to alias the pointer we're checking.
958       AliasResult AR = getBestAAResults().alias(
959           MemoryLocation::getBeforeOrAfter(*CI),
960           MemoryLocation::getBeforeOrAfter(Object), AAQI);
961       if (AR != AliasResult::MustAlias)
962         IsMustAlias = false;
963       // Operand doesn't alias 'Object', continue looking for other aliases
964       if (AR == AliasResult::NoAlias)
965         continue;
966       // Operand aliases 'Object', but call doesn't modify it. Strengthen
967       // initial assumption and keep looking in case if there are more aliases.
968       if (Call->onlyReadsMemory(OperandNo)) {
969         Result = setRef(Result);
970         continue;
971       }
972       // Operand aliases 'Object' but call only writes into it.
973       if (Call->onlyWritesMemory(OperandNo)) {
974         Result = setMod(Result);
975         continue;
976       }
977       // This operand aliases 'Object' and call reads and writes into it.
978       // Setting ModRef will not yield an early return below, MustAlias is not
979       // used further.
980       Result = ModRefInfo::ModRef;
981       break;
982     }
983 
984     // No operand aliases, reset Must bit. Add below if at least one aliases
985     // and all aliases found are MustAlias.
986     if (isNoModRef(Result))
987       IsMustAlias = false;
988 
989     // Early return if we improved mod ref information
990     if (!isModAndRefSet(Result)) {
991       if (isNoModRef(Result))
992         return ModRefInfo::NoModRef;
993       return IsMustAlias ? setMust(Result) : clearMust(Result);
994     }
995   }
996 
997   // Ideally, there should be no need to special case for memcpy/memove
998   // intrinsics here since general machinery (based on memory attributes) should
999   // already handle it just fine. Unfortunately, it doesn't due to deficiency in
1000   // operand bundles support. At the moment it's not clear if complexity behind
1001   // enhancing general mechanism worths it.
1002   // TODO: Consider improving operand bundles support in general mechanism.
1003   if (auto *Inst = dyn_cast<AnyMemTransferInst>(Call)) {
1004     AliasResult SrcAA =
1005         getBestAAResults().alias(MemoryLocation::getForSource(Inst), Loc, AAQI);
1006     AliasResult DestAA =
1007         getBestAAResults().alias(MemoryLocation::getForDest(Inst), Loc, AAQI);
1008     // It's also possible for Loc to alias both src and dest, or neither.
1009     ModRefInfo rv = ModRefInfo::NoModRef;
1010     if (SrcAA != AliasResult::NoAlias || Call->hasReadingOperandBundles())
1011       rv = setRef(rv);
1012     if (DestAA != AliasResult::NoAlias || Call->hasClobberingOperandBundles())
1013       rv = setMod(rv);
1014     return rv;
1015   }
1016 
1017   // Guard intrinsics are marked as arbitrarily writing so that proper control
1018   // dependencies are maintained but they never mods any particular memory
1019   // location.
1020   //
1021   // *Unlike* assumes, guard intrinsics are modeled as reading memory since the
1022   // heap state at the point the guard is issued needs to be consistent in case
1023   // the guard invokes the "deopt" continuation.
1024   if (isIntrinsicCall(Call, Intrinsic::experimental_guard))
1025     return ModRefInfo::Ref;
1026   // The same applies to deoptimize which is essentially a guard(false).
1027   if (isIntrinsicCall(Call, Intrinsic::experimental_deoptimize))
1028     return ModRefInfo::Ref;
1029 
1030   // Like assumes, invariant.start intrinsics were also marked as arbitrarily
1031   // writing so that proper control dependencies are maintained but they never
1032   // mod any particular memory location visible to the IR.
1033   // *Unlike* assumes (which are now modeled as NoModRef), invariant.start
1034   // intrinsic is now modeled as reading memory. This prevents hoisting the
1035   // invariant.start intrinsic over stores. Consider:
1036   // *ptr = 40;
1037   // *ptr = 50;
1038   // invariant_start(ptr)
1039   // int val = *ptr;
1040   // print(val);
1041   //
1042   // This cannot be transformed to:
1043   //
1044   // *ptr = 40;
1045   // invariant_start(ptr)
1046   // *ptr = 50;
1047   // int val = *ptr;
1048   // print(val);
1049   //
1050   // The transformation will cause the second store to be ignored (based on
1051   // rules of invariant.start)  and print 40, while the first program always
1052   // prints 50.
1053   if (isIntrinsicCall(Call, Intrinsic::invariant_start))
1054     return ModRefInfo::Ref;
1055 
1056   // The AAResultBase base class has some smarts, lets use them.
1057   return AAResultBase::getModRefInfo(Call, Loc, AAQI);
1058 }
1059 
1060 ModRefInfo BasicAAResult::getModRefInfo(const CallBase *Call1,
1061                                         const CallBase *Call2,
1062                                         AAQueryInfo &AAQI) {
1063   // Guard intrinsics are marked as arbitrarily writing so that proper control
1064   // dependencies are maintained but they never mods any particular memory
1065   // location.
1066   //
1067   // *Unlike* assumes, guard intrinsics are modeled as reading memory since the
1068   // heap state at the point the guard is issued needs to be consistent in case
1069   // the guard invokes the "deopt" continuation.
1070 
1071   // NB! This function is *not* commutative, so we special case two
1072   // possibilities for guard intrinsics.
1073 
1074   if (isIntrinsicCall(Call1, Intrinsic::experimental_guard))
1075     return isModSet(createModRefInfo(getModRefBehavior(Call2)))
1076                ? ModRefInfo::Ref
1077                : ModRefInfo::NoModRef;
1078 
1079   if (isIntrinsicCall(Call2, Intrinsic::experimental_guard))
1080     return isModSet(createModRefInfo(getModRefBehavior(Call1)))
1081                ? ModRefInfo::Mod
1082                : ModRefInfo::NoModRef;
1083 
1084   // The AAResultBase base class has some smarts, lets use them.
1085   return AAResultBase::getModRefInfo(Call1, Call2, AAQI);
1086 }
1087 
1088 /// Return true if we know V to the base address of the corresponding memory
1089 /// object.  This implies that any address less than V must be out of bounds
1090 /// for the underlying object.  Note that just being isIdentifiedObject() is
1091 /// not enough - For example, a negative offset from a noalias argument or call
1092 /// can be inbounds w.r.t the actual underlying object.
1093 static bool isBaseOfObject(const Value *V) {
1094   // TODO: We can handle other cases here
1095   // 1) For GC languages, arguments to functions are often required to be
1096   //    base pointers.
1097   // 2) Result of allocation routines are often base pointers.  Leverage TLI.
1098   return (isa<AllocaInst>(V) || isa<GlobalVariable>(V));
1099 }
1100 
1101 /// Provides a bunch of ad-hoc rules to disambiguate a GEP instruction against
1102 /// another pointer.
1103 ///
1104 /// We know that V1 is a GEP, but we don't know anything about V2.
1105 /// UnderlyingV1 is getUnderlyingObject(GEP1), UnderlyingV2 is the same for
1106 /// V2.
1107 AliasResult BasicAAResult::aliasGEP(
1108     const GEPOperator *GEP1, LocationSize V1Size,
1109     const Value *V2, LocationSize V2Size,
1110     const Value *UnderlyingV1, const Value *UnderlyingV2, AAQueryInfo &AAQI) {
1111   if (!V1Size.hasValue() && !V2Size.hasValue()) {
1112     // TODO: This limitation exists for compile-time reasons. Relax it if we
1113     // can avoid exponential pathological cases.
1114     if (!isa<GEPOperator>(V2))
1115       return AliasResult::MayAlias;
1116 
1117     // If both accesses have unknown size, we can only check whether the base
1118     // objects don't alias.
1119     AliasResult BaseAlias = getBestAAResults().alias(
1120         MemoryLocation::getBeforeOrAfter(UnderlyingV1),
1121         MemoryLocation::getBeforeOrAfter(UnderlyingV2), AAQI);
1122     return BaseAlias == AliasResult::NoAlias ? AliasResult::NoAlias
1123                                              : AliasResult::MayAlias;
1124   }
1125 
1126   DecomposedGEP DecompGEP1 = DecomposeGEPExpression(GEP1, DL, &AC, DT);
1127   DecomposedGEP DecompGEP2 = DecomposeGEPExpression(V2, DL, &AC, DT);
1128 
1129   // Bail if we were not able to decompose anything.
1130   if (DecompGEP1.Base == GEP1 && DecompGEP2.Base == V2)
1131     return AliasResult::MayAlias;
1132 
1133   // Subtract the GEP2 pointer from the GEP1 pointer to find out their
1134   // symbolic difference.
1135   subtractDecomposedGEPs(DecompGEP1, DecompGEP2);
1136 
1137   // If an inbounds GEP would have to start from an out of bounds address
1138   // for the two to alias, then we can assume noalias.
1139   if (*DecompGEP1.InBounds && DecompGEP1.VarIndices.empty() &&
1140       V2Size.hasValue() && DecompGEP1.Offset.sge(V2Size.getValue()) &&
1141       isBaseOfObject(DecompGEP2.Base))
1142     return AliasResult::NoAlias;
1143 
1144   if (isa<GEPOperator>(V2)) {
1145     // Symmetric case to above.
1146     if (*DecompGEP2.InBounds && DecompGEP1.VarIndices.empty() &&
1147         V1Size.hasValue() && DecompGEP1.Offset.sle(-V1Size.getValue()) &&
1148         isBaseOfObject(DecompGEP1.Base))
1149       return AliasResult::NoAlias;
1150   }
1151 
1152   // For GEPs with identical offsets, we can preserve the size and AAInfo
1153   // when performing the alias check on the underlying objects.
1154   if (DecompGEP1.Offset == 0 && DecompGEP1.VarIndices.empty())
1155     return getBestAAResults().alias(MemoryLocation(DecompGEP1.Base, V1Size),
1156                                     MemoryLocation(DecompGEP2.Base, V2Size),
1157                                     AAQI);
1158 
1159   // Do the base pointers alias?
1160   AliasResult BaseAlias = getBestAAResults().alias(
1161       MemoryLocation::getBeforeOrAfter(DecompGEP1.Base),
1162       MemoryLocation::getBeforeOrAfter(DecompGEP2.Base), AAQI);
1163 
1164   // If we get a No or May, then return it immediately, no amount of analysis
1165   // will improve this situation.
1166   if (BaseAlias != AliasResult::MustAlias) {
1167     assert(BaseAlias == AliasResult::NoAlias ||
1168            BaseAlias == AliasResult::MayAlias);
1169     return BaseAlias;
1170   }
1171 
1172   // If there is a constant difference between the pointers, but the difference
1173   // is less than the size of the associated memory object, then we know
1174   // that the objects are partially overlapping.  If the difference is
1175   // greater, we know they do not overlap.
1176   if (DecompGEP1.VarIndices.empty()) {
1177     APInt &Off = DecompGEP1.Offset;
1178 
1179     // Initialize for Off >= 0 (V2 <= GEP1) case.
1180     const Value *LeftPtr = V2;
1181     const Value *RightPtr = GEP1;
1182     LocationSize VLeftSize = V2Size;
1183     LocationSize VRightSize = V1Size;
1184     const bool Swapped = Off.isNegative();
1185 
1186     if (Swapped) {
1187       // Swap if we have the situation where:
1188       // +                +
1189       // | BaseOffset     |
1190       // ---------------->|
1191       // |-->V1Size       |-------> V2Size
1192       // GEP1             V2
1193       std::swap(LeftPtr, RightPtr);
1194       std::swap(VLeftSize, VRightSize);
1195       Off = -Off;
1196     }
1197 
1198     if (!VLeftSize.hasValue())
1199       return AliasResult::MayAlias;
1200 
1201     const uint64_t LSize = VLeftSize.getValue();
1202     if (Off.ult(LSize)) {
1203       // Conservatively drop processing if a phi was visited and/or offset is
1204       // too big.
1205       AliasResult AR = AliasResult::PartialAlias;
1206       if (VRightSize.hasValue() && Off.ule(INT32_MAX) &&
1207           (Off + VRightSize.getValue()).ule(LSize)) {
1208         // Memory referenced by right pointer is nested. Save the offset in
1209         // cache. Note that originally offset estimated as GEP1-V2, but
1210         // AliasResult contains the shift that represents GEP1+Offset=V2.
1211         AR.setOffset(-Off.getSExtValue());
1212         AR.swap(Swapped);
1213       }
1214       return AR;
1215     }
1216     return AliasResult::NoAlias;
1217   }
1218 
1219   // We need to know both acess sizes for all the following heuristics.
1220   if (!V1Size.hasValue() || !V2Size.hasValue())
1221     return AliasResult::MayAlias;
1222 
1223   APInt GCD;
1224   ConstantRange OffsetRange = ConstantRange(DecompGEP1.Offset);
1225   for (unsigned i = 0, e = DecompGEP1.VarIndices.size(); i != e; ++i) {
1226     const VariableGEPIndex &Index = DecompGEP1.VarIndices[i];
1227     const APInt &Scale = Index.Scale;
1228     APInt ScaleForGCD = Scale;
1229     if (!Index.IsNSW)
1230       ScaleForGCD = APInt::getOneBitSet(Scale.getBitWidth(),
1231                                         Scale.countTrailingZeros());
1232 
1233     if (i == 0)
1234       GCD = ScaleForGCD.abs();
1235     else
1236       GCD = APIntOps::GreatestCommonDivisor(GCD, ScaleForGCD.abs());
1237 
1238     ConstantRange CR = computeConstantRange(Index.Val.V, /* ForSigned */ false,
1239                                             true, &AC, Index.CxtI);
1240     KnownBits Known =
1241         computeKnownBits(Index.Val.V, DL, 0, &AC, Index.CxtI, DT);
1242     CR = CR.intersectWith(
1243         ConstantRange::fromKnownBits(Known, /* Signed */ true),
1244         ConstantRange::Signed);
1245     CR = Index.Val.evaluateWith(CR).sextOrTrunc(OffsetRange.getBitWidth());
1246 
1247     assert(OffsetRange.getBitWidth() == Scale.getBitWidth() &&
1248            "Bit widths are normalized to MaxIndexSize");
1249     if (Index.IsNSW)
1250       OffsetRange = OffsetRange.add(CR.smul_sat(ConstantRange(Scale)));
1251     else
1252       OffsetRange = OffsetRange.add(CR.smul_fast(ConstantRange(Scale)));
1253   }
1254 
1255   // We now have accesses at two offsets from the same base:
1256   //  1. (...)*GCD + DecompGEP1.Offset with size V1Size
1257   //  2. 0 with size V2Size
1258   // Using arithmetic modulo GCD, the accesses are at
1259   // [ModOffset..ModOffset+V1Size) and [0..V2Size). If the first access fits
1260   // into the range [V2Size..GCD), then we know they cannot overlap.
1261   APInt ModOffset = DecompGEP1.Offset.srem(GCD);
1262   if (ModOffset.isNegative())
1263     ModOffset += GCD; // We want mod, not rem.
1264   if (ModOffset.uge(V2Size.getValue()) &&
1265       (GCD - ModOffset).uge(V1Size.getValue()))
1266     return AliasResult::NoAlias;
1267 
1268   // Compute ranges of potentially accessed bytes for both accesses. If the
1269   // interseciton is empty, there can be no overlap.
1270   unsigned BW = OffsetRange.getBitWidth();
1271   ConstantRange Range1 = OffsetRange.add(
1272       ConstantRange(APInt(BW, 0), APInt(BW, V1Size.getValue())));
1273   ConstantRange Range2 =
1274       ConstantRange(APInt(BW, 0), APInt(BW, V2Size.getValue()));
1275   if (Range1.intersectWith(Range2).isEmptySet())
1276     return AliasResult::NoAlias;
1277 
1278   // Try to determine the range of values for VarIndex such that
1279   // VarIndex <= -MinAbsVarIndex || MinAbsVarIndex <= VarIndex.
1280   Optional<APInt> MinAbsVarIndex;
1281   if (DecompGEP1.VarIndices.size() == 1) {
1282     // VarIndex = Scale*V.
1283     const VariableGEPIndex &Var = DecompGEP1.VarIndices[0];
1284     if (Var.Val.TruncBits == 0 &&
1285         isKnownNonZero(Var.Val.V, DL, 0, &AC, Var.CxtI, DT)) {
1286       // If V != 0, then abs(VarIndex) > 0.
1287       MinAbsVarIndex = APInt(Var.Scale.getBitWidth(), 1);
1288 
1289       // Check if abs(V*Scale) >= abs(Scale) holds in the presence of
1290       // potentially wrapping math.
1291       auto MultiplyByScaleNoWrap = [](const VariableGEPIndex &Var) {
1292         if (Var.IsNSW)
1293           return true;
1294 
1295         int ValOrigBW = Var.Val.V->getType()->getPrimitiveSizeInBits();
1296         // If Scale is small enough so that abs(V*Scale) >= abs(Scale) holds.
1297         // The max value of abs(V) is 2^ValOrigBW - 1. Multiplying with a
1298         // constant smaller than 2^(bitwidth(Val) - ValOrigBW) won't wrap.
1299         int MaxScaleValueBW = Var.Val.getBitWidth() - ValOrigBW;
1300         if (MaxScaleValueBW <= 0)
1301           return false;
1302         return Var.Scale.ule(
1303             APInt::getMaxValue(MaxScaleValueBW).zext(Var.Scale.getBitWidth()));
1304       };
1305       // Refine MinAbsVarIndex, if abs(Scale*V) >= abs(Scale) holds in the
1306       // presence of potentially wrapping math.
1307       if (MultiplyByScaleNoWrap(Var)) {
1308         // If V != 0 then abs(VarIndex) >= abs(Scale).
1309         MinAbsVarIndex = Var.Scale.abs();
1310       }
1311     }
1312   } else if (DecompGEP1.VarIndices.size() == 2) {
1313     // VarIndex = Scale*V0 + (-Scale)*V1.
1314     // If V0 != V1 then abs(VarIndex) >= abs(Scale).
1315     // Check that VisitedPhiBBs is empty, to avoid reasoning about
1316     // inequality of values across loop iterations.
1317     const VariableGEPIndex &Var0 = DecompGEP1.VarIndices[0];
1318     const VariableGEPIndex &Var1 = DecompGEP1.VarIndices[1];
1319     if (Var0.Scale == -Var1.Scale && Var0.Val.TruncBits == 0 &&
1320         Var0.Val.hasSameCastsAs(Var1.Val) && VisitedPhiBBs.empty() &&
1321         isKnownNonEqual(Var0.Val.V, Var1.Val.V, DL, &AC, /* CxtI */ nullptr,
1322                         DT))
1323       MinAbsVarIndex = Var0.Scale.abs();
1324   }
1325 
1326   if (MinAbsVarIndex) {
1327     // The constant offset will have added at least +/-MinAbsVarIndex to it.
1328     APInt OffsetLo = DecompGEP1.Offset - *MinAbsVarIndex;
1329     APInt OffsetHi = DecompGEP1.Offset + *MinAbsVarIndex;
1330     // We know that Offset <= OffsetLo || Offset >= OffsetHi
1331     if (OffsetLo.isNegative() && (-OffsetLo).uge(V1Size.getValue()) &&
1332         OffsetHi.isNonNegative() && OffsetHi.uge(V2Size.getValue()))
1333       return AliasResult::NoAlias;
1334   }
1335 
1336   if (constantOffsetHeuristic(DecompGEP1, V1Size, V2Size, &AC, DT))
1337     return AliasResult::NoAlias;
1338 
1339   // Statically, we can see that the base objects are the same, but the
1340   // pointers have dynamic offsets which we can't resolve. And none of our
1341   // little tricks above worked.
1342   return AliasResult::MayAlias;
1343 }
1344 
1345 static AliasResult MergeAliasResults(AliasResult A, AliasResult B) {
1346   // If the results agree, take it.
1347   if (A == B)
1348     return A;
1349   // A mix of PartialAlias and MustAlias is PartialAlias.
1350   if ((A == AliasResult::PartialAlias && B == AliasResult::MustAlias) ||
1351       (B == AliasResult::PartialAlias && A == AliasResult::MustAlias))
1352     return AliasResult::PartialAlias;
1353   // Otherwise, we don't know anything.
1354   return AliasResult::MayAlias;
1355 }
1356 
1357 /// Provides a bunch of ad-hoc rules to disambiguate a Select instruction
1358 /// against another.
1359 AliasResult
1360 BasicAAResult::aliasSelect(const SelectInst *SI, LocationSize SISize,
1361                            const Value *V2, LocationSize V2Size,
1362                            AAQueryInfo &AAQI) {
1363   // If the values are Selects with the same condition, we can do a more precise
1364   // check: just check for aliases between the values on corresponding arms.
1365   if (const SelectInst *SI2 = dyn_cast<SelectInst>(V2))
1366     if (SI->getCondition() == SI2->getCondition()) {
1367       AliasResult Alias = getBestAAResults().alias(
1368           MemoryLocation(SI->getTrueValue(), SISize),
1369           MemoryLocation(SI2->getTrueValue(), V2Size), AAQI);
1370       if (Alias == AliasResult::MayAlias)
1371         return AliasResult::MayAlias;
1372       AliasResult ThisAlias = getBestAAResults().alias(
1373           MemoryLocation(SI->getFalseValue(), SISize),
1374           MemoryLocation(SI2->getFalseValue(), V2Size), AAQI);
1375       return MergeAliasResults(ThisAlias, Alias);
1376     }
1377 
1378   // If both arms of the Select node NoAlias or MustAlias V2, then returns
1379   // NoAlias / MustAlias. Otherwise, returns MayAlias.
1380   AliasResult Alias =
1381       getBestAAResults().alias(MemoryLocation(SI->getTrueValue(), SISize),
1382                                MemoryLocation(V2, V2Size), AAQI);
1383   if (Alias == AliasResult::MayAlias)
1384     return AliasResult::MayAlias;
1385 
1386   AliasResult ThisAlias =
1387       getBestAAResults().alias(MemoryLocation(SI->getFalseValue(), SISize),
1388                                MemoryLocation(V2, V2Size), AAQI);
1389   return MergeAliasResults(ThisAlias, Alias);
1390 }
1391 
1392 /// Provide a bunch of ad-hoc rules to disambiguate a PHI instruction against
1393 /// another.
1394 AliasResult BasicAAResult::aliasPHI(const PHINode *PN, LocationSize PNSize,
1395                                     const Value *V2, LocationSize V2Size,
1396                                     AAQueryInfo &AAQI) {
1397   if (!PN->getNumIncomingValues())
1398     return AliasResult::NoAlias;
1399   // If the values are PHIs in the same block, we can do a more precise
1400   // as well as efficient check: just check for aliases between the values
1401   // on corresponding edges.
1402   if (const PHINode *PN2 = dyn_cast<PHINode>(V2))
1403     if (PN2->getParent() == PN->getParent()) {
1404       Optional<AliasResult> Alias;
1405       for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
1406         AliasResult ThisAlias = getBestAAResults().alias(
1407             MemoryLocation(PN->getIncomingValue(i), PNSize),
1408             MemoryLocation(
1409                 PN2->getIncomingValueForBlock(PN->getIncomingBlock(i)), V2Size),
1410             AAQI);
1411         if (Alias)
1412           *Alias = MergeAliasResults(*Alias, ThisAlias);
1413         else
1414           Alias = ThisAlias;
1415         if (*Alias == AliasResult::MayAlias)
1416           break;
1417       }
1418       return *Alias;
1419     }
1420 
1421   SmallVector<Value *, 4> V1Srcs;
1422   // If a phi operand recurses back to the phi, we can still determine NoAlias
1423   // if we don't alias the underlying objects of the other phi operands, as we
1424   // know that the recursive phi needs to be based on them in some way.
1425   bool isRecursive = false;
1426   auto CheckForRecPhi = [&](Value *PV) {
1427     if (!EnableRecPhiAnalysis)
1428       return false;
1429     if (getUnderlyingObject(PV) == PN) {
1430       isRecursive = true;
1431       return true;
1432     }
1433     return false;
1434   };
1435 
1436   if (PV) {
1437     // If we have PhiValues then use it to get the underlying phi values.
1438     const PhiValues::ValueSet &PhiValueSet = PV->getValuesForPhi(PN);
1439     // If we have more phi values than the search depth then return MayAlias
1440     // conservatively to avoid compile time explosion. The worst possible case
1441     // is if both sides are PHI nodes. In which case, this is O(m x n) time
1442     // where 'm' and 'n' are the number of PHI sources.
1443     if (PhiValueSet.size() > MaxLookupSearchDepth)
1444       return AliasResult::MayAlias;
1445     // Add the values to V1Srcs
1446     for (Value *PV1 : PhiValueSet) {
1447       if (CheckForRecPhi(PV1))
1448         continue;
1449       V1Srcs.push_back(PV1);
1450     }
1451   } else {
1452     // If we don't have PhiInfo then just look at the operands of the phi itself
1453     // FIXME: Remove this once we can guarantee that we have PhiInfo always
1454     SmallPtrSet<Value *, 4> UniqueSrc;
1455     Value *OnePhi = nullptr;
1456     for (Value *PV1 : PN->incoming_values()) {
1457       if (isa<PHINode>(PV1)) {
1458         if (OnePhi && OnePhi != PV1) {
1459           // To control potential compile time explosion, we choose to be
1460           // conserviate when we have more than one Phi input.  It is important
1461           // that we handle the single phi case as that lets us handle LCSSA
1462           // phi nodes and (combined with the recursive phi handling) simple
1463           // pointer induction variable patterns.
1464           return AliasResult::MayAlias;
1465         }
1466         OnePhi = PV1;
1467       }
1468 
1469       if (CheckForRecPhi(PV1))
1470         continue;
1471 
1472       if (UniqueSrc.insert(PV1).second)
1473         V1Srcs.push_back(PV1);
1474     }
1475 
1476     if (OnePhi && UniqueSrc.size() > 1)
1477       // Out of an abundance of caution, allow only the trivial lcssa and
1478       // recursive phi cases.
1479       return AliasResult::MayAlias;
1480   }
1481 
1482   // If V1Srcs is empty then that means that the phi has no underlying non-phi
1483   // value. This should only be possible in blocks unreachable from the entry
1484   // block, but return MayAlias just in case.
1485   if (V1Srcs.empty())
1486     return AliasResult::MayAlias;
1487 
1488   // If this PHI node is recursive, indicate that the pointer may be moved
1489   // across iterations. We can only prove NoAlias if different underlying
1490   // objects are involved.
1491   if (isRecursive)
1492     PNSize = LocationSize::beforeOrAfterPointer();
1493 
1494   // In the recursive alias queries below, we may compare values from two
1495   // different loop iterations. Keep track of visited phi blocks, which will
1496   // be used when determining value equivalence.
1497   bool BlockInserted = VisitedPhiBBs.insert(PN->getParent()).second;
1498   auto _ = make_scope_exit([&]() {
1499     if (BlockInserted)
1500       VisitedPhiBBs.erase(PN->getParent());
1501   });
1502 
1503   // If we inserted a block into VisitedPhiBBs, alias analysis results that
1504   // have been cached earlier may no longer be valid. Perform recursive queries
1505   // with a new AAQueryInfo.
1506   AAQueryInfo NewAAQI = AAQI.withEmptyCache();
1507   AAQueryInfo *UseAAQI = BlockInserted ? &NewAAQI : &AAQI;
1508 
1509   AliasResult Alias = getBestAAResults().alias(
1510       MemoryLocation(V1Srcs[0], PNSize), MemoryLocation(V2, V2Size), *UseAAQI);
1511 
1512   // Early exit if the check of the first PHI source against V2 is MayAlias.
1513   // Other results are not possible.
1514   if (Alias == AliasResult::MayAlias)
1515     return AliasResult::MayAlias;
1516   // With recursive phis we cannot guarantee that MustAlias/PartialAlias will
1517   // remain valid to all elements and needs to conservatively return MayAlias.
1518   if (isRecursive && Alias != AliasResult::NoAlias)
1519     return AliasResult::MayAlias;
1520 
1521   // If all sources of the PHI node NoAlias or MustAlias V2, then returns
1522   // NoAlias / MustAlias. Otherwise, returns MayAlias.
1523   for (unsigned i = 1, e = V1Srcs.size(); i != e; ++i) {
1524     Value *V = V1Srcs[i];
1525 
1526     AliasResult ThisAlias = getBestAAResults().alias(
1527         MemoryLocation(V, PNSize), MemoryLocation(V2, V2Size), *UseAAQI);
1528     Alias = MergeAliasResults(ThisAlias, Alias);
1529     if (Alias == AliasResult::MayAlias)
1530       break;
1531   }
1532 
1533   return Alias;
1534 }
1535 
1536 /// Provides a bunch of ad-hoc rules to disambiguate in common cases, such as
1537 /// array references.
1538 AliasResult BasicAAResult::aliasCheck(const Value *V1, LocationSize V1Size,
1539                                       const Value *V2, LocationSize V2Size,
1540                                       AAQueryInfo &AAQI) {
1541   // If either of the memory references is empty, it doesn't matter what the
1542   // pointer values are.
1543   if (V1Size.isZero() || V2Size.isZero())
1544     return AliasResult::NoAlias;
1545 
1546   // Strip off any casts if they exist.
1547   V1 = V1->stripPointerCastsForAliasAnalysis();
1548   V2 = V2->stripPointerCastsForAliasAnalysis();
1549 
1550   // If V1 or V2 is undef, the result is NoAlias because we can always pick a
1551   // value for undef that aliases nothing in the program.
1552   if (isa<UndefValue>(V1) || isa<UndefValue>(V2))
1553     return AliasResult::NoAlias;
1554 
1555   // Are we checking for alias of the same value?
1556   // Because we look 'through' phi nodes, we could look at "Value" pointers from
1557   // different iterations. We must therefore make sure that this is not the
1558   // case. The function isValueEqualInPotentialCycles ensures that this cannot
1559   // happen by looking at the visited phi nodes and making sure they cannot
1560   // reach the value.
1561   if (isValueEqualInPotentialCycles(V1, V2))
1562     return AliasResult::MustAlias;
1563 
1564   if (!V1->getType()->isPointerTy() || !V2->getType()->isPointerTy())
1565     return AliasResult::NoAlias; // Scalars cannot alias each other
1566 
1567   // Figure out what objects these things are pointing to if we can.
1568   const Value *O1 = getUnderlyingObject(V1, MaxLookupSearchDepth);
1569   const Value *O2 = getUnderlyingObject(V2, MaxLookupSearchDepth);
1570 
1571   // Null values in the default address space don't point to any object, so they
1572   // don't alias any other pointer.
1573   if (const ConstantPointerNull *CPN = dyn_cast<ConstantPointerNull>(O1))
1574     if (!NullPointerIsDefined(&F, CPN->getType()->getAddressSpace()))
1575       return AliasResult::NoAlias;
1576   if (const ConstantPointerNull *CPN = dyn_cast<ConstantPointerNull>(O2))
1577     if (!NullPointerIsDefined(&F, CPN->getType()->getAddressSpace()))
1578       return AliasResult::NoAlias;
1579 
1580   if (O1 != O2) {
1581     // If V1/V2 point to two different objects, we know that we have no alias.
1582     if (isIdentifiedObject(O1) && isIdentifiedObject(O2))
1583       return AliasResult::NoAlias;
1584 
1585     // Constant pointers can't alias with non-const isIdentifiedObject objects.
1586     if ((isa<Constant>(O1) && isIdentifiedObject(O2) && !isa<Constant>(O2)) ||
1587         (isa<Constant>(O2) && isIdentifiedObject(O1) && !isa<Constant>(O1)))
1588       return AliasResult::NoAlias;
1589 
1590     // Function arguments can't alias with things that are known to be
1591     // unambigously identified at the function level.
1592     if ((isa<Argument>(O1) && isIdentifiedFunctionLocal(O2)) ||
1593         (isa<Argument>(O2) && isIdentifiedFunctionLocal(O1)))
1594       return AliasResult::NoAlias;
1595 
1596     // If one pointer is the result of a call/invoke or load and the other is a
1597     // non-escaping local object within the same function, then we know the
1598     // object couldn't escape to a point where the call could return it.
1599     //
1600     // Note that if the pointers are in different functions, there are a
1601     // variety of complications. A call with a nocapture argument may still
1602     // temporary store the nocapture argument's value in a temporary memory
1603     // location if that memory location doesn't escape. Or it may pass a
1604     // nocapture value to other functions as long as they don't capture it.
1605     if (isEscapeSource(O1) &&
1606         AAQI.CI->isNotCapturedBeforeOrAt(O2, cast<Instruction>(O1)))
1607       return AliasResult::NoAlias;
1608     if (isEscapeSource(O2) &&
1609         AAQI.CI->isNotCapturedBeforeOrAt(O1, cast<Instruction>(O2)))
1610       return AliasResult::NoAlias;
1611   }
1612 
1613   // If the size of one access is larger than the entire object on the other
1614   // side, then we know such behavior is undefined and can assume no alias.
1615   bool NullIsValidLocation = NullPointerIsDefined(&F);
1616   if ((isObjectSmallerThan(
1617           O2, getMinimalExtentFrom(*V1, V1Size, DL, NullIsValidLocation), DL,
1618           TLI, NullIsValidLocation)) ||
1619       (isObjectSmallerThan(
1620           O1, getMinimalExtentFrom(*V2, V2Size, DL, NullIsValidLocation), DL,
1621           TLI, NullIsValidLocation)))
1622     return AliasResult::NoAlias;
1623 
1624   // If one the accesses may be before the accessed pointer, canonicalize this
1625   // by using unknown after-pointer sizes for both accesses. This is
1626   // equivalent, because regardless of which pointer is lower, one of them
1627   // will always came after the other, as long as the underlying objects aren't
1628   // disjoint. We do this so that the rest of BasicAA does not have to deal
1629   // with accesses before the base pointer, and to improve cache utilization by
1630   // merging equivalent states.
1631   if (V1Size.mayBeBeforePointer() || V2Size.mayBeBeforePointer()) {
1632     V1Size = LocationSize::afterPointer();
1633     V2Size = LocationSize::afterPointer();
1634   }
1635 
1636   // FIXME: If this depth limit is hit, then we may cache sub-optimal results
1637   // for recursive queries. For this reason, this limit is chosen to be large
1638   // enough to be very rarely hit, while still being small enough to avoid
1639   // stack overflows.
1640   if (AAQI.Depth >= 512)
1641     return AliasResult::MayAlias;
1642 
1643   // Check the cache before climbing up use-def chains. This also terminates
1644   // otherwise infinitely recursive queries.
1645   AAQueryInfo::LocPair Locs({V1, V1Size}, {V2, V2Size});
1646   const bool Swapped = V1 > V2;
1647   if (Swapped)
1648     std::swap(Locs.first, Locs.second);
1649   const auto &Pair = AAQI.AliasCache.try_emplace(
1650       Locs, AAQueryInfo::CacheEntry{AliasResult::NoAlias, 0});
1651   if (!Pair.second) {
1652     auto &Entry = Pair.first->second;
1653     if (!Entry.isDefinitive()) {
1654       // Remember that we used an assumption.
1655       ++Entry.NumAssumptionUses;
1656       ++AAQI.NumAssumptionUses;
1657     }
1658     // Cache contains sorted {V1,V2} pairs but we should return original order.
1659     auto Result = Entry.Result;
1660     Result.swap(Swapped);
1661     return Result;
1662   }
1663 
1664   int OrigNumAssumptionUses = AAQI.NumAssumptionUses;
1665   unsigned OrigNumAssumptionBasedResults = AAQI.AssumptionBasedResults.size();
1666   AliasResult Result =
1667       aliasCheckRecursive(V1, V1Size, V2, V2Size, AAQI, O1, O2);
1668 
1669   auto It = AAQI.AliasCache.find(Locs);
1670   assert(It != AAQI.AliasCache.end() && "Must be in cache");
1671   auto &Entry = It->second;
1672 
1673   // Check whether a NoAlias assumption has been used, but disproven.
1674   bool AssumptionDisproven =
1675       Entry.NumAssumptionUses > 0 && Result != AliasResult::NoAlias;
1676   if (AssumptionDisproven)
1677     Result = AliasResult::MayAlias;
1678 
1679   // This is a definitive result now, when considered as a root query.
1680   AAQI.NumAssumptionUses -= Entry.NumAssumptionUses;
1681   Entry.Result = Result;
1682   // Cache contains sorted {V1,V2} pairs.
1683   Entry.Result.swap(Swapped);
1684   Entry.NumAssumptionUses = -1;
1685 
1686   // If the assumption has been disproven, remove any results that may have
1687   // been based on this assumption. Do this after the Entry updates above to
1688   // avoid iterator invalidation.
1689   if (AssumptionDisproven)
1690     while (AAQI.AssumptionBasedResults.size() > OrigNumAssumptionBasedResults)
1691       AAQI.AliasCache.erase(AAQI.AssumptionBasedResults.pop_back_val());
1692 
1693   // The result may still be based on assumptions higher up in the chain.
1694   // Remember it, so it can be purged from the cache later.
1695   if (OrigNumAssumptionUses != AAQI.NumAssumptionUses &&
1696       Result != AliasResult::MayAlias)
1697     AAQI.AssumptionBasedResults.push_back(Locs);
1698   return Result;
1699 }
1700 
1701 AliasResult BasicAAResult::aliasCheckRecursive(
1702     const Value *V1, LocationSize V1Size,
1703     const Value *V2, LocationSize V2Size,
1704     AAQueryInfo &AAQI, const Value *O1, const Value *O2) {
1705   if (const GEPOperator *GV1 = dyn_cast<GEPOperator>(V1)) {
1706     AliasResult Result = aliasGEP(GV1, V1Size, V2, V2Size, O1, O2, AAQI);
1707     if (Result != AliasResult::MayAlias)
1708       return Result;
1709   } else if (const GEPOperator *GV2 = dyn_cast<GEPOperator>(V2)) {
1710     AliasResult Result = aliasGEP(GV2, V2Size, V1, V1Size, O2, O1, AAQI);
1711     Result.swap();
1712     if (Result != AliasResult::MayAlias)
1713       return Result;
1714   }
1715 
1716   if (const PHINode *PN = dyn_cast<PHINode>(V1)) {
1717     AliasResult Result = aliasPHI(PN, V1Size, V2, V2Size, AAQI);
1718     if (Result != AliasResult::MayAlias)
1719       return Result;
1720   } else if (const PHINode *PN = dyn_cast<PHINode>(V2)) {
1721     AliasResult Result = aliasPHI(PN, V2Size, V1, V1Size, AAQI);
1722     Result.swap();
1723     if (Result != AliasResult::MayAlias)
1724       return Result;
1725   }
1726 
1727   if (const SelectInst *S1 = dyn_cast<SelectInst>(V1)) {
1728     AliasResult Result = aliasSelect(S1, V1Size, V2, V2Size, AAQI);
1729     if (Result != AliasResult::MayAlias)
1730       return Result;
1731   } else if (const SelectInst *S2 = dyn_cast<SelectInst>(V2)) {
1732     AliasResult Result = aliasSelect(S2, V2Size, V1, V1Size, AAQI);
1733     Result.swap();
1734     if (Result != AliasResult::MayAlias)
1735       return Result;
1736   }
1737 
1738   // If both pointers are pointing into the same object and one of them
1739   // accesses the entire object, then the accesses must overlap in some way.
1740   if (O1 == O2) {
1741     bool NullIsValidLocation = NullPointerIsDefined(&F);
1742     if (V1Size.isPrecise() && V2Size.isPrecise() &&
1743         (isObjectSize(O1, V1Size.getValue(), DL, TLI, NullIsValidLocation) ||
1744          isObjectSize(O2, V2Size.getValue(), DL, TLI, NullIsValidLocation)))
1745       return AliasResult::PartialAlias;
1746   }
1747 
1748   return AliasResult::MayAlias;
1749 }
1750 
1751 /// Check whether two Values can be considered equivalent.
1752 ///
1753 /// In addition to pointer equivalence of \p V1 and \p V2 this checks whether
1754 /// they can not be part of a cycle in the value graph by looking at all
1755 /// visited phi nodes an making sure that the phis cannot reach the value. We
1756 /// have to do this because we are looking through phi nodes (That is we say
1757 /// noalias(V, phi(VA, VB)) if noalias(V, VA) and noalias(V, VB).
1758 bool BasicAAResult::isValueEqualInPotentialCycles(const Value *V,
1759                                                   const Value *V2) {
1760   if (V != V2)
1761     return false;
1762 
1763   const Instruction *Inst = dyn_cast<Instruction>(V);
1764   if (!Inst)
1765     return true;
1766 
1767   if (VisitedPhiBBs.empty())
1768     return true;
1769 
1770   if (VisitedPhiBBs.size() > MaxNumPhiBBsValueReachabilityCheck)
1771     return false;
1772 
1773   // Make sure that the visited phis cannot reach the Value. This ensures that
1774   // the Values cannot come from different iterations of a potential cycle the
1775   // phi nodes could be involved in.
1776   for (auto *P : VisitedPhiBBs)
1777     if (isPotentiallyReachable(&P->front(), Inst, nullptr, DT))
1778       return false;
1779 
1780   return true;
1781 }
1782 
1783 /// Computes the symbolic difference between two de-composed GEPs.
1784 void BasicAAResult::subtractDecomposedGEPs(DecomposedGEP &DestGEP,
1785                                            const DecomposedGEP &SrcGEP) {
1786   DestGEP.Offset -= SrcGEP.Offset;
1787   for (const VariableGEPIndex &Src : SrcGEP.VarIndices) {
1788     // Find V in Dest.  This is N^2, but pointer indices almost never have more
1789     // than a few variable indexes.
1790     bool Found = false;
1791     for (auto I : enumerate(DestGEP.VarIndices)) {
1792       VariableGEPIndex &Dest = I.value();
1793       if (!isValueEqualInPotentialCycles(Dest.Val.V, Src.Val.V) ||
1794           !Dest.Val.hasSameCastsAs(Src.Val))
1795         continue;
1796 
1797       // If we found it, subtract off Scale V's from the entry in Dest.  If it
1798       // goes to zero, remove the entry.
1799       if (Dest.Scale != Src.Scale) {
1800         Dest.Scale -= Src.Scale;
1801         Dest.IsNSW = false;
1802       } else {
1803         DestGEP.VarIndices.erase(DestGEP.VarIndices.begin() + I.index());
1804       }
1805       Found = true;
1806       break;
1807     }
1808 
1809     // If we didn't consume this entry, add it to the end of the Dest list.
1810     if (!Found) {
1811       VariableGEPIndex Entry = {Src.Val, -Src.Scale, Src.CxtI, Src.IsNSW};
1812       DestGEP.VarIndices.push_back(Entry);
1813     }
1814   }
1815 }
1816 
1817 bool BasicAAResult::constantOffsetHeuristic(
1818     const DecomposedGEP &GEP, LocationSize MaybeV1Size,
1819     LocationSize MaybeV2Size, AssumptionCache *AC, DominatorTree *DT) {
1820   if (GEP.VarIndices.size() != 2 || !MaybeV1Size.hasValue() ||
1821       !MaybeV2Size.hasValue())
1822     return false;
1823 
1824   const uint64_t V1Size = MaybeV1Size.getValue();
1825   const uint64_t V2Size = MaybeV2Size.getValue();
1826 
1827   const VariableGEPIndex &Var0 = GEP.VarIndices[0], &Var1 = GEP.VarIndices[1];
1828 
1829   if (Var0.Val.TruncBits != 0 || !Var0.Val.hasSameCastsAs(Var1.Val) ||
1830       Var0.Scale != -Var1.Scale ||
1831       Var0.Val.V->getType() != Var1.Val.V->getType())
1832     return false;
1833 
1834   // We'll strip off the Extensions of Var0 and Var1 and do another round
1835   // of GetLinearExpression decomposition. In the example above, if Var0
1836   // is zext(%x + 1) we should get V1 == %x and V1Offset == 1.
1837 
1838   LinearExpression E0 =
1839       GetLinearExpression(CastedValue(Var0.Val.V), DL, 0, AC, DT);
1840   LinearExpression E1 =
1841       GetLinearExpression(CastedValue(Var1.Val.V), DL, 0, AC, DT);
1842   if (E0.Scale != E1.Scale || !E0.Val.hasSameCastsAs(E1.Val) ||
1843       !isValueEqualInPotentialCycles(E0.Val.V, E1.Val.V))
1844     return false;
1845 
1846   // We have a hit - Var0 and Var1 only differ by a constant offset!
1847 
1848   // If we've been sext'ed then zext'd the maximum difference between Var0 and
1849   // Var1 is possible to calculate, but we're just interested in the absolute
1850   // minimum difference between the two. The minimum distance may occur due to
1851   // wrapping; consider "add i3 %i, 5": if %i == 7 then 7 + 5 mod 8 == 4, and so
1852   // the minimum distance between %i and %i + 5 is 3.
1853   APInt MinDiff = E0.Offset - E1.Offset, Wrapped = -MinDiff;
1854   MinDiff = APIntOps::umin(MinDiff, Wrapped);
1855   APInt MinDiffBytes =
1856     MinDiff.zextOrTrunc(Var0.Scale.getBitWidth()) * Var0.Scale.abs();
1857 
1858   // We can't definitely say whether GEP1 is before or after V2 due to wrapping
1859   // arithmetic (i.e. for some values of GEP1 and V2 GEP1 < V2, and for other
1860   // values GEP1 > V2). We'll therefore only declare NoAlias if both V1Size and
1861   // V2Size can fit in the MinDiffBytes gap.
1862   return MinDiffBytes.uge(V1Size + GEP.Offset.abs()) &&
1863          MinDiffBytes.uge(V2Size + GEP.Offset.abs());
1864 }
1865 
1866 //===----------------------------------------------------------------------===//
1867 // BasicAliasAnalysis Pass
1868 //===----------------------------------------------------------------------===//
1869 
1870 AnalysisKey BasicAA::Key;
1871 
1872 BasicAAResult BasicAA::run(Function &F, FunctionAnalysisManager &AM) {
1873   auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
1874   auto &AC = AM.getResult<AssumptionAnalysis>(F);
1875   auto *DT = &AM.getResult<DominatorTreeAnalysis>(F);
1876   auto *PV = AM.getCachedResult<PhiValuesAnalysis>(F);
1877   return BasicAAResult(F.getParent()->getDataLayout(), F, TLI, AC, DT, PV);
1878 }
1879 
1880 BasicAAWrapperPass::BasicAAWrapperPass() : FunctionPass(ID) {
1881   initializeBasicAAWrapperPassPass(*PassRegistry::getPassRegistry());
1882 }
1883 
1884 char BasicAAWrapperPass::ID = 0;
1885 
1886 void BasicAAWrapperPass::anchor() {}
1887 
1888 INITIALIZE_PASS_BEGIN(BasicAAWrapperPass, "basic-aa",
1889                       "Basic Alias Analysis (stateless AA impl)", true, true)
1890 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
1891 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
1892 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
1893 INITIALIZE_PASS_DEPENDENCY(PhiValuesWrapperPass)
1894 INITIALIZE_PASS_END(BasicAAWrapperPass, "basic-aa",
1895                     "Basic Alias Analysis (stateless AA impl)", true, true)
1896 
1897 FunctionPass *llvm::createBasicAAWrapperPass() {
1898   return new BasicAAWrapperPass();
1899 }
1900 
1901 bool BasicAAWrapperPass::runOnFunction(Function &F) {
1902   auto &ACT = getAnalysis<AssumptionCacheTracker>();
1903   auto &TLIWP = getAnalysis<TargetLibraryInfoWrapperPass>();
1904   auto &DTWP = getAnalysis<DominatorTreeWrapperPass>();
1905   auto *PVWP = getAnalysisIfAvailable<PhiValuesWrapperPass>();
1906 
1907   Result.reset(new BasicAAResult(F.getParent()->getDataLayout(), F,
1908                                  TLIWP.getTLI(F), ACT.getAssumptionCache(F),
1909                                  &DTWP.getDomTree(),
1910                                  PVWP ? &PVWP->getResult() : nullptr));
1911 
1912   return false;
1913 }
1914 
1915 void BasicAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
1916   AU.setPreservesAll();
1917   AU.addRequiredTransitive<AssumptionCacheTracker>();
1918   AU.addRequiredTransitive<DominatorTreeWrapperPass>();
1919   AU.addRequiredTransitive<TargetLibraryInfoWrapperPass>();
1920   AU.addUsedIfAvailable<PhiValuesWrapperPass>();
1921 }
1922 
1923 BasicAAResult llvm::createLegacyPMBasicAAResult(Pass &P, Function &F) {
1924   return BasicAAResult(
1925       F.getParent()->getDataLayout(), F,
1926       P.getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F),
1927       P.getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F));
1928 }
1929