106f32e7eSjoerg //===- BasicAliasAnalysis.cpp - Stateless Alias Analysis Impl -------------===//
206f32e7eSjoerg //
306f32e7eSjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
406f32e7eSjoerg // See https://llvm.org/LICENSE.txt for license information.
506f32e7eSjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
606f32e7eSjoerg //
706f32e7eSjoerg //===----------------------------------------------------------------------===//
806f32e7eSjoerg //
906f32e7eSjoerg // This file defines the primary stateless implementation of the
1006f32e7eSjoerg // Alias Analysis interface that implements identities (two different
1106f32e7eSjoerg // globals cannot alias, etc), but does no stateful analysis.
1206f32e7eSjoerg //
1306f32e7eSjoerg //===----------------------------------------------------------------------===//
1406f32e7eSjoerg 
1506f32e7eSjoerg #include "llvm/Analysis/BasicAliasAnalysis.h"
1606f32e7eSjoerg #include "llvm/ADT/APInt.h"
17*da58b97aSjoerg #include "llvm/ADT/ScopeExit.h"
1806f32e7eSjoerg #include "llvm/ADT/SmallPtrSet.h"
1906f32e7eSjoerg #include "llvm/ADT/SmallVector.h"
2006f32e7eSjoerg #include "llvm/ADT/Statistic.h"
2106f32e7eSjoerg #include "llvm/Analysis/AliasAnalysis.h"
2206f32e7eSjoerg #include "llvm/Analysis/AssumptionCache.h"
2306f32e7eSjoerg #include "llvm/Analysis/CFG.h"
2406f32e7eSjoerg #include "llvm/Analysis/CaptureTracking.h"
2506f32e7eSjoerg #include "llvm/Analysis/InstructionSimplify.h"
2606f32e7eSjoerg #include "llvm/Analysis/MemoryBuiltins.h"
2706f32e7eSjoerg #include "llvm/Analysis/MemoryLocation.h"
28*da58b97aSjoerg #include "llvm/Analysis/PhiValues.h"
2906f32e7eSjoerg #include "llvm/Analysis/TargetLibraryInfo.h"
3006f32e7eSjoerg #include "llvm/Analysis/ValueTracking.h"
3106f32e7eSjoerg #include "llvm/IR/Argument.h"
3206f32e7eSjoerg #include "llvm/IR/Attributes.h"
3306f32e7eSjoerg #include "llvm/IR/Constant.h"
3406f32e7eSjoerg #include "llvm/IR/Constants.h"
3506f32e7eSjoerg #include "llvm/IR/DataLayout.h"
3606f32e7eSjoerg #include "llvm/IR/DerivedTypes.h"
3706f32e7eSjoerg #include "llvm/IR/Dominators.h"
3806f32e7eSjoerg #include "llvm/IR/Function.h"
3906f32e7eSjoerg #include "llvm/IR/GetElementPtrTypeIterator.h"
4006f32e7eSjoerg #include "llvm/IR/GlobalAlias.h"
4106f32e7eSjoerg #include "llvm/IR/GlobalVariable.h"
4206f32e7eSjoerg #include "llvm/IR/InstrTypes.h"
4306f32e7eSjoerg #include "llvm/IR/Instruction.h"
4406f32e7eSjoerg #include "llvm/IR/Instructions.h"
4506f32e7eSjoerg #include "llvm/IR/IntrinsicInst.h"
4606f32e7eSjoerg #include "llvm/IR/Intrinsics.h"
4706f32e7eSjoerg #include "llvm/IR/Metadata.h"
4806f32e7eSjoerg #include "llvm/IR/Operator.h"
4906f32e7eSjoerg #include "llvm/IR/Type.h"
5006f32e7eSjoerg #include "llvm/IR/User.h"
5106f32e7eSjoerg #include "llvm/IR/Value.h"
52*da58b97aSjoerg #include "llvm/InitializePasses.h"
5306f32e7eSjoerg #include "llvm/Pass.h"
5406f32e7eSjoerg #include "llvm/Support/Casting.h"
5506f32e7eSjoerg #include "llvm/Support/CommandLine.h"
5606f32e7eSjoerg #include "llvm/Support/Compiler.h"
5706f32e7eSjoerg #include "llvm/Support/KnownBits.h"
5806f32e7eSjoerg #include <cassert>
5906f32e7eSjoerg #include <cstdint>
6006f32e7eSjoerg #include <cstdlib>
6106f32e7eSjoerg #include <utility>
6206f32e7eSjoerg 
6306f32e7eSjoerg #define DEBUG_TYPE "basicaa"
6406f32e7eSjoerg 
6506f32e7eSjoerg using namespace llvm;
6606f32e7eSjoerg 
6706f32e7eSjoerg /// Enable analysis of recursive PHI nodes.
68*da58b97aSjoerg static cl::opt<bool> EnableRecPhiAnalysis("basic-aa-recphi", cl::Hidden,
69*da58b97aSjoerg                                           cl::init(true));
7006f32e7eSjoerg 
7106f32e7eSjoerg /// By default, even on 32-bit architectures we use 64-bit integers for
7206f32e7eSjoerg /// calculations. This will allow us to more-aggressively decompose indexing
7306f32e7eSjoerg /// expressions calculated using i64 values (e.g., long long in C) which is
7406f32e7eSjoerg /// common enough to worry about.
75*da58b97aSjoerg static cl::opt<bool> ForceAtLeast64Bits("basic-aa-force-at-least-64b",
7606f32e7eSjoerg                                         cl::Hidden, cl::init(true));
77*da58b97aSjoerg static cl::opt<bool> DoubleCalcBits("basic-aa-double-calc-bits",
7806f32e7eSjoerg                                     cl::Hidden, cl::init(false));
7906f32e7eSjoerg 
8006f32e7eSjoerg /// SearchLimitReached / SearchTimes shows how often the limit of
8106f32e7eSjoerg /// to decompose GEPs is reached. It will affect the precision
8206f32e7eSjoerg /// of basic alias analysis.
8306f32e7eSjoerg STATISTIC(SearchLimitReached, "Number of times the limit to "
8406f32e7eSjoerg                               "decompose GEPs is reached");
8506f32e7eSjoerg STATISTIC(SearchTimes, "Number of times a GEP is decomposed");
8606f32e7eSjoerg 
8706f32e7eSjoerg /// Cutoff after which to stop analysing a set of phi nodes potentially involved
8806f32e7eSjoerg /// in a cycle. Because we are analysing 'through' phi nodes, we need to be
8906f32e7eSjoerg /// careful with value equivalence. We use reachability to make sure a value
9006f32e7eSjoerg /// cannot be involved in a cycle.
9106f32e7eSjoerg const unsigned MaxNumPhiBBsValueReachabilityCheck = 20;
9206f32e7eSjoerg 
9306f32e7eSjoerg // The max limit of the search depth in DecomposeGEPExpression() and
94*da58b97aSjoerg // getUnderlyingObject(), both functions need to use the same search
9506f32e7eSjoerg // depth otherwise the algorithm in aliasGEP will assert.
9606f32e7eSjoerg static const unsigned MaxLookupSearchDepth = 6;
9706f32e7eSjoerg 
invalidate(Function & Fn,const PreservedAnalyses & PA,FunctionAnalysisManager::Invalidator & Inv)9806f32e7eSjoerg bool BasicAAResult::invalidate(Function &Fn, const PreservedAnalyses &PA,
9906f32e7eSjoerg                                FunctionAnalysisManager::Invalidator &Inv) {
10006f32e7eSjoerg   // We don't care if this analysis itself is preserved, it has no state. But
10106f32e7eSjoerg   // we need to check that the analyses it depends on have been. Note that we
10206f32e7eSjoerg   // may be created without handles to some analyses and in that case don't
10306f32e7eSjoerg   // depend on them.
10406f32e7eSjoerg   if (Inv.invalidate<AssumptionAnalysis>(Fn, PA) ||
10506f32e7eSjoerg       (DT && Inv.invalidate<DominatorTreeAnalysis>(Fn, PA)) ||
10606f32e7eSjoerg       (PV && Inv.invalidate<PhiValuesAnalysis>(Fn, PA)))
10706f32e7eSjoerg     return true;
10806f32e7eSjoerg 
10906f32e7eSjoerg   // Otherwise this analysis result remains valid.
11006f32e7eSjoerg   return false;
11106f32e7eSjoerg }
11206f32e7eSjoerg 
11306f32e7eSjoerg //===----------------------------------------------------------------------===//
11406f32e7eSjoerg // Useful predicates
11506f32e7eSjoerg //===----------------------------------------------------------------------===//
11606f32e7eSjoerg 
11706f32e7eSjoerg /// Returns true if the pointer is one which would have been considered an
11806f32e7eSjoerg /// escape by isNonEscapingLocalObject.
isEscapeSource(const Value * V)11906f32e7eSjoerg static bool isEscapeSource(const Value *V) {
12006f32e7eSjoerg   if (isa<CallBase>(V))
12106f32e7eSjoerg     return true;
12206f32e7eSjoerg 
12306f32e7eSjoerg   if (isa<Argument>(V))
12406f32e7eSjoerg     return true;
12506f32e7eSjoerg 
12606f32e7eSjoerg   // The load case works because isNonEscapingLocalObject considers all
12706f32e7eSjoerg   // stores to be escapes (it passes true for the StoreCaptures argument
12806f32e7eSjoerg   // to PointerMayBeCaptured).
12906f32e7eSjoerg   if (isa<LoadInst>(V))
13006f32e7eSjoerg     return true;
13106f32e7eSjoerg 
132*da58b97aSjoerg   // The inttoptr case works because isNonEscapingLocalObject considers all
133*da58b97aSjoerg   // means of converting or equating a pointer to an int (ptrtoint, ptr store
134*da58b97aSjoerg   // which could be followed by an integer load, ptr<->int compare) as
135*da58b97aSjoerg   // escaping, and objects located at well-known addresses via platform-specific
136*da58b97aSjoerg   // means cannot be considered non-escaping local objects.
137*da58b97aSjoerg   if (isa<IntToPtrInst>(V))
138*da58b97aSjoerg     return true;
139*da58b97aSjoerg 
14006f32e7eSjoerg   return false;
14106f32e7eSjoerg }
14206f32e7eSjoerg 
14306f32e7eSjoerg /// Returns the size of the object specified by V or UnknownSize if unknown.
getObjectSize(const Value * V,const DataLayout & DL,const TargetLibraryInfo & TLI,bool NullIsValidLoc,bool RoundToAlign=false)14406f32e7eSjoerg static uint64_t getObjectSize(const Value *V, const DataLayout &DL,
14506f32e7eSjoerg                               const TargetLibraryInfo &TLI,
14606f32e7eSjoerg                               bool NullIsValidLoc,
14706f32e7eSjoerg                               bool RoundToAlign = false) {
14806f32e7eSjoerg   uint64_t Size;
14906f32e7eSjoerg   ObjectSizeOpts Opts;
15006f32e7eSjoerg   Opts.RoundToAlign = RoundToAlign;
15106f32e7eSjoerg   Opts.NullIsUnknownSize = NullIsValidLoc;
15206f32e7eSjoerg   if (getObjectSize(V, Size, DL, &TLI, Opts))
15306f32e7eSjoerg     return Size;
15406f32e7eSjoerg   return MemoryLocation::UnknownSize;
15506f32e7eSjoerg }
15606f32e7eSjoerg 
15706f32e7eSjoerg /// Returns true if we can prove that the object specified by V is smaller than
15806f32e7eSjoerg /// Size.
isObjectSmallerThan(const Value * V,uint64_t Size,const DataLayout & DL,const TargetLibraryInfo & TLI,bool NullIsValidLoc)15906f32e7eSjoerg static bool isObjectSmallerThan(const Value *V, uint64_t Size,
16006f32e7eSjoerg                                 const DataLayout &DL,
16106f32e7eSjoerg                                 const TargetLibraryInfo &TLI,
16206f32e7eSjoerg                                 bool NullIsValidLoc) {
16306f32e7eSjoerg   // Note that the meanings of the "object" are slightly different in the
16406f32e7eSjoerg   // following contexts:
16506f32e7eSjoerg   //    c1: llvm::getObjectSize()
16606f32e7eSjoerg   //    c2: llvm.objectsize() intrinsic
16706f32e7eSjoerg   //    c3: isObjectSmallerThan()
16806f32e7eSjoerg   // c1 and c2 share the same meaning; however, the meaning of "object" in c3
16906f32e7eSjoerg   // refers to the "entire object".
17006f32e7eSjoerg   //
17106f32e7eSjoerg   //  Consider this example:
17206f32e7eSjoerg   //     char *p = (char*)malloc(100)
17306f32e7eSjoerg   //     char *q = p+80;
17406f32e7eSjoerg   //
17506f32e7eSjoerg   //  In the context of c1 and c2, the "object" pointed by q refers to the
17606f32e7eSjoerg   // stretch of memory of q[0:19]. So, getObjectSize(q) should return 20.
17706f32e7eSjoerg   //
17806f32e7eSjoerg   //  However, in the context of c3, the "object" refers to the chunk of memory
17906f32e7eSjoerg   // being allocated. So, the "object" has 100 bytes, and q points to the middle
18006f32e7eSjoerg   // the "object". In case q is passed to isObjectSmallerThan() as the 1st
18106f32e7eSjoerg   // parameter, before the llvm::getObjectSize() is called to get the size of
18206f32e7eSjoerg   // entire object, we should:
18306f32e7eSjoerg   //    - either rewind the pointer q to the base-address of the object in
18406f32e7eSjoerg   //      question (in this case rewind to p), or
18506f32e7eSjoerg   //    - just give up. It is up to caller to make sure the pointer is pointing
18606f32e7eSjoerg   //      to the base address the object.
18706f32e7eSjoerg   //
18806f32e7eSjoerg   // We go for 2nd option for simplicity.
18906f32e7eSjoerg   if (!isIdentifiedObject(V))
19006f32e7eSjoerg     return false;
19106f32e7eSjoerg 
19206f32e7eSjoerg   // This function needs to use the aligned object size because we allow
19306f32e7eSjoerg   // reads a bit past the end given sufficient alignment.
19406f32e7eSjoerg   uint64_t ObjectSize = getObjectSize(V, DL, TLI, NullIsValidLoc,
19506f32e7eSjoerg                                       /*RoundToAlign*/ true);
19606f32e7eSjoerg 
19706f32e7eSjoerg   return ObjectSize != MemoryLocation::UnknownSize && ObjectSize < Size;
19806f32e7eSjoerg }
19906f32e7eSjoerg 
20006f32e7eSjoerg /// Return the minimal extent from \p V to the end of the underlying object,
20106f32e7eSjoerg /// assuming the result is used in an aliasing query. E.g., we do use the query
20206f32e7eSjoerg /// location size and the fact that null pointers cannot alias here.
getMinimalExtentFrom(const Value & V,const LocationSize & LocSize,const DataLayout & DL,bool NullIsValidLoc)20306f32e7eSjoerg static uint64_t getMinimalExtentFrom(const Value &V,
20406f32e7eSjoerg                                      const LocationSize &LocSize,
20506f32e7eSjoerg                                      const DataLayout &DL,
20606f32e7eSjoerg                                      bool NullIsValidLoc) {
20706f32e7eSjoerg   // If we have dereferenceability information we know a lower bound for the
20806f32e7eSjoerg   // extent as accesses for a lower offset would be valid. We need to exclude
20906f32e7eSjoerg   // the "or null" part if null is a valid pointer.
210*da58b97aSjoerg   bool CanBeNull, CanBeFreed;
211*da58b97aSjoerg   uint64_t DerefBytes =
212*da58b97aSjoerg     V.getPointerDereferenceableBytes(DL, CanBeNull, CanBeFreed);
21306f32e7eSjoerg   DerefBytes = (CanBeNull && NullIsValidLoc) ? 0 : DerefBytes;
214*da58b97aSjoerg   DerefBytes = CanBeFreed ? 0 : DerefBytes;
21506f32e7eSjoerg   // If queried with a precise location size, we assume that location size to be
21606f32e7eSjoerg   // accessed, thus valid.
21706f32e7eSjoerg   if (LocSize.isPrecise())
21806f32e7eSjoerg     DerefBytes = std::max(DerefBytes, LocSize.getValue());
21906f32e7eSjoerg   return DerefBytes;
22006f32e7eSjoerg }
22106f32e7eSjoerg 
22206f32e7eSjoerg /// Returns true if we can prove that the object specified by V has size Size.
isObjectSize(const Value * V,uint64_t Size,const DataLayout & DL,const TargetLibraryInfo & TLI,bool NullIsValidLoc)22306f32e7eSjoerg static bool isObjectSize(const Value *V, uint64_t Size, const DataLayout &DL,
22406f32e7eSjoerg                          const TargetLibraryInfo &TLI, bool NullIsValidLoc) {
22506f32e7eSjoerg   uint64_t ObjectSize = getObjectSize(V, DL, TLI, NullIsValidLoc);
22606f32e7eSjoerg   return ObjectSize != MemoryLocation::UnknownSize && ObjectSize == Size;
22706f32e7eSjoerg }
22806f32e7eSjoerg 
22906f32e7eSjoerg //===----------------------------------------------------------------------===//
23006f32e7eSjoerg // GetElementPtr Instruction Decomposition and Analysis
23106f32e7eSjoerg //===----------------------------------------------------------------------===//
23206f32e7eSjoerg 
233*da58b97aSjoerg namespace {
234*da58b97aSjoerg /// Represents zext(sext(V)).
235*da58b97aSjoerg struct ExtendedValue {
236*da58b97aSjoerg   const Value *V;
237*da58b97aSjoerg   unsigned ZExtBits;
238*da58b97aSjoerg   unsigned SExtBits;
239*da58b97aSjoerg 
ExtendedValue__anon800a57740111::ExtendedValue240*da58b97aSjoerg   explicit ExtendedValue(const Value *V, unsigned ZExtBits = 0,
241*da58b97aSjoerg                          unsigned SExtBits = 0)
242*da58b97aSjoerg       : V(V), ZExtBits(ZExtBits), SExtBits(SExtBits) {}
243*da58b97aSjoerg 
getBitWidth__anon800a57740111::ExtendedValue244*da58b97aSjoerg   unsigned getBitWidth() const {
245*da58b97aSjoerg     return V->getType()->getPrimitiveSizeInBits() + ZExtBits + SExtBits;
246*da58b97aSjoerg   }
247*da58b97aSjoerg 
withValue__anon800a57740111::ExtendedValue248*da58b97aSjoerg   ExtendedValue withValue(const Value *NewV) const {
249*da58b97aSjoerg     return ExtendedValue(NewV, ZExtBits, SExtBits);
250*da58b97aSjoerg   }
251*da58b97aSjoerg 
withZExtOfValue__anon800a57740111::ExtendedValue252*da58b97aSjoerg   ExtendedValue withZExtOfValue(const Value *NewV) const {
253*da58b97aSjoerg     unsigned ExtendBy = V->getType()->getPrimitiveSizeInBits() -
254*da58b97aSjoerg                         NewV->getType()->getPrimitiveSizeInBits();
255*da58b97aSjoerg     // zext(sext(zext(NewV))) == zext(zext(zext(NewV)))
256*da58b97aSjoerg     return ExtendedValue(NewV, ZExtBits + SExtBits + ExtendBy, 0);
257*da58b97aSjoerg   }
258*da58b97aSjoerg 
withSExtOfValue__anon800a57740111::ExtendedValue259*da58b97aSjoerg   ExtendedValue withSExtOfValue(const Value *NewV) const {
260*da58b97aSjoerg     unsigned ExtendBy = V->getType()->getPrimitiveSizeInBits() -
261*da58b97aSjoerg                         NewV->getType()->getPrimitiveSizeInBits();
262*da58b97aSjoerg     // zext(sext(sext(NewV)))
263*da58b97aSjoerg     return ExtendedValue(NewV, ZExtBits, SExtBits + ExtendBy);
264*da58b97aSjoerg   }
265*da58b97aSjoerg 
evaluateWith__anon800a57740111::ExtendedValue266*da58b97aSjoerg   APInt evaluateWith(APInt N) const {
267*da58b97aSjoerg     assert(N.getBitWidth() == V->getType()->getPrimitiveSizeInBits() &&
268*da58b97aSjoerg            "Incompatible bit width");
269*da58b97aSjoerg     if (SExtBits) N = N.sext(N.getBitWidth() + SExtBits);
270*da58b97aSjoerg     if (ZExtBits) N = N.zext(N.getBitWidth() + ZExtBits);
271*da58b97aSjoerg     return N;
272*da58b97aSjoerg   }
273*da58b97aSjoerg 
canDistributeOver__anon800a57740111::ExtendedValue274*da58b97aSjoerg   bool canDistributeOver(bool NUW, bool NSW) const {
275*da58b97aSjoerg     // zext(x op<nuw> y) == zext(x) op<nuw> zext(y)
276*da58b97aSjoerg     // sext(x op<nsw> y) == sext(x) op<nsw> sext(y)
277*da58b97aSjoerg     return (!ZExtBits || NUW) && (!SExtBits || NSW);
278*da58b97aSjoerg   }
279*da58b97aSjoerg };
280*da58b97aSjoerg 
281*da58b97aSjoerg /// Represents zext(sext(V)) * Scale + Offset.
282*da58b97aSjoerg struct LinearExpression {
283*da58b97aSjoerg   ExtendedValue Val;
284*da58b97aSjoerg   APInt Scale;
285*da58b97aSjoerg   APInt Offset;
286*da58b97aSjoerg 
LinearExpression__anon800a57740111::LinearExpression287*da58b97aSjoerg   LinearExpression(const ExtendedValue &Val, const APInt &Scale,
288*da58b97aSjoerg                    const APInt &Offset)
289*da58b97aSjoerg       : Val(Val), Scale(Scale), Offset(Offset) {}
290*da58b97aSjoerg 
LinearExpression__anon800a57740111::LinearExpression291*da58b97aSjoerg   LinearExpression(const ExtendedValue &Val) : Val(Val) {
292*da58b97aSjoerg     unsigned BitWidth = Val.getBitWidth();
293*da58b97aSjoerg     Scale = APInt(BitWidth, 1);
294*da58b97aSjoerg     Offset = APInt(BitWidth, 0);
295*da58b97aSjoerg   }
296*da58b97aSjoerg };
297*da58b97aSjoerg }
298*da58b97aSjoerg 
29906f32e7eSjoerg /// Analyzes the specified value as a linear expression: "A*V + B", where A and
30006f32e7eSjoerg /// B are constant integers.
GetLinearExpression(const ExtendedValue & Val,const DataLayout & DL,unsigned Depth,AssumptionCache * AC,DominatorTree * DT)301*da58b97aSjoerg static LinearExpression GetLinearExpression(
302*da58b97aSjoerg     const ExtendedValue &Val,  const DataLayout &DL, unsigned Depth,
303*da58b97aSjoerg     AssumptionCache *AC, DominatorTree *DT) {
30406f32e7eSjoerg   // Limit our recursion depth.
305*da58b97aSjoerg   if (Depth == 6)
306*da58b97aSjoerg     return Val;
30706f32e7eSjoerg 
308*da58b97aSjoerg   if (const ConstantInt *Const = dyn_cast<ConstantInt>(Val.V))
309*da58b97aSjoerg     return LinearExpression(Val, APInt(Val.getBitWidth(), 0),
310*da58b97aSjoerg                             Val.evaluateWith(Const->getValue()));
31106f32e7eSjoerg 
312*da58b97aSjoerg   if (const BinaryOperator *BOp = dyn_cast<BinaryOperator>(Val.V)) {
31306f32e7eSjoerg     if (ConstantInt *RHSC = dyn_cast<ConstantInt>(BOp->getOperand(1))) {
314*da58b97aSjoerg       APInt RHS = Val.evaluateWith(RHSC->getValue());
315*da58b97aSjoerg       // The only non-OBO case we deal with is or, and only limited to the
316*da58b97aSjoerg       // case where it is both nuw and nsw.
317*da58b97aSjoerg       bool NUW = true, NSW = true;
318*da58b97aSjoerg       if (isa<OverflowingBinaryOperator>(BOp)) {
319*da58b97aSjoerg         NUW &= BOp->hasNoUnsignedWrap();
320*da58b97aSjoerg         NSW &= BOp->hasNoSignedWrap();
321*da58b97aSjoerg       }
322*da58b97aSjoerg       if (!Val.canDistributeOver(NUW, NSW))
323*da58b97aSjoerg         return Val;
32406f32e7eSjoerg 
32506f32e7eSjoerg       switch (BOp->getOpcode()) {
32606f32e7eSjoerg       default:
32706f32e7eSjoerg         // We don't understand this instruction, so we can't decompose it any
32806f32e7eSjoerg         // further.
329*da58b97aSjoerg         return Val;
33006f32e7eSjoerg       case Instruction::Or:
33106f32e7eSjoerg         // X|C == X+C if all the bits in C are unset in X.  Otherwise we can't
33206f32e7eSjoerg         // analyze it.
33306f32e7eSjoerg         if (!MaskedValueIsZero(BOp->getOperand(0), RHSC->getValue(), DL, 0, AC,
334*da58b97aSjoerg                                BOp, DT))
335*da58b97aSjoerg           return Val;
33606f32e7eSjoerg 
337*da58b97aSjoerg         LLVM_FALLTHROUGH;
338*da58b97aSjoerg       case Instruction::Add: {
339*da58b97aSjoerg         LinearExpression E = GetLinearExpression(
340*da58b97aSjoerg             Val.withValue(BOp->getOperand(0)), DL, Depth + 1, AC, DT);
341*da58b97aSjoerg         E.Offset += RHS;
342*da58b97aSjoerg         return E;
343*da58b97aSjoerg       }
344*da58b97aSjoerg       case Instruction::Sub: {
345*da58b97aSjoerg         LinearExpression E = GetLinearExpression(
346*da58b97aSjoerg             Val.withValue(BOp->getOperand(0)), DL, Depth + 1, AC, DT);
347*da58b97aSjoerg         E.Offset -= RHS;
348*da58b97aSjoerg         return E;
349*da58b97aSjoerg       }
350*da58b97aSjoerg       case Instruction::Mul: {
351*da58b97aSjoerg         LinearExpression E = GetLinearExpression(
352*da58b97aSjoerg             Val.withValue(BOp->getOperand(0)), DL, Depth + 1, AC, DT);
353*da58b97aSjoerg         E.Offset *= RHS;
354*da58b97aSjoerg         E.Scale *= RHS;
355*da58b97aSjoerg         return E;
356*da58b97aSjoerg       }
357*da58b97aSjoerg       case Instruction::Shl:
35806f32e7eSjoerg         // We're trying to linearize an expression of the kind:
35906f32e7eSjoerg         //   shl i8 -128, 36
36006f32e7eSjoerg         // where the shift count exceeds the bitwidth of the type.
36106f32e7eSjoerg         // We can't decompose this further (the expression would return
36206f32e7eSjoerg         // a poison value).
363*da58b97aSjoerg         if (RHS.getLimitedValue() > Val.getBitWidth())
364*da58b97aSjoerg           return Val;
36506f32e7eSjoerg 
366*da58b97aSjoerg         LinearExpression E = GetLinearExpression(
367*da58b97aSjoerg             Val.withValue(BOp->getOperand(0)), DL, Depth + 1, AC, DT);
368*da58b97aSjoerg         E.Offset <<= RHS.getLimitedValue();
369*da58b97aSjoerg         E.Scale <<= RHS.getLimitedValue();
370*da58b97aSjoerg         return E;
37106f32e7eSjoerg       }
37206f32e7eSjoerg     }
37306f32e7eSjoerg   }
37406f32e7eSjoerg 
375*da58b97aSjoerg   if (isa<ZExtInst>(Val.V))
376*da58b97aSjoerg     return GetLinearExpression(
377*da58b97aSjoerg         Val.withZExtOfValue(cast<CastInst>(Val.V)->getOperand(0)),
378*da58b97aSjoerg         DL, Depth + 1, AC, DT);
37906f32e7eSjoerg 
380*da58b97aSjoerg   if (isa<SExtInst>(Val.V))
381*da58b97aSjoerg     return GetLinearExpression(
382*da58b97aSjoerg         Val.withSExtOfValue(cast<CastInst>(Val.V)->getOperand(0)),
383*da58b97aSjoerg         DL, Depth + 1, AC, DT);
38406f32e7eSjoerg 
385*da58b97aSjoerg   return Val;
38606f32e7eSjoerg }
38706f32e7eSjoerg 
38806f32e7eSjoerg /// To ensure a pointer offset fits in an integer of size PointerSize
38906f32e7eSjoerg /// (in bits) when that size is smaller than the maximum pointer size. This is
39006f32e7eSjoerg /// an issue, for example, in particular for 32b pointers with negative indices
39106f32e7eSjoerg /// that rely on two's complement wrap-arounds for precise alias information
39206f32e7eSjoerg /// where the maximum pointer size is 64b.
adjustToPointerSize(const APInt & Offset,unsigned PointerSize)393*da58b97aSjoerg static APInt adjustToPointerSize(const APInt &Offset, unsigned PointerSize) {
39406f32e7eSjoerg   assert(PointerSize <= Offset.getBitWidth() && "Invalid PointerSize!");
39506f32e7eSjoerg   unsigned ShiftBits = Offset.getBitWidth() - PointerSize;
39606f32e7eSjoerg   return (Offset << ShiftBits).ashr(ShiftBits);
39706f32e7eSjoerg }
39806f32e7eSjoerg 
getMaxPointerSize(const DataLayout & DL)39906f32e7eSjoerg static unsigned getMaxPointerSize(const DataLayout &DL) {
40006f32e7eSjoerg   unsigned MaxPointerSize = DL.getMaxPointerSizeInBits();
40106f32e7eSjoerg   if (MaxPointerSize < 64 && ForceAtLeast64Bits) MaxPointerSize = 64;
40206f32e7eSjoerg   if (DoubleCalcBits) MaxPointerSize *= 2;
40306f32e7eSjoerg 
40406f32e7eSjoerg   return MaxPointerSize;
40506f32e7eSjoerg }
40606f32e7eSjoerg 
40706f32e7eSjoerg /// If V is a symbolic pointer expression, decompose it into a base pointer
40806f32e7eSjoerg /// with a constant offset and a number of scaled symbolic offsets.
40906f32e7eSjoerg ///
41006f32e7eSjoerg /// The scaled symbolic offsets (represented by pairs of a Value* and a scale
41106f32e7eSjoerg /// in the VarIndices vector) are Value*'s that are known to be scaled by the
41206f32e7eSjoerg /// specified amount, but which may have other unrepresented high bits. As
41306f32e7eSjoerg /// such, the gep cannot necessarily be reconstructed from its decomposed form.
41406f32e7eSjoerg ///
415*da58b97aSjoerg /// This function is capable of analyzing everything that getUnderlyingObject
416*da58b97aSjoerg /// can look through. To be able to do that getUnderlyingObject and
417*da58b97aSjoerg /// DecomposeGEPExpression must use the same search depth
418*da58b97aSjoerg /// (MaxLookupSearchDepth).
419*da58b97aSjoerg BasicAAResult::DecomposedGEP
DecomposeGEPExpression(const Value * V,const DataLayout & DL,AssumptionCache * AC,DominatorTree * DT)420*da58b97aSjoerg BasicAAResult::DecomposeGEPExpression(const Value *V, const DataLayout &DL,
421*da58b97aSjoerg                                       AssumptionCache *AC, DominatorTree *DT) {
42206f32e7eSjoerg   // Limit recursion depth to limit compile time in crazy cases.
42306f32e7eSjoerg   unsigned MaxLookup = MaxLookupSearchDepth;
42406f32e7eSjoerg   SearchTimes++;
425*da58b97aSjoerg   const Instruction *CxtI = dyn_cast<Instruction>(V);
42606f32e7eSjoerg 
42706f32e7eSjoerg   unsigned MaxPointerSize = getMaxPointerSize(DL);
428*da58b97aSjoerg   DecomposedGEP Decomposed;
429*da58b97aSjoerg   Decomposed.Offset = APInt(MaxPointerSize, 0);
430*da58b97aSjoerg   Decomposed.HasCompileTimeConstantScale = true;
43106f32e7eSjoerg   do {
43206f32e7eSjoerg     // See if this is a bitcast or GEP.
43306f32e7eSjoerg     const Operator *Op = dyn_cast<Operator>(V);
43406f32e7eSjoerg     if (!Op) {
43506f32e7eSjoerg       // The only non-operator case we can handle are GlobalAliases.
43606f32e7eSjoerg       if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
43706f32e7eSjoerg         if (!GA->isInterposable()) {
43806f32e7eSjoerg           V = GA->getAliasee();
43906f32e7eSjoerg           continue;
44006f32e7eSjoerg         }
44106f32e7eSjoerg       }
44206f32e7eSjoerg       Decomposed.Base = V;
443*da58b97aSjoerg       return Decomposed;
44406f32e7eSjoerg     }
44506f32e7eSjoerg 
44606f32e7eSjoerg     if (Op->getOpcode() == Instruction::BitCast ||
44706f32e7eSjoerg         Op->getOpcode() == Instruction::AddrSpaceCast) {
44806f32e7eSjoerg       V = Op->getOperand(0);
44906f32e7eSjoerg       continue;
45006f32e7eSjoerg     }
45106f32e7eSjoerg 
45206f32e7eSjoerg     const GEPOperator *GEPOp = dyn_cast<GEPOperator>(Op);
45306f32e7eSjoerg     if (!GEPOp) {
454*da58b97aSjoerg       if (const auto *PHI = dyn_cast<PHINode>(V)) {
455*da58b97aSjoerg         // Look through single-arg phi nodes created by LCSSA.
456*da58b97aSjoerg         if (PHI->getNumIncomingValues() == 1) {
457*da58b97aSjoerg           V = PHI->getIncomingValue(0);
458*da58b97aSjoerg           continue;
459*da58b97aSjoerg         }
460*da58b97aSjoerg       } else if (const auto *Call = dyn_cast<CallBase>(V)) {
46106f32e7eSjoerg         // CaptureTracking can know about special capturing properties of some
46206f32e7eSjoerg         // intrinsics like launder.invariant.group, that can't be expressed with
46306f32e7eSjoerg         // the attributes, but have properties like returning aliasing pointer.
46406f32e7eSjoerg         // Because some analysis may assume that nocaptured pointer is not
46506f32e7eSjoerg         // returned from some special intrinsic (because function would have to
46606f32e7eSjoerg         // be marked with returns attribute), it is crucial to use this function
46706f32e7eSjoerg         // because it should be in sync with CaptureTracking. Not using it may
46806f32e7eSjoerg         // cause weird miscompilations where 2 aliasing pointers are assumed to
46906f32e7eSjoerg         // noalias.
47006f32e7eSjoerg         if (auto *RP = getArgumentAliasingToReturnedPointer(Call, false)) {
47106f32e7eSjoerg           V = RP;
47206f32e7eSjoerg           continue;
47306f32e7eSjoerg         }
47406f32e7eSjoerg       }
47506f32e7eSjoerg 
476*da58b97aSjoerg       Decomposed.Base = V;
477*da58b97aSjoerg       return Decomposed;
47806f32e7eSjoerg     }
47906f32e7eSjoerg 
480*da58b97aSjoerg     // Track whether we've seen at least one in bounds gep, and if so, whether
481*da58b97aSjoerg     // all geps parsed were in bounds.
482*da58b97aSjoerg     if (Decomposed.InBounds == None)
483*da58b97aSjoerg       Decomposed.InBounds = GEPOp->isInBounds();
484*da58b97aSjoerg     else if (!GEPOp->isInBounds())
485*da58b97aSjoerg       Decomposed.InBounds = false;
48606f32e7eSjoerg 
48706f32e7eSjoerg     // Don't attempt to analyze GEPs over unsized objects.
48806f32e7eSjoerg     if (!GEPOp->getSourceElementType()->isSized()) {
48906f32e7eSjoerg       Decomposed.Base = V;
490*da58b97aSjoerg       return Decomposed;
491*da58b97aSjoerg     }
492*da58b97aSjoerg 
493*da58b97aSjoerg     // Don't attempt to analyze GEPs if index scale is not a compile-time
494*da58b97aSjoerg     // constant.
495*da58b97aSjoerg     if (isa<ScalableVectorType>(GEPOp->getSourceElementType())) {
496*da58b97aSjoerg       Decomposed.Base = V;
497*da58b97aSjoerg       Decomposed.HasCompileTimeConstantScale = false;
498*da58b97aSjoerg       return Decomposed;
49906f32e7eSjoerg     }
50006f32e7eSjoerg 
50106f32e7eSjoerg     unsigned AS = GEPOp->getPointerAddressSpace();
50206f32e7eSjoerg     // Walk the indices of the GEP, accumulating them into BaseOff/VarIndices.
50306f32e7eSjoerg     gep_type_iterator GTI = gep_type_begin(GEPOp);
50406f32e7eSjoerg     unsigned PointerSize = DL.getPointerSizeInBits(AS);
50506f32e7eSjoerg     // Assume all GEP operands are constants until proven otherwise.
50606f32e7eSjoerg     bool GepHasConstantOffset = true;
50706f32e7eSjoerg     for (User::const_op_iterator I = GEPOp->op_begin() + 1, E = GEPOp->op_end();
50806f32e7eSjoerg          I != E; ++I, ++GTI) {
50906f32e7eSjoerg       const Value *Index = *I;
51006f32e7eSjoerg       // Compute the (potentially symbolic) offset in bytes for this index.
51106f32e7eSjoerg       if (StructType *STy = GTI.getStructTypeOrNull()) {
51206f32e7eSjoerg         // For a struct, add the member offset.
51306f32e7eSjoerg         unsigned FieldNo = cast<ConstantInt>(Index)->getZExtValue();
51406f32e7eSjoerg         if (FieldNo == 0)
51506f32e7eSjoerg           continue;
51606f32e7eSjoerg 
517*da58b97aSjoerg         Decomposed.Offset += DL.getStructLayout(STy)->getElementOffset(FieldNo);
51806f32e7eSjoerg         continue;
51906f32e7eSjoerg       }
52006f32e7eSjoerg 
52106f32e7eSjoerg       // For an array/pointer, add the element offset, explicitly scaled.
52206f32e7eSjoerg       if (const ConstantInt *CIdx = dyn_cast<ConstantInt>(Index)) {
52306f32e7eSjoerg         if (CIdx->isZero())
52406f32e7eSjoerg           continue;
525*da58b97aSjoerg         Decomposed.Offset +=
526*da58b97aSjoerg             DL.getTypeAllocSize(GTI.getIndexedType()).getFixedSize() *
527*da58b97aSjoerg             CIdx->getValue().sextOrTrunc(MaxPointerSize);
52806f32e7eSjoerg         continue;
52906f32e7eSjoerg       }
53006f32e7eSjoerg 
53106f32e7eSjoerg       GepHasConstantOffset = false;
53206f32e7eSjoerg 
533*da58b97aSjoerg       APInt Scale(MaxPointerSize,
534*da58b97aSjoerg                   DL.getTypeAllocSize(GTI.getIndexedType()).getFixedSize());
53506f32e7eSjoerg       // If the integer type is smaller than the pointer size, it is implicitly
53606f32e7eSjoerg       // sign extended to pointer size.
53706f32e7eSjoerg       unsigned Width = Index->getType()->getIntegerBitWidth();
538*da58b97aSjoerg       unsigned SExtBits = PointerSize > Width ? PointerSize - Width : 0;
539*da58b97aSjoerg       LinearExpression LE = GetLinearExpression(
540*da58b97aSjoerg           ExtendedValue(Index, 0, SExtBits), DL, 0, AC, DT);
54106f32e7eSjoerg 
54206f32e7eSjoerg       // The GEP index scale ("Scale") scales C1*V+C2, yielding (C1*V+C2)*Scale.
54306f32e7eSjoerg       // This gives us an aggregate computation of (C1*Scale)*V + C2*Scale.
54406f32e7eSjoerg 
54506f32e7eSjoerg       // It can be the case that, even through C1*V+C2 does not overflow for
54606f32e7eSjoerg       // relevant values of V, (C2*Scale) can overflow. In that case, we cannot
54706f32e7eSjoerg       // decompose the expression in this way.
54806f32e7eSjoerg       //
54906f32e7eSjoerg       // FIXME: C1*Scale and the other operations in the decomposed
55006f32e7eSjoerg       // (C1*Scale)*V+C2*Scale can also overflow. We should check for this
55106f32e7eSjoerg       // possibility.
552*da58b97aSjoerg       bool Overflow;
553*da58b97aSjoerg       APInt ScaledOffset = LE.Offset.sextOrTrunc(MaxPointerSize)
554*da58b97aSjoerg                            .smul_ov(Scale, Overflow);
555*da58b97aSjoerg       if (Overflow) {
556*da58b97aSjoerg         LE = LinearExpression(ExtendedValue(Index, 0, SExtBits));
55706f32e7eSjoerg       } else {
558*da58b97aSjoerg         Decomposed.Offset += ScaledOffset;
559*da58b97aSjoerg         Scale *= LE.Scale.sextOrTrunc(MaxPointerSize);
56006f32e7eSjoerg       }
56106f32e7eSjoerg 
56206f32e7eSjoerg       // If we already had an occurrence of this index variable, merge this
56306f32e7eSjoerg       // scale into it.  For example, we want to handle:
56406f32e7eSjoerg       //   A[x][x] -> x*16 + x*4 -> x*20
56506f32e7eSjoerg       // This also ensures that 'x' only appears in the index list once.
56606f32e7eSjoerg       for (unsigned i = 0, e = Decomposed.VarIndices.size(); i != e; ++i) {
567*da58b97aSjoerg         if (Decomposed.VarIndices[i].V == LE.Val.V &&
568*da58b97aSjoerg             Decomposed.VarIndices[i].ZExtBits == LE.Val.ZExtBits &&
569*da58b97aSjoerg             Decomposed.VarIndices[i].SExtBits == LE.Val.SExtBits) {
57006f32e7eSjoerg           Scale += Decomposed.VarIndices[i].Scale;
57106f32e7eSjoerg           Decomposed.VarIndices.erase(Decomposed.VarIndices.begin() + i);
57206f32e7eSjoerg           break;
57306f32e7eSjoerg         }
57406f32e7eSjoerg       }
57506f32e7eSjoerg 
57606f32e7eSjoerg       // Make sure that we have a scale that makes sense for this target's
57706f32e7eSjoerg       // pointer size.
57806f32e7eSjoerg       Scale = adjustToPointerSize(Scale, PointerSize);
57906f32e7eSjoerg 
58006f32e7eSjoerg       if (!!Scale) {
581*da58b97aSjoerg         VariableGEPIndex Entry = {LE.Val.V, LE.Val.ZExtBits, LE.Val.SExtBits,
582*da58b97aSjoerg                                   Scale, CxtI};
58306f32e7eSjoerg         Decomposed.VarIndices.push_back(Entry);
58406f32e7eSjoerg       }
58506f32e7eSjoerg     }
58606f32e7eSjoerg 
58706f32e7eSjoerg     // Take care of wrap-arounds
588*da58b97aSjoerg     if (GepHasConstantOffset)
589*da58b97aSjoerg       Decomposed.Offset = adjustToPointerSize(Decomposed.Offset, PointerSize);
59006f32e7eSjoerg 
59106f32e7eSjoerg     // Analyze the base pointer next.
59206f32e7eSjoerg     V = GEPOp->getOperand(0);
59306f32e7eSjoerg   } while (--MaxLookup);
59406f32e7eSjoerg 
59506f32e7eSjoerg   // If the chain of expressions is too deep, just return early.
59606f32e7eSjoerg   Decomposed.Base = V;
59706f32e7eSjoerg   SearchLimitReached++;
598*da58b97aSjoerg   return Decomposed;
59906f32e7eSjoerg }
60006f32e7eSjoerg 
60106f32e7eSjoerg /// Returns whether the given pointer value points to memory that is local to
60206f32e7eSjoerg /// the function, with global constants being considered local to all
60306f32e7eSjoerg /// functions.
pointsToConstantMemory(const MemoryLocation & Loc,AAQueryInfo & AAQI,bool OrLocal)60406f32e7eSjoerg bool BasicAAResult::pointsToConstantMemory(const MemoryLocation &Loc,
60506f32e7eSjoerg                                            AAQueryInfo &AAQI, bool OrLocal) {
60606f32e7eSjoerg   assert(Visited.empty() && "Visited must be cleared after use!");
60706f32e7eSjoerg 
60806f32e7eSjoerg   unsigned MaxLookup = 8;
60906f32e7eSjoerg   SmallVector<const Value *, 16> Worklist;
61006f32e7eSjoerg   Worklist.push_back(Loc.Ptr);
61106f32e7eSjoerg   do {
612*da58b97aSjoerg     const Value *V = getUnderlyingObject(Worklist.pop_back_val());
61306f32e7eSjoerg     if (!Visited.insert(V).second) {
61406f32e7eSjoerg       Visited.clear();
61506f32e7eSjoerg       return AAResultBase::pointsToConstantMemory(Loc, AAQI, OrLocal);
61606f32e7eSjoerg     }
61706f32e7eSjoerg 
61806f32e7eSjoerg     // An alloca instruction defines local memory.
61906f32e7eSjoerg     if (OrLocal && isa<AllocaInst>(V))
62006f32e7eSjoerg       continue;
62106f32e7eSjoerg 
62206f32e7eSjoerg     // A global constant counts as local memory for our purposes.
62306f32e7eSjoerg     if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
62406f32e7eSjoerg       // Note: this doesn't require GV to be "ODR" because it isn't legal for a
62506f32e7eSjoerg       // global to be marked constant in some modules and non-constant in
62606f32e7eSjoerg       // others.  GV may even be a declaration, not a definition.
62706f32e7eSjoerg       if (!GV->isConstant()) {
62806f32e7eSjoerg         Visited.clear();
62906f32e7eSjoerg         return AAResultBase::pointsToConstantMemory(Loc, AAQI, OrLocal);
63006f32e7eSjoerg       }
63106f32e7eSjoerg       continue;
63206f32e7eSjoerg     }
63306f32e7eSjoerg 
63406f32e7eSjoerg     // If both select values point to local memory, then so does the select.
63506f32e7eSjoerg     if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {
63606f32e7eSjoerg       Worklist.push_back(SI->getTrueValue());
63706f32e7eSjoerg       Worklist.push_back(SI->getFalseValue());
63806f32e7eSjoerg       continue;
63906f32e7eSjoerg     }
64006f32e7eSjoerg 
64106f32e7eSjoerg     // If all values incoming to a phi node point to local memory, then so does
64206f32e7eSjoerg     // the phi.
64306f32e7eSjoerg     if (const PHINode *PN = dyn_cast<PHINode>(V)) {
64406f32e7eSjoerg       // Don't bother inspecting phi nodes with many operands.
64506f32e7eSjoerg       if (PN->getNumIncomingValues() > MaxLookup) {
64606f32e7eSjoerg         Visited.clear();
64706f32e7eSjoerg         return AAResultBase::pointsToConstantMemory(Loc, AAQI, OrLocal);
64806f32e7eSjoerg       }
649*da58b97aSjoerg       append_range(Worklist, PN->incoming_values());
65006f32e7eSjoerg       continue;
65106f32e7eSjoerg     }
65206f32e7eSjoerg 
65306f32e7eSjoerg     // Otherwise be conservative.
65406f32e7eSjoerg     Visited.clear();
65506f32e7eSjoerg     return AAResultBase::pointsToConstantMemory(Loc, AAQI, OrLocal);
65606f32e7eSjoerg   } while (!Worklist.empty() && --MaxLookup);
65706f32e7eSjoerg 
65806f32e7eSjoerg   Visited.clear();
65906f32e7eSjoerg   return Worklist.empty();
66006f32e7eSjoerg }
66106f32e7eSjoerg 
isIntrinsicCall(const CallBase * Call,Intrinsic::ID IID)662*da58b97aSjoerg static bool isIntrinsicCall(const CallBase *Call, Intrinsic::ID IID) {
663*da58b97aSjoerg   const IntrinsicInst *II = dyn_cast<IntrinsicInst>(Call);
664*da58b97aSjoerg   return II && II->getIntrinsicID() == IID;
665*da58b97aSjoerg }
666*da58b97aSjoerg 
66706f32e7eSjoerg /// Returns the behavior when calling the given call site.
getModRefBehavior(const CallBase * Call)66806f32e7eSjoerg FunctionModRefBehavior BasicAAResult::getModRefBehavior(const CallBase *Call) {
66906f32e7eSjoerg   if (Call->doesNotAccessMemory())
67006f32e7eSjoerg     // Can't do better than this.
67106f32e7eSjoerg     return FMRB_DoesNotAccessMemory;
67206f32e7eSjoerg 
67306f32e7eSjoerg   FunctionModRefBehavior Min = FMRB_UnknownModRefBehavior;
67406f32e7eSjoerg 
67506f32e7eSjoerg   // If the callsite knows it only reads memory, don't return worse
67606f32e7eSjoerg   // than that.
67706f32e7eSjoerg   if (Call->onlyReadsMemory())
67806f32e7eSjoerg     Min = FMRB_OnlyReadsMemory;
67906f32e7eSjoerg   else if (Call->doesNotReadMemory())
680*da58b97aSjoerg     Min = FMRB_OnlyWritesMemory;
68106f32e7eSjoerg 
68206f32e7eSjoerg   if (Call->onlyAccessesArgMemory())
68306f32e7eSjoerg     Min = FunctionModRefBehavior(Min & FMRB_OnlyAccessesArgumentPointees);
68406f32e7eSjoerg   else if (Call->onlyAccessesInaccessibleMemory())
68506f32e7eSjoerg     Min = FunctionModRefBehavior(Min & FMRB_OnlyAccessesInaccessibleMem);
68606f32e7eSjoerg   else if (Call->onlyAccessesInaccessibleMemOrArgMem())
68706f32e7eSjoerg     Min = FunctionModRefBehavior(Min & FMRB_OnlyAccessesInaccessibleOrArgMem);
68806f32e7eSjoerg 
68906f32e7eSjoerg   // If the call has operand bundles then aliasing attributes from the function
69006f32e7eSjoerg   // it calls do not directly apply to the call.  This can be made more precise
69106f32e7eSjoerg   // in the future.
69206f32e7eSjoerg   if (!Call->hasOperandBundles())
69306f32e7eSjoerg     if (const Function *F = Call->getCalledFunction())
69406f32e7eSjoerg       Min =
69506f32e7eSjoerg           FunctionModRefBehavior(Min & getBestAAResults().getModRefBehavior(F));
69606f32e7eSjoerg 
69706f32e7eSjoerg   return Min;
69806f32e7eSjoerg }
69906f32e7eSjoerg 
70006f32e7eSjoerg /// Returns the behavior when calling the given function. For use when the call
70106f32e7eSjoerg /// site is not known.
getModRefBehavior(const Function * F)70206f32e7eSjoerg FunctionModRefBehavior BasicAAResult::getModRefBehavior(const Function *F) {
70306f32e7eSjoerg   // If the function declares it doesn't access memory, we can't do better.
70406f32e7eSjoerg   if (F->doesNotAccessMemory())
70506f32e7eSjoerg     return FMRB_DoesNotAccessMemory;
70606f32e7eSjoerg 
70706f32e7eSjoerg   FunctionModRefBehavior Min = FMRB_UnknownModRefBehavior;
70806f32e7eSjoerg 
70906f32e7eSjoerg   // If the function declares it only reads memory, go with that.
71006f32e7eSjoerg   if (F->onlyReadsMemory())
71106f32e7eSjoerg     Min = FMRB_OnlyReadsMemory;
71206f32e7eSjoerg   else if (F->doesNotReadMemory())
713*da58b97aSjoerg     Min = FMRB_OnlyWritesMemory;
71406f32e7eSjoerg 
71506f32e7eSjoerg   if (F->onlyAccessesArgMemory())
71606f32e7eSjoerg     Min = FunctionModRefBehavior(Min & FMRB_OnlyAccessesArgumentPointees);
71706f32e7eSjoerg   else if (F->onlyAccessesInaccessibleMemory())
71806f32e7eSjoerg     Min = FunctionModRefBehavior(Min & FMRB_OnlyAccessesInaccessibleMem);
71906f32e7eSjoerg   else if (F->onlyAccessesInaccessibleMemOrArgMem())
72006f32e7eSjoerg     Min = FunctionModRefBehavior(Min & FMRB_OnlyAccessesInaccessibleOrArgMem);
72106f32e7eSjoerg 
72206f32e7eSjoerg   return Min;
72306f32e7eSjoerg }
72406f32e7eSjoerg 
72506f32e7eSjoerg /// Returns true if this is a writeonly (i.e Mod only) parameter.
isWriteOnlyParam(const CallBase * Call,unsigned ArgIdx,const TargetLibraryInfo & TLI)72606f32e7eSjoerg static bool isWriteOnlyParam(const CallBase *Call, unsigned ArgIdx,
72706f32e7eSjoerg                              const TargetLibraryInfo &TLI) {
72806f32e7eSjoerg   if (Call->paramHasAttr(ArgIdx, Attribute::WriteOnly))
72906f32e7eSjoerg     return true;
73006f32e7eSjoerg 
73106f32e7eSjoerg   // We can bound the aliasing properties of memset_pattern16 just as we can
73206f32e7eSjoerg   // for memcpy/memset.  This is particularly important because the
73306f32e7eSjoerg   // LoopIdiomRecognizer likes to turn loops into calls to memset_pattern16
73406f32e7eSjoerg   // whenever possible.
73506f32e7eSjoerg   // FIXME Consider handling this in InferFunctionAttr.cpp together with other
73606f32e7eSjoerg   // attributes.
73706f32e7eSjoerg   LibFunc F;
73806f32e7eSjoerg   if (Call->getCalledFunction() &&
73906f32e7eSjoerg       TLI.getLibFunc(*Call->getCalledFunction(), F) &&
74006f32e7eSjoerg       F == LibFunc_memset_pattern16 && TLI.has(F))
74106f32e7eSjoerg     if (ArgIdx == 0)
74206f32e7eSjoerg       return true;
74306f32e7eSjoerg 
74406f32e7eSjoerg   // TODO: memset_pattern4, memset_pattern8
74506f32e7eSjoerg   // TODO: _chk variants
74606f32e7eSjoerg   // TODO: strcmp, strcpy
74706f32e7eSjoerg 
74806f32e7eSjoerg   return false;
74906f32e7eSjoerg }
75006f32e7eSjoerg 
getArgModRefInfo(const CallBase * Call,unsigned ArgIdx)75106f32e7eSjoerg ModRefInfo BasicAAResult::getArgModRefInfo(const CallBase *Call,
75206f32e7eSjoerg                                            unsigned ArgIdx) {
75306f32e7eSjoerg   // Checking for known builtin intrinsics and target library functions.
75406f32e7eSjoerg   if (isWriteOnlyParam(Call, ArgIdx, TLI))
75506f32e7eSjoerg     return ModRefInfo::Mod;
75606f32e7eSjoerg 
75706f32e7eSjoerg   if (Call->paramHasAttr(ArgIdx, Attribute::ReadOnly))
75806f32e7eSjoerg     return ModRefInfo::Ref;
75906f32e7eSjoerg 
76006f32e7eSjoerg   if (Call->paramHasAttr(ArgIdx, Attribute::ReadNone))
76106f32e7eSjoerg     return ModRefInfo::NoModRef;
76206f32e7eSjoerg 
76306f32e7eSjoerg   return AAResultBase::getArgModRefInfo(Call, ArgIdx);
76406f32e7eSjoerg }
76506f32e7eSjoerg 
76606f32e7eSjoerg #ifndef NDEBUG
getParent(const Value * V)76706f32e7eSjoerg static const Function *getParent(const Value *V) {
76806f32e7eSjoerg   if (const Instruction *inst = dyn_cast<Instruction>(V)) {
76906f32e7eSjoerg     if (!inst->getParent())
77006f32e7eSjoerg       return nullptr;
77106f32e7eSjoerg     return inst->getParent()->getParent();
77206f32e7eSjoerg   }
77306f32e7eSjoerg 
77406f32e7eSjoerg   if (const Argument *arg = dyn_cast<Argument>(V))
77506f32e7eSjoerg     return arg->getParent();
77606f32e7eSjoerg 
77706f32e7eSjoerg   return nullptr;
77806f32e7eSjoerg }
77906f32e7eSjoerg 
notDifferentParent(const Value * O1,const Value * O2)78006f32e7eSjoerg static bool notDifferentParent(const Value *O1, const Value *O2) {
78106f32e7eSjoerg 
78206f32e7eSjoerg   const Function *F1 = getParent(O1);
78306f32e7eSjoerg   const Function *F2 = getParent(O2);
78406f32e7eSjoerg 
78506f32e7eSjoerg   return !F1 || !F2 || F1 == F2;
78606f32e7eSjoerg }
78706f32e7eSjoerg #endif
78806f32e7eSjoerg 
alias(const MemoryLocation & LocA,const MemoryLocation & LocB,AAQueryInfo & AAQI)78906f32e7eSjoerg AliasResult BasicAAResult::alias(const MemoryLocation &LocA,
79006f32e7eSjoerg                                  const MemoryLocation &LocB,
79106f32e7eSjoerg                                  AAQueryInfo &AAQI) {
79206f32e7eSjoerg   assert(notDifferentParent(LocA.Ptr, LocB.Ptr) &&
79306f32e7eSjoerg          "BasicAliasAnalysis doesn't support interprocedural queries.");
794*da58b97aSjoerg   return aliasCheck(LocA.Ptr, LocA.Size, LocB.Ptr, LocB.Size, AAQI);
79506f32e7eSjoerg }
79606f32e7eSjoerg 
79706f32e7eSjoerg /// Checks to see if the specified callsite can clobber the specified memory
79806f32e7eSjoerg /// object.
79906f32e7eSjoerg ///
80006f32e7eSjoerg /// Since we only look at local properties of this function, we really can't
80106f32e7eSjoerg /// say much about this query.  We do, however, use simple "address taken"
80206f32e7eSjoerg /// analysis on local objects.
getModRefInfo(const CallBase * Call,const MemoryLocation & Loc,AAQueryInfo & AAQI)80306f32e7eSjoerg ModRefInfo BasicAAResult::getModRefInfo(const CallBase *Call,
80406f32e7eSjoerg                                         const MemoryLocation &Loc,
80506f32e7eSjoerg                                         AAQueryInfo &AAQI) {
80606f32e7eSjoerg   assert(notDifferentParent(Call, Loc.Ptr) &&
80706f32e7eSjoerg          "AliasAnalysis query involving multiple functions!");
80806f32e7eSjoerg 
809*da58b97aSjoerg   const Value *Object = getUnderlyingObject(Loc.Ptr);
81006f32e7eSjoerg 
81106f32e7eSjoerg   // Calls marked 'tail' cannot read or write allocas from the current frame
81206f32e7eSjoerg   // because the current frame might be destroyed by the time they run. However,
81306f32e7eSjoerg   // a tail call may use an alloca with byval. Calling with byval copies the
81406f32e7eSjoerg   // contents of the alloca into argument registers or stack slots, so there is
81506f32e7eSjoerg   // no lifetime issue.
81606f32e7eSjoerg   if (isa<AllocaInst>(Object))
81706f32e7eSjoerg     if (const CallInst *CI = dyn_cast<CallInst>(Call))
81806f32e7eSjoerg       if (CI->isTailCall() &&
81906f32e7eSjoerg           !CI->getAttributes().hasAttrSomewhere(Attribute::ByVal))
82006f32e7eSjoerg         return ModRefInfo::NoModRef;
82106f32e7eSjoerg 
82206f32e7eSjoerg   // Stack restore is able to modify unescaped dynamic allocas. Assume it may
82306f32e7eSjoerg   // modify them even though the alloca is not escaped.
82406f32e7eSjoerg   if (auto *AI = dyn_cast<AllocaInst>(Object))
82506f32e7eSjoerg     if (!AI->isStaticAlloca() && isIntrinsicCall(Call, Intrinsic::stackrestore))
82606f32e7eSjoerg       return ModRefInfo::Mod;
82706f32e7eSjoerg 
82806f32e7eSjoerg   // If the pointer is to a locally allocated object that does not escape,
82906f32e7eSjoerg   // then the call can not mod/ref the pointer unless the call takes the pointer
83006f32e7eSjoerg   // as an argument, and itself doesn't capture it.
83106f32e7eSjoerg   if (!isa<Constant>(Object) && Call != Object &&
83206f32e7eSjoerg       isNonEscapingLocalObject(Object, &AAQI.IsCapturedCache)) {
83306f32e7eSjoerg 
83406f32e7eSjoerg     // Optimistically assume that call doesn't touch Object and check this
83506f32e7eSjoerg     // assumption in the following loop.
83606f32e7eSjoerg     ModRefInfo Result = ModRefInfo::NoModRef;
83706f32e7eSjoerg     bool IsMustAlias = true;
83806f32e7eSjoerg 
83906f32e7eSjoerg     unsigned OperandNo = 0;
84006f32e7eSjoerg     for (auto CI = Call->data_operands_begin(), CE = Call->data_operands_end();
84106f32e7eSjoerg          CI != CE; ++CI, ++OperandNo) {
84206f32e7eSjoerg       // Only look at the no-capture or byval pointer arguments.  If this
84306f32e7eSjoerg       // pointer were passed to arguments that were neither of these, then it
84406f32e7eSjoerg       // couldn't be no-capture.
84506f32e7eSjoerg       if (!(*CI)->getType()->isPointerTy() ||
84606f32e7eSjoerg           (!Call->doesNotCapture(OperandNo) &&
84706f32e7eSjoerg            OperandNo < Call->getNumArgOperands() &&
84806f32e7eSjoerg            !Call->isByValArgument(OperandNo)))
84906f32e7eSjoerg         continue;
85006f32e7eSjoerg 
85106f32e7eSjoerg       // Call doesn't access memory through this operand, so we don't care
85206f32e7eSjoerg       // if it aliases with Object.
85306f32e7eSjoerg       if (Call->doesNotAccessMemory(OperandNo))
85406f32e7eSjoerg         continue;
85506f32e7eSjoerg 
85606f32e7eSjoerg       // If this is a no-capture pointer argument, see if we can tell that it
85706f32e7eSjoerg       // is impossible to alias the pointer we're checking.
858*da58b97aSjoerg       AliasResult AR = getBestAAResults().alias(
859*da58b97aSjoerg           MemoryLocation::getBeforeOrAfter(*CI),
860*da58b97aSjoerg           MemoryLocation::getBeforeOrAfter(Object), AAQI);
861*da58b97aSjoerg       if (AR != AliasResult::MustAlias)
86206f32e7eSjoerg         IsMustAlias = false;
86306f32e7eSjoerg       // Operand doesn't alias 'Object', continue looking for other aliases
864*da58b97aSjoerg       if (AR == AliasResult::NoAlias)
86506f32e7eSjoerg         continue;
86606f32e7eSjoerg       // Operand aliases 'Object', but call doesn't modify it. Strengthen
86706f32e7eSjoerg       // initial assumption and keep looking in case if there are more aliases.
86806f32e7eSjoerg       if (Call->onlyReadsMemory(OperandNo)) {
86906f32e7eSjoerg         Result = setRef(Result);
87006f32e7eSjoerg         continue;
87106f32e7eSjoerg       }
87206f32e7eSjoerg       // Operand aliases 'Object' but call only writes into it.
87306f32e7eSjoerg       if (Call->doesNotReadMemory(OperandNo)) {
87406f32e7eSjoerg         Result = setMod(Result);
87506f32e7eSjoerg         continue;
87606f32e7eSjoerg       }
87706f32e7eSjoerg       // This operand aliases 'Object' and call reads and writes into it.
87806f32e7eSjoerg       // Setting ModRef will not yield an early return below, MustAlias is not
87906f32e7eSjoerg       // used further.
88006f32e7eSjoerg       Result = ModRefInfo::ModRef;
88106f32e7eSjoerg       break;
88206f32e7eSjoerg     }
88306f32e7eSjoerg 
88406f32e7eSjoerg     // No operand aliases, reset Must bit. Add below if at least one aliases
88506f32e7eSjoerg     // and all aliases found are MustAlias.
88606f32e7eSjoerg     if (isNoModRef(Result))
88706f32e7eSjoerg       IsMustAlias = false;
88806f32e7eSjoerg 
88906f32e7eSjoerg     // Early return if we improved mod ref information
89006f32e7eSjoerg     if (!isModAndRefSet(Result)) {
89106f32e7eSjoerg       if (isNoModRef(Result))
89206f32e7eSjoerg         return ModRefInfo::NoModRef;
89306f32e7eSjoerg       return IsMustAlias ? setMust(Result) : clearMust(Result);
89406f32e7eSjoerg     }
89506f32e7eSjoerg   }
89606f32e7eSjoerg 
897*da58b97aSjoerg   // If the call is malloc/calloc like, we can assume that it doesn't
89806f32e7eSjoerg   // modify any IR visible value.  This is only valid because we assume these
89906f32e7eSjoerg   // routines do not read values visible in the IR.  TODO: Consider special
90006f32e7eSjoerg   // casing realloc and strdup routines which access only their arguments as
90106f32e7eSjoerg   // well.  Or alternatively, replace all of this with inaccessiblememonly once
90206f32e7eSjoerg   // that's implemented fully.
90306f32e7eSjoerg   if (isMallocOrCallocLikeFn(Call, &TLI)) {
90406f32e7eSjoerg     // Be conservative if the accessed pointer may alias the allocation -
90506f32e7eSjoerg     // fallback to the generic handling below.
906*da58b97aSjoerg     if (getBestAAResults().alias(MemoryLocation::getBeforeOrAfter(Call), Loc,
907*da58b97aSjoerg                                  AAQI) == AliasResult::NoAlias)
90806f32e7eSjoerg       return ModRefInfo::NoModRef;
90906f32e7eSjoerg   }
91006f32e7eSjoerg 
911*da58b97aSjoerg   // The semantics of memcpy intrinsics either exactly overlap or do not
912*da58b97aSjoerg   // overlap, i.e., source and destination of any given memcpy are either
913*da58b97aSjoerg   // no-alias or must-alias.
91406f32e7eSjoerg   if (auto *Inst = dyn_cast<AnyMemCpyInst>(Call)) {
915*da58b97aSjoerg     AliasResult SrcAA =
916*da58b97aSjoerg         getBestAAResults().alias(MemoryLocation::getForSource(Inst), Loc, AAQI);
917*da58b97aSjoerg     AliasResult DestAA =
918*da58b97aSjoerg         getBestAAResults().alias(MemoryLocation::getForDest(Inst), Loc, AAQI);
91906f32e7eSjoerg     // It's also possible for Loc to alias both src and dest, or neither.
92006f32e7eSjoerg     ModRefInfo rv = ModRefInfo::NoModRef;
921*da58b97aSjoerg     if (SrcAA != AliasResult::NoAlias)
92206f32e7eSjoerg       rv = setRef(rv);
923*da58b97aSjoerg     if (DestAA != AliasResult::NoAlias)
92406f32e7eSjoerg       rv = setMod(rv);
92506f32e7eSjoerg     return rv;
92606f32e7eSjoerg   }
92706f32e7eSjoerg 
928*da58b97aSjoerg   // Guard intrinsics are marked as arbitrarily writing so that proper control
929*da58b97aSjoerg   // dependencies are maintained but they never mods any particular memory
930*da58b97aSjoerg   // location.
93106f32e7eSjoerg   //
93206f32e7eSjoerg   // *Unlike* assumes, guard intrinsics are modeled as reading memory since the
93306f32e7eSjoerg   // heap state at the point the guard is issued needs to be consistent in case
93406f32e7eSjoerg   // the guard invokes the "deopt" continuation.
93506f32e7eSjoerg   if (isIntrinsicCall(Call, Intrinsic::experimental_guard))
93606f32e7eSjoerg     return ModRefInfo::Ref;
937*da58b97aSjoerg   // The same applies to deoptimize which is essentially a guard(false).
938*da58b97aSjoerg   if (isIntrinsicCall(Call, Intrinsic::experimental_deoptimize))
939*da58b97aSjoerg     return ModRefInfo::Ref;
94006f32e7eSjoerg 
94106f32e7eSjoerg   // Like assumes, invariant.start intrinsics were also marked as arbitrarily
94206f32e7eSjoerg   // writing so that proper control dependencies are maintained but they never
94306f32e7eSjoerg   // mod any particular memory location visible to the IR.
94406f32e7eSjoerg   // *Unlike* assumes (which are now modeled as NoModRef), invariant.start
94506f32e7eSjoerg   // intrinsic is now modeled as reading memory. This prevents hoisting the
94606f32e7eSjoerg   // invariant.start intrinsic over stores. Consider:
94706f32e7eSjoerg   // *ptr = 40;
94806f32e7eSjoerg   // *ptr = 50;
94906f32e7eSjoerg   // invariant_start(ptr)
95006f32e7eSjoerg   // int val = *ptr;
95106f32e7eSjoerg   // print(val);
95206f32e7eSjoerg   //
95306f32e7eSjoerg   // This cannot be transformed to:
95406f32e7eSjoerg   //
95506f32e7eSjoerg   // *ptr = 40;
95606f32e7eSjoerg   // invariant_start(ptr)
95706f32e7eSjoerg   // *ptr = 50;
95806f32e7eSjoerg   // int val = *ptr;
95906f32e7eSjoerg   // print(val);
96006f32e7eSjoerg   //
96106f32e7eSjoerg   // The transformation will cause the second store to be ignored (based on
96206f32e7eSjoerg   // rules of invariant.start)  and print 40, while the first program always
96306f32e7eSjoerg   // prints 50.
96406f32e7eSjoerg   if (isIntrinsicCall(Call, Intrinsic::invariant_start))
96506f32e7eSjoerg     return ModRefInfo::Ref;
96606f32e7eSjoerg 
96706f32e7eSjoerg   // The AAResultBase base class has some smarts, lets use them.
96806f32e7eSjoerg   return AAResultBase::getModRefInfo(Call, Loc, AAQI);
96906f32e7eSjoerg }
97006f32e7eSjoerg 
getModRefInfo(const CallBase * Call1,const CallBase * Call2,AAQueryInfo & AAQI)97106f32e7eSjoerg ModRefInfo BasicAAResult::getModRefInfo(const CallBase *Call1,
97206f32e7eSjoerg                                         const CallBase *Call2,
97306f32e7eSjoerg                                         AAQueryInfo &AAQI) {
974*da58b97aSjoerg   // Guard intrinsics are marked as arbitrarily writing so that proper control
975*da58b97aSjoerg   // dependencies are maintained but they never mods any particular memory
976*da58b97aSjoerg   // location.
97706f32e7eSjoerg   //
97806f32e7eSjoerg   // *Unlike* assumes, guard intrinsics are modeled as reading memory since the
97906f32e7eSjoerg   // heap state at the point the guard is issued needs to be consistent in case
98006f32e7eSjoerg   // the guard invokes the "deopt" continuation.
98106f32e7eSjoerg 
98206f32e7eSjoerg   // NB! This function is *not* commutative, so we special case two
98306f32e7eSjoerg   // possibilities for guard intrinsics.
98406f32e7eSjoerg 
98506f32e7eSjoerg   if (isIntrinsicCall(Call1, Intrinsic::experimental_guard))
98606f32e7eSjoerg     return isModSet(createModRefInfo(getModRefBehavior(Call2)))
98706f32e7eSjoerg                ? ModRefInfo::Ref
98806f32e7eSjoerg                : ModRefInfo::NoModRef;
98906f32e7eSjoerg 
99006f32e7eSjoerg   if (isIntrinsicCall(Call2, Intrinsic::experimental_guard))
99106f32e7eSjoerg     return isModSet(createModRefInfo(getModRefBehavior(Call1)))
99206f32e7eSjoerg                ? ModRefInfo::Mod
99306f32e7eSjoerg                : ModRefInfo::NoModRef;
99406f32e7eSjoerg 
99506f32e7eSjoerg   // The AAResultBase base class has some smarts, lets use them.
99606f32e7eSjoerg   return AAResultBase::getModRefInfo(Call1, Call2, AAQI);
99706f32e7eSjoerg }
99806f32e7eSjoerg 
999*da58b97aSjoerg /// Return true if we know V to the base address of the corresponding memory
1000*da58b97aSjoerg /// object.  This implies that any address less than V must be out of bounds
1001*da58b97aSjoerg /// for the underlying object.  Note that just being isIdentifiedObject() is
1002*da58b97aSjoerg /// not enough - For example, a negative offset from a noalias argument or call
1003*da58b97aSjoerg /// can be inbounds w.r.t the actual underlying object.
isBaseOfObject(const Value * V)1004*da58b97aSjoerg static bool isBaseOfObject(const Value *V) {
1005*da58b97aSjoerg   // TODO: We can handle other cases here
1006*da58b97aSjoerg   // 1) For GC languages, arguments to functions are often required to be
1007*da58b97aSjoerg   //    base pointers.
1008*da58b97aSjoerg   // 2) Result of allocation routines are often base pointers.  Leverage TLI.
1009*da58b97aSjoerg   return (isa<AllocaInst>(V) || isa<GlobalVariable>(V));
101006f32e7eSjoerg }
101106f32e7eSjoerg 
101206f32e7eSjoerg /// Provides a bunch of ad-hoc rules to disambiguate a GEP instruction against
101306f32e7eSjoerg /// another pointer.
101406f32e7eSjoerg ///
101506f32e7eSjoerg /// We know that V1 is a GEP, but we don't know anything about V2.
1016*da58b97aSjoerg /// UnderlyingV1 is getUnderlyingObject(GEP1), UnderlyingV2 is the same for
101706f32e7eSjoerg /// V2.
aliasGEP(const GEPOperator * GEP1,LocationSize V1Size,const Value * V2,LocationSize V2Size,const Value * UnderlyingV1,const Value * UnderlyingV2,AAQueryInfo & AAQI)101806f32e7eSjoerg AliasResult BasicAAResult::aliasGEP(
1019*da58b97aSjoerg     const GEPOperator *GEP1, LocationSize V1Size,
1020*da58b97aSjoerg     const Value *V2, LocationSize V2Size,
102106f32e7eSjoerg     const Value *UnderlyingV1, const Value *UnderlyingV2, AAQueryInfo &AAQI) {
1022*da58b97aSjoerg   if (!V1Size.hasValue() && !V2Size.hasValue()) {
1023*da58b97aSjoerg     // TODO: This limitation exists for compile-time reasons. Relax it if we
1024*da58b97aSjoerg     // can avoid exponential pathological cases.
1025*da58b97aSjoerg     if (!isa<GEPOperator>(V2))
1026*da58b97aSjoerg       return AliasResult::MayAlias;
102706f32e7eSjoerg 
1028*da58b97aSjoerg     // If both accesses have unknown size, we can only check whether the base
1029*da58b97aSjoerg     // objects don't alias.
1030*da58b97aSjoerg     AliasResult BaseAlias = getBestAAResults().alias(
1031*da58b97aSjoerg         MemoryLocation::getBeforeOrAfter(UnderlyingV1),
1032*da58b97aSjoerg         MemoryLocation::getBeforeOrAfter(UnderlyingV2), AAQI);
1033*da58b97aSjoerg     return BaseAlias == AliasResult::NoAlias ? AliasResult::NoAlias
1034*da58b97aSjoerg                                              : AliasResult::MayAlias;
1035*da58b97aSjoerg   }
103606f32e7eSjoerg 
1037*da58b97aSjoerg   DecomposedGEP DecompGEP1 = DecomposeGEPExpression(GEP1, DL, &AC, DT);
1038*da58b97aSjoerg   DecomposedGEP DecompGEP2 = DecomposeGEPExpression(V2, DL, &AC, DT);
1039*da58b97aSjoerg 
1040*da58b97aSjoerg   // Don't attempt to analyze the decomposed GEP if index scale is not a
1041*da58b97aSjoerg   // compile-time constant.
1042*da58b97aSjoerg   if (!DecompGEP1.HasCompileTimeConstantScale ||
1043*da58b97aSjoerg       !DecompGEP2.HasCompileTimeConstantScale)
1044*da58b97aSjoerg     return AliasResult::MayAlias;
104506f32e7eSjoerg 
104606f32e7eSjoerg   assert(DecompGEP1.Base == UnderlyingV1 && DecompGEP2.Base == UnderlyingV2 &&
104706f32e7eSjoerg          "DecomposeGEPExpression returned a result different from "
1048*da58b97aSjoerg          "getUnderlyingObject");
104906f32e7eSjoerg 
105006f32e7eSjoerg   // Subtract the GEP2 pointer from the GEP1 pointer to find out their
105106f32e7eSjoerg   // symbolic difference.
1052*da58b97aSjoerg   DecompGEP1.Offset -= DecompGEP2.Offset;
105306f32e7eSjoerg   GetIndexDifference(DecompGEP1.VarIndices, DecompGEP2.VarIndices);
105406f32e7eSjoerg 
1055*da58b97aSjoerg   // If an inbounds GEP would have to start from an out of bounds address
1056*da58b97aSjoerg   // for the two to alias, then we can assume noalias.
1057*da58b97aSjoerg   if (*DecompGEP1.InBounds && DecompGEP1.VarIndices.empty() &&
1058*da58b97aSjoerg       V2Size.hasValue() && DecompGEP1.Offset.sge(V2Size.getValue()) &&
1059*da58b97aSjoerg       isBaseOfObject(DecompGEP2.Base))
1060*da58b97aSjoerg     return AliasResult::NoAlias;
106106f32e7eSjoerg 
1062*da58b97aSjoerg   if (isa<GEPOperator>(V2)) {
1063*da58b97aSjoerg     // Symmetric case to above.
1064*da58b97aSjoerg     if (*DecompGEP2.InBounds && DecompGEP1.VarIndices.empty() &&
1065*da58b97aSjoerg         V1Size.hasValue() && DecompGEP1.Offset.sle(-V1Size.getValue()) &&
1066*da58b97aSjoerg         isBaseOfObject(DecompGEP1.Base))
1067*da58b97aSjoerg       return AliasResult::NoAlias;
106806f32e7eSjoerg   }
106906f32e7eSjoerg 
1070*da58b97aSjoerg   // For GEPs with identical offsets, we can preserve the size and AAInfo
1071*da58b97aSjoerg   // when performing the alias check on the underlying objects.
1072*da58b97aSjoerg   if (DecompGEP1.Offset == 0 && DecompGEP1.VarIndices.empty())
1073*da58b97aSjoerg     return getBestAAResults().alias(
1074*da58b97aSjoerg         MemoryLocation(UnderlyingV1, V1Size),
1075*da58b97aSjoerg         MemoryLocation(UnderlyingV2, V2Size), AAQI);
107606f32e7eSjoerg 
1077*da58b97aSjoerg   // Do the base pointers alias?
1078*da58b97aSjoerg   AliasResult BaseAlias = getBestAAResults().alias(
1079*da58b97aSjoerg       MemoryLocation::getBeforeOrAfter(UnderlyingV1),
1080*da58b97aSjoerg       MemoryLocation::getBeforeOrAfter(UnderlyingV2), AAQI);
1081*da58b97aSjoerg 
1082*da58b97aSjoerg   // If we get a No or May, then return it immediately, no amount of analysis
1083*da58b97aSjoerg   // will improve this situation.
1084*da58b97aSjoerg   if (BaseAlias != AliasResult::MustAlias) {
1085*da58b97aSjoerg     assert(BaseAlias == AliasResult::NoAlias ||
1086*da58b97aSjoerg            BaseAlias == AliasResult::MayAlias);
1087*da58b97aSjoerg     return BaseAlias;
1088*da58b97aSjoerg   }
108906f32e7eSjoerg 
109006f32e7eSjoerg   // If there is a constant difference between the pointers, but the difference
109106f32e7eSjoerg   // is less than the size of the associated memory object, then we know
109206f32e7eSjoerg   // that the objects are partially overlapping.  If the difference is
109306f32e7eSjoerg   // greater, we know they do not overlap.
1094*da58b97aSjoerg   if (DecompGEP1.Offset != 0 && DecompGEP1.VarIndices.empty()) {
1095*da58b97aSjoerg     APInt &Off = DecompGEP1.Offset;
1096*da58b97aSjoerg 
1097*da58b97aSjoerg     // Initialize for Off >= 0 (V2 <= GEP1) case.
1098*da58b97aSjoerg     const Value *LeftPtr = V2;
1099*da58b97aSjoerg     const Value *RightPtr = GEP1;
1100*da58b97aSjoerg     LocationSize VLeftSize = V2Size;
1101*da58b97aSjoerg     LocationSize VRightSize = V1Size;
1102*da58b97aSjoerg     const bool Swapped = Off.isNegative();
1103*da58b97aSjoerg 
1104*da58b97aSjoerg     if (Swapped) {
1105*da58b97aSjoerg       // Swap if we have the situation where:
110606f32e7eSjoerg       // +                +
110706f32e7eSjoerg       // | BaseOffset     |
110806f32e7eSjoerg       // ---------------->|
110906f32e7eSjoerg       // |-->V1Size       |-------> V2Size
111006f32e7eSjoerg       // GEP1             V2
1111*da58b97aSjoerg       std::swap(LeftPtr, RightPtr);
1112*da58b97aSjoerg       std::swap(VLeftSize, VRightSize);
1113*da58b97aSjoerg       Off = -Off;
111406f32e7eSjoerg     }
1115*da58b97aSjoerg 
1116*da58b97aSjoerg     if (VLeftSize.hasValue()) {
1117*da58b97aSjoerg       const uint64_t LSize = VLeftSize.getValue();
1118*da58b97aSjoerg       if (Off.ult(LSize)) {
1119*da58b97aSjoerg         // Conservatively drop processing if a phi was visited and/or offset is
1120*da58b97aSjoerg         // too big.
1121*da58b97aSjoerg         AliasResult AR = AliasResult::PartialAlias;
1122*da58b97aSjoerg         if (VRightSize.hasValue() && Off.ule(INT32_MAX) &&
1123*da58b97aSjoerg             (Off + VRightSize.getValue()).ule(LSize)) {
1124*da58b97aSjoerg           // Memory referenced by right pointer is nested. Save the offset in
1125*da58b97aSjoerg           // cache. Note that originally offset estimated as GEP1-V2, but
1126*da58b97aSjoerg           // AliasResult contains the shift that represents GEP1+Offset=V2.
1127*da58b97aSjoerg           AR.setOffset(-Off.getSExtValue());
1128*da58b97aSjoerg           AR.swap(Swapped);
1129*da58b97aSjoerg         }
1130*da58b97aSjoerg         return AR;
1131*da58b97aSjoerg       }
1132*da58b97aSjoerg       return AliasResult::NoAlias;
113306f32e7eSjoerg     }
113406f32e7eSjoerg   }
113506f32e7eSjoerg 
113606f32e7eSjoerg   if (!DecompGEP1.VarIndices.empty()) {
1137*da58b97aSjoerg     APInt GCD;
1138*da58b97aSjoerg     bool AllNonNegative = DecompGEP1.Offset.isNonNegative();
1139*da58b97aSjoerg     bool AllNonPositive = DecompGEP1.Offset.isNonPositive();
114006f32e7eSjoerg     for (unsigned i = 0, e = DecompGEP1.VarIndices.size(); i != e; ++i) {
1141*da58b97aSjoerg       const APInt &Scale = DecompGEP1.VarIndices[i].Scale;
1142*da58b97aSjoerg       if (i == 0)
1143*da58b97aSjoerg         GCD = Scale.abs();
1144*da58b97aSjoerg       else
1145*da58b97aSjoerg         GCD = APIntOps::GreatestCommonDivisor(GCD, Scale.abs());
114606f32e7eSjoerg 
1147*da58b97aSjoerg       if (AllNonNegative || AllNonPositive) {
114806f32e7eSjoerg         // If the Value could change between cycles, then any reasoning about
114906f32e7eSjoerg         // the Value this cycle may not hold in the next cycle. We'll just
115006f32e7eSjoerg         // give up if we can't determine conditions that hold for every cycle:
115106f32e7eSjoerg         const Value *V = DecompGEP1.VarIndices[i].V;
1152*da58b97aSjoerg         const Instruction *CxtI = DecompGEP1.VarIndices[i].CxtI;
115306f32e7eSjoerg 
1154*da58b97aSjoerg         KnownBits Known = computeKnownBits(V, DL, 0, &AC, CxtI, DT);
115506f32e7eSjoerg         bool SignKnownZero = Known.isNonNegative();
115606f32e7eSjoerg         bool SignKnownOne = Known.isNegative();
115706f32e7eSjoerg 
115806f32e7eSjoerg         // Zero-extension widens the variable, and so forces the sign
115906f32e7eSjoerg         // bit to zero.
116006f32e7eSjoerg         bool IsZExt = DecompGEP1.VarIndices[i].ZExtBits > 0 || isa<ZExtInst>(V);
116106f32e7eSjoerg         SignKnownZero |= IsZExt;
116206f32e7eSjoerg         SignKnownOne &= !IsZExt;
116306f32e7eSjoerg 
1164*da58b97aSjoerg         AllNonNegative &= (SignKnownZero && Scale.isNonNegative()) ||
1165*da58b97aSjoerg                           (SignKnownOne && Scale.isNonPositive());
1166*da58b97aSjoerg         AllNonPositive &= (SignKnownZero && Scale.isNonPositive()) ||
1167*da58b97aSjoerg                           (SignKnownOne && Scale.isNonNegative());
116806f32e7eSjoerg       }
116906f32e7eSjoerg     }
117006f32e7eSjoerg 
1171*da58b97aSjoerg     // We now have accesses at two offsets from the same base:
1172*da58b97aSjoerg     //  1. (...)*GCD + DecompGEP1.Offset with size V1Size
1173*da58b97aSjoerg     //  2. 0 with size V2Size
1174*da58b97aSjoerg     // Using arithmetic modulo GCD, the accesses are at
1175*da58b97aSjoerg     // [ModOffset..ModOffset+V1Size) and [0..V2Size). If the first access fits
1176*da58b97aSjoerg     // into the range [V2Size..GCD), then we know they cannot overlap.
1177*da58b97aSjoerg     APInt ModOffset = DecompGEP1.Offset.srem(GCD);
1178*da58b97aSjoerg     if (ModOffset.isNegative())
1179*da58b97aSjoerg       ModOffset += GCD; // We want mod, not rem.
1180*da58b97aSjoerg     if (V1Size.hasValue() && V2Size.hasValue() &&
1181*da58b97aSjoerg         ModOffset.uge(V2Size.getValue()) &&
1182*da58b97aSjoerg         (GCD - ModOffset).uge(V1Size.getValue()))
1183*da58b97aSjoerg       return AliasResult::NoAlias;
118406f32e7eSjoerg 
1185*da58b97aSjoerg     // If we know all the variables are non-negative, then the total offset is
1186*da58b97aSjoerg     // also non-negative and >= DecompGEP1.Offset. We have the following layout:
1187*da58b97aSjoerg     // [0, V2Size) ... [TotalOffset, TotalOffer+V1Size]
1188*da58b97aSjoerg     // If DecompGEP1.Offset >= V2Size, the accesses don't alias.
1189*da58b97aSjoerg     if (AllNonNegative && V2Size.hasValue() &&
1190*da58b97aSjoerg         DecompGEP1.Offset.uge(V2Size.getValue()))
1191*da58b97aSjoerg       return AliasResult::NoAlias;
1192*da58b97aSjoerg     // Similarly, if the variables are non-positive, then the total offset is
1193*da58b97aSjoerg     // also non-positive and <= DecompGEP1.Offset. We have the following layout:
1194*da58b97aSjoerg     // [TotalOffset, TotalOffset+V1Size) ... [0, V2Size)
1195*da58b97aSjoerg     // If -DecompGEP1.Offset >= V1Size, the accesses don't alias.
1196*da58b97aSjoerg     if (AllNonPositive && V1Size.hasValue() &&
1197*da58b97aSjoerg         (-DecompGEP1.Offset).uge(V1Size.getValue()))
1198*da58b97aSjoerg       return AliasResult::NoAlias;
119906f32e7eSjoerg 
1200*da58b97aSjoerg     if (V1Size.hasValue() && V2Size.hasValue()) {
1201*da58b97aSjoerg       // Try to determine whether abs(VarIndex) > 0.
1202*da58b97aSjoerg       Optional<APInt> MinAbsVarIndex;
1203*da58b97aSjoerg       if (DecompGEP1.VarIndices.size() == 1) {
1204*da58b97aSjoerg         // VarIndex = Scale*V. If V != 0 then abs(VarIndex) >= abs(Scale).
1205*da58b97aSjoerg         const VariableGEPIndex &Var = DecompGEP1.VarIndices[0];
1206*da58b97aSjoerg         if (isKnownNonZero(Var.V, DL, 0, &AC, Var.CxtI, DT))
1207*da58b97aSjoerg           MinAbsVarIndex = Var.Scale.abs();
1208*da58b97aSjoerg       } else if (DecompGEP1.VarIndices.size() == 2) {
1209*da58b97aSjoerg         // VarIndex = Scale*V0 + (-Scale)*V1.
1210*da58b97aSjoerg         // If V0 != V1 then abs(VarIndex) >= abs(Scale).
1211*da58b97aSjoerg         // Check that VisitedPhiBBs is empty, to avoid reasoning about
1212*da58b97aSjoerg         // inequality of values across loop iterations.
1213*da58b97aSjoerg         const VariableGEPIndex &Var0 = DecompGEP1.VarIndices[0];
1214*da58b97aSjoerg         const VariableGEPIndex &Var1 = DecompGEP1.VarIndices[1];
1215*da58b97aSjoerg         if (Var0.Scale == -Var1.Scale && Var0.ZExtBits == Var1.ZExtBits &&
1216*da58b97aSjoerg             Var0.SExtBits == Var1.SExtBits && VisitedPhiBBs.empty() &&
1217*da58b97aSjoerg             isKnownNonEqual(Var0.V, Var1.V, DL, &AC, /* CxtI */ nullptr, DT))
1218*da58b97aSjoerg           MinAbsVarIndex = Var0.Scale.abs();
1219*da58b97aSjoerg       }
1220*da58b97aSjoerg 
1221*da58b97aSjoerg       if (MinAbsVarIndex) {
1222*da58b97aSjoerg         // The constant offset will have added at least +/-MinAbsVarIndex to it.
1223*da58b97aSjoerg         APInt OffsetLo = DecompGEP1.Offset - *MinAbsVarIndex;
1224*da58b97aSjoerg         APInt OffsetHi = DecompGEP1.Offset + *MinAbsVarIndex;
1225*da58b97aSjoerg         // Check that an access at OffsetLo or lower, and an access at OffsetHi
1226*da58b97aSjoerg         // or higher both do not alias.
1227*da58b97aSjoerg         if (OffsetLo.isNegative() && (-OffsetLo).uge(V1Size.getValue()) &&
1228*da58b97aSjoerg             OffsetHi.isNonNegative() && OffsetHi.uge(V2Size.getValue()))
1229*da58b97aSjoerg           return AliasResult::NoAlias;
1230*da58b97aSjoerg       }
1231*da58b97aSjoerg     }
123206f32e7eSjoerg 
123306f32e7eSjoerg     if (constantOffsetHeuristic(DecompGEP1.VarIndices, V1Size, V2Size,
1234*da58b97aSjoerg                                 DecompGEP1.Offset, &AC, DT))
1235*da58b97aSjoerg       return AliasResult::NoAlias;
123606f32e7eSjoerg   }
123706f32e7eSjoerg 
123806f32e7eSjoerg   // Statically, we can see that the base objects are the same, but the
123906f32e7eSjoerg   // pointers have dynamic offsets which we can't resolve. And none of our
124006f32e7eSjoerg   // little tricks above worked.
1241*da58b97aSjoerg   return AliasResult::MayAlias;
124206f32e7eSjoerg }
124306f32e7eSjoerg 
MergeAliasResults(AliasResult A,AliasResult B)124406f32e7eSjoerg static AliasResult MergeAliasResults(AliasResult A, AliasResult B) {
124506f32e7eSjoerg   // If the results agree, take it.
124606f32e7eSjoerg   if (A == B)
124706f32e7eSjoerg     return A;
124806f32e7eSjoerg   // A mix of PartialAlias and MustAlias is PartialAlias.
1249*da58b97aSjoerg   if ((A == AliasResult::PartialAlias && B == AliasResult::MustAlias) ||
1250*da58b97aSjoerg       (B == AliasResult::PartialAlias && A == AliasResult::MustAlias))
1251*da58b97aSjoerg     return AliasResult::PartialAlias;
125206f32e7eSjoerg   // Otherwise, we don't know anything.
1253*da58b97aSjoerg   return AliasResult::MayAlias;
125406f32e7eSjoerg }
125506f32e7eSjoerg 
125606f32e7eSjoerg /// Provides a bunch of ad-hoc rules to disambiguate a Select instruction
125706f32e7eSjoerg /// against another.
125806f32e7eSjoerg AliasResult
aliasSelect(const SelectInst * SI,LocationSize SISize,const Value * V2,LocationSize V2Size,AAQueryInfo & AAQI)125906f32e7eSjoerg BasicAAResult::aliasSelect(const SelectInst *SI, LocationSize SISize,
1260*da58b97aSjoerg                            const Value *V2, LocationSize V2Size,
1261*da58b97aSjoerg                            AAQueryInfo &AAQI) {
126206f32e7eSjoerg   // If the values are Selects with the same condition, we can do a more precise
126306f32e7eSjoerg   // check: just check for aliases between the values on corresponding arms.
126406f32e7eSjoerg   if (const SelectInst *SI2 = dyn_cast<SelectInst>(V2))
126506f32e7eSjoerg     if (SI->getCondition() == SI2->getCondition()) {
1266*da58b97aSjoerg       AliasResult Alias = getBestAAResults().alias(
1267*da58b97aSjoerg           MemoryLocation(SI->getTrueValue(), SISize),
1268*da58b97aSjoerg           MemoryLocation(SI2->getTrueValue(), V2Size), AAQI);
1269*da58b97aSjoerg       if (Alias == AliasResult::MayAlias)
1270*da58b97aSjoerg         return AliasResult::MayAlias;
1271*da58b97aSjoerg       AliasResult ThisAlias = getBestAAResults().alias(
1272*da58b97aSjoerg           MemoryLocation(SI->getFalseValue(), SISize),
1273*da58b97aSjoerg           MemoryLocation(SI2->getFalseValue(), V2Size), AAQI);
127406f32e7eSjoerg       return MergeAliasResults(ThisAlias, Alias);
127506f32e7eSjoerg     }
127606f32e7eSjoerg 
127706f32e7eSjoerg   // If both arms of the Select node NoAlias or MustAlias V2, then returns
127806f32e7eSjoerg   // NoAlias / MustAlias. Otherwise, returns MayAlias.
1279*da58b97aSjoerg   AliasResult Alias = getBestAAResults().alias(
1280*da58b97aSjoerg       MemoryLocation(V2, V2Size),
1281*da58b97aSjoerg       MemoryLocation(SI->getTrueValue(), SISize), AAQI);
1282*da58b97aSjoerg   if (Alias == AliasResult::MayAlias)
1283*da58b97aSjoerg     return AliasResult::MayAlias;
128406f32e7eSjoerg 
1285*da58b97aSjoerg   AliasResult ThisAlias = getBestAAResults().alias(
1286*da58b97aSjoerg       MemoryLocation(V2, V2Size),
1287*da58b97aSjoerg       MemoryLocation(SI->getFalseValue(), SISize), AAQI);
128806f32e7eSjoerg   return MergeAliasResults(ThisAlias, Alias);
128906f32e7eSjoerg }
129006f32e7eSjoerg 
129106f32e7eSjoerg /// Provide a bunch of ad-hoc rules to disambiguate a PHI instruction against
129206f32e7eSjoerg /// another.
aliasPHI(const PHINode * PN,LocationSize PNSize,const Value * V2,LocationSize V2Size,AAQueryInfo & AAQI)129306f32e7eSjoerg AliasResult BasicAAResult::aliasPHI(const PHINode *PN, LocationSize PNSize,
1294*da58b97aSjoerg                                     const Value *V2, LocationSize V2Size,
1295*da58b97aSjoerg                                     AAQueryInfo &AAQI) {
129606f32e7eSjoerg   // If the values are PHIs in the same block, we can do a more precise
129706f32e7eSjoerg   // as well as efficient check: just check for aliases between the values
129806f32e7eSjoerg   // on corresponding edges.
129906f32e7eSjoerg   if (const PHINode *PN2 = dyn_cast<PHINode>(V2))
130006f32e7eSjoerg     if (PN2->getParent() == PN->getParent()) {
1301*da58b97aSjoerg       Optional<AliasResult> Alias;
130206f32e7eSjoerg       for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
1303*da58b97aSjoerg         AliasResult ThisAlias = getBestAAResults().alias(
1304*da58b97aSjoerg             MemoryLocation(PN->getIncomingValue(i), PNSize),
1305*da58b97aSjoerg             MemoryLocation(
1306*da58b97aSjoerg                 PN2->getIncomingValueForBlock(PN->getIncomingBlock(i)), V2Size),
1307*da58b97aSjoerg             AAQI);
1308*da58b97aSjoerg         if (Alias)
1309*da58b97aSjoerg           *Alias = MergeAliasResults(*Alias, ThisAlias);
1310*da58b97aSjoerg         else
1311*da58b97aSjoerg           Alias = ThisAlias;
1312*da58b97aSjoerg         if (*Alias == AliasResult::MayAlias)
131306f32e7eSjoerg           break;
131406f32e7eSjoerg       }
1315*da58b97aSjoerg       return *Alias;
131606f32e7eSjoerg     }
131706f32e7eSjoerg 
131806f32e7eSjoerg   SmallVector<Value *, 4> V1Srcs;
1319*da58b97aSjoerg   // If a phi operand recurses back to the phi, we can still determine NoAlias
1320*da58b97aSjoerg   // if we don't alias the underlying objects of the other phi operands, as we
1321*da58b97aSjoerg   // know that the recursive phi needs to be based on them in some way.
132206f32e7eSjoerg   bool isRecursive = false;
1323*da58b97aSjoerg   auto CheckForRecPhi = [&](Value *PV) {
1324*da58b97aSjoerg     if (!EnableRecPhiAnalysis)
1325*da58b97aSjoerg       return false;
1326*da58b97aSjoerg     if (getUnderlyingObject(PV) == PN) {
1327*da58b97aSjoerg       isRecursive = true;
1328*da58b97aSjoerg       return true;
1329*da58b97aSjoerg     }
1330*da58b97aSjoerg     return false;
1331*da58b97aSjoerg   };
1332*da58b97aSjoerg 
133306f32e7eSjoerg   if (PV) {
133406f32e7eSjoerg     // If we have PhiValues then use it to get the underlying phi values.
133506f32e7eSjoerg     const PhiValues::ValueSet &PhiValueSet = PV->getValuesForPhi(PN);
133606f32e7eSjoerg     // If we have more phi values than the search depth then return MayAlias
133706f32e7eSjoerg     // conservatively to avoid compile time explosion. The worst possible case
133806f32e7eSjoerg     // is if both sides are PHI nodes. In which case, this is O(m x n) time
133906f32e7eSjoerg     // where 'm' and 'n' are the number of PHI sources.
134006f32e7eSjoerg     if (PhiValueSet.size() > MaxLookupSearchDepth)
1341*da58b97aSjoerg       return AliasResult::MayAlias;
134206f32e7eSjoerg     // Add the values to V1Srcs
134306f32e7eSjoerg     for (Value *PV1 : PhiValueSet) {
1344*da58b97aSjoerg       if (CheckForRecPhi(PV1))
134506f32e7eSjoerg         continue;
134606f32e7eSjoerg       V1Srcs.push_back(PV1);
134706f32e7eSjoerg     }
134806f32e7eSjoerg   } else {
134906f32e7eSjoerg     // If we don't have PhiInfo then just look at the operands of the phi itself
135006f32e7eSjoerg     // FIXME: Remove this once we can guarantee that we have PhiInfo always
135106f32e7eSjoerg     SmallPtrSet<Value *, 4> UniqueSrc;
1352*da58b97aSjoerg     Value *OnePhi = nullptr;
135306f32e7eSjoerg     for (Value *PV1 : PN->incoming_values()) {
1354*da58b97aSjoerg       if (isa<PHINode>(PV1)) {
1355*da58b97aSjoerg         if (OnePhi && OnePhi != PV1) {
1356*da58b97aSjoerg           // To control potential compile time explosion, we choose to be
1357*da58b97aSjoerg           // conserviate when we have more than one Phi input.  It is important
1358*da58b97aSjoerg           // that we handle the single phi case as that lets us handle LCSSA
1359*da58b97aSjoerg           // phi nodes and (combined with the recursive phi handling) simple
1360*da58b97aSjoerg           // pointer induction variable patterns.
1361*da58b97aSjoerg           return AliasResult::MayAlias;
1362*da58b97aSjoerg         }
1363*da58b97aSjoerg         OnePhi = PV1;
1364*da58b97aSjoerg       }
136506f32e7eSjoerg 
1366*da58b97aSjoerg       if (CheckForRecPhi(PV1))
136706f32e7eSjoerg         continue;
136806f32e7eSjoerg 
136906f32e7eSjoerg       if (UniqueSrc.insert(PV1).second)
137006f32e7eSjoerg         V1Srcs.push_back(PV1);
137106f32e7eSjoerg     }
1372*da58b97aSjoerg 
1373*da58b97aSjoerg     if (OnePhi && UniqueSrc.size() > 1)
1374*da58b97aSjoerg       // Out of an abundance of caution, allow only the trivial lcssa and
1375*da58b97aSjoerg       // recursive phi cases.
1376*da58b97aSjoerg       return AliasResult::MayAlias;
137706f32e7eSjoerg   }
137806f32e7eSjoerg 
137906f32e7eSjoerg   // If V1Srcs is empty then that means that the phi has no underlying non-phi
138006f32e7eSjoerg   // value. This should only be possible in blocks unreachable from the entry
138106f32e7eSjoerg   // block, but return MayAlias just in case.
138206f32e7eSjoerg   if (V1Srcs.empty())
1383*da58b97aSjoerg     return AliasResult::MayAlias;
138406f32e7eSjoerg 
1385*da58b97aSjoerg   // If this PHI node is recursive, indicate that the pointer may be moved
1386*da58b97aSjoerg   // across iterations. We can only prove NoAlias if different underlying
1387*da58b97aSjoerg   // objects are involved.
138806f32e7eSjoerg   if (isRecursive)
1389*da58b97aSjoerg     PNSize = LocationSize::beforeOrAfterPointer();
139006f32e7eSjoerg 
1391*da58b97aSjoerg   // In the recursive alias queries below, we may compare values from two
1392*da58b97aSjoerg   // different loop iterations. Keep track of visited phi blocks, which will
1393*da58b97aSjoerg   // be used when determining value equivalence.
1394*da58b97aSjoerg   bool BlockInserted = VisitedPhiBBs.insert(PN->getParent()).second;
1395*da58b97aSjoerg   auto _ = make_scope_exit([&]() {
1396*da58b97aSjoerg     if (BlockInserted)
1397*da58b97aSjoerg       VisitedPhiBBs.erase(PN->getParent());
1398*da58b97aSjoerg   });
1399*da58b97aSjoerg 
1400*da58b97aSjoerg   // If we inserted a block into VisitedPhiBBs, alias analysis results that
1401*da58b97aSjoerg   // have been cached earlier may no longer be valid. Perform recursive queries
1402*da58b97aSjoerg   // with a new AAQueryInfo.
1403*da58b97aSjoerg   AAQueryInfo NewAAQI = AAQI.withEmptyCache();
1404*da58b97aSjoerg   AAQueryInfo *UseAAQI = BlockInserted ? &NewAAQI : &AAQI;
1405*da58b97aSjoerg 
1406*da58b97aSjoerg   AliasResult Alias = getBestAAResults().alias(
1407*da58b97aSjoerg       MemoryLocation(V2, V2Size),
1408*da58b97aSjoerg       MemoryLocation(V1Srcs[0], PNSize), *UseAAQI);
140906f32e7eSjoerg 
141006f32e7eSjoerg   // Early exit if the check of the first PHI source against V2 is MayAlias.
141106f32e7eSjoerg   // Other results are not possible.
1412*da58b97aSjoerg   if (Alias == AliasResult::MayAlias)
1413*da58b97aSjoerg     return AliasResult::MayAlias;
1414*da58b97aSjoerg   // With recursive phis we cannot guarantee that MustAlias/PartialAlias will
1415*da58b97aSjoerg   // remain valid to all elements and needs to conservatively return MayAlias.
1416*da58b97aSjoerg   if (isRecursive && Alias != AliasResult::NoAlias)
1417*da58b97aSjoerg     return AliasResult::MayAlias;
141806f32e7eSjoerg 
141906f32e7eSjoerg   // If all sources of the PHI node NoAlias or MustAlias V2, then returns
142006f32e7eSjoerg   // NoAlias / MustAlias. Otherwise, returns MayAlias.
142106f32e7eSjoerg   for (unsigned i = 1, e = V1Srcs.size(); i != e; ++i) {
142206f32e7eSjoerg     Value *V = V1Srcs[i];
142306f32e7eSjoerg 
1424*da58b97aSjoerg     AliasResult ThisAlias = getBestAAResults().alias(
1425*da58b97aSjoerg         MemoryLocation(V2, V2Size), MemoryLocation(V, PNSize), *UseAAQI);
142606f32e7eSjoerg     Alias = MergeAliasResults(ThisAlias, Alias);
1427*da58b97aSjoerg     if (Alias == AliasResult::MayAlias)
142806f32e7eSjoerg       break;
142906f32e7eSjoerg   }
143006f32e7eSjoerg 
143106f32e7eSjoerg   return Alias;
143206f32e7eSjoerg }
143306f32e7eSjoerg 
143406f32e7eSjoerg /// Provides a bunch of ad-hoc rules to disambiguate in common cases, such as
143506f32e7eSjoerg /// array references.
aliasCheck(const Value * V1,LocationSize V1Size,const Value * V2,LocationSize V2Size,AAQueryInfo & AAQI)143606f32e7eSjoerg AliasResult BasicAAResult::aliasCheck(const Value *V1, LocationSize V1Size,
1437*da58b97aSjoerg                                       const Value *V2, LocationSize V2Size,
1438*da58b97aSjoerg                                       AAQueryInfo &AAQI) {
143906f32e7eSjoerg   // If either of the memory references is empty, it doesn't matter what the
144006f32e7eSjoerg   // pointer values are.
144106f32e7eSjoerg   if (V1Size.isZero() || V2Size.isZero())
1442*da58b97aSjoerg     return AliasResult::NoAlias;
144306f32e7eSjoerg 
144406f32e7eSjoerg   // Strip off any casts if they exist.
1445*da58b97aSjoerg   V1 = V1->stripPointerCastsForAliasAnalysis();
1446*da58b97aSjoerg   V2 = V2->stripPointerCastsForAliasAnalysis();
144706f32e7eSjoerg 
144806f32e7eSjoerg   // If V1 or V2 is undef, the result is NoAlias because we can always pick a
144906f32e7eSjoerg   // value for undef that aliases nothing in the program.
145006f32e7eSjoerg   if (isa<UndefValue>(V1) || isa<UndefValue>(V2))
1451*da58b97aSjoerg     return AliasResult::NoAlias;
145206f32e7eSjoerg 
145306f32e7eSjoerg   // Are we checking for alias of the same value?
145406f32e7eSjoerg   // Because we look 'through' phi nodes, we could look at "Value" pointers from
145506f32e7eSjoerg   // different iterations. We must therefore make sure that this is not the
145606f32e7eSjoerg   // case. The function isValueEqualInPotentialCycles ensures that this cannot
145706f32e7eSjoerg   // happen by looking at the visited phi nodes and making sure they cannot
145806f32e7eSjoerg   // reach the value.
145906f32e7eSjoerg   if (isValueEqualInPotentialCycles(V1, V2))
1460*da58b97aSjoerg     return AliasResult::MustAlias;
146106f32e7eSjoerg 
146206f32e7eSjoerg   if (!V1->getType()->isPointerTy() || !V2->getType()->isPointerTy())
1463*da58b97aSjoerg     return AliasResult::NoAlias; // Scalars cannot alias each other
146406f32e7eSjoerg 
146506f32e7eSjoerg   // Figure out what objects these things are pointing to if we can.
1466*da58b97aSjoerg   const Value *O1 = getUnderlyingObject(V1, MaxLookupSearchDepth);
1467*da58b97aSjoerg   const Value *O2 = getUnderlyingObject(V2, MaxLookupSearchDepth);
146806f32e7eSjoerg 
146906f32e7eSjoerg   // Null values in the default address space don't point to any object, so they
147006f32e7eSjoerg   // don't alias any other pointer.
147106f32e7eSjoerg   if (const ConstantPointerNull *CPN = dyn_cast<ConstantPointerNull>(O1))
147206f32e7eSjoerg     if (!NullPointerIsDefined(&F, CPN->getType()->getAddressSpace()))
1473*da58b97aSjoerg       return AliasResult::NoAlias;
147406f32e7eSjoerg   if (const ConstantPointerNull *CPN = dyn_cast<ConstantPointerNull>(O2))
147506f32e7eSjoerg     if (!NullPointerIsDefined(&F, CPN->getType()->getAddressSpace()))
1476*da58b97aSjoerg       return AliasResult::NoAlias;
147706f32e7eSjoerg 
147806f32e7eSjoerg   if (O1 != O2) {
147906f32e7eSjoerg     // If V1/V2 point to two different objects, we know that we have no alias.
148006f32e7eSjoerg     if (isIdentifiedObject(O1) && isIdentifiedObject(O2))
1481*da58b97aSjoerg       return AliasResult::NoAlias;
148206f32e7eSjoerg 
148306f32e7eSjoerg     // Constant pointers can't alias with non-const isIdentifiedObject objects.
148406f32e7eSjoerg     if ((isa<Constant>(O1) && isIdentifiedObject(O2) && !isa<Constant>(O2)) ||
148506f32e7eSjoerg         (isa<Constant>(O2) && isIdentifiedObject(O1) && !isa<Constant>(O1)))
1486*da58b97aSjoerg       return AliasResult::NoAlias;
148706f32e7eSjoerg 
148806f32e7eSjoerg     // Function arguments can't alias with things that are known to be
148906f32e7eSjoerg     // unambigously identified at the function level.
149006f32e7eSjoerg     if ((isa<Argument>(O1) && isIdentifiedFunctionLocal(O2)) ||
149106f32e7eSjoerg         (isa<Argument>(O2) && isIdentifiedFunctionLocal(O1)))
1492*da58b97aSjoerg       return AliasResult::NoAlias;
149306f32e7eSjoerg 
149406f32e7eSjoerg     // If one pointer is the result of a call/invoke or load and the other is a
149506f32e7eSjoerg     // non-escaping local object within the same function, then we know the
149606f32e7eSjoerg     // object couldn't escape to a point where the call could return it.
149706f32e7eSjoerg     //
149806f32e7eSjoerg     // Note that if the pointers are in different functions, there are a
149906f32e7eSjoerg     // variety of complications. A call with a nocapture argument may still
150006f32e7eSjoerg     // temporary store the nocapture argument's value in a temporary memory
150106f32e7eSjoerg     // location if that memory location doesn't escape. Or it may pass a
150206f32e7eSjoerg     // nocapture value to other functions as long as they don't capture it.
150306f32e7eSjoerg     if (isEscapeSource(O1) &&
150406f32e7eSjoerg         isNonEscapingLocalObject(O2, &AAQI.IsCapturedCache))
1505*da58b97aSjoerg       return AliasResult::NoAlias;
150606f32e7eSjoerg     if (isEscapeSource(O2) &&
150706f32e7eSjoerg         isNonEscapingLocalObject(O1, &AAQI.IsCapturedCache))
1508*da58b97aSjoerg       return AliasResult::NoAlias;
150906f32e7eSjoerg   }
151006f32e7eSjoerg 
151106f32e7eSjoerg   // If the size of one access is larger than the entire object on the other
151206f32e7eSjoerg   // side, then we know such behavior is undefined and can assume no alias.
151306f32e7eSjoerg   bool NullIsValidLocation = NullPointerIsDefined(&F);
151406f32e7eSjoerg   if ((isObjectSmallerThan(
151506f32e7eSjoerg           O2, getMinimalExtentFrom(*V1, V1Size, DL, NullIsValidLocation), DL,
151606f32e7eSjoerg           TLI, NullIsValidLocation)) ||
151706f32e7eSjoerg       (isObjectSmallerThan(
151806f32e7eSjoerg           O1, getMinimalExtentFrom(*V2, V2Size, DL, NullIsValidLocation), DL,
151906f32e7eSjoerg           TLI, NullIsValidLocation)))
1520*da58b97aSjoerg     return AliasResult::NoAlias;
1521*da58b97aSjoerg 
1522*da58b97aSjoerg   // If one the accesses may be before the accessed pointer, canonicalize this
1523*da58b97aSjoerg   // by using unknown after-pointer sizes for both accesses. This is
1524*da58b97aSjoerg   // equivalent, because regardless of which pointer is lower, one of them
1525*da58b97aSjoerg   // will always came after the other, as long as the underlying objects aren't
1526*da58b97aSjoerg   // disjoint. We do this so that the rest of BasicAA does not have to deal
1527*da58b97aSjoerg   // with accesses before the base pointer, and to improve cache utilization by
1528*da58b97aSjoerg   // merging equivalent states.
1529*da58b97aSjoerg   if (V1Size.mayBeBeforePointer() || V2Size.mayBeBeforePointer()) {
1530*da58b97aSjoerg     V1Size = LocationSize::afterPointer();
1531*da58b97aSjoerg     V2Size = LocationSize::afterPointer();
1532*da58b97aSjoerg   }
1533*da58b97aSjoerg 
1534*da58b97aSjoerg   // FIXME: If this depth limit is hit, then we may cache sub-optimal results
1535*da58b97aSjoerg   // for recursive queries. For this reason, this limit is chosen to be large
1536*da58b97aSjoerg   // enough to be very rarely hit, while still being small enough to avoid
1537*da58b97aSjoerg   // stack overflows.
1538*da58b97aSjoerg   if (AAQI.Depth >= 512)
1539*da58b97aSjoerg     return AliasResult::MayAlias;
154006f32e7eSjoerg 
154106f32e7eSjoerg   // Check the cache before climbing up use-def chains. This also terminates
154206f32e7eSjoerg   // otherwise infinitely recursive queries.
1543*da58b97aSjoerg   AAQueryInfo::LocPair Locs({V1, V1Size}, {V2, V2Size});
1544*da58b97aSjoerg   const bool Swapped = V1 > V2;
1545*da58b97aSjoerg   if (Swapped)
154606f32e7eSjoerg     std::swap(Locs.first, Locs.second);
1547*da58b97aSjoerg   const auto &Pair = AAQI.AliasCache.try_emplace(
1548*da58b97aSjoerg       Locs, AAQueryInfo::CacheEntry{AliasResult::NoAlias, 0});
1549*da58b97aSjoerg   if (!Pair.second) {
1550*da58b97aSjoerg     auto &Entry = Pair.first->second;
1551*da58b97aSjoerg     if (!Entry.isDefinitive()) {
1552*da58b97aSjoerg       // Remember that we used an assumption.
1553*da58b97aSjoerg       ++Entry.NumAssumptionUses;
1554*da58b97aSjoerg       ++AAQI.NumAssumptionUses;
155506f32e7eSjoerg     }
1556*da58b97aSjoerg     // Cache contains sorted {V1,V2} pairs but we should return original order.
1557*da58b97aSjoerg     auto Result = Entry.Result;
1558*da58b97aSjoerg     Result.swap(Swapped);
155906f32e7eSjoerg     return Result;
156006f32e7eSjoerg   }
1561*da58b97aSjoerg 
1562*da58b97aSjoerg   int OrigNumAssumptionUses = AAQI.NumAssumptionUses;
1563*da58b97aSjoerg   unsigned OrigNumAssumptionBasedResults = AAQI.AssumptionBasedResults.size();
1564*da58b97aSjoerg   AliasResult Result =
1565*da58b97aSjoerg       aliasCheckRecursive(V1, V1Size, V2, V2Size, AAQI, O1, O2);
1566*da58b97aSjoerg 
1567*da58b97aSjoerg   auto It = AAQI.AliasCache.find(Locs);
1568*da58b97aSjoerg   assert(It != AAQI.AliasCache.end() && "Must be in cache");
1569*da58b97aSjoerg   auto &Entry = It->second;
1570*da58b97aSjoerg 
1571*da58b97aSjoerg   // Check whether a NoAlias assumption has been used, but disproven.
1572*da58b97aSjoerg   bool AssumptionDisproven =
1573*da58b97aSjoerg       Entry.NumAssumptionUses > 0 && Result != AliasResult::NoAlias;
1574*da58b97aSjoerg   if (AssumptionDisproven)
1575*da58b97aSjoerg     Result = AliasResult::MayAlias;
1576*da58b97aSjoerg 
1577*da58b97aSjoerg   // This is a definitive result now, when considered as a root query.
1578*da58b97aSjoerg   AAQI.NumAssumptionUses -= Entry.NumAssumptionUses;
1579*da58b97aSjoerg   Entry.Result = Result;
1580*da58b97aSjoerg   // Cache contains sorted {V1,V2} pairs.
1581*da58b97aSjoerg   Entry.Result.swap(Swapped);
1582*da58b97aSjoerg   Entry.NumAssumptionUses = -1;
1583*da58b97aSjoerg 
1584*da58b97aSjoerg   // If the assumption has been disproven, remove any results that may have
1585*da58b97aSjoerg   // been based on this assumption. Do this after the Entry updates above to
1586*da58b97aSjoerg   // avoid iterator invalidation.
1587*da58b97aSjoerg   if (AssumptionDisproven)
1588*da58b97aSjoerg     while (AAQI.AssumptionBasedResults.size() > OrigNumAssumptionBasedResults)
1589*da58b97aSjoerg       AAQI.AliasCache.erase(AAQI.AssumptionBasedResults.pop_back_val());
1590*da58b97aSjoerg 
1591*da58b97aSjoerg   // The result may still be based on assumptions higher up in the chain.
1592*da58b97aSjoerg   // Remember it, so it can be purged from the cache later.
1593*da58b97aSjoerg   if (OrigNumAssumptionUses != AAQI.NumAssumptionUses &&
1594*da58b97aSjoerg       Result != AliasResult::MayAlias)
1595*da58b97aSjoerg     AAQI.AssumptionBasedResults.push_back(Locs);
1596*da58b97aSjoerg   return Result;
159706f32e7eSjoerg }
159806f32e7eSjoerg 
aliasCheckRecursive(const Value * V1,LocationSize V1Size,const Value * V2,LocationSize V2Size,AAQueryInfo & AAQI,const Value * O1,const Value * O2)1599*da58b97aSjoerg AliasResult BasicAAResult::aliasCheckRecursive(
1600*da58b97aSjoerg     const Value *V1, LocationSize V1Size,
1601*da58b97aSjoerg     const Value *V2, LocationSize V2Size,
1602*da58b97aSjoerg     AAQueryInfo &AAQI, const Value *O1, const Value *O2) {
1603*da58b97aSjoerg   if (const GEPOperator *GV1 = dyn_cast<GEPOperator>(V1)) {
1604*da58b97aSjoerg     AliasResult Result = aliasGEP(GV1, V1Size, V2, V2Size, O1, O2, AAQI);
1605*da58b97aSjoerg     if (Result != AliasResult::MayAlias)
1606*da58b97aSjoerg       return Result;
1607*da58b97aSjoerg   } else if (const GEPOperator *GV2 = dyn_cast<GEPOperator>(V2)) {
1608*da58b97aSjoerg     AliasResult Result = aliasGEP(GV2, V2Size, V1, V1Size, O2, O1, AAQI);
1609*da58b97aSjoerg     if (Result != AliasResult::MayAlias)
1610*da58b97aSjoerg       return Result;
161106f32e7eSjoerg   }
1612*da58b97aSjoerg 
161306f32e7eSjoerg   if (const PHINode *PN = dyn_cast<PHINode>(V1)) {
1614*da58b97aSjoerg     AliasResult Result = aliasPHI(PN, V1Size, V2, V2Size, AAQI);
1615*da58b97aSjoerg     if (Result != AliasResult::MayAlias)
1616*da58b97aSjoerg       return Result;
1617*da58b97aSjoerg   } else if (const PHINode *PN = dyn_cast<PHINode>(V2)) {
1618*da58b97aSjoerg     AliasResult Result = aliasPHI(PN, V2Size, V1, V1Size, AAQI);
1619*da58b97aSjoerg     if (Result != AliasResult::MayAlias)
1620*da58b97aSjoerg       return Result;
162106f32e7eSjoerg   }
162206f32e7eSjoerg 
162306f32e7eSjoerg   if (const SelectInst *S1 = dyn_cast<SelectInst>(V1)) {
1624*da58b97aSjoerg     AliasResult Result = aliasSelect(S1, V1Size, V2, V2Size, AAQI);
1625*da58b97aSjoerg     if (Result != AliasResult::MayAlias)
1626*da58b97aSjoerg       return Result;
1627*da58b97aSjoerg   } else if (const SelectInst *S2 = dyn_cast<SelectInst>(V2)) {
1628*da58b97aSjoerg     AliasResult Result = aliasSelect(S2, V2Size, V1, V1Size, AAQI);
1629*da58b97aSjoerg     if (Result != AliasResult::MayAlias)
1630*da58b97aSjoerg       return Result;
163106f32e7eSjoerg   }
163206f32e7eSjoerg 
163306f32e7eSjoerg   // If both pointers are pointing into the same object and one of them
163406f32e7eSjoerg   // accesses the entire object, then the accesses must overlap in some way.
1635*da58b97aSjoerg   if (O1 == O2) {
1636*da58b97aSjoerg     bool NullIsValidLocation = NullPointerIsDefined(&F);
163706f32e7eSjoerg     if (V1Size.isPrecise() && V2Size.isPrecise() &&
163806f32e7eSjoerg         (isObjectSize(O1, V1Size.getValue(), DL, TLI, NullIsValidLocation) ||
1639*da58b97aSjoerg          isObjectSize(O2, V2Size.getValue(), DL, TLI, NullIsValidLocation)))
1640*da58b97aSjoerg       return AliasResult::PartialAlias;
164106f32e7eSjoerg   }
164206f32e7eSjoerg 
1643*da58b97aSjoerg   return AliasResult::MayAlias;
164406f32e7eSjoerg }
164506f32e7eSjoerg 
164606f32e7eSjoerg /// Check whether two Values can be considered equivalent.
164706f32e7eSjoerg ///
164806f32e7eSjoerg /// In addition to pointer equivalence of \p V1 and \p V2 this checks whether
164906f32e7eSjoerg /// they can not be part of a cycle in the value graph by looking at all
165006f32e7eSjoerg /// visited phi nodes an making sure that the phis cannot reach the value. We
165106f32e7eSjoerg /// have to do this because we are looking through phi nodes (That is we say
165206f32e7eSjoerg /// noalias(V, phi(VA, VB)) if noalias(V, VA) and noalias(V, VB).
isValueEqualInPotentialCycles(const Value * V,const Value * V2)165306f32e7eSjoerg bool BasicAAResult::isValueEqualInPotentialCycles(const Value *V,
165406f32e7eSjoerg                                                   const Value *V2) {
165506f32e7eSjoerg   if (V != V2)
165606f32e7eSjoerg     return false;
165706f32e7eSjoerg 
165806f32e7eSjoerg   const Instruction *Inst = dyn_cast<Instruction>(V);
165906f32e7eSjoerg   if (!Inst)
166006f32e7eSjoerg     return true;
166106f32e7eSjoerg 
166206f32e7eSjoerg   if (VisitedPhiBBs.empty())
166306f32e7eSjoerg     return true;
166406f32e7eSjoerg 
166506f32e7eSjoerg   if (VisitedPhiBBs.size() > MaxNumPhiBBsValueReachabilityCheck)
166606f32e7eSjoerg     return false;
166706f32e7eSjoerg 
166806f32e7eSjoerg   // Make sure that the visited phis cannot reach the Value. This ensures that
166906f32e7eSjoerg   // the Values cannot come from different iterations of a potential cycle the
167006f32e7eSjoerg   // phi nodes could be involved in.
167106f32e7eSjoerg   for (auto *P : VisitedPhiBBs)
1672*da58b97aSjoerg     if (isPotentiallyReachable(&P->front(), Inst, nullptr, DT))
167306f32e7eSjoerg       return false;
167406f32e7eSjoerg 
167506f32e7eSjoerg   return true;
167606f32e7eSjoerg }
167706f32e7eSjoerg 
167806f32e7eSjoerg /// Computes the symbolic difference between two de-composed GEPs.
167906f32e7eSjoerg ///
168006f32e7eSjoerg /// Dest and Src are the variable indices from two decomposed GetElementPtr
168106f32e7eSjoerg /// instructions GEP1 and GEP2 which have common base pointers.
GetIndexDifference(SmallVectorImpl<VariableGEPIndex> & Dest,const SmallVectorImpl<VariableGEPIndex> & Src)168206f32e7eSjoerg void BasicAAResult::GetIndexDifference(
168306f32e7eSjoerg     SmallVectorImpl<VariableGEPIndex> &Dest,
168406f32e7eSjoerg     const SmallVectorImpl<VariableGEPIndex> &Src) {
168506f32e7eSjoerg   if (Src.empty())
168606f32e7eSjoerg     return;
168706f32e7eSjoerg 
168806f32e7eSjoerg   for (unsigned i = 0, e = Src.size(); i != e; ++i) {
168906f32e7eSjoerg     const Value *V = Src[i].V;
169006f32e7eSjoerg     unsigned ZExtBits = Src[i].ZExtBits, SExtBits = Src[i].SExtBits;
169106f32e7eSjoerg     APInt Scale = Src[i].Scale;
169206f32e7eSjoerg 
169306f32e7eSjoerg     // Find V in Dest.  This is N^2, but pointer indices almost never have more
169406f32e7eSjoerg     // than a few variable indexes.
169506f32e7eSjoerg     for (unsigned j = 0, e = Dest.size(); j != e; ++j) {
169606f32e7eSjoerg       if (!isValueEqualInPotentialCycles(Dest[j].V, V) ||
169706f32e7eSjoerg           Dest[j].ZExtBits != ZExtBits || Dest[j].SExtBits != SExtBits)
169806f32e7eSjoerg         continue;
169906f32e7eSjoerg 
170006f32e7eSjoerg       // If we found it, subtract off Scale V's from the entry in Dest.  If it
170106f32e7eSjoerg       // goes to zero, remove the entry.
170206f32e7eSjoerg       if (Dest[j].Scale != Scale)
170306f32e7eSjoerg         Dest[j].Scale -= Scale;
170406f32e7eSjoerg       else
170506f32e7eSjoerg         Dest.erase(Dest.begin() + j);
170606f32e7eSjoerg       Scale = 0;
170706f32e7eSjoerg       break;
170806f32e7eSjoerg     }
170906f32e7eSjoerg 
171006f32e7eSjoerg     // If we didn't consume this entry, add it to the end of the Dest list.
171106f32e7eSjoerg     if (!!Scale) {
1712*da58b97aSjoerg       VariableGEPIndex Entry = {V, ZExtBits, SExtBits, -Scale, Src[i].CxtI};
171306f32e7eSjoerg       Dest.push_back(Entry);
171406f32e7eSjoerg     }
171506f32e7eSjoerg   }
171606f32e7eSjoerg }
171706f32e7eSjoerg 
constantOffsetHeuristic(const SmallVectorImpl<VariableGEPIndex> & VarIndices,LocationSize MaybeV1Size,LocationSize MaybeV2Size,const APInt & BaseOffset,AssumptionCache * AC,DominatorTree * DT)171806f32e7eSjoerg bool BasicAAResult::constantOffsetHeuristic(
171906f32e7eSjoerg     const SmallVectorImpl<VariableGEPIndex> &VarIndices,
1720*da58b97aSjoerg     LocationSize MaybeV1Size, LocationSize MaybeV2Size, const APInt &BaseOffset,
172106f32e7eSjoerg     AssumptionCache *AC, DominatorTree *DT) {
1722*da58b97aSjoerg   if (VarIndices.size() != 2 || !MaybeV1Size.hasValue() ||
1723*da58b97aSjoerg       !MaybeV2Size.hasValue())
172406f32e7eSjoerg     return false;
172506f32e7eSjoerg 
172606f32e7eSjoerg   const uint64_t V1Size = MaybeV1Size.getValue();
172706f32e7eSjoerg   const uint64_t V2Size = MaybeV2Size.getValue();
172806f32e7eSjoerg 
172906f32e7eSjoerg   const VariableGEPIndex &Var0 = VarIndices[0], &Var1 = VarIndices[1];
173006f32e7eSjoerg 
173106f32e7eSjoerg   if (Var0.ZExtBits != Var1.ZExtBits || Var0.SExtBits != Var1.SExtBits ||
1732*da58b97aSjoerg       Var0.Scale != -Var1.Scale || Var0.V->getType() != Var1.V->getType())
173306f32e7eSjoerg     return false;
173406f32e7eSjoerg 
173506f32e7eSjoerg   // We'll strip off the Extensions of Var0 and Var1 and do another round
173606f32e7eSjoerg   // of GetLinearExpression decomposition. In the example above, if Var0
173706f32e7eSjoerg   // is zext(%x + 1) we should get V1 == %x and V1Offset == 1.
173806f32e7eSjoerg 
1739*da58b97aSjoerg   LinearExpression E0 =
1740*da58b97aSjoerg       GetLinearExpression(ExtendedValue(Var0.V), DL, 0, AC, DT);
1741*da58b97aSjoerg   LinearExpression E1 =
1742*da58b97aSjoerg       GetLinearExpression(ExtendedValue(Var1.V), DL, 0, AC, DT);
1743*da58b97aSjoerg   if (E0.Scale != E1.Scale || E0.Val.ZExtBits != E1.Val.ZExtBits ||
1744*da58b97aSjoerg       E0.Val.SExtBits != E1.Val.SExtBits ||
1745*da58b97aSjoerg       !isValueEqualInPotentialCycles(E0.Val.V, E1.Val.V))
174606f32e7eSjoerg     return false;
174706f32e7eSjoerg 
174806f32e7eSjoerg   // We have a hit - Var0 and Var1 only differ by a constant offset!
174906f32e7eSjoerg 
175006f32e7eSjoerg   // If we've been sext'ed then zext'd the maximum difference between Var0 and
175106f32e7eSjoerg   // Var1 is possible to calculate, but we're just interested in the absolute
175206f32e7eSjoerg   // minimum difference between the two. The minimum distance may occur due to
175306f32e7eSjoerg   // wrapping; consider "add i3 %i, 5": if %i == 7 then 7 + 5 mod 8 == 4, and so
175406f32e7eSjoerg   // the minimum distance between %i and %i + 5 is 3.
1755*da58b97aSjoerg   APInt MinDiff = E0.Offset - E1.Offset, Wrapped = -MinDiff;
175606f32e7eSjoerg   MinDiff = APIntOps::umin(MinDiff, Wrapped);
175706f32e7eSjoerg   APInt MinDiffBytes =
175806f32e7eSjoerg     MinDiff.zextOrTrunc(Var0.Scale.getBitWidth()) * Var0.Scale.abs();
175906f32e7eSjoerg 
176006f32e7eSjoerg   // We can't definitely say whether GEP1 is before or after V2 due to wrapping
176106f32e7eSjoerg   // arithmetic (i.e. for some values of GEP1 and V2 GEP1 < V2, and for other
176206f32e7eSjoerg   // values GEP1 > V2). We'll therefore only declare NoAlias if both V1Size and
176306f32e7eSjoerg   // V2Size can fit in the MinDiffBytes gap.
176406f32e7eSjoerg   return MinDiffBytes.uge(V1Size + BaseOffset.abs()) &&
176506f32e7eSjoerg          MinDiffBytes.uge(V2Size + BaseOffset.abs());
176606f32e7eSjoerg }
176706f32e7eSjoerg 
176806f32e7eSjoerg //===----------------------------------------------------------------------===//
176906f32e7eSjoerg // BasicAliasAnalysis Pass
177006f32e7eSjoerg //===----------------------------------------------------------------------===//
177106f32e7eSjoerg 
177206f32e7eSjoerg AnalysisKey BasicAA::Key;
177306f32e7eSjoerg 
run(Function & F,FunctionAnalysisManager & AM)177406f32e7eSjoerg BasicAAResult BasicAA::run(Function &F, FunctionAnalysisManager &AM) {
1775*da58b97aSjoerg   auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
1776*da58b97aSjoerg   auto &AC = AM.getResult<AssumptionAnalysis>(F);
1777*da58b97aSjoerg   auto *DT = &AM.getResult<DominatorTreeAnalysis>(F);
1778*da58b97aSjoerg   auto *PV = AM.getCachedResult<PhiValuesAnalysis>(F);
1779*da58b97aSjoerg   return BasicAAResult(F.getParent()->getDataLayout(), F, TLI, AC, DT, PV);
178006f32e7eSjoerg }
178106f32e7eSjoerg 
BasicAAWrapperPass()178206f32e7eSjoerg BasicAAWrapperPass::BasicAAWrapperPass() : FunctionPass(ID) {
178306f32e7eSjoerg   initializeBasicAAWrapperPassPass(*PassRegistry::getPassRegistry());
178406f32e7eSjoerg }
178506f32e7eSjoerg 
178606f32e7eSjoerg char BasicAAWrapperPass::ID = 0;
178706f32e7eSjoerg 
anchor()178806f32e7eSjoerg void BasicAAWrapperPass::anchor() {}
178906f32e7eSjoerg 
1790*da58b97aSjoerg INITIALIZE_PASS_BEGIN(BasicAAWrapperPass, "basic-aa",
1791*da58b97aSjoerg                       "Basic Alias Analysis (stateless AA impl)", true, true)
INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)179206f32e7eSjoerg INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
179306f32e7eSjoerg INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
179406f32e7eSjoerg INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
1795*da58b97aSjoerg INITIALIZE_PASS_DEPENDENCY(PhiValuesWrapperPass)
1796*da58b97aSjoerg INITIALIZE_PASS_END(BasicAAWrapperPass, "basic-aa",
1797*da58b97aSjoerg                     "Basic Alias Analysis (stateless AA impl)", true, true)
179806f32e7eSjoerg 
179906f32e7eSjoerg FunctionPass *llvm::createBasicAAWrapperPass() {
180006f32e7eSjoerg   return new BasicAAWrapperPass();
180106f32e7eSjoerg }
180206f32e7eSjoerg 
runOnFunction(Function & F)180306f32e7eSjoerg bool BasicAAWrapperPass::runOnFunction(Function &F) {
180406f32e7eSjoerg   auto &ACT = getAnalysis<AssumptionCacheTracker>();
180506f32e7eSjoerg   auto &TLIWP = getAnalysis<TargetLibraryInfoWrapperPass>();
180606f32e7eSjoerg   auto &DTWP = getAnalysis<DominatorTreeWrapperPass>();
180706f32e7eSjoerg   auto *PVWP = getAnalysisIfAvailable<PhiValuesWrapperPass>();
180806f32e7eSjoerg 
180906f32e7eSjoerg   Result.reset(new BasicAAResult(F.getParent()->getDataLayout(), F,
181006f32e7eSjoerg                                  TLIWP.getTLI(F), ACT.getAssumptionCache(F),
181106f32e7eSjoerg                                  &DTWP.getDomTree(),
181206f32e7eSjoerg                                  PVWP ? &PVWP->getResult() : nullptr));
181306f32e7eSjoerg 
181406f32e7eSjoerg   return false;
181506f32e7eSjoerg }
181606f32e7eSjoerg 
getAnalysisUsage(AnalysisUsage & AU) const181706f32e7eSjoerg void BasicAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
181806f32e7eSjoerg   AU.setPreservesAll();
1819*da58b97aSjoerg   AU.addRequiredTransitive<AssumptionCacheTracker>();
1820*da58b97aSjoerg   AU.addRequiredTransitive<DominatorTreeWrapperPass>();
1821*da58b97aSjoerg   AU.addRequiredTransitive<TargetLibraryInfoWrapperPass>();
182206f32e7eSjoerg   AU.addUsedIfAvailable<PhiValuesWrapperPass>();
182306f32e7eSjoerg }
182406f32e7eSjoerg 
createLegacyPMBasicAAResult(Pass & P,Function & F)182506f32e7eSjoerg BasicAAResult llvm::createLegacyPMBasicAAResult(Pass &P, Function &F) {
182606f32e7eSjoerg   return BasicAAResult(
182706f32e7eSjoerg       F.getParent()->getDataLayout(), F,
182806f32e7eSjoerg       P.getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F),
182906f32e7eSjoerg       P.getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F));
183006f32e7eSjoerg }
1831