64#define DEBUG_TYPE "basicaa"
78STATISTIC(SearchLimitReached,
"Number of times the limit to "
79 "decompose GEPs is reached");
80STATISTIC(SearchTimes,
"Number of times a GEP is decomposed");
105 bool RoundToAlign =
false) {
111 if (
Size->isScalable())
124 bool NullIsValidLoc) {
150 std::optional<TypeSize> ObjectSize =
getObjectSize(V,
DL, TLI, NullIsValidLoc,
162 bool NullIsValidLoc) {
167 bool CanBeNull, CanBeFreed;
169 V.getPointerDereferenceableBytes(
DL, CanBeNull, CanBeFreed);
170 DerefBytes = (CanBeNull && NullIsValidLoc) ? 0 : DerefBytes;
181 std::optional<TypeSize> ObjectSize =
183 return ObjectSize && *ObjectSize ==
Size;
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 =
236 if (EarliestCapture.first)
237 Inst2Obj[EarliestCapture.first].push_back(Object);
238 Iter.first->second = EarliestCapture;
241 auto IsNotCapturedBefore = [&]() {
243 Instruction *CaptureInst = Iter.first->second.first;
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);
281 unsigned ZExtBits = 0;
282 unsigned SExtBits = 0;
283 unsigned TruncBits = 0;
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);
304 CastedValue withZExtOfValue(
const Value *NewV,
bool ZExtNonNegative)
const {
305 unsigned ExtendBy =
V->getType()->getPrimitiveSizeInBits() -
307 if (ExtendBy <= TruncBits)
310 return CastedValue(NewV, ZExtBits, SExtBits, TruncBits - ExtendBy,
314 ExtendBy -= TruncBits;
319 return CastedValue(NewV, ZExtBits + SExtBits + ExtendBy, 0, 0,
324 CastedValue withSExtOfValue(
const Value *NewV)
const {
325 unsigned ExtendBy =
V->getType()->getPrimitiveSizeInBits() -
327 if (ExtendBy <= TruncBits)
330 return CastedValue(NewV, ZExtBits, SExtBits, TruncBits - ExtendBy,
334 ExtendBy -= TruncBits;
337 return CastedValue(NewV, ZExtBits, SExtBits + ExtendBy, 0, IsNonNegative);
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);
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 {
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)
378 if (IsNonNegative ||
Other.IsNonNegative)
379 return (ZExtBits + SExtBits ==
Other.ZExtBits +
Other.SExtBits &&
380 TruncBits ==
Other.TruncBits);
386struct LinearExpression {
396 LinearExpression(
const CastedValue &Val,
const APInt &Scale,
398 : Val(Val), Scale(Scale),
Offset(
Offset), IsNUW(IsNUW), IsNSW(IsNSW) {}
400 LinearExpression(
const CastedValue &Val)
401 : Val(Val), IsNUW(
true), IsNSW(
true) {
402 unsigned BitWidth = Val.getBitWidth();
407 LinearExpression mul(
const APInt &
Other,
bool MulIsNUW,
bool MulIsNSW)
const {
410 bool NSW = IsNSW && (
Other.isOne() || (MulIsNSW &&
Offset.isZero()));
411 bool NUW = IsNUW && (
Other.isOne() || MulIsNUW);
426 if (
const ConstantInt *Const = dyn_cast<ConstantInt>(Val.V))
427 return LinearExpression(Val,
APInt(Val.getBitWidth(), 0),
428 Val.evaluateWith(Const->getValue()),
true,
true);
430 if (
const BinaryOperator *BOp = dyn_cast<BinaryOperator>(Val.V)) {
431 if (
ConstantInt *RHSC = dyn_cast<ConstantInt>(BOp->getOperand(1))) {
432 APInt RHS = Val.evaluateWith(RHSC->getValue());
435 bool NUW =
true, NSW =
true;
436 if (isa<OverflowingBinaryOperator>(BOp)) {
437 NUW &= BOp->hasNoUnsignedWrap();
438 NSW &= BOp->hasNoSignedWrap();
440 if (!Val.canDistributeOver(NUW, NSW))
448 LinearExpression E(Val);
449 switch (BOp->getOpcode()) {
454 case Instruction::Or:
456 if (!cast<PossiblyDisjointInst>(BOp)->isDisjoint())
460 case Instruction::Add: {
468 case Instruction::Sub: {
476 case Instruction::Mul:
481 case Instruction::Shl:
487 if (
RHS.getLimitedValue() > Val.getBitWidth())
492 E.Offset <<=
RHS.getLimitedValue();
493 E.Scale <<=
RHS.getLimitedValue();
502 if (
const auto *ZExt = dyn_cast<ZExtInst>(Val.V))
504 Val.withZExtOfValue(ZExt->getOperand(0), ZExt->hasNonNeg()),
DL,
507 if (isa<SExtInst>(Val.V))
509 Val.withSExtOfValue(cast<CastInst>(Val.V)->getOperand(0)),
518struct VariableGEPIndex {
533 bool hasNegatedScaleOf(
const VariableGEPIndex &
Other)
const {
534 if (IsNegated ==
Other.IsNegated)
535 return Scale == -
Other.Scale;
536 return Scale ==
Other.Scale;
544 OS <<
"(V=" << Val.V->getName()
545 <<
", zextbits=" << Val.ZExtBits
546 <<
", sextbits=" << Val.SExtBits
547 <<
", truncbits=" << Val.TruncBits
548 <<
", scale=" << Scale
550 <<
", negated=" << IsNegated <<
")";
599 const Instruction *CxtI = dyn_cast<Instruction>(V);
601 unsigned IndexSize =
DL.getIndexTypeSizeInBits(V->getType());
602 DecomposedGEP Decomposed;
603 Decomposed.Offset =
APInt(IndexSize, 0);
609 if (
const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
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);
624 if (
DL.getIndexTypeSizeInBits(NewV->
getType()) != IndexSize) {
634 if (
const auto *
PHI = dyn_cast<PHINode>(V)) {
636 if (
PHI->getNumIncomingValues() == 1) {
637 V =
PHI->getIncomingValue(0);
640 }
else if (
const auto *Call = dyn_cast<CallBase>(V)) {
668 I != E; ++
I, ++GTI) {
673 unsigned FieldNo = cast<ConstantInt>(Index)->getZExtValue();
677 Decomposed.Offset +=
DL.getStructLayout(STy)->getElementOffset(FieldNo);
682 if (
const ConstantInt *CIdx = dyn_cast<ConstantInt>(Index)) {
694 CIdx->getValue().sextOrTrunc(IndexSize);
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);
718 Decomposed.Offset +=
LE.Offset;
721 Decomposed.NWFlags = Decomposed.NWFlags.withoutNoUnsignedWrap();
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;
733 LE.IsNSW =
LE.IsNUW =
false;
734 Decomposed.VarIndices.erase(Decomposed.VarIndices.begin() + i);
740 VariableGEPIndex
Entry = {
LE.Val, Scale, CxtI,
LE.IsNSW,
742 Decomposed.VarIndices.push_back(Entry);
748 }
while (--MaxLookup);
752 SearchLimitReached++;
759 assert(Visited.empty() &&
"Visited must be cleared after use!");
762 unsigned MaxLookup = 8;
769 if (!Visited.insert(V).second)
773 if (IgnoreLocals && isa<AllocaInst>(V))
782 if (
const Argument *Arg = dyn_cast<Argument>(V)) {
783 if (Arg->hasNoAliasAttr() && Arg->onlyReadsMemory()) {
794 if (!GV->isConstant())
800 if (
const SelectInst *SI = dyn_cast<SelectInst>(V)) {
808 if (
const PHINode *PN = dyn_cast<PHINode>(V)) {
810 if (PN->getNumIncomingValues() > MaxLookup)
818 }
while (!Worklist.
empty() && --MaxLookup);
821 if (!Worklist.
empty())
829 return II &&
II->getIntrinsicID() == IID;
835 MemoryEffects Min = Call->getAttributes().getMemoryEffects();
837 if (
const Function *F = dyn_cast<Function>(Call->getCalledOperand())) {
841 if (Call->hasReadingOperandBundles())
843 if (Call->hasClobberingOperandBundles())
845 if (Call->isVolatile()) {
858 switch (
F->getIntrinsicID()) {
859 case Intrinsic::experimental_guard:
860 case Intrinsic::experimental_deoptimize:
867 return F->getMemoryEffects();
872 if (Call->doesNotAccessMemory(ArgIdx))
875 if (Call->onlyWritesMemory(ArgIdx))
878 if (Call->onlyReadsMemory(ArgIdx))
886 if (
const Instruction *inst = dyn_cast<Instruction>(V)) {
887 if (!inst->getParent())
892 if (
const Argument *arg = dyn_cast<Argument>(V))
893 return arg->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);
925 "AliasAnalysis query involving multiple functions!");
934 if (isa<AllocaInst>(Object))
935 if (
const CallInst *CI = dyn_cast<CallInst>(Call))
936 if (CI->isTailCall() &&
937 !CI->getAttributes().hasAttrSomewhere(Attribute::ByVal))
942 if (
auto *AI = dyn_cast<AllocaInst>(Object))
943 if (!AI->isStaticAlloca() &&
isIntrinsicCall(Call, Intrinsic::stackrestore))
950 if (ME.doesNotAccessMemory())
964 if (
isModOrRefSet(OtherMR) && !isa<Constant>(Object) && Call != Object &&
965 (isa<AllocaInst>(Object) || !Call->hasFnAttr(Attribute::ReturnsTwice))) {
977 if ((ArgMR | OtherMR) != OtherMR) {
979 for (
const Use &U : Call->data_ops()) {
980 const Value *Arg = U;
983 unsigned ArgIdx = Call->getDataOperandNo(&U);
985 Call->isArgOperand(&U)
993 if (NewArgMR == ArgMR)
1085 auto BaseObjectsAlias = [&]() {
1096 if (!isa<GEPOperator>(V2))
1101 return BaseObjectsAlias();
1105 DecomposedGEP DecompGEP1 = DecomposeGEPExpression(GEP1, DL, &AC, DT);
1106 DecomposedGEP DecompGEP2 = DecomposeGEPExpression(V2, DL, &AC, DT);
1109 if (DecompGEP1.Base == GEP1 && DecompGEP2.Base == V2)
1113 if (DecompGEP1.Offset.getBitWidth() != DecompGEP2.Offset.getBitWidth())
1114 return BaseObjectsAlias();
1117 if (DecompGEP1.VarIndices.size() < DecompGEP2.VarIndices.size()) {
1125 subtractDecomposedGEPs(DecompGEP1, DecompGEP2, AAQI);
1132 if (DecompGEP1.NWFlags.isInBounds() && DecompGEP1.VarIndices.empty() &&
1134 DecompGEP1.Offset.sge(V2Size.
getValue()) &&
1139 if (DecompGEP2.NWFlags.isInBounds() && DecompGEP1.VarIndices.empty() &&
1141 DecompGEP1.Offset.sle(-V1Size.
getValue()) &&
1147 if (DecompGEP1.Offset == 0 && DecompGEP1.VarIndices.empty())
1168 if (DecompGEP1.VarIndices.empty()) {
1174 const bool Swapped =
Off.isNegative();
1192 if (
Off.ult(LSize)) {
1197 Off.ule(INT32_MAX) && (Off + VRightSize.
getValue()).ule(LSize)) {
1213 if (!Overflow &&
Off.uge(UpperRange))
1221 if (DecompGEP1.VarIndices.size() == 1 &&
1222 DecompGEP1.VarIndices[0].Val.TruncBits == 0 &&
1223 DecompGEP1.Offset.isZero() &&
1226 const VariableGEPIndex &ScalableVar = DecompGEP1.VarIndices[0];
1228 ScalableVar.IsNegated ? -ScalableVar.Scale : ScalableVar.Scale;
1233 bool Overflows = !DecompGEP1.VarIndices[0].IsNSW;
1258 if (!DecompGEP1.VarIndices.empty() &&
1259 DecompGEP1.NWFlags.hasNoUnsignedWrap() && V2Size.
hasValue() &&
1269 unsigned BW = DecompGEP1.Offset.getBitWidth();
1276 for (
unsigned i = 0, e = DecompGEP1.VarIndices.size(); i != e; ++i) {
1277 const VariableGEPIndex &
Index = DecompGEP1.VarIndices[i];
1279 APInt ScaleForGCD = Scale;
1285 GCD = ScaleForGCD.
abs();
1290 true, &AC,
Index.CxtI);
1298 "Bit widths are normalized to MaxIndexSize");
1304 if (
Index.IsNegated)
1305 OffsetRange = OffsetRange.
sub(CR);
1307 OffsetRange = OffsetRange.
add(CR);
1316 APInt ModOffset = DecompGEP1.Offset.
srem(GCD);
1320 (GCD - ModOffset).uge(V1Size.
getValue()))
1334 auto MultiplyByScaleNoWrap = [](
const VariableGEPIndex &Var) {
1338 int ValOrigBW = Var.Val.V->getType()->getPrimitiveSizeInBits();
1342 int MaxScaleValueBW = Var.Val.getBitWidth() - ValOrigBW;
1343 if (MaxScaleValueBW <= 0)
1345 return Var.Scale.ule(
1351 std::optional<APInt> MinAbsVarIndex;
1352 if (DecompGEP1.VarIndices.size() == 1) {
1354 const VariableGEPIndex &Var = DecompGEP1.VarIndices[0];
1355 if (Var.Val.TruncBits == 0 &&
1359 if (MultiplyByScaleNoWrap(Var)) {
1361 MinAbsVarIndex = Var.Scale.abs();
1364 }
else if (DecompGEP1.VarIndices.size() == 2) {
1369 const VariableGEPIndex &Var0 = DecompGEP1.VarIndices[0];
1370 const VariableGEPIndex &Var1 = DecompGEP1.VarIndices[1];
1371 if (Var0.hasNegatedScaleOf(Var1) && Var0.Val.TruncBits == 0 &&
1373 MultiplyByScaleNoWrap(Var0) && MultiplyByScaleNoWrap(Var1) &&
1378 MinAbsVarIndex = Var0.Scale.abs();
1381 if (MinAbsVarIndex) {
1383 APInt OffsetLo = DecompGEP1.Offset - *MinAbsVarIndex;
1384 APInt OffsetHi = DecompGEP1.Offset + *MinAbsVarIndex;
1391 if (constantOffsetHeuristic(DecompGEP1, V1Size, V2Size, &AC, DT, AAQI))
1420 if (
const SelectInst *SI2 = dyn_cast<SelectInst>(V2))
1421 if (isValueEqualInPotentialCycles(
SI->getCondition(), SI2->getCondition(),
1458 if (
const PHINode *PN2 = dyn_cast<PHINode>(V2))
1460 std::optional<AliasResult> Alias;
1481 bool isRecursive =
false;
1482 auto CheckForRecPhi = [&](
Value *PV) {
1493 Value *OnePhi =
nullptr;
1499 if (isa<PHINode>(PV1)) {
1500 if (OnePhi && OnePhi != PV1) {
1511 if (CheckForRecPhi(PV1))
1514 if (UniqueSrc.
insert(PV1).second)
1518 if (OnePhi && UniqueSrc.
size() > 1)
1553 for (
unsigned i = 1, e = V1Srcs.
size(); i != e; ++i) {
1570 if (isa<Argument>(V))
1572 auto *E = dyn_cast<ExtractValueInst>(V);
1573 return E && isa<Argument>(E->getOperand(0));
1593 if (isa<UndefValue>(V1) || isa<UndefValue>(V2))
1602 if (isValueEqualInPotentialCycles(V1, V2, AAQI))
1640 O2, dyn_cast<Instruction>(O1),
true)))
1644 O1, dyn_cast<Instruction>(O2),
true)))
1653 TLI, NullIsValidLocation)) ||
1656 TLI, NullIsValidLocation)))
1666 if (OBU.
getTagName() ==
"separate_storage") {
1676 auto ValidAssumeForPtrContext = [&](
const Value *
Ptr) {
1681 if (
const Argument *PtrA = dyn_cast<Argument>(
Ptr)) {
1683 &*PtrA->
getParent()->getEntryBlock().begin();
1690 if ((O1 == HintO1 && O2 == HintO2) || (O1 == HintO2 && O2 == HintO1)) {
1696 ValidAssumeForPtrContext(V1) || ValidAssumeForPtrContext(V2)) {
1720 if (AAQI.
Depth >= 512)
1729 const bool Swapped = V1 > V2;
1735 auto &
Entry = Pair.first->second;
1736 if (!
Entry.isDefinitive()) {
1741 if (
Entry.isAssumption())
1742 ++
Entry.NumAssumptionUses;
1753 aliasCheckRecursive(V1, V1Size, V2, V2Size, AAQI, O1, O2);
1757 auto &
Entry = It->second;
1760 bool AssumptionDisproven =
1762 if (AssumptionDisproven)
1769 Entry.Result.swap(Swapped);
1774 if (AssumptionDisproven)
1790 if (AAQI.
Depth == 1) {
1808 if (
const GEPOperator *GV1 = dyn_cast<GEPOperator>(V1)) {
1812 }
else if (
const GEPOperator *GV2 = dyn_cast<GEPOperator>(V2)) {
1819 if (
const PHINode *PN = dyn_cast<PHINode>(V1)) {
1823 }
else if (
const PHINode *PN = dyn_cast<PHINode>(V2)) {
1834 }
else if (
const SelectInst *S2 = dyn_cast<SelectInst>(V2)) {
1860bool BasicAAResult::isValueEqualInPotentialCycles(
const Value *V,
1871 const Instruction *Inst = dyn_cast<Instruction>(V);
1872 if (!Inst || Inst->
getParent()->isEntryBlock())
1879void BasicAAResult::subtractDecomposedGEPs(DecomposedGEP &DestGEP,
1880 const DecomposedGEP &SrcGEP,
1884 if (DestGEP.Offset.ult(SrcGEP.Offset))
1885 DestGEP.NWFlags = DestGEP.NWFlags.withoutNoUnsignedWrap();
1887 DestGEP.Offset -= SrcGEP.Offset;
1888 for (
const VariableGEPIndex &Src : SrcGEP.VarIndices) {
1892 for (
auto I :
enumerate(DestGEP.VarIndices)) {
1893 VariableGEPIndex &Dest =
I.value();
1894 if ((!isValueEqualInPotentialCycles(Dest.Val.V, Src.Val.V, AAQI) &&
1896 !Dest.Val.hasSameCastsAs(Src.Val))
1900 if (Dest.IsNegated) {
1901 Dest.Scale = -Dest.Scale;
1902 Dest.IsNegated =
false;
1908 if (Dest.Scale != Src.Scale) {
1911 if (Dest.Scale.ult(Src.Scale))
1912 DestGEP.NWFlags = DestGEP.NWFlags.withoutNoUnsignedWrap();
1914 Dest.Scale -= Src.Scale;
1917 DestGEP.VarIndices.erase(DestGEP.VarIndices.begin() +
I.index());
1925 VariableGEPIndex
Entry = {Src.Val, Src.Scale, Src.CxtI, Src.IsNSW,
1927 DestGEP.VarIndices.push_back(Entry);
1930 DestGEP.NWFlags = DestGEP.NWFlags.withoutNoUnsignedWrap();
1935bool BasicAAResult::constantOffsetHeuristic(
const DecomposedGEP &
GEP,
1941 if (
GEP.VarIndices.size() != 2 || !MaybeV1Size.
hasValue() ||
1948 const VariableGEPIndex &Var0 =
GEP.VarIndices[0], &Var1 =
GEP.VarIndices[1];
1950 if (Var0.Val.TruncBits != 0 || !Var0.Val.hasSameCastsAs(Var1.Val) ||
1951 !Var0.hasNegatedScaleOf(Var1) ||
1952 Var0.Val.V->getType() != Var1.Val.V->getType())
1959 LinearExpression E0 =
1961 LinearExpression E1 =
1963 if (E0.Scale != E1.Scale || !E0.Val.hasSameCastsAs(E1.Val) ||
1964 !isValueEqualInPotentialCycles(E0.Val.V, E1.Val.V, AAQI))
1974 APInt MinDiff = E0.Offset - E1.Offset, Wrapped = -MinDiff;
1976 APInt MinDiffBytes =
1977 MinDiff.
zextOrTrunc(Var0.Scale.getBitWidth()) * Var0.Scale.
abs();
1983 return MinDiffBytes.
uge(V1Size +
GEP.Offset.abs()) &&
1984 MinDiffBytes.
uge(V2Size +
GEP.Offset.abs());
2004void BasicAAWrapperPass::anchor() {}
2007 "Basic Alias Analysis (stateless AA impl)",
true,
true)
2019 auto &ACT = getAnalysis<AssumptionCacheTracker>();
2020 auto &TLIWP = getAnalysis<TargetLibraryInfoWrapperPass>();
2021 auto &DTWP = getAnalysis<DominatorTreeWrapperPass>();
2024 TLIWP.getTLI(
F), ACT.getAssumptionCache(
F),
2025 &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.
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)
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.
Class for arbitrary precision integers.
LLVM_ABI APInt umul_ov(const APInt &RHS, bool &Overflow) const
LLVM_ABI APInt zext(unsigned width) const
Zero extend to a new width.
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 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 srem(const APInt &RHS) const
Function for signed remainder operation.
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)
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.
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.
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.
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...
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)
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 hasNoUnsignedWrap() const
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()
Create MemoryEffectsBase that can read any memory.
MemoryEffectsBase getWithoutLoc(Location Loc) const
Get new MemoryEffectsBase with NoModRef on the given Loc.
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.
static LLVM_ABI MemoryLocation getForArgument(const CallBase *Call, unsigned ArgIdx, const TargetLibraryInfo *TLI)
Return a location representing a particular argument of a call.
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.
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.
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)
LLVM_ABI TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
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.
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.
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).
This is an optimization pass for GlobalISel generic memory operations.
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
bool capturesReadProvenanceOnly(CaptureComponents CC)
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,...
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...
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)
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.
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.
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.
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.
gep_type_iterator gep_type_begin(const User *GEP)
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
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.