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