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.
78 STATISTIC(SearchLimitReached,
"Number of times the limit to "
79 "decompose GEPs is reached");
80 STATISTIC(SearchTimes,
"Number of times a GEP is decomposed");
83 FunctionAnalysisManager::Invalidator &Inv) {
84 // We don't care if this analysis itself is preserved, it has no state. But
85 // we need to check that the analyses it depends on have been. Note that we
86 // may be created without handles to some analyses and in that case don't
92 // Otherwise this analysis result remains valid.
96//===----------------------------------------------------------------------===//
98//===----------------------------------------------------------------------===//
100/// Returns the size of the object specified by V or UnknownSize if unknown.
105 bool RoundToAlign =
false) {
110 // FIXME: Remove this check, only exists to preserve previous behavior.
111 if (
Size->isScalable())
118/// Returns true if we can prove that the object specified by V is smaller than
119/// Size. Bails out early unless the root object is passed as the first
124 bool NullIsValidLoc) {
125 // Note that the meanings of the "object" are slightly different in the
126 // following contexts:
127 // c1: llvm::getObjectSize()
128 // c2: llvm.objectsize() intrinsic
129 // c3: isObjectSmallerThan()
130 // c1 and c2 share the same meaning; however, the meaning of "object" in c3
131 // refers to the "entire object".
133 // Consider this example:
134 // char *p = (char*)malloc(100)
137 // In the context of c1 and c2, the "object" pointed by q refers to the
138 // stretch of memory of q[0:19]. So, getObjectSize(q) should return 20.
140 // In the context of c3, the "object" refers to the chunk of memory being
141 // allocated. So, the "object" has 100 bytes, and q points to the middle the
142 // "object". However, unless p, the root object, is passed as the first
143 // parameter, the call to isIdentifiedObject() makes isObjectSmallerThan()
148 // This function needs to use the aligned object size because we allow
149 // reads a bit past the end given sufficient alignment.
150 std::optional<TypeSize> ObjectSize =
getObjectSize(V,
DL, TLI, NullIsValidLoc,
151 /*RoundToAlign*/ true);
156/// Return the minimal extent from \p V to the end of the underlying object,
157/// assuming the result is used in an aliasing query. E.g., we do use the query
158/// location size and the fact that null pointers cannot alias here.
162 bool NullIsValidLoc) {
163 // If we have dereferenceability information we know a lower bound for the
164 // extent as accesses for a lower offset would be valid. We need to exclude
165 // the "or null" part if null is a valid pointer. We can ignore frees, as an
166 // access after free would be undefined behavior.
167 bool CanBeNull, CanBeFreed;
169 V.getPointerDereferenceableBytes(
DL, CanBeNull, CanBeFreed);
170 DerefBytes = (CanBeNull && NullIsValidLoc) ? 0 : DerefBytes;
171 // If queried with a precise location size, we assume that location size to be
172 // accessed, thus valid.
178/// Returns true if we can prove that the object specified by V has size Size.
181 std::optional<TypeSize> ObjectSize =
183 return ObjectSize && *ObjectSize ==
Size;
186/// Return true if both V1 and V2 are VScale
192//===----------------------------------------------------------------------===//
193// CaptureAnalysis implementations
194//===----------------------------------------------------------------------===//
204 auto [CacheIt, Inserted] =
207 return CacheIt->second;
212 CacheIt->second = Ret;
220 return Succs.
empty() ||
230 auto Iter = EarliestEscapes.try_emplace(Object);
232 std::pair<Instruction *, CaptureComponents> EarliestCapture =
234 /*ReturnCaptures=*/false, DT,
236 if (EarliestCapture.first)
237 Inst2Obj[EarliestCapture.first].push_back(Object);
238 Iter.first->second = EarliestCapture;
241 auto IsNotCapturedBefore = [&]() {
242 // No capturing instruction.
243 Instruction *CaptureInst = Iter.first->second.first;
247 // No context instruction means any use is capturing.
251 if (
I == CaptureInst) {
259 if (IsNotCapturedBefore())
261 return Iter.first->second.second;
265 auto Iter = Inst2Obj.find(
I);
266 if (Iter != Inst2Obj.end()) {
267 for (
const Value *Obj : Iter->second)
268 EarliestEscapes.erase(Obj);
273//===----------------------------------------------------------------------===//
274// GetElementPtr Instruction Decomposition and Analysis
275//===----------------------------------------------------------------------===//
278/// Represents zext(sext(trunc(V))).
281 unsigned ZExtBits = 0;
282 unsigned SExtBits = 0;
283 unsigned TruncBits = 0;
284 /// Whether trunc(V) is non-negative.
285 bool IsNonNegative =
false;
287 explicit CastedValue(
const Value *V) : V(V) {}
288 explicit CastedValue(
const Value *V,
unsigned ZExtBits,
unsigned SExtBits,
289 unsigned TruncBits,
bool IsNonNegative)
290 : V(V), ZExtBits(ZExtBits), SExtBits(SExtBits), TruncBits(TruncBits),
291 IsNonNegative(IsNonNegative) {}
294 return V->getType()->getPrimitiveSizeInBits() - TruncBits + ZExtBits +
298 CastedValue withValue(
const Value *NewV,
bool PreserveNonNeg)
const {
299 return CastedValue(NewV, ZExtBits, SExtBits, TruncBits,
300 IsNonNegative && PreserveNonNeg);
303 /// Replace V with zext(NewV)
304 CastedValue withZExtOfValue(
const Value *NewV,
bool ZExtNonNegative)
const {
305 unsigned ExtendBy =
V->getType()->getPrimitiveSizeInBits() -
307 if (ExtendBy <= TruncBits)
308 // zext<nneg>(trunc(zext(NewV))) == zext<nneg>(trunc(NewV))
309 // The nneg can be preserved on the outer zext here.
310 return CastedValue(NewV, ZExtBits, SExtBits, TruncBits - ExtendBy,
313 // zext(sext(zext(NewV))) == zext(zext(zext(NewV)))
314 ExtendBy -= TruncBits;
315 // zext<nneg>(zext(NewV)) == zext(NewV)
316 // zext(zext<nneg>(NewV)) == zext<nneg>(NewV)
317 // The nneg can be preserved from the inner zext here but must be dropped
319 return CastedValue(NewV, ZExtBits + SExtBits + ExtendBy, 0, 0,
323 /// Replace V with sext(NewV)
324 CastedValue withSExtOfValue(
const Value *NewV)
const {
325 unsigned ExtendBy =
V->getType()->getPrimitiveSizeInBits() -
327 if (ExtendBy <= TruncBits)
328 // zext<nneg>(trunc(sext(NewV))) == zext<nneg>(trunc(NewV))
329 // The nneg can be preserved on the outer zext here
330 return CastedValue(NewV, ZExtBits, SExtBits, TruncBits - ExtendBy,
333 // zext(sext(sext(NewV)))
334 ExtendBy -= TruncBits;
335 // zext<nneg>(sext(sext(NewV))) = zext<nneg>(sext(NewV))
336 // The nneg can be preserved on the outer zext here
337 return CastedValue(NewV, ZExtBits, SExtBits + ExtendBy, 0, IsNonNegative);
340 APInt evaluateWith(APInt
N)
const {
341 assert(
N.getBitWidth() ==
V->getType()->getPrimitiveSizeInBits() &&
342 "Incompatible bit width");
343 if (TruncBits)
N =
N.trunc(
N.getBitWidth() - TruncBits);
344 if (SExtBits)
N =
N.sext(
N.getBitWidth() + SExtBits);
345 if (ZExtBits)
N =
N.zext(
N.getBitWidth() + ZExtBits);
349 ConstantRange evaluateWith(ConstantRange
N)
const {
350 assert(
N.getBitWidth() ==
V->getType()->getPrimitiveSizeInBits() &&
351 "Incompatible bit width");
352 if (TruncBits)
N =
N.truncate(
N.getBitWidth() - TruncBits);
353 if (IsNonNegative && !
N.isAllNonNegative())
357 if (SExtBits)
N =
N.signExtend(
N.getBitWidth() + SExtBits);
358 if (ZExtBits)
N =
N.zeroExtend(
N.getBitWidth() + ZExtBits);
362 bool canDistributeOver(
bool NUW,
bool NSW)
const {
363 // zext(x op<nuw> y) == zext(x) op<nuw> zext(y)
364 // sext(x op<nsw> y) == sext(x) op<nsw> sext(y)
365 // trunc(x op y) == trunc(x) op trunc(y)
366 return (!ZExtBits || NUW) && (!SExtBits || NSW);
369 bool hasSameCastsAs(
const CastedValue &
Other)
const {
370 if (
V->getType() !=
Other.V->getType())
373 if (ZExtBits ==
Other.ZExtBits && SExtBits ==
Other.SExtBits &&
374 TruncBits ==
Other.TruncBits)
376 // If either CastedValue has a nneg zext then the sext/zext bits are
377 // interchangable for that value.
378 if (IsNonNegative ||
Other.IsNonNegative)
379 return (ZExtBits + SExtBits ==
Other.ZExtBits +
Other.SExtBits &&
380 TruncBits ==
Other.TruncBits);
385/// Represents zext(sext(trunc(V))) * Scale + Offset.
391 /// True if all operations in this expression are NUW.
393 /// True if all operations in this expression are NSW.
397 const APInt &
Offset,
bool IsNUW,
bool IsNSW)
401 : Val(Val), IsNUW(
true), IsNSW(
true) {
402 unsigned BitWidth = Val.getBitWidth();
408 // The check for zero offset is necessary, because generally
409 // (X +nsw Y) *nsw Z does not imply (X *nsw Z) +nsw (Y *nsw Z).
410 bool NSW = IsNSW && (
Other.isOne() || (MulIsNSW &&
Offset.isZero()));
411 bool NUW = IsNUW && (
Other.isOne() || MulIsNUW);
417/// Analyzes the specified value as a linear expression: "A*V + B", where A and
418/// B are constant integers.
422 // Limit our recursion depth.
428 Val.evaluateWith(Const->getValue()),
true,
true);
432 APInt RHS = Val.evaluateWith(RHSC->getValue());
433 // The only non-OBO case we deal with is or, and only limited to the
434 // case where it is both nuw and nsw.
435 bool NUW =
true, NSW =
true;
437 NUW &= BOp->hasNoUnsignedWrap();
438 NSW &= BOp->hasNoSignedWrap();
440 if (!Val.canDistributeOver(NUW, NSW))
443 // While we can distribute over trunc, we cannot preserve nowrap flags
449 switch (BOp->getOpcode()) {
451 // We don't understand this instruction, so we can't decompose it any
454 case Instruction::Or:
455 // X|C == X+C if it is disjoint. Otherwise we can't analyze it.
460 case Instruction::Add: {
468 case Instruction::Sub: {
472 E.IsNUW =
false;
// sub nuw x, y is not add nuw x, -y.
476 case Instruction::Mul:
481 case Instruction::Shl:
482 // We're trying to linearize an expression of the kind:
484 // where the shift count exceeds the bitwidth of the type.
485 // We can't decompose this further (the expression would return
487 if (
RHS.getLimitedValue() > Val.getBitWidth())
492 E.Offset <<=
RHS.getLimitedValue();
493 E.Scale <<=
RHS.getLimitedValue();
504 Val.withZExtOfValue(ZExt->getOperand(0), ZExt->hasNonNeg()),
DL,
516// A linear transformation of a Value; this class represents
517// ZExt(SExt(Trunc(V, TruncBits), SExtBits), ZExtBits) * Scale.
518struct VariableGEPIndex {
522 // Context instruction to use when querying information about this index.
525 /// True if all operations in this expression are NSW.
528 /// True if the index should be subtracted rather than added. We don't simply
529 /// negate the Scale, to avoid losing the NSW flag: X - INT_MIN*1 may be
530 /// non-wrapping, while X + INT_MIN*(-1) wraps.
533 bool hasNegatedScaleOf(
const VariableGEPIndex &
Other)
const {
534 if (IsNegated ==
Other.IsNegated)
535 return Scale == -
Other.Scale;
536 return Scale ==
Other.Scale;
543 void print(raw_ostream &OS)
const {
544 OS <<
"(V=" << Val.V->
getName()
545 <<
", zextbits=" << Val.ZExtBits
546 <<
", sextbits=" << Val.SExtBits
547 <<
", truncbits=" << Val.TruncBits
548 <<
", scale=" << Scale
550 <<
", negated=" << IsNegated <<
")";
555// Represents the internal structure of a GEP, decomposed into a base pointer,
556// constant offsets, and variable scaled indices.
558 // Base pointer of the GEP
560 // Total constant offset from base.
562 // Scaled variable (non-constant) indices.
564 // Nowrap flags common to all GEP operations involved in expression.
572 OS <<
", inbounds=" << (
NWFlags.isInBounds() ?
"1" :
"0")
573 <<
", nuw=" << (
NWFlags.hasNoUnsignedWrap() ?
"1" :
"0")
574 <<
"(DecomposedGEP Base=" <<
Base->getName() <<
", Offset=" <<
Offset
576 for (
size_t i = 0; i <
VarIndices.size(); i++) {
586/// If V is a symbolic pointer expression, decompose it into a base pointer
587/// with a constant offset and a number of scaled symbolic offsets.
589/// The scaled symbolic offsets (represented by pairs of a Value* and a scale
590/// in the VarIndices vector) are Value*'s that are known to be scaled by the
591/// specified amount, but which may have other unrepresented high bits. As
592/// such, the gep cannot necessarily be reconstructed from its decomposed form.
596 // Limit recursion depth to limit compile time in crazy cases.
601 unsigned IndexSize =
DL.getIndexTypeSizeInBits(V->getType());
602 DecomposedGEP Decomposed;
603 Decomposed.Offset =
APInt(IndexSize, 0);
605 // See if this is a bitcast or GEP.
608 // The only non-operator case we can handle are GlobalAliases.
610 if (!GA->isInterposable()) {
611 V = GA->getAliasee();
619 if (
Op->getOpcode() == Instruction::BitCast ||
620 Op->getOpcode() == Instruction::AddrSpaceCast) {
621 Value *NewV =
Op->getOperand(0);
622 // Don't look through casts between address spaces with differing index
624 if (DL.getIndexTypeSizeInBits(NewV->
getType()) != IndexSize) {
635 // Look through single-arg phi nodes created by LCSSA.
636 if (
PHI->getNumIncomingValues() == 1) {
637 V =
PHI->getIncomingValue(0);
641 // CaptureTracking can know about special capturing properties of some
642 // intrinsics like launder.invariant.group, that can't be expressed with
643 // the attributes, but have properties like returning aliasing pointer.
644 // Because some analysis may assume that nocaptured pointer is not
645 // returned from some special intrinsic (because function would have to
646 // be marked with returns attribute), it is crucial to use this function
647 // because it should be in sync with CaptureTracking. Not using it may
648 // cause weird miscompilations where 2 aliasing pointers are assumed to
660 // Track the common nowrap flags for all GEPs we see.
665 // Walk the indices of the GEP, accumulating them into BaseOff/VarIndices.
668 I !=
E; ++
I, ++GTI) {
670 // Compute the (potentially symbolic) offset in bytes for this index.
672 // For a struct, add the member offset.
677 Decomposed.Offset += DL.getStructLayout(STy)->getElementOffset(FieldNo);
681 // For an array/pointer, add the element offset, explicitly scaled.
686 // Don't attempt to analyze GEPs if the scalable index is not zero.
694 CIdx->getValue().sextOrTrunc(IndexSize);
704 // If the integer type is smaller than the index size, it is implicitly
705 // sign extended or truncated to index size.
708 bool NonNeg = NUSW && NUW;
709 unsigned Width =
Index->getType()->getIntegerBitWidth();
710 unsigned SExtBits = IndexSize > Width ? IndexSize - Width : 0;
711 unsigned TruncBits = IndexSize < Width ? Width - IndexSize : 0;
713 CastedValue(Index, 0, SExtBits, TruncBits, NonNeg), DL, 0, AC, DT);
715 // Scale by the type size.
717 LE =
LE.mul(APInt(IndexSize, TypeSize), NUW, NUSW);
718 Decomposed.Offset +=
LE.Offset;
719 APInt Scale =
LE.Scale;
721 Decomposed.NWFlags = Decomposed.NWFlags.withoutNoUnsignedWrap();
723 // If we already had an occurrence of this index variable, merge this
724 // scale into it. For example, we want to handle:
725 // A[x][x] -> x*16 + x*4 -> x*20
726 // This also ensures that 'x' only appears in the index list once.
727 for (
unsigned i = 0, e = Decomposed.VarIndices.size(); i != e; ++i) {
728 if ((Decomposed.VarIndices[i].Val.V ==
LE.Val.V ||
730 Decomposed.VarIndices[i].Val.hasSameCastsAs(
LE.Val)) {
731 Scale += Decomposed.VarIndices[i].Scale;
732 // We cannot guarantee no-wrap for the merge.
733 LE.IsNSW =
LE.IsNUW =
false;
734 Decomposed.VarIndices.erase(Decomposed.VarIndices.begin() + i);
740 VariableGEPIndex
Entry = {
LE.Val, Scale, CxtI,
LE.IsNSW,
741 /* IsNegated */ false};
742 Decomposed.VarIndices.push_back(Entry);
746 // Analyze the base pointer next.
748 }
while (--MaxLookup);
750 // If the chain of expressions is too deep, just return early.
752 SearchLimitReached++;
759 assert(Visited.empty() &&
"Visited must be cleared after use!");
762 unsigned MaxLookup = 8;
769 if (!Visited.insert(V).second)
772 // Ignore allocas if we were instructed to do so.
776 // If the location points to memory that is known to be invariant for
777 // the life of the underlying SSA value, then we can exclude Mod from
778 // the set of valid memory effects.
780 // An argument that is marked readonly and noalias is known to be
781 // invariant while that function is executing.
783 if (Arg->hasNoAliasAttr() && Arg->onlyReadsMemory()) {
789 // A global constant can't be mutated.
791 // Note: this doesn't require GV to be "ODR" because it isn't legal for a
792 // global to be marked constant in some modules and non-constant in
793 // others. GV may even be a declaration, not a definition.
794 if (!GV->isConstant())
799 // If both select values point to local memory, then so does the select.
806 // If all values incoming to a phi node point to local memory, then so does
809 // Don't bother inspecting phi nodes with many operands.
810 if (PN->getNumIncomingValues() > MaxLookup)
816 // Otherwise be conservative.
818 }
while (!Worklist.
empty() && --MaxLookup);
820 // If we hit the maximum number of instructions to examine, be conservative.
821 if (!Worklist.
empty())
829 return II &&
II->getIntrinsicID() == IID;
832/// Returns the behavior when calling the given call site.
839 // Operand bundles on the call may also read or write memory, in addition
840 // to the behavior of the called function.
841 if (
Call->hasReadingOperandBundles())
843 if (
Call->hasClobberingOperandBundles())
845 if (
Call->isVolatile()) {
846 // Volatile operations also access inaccessible memory.
855/// Returns the behavior when calling the given function. For use when the call
856/// site is not known.
858 switch (F->getIntrinsicID()) {
859 case Intrinsic::experimental_guard:
860 case Intrinsic::experimental_deoptimize:
861 // These intrinsics can read arbitrary memory, and additionally modref
862 // inaccessible memory to model control dependence.
867 return F->getMemoryEffects();
872 if (
Call->doesNotAccessMemory(ArgIdx))
875 if (
Call->onlyWritesMemory(ArgIdx))
878 if (
Call->onlyReadsMemory(ArgIdx))
887 if (!inst->getParent())
889 return inst->getParent()->getParent();
903 return !F1 || !F2 || F1 == F2;
911 "BasicAliasAnalysis doesn't support interprocedural queries.");
912 return aliasCheck(LocA.
Ptr, LocA.
Size, LocB.
Ptr, LocB.
Size, AAQI, CtxI);
915/// Checks to see if the specified callsite can clobber the specified memory
918/// Since we only look at local properties of this function, we really can't
919/// say much about this query. We do, however, use simple "address taken"
920/// analysis on local objects.
925 "AliasAnalysis query involving multiple functions!");
929 // Calls marked 'tail' cannot read or write allocas from the current frame
930 // because the current frame might be destroyed by the time they run. However,
931 // a tail call may use an alloca with byval. Calling with byval copies the
932 // contents of the alloca into argument registers or stack slots, so there is
933 // no lifetime issue.
936 if (CI->isTailCall() &&
937 !CI->getAttributes().hasAttrSomewhere(Attribute::ByVal))
940 // Stack restore is able to modify unescaped dynamic allocas. Assume it may
941 // modify them even though the alloca is not escaped.
946 // We can completely ignore inaccessible memory here, because MemoryLocations
947 // can only reference accessible memory.
950 if (ME.doesNotAccessMemory())
957 // An identified function-local object that does not escape can only be
958 // accessed via call arguments. Reduce OtherMR (which includes accesses to
959 // escaped memory) based on that.
961 // We model calls that can return twice (setjmp) as clobbering non-escaping
962 // objects, to model any accesses that may occur prior to the second return.
963 // As an exception, ignore allocas, as setjmp is not required to preserve
964 // non-volatile stores for them.
975 // Refine the modref info for argument memory. We only bother to do this
976 // if ArgMR is not a subset of OtherMR, otherwise this won't have an impact
977 // on the final result.
978 if ((ArgMR | OtherMR) != OtherMR) {
980 for (
const Use &U :
Call->data_ops()) {
981 const Value *Arg = U;
984 unsigned ArgIdx =
Call->getDataOperandNo(&U);
986 Call->isArgOperand(&U)
993 // Exit early if we cannot improve over the original ArgMR.
994 if (NewArgMR == ArgMR)
1002 // Refine accesses to errno memory.
1003 if ((ErrnoMR | Result) != Result) {
1005 // Exclusion conditions do not hold, this memory location may alias errno.
1013 // If the call is malloc/calloc like, we can assume that it doesn't
1014 // modify any IR visible value. This is only valid because we assume these
1015 // routines do not read values visible in the IR. TODO: Consider special
1016 // casing realloc and strdup routines which access only their arguments as
1017 // well. Or alternatively, replace all of this with inaccessiblememonly once
1018 // that's implemented fully.
1020 // Be conservative if the accessed pointer may alias the allocation -
1021 // fallback to the generic handling below.
1027 // Like assumes, invariant.start intrinsics were also marked as arbitrarily
1028 // writing so that proper control dependencies are maintained but they never
1029 // mod any particular memory location visible to the IR.
1030 // *Unlike* assumes (which are now modeled as NoModRef), invariant.start
1031 // intrinsic is now modeled as reading memory. This prevents hoisting the
1032 // invariant.start intrinsic over stores. Consider:
1035 // invariant_start(ptr)
1039 // This cannot be transformed to:
1042 // invariant_start(ptr)
1047 // The transformation will cause the second store to be ignored (based on
1048 // rules of invariant.start) and print 40, while the first program always
1060 // Guard intrinsics are marked as arbitrarily writing so that proper control
1061 // dependencies are maintained but they never mods any particular memory
1064 // *Unlike* assumes, guard intrinsics are modeled as reading memory since the
1065 // heap state at the point the guard is issued needs to be consistent in case
1066 // the guard invokes the "deopt" continuation.
1068 // NB! This function is *not* commutative, so we special case two
1069 // possibilities for guard intrinsics.
1085/// Provides a bunch of ad-hoc rules to disambiguate a GEP instruction against
1088/// We know that V1 is a GEP, but we don't know anything about V2.
1089/// UnderlyingV1 is getUnderlyingObject(GEP1), UnderlyingV2 is the same for
1095 auto BaseObjectsAlias = [&]() {
1104 // TODO: This limitation exists for compile-time reasons. Relax it if we
1105 // can avoid exponential pathological cases.
1109 // If both accesses have unknown size, we can only check whether the base
1110 // objects don't alias.
1111 return BaseObjectsAlias();
1114 DominatorTree *DT = getDT(AAQI);
1115 DecomposedGEP DecompGEP1 = DecomposeGEPExpression(GEP1, DL, &AC, DT);
1116 DecomposedGEP DecompGEP2 = DecomposeGEPExpression(V2, DL, &AC, DT);
1118 // Bail if we were not able to decompose anything.
1119 if (DecompGEP1.Base == GEP1 && DecompGEP2.Base == V2)
1122 // Fall back to base objects if pointers have different index widths.
1123 if (DecompGEP1.Offset.getBitWidth() != DecompGEP2.Offset.getBitWidth())
1124 return BaseObjectsAlias();
1126 // Swap GEP1 and GEP2 if GEP2 has more variable indices.
1127 if (DecompGEP1.VarIndices.size() < DecompGEP2.VarIndices.size()) {
1133 // Subtract the GEP2 pointer from the GEP1 pointer to find out their
1134 // symbolic difference.
1135 subtractDecomposedGEPs(DecompGEP1, DecompGEP2, AAQI);
1137 // If an inbounds GEP would have to start from an out of bounds address
1138 // for the two to alias, then we can assume noalias.
1139 // TODO: Remove !isScalable() once BasicAA fully support scalable location
1142 if (DecompGEP1.NWFlags.isInBounds() && DecompGEP1.VarIndices.empty() &&
1144 DecompGEP1.Offset.sge(V2Size.
getValue()) &&
1148 // Symmetric case to above.
1149 if (DecompGEP2.NWFlags.isInBounds() && DecompGEP1.VarIndices.empty() &&
1151 DecompGEP1.Offset.sle(-V1Size.
getValue()) &&
1155 // For GEPs with identical offsets, we can preserve the size and AAInfo
1156 // when performing the alias check on the underlying objects.
1157 if (DecompGEP1.Offset == 0 && DecompGEP1.VarIndices.empty())
1158 return AAQI.
AAR.
alias(MemoryLocation(DecompGEP1.Base, V1Size),
1159 MemoryLocation(DecompGEP2.Base, V2Size), AAQI);
1161 // Do the base pointers alias?
1162 AliasResult BaseAlias =
1166 // If we get a No or May, then return it immediately, no amount of analysis
1167 // will improve this situation.
1174 // If there is a constant difference between the pointers, but the difference
1175 // is less than the size of the associated memory object, then we know
1176 // that the objects are partially overlapping. If the difference is
1177 // greater, we know they do not overlap.
1178 if (DecompGEP1.VarIndices.empty()) {
1179 APInt &
Off = DecompGEP1.Offset;
1181 // Initialize for Off >= 0 (V2 <= GEP1) case.
1182 LocationSize VLeftSize = V2Size;
1183 LocationSize VRightSize = V1Size;
1184 const bool Swapped =
Off.isNegative();
1187 // Swap if we have the situation where:
1190 // ---------------->|
1191 // |-->V1Size |-------> V2Size
1200 const TypeSize LSize = VLeftSize.
getValue();
1202 if (
Off.ult(LSize)) {
1203 // Conservatively drop processing if a phi was visited and/or offset is
1207 Off.ule(INT32_MAX) && (Off + VRightSize.
getValue()).ule(LSize)) {
1208 // Memory referenced by right pointer is nested. Save the offset in
1209 // cache. Note that originally offset estimated as GEP1-V2, but
1210 // AliasResult contains the shift that represents GEP1+Offset=V2.
1218 // We can use the getVScaleRange to prove that Off >= (CR.upper * LSize).
1223 if (!Overflow &&
Off.uge(UpperRange))
1228 // VScale Alias Analysis - Given one scalable offset between accesses and a
1229 // scalable typesize, we can divide each side by vscale, treating both values
1230 // as a constant. We prove that Offset/vscale >= TypeSize/vscale.
1231 if (DecompGEP1.VarIndices.size() == 1 &&
1232 DecompGEP1.VarIndices[0].Val.TruncBits == 0 &&
1233 DecompGEP1.Offset.isZero() &&
1236 const VariableGEPIndex &ScalableVar = DecompGEP1.VarIndices[0];
1238 ScalableVar.IsNegated ? -ScalableVar.Scale : ScalableVar.Scale;
1239 LocationSize VLeftSize = Scale.
isNegative() ? V1Size : V2Size;
1241 // Check if the offset is known to not overflow, if it does then attempt to
1242 // prove it with the known values of vscale_range.
1243 bool Overflows = !DecompGEP1.VarIndices[0].IsNSW;
1250 // Note that we do not check that the typesize is scalable, as vscale >= 1
1251 // so noalias still holds so long as the dependency distance is at least
1252 // as big as the typesize.
1259 // If the difference between pointers is Offset +<nuw> Indices then we know
1260 // that the addition does not wrap the pointer index type (add nuw) and the
1261 // constant Offset is a lower bound on the distance between the pointers. We
1262 // can then prove NoAlias via Offset u>= VLeftSize.
1264 // | BaseOffset | +<nuw> Indices |
1265 // ---------------->|-------------------->|
1266 // |-->V2Size | |-------> V1Size
1268 if (!DecompGEP1.VarIndices.empty() &&
1269 DecompGEP1.NWFlags.hasNoUnsignedWrap() && V2Size.
hasValue() &&
1273 // Bail on analysing scalable LocationSize
1277 // We need to know both access sizes for all the following heuristics. Don't
1278 // try to reason about sizes larger than the index space.
1279 unsigned BW = DecompGEP1.Offset.getBitWidth();
1285 ConstantRange OffsetRange = ConstantRange(DecompGEP1.Offset);
1286 for (
unsigned i = 0, e = DecompGEP1.VarIndices.size(); i != e; ++i) {
1287 const VariableGEPIndex &
Index = DecompGEP1.VarIndices[i];
1288 const APInt &Scale =
Index.Scale;
1289 APInt ScaleForGCD = Scale;
1295 GCD = ScaleForGCD.
abs();
1300 true, &AC,
Index.CxtI);
1308 "Bit widths are normalized to MaxIndexSize");
1310 CR = CR.
smul_sat(ConstantRange(Scale));
1312 CR = CR.
smul_fast(ConstantRange(Scale));
1314 if (
Index.IsNegated)
1315 OffsetRange = OffsetRange.
sub(CR);
1317 OffsetRange = OffsetRange.
add(CR);
1320 // We now have accesses at two offsets from the same base:
1321 // 1. (...)*GCD + DecompGEP1.Offset with size V1Size
1322 // 2. 0 with size V2Size
1323 // Using arithmetic modulo GCD, the accesses are at
1324 // [ModOffset..ModOffset+V1Size) and [0..V2Size). If the first access fits
1325 // into the range [V2Size..GCD), then we know they cannot overlap.
1326 APInt ModOffset = DecompGEP1.Offset.srem(GCD);
1328 ModOffset += GCD;
// We want mod, not rem.
1330 (GCD - ModOffset).uge(V1Size.
getValue()))
1333 // Compute ranges of potentially accessed bytes for both accesses. If the
1334 // interseciton is empty, there can be no overlap.
1335 ConstantRange Range1 = OffsetRange.
add(
1336 ConstantRange(APInt(BW, 0), APInt(BW, V1Size.
getValue())));
1337 ConstantRange Range2 =
1338 ConstantRange(APInt(BW, 0), APInt(BW, V2Size.
getValue()));
1342 // Check if abs(V*Scale) >= abs(Scale) holds in the presence of
1343 // potentially wrapping math.
1344 auto MultiplyByScaleNoWrap = [](
const VariableGEPIndex &Var) {
1348 int ValOrigBW = Var.Val.V->getType()->getPrimitiveSizeInBits();
1349 // If Scale is small enough so that abs(V*Scale) >= abs(Scale) holds.
1350 // The max value of abs(V) is 2^ValOrigBW - 1. Multiplying with a
1351 // constant smaller than 2^(bitwidth(Val) - ValOrigBW) won't wrap.
1352 int MaxScaleValueBW = Var.Val.getBitWidth() - ValOrigBW;
1353 if (MaxScaleValueBW <= 0)
1355 return Var.Scale.ule(
1359 // Try to determine the range of values for VarIndex such that
1360 // VarIndex <= -MinAbsVarIndex || MinAbsVarIndex <= VarIndex.
1361 std::optional<APInt> MinAbsVarIndex;
1362 if (DecompGEP1.VarIndices.size() == 1) {
1363 // VarIndex = Scale*V.
1364 const VariableGEPIndex &Var = DecompGEP1.VarIndices[0];
1365 if (Var.Val.TruncBits == 0 &&
1366 isKnownNonZero(Var.Val.V, SimplifyQuery(DL, DT, &AC, Var.CxtI))) {
1367 // Refine MinAbsVarIndex, if abs(Scale*V) >= abs(Scale) holds in the
1368 // presence of potentially wrapping math.
1369 if (MultiplyByScaleNoWrap(Var)) {
1370 // If V != 0 then abs(VarIndex) >= abs(Scale).
1371 MinAbsVarIndex = Var.Scale.
abs();
1374 }
else if (DecompGEP1.VarIndices.size() == 2) {
1375 // VarIndex = Scale*V0 + (-Scale)*V1.
1376 // If V0 != V1 then abs(VarIndex) >= abs(Scale).
1377 // Check that MayBeCrossIteration is false, to avoid reasoning about
1378 // inequality of values across loop iterations.
1379 const VariableGEPIndex &Var0 = DecompGEP1.VarIndices[0];
1380 const VariableGEPIndex &Var1 = DecompGEP1.VarIndices[1];
1381 if (Var0.hasNegatedScaleOf(Var1) && Var0.Val.TruncBits == 0 &&
1383 MultiplyByScaleNoWrap(Var0) && MultiplyByScaleNoWrap(Var1) &&
1385 SimplifyQuery(DL, DT, &AC,
/*CxtI=*/Var0.CxtI
1388 MinAbsVarIndex = Var0.Scale.
abs();
1391 if (MinAbsVarIndex) {
1392 // The constant offset will have added at least +/-MinAbsVarIndex to it.
1393 APInt OffsetLo = DecompGEP1.Offset - *MinAbsVarIndex;
1394 APInt OffsetHi = DecompGEP1.Offset + *MinAbsVarIndex;
1395 // We know that Offset <= OffsetLo || Offset >= OffsetHi
1401 if (constantOffsetHeuristic(DecompGEP1, V1Size, V2Size, &AC, DT, AAQI))
1404 // Statically, we can see that the base objects are the same, but the
1405 // pointers have dynamic offsets which we can't resolve. And none of our
1406 // little tricks above worked.
1411 // If the results agree, take it.
1414 // A mix of PartialAlias and MustAlias is PartialAlias.
1418 // Otherwise, we don't know anything.
1422/// Provides a bunch of ad-hoc rules to disambiguate a Select instruction
1428 // If the values are Selects with the same condition, we can do a more precise
1429 // check: just check for aliases between the values on corresponding arms.
1431 if (isValueEqualInPotentialCycles(
SI->getCondition(), SI2->getCondition(),
1434 AAQI.
AAR.
alias(MemoryLocation(
SI->getTrueValue(), SISize),
1435 MemoryLocation(SI2->getTrueValue(), V2Size), AAQI);
1438 AliasResult ThisAlias =
1439 AAQI.
AAR.
alias(MemoryLocation(
SI->getFalseValue(), SISize),
1440 MemoryLocation(SI2->getFalseValue(), V2Size), AAQI);
1444 // If both arms of the Select node NoAlias or MustAlias V2, then returns
1445 // NoAlias / MustAlias. Otherwise, returns MayAlias.
1446 AliasResult Alias = AAQI.
AAR.
alias(MemoryLocation(
SI->getTrueValue(), SISize),
1447 MemoryLocation(V2, V2Size), AAQI);
1451 AliasResult ThisAlias =
1452 AAQI.
AAR.
alias(MemoryLocation(
SI->getFalseValue(), SISize),
1453 MemoryLocation(V2, V2Size), AAQI);
1457/// Provide a bunch of ad-hoc rules to disambiguate a PHI instruction against
1464 // If the values are PHIs in the same block, we can do a more precise
1465 // as well as efficient check: just check for aliases between the values
1466 // on corresponding edges. Don't do this if we are analyzing across
1467 // iterations, as we may pick a different phi entry in different iterations.
1470 std::optional<AliasResult> Alias;
1472 AliasResult ThisAlias = AAQI.
AAR.
alias(
1488 // If a phi operand recurses back to the phi, we can still determine NoAlias
1489 // if we don't alias the underlying objects of the other phi operands, as we
1490 // know that the recursive phi needs to be based on them in some way.
1491 bool isRecursive =
false;
1492 auto CheckForRecPhi = [&](
Value *PV) {
1502 SmallPtrSet<Value *, 4> UniqueSrc;
1503 Value *OnePhi =
nullptr;
1505 // Skip the phi itself being the incoming value.
1510 if (OnePhi && OnePhi != PV1) {
1511 // To control potential compile time explosion, we choose to be
1512 // conserviate when we have more than one Phi input. It is important
1513 // that we handle the single phi case as that lets us handle LCSSA
1514 // phi nodes and (combined with the recursive phi handling) simple
1515 // pointer induction variable patterns.
1521 if (CheckForRecPhi(PV1))
1524 if (UniqueSrc.
insert(PV1).second)
1528 if (OnePhi && UniqueSrc.
size() > 1)
1529 // Out of an abundance of caution, allow only the trivial lcssa and
1530 // recursive phi cases.
1533 // If V1Srcs is empty then that means that the phi has no underlying non-phi
1534 // value. This should only be possible in blocks unreachable from the entry
1535 // block, but return MayAlias just in case.
1539 // If this PHI node is recursive, indicate that the pointer may be moved
1540 // across iterations. We can only prove NoAlias if different underlying
1541 // objects are involved.
1545 // In the recursive alias queries below, we may compare values from two
1546 // different loop iterations.
1549 AliasResult Alias = AAQI.
AAR.
alias(MemoryLocation(V1Srcs[0], PNSize),
1550 MemoryLocation(V2, V2Size), AAQI);
1552 // Early exit if the check of the first PHI source against V2 is MayAlias.
1553 // Other results are not possible.
1556 // With recursive phis we cannot guarantee that MustAlias/PartialAlias will
1557 // remain valid to all elements and needs to conservatively return MayAlias.
1561 // If all sources of the PHI node NoAlias or MustAlias V2, then returns
1562 // NoAlias / MustAlias. Otherwise, returns MayAlias.
1563 for (
unsigned i = 1, e = V1Srcs.
size(); i != e; ++i) {
1566 AliasResult ThisAlias = AAQI.
AAR.
alias(
1567 MemoryLocation(V, PNSize), MemoryLocation(V2, V2Size), AAQI);
1576// Return true for an Argument or extractvalue(Argument). These are all known
1577// to not alias with FunctionLocal objects and can come up from coerced function
1586/// Provides a bunch of ad-hoc rules to disambiguate in common cases, such as
1587/// array references.
1592 // If either of the memory references is empty, it doesn't matter what the
1593 // pointer values are.
1597 // Strip off any casts if they exist.
1601 // If V1 or V2 is undef, the result is NoAlias because we can always pick a
1602 // value for undef that aliases nothing in the program.
1606 // Are we checking for alias of the same value?
1607 // Because we look 'through' phi nodes, we could look at "Value" pointers from
1608 // different iterations. We must therefore make sure that this is not the
1609 // case. The function isValueEqualInPotentialCycles ensures that this cannot
1610 // happen by looking at the visited phi nodes and making sure they cannot
1612 if (isValueEqualInPotentialCycles(V1, V2, AAQI))
1615 // Figure out what objects these things are pointing to if we can.
1619 // Null values in the default address space don't point to any object, so they
1620 // don't alias any other pointer.
1629 // If V1/V2 point to two different objects, we know that we have no alias.
1633 // Function arguments can't alias with things that are known to be
1634 // unambigously identified at the function level.
1639 // If one pointer is the result of a call/invoke or load and the other is a
1640 // non-escaping local object within the same function, then we know the
1641 // object couldn't escape to a point where the call could return it.
1643 // Note that if the pointers are in different functions, there are a
1644 // variety of complications. A call with a nocapture argument may still
1645 // temporary store the nocapture argument's value in a temporary memory
1646 // location if that memory location doesn't escape. Or it may pass a
1647 // nocapture value to other functions as long as they don't capture it.
1658 // If the size of one access is larger than the entire object on the other
1659 // side, then we know such behavior is undefined and can assume no alias.
1663 TLI, NullIsValidLocation)) ||
1666 TLI, NullIsValidLocation)))
1670 for (AssumptionCache::ResultElem &Elem : AC.assumptionsFor(O1)) {
1675 OperandBundleUse OBU =
Assume->getOperandBundleAt(Elem.Index);
1676 if (OBU.
getTagName() ==
"separate_storage") {
1680 // This is often a no-op; instcombine rewrites this for us. No-op
1681 // getUnderlyingObject calls are fast, though.
1685 DominatorTree *DT = getDT(AAQI);
1686 auto ValidAssumeForPtrContext = [&](
const Value *
Ptr) {
1689 /* AllowEphemerals */ true);
1693 &*PtrA->getParent()->getEntryBlock().begin();
1695 /* AllowEphemerals */ true);
1700 if ((O1 == HintO1 && O2 == HintO2) || (O1 == HintO2 && O2 == HintO1)) {
1701 // Note that we go back to V1 and V2 for the
1702 // ValidAssumeForPtrContext checks; they're dominated by O1 and O2,
1703 // so strictly more assumptions are valid for them.
1705 /* AllowEphemerals */ true)) ||
1706 ValidAssumeForPtrContext(V1) || ValidAssumeForPtrContext(V2)) {
1714 // If one the accesses may be before the accessed pointer, canonicalize this
1715 // by using unknown after-pointer sizes for both accesses. This is
1716 // equivalent, because regardless of which pointer is lower, one of them
1717 // will always came after the other, as long as the underlying objects aren't
1718 // disjoint. We do this so that the rest of BasicAA does not have to deal
1719 // with accesses before the base pointer, and to improve cache utilization by
1720 // merging equivalent states.
1726 // FIXME: If this depth limit is hit, then we may cache sub-optimal results
1727 // for recursive queries. For this reason, this limit is chosen to be large
1728 // enough to be very rarely hit, while still being small enough to avoid
1730 if (AAQI.
Depth >= 512)
1733 // Check the cache before climbing up use-def chains. This also terminates
1734 // otherwise infinitely recursive queries. Include MayBeCrossIteration in the
1735 // cache key, because some cases where MayBeCrossIteration==false returns
1736 // MustAlias or NoAlias may become MayAlias under MayBeCrossIteration==true.
1739 const bool Swapped = V1 > V2;
1745 auto &
Entry = Pair.first->second;
1746 if (!
Entry.isDefinitive()) {
1747 // Remember that we used an assumption. This may either be a direct use
1748 // of an assumption, or a use of an entry that may itself be based on an
1751 if (
Entry.isAssumption())
1752 ++
Entry.NumAssumptionUses;
1754 // Cache contains sorted {V1,V2} pairs but we should return original order.
1763 aliasCheckRecursive(V1, V1Size, V2, V2Size, AAQI, O1, O2);
1767 auto &
Entry = It->second;
1769 // Check whether a NoAlias assumption has been used, but disproven.
1770 bool AssumptionDisproven =
1772 if (AssumptionDisproven)
1775 // This is a definitive result now, when considered as a root query.
1778 // Cache contains sorted {V1,V2} pairs.
1779 Entry.Result.swap(Swapped);
1781 // If the assumption has been disproven, remove any results that may have
1782 // been based on this assumption. Do this after the Entry updates above to
1783 // avoid iterator invalidation.
1784 if (AssumptionDisproven)
1788 // The result may still be based on assumptions higher up in the chain.
1789 // Remember it, so it can be purged from the cache later.
1798 // Depth is incremented before this function is called, so Depth==1 indicates
1800 if (AAQI.
Depth == 1) {
1801 // Any remaining assumption based results must be based on proven
1802 // assumptions, so convert them to definitive results.
1819 AliasResult
Result = aliasGEP(GV1, V1Size, V2, V2Size, O1, O2, AAQI);
1823 AliasResult
Result = aliasGEP(GV2, V2Size, V1, V1Size, O2, O1, AAQI);
1830 AliasResult
Result = aliasPHI(PN, V1Size, V2, V2Size, AAQI);
1834 AliasResult
Result = aliasPHI(PN, V2Size, V1, V1Size, AAQI);
1841 AliasResult
Result = aliasSelect(
S1, V1Size, V2, V2Size, AAQI);
1845 AliasResult
Result = aliasSelect(S2, V2Size, V1, V1Size, AAQI);
1851 // If both pointers are pointing into the same object and one of them
1852 // accesses the entire object, then the accesses must overlap in some way.
1866 // There cannot be any alias with errno if the given memory location is an
1867 // identified function-local object, or the size of the memory access is
1868 // larger than the integer size.
1869 if (
Loc.Size.hasValue() &&
1870 Loc.Size.getValue().getKnownMinValue() * 8 > TLI.getIntSize())
1878/// Check whether two Values can be considered equivalent.
1880/// If the values may come from different cycle iterations, this will also
1881/// check that the values are not part of cycle. We have to do this because we
1882/// are looking through phi nodes, that is we say
1883/// noalias(V, phi(VA, VB)) if noalias(V, VA) and noalias(V, VB).
1884bool BasicAAResult::isValueEqualInPotentialCycles(
const Value *V,
1893 // Non-instructions and instructions in the entry block cannot be part of
1896 if (!Inst || Inst->
getParent()->isEntryBlock())
1899 return isNotInCycle(Inst, getDT(AAQI),
/*LI*/ nullptr);
1902/// Computes the symbolic difference between two de-composed GEPs.
1903void BasicAAResult::subtractDecomposedGEPs(DecomposedGEP &DestGEP,
1904 const DecomposedGEP &SrcGEP,
1906 // Drop nuw flag from GEP if subtraction of constant offsets overflows in an
1908 if (DestGEP.Offset.ult(SrcGEP.Offset))
1909 DestGEP.NWFlags = DestGEP.NWFlags.withoutNoUnsignedWrap();
1911 DestGEP.Offset -= SrcGEP.Offset;
1912 for (
const VariableGEPIndex &Src : SrcGEP.VarIndices) {
1913 // Find V in Dest. This is N^2, but pointer indices almost never have more
1914 // than a few variable indexes.
1916 for (
auto I :
enumerate(DestGEP.VarIndices)) {
1917 VariableGEPIndex &Dest =
I.value();
1918 if ((!isValueEqualInPotentialCycles(Dest.Val.V, Src.Val.V, AAQI) &&
1920 !Dest.Val.hasSameCastsAs(Src.Val))
1923 // Normalize IsNegated if we're going to lose the NSW flag anyway.
1924 if (Dest.IsNegated) {
1925 Dest.Scale = -Dest.Scale;
1926 Dest.IsNegated =
false;
1930 // If we found it, subtract off Scale V's from the entry in Dest. If it
1931 // goes to zero, remove the entry.
1932 if (Dest.Scale != Src.Scale) {
1933 // Drop nuw flag from GEP if subtraction of V's Scale overflows in an
1935 if (Dest.Scale.
ult(Src.Scale))
1936 DestGEP.NWFlags = DestGEP.NWFlags.withoutNoUnsignedWrap();
1938 Dest.Scale -= Src.Scale;
1941 DestGEP.VarIndices.erase(DestGEP.VarIndices.begin() +
I.index());
1947 // If we didn't consume this entry, add it to the end of the Dest list.
1949 VariableGEPIndex
Entry = {Src.Val, Src.Scale, Src.CxtI, Src.IsNSW,
1950 /* IsNegated */ true};
1951 DestGEP.VarIndices.push_back(Entry);
1953 // Drop nuw flag when we have unconsumed variable indices from SrcGEP.
1954 DestGEP.NWFlags = DestGEP.NWFlags.withoutNoUnsignedWrap();
1959bool BasicAAResult::constantOffsetHeuristic(
const DecomposedGEP &
GEP,
1965 if (
GEP.VarIndices.size() != 2 || !MaybeV1Size.
hasValue() ||
1969 const uint64_t V1Size = MaybeV1Size.
getValue();
1970 const uint64_t V2Size = MaybeV2Size.
getValue();
1972 const VariableGEPIndex &Var0 =
GEP.VarIndices[0], &Var1 =
GEP.VarIndices[1];
1974 if (Var0.Val.TruncBits != 0 || !Var0.Val.hasSameCastsAs(Var1.Val) ||
1975 !Var0.hasNegatedScaleOf(Var1) ||
1979 // We'll strip off the Extensions of Var0 and Var1 and do another round
1980 // of GetLinearExpression decomposition. In the example above, if Var0
1981 // is zext(%x + 1) we should get V1 == %x and V1Offset == 1.
1983 LinearExpression E0 =
1985 LinearExpression E1 =
1987 if (E0.
Scale != E1.
Scale || !E0.Val.hasSameCastsAs(E1.Val) ||
1988 !isValueEqualInPotentialCycles(E0.Val.V, E1.Val.V, AAQI))
1991 // We have a hit - Var0 and Var1 only differ by a constant offset!
1993 // If we've been sext'ed then zext'd the maximum difference between Var0 and
1994 // Var1 is possible to calculate, but we're just interested in the absolute
1995 // minimum difference between the two. The minimum distance may occur due to
1996 // wrapping; consider "add i3 %i, 5": if %i == 7 then 7 + 5 mod 8 == 4, and so
1997 // the minimum distance between %i and %i + 5 is 3.
1998 APInt MinDiff = E0.
Offset - E1.
Offset, Wrapped = -MinDiff;
2000 APInt MinDiffBytes =
2003 // We can't definitely say whether GEP1 is before or after V2 due to wrapping
2004 // arithmetic (i.e. for some values of GEP1 and V2 GEP1 < V2, and for other
2005 // values GEP1 > V2). We'll therefore only declare NoAlias if both V1Size and
2006 // V2Size can fit in the MinDiffBytes gap.
2007 return MinDiffBytes.
uge(V1Size +
GEP.Offset.abs()) &&
2008 MinDiffBytes.
uge(V2Size +
GEP.Offset.abs());
2011//===----------------------------------------------------------------------===//
2012// BasicAliasAnalysis Pass
2013//===----------------------------------------------------------------------===//
2028void BasicAAWrapperPass::anchor() {}
2031 "Basic Alias Analysis (stateless AA impl)",
true,
true)
2036 "Basic Alias Analysis (stateless AA impl)",
true,
true)
2048 TLIWP.getTLI(
F), ACT.getAssumptionCache(
F),
2049 &DTWP.getDomTree()));
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
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 isArgumentOrArgumentLike(const Value *V)
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)
This is the interface for LLVM's primary stateless and local alias analysis.
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
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)
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.
LLVM_ABI AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB)
The main low level interface to the alias analysis implementation.
LLVM_ABI MemoryEffects getMemoryEffects(const CallBase *Call)
Return the behavior of the given call site.
LLVM_ABI ModRefInfo getArgModRefInfo(const CallBase *Call, unsigned ArgIdx)
Get the ModRef info associated with a pointer argument of a call.
LLVM_ABI AliasResult aliasErrno(const MemoryLocation &Loc, const Module *M)
Class for arbitrary precision integers.
LLVM_ABI APInt umul_ov(const APInt &RHS, bool &Overflow) const
LLVM_ABI 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 ult(const APInt &RHS) const
Unsigned less than comparison.
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.
LLVM_ABI 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)
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.
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.
This is the AA result object for the basic, local, and stateless alias analysis.
LLVM_ABI AliasResult aliasErrno(const MemoryLocation &Loc, const Module *M)
LLVM_ABI ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc, AAQueryInfo &AAQI)
Checks to see if the specified callsite can clobber the specified memory object.
LLVM_ABI ModRefInfo getArgModRefInfo(const CallBase *Call, unsigned ArgIdx)
Get the location associated with a pointer argument of a callsite.
LLVM_ABI MemoryEffects getMemoryEffects(const CallBase *Call, AAQueryInfo &AAQI)
Returns the behavior when calling the given call site.
LLVM_ABI 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.
LLVM_ABI bool invalidate(Function &Fn, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &Inv)
Handle invalidation events in the new pass manager.
LLVM_ABI 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...
LLVM_ABI BasicAAResult run(Function &F, FunctionAnalysisManager &AM)
LLVM Basic Block Representation.
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.
LLVM_ABI 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 LLVM_ABI ConstantRange fromKnownBits(const KnownBits &Known, bool IsSigned)
Initialize a range based on a known bits constraint.
LLVM_ABI ConstantRange smul_fast(const ConstantRange &Other) const
Return range of possible values for a signed multiplication of this and Other.
LLVM_ABI bool isEmptySet() const
Return true if this set contains no members.
LLVM_ABI ConstantRange smul_sat(const ConstantRange &Other) const
Perform a signed saturating multiplication of two constant ranges.
LLVM_ABI APInt getUnsignedMax() const
Return the largest unsigned value contained in the ConstantRange.
LLVM_ABI ConstantRange intersectWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the intersection of this range with another range.
LLVM_ABI APInt getSignedMax() const
Return the largest signed value contained in the ConstantRange.
uint32_t getBitWidth() const
Get the bit width of this ConstantRange.
LLVM_ABI ConstantRange sub(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a subtraction of a value in this r...
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)
CaptureComponents getCapturesBefore(const Value *Object, const Instruction *I, bool OrAt) override
Return how Object may be captured before instruction I, considering only provenance captures.
FunctionPass class - This class is used to implement most global optimizations.
Represents flags for the getelementptr instruction/expression.
static GEPNoWrapFlags all()
bool hasNoUnsignedSignedWrap() const
LLVM_ABI 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()
MemoryEffectsBase getWithoutLoc(Location Loc) const
Get new MemoryEffectsBase with NoModRef on the given Loc.
static MemoryEffectsBase inaccessibleMemOnly(ModRefInfo MR=ModRefInfo::ModRef)
static MemoryEffectsBase writeOnly()
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.
static LLVM_ABI MemoryLocation getForArgument(const CallBase *Call, unsigned ArgIdx, const TargetLibraryInfo *TLI)
Return a location representing a particular argument of a call.
A Module instance is used to store all the information related to an LLVM module.
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.
AnalysisType & getAnalysis() const
getAnalysis<AnalysisType>() - This function is used by subclasses to get to the analysis information ...
A set of analyses that are preserved following a run of a transformation pass.
This class represents the LLVM 'select' instruction.
CaptureComponents getCapturesBefore(const Value *Object, const Instruction *I, bool OrAt) override
Return how Object may be captured before instruction I, considering only provenance captures.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
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.
LLVM_ABI TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
A Use represents the edge between a Value definition and its users.
const Use * const_op_iterator
Value * getOperand(unsigned i) const
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
LLVM_ABI 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.
LLVM_ABI APInt GreatestCommonDivisor(APInt A, APInt B)
Compute GCD of two unsigned APInt values.
bool match(Val *V, const Pattern &P)
IntrinsicID_match m_VScale()
Matches a call to llvm.vscale().
initializer< Ty > init(const Ty &Val)
@ Assume
Do not drop type tests (default).
friend class Instruction
Iterator for Instructions in a `BasicBlock.
This is an optimization pass for GlobalISel generic memory operations.
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
bool capturesReadProvenanceOnly(CaptureComponents CC)
FunctionAddr VTableAddr Value
LLVM_ABI 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,...
SaveAndRestore(T &) -> SaveAndRestore< T >
detail::scope_exit< std::decay_t< Callable > > make_scope_exit(Callable &&F)
LLVM_ABI 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,...
LLVM_ABI 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...
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
auto successors(const MachineBasicBlock *BB)
LLVM_ABI 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.
constexpr bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
LLVM_ABI std::pair< Instruction *, CaptureComponents > FindEarliestCapture(const Value *V, Function &F, bool ReturnCaptures, const DominatorTree &DT, CaptureComponents Mask, unsigned MaxUsesToExplore=0)
MemoryEffectsBase< IRMemLocation > MemoryEffects
Summary of how a function affects memory in the program.
LLVM_ABI std::optional< TypeSize > getBaseObjectSize(const Value *Ptr, const DataLayout &DL, const TargetLibraryInfo *TLI, ObjectSizeOpts Opts={})
Like getObjectSize(), but only returns the size of base objects (like allocas, global variables and a...
LLVM_ABI 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.
LLVM_ABI 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 capturesFullProvenance(CaptureComponents CC)
bool isModSet(const ModRefInfo MRI)
LLVM_ABI void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
LLVM_ABI bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
generic_gep_type_iterator<> gep_type_iterator
bool isModOrRefSet(const ModRefInfo MRI)
constexpr unsigned MaxLookupSearchDepth
The max limit of the search depth in DecomposeGEPExpression() and getUnderlyingObject().
LLVM_ABI 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...
LLVM_ABI FunctionPass * createBasicAAWrapperPass()
LLVM_ABI 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...
CaptureComponents
Components of the pointer that may be captured.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
LLVM_ABI 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.
@ ArgMem
Access to memory via argument pointers.
@ InaccessibleMem
Memory that is inaccessible via LLVM IR.
LLVM_ABI bool isKnownNonEqual(const Value *V1, const Value *V2, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the given values are known to be non-equal when defined.
DWARFExpression::Operation Op
LLVM_ABI bool PointerMayBeCaptured(const Value *V, bool ReturnCaptures, unsigned MaxUsesToExplore=0)
PointerMayBeCaptured - Return true if this pointer value may be captured by the enclosing function (w...
bool isModAndRefSet(const ModRefInfo MRI)
LLVM_ABI bool isIdentifiedFunctionLocal(const Value *V)
Return true if V is umabigously identified at the function-level.
constexpr unsigned BitWidth
LLVM_ABI bool isEscapeSource(const Value *V)
Returns true if the pointer is one which would have been considered an escape by isNotCapturedBefore.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
gep_type_iterator gep_type_begin(const User *GEP)
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
LLVM_ABI const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=MaxLookupSearchDepth)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
bool capturesNothing(CaptureComponents CC)
LLVM_ABI bool isIdentifiedObject(const Value *V)
Return true if this pointer refers to a distinct and identifiable object.
LLVM_ABI 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 CaptureComponents getCapturesBefore(const Value *Object, const Instruction *I, bool OrAt)=0
Return how Object may be captured before instruction I, considering only provenance captures.
virtual ~CaptureAnalysis()=0
Linear expression BasePtr + Index * Scale + Offset.
LinearExpression(Value *BasePtr, unsigned BitWidth)
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.
StringRef getTagName() const
Return the tag of this operand bundle as a string.