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