1//===- BasicAliasAnalysis.cpp - Stateless Alias Analysis Impl -------------===// 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 7//===----------------------------------------------------------------------===// 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. 13//===----------------------------------------------------------------------===// 64#define DEBUG_TYPE "basicaa" 68/// Enable analysis of recursive PHI nodes. 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");
82// The max limit of the search depth in DecomposeGEPExpression() and 83// getUnderlyingObject(). 88// We don't care if this analysis itself is preserved, it has no state. But 89// we need to check that the analyses it depends on have been. Note that we 90// may be created without handles to some analyses and in that case don't 96// Otherwise this analysis result remains valid. 100//===----------------------------------------------------------------------===// 102//===----------------------------------------------------------------------===// 104/// Returns the size of the object specified by V or UnknownSize if unknown. 109bool RoundToAlign =
false) {
119/// Returns true if we can prove that the object specified by V is smaller than 120/// Size. Bails out early unless the root object is passed as the first 125bool NullIsValidLoc) {
126// Note that the meanings of the "object" are slightly different in the 127// following contexts: 128// c1: llvm::getObjectSize() 129// c2: llvm.objectsize() intrinsic 130// c3: isObjectSmallerThan() 131// c1 and c2 share the same meaning; however, the meaning of "object" in c3 132// refers to the "entire object". 134// Consider this example: 135// char *p = (char*)malloc(100) 138// In the context of c1 and c2, the "object" pointed by q refers to the 139// stretch of memory of q[0:19]. So, getObjectSize(q) should return 20. 141// In the context of c3, the "object" refers to the chunk of memory being 142// allocated. So, the "object" has 100 bytes, and q points to the middle the 143// "object". However, unless p, the root object, is passed as the first 144// parameter, the call to isIdentifiedObject() makes isObjectSmallerThan() 149// This function needs to use the aligned object size because we allow 150// reads a bit past the end given sufficient alignment. 151 std::optional<TypeSize> ObjectSize =
getObjectSize(V,
DL, TLI, NullIsValidLoc,
152/*RoundToAlign*/true);
157/// Return the minimal extent from \p V to the end of the underlying object, 158/// assuming the result is used in an aliasing query. E.g., we do use the query 159/// location size and the fact that null pointers cannot alias here. 163bool NullIsValidLoc) {
164// If we have dereferenceability information we know a lower bound for the 165// extent as accesses for a lower offset would be valid. We need to exclude 166// the "or null" part if null is a valid pointer. We can ignore frees, as an 167// access after free would be undefined behavior. 168bool CanBeNull, CanBeFreed;
170 V.getPointerDereferenceableBytes(
DL, CanBeNull, CanBeFreed);
171 DerefBytes = (CanBeNull && NullIsValidLoc) ? 0 : DerefBytes;
172// If queried with a precise location size, we assume that location size to be 173// accessed, thus valid. 179/// Returns true if we can prove that the object specified by V has size Size. 182 std::optional<TypeSize> ObjectSize =
184return ObjectSize && *ObjectSize ==
Size;
187/// Return true if both V1 and V2 are VScale 193//===----------------------------------------------------------------------===// 194// CaptureAnalysis implementations 195//===----------------------------------------------------------------------===// 209return Succs.
empty() ||
219auto Iter = EarliestEscapes.insert({Object,
nullptr});
223/*ReturnCaptures=*/false,
/*StoreCaptures=*/true, DT);
225 Inst2Obj[EarliestCapture].push_back(Object);
226 Iter.first->second = EarliestCapture;
229// No capturing instruction. 230if (!Iter.first->second)
233// No context instruction means any use is capturing. 237if (
I == Iter.first->second) {
247auto Iter = Inst2Obj.find(
I);
248if (Iter != Inst2Obj.end()) {
249for (
constValue *Obj : Iter->second)
250 EarliestEscapes.erase(Obj);
255//===----------------------------------------------------------------------===// 256// GetElementPtr Instruction Decomposition and Analysis 257//===----------------------------------------------------------------------===// 260/// Represents zext(sext(trunc(V))). 263unsigned ZExtBits = 0;
264unsigned SExtBits = 0;
265unsigned TruncBits = 0;
266 /// Whether trunc(V) is non-negative. 267bool IsNonNegative =
false;
269explicit CastedValue(
constValue *V) : V(V) {}
270explicit CastedValue(
constValue *V,
unsigned ZExtBits,
unsigned SExtBits,
271unsigned TruncBits,
bool IsNonNegative)
272 : V(V), ZExtBits(ZExtBits), SExtBits(SExtBits), TruncBits(TruncBits),
273 IsNonNegative(IsNonNegative) {}
276returnV->getType()->getPrimitiveSizeInBits() - TruncBits + ZExtBits +
280 CastedValue withValue(
constValue *NewV,
bool PreserveNonNeg)
const{
281return CastedValue(NewV, ZExtBits, SExtBits, TruncBits,
282 IsNonNegative && PreserveNonNeg);
285 /// Replace V with zext(NewV) 286 CastedValue withZExtOfValue(
constValue *NewV,
bool ZExtNonNegative)
const{
287unsigned ExtendBy =
V->getType()->getPrimitiveSizeInBits() -
289if (ExtendBy <= TruncBits)
290// zext<nneg>(trunc(zext(NewV))) == zext<nneg>(trunc(NewV)) 291// The nneg can be preserved on the outer zext here. 292return CastedValue(NewV, ZExtBits, SExtBits, TruncBits - ExtendBy,
295// zext(sext(zext(NewV))) == zext(zext(zext(NewV))) 296 ExtendBy -= TruncBits;
297// zext<nneg>(zext(NewV)) == zext(NewV) 298// zext(zext<nneg>(NewV)) == zext<nneg>(NewV) 299// The nneg can be preserved from the inner zext here but must be dropped 301return CastedValue(NewV, ZExtBits + SExtBits + ExtendBy, 0, 0,
305 /// Replace V with sext(NewV) 306 CastedValue withSExtOfValue(
constValue *NewV)
const{
307unsigned ExtendBy =
V->getType()->getPrimitiveSizeInBits() -
309if (ExtendBy <= TruncBits)
310// zext<nneg>(trunc(sext(NewV))) == zext<nneg>(trunc(NewV)) 311// The nneg can be preserved on the outer zext here 312return CastedValue(NewV, ZExtBits, SExtBits, TruncBits - ExtendBy,
315// zext(sext(sext(NewV))) 316 ExtendBy -= TruncBits;
317// zext<nneg>(sext(sext(NewV))) = zext<nneg>(sext(NewV)) 318// The nneg can be preserved on the outer zext here 319return CastedValue(NewV, ZExtBits, SExtBits + ExtendBy, 0, IsNonNegative);
323assert(
N.getBitWidth() ==
V->getType()->getPrimitiveSizeInBits() &&
324"Incompatible bit width");
325if (TruncBits)
N =
N.trunc(
N.getBitWidth() - TruncBits);
326if (SExtBits)
N =
N.sext(
N.getBitWidth() + SExtBits);
327if (ZExtBits)
N =
N.zext(
N.getBitWidth() + ZExtBits);
332assert(
N.getBitWidth() ==
V->getType()->getPrimitiveSizeInBits() &&
333"Incompatible bit width");
334if (TruncBits)
N =
N.truncate(
N.getBitWidth() - TruncBits);
335if (IsNonNegative && !
N.isAllNonNegative())
339if (SExtBits)
N =
N.signExtend(
N.getBitWidth() + SExtBits);
340if (ZExtBits)
N =
N.zeroExtend(
N.getBitWidth() + ZExtBits);
344bool canDistributeOver(
bool NUW,
bool NSW)
const{
345// zext(x op<nuw> y) == zext(x) op<nuw> zext(y) 346// sext(x op<nsw> y) == sext(x) op<nsw> sext(y) 347// trunc(x op y) == trunc(x) op trunc(y) 348return (!ZExtBits || NUW) && (!SExtBits || NSW);
351bool hasSameCastsAs(
const CastedValue &
Other)
const{
352if (
V->getType() !=
Other.V->getType())
355if (ZExtBits ==
Other.ZExtBits && SExtBits ==
Other.SExtBits &&
356 TruncBits ==
Other.TruncBits)
358// If either CastedValue has a nneg zext then the sext/zext bits are 359// interchangable for that value. 360if (IsNonNegative ||
Other.IsNonNegative)
361return (ZExtBits + SExtBits ==
Other.ZExtBits +
Other.SExtBits &&
362 TruncBits ==
Other.TruncBits);
367/// Represents zext(sext(trunc(V))) * Scale + Offset. 368structLinearExpression {
373 /// True if all operations in this expression are NUW. 375 /// True if all operations in this expression are NSW. 378 LinearExpression(
const CastedValue &Val,
constAPInt &Scale,
380 : Val(Val), Scale(Scale),
Offset(
Offset), IsNUW(IsNUW), IsNSW(IsNSW) {}
382 LinearExpression(
const CastedValue &Val)
383 : Val(Val), IsNUW(
true), IsNSW(
true) {
384unsignedBitWidth = Val.getBitWidth();
389 LinearExpression mul(
constAPInt &
Other,
bool MulIsNUW,
bool MulIsNSW)
const{
390// The check for zero offset is necessary, because generally 391// (X +nsw Y) *nsw Z does not imply (X *nsw Z) +nsw (Y *nsw Z). 392bool NSW = IsNSW && (
Other.isOne() || (MulIsNSW &&
Offset.isZero()));
393bool NUW = IsNUW && (
Other.isOne() || MulIsNUW);
399/// Analyzes the specified value as a linear expression: "A*V + B", where A and 400/// B are constant integers. 404// Limit our recursion depth. 408if (
constConstantInt *Const = dyn_cast<ConstantInt>(Val.V))
409return LinearExpression(Val,
APInt(Val.getBitWidth(), 0),
410 Val.evaluateWith(Const->getValue()),
true,
true);
413if (
ConstantInt *RHSC = dyn_cast<ConstantInt>(BOp->getOperand(1))) {
414APIntRHS = Val.evaluateWith(RHSC->getValue());
415// The only non-OBO case we deal with is or, and only limited to the 416// case where it is both nuw and nsw. 417bool NUW =
true, NSW =
true;
418if (isa<OverflowingBinaryOperator>(BOp)) {
419 NUW &= BOp->hasNoUnsignedWrap();
420 NSW &= BOp->hasNoSignedWrap();
422if (!Val.canDistributeOver(NUW, NSW))
425// While we can distribute over trunc, we cannot preserve nowrap flags 430 LinearExpression E(Val);
431switch (BOp->getOpcode()) {
433// We don't understand this instruction, so we can't decompose it any 437// X|C == X+C if it is disjoint. Otherwise we can't analyze it. 438if (!cast<PossiblyDisjointInst>(BOp)->isDisjoint())
442case Instruction::Add: {
450case Instruction::Sub: {
454 E.IsNUW =
false;
// sub nuw x, y is not add nuw x, -y. 458case Instruction::Mul:
463case Instruction::Shl:
464// We're trying to linearize an expression of the kind: 466// where the shift count exceeds the bitwidth of the type. 467// We can't decompose this further (the expression would return 469if (
RHS.getLimitedValue() > Val.getBitWidth())
474 E.Offset <<=
RHS.getLimitedValue();
475 E.Scale <<=
RHS.getLimitedValue();
484if (
constauto *ZExt = dyn_cast<ZExtInst>(Val.V))
486 Val.withZExtOfValue(ZExt->getOperand(0), ZExt->hasNonNeg()),
DL,
489if (isa<SExtInst>(Val.V))
491 Val.withSExtOfValue(cast<CastInst>(Val.V)->getOperand(0)),
498// A linear transformation of a Value; this class represents 499// ZExt(SExt(Trunc(V, TruncBits), SExtBits), ZExtBits) * Scale. 500structVariableGEPIndex {
504// Context instruction to use when querying information about this index. 507 /// True if all operations in this expression are NSW. 510 /// True if the index should be subtracted rather than added. We don't simply 511 /// negate the Scale, to avoid losing the NSW flag: X - INT_MIN*1 may be 512 /// non-wrapping, while X + INT_MIN*(-1) wraps. 515bool hasNegatedScaleOf(
const VariableGEPIndex &
Other)
const{
516if (IsNegated ==
Other.IsNegated)
517return Scale == -
Other.Scale;
518return Scale ==
Other.Scale;
526OS <<
"(V=" << Val.V->getName()
527 <<
", zextbits=" << Val.ZExtBits
528 <<
", sextbits=" << Val.SExtBits
529 <<
", truncbits=" << Val.TruncBits
530 <<
", scale=" << Scale
532 <<
", negated=" << IsNegated <<
")";
537// Represents the internal structure of a GEP, decomposed into a base pointer, 538// constant offsets, and variable scaled indices. 540// Base pointer of the GEP 542// Total constant offset from base. 544// Scaled variable (non-constant) indices. 546// Nowrap flags common to all GEP operations involved in expression. 568/// If V is a symbolic pointer expression, decompose it into a base pointer 569/// with a constant offset and a number of scaled symbolic offsets. 571/// The scaled symbolic offsets (represented by pairs of a Value* and a scale 572/// in the VarIndices vector) are Value*'s that are known to be scaled by the 573/// specified amount, but which may have other unrepresented high bits. As 574/// such, the gep cannot necessarily be reconstructed from its decomposed form. 578// Limit recursion depth to limit compile time in crazy cases. 583unsigned IndexSize =
DL.getIndexTypeSizeInBits(V->getType());
584 DecomposedGEP Decomposed;
585 Decomposed.Offset =
APInt(IndexSize, 0);
587// See if this is a bitcast or GEP. 590// The only non-operator case we can handle are GlobalAliases. 591if (
constGlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
592if (!GA->isInterposable()) {
593 V = GA->getAliasee();
601if (
Op->getOpcode() == Instruction::BitCast ||
602Op->getOpcode() == Instruction::AddrSpaceCast) {
604// Don't look through casts between address spaces with differing index 606if (
DL.getIndexTypeSizeInBits(NewV->
getType()) != IndexSize) {
616if (
constauto *
PHI = dyn_cast<PHINode>(V)) {
617// Look through single-arg phi nodes created by LCSSA. 618if (
PHI->getNumIncomingValues() == 1) {
619V =
PHI->getIncomingValue(0);
622 }
elseif (
constauto *Call = dyn_cast<CallBase>(V)) {
623// CaptureTracking can know about special capturing properties of some 624// intrinsics like launder.invariant.group, that can't be expressed with 625// the attributes, but have properties like returning aliasing pointer. 626// Because some analysis may assume that nocaptured pointer is not 627// returned from some special intrinsic (because function would have to 628// be marked with returns attribute), it is crucial to use this function 629// because it should be in sync with CaptureTracking. Not using it may 630// cause weird miscompilations where 2 aliasing pointers are assumed to 642// Track the common nowrap flags for all GEPs we see. 647// Walk the indices of the GEP, accumulating them into BaseOff/VarIndices. 652// Compute the (potentially symbolic) offset in bytes for this index. 654// For a struct, add the member offset. 655unsigned FieldNo = cast<ConstantInt>(Index)->getZExtValue();
659 Decomposed.Offset +=
DL.getStructLayout(STy)->getElementOffset(FieldNo);
663// For an array/pointer, add the element offset, explicitly scaled. 664if (
constConstantInt *CIdx = dyn_cast<ConstantInt>(Index)) {
668// Don't attempt to analyze GEPs if the scalable index is not zero. 676 CIdx->getValue().sextOrTrunc(IndexSize);
686// If the integer type is smaller than the index size, it is implicitly 687// sign extended or truncated to index size. 690bool NonNeg = NUSW && NUW;
691unsigned Width =
Index->getType()->getIntegerBitWidth();
692unsigned SExtBits = IndexSize > Width ? IndexSize - Width : 0;
693unsigned TruncBits = IndexSize < Width ? Width - IndexSize : 0;
695 CastedValue(Index, 0, SExtBits, TruncBits, NonNeg), DL, 0, AC, DT);
697// Scale by the type size. 700 Decomposed.Offset +=
LE.Offset;
703 Decomposed.NWFlags = Decomposed.NWFlags.withoutNoUnsignedWrap();
705// If we already had an occurrence of this index variable, merge this 706// scale into it. For example, we want to handle: 707// A[x][x] -> x*16 + x*4 -> x*20 708// This also ensures that 'x' only appears in the index list once. 709for (
unsigned i = 0, e = Decomposed.VarIndices.size(); i != e; ++i) {
710if ((Decomposed.VarIndices[i].Val.V ==
LE.Val.V ||
712 Decomposed.VarIndices[i].Val.hasSameCastsAs(
LE.Val)) {
713 Scale += Decomposed.VarIndices[i].Scale;
714// We cannot guarantee no-wrap for the merge. 715LE.IsNSW =
LE.IsNUW =
false;
716 Decomposed.VarIndices.erase(Decomposed.VarIndices.begin() + i);
722 VariableGEPIndex
Entry = {
LE.Val, Scale, CxtI,
LE.IsNSW,
723/* IsNegated */false};
724 Decomposed.VarIndices.push_back(Entry);
728// Analyze the base pointer next. 730 }
while (--MaxLookup);
732// If the chain of expressions is too deep, just return early. 734 SearchLimitReached++;
741assert(Visited.empty() &&
"Visited must be cleared after use!");
744unsigned MaxLookup = 8;
751if (!Visited.insert(V).second)
754// Ignore allocas if we were instructed to do so. 755if (IgnoreLocals && isa<AllocaInst>(V))
758// If the location points to memory that is known to be invariant for 759// the life of the underlying SSA value, then we can exclude Mod from 760// the set of valid memory effects. 762// An argument that is marked readonly and noalias is known to be 763// invariant while that function is executing. 764if (
constArgument *Arg = dyn_cast<Argument>(V)) {
765if (Arg->hasNoAliasAttr() && Arg->onlyReadsMemory()) {
771// A global constant can't be mutated. 773// Note: this doesn't require GV to be "ODR" because it isn't legal for a 774// global to be marked constant in some modules and non-constant in 775// others. GV may even be a declaration, not a definition. 776if (!GV->isConstant())
781// If both select values point to local memory, then so does the select. 782if (
constSelectInst *SI = dyn_cast<SelectInst>(V)) {
788// If all values incoming to a phi node point to local memory, then so does 790if (
constPHINode *PN = dyn_cast<PHINode>(V)) {
791// Don't bother inspecting phi nodes with many operands. 792if (PN->getNumIncomingValues() > MaxLookup)
798// Otherwise be conservative. 800 }
while (!Worklist.
empty() && --MaxLookup);
802// If we hit the maximum number of instructions to examine, be conservative. 803if (!Worklist.
empty())
811returnII &&
II->getIntrinsicID() == IID;
814/// Returns the behavior when calling the given call site. 819if (
constFunction *F = dyn_cast<Function>(Call->getCalledOperand())) {
821// Operand bundles on the call may also read or write memory, in addition 822// to the behavior of the called function. 823if (Call->hasReadingOperandBundles())
825if (Call->hasClobberingOperandBundles())
833/// Returns the behavior when calling the given function. For use when the call 834/// site is not known. 836switch (
F->getIntrinsicID()) {
837case Intrinsic::experimental_guard:
838case Intrinsic::experimental_deoptimize:
839// These intrinsics can read arbitrary memory, and additionally modref 840// inaccessible memory to model control dependence. 845returnF->getMemoryEffects();
850if (Call->paramHasAttr(ArgIdx, Attribute::WriteOnly))
853if (Call->paramHasAttr(ArgIdx, Attribute::ReadOnly))
856if (Call->paramHasAttr(ArgIdx, Attribute::ReadNone))
864if (
constInstruction *inst = dyn_cast<Instruction>(V)) {
865if (!inst->getParent())
870if (
constArgument *arg = dyn_cast<Argument>(V))
871return arg->getParent();
881return !F1 || !F2 || F1 == F2;
889"BasicAliasAnalysis doesn't support interprocedural queries.");
890return aliasCheck(LocA.
Ptr, LocA.
Size, LocB.
Ptr, LocB.
Size, AAQI, CtxI);
893/// Checks to see if the specified callsite can clobber the specified memory 896/// Since we only look at local properties of this function, we really can't 897/// say much about this query. We do, however, use simple "address taken" 898/// analysis on local objects. 903"AliasAnalysis query involving multiple functions!");
907// Calls marked 'tail' cannot read or write allocas from the current frame 908// because the current frame might be destroyed by the time they run. However, 909// a tail call may use an alloca with byval. Calling with byval copies the 910// contents of the alloca into argument registers or stack slots, so there is 912if (isa<AllocaInst>(Object))
913if (
constCallInst *CI = dyn_cast<CallInst>(Call))
914if (CI->isTailCall() &&
915 !CI->getAttributes().hasAttrSomewhere(Attribute::ByVal))
918// Stack restore is able to modify unescaped dynamic allocas. Assume it may 919// modify them even though the alloca is not escaped. 920if (
auto *AI = dyn_cast<AllocaInst>(Object))
921if (!AI->isStaticAlloca() &&
isIntrinsicCall(Call, Intrinsic::stackrestore))
924// A call can access a locally allocated object either because it is passed as 925// an argument to the call, or because it has escaped prior to the call. 927// Make sure the object has not escaped here, and then check that none of the 928// call arguments alias the object below. 930// We model calls that can return twice (setjmp) as clobbering non-escaping 931// objects, to model any accesses that may occur prior to the second return. 932// As an exception, ignore allocas, as setjmp is not required to preserve 933// non-volatile stores for them. 934if (!isa<Constant>(Object) && Call != Object &&
936 (isa<AllocaInst>(Object) || !Call->hasFnAttr(Attribute::ReturnsTwice))) {
938// Optimistically assume that call doesn't touch Object and check this 939// assumption in the following loop. 942unsigned OperandNo = 0;
943for (
auto CI = Call->data_operands_begin(), CE = Call->data_operands_end();
944 CI != CE; ++CI, ++OperandNo) {
945if (!(*CI)->getType()->isPointerTy())
948// Call doesn't access memory through this operand, so we don't care 949// if it aliases with Object. 950if (Call->doesNotAccessMemory(OperandNo))
953// If this is a no-capture pointer argument, see if we can tell that it 954// is impossible to alias the pointer we're checking. 958// Operand doesn't alias 'Object', continue looking for other aliases 961// Operand aliases 'Object', but call doesn't modify it. Strengthen 962// initial assumption and keep looking in case if there are more aliases. 963if (Call->onlyReadsMemory(OperandNo)) {
967// Operand aliases 'Object' but call only writes into it. 968if (Call->onlyWritesMemory(OperandNo)) {
972// This operand aliases 'Object' and call reads and writes into it. 973// Setting ModRef will not yield an early return below, MustAlias is not 979// Early return if we improved mod ref information 984// If the call is malloc/calloc like, we can assume that it doesn't 985// modify any IR visible value. This is only valid because we assume these 986// routines do not read values visible in the IR. TODO: Consider special 987// casing realloc and strdup routines which access only their arguments as 988// well. Or alternatively, replace all of this with inaccessiblememonly once 989// that's implemented fully. 991// Be conservative if the accessed pointer may alias the allocation - 992// fallback to the generic handling below. 998// Like assumes, invariant.start intrinsics were also marked as arbitrarily 999// writing so that proper control dependencies are maintained but they never 1000// mod any particular memory location visible to the IR. 1001// *Unlike* assumes (which are now modeled as NoModRef), invariant.start 1002// intrinsic is now modeled as reading memory. This prevents hoisting the 1003// invariant.start intrinsic over stores. Consider: 1006// invariant_start(ptr) 1010// This cannot be transformed to: 1013// invariant_start(ptr) 1018// The transformation will cause the second store to be ignored (based on 1019// rules of invariant.start) and print 40, while the first program always 1031// Guard intrinsics are marked as arbitrarily writing so that proper control 1032// dependencies are maintained but they never mods any particular memory 1035// *Unlike* assumes, guard intrinsics are modeled as reading memory since the 1036// heap state at the point the guard is issued needs to be consistent in case 1037// the guard invokes the "deopt" continuation. 1039// NB! This function is *not* commutative, so we special case two 1040// possibilities for guard intrinsics. 1056/// Provides a bunch of ad-hoc rules to disambiguate a GEP instruction against 1059/// We know that V1 is a GEP, but we don't know anything about V2. 1060/// UnderlyingV1 is getUnderlyingObject(GEP1), UnderlyingV2 is the same for 1066auto BaseObjectsAlias = [&]() {
1075// TODO: This limitation exists for compile-time reasons. Relax it if we 1076// can avoid exponential pathological cases. 1077if (!isa<GEPOperator>(V2))
1080// If both accesses have unknown size, we can only check whether the base 1081// objects don't alias. 1082return BaseObjectsAlias();
1086 DecomposedGEP DecompGEP1 = DecomposeGEPExpression(GEP1, DL, &AC, DT);
1087 DecomposedGEP DecompGEP2 = DecomposeGEPExpression(V2, DL, &AC, DT);
1089// Bail if we were not able to decompose anything. 1090if (DecompGEP1.Base == GEP1 && DecompGEP2.Base == V2)
1093// Fall back to base objects if pointers have different index widths. 1094if (DecompGEP1.Offset.getBitWidth() != DecompGEP2.Offset.getBitWidth())
1095return BaseObjectsAlias();
1097// Swap GEP1 and GEP2 if GEP2 has more variable indices. 1098if (DecompGEP1.VarIndices.size() < DecompGEP2.VarIndices.size()) {
1104// Subtract the GEP2 pointer from the GEP1 pointer to find out their 1105// symbolic difference. 1106 subtractDecomposedGEPs(DecompGEP1, DecompGEP2, AAQI);
1108// If an inbounds GEP would have to start from an out of bounds address 1109// for the two to alias, then we can assume noalias. 1110// TODO: Remove !isScalable() once BasicAA fully support scalable location 1113if (DecompGEP1.NWFlags.isInBounds() && DecompGEP1.VarIndices.empty() &&
1115 DecompGEP1.Offset.sge(V2Size.
getValue()) &&
1119// Symmetric case to above. 1120if (DecompGEP2.NWFlags.isInBounds() && DecompGEP1.VarIndices.empty() &&
1122 DecompGEP1.Offset.sle(-V1Size.
getValue()) &&
1126// For GEPs with identical offsets, we can preserve the size and AAInfo 1127// when performing the alias check on the underlying objects. 1128if (DecompGEP1.Offset == 0 && DecompGEP1.VarIndices.empty())
1132// Do the base pointers alias? 1137// If we get a No or May, then return it immediately, no amount of analysis 1138// will improve this situation. 1145// If there is a constant difference between the pointers, but the difference 1146// is less than the size of the associated memory object, then we know 1147// that the objects are partially overlapping. If the difference is 1148// greater, we know they do not overlap. 1149if (DecompGEP1.VarIndices.empty()) {
1152// Initialize for Off >= 0 (V2 <= GEP1) case. 1155constbool Swapped =
Off.isNegative();
1158// Swap if we have the situation where: 1161// ---------------->| 1162// |-->V1Size |-------> V2Size 1173if (
Off.ult(LSize)) {
1174// Conservatively drop processing if a phi was visited and/or offset is 1178Off.ule(INT32_MAX) && (Off + VRightSize.
getValue()).ule(LSize)) {
1179// Memory referenced by right pointer is nested. Save the offset in 1180// cache. Note that originally offset estimated as GEP1-V2, but 1181// AliasResult contains the shift that represents GEP1+Offset=V2. 1189// We can use the getVScaleRange to prove that Off >= (CR.upper * LSize). 1194if (!Overflow &&
Off.uge(UpperRange))
1199// VScale Alias Analysis - Given one scalable offset between accesses and a 1200// scalable typesize, we can divide each side by vscale, treating both values 1201// as a constant. We prove that Offset/vscale >= TypeSize/vscale. 1202if (DecompGEP1.VarIndices.size() == 1 &&
1203 DecompGEP1.VarIndices[0].Val.TruncBits == 0 &&
1204 DecompGEP1.Offset.isZero() &&
1207const VariableGEPIndex &ScalableVar = DecompGEP1.VarIndices[0];
1209 ScalableVar.IsNegated ? -ScalableVar.Scale : ScalableVar.Scale;
1212// Check if the offset is known to not overflow, if it does then attempt to 1213// prove it with the known values of vscale_range. 1214bool Overflows = !DecompGEP1.VarIndices[0].IsNSW;
1221// Note that we do not check that the typesize is scalable, as vscale >= 1 1222// so noalias still holds so long as the dependency distance is at least 1223// as big as the typesize. 1230// If the difference between pointers is Offset +<nuw> Indices then we know 1231// that the addition does not wrap the pointer index type (add nuw) and the 1232// constant Offset is a lower bound on the distance between the pointers. We 1233// can then prove NoAlias via Offset u>= VLeftSize. 1235// | BaseOffset | +<nuw> Indices | 1236// ---------------->|-------------------->| 1237// |-->V2Size | |-------> V1Size 1239if (!DecompGEP1.VarIndices.empty() &&
1240 DecompGEP1.NWFlags.hasNoUnsignedWrap() && V2Size.
hasValue() &&
1244// Bail on analysing scalable LocationSize 1248// We need to know both acess sizes for all the following heuristics. 1254for (
unsigned i = 0, e = DecompGEP1.VarIndices.size(); i != e; ++i) {
1255const VariableGEPIndex &
Index = DecompGEP1.VarIndices[i];
1257APInt ScaleForGCD = Scale;
1263 GCD = ScaleForGCD.
abs();
1268true, &AC,
Index.CxtI);
1277"Bit widths are normalized to MaxIndexSize");
1284 OffsetRange = OffsetRange.
sub(CR);
1286 OffsetRange = OffsetRange.
add(CR);
1289// We now have accesses at two offsets from the same base: 1290// 1. (...)*GCD + DecompGEP1.Offset with size V1Size 1291// 2. 0 with size V2Size 1292// Using arithmetic modulo GCD, the accesses are at 1293// [ModOffset..ModOffset+V1Size) and [0..V2Size). If the first access fits 1294// into the range [V2Size..GCD), then we know they cannot overlap. 1295APInt ModOffset = DecompGEP1.Offset.
srem(GCD);
1297 ModOffset += GCD;
// We want mod, not rem. 1299 (GCD - ModOffset).uge(V1Size.
getValue()))
1302// Compute ranges of potentially accessed bytes for both accesses. If the 1303// interseciton is empty, there can be no overlap. 1312// Try to determine the range of values for VarIndex such that 1313// VarIndex <= -MinAbsVarIndex || MinAbsVarIndex <= VarIndex. 1314 std::optional<APInt> MinAbsVarIndex;
1315if (DecompGEP1.VarIndices.size() == 1) {
1316// VarIndex = Scale*V. 1317const VariableGEPIndex &Var = DecompGEP1.VarIndices[0];
1318if (Var.Val.TruncBits == 0 &&
1320// Check if abs(V*Scale) >= abs(Scale) holds in the presence of 1321// potentially wrapping math. 1322auto MultiplyByScaleNoWrap = [](
const VariableGEPIndex &Var) {
1326int ValOrigBW = Var.Val.V->getType()->getPrimitiveSizeInBits();
1327// If Scale is small enough so that abs(V*Scale) >= abs(Scale) holds. 1328// The max value of abs(V) is 2^ValOrigBW - 1. Multiplying with a 1329// constant smaller than 2^(bitwidth(Val) - ValOrigBW) won't wrap. 1330int MaxScaleValueBW = Var.Val.getBitWidth() - ValOrigBW;
1331if (MaxScaleValueBW <= 0)
1333return Var.Scale.ule(
1336// Refine MinAbsVarIndex, if abs(Scale*V) >= abs(Scale) holds in the 1337// presence of potentially wrapping math. 1338if (MultiplyByScaleNoWrap(Var)) {
1339// If V != 0 then abs(VarIndex) >= abs(Scale). 1340 MinAbsVarIndex = Var.Scale.abs();
1343 }
elseif (DecompGEP1.VarIndices.size() == 2) {
1344// VarIndex = Scale*V0 + (-Scale)*V1. 1345// If V0 != V1 then abs(VarIndex) >= abs(Scale). 1346// Check that MayBeCrossIteration is false, to avoid reasoning about 1347// inequality of values across loop iterations. 1348const VariableGEPIndex &Var0 = DecompGEP1.VarIndices[0];
1349const VariableGEPIndex &Var1 = DecompGEP1.VarIndices[1];
1350if (Var0.hasNegatedScaleOf(Var1) && Var0.Val.TruncBits == 0 &&
1354 MinAbsVarIndex = Var0.Scale.abs();
1357if (MinAbsVarIndex) {
1358// The constant offset will have added at least +/-MinAbsVarIndex to it. 1359APInt OffsetLo = DecompGEP1.Offset - *MinAbsVarIndex;
1360APInt OffsetHi = DecompGEP1.Offset + *MinAbsVarIndex;
1361// We know that Offset <= OffsetLo || Offset >= OffsetHi 1367if (constantOffsetHeuristic(DecompGEP1, V1Size, V2Size, &AC, DT, AAQI))
1370// Statically, we can see that the base objects are the same, but the 1371// pointers have dynamic offsets which we can't resolve. And none of our 1372// little tricks above worked. 1377// If the results agree, take it. 1380// A mix of PartialAlias and MustAlias is PartialAlias. 1384// Otherwise, we don't know anything. 1388/// Provides a bunch of ad-hoc rules to disambiguate a Select instruction 1394// If the values are Selects with the same condition, we can do a more precise 1395// check: just check for aliases between the values on corresponding arms. 1396if (
constSelectInst *SI2 = dyn_cast<SelectInst>(V2))
1397if (isValueEqualInPotentialCycles(
SI->getCondition(), SI2->getCondition(),
1410// If both arms of the Select node NoAlias or MustAlias V2, then returns 1411// NoAlias / MustAlias. Otherwise, returns MayAlias. 1423/// Provide a bunch of ad-hoc rules to disambiguate a PHI instruction against 1430// If the values are PHIs in the same block, we can do a more precise 1431// as well as efficient check: just check for aliases between the values 1432// on corresponding edges. Don't do this if we are analyzing across 1433// iterations, as we may pick a different phi entry in different iterations. 1434if (
constPHINode *PN2 = dyn_cast<PHINode>(V2))
1436 std::optional<AliasResult> Alias;
1454// If a phi operand recurses back to the phi, we can still determine NoAlias 1455// if we don't alias the underlying objects of the other phi operands, as we 1456// know that the recursive phi needs to be based on them in some way. 1457bool isRecursive =
false;
1458auto CheckForRecPhi = [&](
Value *PV) {
1469Value *OnePhi =
nullptr;
1471// Skip the phi itself being the incoming value. 1475if (isa<PHINode>(PV1)) {
1476if (OnePhi && OnePhi != PV1) {
1477// To control potential compile time explosion, we choose to be 1478// conserviate when we have more than one Phi input. It is important 1479// that we handle the single phi case as that lets us handle LCSSA 1480// phi nodes and (combined with the recursive phi handling) simple 1481// pointer induction variable patterns. 1487if (CheckForRecPhi(PV1))
1490if (UniqueSrc.
insert(PV1).second)
1494if (OnePhi && UniqueSrc.
size() > 1)
1495// Out of an abundance of caution, allow only the trivial lcssa and 1496// recursive phi cases. 1499// If V1Srcs is empty then that means that the phi has no underlying non-phi 1500// value. This should only be possible in blocks unreachable from the entry 1501// block, but return MayAlias just in case. 1505// If this PHI node is recursive, indicate that the pointer may be moved 1506// across iterations. We can only prove NoAlias if different underlying 1507// objects are involved. 1511// In the recursive alias queries below, we may compare values from two 1512// different loop iterations. 1518// Early exit if the check of the first PHI source against V2 is MayAlias. 1519// Other results are not possible. 1522// With recursive phis we cannot guarantee that MustAlias/PartialAlias will 1523// remain valid to all elements and needs to conservatively return MayAlias. 1527// If all sources of the PHI node NoAlias or MustAlias V2, then returns 1528// NoAlias / MustAlias. Otherwise, returns MayAlias. 1529for (
unsigned i = 1, e = V1Srcs.
size(); i != e; ++i) {
1542/// Provides a bunch of ad-hoc rules to disambiguate in common cases, such as 1543/// array references. 1548// If either of the memory references is empty, it doesn't matter what the 1549// pointer values are. 1553// Strip off any casts if they exist. 1555V2 =
V2->stripPointerCastsForAliasAnalysis();
1557// If V1 or V2 is undef, the result is NoAlias because we can always pick a 1558// value for undef that aliases nothing in the program. 1559if (isa<UndefValue>(V1) || isa<UndefValue>(V2))
1562// Are we checking for alias of the same value? 1563// Because we look 'through' phi nodes, we could look at "Value" pointers from 1564// different iterations. We must therefore make sure that this is not the 1565// case. The function isValueEqualInPotentialCycles ensures that this cannot 1566// happen by looking at the visited phi nodes and making sure they cannot 1568if (isValueEqualInPotentialCycles(V1, V2, AAQI))
1574// Figure out what objects these things are pointing to if we can. 1578// Null values in the default address space don't point to any object, so they 1579// don't alias any other pointer. 1588// If V1/V2 point to two different objects, we know that we have no alias. 1592// Function arguments can't alias with things that are known to be 1593// unambigously identified at the function level. 1598// If one pointer is the result of a call/invoke or load and the other is a 1599// non-escaping local object within the same function, then we know the 1600// object couldn't escape to a point where the call could return it. 1602// Note that if the pointers are in different functions, there are a 1603// variety of complications. A call with a nocapture argument may still 1604// temporary store the nocapture argument's value in a temporary memory 1605// location if that memory location doesn't escape. Or it may pass a 1606// nocapture value to other functions as long as they don't capture it. 1608 O2, dyn_cast<Instruction>(O1),
/*OrAt*/true))
1611 O1, dyn_cast<Instruction>(O2),
/*OrAt*/true))
1615// If the size of one access is larger than the entire object on the other 1616// side, then we know such behavior is undefined and can assume no alias. 1620 TLI, NullIsValidLocation)) ||
1623 TLI, NullIsValidLocation)))
1637// This is often a no-op; instcombine rewrites this for us. No-op 1638// getUnderlyingObject calls are fast, though. 1643auto ValidAssumeForPtrContext = [&](
constValue *
Ptr) {
1646/* AllowEphemerals */true);
1648if (
constArgument *PtrA = dyn_cast<Argument>(
Ptr)) {
1650 &*PtrA->
getParent()->getEntryBlock().begin();
1652/* AllowEphemerals */true);
1657if ((O1 == HintO1 && O2 == HintO2) || (O1 == HintO2 && O2 == HintO1)) {
1658// Note that we go back to V1 and V2 for the 1659// ValidAssumeForPtrContext checks; they're dominated by O1 and O2, 1660// so strictly more assumptions are valid for them. 1662/* AllowEphemerals */true)) ||
1663 ValidAssumeForPtrContext(V1) || ValidAssumeForPtrContext(V2)) {
1671// If one the accesses may be before the accessed pointer, canonicalize this 1672// by using unknown after-pointer sizes for both accesses. This is 1673// equivalent, because regardless of which pointer is lower, one of them 1674// will always came after the other, as long as the underlying objects aren't 1675// disjoint. We do this so that the rest of BasicAA does not have to deal 1676// with accesses before the base pointer, and to improve cache utilization by 1677// merging equivalent states. 1683// FIXME: If this depth limit is hit, then we may cache sub-optimal results 1684// for recursive queries. For this reason, this limit is chosen to be large 1685// enough to be very rarely hit, while still being small enough to avoid 1687if (AAQI.
Depth >= 512)
1690// Check the cache before climbing up use-def chains. This also terminates 1691// otherwise infinitely recursive queries. Include MayBeCrossIteration in the 1692// cache key, because some cases where MayBeCrossIteration==false returns 1693// MustAlias or NoAlias may become MayAlias under MayBeCrossIteration==true. 1696constbool Swapped = V1 >
V2;
1702auto &
Entry = Pair.first->second;
1703if (!
Entry.isDefinitive()) {
1704// Remember that we used an assumption. This may either be a direct use 1705// of an assumption, or a use of an entry that may itself be based on an 1708if (
Entry.isAssumption())
1709 ++
Entry.NumAssumptionUses;
1711// Cache contains sorted {V1,V2} pairs but we should return original order. 1720 aliasCheckRecursive(V1, V1Size, V2, V2Size, AAQI, O1, O2);
1724auto &
Entry = It->second;
1726// Check whether a NoAlias assumption has been used, but disproven. 1727bool AssumptionDisproven =
1729if (AssumptionDisproven)
1732// This is a definitive result now, when considered as a root query. 1735// Cache contains sorted {V1,V2} pairs. 1736Entry.Result.swap(Swapped);
1738// If the assumption has been disproven, remove any results that may have 1739// been based on this assumption. Do this after the Entry updates above to 1740// avoid iterator invalidation. 1741if (AssumptionDisproven)
1745// The result may still be based on assumptions higher up in the chain. 1746// Remember it, so it can be purged from the cache later. 1755// Depth is incremented before this function is called, so Depth==1 indicates 1757if (AAQI.
Depth == 1) {
1758// Any remaining assumption based results must be based on proven 1759// assumptions, so convert them to definitive results. 1775if (
constGEPOperator *GV1 = dyn_cast<GEPOperator>(V1)) {
1779 }
elseif (
constGEPOperator *GV2 = dyn_cast<GEPOperator>(V2)) {
1786if (
constPHINode *PN = dyn_cast<PHINode>(V1)) {
1790 }
elseif (
constPHINode *PN = dyn_cast<PHINode>(V2)) {
1801 }
elseif (
constSelectInst *S2 = dyn_cast<SelectInst>(V2)) {
1808// If both pointers are pointing into the same object and one of them 1809// accesses the entire object, then the accesses must overlap in some way. 1821/// Check whether two Values can be considered equivalent. 1823/// If the values may come from different cycle iterations, this will also 1824/// check that the values are not part of cycle. We have to do this because we 1825/// are looking through phi nodes, that is we say 1826/// noalias(V, phi(VA, VB)) if noalias(V, VA) and noalias(V, VB). 1827bool BasicAAResult::isValueEqualInPotentialCycles(
constValue *V,
1836// Non-instructions and instructions in the entry block cannot be part of 1839if (!Inst || Inst->
getParent()->isEntryBlock())
1845/// Computes the symbolic difference between two de-composed GEPs. 1846void BasicAAResult::subtractDecomposedGEPs(DecomposedGEP &DestGEP,
1847const DecomposedGEP &SrcGEP,
1849// Drop nuw flag from GEP if subtraction of constant offsets overflows in an 1851if (DestGEP.Offset.ult(SrcGEP.Offset))
1852 DestGEP.NWFlags = DestGEP.NWFlags.withoutNoUnsignedWrap();
1854 DestGEP.Offset -= SrcGEP.Offset;
1855for (
const VariableGEPIndex &Src : SrcGEP.VarIndices) {
1856// Find V in Dest. This is N^2, but pointer indices almost never have more 1857// than a few variable indexes. 1860 VariableGEPIndex &Dest =
I.value();
1861if ((!isValueEqualInPotentialCycles(Dest.Val.V, Src.Val.V, AAQI) &&
1863 !Dest.Val.hasSameCastsAs(Src.Val))
1866// Normalize IsNegated if we're going to lose the NSW flag anyway. 1867if (Dest.IsNegated) {
1868 Dest.Scale = -Dest.Scale;
1869 Dest.IsNegated =
false;
1873// If we found it, subtract off Scale V's from the entry in Dest. If it 1874// goes to zero, remove the entry. 1875if (Dest.Scale != Src.Scale) {
1876// Drop nuw flag from GEP if subtraction of V's Scale overflows in an 1878if (Dest.Scale.ult(Src.Scale))
1879 DestGEP.NWFlags = DestGEP.NWFlags.withoutNoUnsignedWrap();
1881 Dest.Scale -= Src.Scale;
1884 DestGEP.VarIndices.erase(DestGEP.VarIndices.begin() +
I.index());
1890// If we didn't consume this entry, add it to the end of the Dest list. 1892 VariableGEPIndex
Entry = {Src.Val, Src.Scale, Src.CxtI, Src.IsNSW,
1893/* IsNegated */true};
1894 DestGEP.VarIndices.push_back(Entry);
1896// Drop nuw flag when we have unconsumed variable indices from SrcGEP. 1897 DestGEP.NWFlags = DestGEP.NWFlags.withoutNoUnsignedWrap();
1902bool BasicAAResult::constantOffsetHeuristic(
const DecomposedGEP &
GEP,
1908if (
GEP.VarIndices.size() != 2 || !MaybeV1Size.
hasValue() ||
1915const VariableGEPIndex &Var0 =
GEP.VarIndices[0], &Var1 =
GEP.VarIndices[1];
1917if (Var0.Val.TruncBits != 0 || !Var0.Val.hasSameCastsAs(Var1.Val) ||
1918 !Var0.hasNegatedScaleOf(Var1) ||
1919 Var0.Val.V->getType() != Var1.Val.V->getType())
1922// We'll strip off the Extensions of Var0 and Var1 and do another round 1923// of GetLinearExpression decomposition. In the example above, if Var0 1924// is zext(%x + 1) we should get V1 == %x and V1Offset == 1. 1926 LinearExpression E0 =
1928 LinearExpression E1 =
1930if (E0.Scale != E1.Scale || !E0.Val.hasSameCastsAs(E1.Val) ||
1931 !isValueEqualInPotentialCycles(E0.Val.V, E1.Val.V, AAQI))
1934// We have a hit - Var0 and Var1 only differ by a constant offset! 1936// If we've been sext'ed then zext'd the maximum difference between Var0 and 1937// Var1 is possible to calculate, but we're just interested in the absolute 1938// minimum difference between the two. The minimum distance may occur due to 1939// wrapping; consider "add i3 %i, 5": if %i == 7 then 7 + 5 mod 8 == 4, and so 1940// the minimum distance between %i and %i + 5 is 3. 1941APInt MinDiff = E0.Offset - E1.Offset, Wrapped = -MinDiff;
1944 MinDiff.
zextOrTrunc(Var0.Scale.getBitWidth()) * Var0.Scale.
abs();
1946// We can't definitely say whether GEP1 is before or after V2 due to wrapping 1947// arithmetic (i.e. for some values of GEP1 and V2 GEP1 < V2, and for other 1948// values GEP1 > V2). We'll therefore only declare NoAlias if both V1Size and 1949// V2Size can fit in the MinDiffBytes gap. 1950return MinDiffBytes.
uge(V1Size +
GEP.Offset.abs()) &&
1951 MinDiffBytes.
uge(V2Size +
GEP.Offset.abs());
1954//===----------------------------------------------------------------------===// 1955// BasicAliasAnalysis Pass 1956//===----------------------------------------------------------------------===// 1973void BasicAAWrapperPass::anchor() {}
1976"Basic Alias Analysis (stateless AA impl)",
true,
true)
1988auto &ACT = getAnalysis<AssumptionCacheTracker>();
1989auto &TLIWP = getAnalysis<TargetLibraryInfoWrapperPass>();
1990auto &DTWP = getAnalysis<DominatorTreeWrapperPass>();
1993 TLIWP.getTLI(
F), ACT.getAssumptionCache(
F),
1994 &DTWP.getDomTree()));
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
This file contains the simple types necessary to represent the attributes associated with functions a...
static cl::opt< bool > EnableRecPhiAnalysis("basic-aa-recphi", cl::Hidden, cl::init(true))
Enable analysis of recursive PHI nodes.
static const Function * getParent(const Value *V)
static bool isObjectSmallerThan(const Value *V, TypeSize Size, const DataLayout &DL, const TargetLibraryInfo &TLI, bool NullIsValidLoc)
Returns true if we can prove that the object specified by V is smaller than Size.
static bool isObjectSize(const Value *V, TypeSize Size, const DataLayout &DL, const TargetLibraryInfo &TLI, bool NullIsValidLoc)
Returns true if we can prove that the object specified by V has size Size.
static cl::opt< bool > EnableSeparateStorageAnalysis("basic-aa-separate-storage", cl::Hidden, cl::init(true))
static bool notDifferentParent(const Value *O1, const Value *O2)
static LinearExpression GetLinearExpression(const CastedValue &Val, const DataLayout &DL, unsigned Depth, AssumptionCache *AC, DominatorTree *DT)
Analyzes the specified value as a linear expression: "A*V + B", where A and B are constant integers.
static bool isNotInCycle(const Instruction *I, const DominatorTree *DT, const LoopInfo *LI)
static bool areBothVScale(const Value *V1, const Value *V2)
Return true if both V1 and V2 are VScale.
static TypeSize getMinimalExtentFrom(const Value &V, const LocationSize &LocSize, const DataLayout &DL, bool NullIsValidLoc)
Return the minimal extent from V to the end of the underlying object, assuming the result is used in ...
static AliasResult MergeAliasResults(AliasResult A, AliasResult B)
static bool isIntrinsicCall(const CallBase *Call, Intrinsic::ID IID)
static const unsigned MaxLookupSearchDepth
This is the interface for LLVM's primary stateless and local alias analysis.
block Block Frequency Analysis
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
std::optional< std::vector< StOtherPiece > > Other
This file provides utility analysis objects describing memory locations.
uint64_t IntrinsicInst * II
#define INITIALIZE_PASS_DEPENDENCY(depName)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
place backedge safepoints impl
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file provides utility classes that use RAII to save and restore values.
This file defines the make_scope_exit function, which executes user-defined cleanup logic at scope ex...
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type.
This class stores info we want to provide to or retain within an alias query.
SmallVector< AAQueryInfo::LocPair, 4 > AssumptionBasedResults
Location pairs for which an assumption based result is currently stored.
unsigned Depth
Query depth used to distinguish recursive queries.
int NumAssumptionUses
How many active NoAlias assumption uses there are.
std::pair< AACacheLoc, AACacheLoc > LocPair
bool MayBeCrossIteration
Tracks whether the accesses may be on different cycle iterations.
AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB)
The main low level interface to the alias analysis implementation.
MemoryEffects getMemoryEffects(const CallBase *Call)
Return the behavior of the given call site.
Class for arbitrary precision integers.
APInt umul_ov(const APInt &RHS, bool &Overflow) const
APInt zext(unsigned width) const
Zero extend to a new width.
APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
APInt abs() const
Get the absolute value.
unsigned getBitWidth() const
Return the number of bits in the APInt.
bool isNegative() const
Determine sign of this APInt.
unsigned countr_zero() const
Count the number of trailing zero bits.
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
APInt srem(const APInt &RHS) const
Function for signed remainder operation.
APInt smul_ov(const APInt &RHS, bool &Overflow) const
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
The possible results of an alias query.
void swap(bool DoSwap=true)
Helper for processing AliasResult for swapped memory location pairs.
@ MayAlias
The two locations may or may not alias.
@ NoAlias
The two locations do not alias at all.
@ PartialAlias
The two locations alias, but only due to a partial overlap.
@ MustAlias
The two locations precisely alias each other.
void setOffset(int32_t NewOffset)
API to communicate dependencies between analyses during invalidation.
bool invalidate(IRUnitT &IR, const PreservedAnalyses &PA)
Trigger the invalidation of some other analysis pass if not already handled and return whether it was...
A container for analyses that lazily runs them and caches their results.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
void setPreservesAll()
Set by analyses that do not transform their input at all.
AnalysisUsage & addRequiredTransitive()
This class represents an incoming formal argument to a Function.
This represents the llvm.assume intrinsic.
A function analysis which provides an AssumptionCache.
An immutable pass that tracks lazily created AssumptionCache objects.
A cache of @llvm.assume calls within a function.
MutableArrayRef< ResultElem > assumptionsFor(const Value *V)
Access the list of assumptions which affect this value.
This is the AA result object for the basic, local, and stateless alias analysis.
ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc, AAQueryInfo &AAQI)
Checks to see if the specified callsite can clobber the specified memory object.
ModRefInfo getArgModRefInfo(const CallBase *Call, unsigned ArgIdx)
Get the location associated with a pointer argument of a callsite.
MemoryEffects getMemoryEffects(const CallBase *Call, AAQueryInfo &AAQI)
Returns the behavior when calling the given call site.
ModRefInfo getModRefInfoMask(const MemoryLocation &Loc, AAQueryInfo &AAQI, bool IgnoreLocals=false)
Returns a bitmask that should be unconditionally applied to the ModRef info of a memory location.
bool invalidate(Function &Fn, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &Inv)
Handle invalidation events in the new pass manager.
AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB, AAQueryInfo &AAQI, const Instruction *CtxI)
Legacy wrapper pass to provide the BasicAAResult object.
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
BasicAAResult run(Function &F, FunctionAnalysisManager &AM)
LLVM Basic Block Representation.
const Function * getParent() const
Return the enclosing method, or null if none.
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
This class represents a function call, abstracting a target machine's calling convention.
This is the shared class of boolean and integer constants.
A constant pointer value that points to null.
This class represents a range of values.
ConstantRange add(const ConstantRange &Other) const
Return a new range representing the possible values resulting from an addition of a value in this ran...
static ConstantRange fromKnownBits(const KnownBits &Known, bool IsSigned)
Initialize a range based on a known bits constraint.
ConstantRange smul_fast(const ConstantRange &Other) const
Return range of possible values for a signed multiplication of this and Other.
bool isEmptySet() const
Return true if this set contains no members.
ConstantRange smul_sat(const ConstantRange &Other) const
Perform a signed saturating multiplication of two constant ranges.
APInt getUnsignedMax() const
Return the largest unsigned value contained in the ConstantRange.
ConstantRange intersectWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the intersection of this range with another range.
APInt getSignedMax() const
Return the largest signed value contained in the ConstantRange.
uint32_t getBitWidth() const
Get the bit width of this ConstantRange.
ConstantRange sub(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a subtraction of a value in this r...
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
iterator find(const_arg_type_t< KeyT > Val)
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
bool erase(const KeyT &Val)
Analysis pass which computes a DominatorTree.
Legacy analysis pass which computes a DominatorTree.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
void removeInstruction(Instruction *I)
bool isNotCapturedBefore(const Value *Object, const Instruction *I, bool OrAt) override
Check whether Object is not captured before instruction I.
FunctionPass class - This class is used to implement most global optimizations.
Represents flags for the getelementptr instruction/expression.
static GEPNoWrapFlags all()
bool hasNoUnsignedWrap() const
bool hasNoUnsignedSignedWrap() const
Type * getSourceElementType() const
bool hasNoUnsignedWrap() const
GEPNoWrapFlags getNoWrapFlags() const
Module * getParent()
Get the module that this global value is contained inside of...
A wrapper class for inspecting calls to intrinsic functions.
bool mayBeBeforePointer() const
Whether accesses before the base pointer are possible.
static constexpr LocationSize beforeOrAfterPointer()
Any location before or after the base pointer (but still within the underlying object).
TypeSize getValue() const
static constexpr LocationSize afterPointer()
Any location after the base pointer (but still within the underlying object).
static MemoryEffectsBase readOnly()
Create MemoryEffectsBase that can read any memory.
static MemoryEffectsBase inaccessibleMemOnly(ModRefInfo MR=ModRefInfo::ModRef)
Create MemoryEffectsBase that can only access inaccessible memory.
static MemoryEffectsBase writeOnly()
Create MemoryEffectsBase that can write any memory.
Representation for a specific memory location.
LocationSize Size
The maximum size of the location, in address-units, or UnknownSize if the size is not known.
static MemoryLocation getBeforeOrAfter(const Value *Ptr, const AAMDNodes &AATags=AAMDNodes())
Return a location that may access any location before or after Ptr, while remaining within the underl...
const Value * Ptr
The address of the start of the location.
This is a utility class that provides an abstraction for the common functionality between Instruction...
op_range incoming_values()
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
A set of analyses that are preserved following a run of a transformation pass.
This class represents the LLVM 'select' instruction.
bool isNotCapturedBefore(const Value *Object, const Instruction *I, bool OrAt) override
Check whether Object is not captured before instruction I.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Class to represent struct types.
Analysis pass providing the TargetLibraryInfo.
Provides information about what library functions are available for the current target.
static constexpr TypeSize getFixed(ScalarTy ExactSize)
bool isPointerTy() const
True if this is an instance of PointerType.
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
A Use represents the edge between a Value definition and its users.
Value * getOperand(unsigned i) const
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
StringRef getName() const
Return a constant reference to the value's name.
const Value * stripPointerCastsForAliasAnalysis() const
Strip off pointer casts, all-zero GEPs, single-argument phi nodes and invariant group info.
constexpr ScalarTy getFixedValue() const
static constexpr bool isKnownLT(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
StructType * getStructTypeOrNull() const
TypeSize getSequentialElementStride(const DataLayout &DL) const
const ParentTy * getParent() const
This class implements an extremely fast bulk output stream that can only output to a stream.
const APInt & umin(const APInt &A, const APInt &B)
Determine the smaller of two APInts considered to be unsigned.
APInt GreatestCommonDivisor(APInt A, APInt B)
Compute GCD of two unsigned APInt values.
bool match(Val *V, const Pattern &P)
VScaleVal_match m_VScale()
initializer< Ty > init(const Ty &Val)
@ Assume
Do not drop type tests (default).
This is an optimization pass for GlobalISel generic memory operations.
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
bool isValidAssumeForContext(const Instruction *I, const Instruction *CxtI, const DominatorTree *DT=nullptr, bool AllowEphemerals=false)
Return true if it is valid to use the assumptions provided by an assume intrinsic,...
detail::scope_exit< std::decay_t< Callable > > make_scope_exit(Callable &&F)
const Value * getArgumentAliasingToReturnedPointer(const CallBase *Call, bool MustPreserveNullness)
This function returns call pointer argument that is considered the same by aliasing rules.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
bool isPotentiallyReachableFromMany(SmallVectorImpl< BasicBlock * > &Worklist, const BasicBlock *StopBB, const SmallPtrSetImpl< BasicBlock * > *ExclusionSet, const DominatorTree *DT=nullptr, const LoopInfo *LI=nullptr)
Determine whether there is at least one path from a block in 'Worklist' to 'StopBB' without passing t...
auto successors(const MachineBasicBlock *BB)
bool isBaseOfObject(const Value *V)
Return true if we know V to the base address of the corresponding memory object.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=6)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
bool isNonEscapingLocalObject(const Value *V, SmallDenseMap< const Value *, bool, 8 > *IsCapturedCache=nullptr)
Returns true if the pointer is to a function-local object that never escapes from the function.
ConstantRange computeConstantRange(const Value *V, bool ForSigned, bool UseInstrInfo=true, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Determine the possible constant range of an integer or vector of integer value.
bool getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout &DL, const TargetLibraryInfo *TLI, ObjectSizeOpts Opts={})
Compute the size of the object pointed by Ptr.
bool isModSet(const ModRefInfo MRI)
bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
ConstantRange getVScaleRange(const Function *F, unsigned BitWidth)
Determine the possible constant range of vscale with the given bit width, based on the vscale_range f...
FunctionPass * createBasicAAWrapperPass()
bool isMallocOrCallocLikeFn(const Value *V, const TargetLibraryInfo *TLI)
Tests if a value is a call or invoke to a library function that allocates memory similar to malloc or...
bool isKnownNonZero(const Value *V, const SimplifyQuery &Q, unsigned Depth=0)
Return true if the given value is known to be non-zero when defined.
ModRefInfo
Flags indicating whether a memory access modifies or references memory.
@ Ref
The access may reference the value stored in memory.
@ ModRef
The access may reference and may modify the value stored in memory.
@ Mod
The access may modify the value stored in memory.
@ NoModRef
The access neither references nor modifies the value stored in memory.
Instruction * FindEarliestCapture(const Value *V, Function &F, bool ReturnCaptures, bool StoreCaptures, const DominatorTree &DT, unsigned MaxUsesToExplore=0)
bool isKnownNonEqual(const Value *V1, const Value *V2, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Return true if the given values are known to be non-equal when defined.
void initializeBasicAAWrapperPassPass(PassRegistry &)
void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
bool isModAndRefSet(const ModRefInfo MRI)
bool isIdentifiedFunctionLocal(const Value *V)
Return true if V is umabigously identified at the function-level.
constexpr unsigned BitWidth
bool isEscapeSource(const Value *V)
Returns true if the pointer is one which would have been considered an escape by isNonEscapingLocalOb...
gep_type_iterator gep_type_begin(const User *GEP)
bool isIdentifiedObject(const Value *V)
Return true if this pointer refers to a distinct and identifiable object.
bool isPotentiallyReachable(const Instruction *From, const Instruction *To, const SmallPtrSetImpl< BasicBlock * > *ExclusionSet=nullptr, const DominatorTree *DT=nullptr, const LoopInfo *LI=nullptr)
Determine whether instruction 'To' is reachable from 'From', without passing through any blocks in Ex...
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
SmallVector< VariableGEPIndex, 4 > VarIndices
void print(raw_ostream &OS) const
static constexpr int Definitive
Cache entry is neither an assumption nor does it use a (non-definitive) assumption.
static constexpr int AssumptionBased
Cache entry is not an assumption itself, but may be using an assumption from higher up the stack.
A special type used by analysis passes to provide an address that identifies that particular analysis...
virtual ~CaptureAnalysis()=0
virtual bool isNotCapturedBefore(const Value *Object, const Instruction *I, bool OrAt)=0
Check whether Object is not captured before instruction I.
Various options to control the behavior of getObjectSize.
bool NullIsUnknownSize
If this is true, null pointers in address space 0 will be treated as though they can't be evaluated.
bool RoundToAlign
Whether to round the result up to the alignment of allocas, byval arguments, and global variables.
A lightweight accessor for an operand bundle meant to be passed around by value.
StringRef getTagName() const
Return the tag of this operand bundle as a string.
A utility class that uses RAII to save and restore the value of a variable.