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