55#include "llvm/IR/IntrinsicsAArch64.h"
56#include "llvm/IR/IntrinsicsRISCV.h"
57#include "llvm/IR/IntrinsicsX86.h"
93 return DL.getPointerTypeSizeInBits(Ty);
118 :
DL(
DL), AC(AC), CxtI(CxtI), DT(DT), ORE(ORE), IIQ(UseInstrInfo) {}
132 CxtI = dyn_cast<Instruction>(V);
146 CxtI = dyn_cast<Instruction>(V1);
150 CxtI = dyn_cast<Instruction>(V2);
158 const APInt &DemandedElts,
160 if (isa<ScalableVectorType>(Shuf->
getType())) {
162 DemandedLHS = DemandedRHS = DemandedElts;
169 DemandedElts, DemandedLHS, DemandedRHS);
180 auto *FVTy = dyn_cast<FixedVectorType>(V->
getType());
192 Query(
DL, AC,
safeCxtI(V, CxtI), DT, UseInstrInfo, ORE));
201 Query(
DL, AC,
safeCxtI(V, CxtI), DT, UseInstrInfo, ORE));
205 unsigned Depth,
const Query &Q);
216 return ::computeKnownBits(
226 return ::computeKnownBits(
227 V, DemandedElts,
Depth,
228 Query(
DL, AC,
safeCxtI(V, CxtI), DT, UseInstrInfo, ORE));
236 "LHS and RHS should have the same type");
238 "LHS and RHS should be integers");
291 return !
I->user_empty() &&
all_of(
I->users(), [](
const User *U) {
292 ICmpInst::Predicate P;
293 return match(U, m_ICmp(P, m_Value(), m_Zero())) && ICmpInst::isEquality(P);
301 bool OrZero,
unsigned Depth,
304 return ::isKnownToBeAPowerOfTwo(
309 unsigned Depth,
const Query &Q);
316 return ::isKnownNonZero(V,
Depth,
317 Query(
DL, AC,
safeCxtI(V, CxtI), DT, UseInstrInfo));
332 if (
auto *CI = dyn_cast<ConstantInt>(V))
333 return CI->getValue().isStrictlyPositive();
356 return ::isKnownNonEqual(V1, V2, 0,
358 UseInstrInfo,
nullptr));
368 return ::MaskedValueIsZero(
373 unsigned Depth,
const Query &Q);
377 auto *FVTy = dyn_cast<FixedVectorType>(V->
getType());
387 return ::ComputeNumSignBits(
400 bool NSW,
const APInt &DemandedElts,
402 unsigned Depth,
const Query &Q) {
432 bool isKnownNegativeOp0 = Known2.
isNegative();
435 (isKnownNonNegativeOp1 && isKnownNonNegativeOp0);
440 (isKnownNegativeOp1 && isKnownNonNegativeOp0 &&
442 (isKnownNegativeOp0 && isKnownNonNegativeOp1 && Known.
isNonZero());
446 bool SelfMultiply = Op0 == Op1;
467 unsigned NumRanges = Ranges.getNumOperands() / 2;
473 for (
unsigned i = 0; i < NumRanges; ++i) {
475 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0));
477 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
481 unsigned CommonPrefixBits =
484 APInt UnsignedMax = Range.getUnsignedMax().zextOrTrunc(
BitWidth);
485 Known.
One &= UnsignedMax & Mask;
486 Known.
Zero &= ~UnsignedMax & Mask;
501 while (!WorkSet.
empty()) {
503 if (!Visited.
insert(V).second)
508 return EphValues.count(U);
513 if (V ==
I || (isa<Instruction>(V) &&
515 !cast<Instruction>(V)->isTerminator())) {
517 if (
const User *U = dyn_cast<User>(V))
529 return CI->isAssumeLikeIntrinsic();
584 if (Pred == ICmpInst::ICMP_UGT)
588 if (Pred == ICmpInst::ICMP_NE)
603 if (!Q.AC || !Q.CxtI)
610 AttrKinds.
push_back(Attribute::Dereferenceable);
616 for (
auto &AssumeVH : Q.AC->assumptionsFor(V)) {
620 assert(
I->getFunction() == Q.CxtI->getFunction() &&
621 "Got assumption for the wrong function!");
641 unsigned Depth,
const Query &Q) {
644 if (!Q.AC || !Q.CxtI)
652 V, {Attribute::Alignment}, Q.CxtI, Q.DT, Q.AC)) {
661 for (
auto &AssumeVH : Q.AC->assumptionsFor(V)) {
665 assert(
I->getParent()->getParent() == Q.CxtI->getParent()->getParent() &&
666 "Got assumption for the wrong function!");
701 QueryNoAC.AC =
nullptr;
709 switch (Cmp->getPredicate()) {
712 case ICmpInst::ICMP_EQ:
719 Known.
One |= RHSKnown.
One;
721 }
else if (
match(Cmp,
732 Known.
One |= RHSKnown.
One & MaskKnown.
One;
747 }
else if (
match(Cmp,
773 }
else if (
match(Cmp,
816 Known.
One |= RHSKnown.
One;
838 Known.
One |= RHSKnown.
One <<
C;
851 case ICmpInst::ICMP_SGE:
864 case ICmpInst::ICMP_SGT:
877 case ICmpInst::ICMP_SLE:
890 case ICmpInst::ICMP_SLT:
903 case ICmpInst::ICMP_ULE:
914 case ICmpInst::ICMP_ULT:
937 case ICmpInst::ICMP_NE: {
961 <<
"Detected conflicting code assumptions. Program may "
962 "have undefined behavior, or compiler may have "
992 if (ShiftAmtIsConstant) {
993 Known = KF(Known2, Known);
1008 if (MaxShiftAmtIsOutOfRange) {
1020 std::optional<bool> ShifterOperandIsNonZero;
1025 ShifterOperandIsNonZero =
1027 if (!*ShifterOperandIsNonZero)
1033 for (
unsigned ShiftAmt = 0; ShiftAmt <
BitWidth; ++ShiftAmt) {
1036 if ((ShiftAmt & ~ShiftAmtKZ) != ShiftAmt)
1038 if ((ShiftAmt | ShiftAmtKO) != ShiftAmt)
1043 if (ShiftAmt == 0) {
1044 if (!ShifterOperandIsNonZero)
1045 ShifterOperandIsNonZero =
1047 if (*ShifterOperandIsNonZero)
1062 const APInt &DemandedElts,
1068 switch (
I->getOpcode()) {
1070 case Instruction::Load:
1072 Q.IIQ.getMetadata(cast<LoadInst>(
I), LLVMContext::MD_range))
1075 case Instruction::And: {
1087 Value *
X =
nullptr, *
Y =
nullptr;
1088 if (!Known.
Zero[0] && !Known.
One[0] &&
1097 case Instruction::Or:
1103 case Instruction::Xor:
1109 case Instruction::Mul: {
1110 bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(
I));
1112 Known, Known2,
Depth, Q);
1115 case Instruction::UDiv: {
1121 case Instruction::Select: {
1157 Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(
RHS)))
1163 case Instruction::FPTrunc:
1164 case Instruction::FPExt:
1165 case Instruction::FPToUI:
1166 case Instruction::FPToSI:
1167 case Instruction::SIToFP:
1168 case Instruction::UIToFP:
1170 case Instruction::PtrToInt:
1171 case Instruction::IntToPtr:
1174 case Instruction::ZExt:
1175 case Instruction::Trunc: {
1176 Type *SrcTy =
I->getOperand(0)->getType();
1178 unsigned SrcBitWidth;
1183 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
1184 Q.DL.getTypeSizeInBits(ScalarTy);
1186 assert(SrcBitWidth &&
"SrcBitWidth can't be zero");
1192 case Instruction::BitCast: {
1193 Type *SrcTy =
I->getOperand(0)->getType();
1197 !
I->getType()->isVectorTy()) {
1203 auto *SrcVecTy = dyn_cast<FixedVectorType>(SrcTy);
1204 if (!SrcVecTy || !SrcVecTy->getElementType()->isIntegerTy() ||
1205 !
I->getType()->isIntOrIntVectorTy() ||
1206 isa<ScalableVectorType>(
I->getType()))
1211 unsigned SubBitWidth = SrcVecTy->getScalarSizeInBits();
1228 unsigned SubScale =
BitWidth / SubBitWidth;
1230 for (
unsigned i = 0; i != NumElts; ++i) {
1231 if (DemandedElts[i])
1232 SubDemandedElts.
setBit(i * SubScale);
1236 for (
unsigned i = 0; i != SubScale; ++i) {
1239 unsigned ShiftElt = Q.DL.isLittleEndian() ? i : SubScale - 1 - i;
1240 Known.
insertBits(KnownSrc, ShiftElt * SubBitWidth);
1245 case Instruction::SExt: {
1247 unsigned SrcBitWidth =
I->getOperand(0)->getType()->getScalarSizeInBits();
1249 Known = Known.
trunc(SrcBitWidth);
1256 case Instruction::Shl: {
1257 bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(
I));
1264 Result.Zero.setSignBit();
1266 Result.One.setSignBit();
1278 case Instruction::LShr: {
1290 case Instruction::AShr: {
1298 case Instruction::Sub: {
1299 bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(
I));
1301 DemandedElts, Known, Known2,
Depth, Q);
1304 case Instruction::Add: {
1305 bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(
I));
1307 DemandedElts, Known, Known2,
Depth, Q);
1310 case Instruction::SRem:
1316 case Instruction::URem:
1321 case Instruction::Alloca:
1324 case Instruction::GetElementPtr: {
1333 for (
unsigned i = 1, e =
I->getNumOperands(); i != e; ++i, ++GTI) {
1349 "Access to structure field must be known at compile time");
1354 unsigned Idx = cast<ConstantInt>(
Index)->getZExtValue();
1357 AccConstIndices +=
Offset;
1368 unsigned IndexBitWidth =
Index->getType()->getScalarSizeInBits();
1371 TypeSize IndexTypeSize = Q.DL.getTypeAllocSize(IndexedTy);
1382 APInt ScalingFactor(IndexBitWidth, TypeSizeInBytes);
1383 IndexConst *= ScalingFactor;
1400 true,
false, Known, IndexBits);
1405 true,
false, Known,
Index);
1409 case Instruction::PHI: {
1412 Value *R =
nullptr, *L =
nullptr;
1422 if ((Opcode == Instruction::LShr || Opcode == Instruction::AShr ||
1423 Opcode == Instruction::Shl) &&
1438 case Instruction::Shl:
1442 case Instruction::LShr:
1447 case Instruction::AShr:
1458 if (Opcode == Instruction::Add ||
1459 Opcode == Instruction::Sub ||
1460 Opcode == Instruction::And ||
1461 Opcode == Instruction::Or ||
1462 Opcode == Instruction::Mul) {
1469 unsigned OpNum =
P->getOperand(0) == R ? 0 : 1;
1470 Instruction *RInst =
P->getIncomingBlock(OpNum)->getTerminator();
1471 Instruction *LInst =
P->getIncomingBlock(1-OpNum)->getTerminator();
1486 auto *OverflowOp = dyn_cast<OverflowingBinaryOperator>(BO);
1487 if (OverflowOp && Q.IIQ.hasNoSignedWrap(OverflowOp)) {
1497 if (Opcode == Instruction::Add) {
1506 else if (Opcode == Instruction::Sub && BO->
getOperand(0) ==
I) {
1514 else if (Opcode == Instruction::Mul && Known2.
isNonNegative() &&
1524 if (
P->getNumIncomingValues() == 0)
1531 if (isa_and_nonnull<UndefValue>(
P->hasConstantValue()))
1536 for (
unsigned u = 0, e =
P->getNumIncomingValues(); u < e; ++u) {
1537 Value *IncValue =
P->getIncomingValue(u);
1539 if (IncValue ==
P)
continue;
1546 RecQ.CxtI =
P->getIncomingBlock(u)->getTerminator();
1561 if (
match(RecQ.CxtI,
1565 if ((TrueSucc ==
P->getParent()) != (FalseSucc ==
P->getParent())) {
1567 if (FalseSucc ==
P->getParent())
1571 case CmpInst::Predicate::ICMP_EQ:
1574 case CmpInst::Predicate::ICMP_ULE:
1577 case CmpInst::Predicate::ICMP_ULT:
1597 case Instruction::Call:
1598 case Instruction::Invoke:
1603 Q.IIQ.getMetadata(cast<Instruction>(
I), LLVMContext::MD_range))
1605 if (
const Value *RV = cast<CallBase>(
I)->getReturnedArgOperand()) {
1611 switch (II->getIntrinsicID()) {
1613 case Intrinsic::abs: {
1615 bool IntMinIsPoison =
match(II->getArgOperand(1),
m_One());
1616 Known = Known2.
abs(IntMinIsPoison);
1619 case Intrinsic::bitreverse:
1624 case Intrinsic::bswap:
1629 case Intrinsic::ctlz: {
1635 PossibleLZ = std::min(PossibleLZ,
BitWidth - 1);
1640 case Intrinsic::cttz: {
1646 PossibleTZ = std::min(PossibleTZ,
BitWidth - 1);
1651 case Intrinsic::ctpop: {
1662 case Intrinsic::fshr:
1663 case Intrinsic::fshl: {
1670 if (II->getIntrinsicID() == Intrinsic::fshr)
1683 case Intrinsic::uadd_sat:
1684 case Intrinsic::usub_sat: {
1685 bool IsAdd = II->getIntrinsicID() == Intrinsic::uadd_sat;
1692 unsigned LeadingKnown;
1701 IsAdd,
false, Known, Known2);
1714 case Intrinsic::umin:
1719 case Intrinsic::umax:
1724 case Intrinsic::smin:
1729 case Intrinsic::smax:
1734 case Intrinsic::x86_sse42_crc32_64_64:
1737 case Intrinsic::riscv_vsetvli:
1738 case Intrinsic::riscv_vsetvli_opt:
1739 case Intrinsic::riscv_vsetvlimax:
1740 case Intrinsic::riscv_vsetvlimax_opt:
1746 case Intrinsic::vscale: {
1747 if (!II->getParent() || !II->getFunction() ||
1748 !II->getFunction()->hasFnAttribute(Attribute::VScaleRange))
1751 auto Attr = II->getFunction()->getFnAttribute(Attribute::VScaleRange);
1752 std::optional<unsigned> VScaleMax = Attr.getVScaleRangeMax();
1757 unsigned VScaleMin = Attr.getVScaleRangeMin();
1761 if (VScaleMin == VScaleMax) {
1762 Known.
One = VScaleMin;
1763 Known.
Zero = VScaleMin;
1777 case Instruction::ShuffleVector: {
1778 auto *Shuf = dyn_cast<ShuffleVectorInst>(
I);
1786 APInt DemandedLHS, DemandedRHS;
1793 if (!!DemandedLHS) {
1794 const Value *
LHS = Shuf->getOperand(0);
1800 if (!!DemandedRHS) {
1801 const Value *
RHS = Shuf->getOperand(1);
1807 case Instruction::InsertElement: {
1808 if (isa<ScalableVectorType>(
I->getType())) {
1812 const Value *Vec =
I->getOperand(0);
1813 const Value *Elt =
I->getOperand(1);
1814 auto *CIdx = dyn_cast<ConstantInt>(
I->getOperand(2));
1817 if (!CIdx || CIdx->getValue().uge(NumElts)) {
1823 unsigned EltIdx = CIdx->getZExtValue();
1825 if (DemandedElts[EltIdx]) {
1832 APInt DemandedVecElts = DemandedElts;
1834 if (!!DemandedVecElts) {
1840 case Instruction::ExtractElement: {
1843 const Value *Vec =
I->getOperand(0);
1845 auto *CIdx = dyn_cast<ConstantInt>(
Idx);
1846 if (isa<ScalableVectorType>(Vec->
getType())) {
1851 unsigned NumElts = cast<FixedVectorType>(Vec->
getType())->getNumElements();
1853 if (CIdx && CIdx->getValue().ult(NumElts))
1858 case Instruction::ExtractValue:
1859 if (
IntrinsicInst *II = dyn_cast<IntrinsicInst>(
I->getOperand(0))) {
1863 switch (II->getIntrinsicID()) {
1865 case Intrinsic::uadd_with_overflow:
1866 case Intrinsic::sadd_with_overflow:
1868 II->getArgOperand(1),
false, DemandedElts,
1869 Known, Known2,
Depth, Q);
1871 case Intrinsic::usub_with_overflow:
1872 case Intrinsic::ssub_with_overflow:
1874 II->getArgOperand(1),
false, DemandedElts,
1875 Known, Known2,
Depth, Q);
1877 case Intrinsic::umul_with_overflow:
1878 case Intrinsic::smul_with_overflow:
1880 DemandedElts, Known, Known2,
Depth, Q);
1886 case Instruction::Freeze:
1897 unsigned Depth,
const Query &Q) {
1928 if (!DemandedElts) {
1934 assert(V &&
"No Value?");
1942 "Not integer or pointer type!");
1944 if (
auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
1946 FVTy->getNumElements() == DemandedElts.
getBitWidth() &&
1947 "DemandedElt width should equal the fixed vector number of elements");
1950 "DemandedElt width should be 1 for scalars or scalable vectors");
1956 "V and Known should have same BitWidth");
1959 "V and Known should have same BitWidth");
1970 if (isa<ConstantPointerNull>(V) || isa<ConstantAggregateZero>(V)) {
1981 for (
unsigned i = 0, e = CDV->getNumElements(); i != e; ++i) {
1982 if (!DemandedElts[i])
1984 APInt Elt = CDV->getElementAsAPInt(i);
1991 if (
const auto *CV = dyn_cast<ConstantVector>(V)) {
1996 for (
unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
1997 if (!DemandedElts[i])
2000 auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element);
2005 const APInt &Elt = ElementCI->getValue();
2016 if (isa<UndefValue>(V))
2021 assert(!isa<ConstantData>(V) &&
"Unhandled constant data!");
2029 if (
const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
2030 if (!GA->isInterposable())
2035 if (
const Operator *
I = dyn_cast<Operator>(V))
2039 if (isa<PointerType>(V->
getType())) {
2050 assert((Known.
Zero & Known.
One) == 0 &&
"Bits known to be one AND zero?");
2056 unsigned Depth, Query &Q) {
2058 Value *Start =
nullptr, *Step =
nullptr;
2064 if (U.get() == Start) {
2080 case Instruction::Mul:
2082 return (OrZero || Q.IIQ.hasNoUnsignedWrap(BO) ||
2083 Q.IIQ.hasNoSignedWrap(BO)) &&
2085 case Instruction::SDiv:
2091 case Instruction::UDiv:
2095 return (OrZero || Q.IIQ.isExact(BO)) &&
2097 case Instruction::Shl:
2098 return OrZero || Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO);
2099 case Instruction::AShr:
2103 case Instruction::LShr:
2104 return OrZero || Q.IIQ.isExact(BO);
2138 Value *
X =
nullptr, *
Y =
nullptr;
2145 if (
const ZExtInst *ZI = dyn_cast<ZExtInst>(V))
2173 if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO) ||
2174 Q.IIQ.hasNoSignedWrap(VOBO)) {
2193 if ((~(LHSBits.
Zero & RHSBits.
Zero)).isPowerOf2())
2203 if (
const PHINode *PN = dyn_cast<PHINode>(V)) {
2220 RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
2221 return isKnownToBeAPowerOfTwo(U.get(), OrZero, NewDepth, RecQ);
2247 F =
I->getFunction();
2249 if (!
GEP->isInBounds() ||
2254 assert(
GEP->getType()->isPointerTy() &&
"We only support plain pointer GEP");
2265 GTI != GTE; ++GTI) {
2267 if (
StructType *STy = GTI.getStructTypeOrNull()) {
2268 ConstantInt *OpC = cast<ConstantInt>(GTI.getOperand());
2272 if (ElementOffset > 0)
2278 if (Q.DL.getTypeAllocSize(GTI.getIndexedType()).isZero())
2283 if (
ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand())) {
2307 if (isa<Constant>(V))
2313 unsigned NumUsesExplored = 0;
2314 for (
const auto *U : V->
users()) {
2322 if (
const auto *CB = dyn_cast<CallBase>(U))
2323 if (
auto *CalledFunc = CB->getCalledFunction())
2325 if (CB->getArgOperand(
Arg.getArgNo()) == V &&
2326 Arg.hasNonNullAttr(
false) &&
2347 NonNullIfTrue =
true;
2349 NonNullIfTrue =
false;
2355 for (
const auto *CmpU : U->users()) {
2357 if (Visited.
insert(CmpU).second)
2360 while (!WorkList.
empty()) {
2369 for (
const auto *CurrU : Curr->users())
2370 if (Visited.
insert(CurrU).second)
2375 if (
const BranchInst *BI = dyn_cast<BranchInst>(Curr)) {
2376 assert(BI->isConditional() &&
"uses a comparison!");
2379 BI->getSuccessor(NonNullIfTrue ? 0 : 1);
2383 }
else if (NonNullIfTrue &&
isGuard(Curr) &&
2384 DT->
dominates(cast<Instruction>(Curr), CtxI)) {
2398 const unsigned NumRanges = Ranges->getNumOperands() / 2;
2400 for (
unsigned i = 0; i < NumRanges; ++i) {
2402 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 0));
2404 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 1));
2406 if (Range.contains(
Value))
2416 Value *Start =
nullptr, *Step =
nullptr;
2417 const APInt *StartC, *StepC;
2423 case Instruction::Add:
2429 case Instruction::Mul:
2432 case Instruction::Shl:
2434 case Instruction::AShr:
2435 case Instruction::LShr:
2455 if (
auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
2457 FVTy->getNumElements() == DemandedElts.
getBitWidth() &&
2458 "DemandedElt width should equal the fixed vector number of elements");
2461 "DemandedElt width should be 1 for scalars");
2465 if (
auto *
C = dyn_cast<Constant>(V)) {
2466 if (
C->isNullValue())
2468 if (isa<ConstantInt>(
C))
2474 if (
auto *VecTy = dyn_cast<FixedVectorType>(
C->getType())) {
2475 for (
unsigned i = 0, e = VecTy->getNumElements(); i != e; ++i) {
2476 if (!DemandedElts[i])
2478 Constant *Elt =
C->getAggregateElement(i);
2481 if (!isa<UndefValue>(Elt) && !isa<ConstantInt>(Elt))
2490 if (
const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
2491 if (!GV->isAbsoluteSymbolRef() && !GV->hasExternalWeakLinkage() &&
2492 GV->getType()->getAddressSpace() == 0)
2497 if (!isa<ConstantExpr>(V))
2501 if (
auto *
I = dyn_cast<Instruction>(V)) {
2502 if (
MDNode *Ranges = Q.IIQ.getMetadata(
I, LLVMContext::MD_range)) {
2505 if (
auto *Ty = dyn_cast<IntegerType>(V->
getType())) {
2506 const APInt ZeroValue(Ty->getBitWidth(), 0);
2524 if (isa<AllocaInst>(V) && Q.DL.getAllocaAddrSpace() == 0)
2529 if (
const Argument *
A = dyn_cast<Argument>(V)) {
2530 if (((
A->hasPassPointeeByValueCopyAttr() &&
2532 A->hasNonNullAttr()))
2537 if (
const LoadInst *LI = dyn_cast<LoadInst>(V))
2538 if (Q.IIQ.getMetadata(LI, LLVMContext::MD_nonnull))
2541 if (
const auto *Call = dyn_cast<CallBase>(V)) {
2542 if (Call->isReturnNonNull())
2549 if (!isa<Constant>(V) &&
2553 const Operator *
I = dyn_cast<Operator>(V);
2558 switch (
I->getOpcode()) {
2559 case Instruction::GetElementPtr:
2560 if (
I->getType()->isPointerTy())
2563 case Instruction::BitCast:
2564 if (
I->getType()->isPointerTy())
2567 case Instruction::IntToPtr:
2571 if (!isa<ScalableVectorType>(
I->getType()) &&
2572 Q.DL.getTypeSizeInBits(
I->getOperand(0)->getType()).getFixedValue() <=
2573 Q.DL.getTypeSizeInBits(
I->getType()).getFixedValue())
2576 case Instruction::PtrToInt:
2579 if (!isa<ScalableVectorType>(
I->getType()) &&
2580 Q.DL.getTypeSizeInBits(
I->getOperand(0)->getType()).getFixedValue() <=
2581 Q.DL.getTypeSizeInBits(
I->getType()).getFixedValue())
2584 case Instruction::Or:
2588 case Instruction::SExt:
2589 case Instruction::ZExt:
2593 case Instruction::Shl: {
2596 if (Q.IIQ.hasNoUnsignedWrap(BO))
2607 case Instruction::LShr:
2608 case Instruction::AShr: {
2624 if (
ConstantInt *Shift = dyn_cast<ConstantInt>(
I->getOperand(1))) {
2625 auto ShiftVal = Shift->getLimitedValue(
BitWidth - 1);
2635 case Instruction::UDiv:
2636 case Instruction::SDiv:
2638 if (cast<PossiblyExactOperator>(
I)->isExact())
2641 case Instruction::Add: {
2678 case Instruction::Mul: {
2682 if ((Q.IIQ.hasNoSignedWrap(BO) || Q.IIQ.hasNoUnsignedWrap(BO)) &&
2688 case Instruction::Select:
2694 case Instruction::PHI: {
2695 auto *PN = cast<PHINode>(
I);
2705 RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
2706 return isKnownNonZero(U.get(), DemandedElts, NewDepth, RecQ);
2709 case Instruction::ExtractElement:
2710 if (
const auto *EEI = dyn_cast<ExtractElementInst>(V)) {
2711 const Value *Vec = EEI->getVectorOperand();
2712 const Value *
Idx = EEI->getIndexOperand();
2713 auto *CIdx = dyn_cast<ConstantInt>(
Idx);
2714 if (
auto *VecTy = dyn_cast<FixedVectorType>(Vec->
getType())) {
2715 unsigned NumElts = VecTy->getNumElements();
2717 if (CIdx && CIdx->getValue().ult(NumElts))
2723 case Instruction::Freeze:
2727 case Instruction::Call:
2735 return Known.
One != 0;
2739 auto *FVTy = dyn_cast<FixedVectorType>(V->
getType());
2740 APInt DemandedElts =
2751static std::optional<std::pair<Value*, Value*>>
2755 return std::nullopt;
2764 case Instruction::Add:
2765 case Instruction::Sub:
2771 case Instruction::Mul: {
2775 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
2776 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
2777 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
2778 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
2784 !cast<ConstantInt>(Op1->
getOperand(1))->isZero())
2788 case Instruction::Shl: {
2791 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
2792 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
2793 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
2794 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
2801 case Instruction::AShr:
2802 case Instruction::LShr: {
2803 auto *PEO1 = cast<PossiblyExactOperator>(Op1);
2804 auto *PEO2 = cast<PossiblyExactOperator>(Op2);
2805 if (!PEO1->isExact() || !PEO2->isExact())
2812 case Instruction::SExt:
2813 case Instruction::ZExt:
2817 case Instruction::PHI: {
2818 const PHINode *PN1 = cast<PHINode>(Op1);
2819 const PHINode *PN2 = cast<PHINode>(Op2);
2825 Value *Start1 =
nullptr, *Step1 =
nullptr;
2827 Value *Start2 =
nullptr, *Step2 =
nullptr;
2834 cast<Operator>(BO2));
2843 if (Values->first != PN1 || Values->second != PN2)
2846 return std::make_pair(Start1, Start2);
2849 return std::nullopt;
2856 if (!BO || BO->
getOpcode() != Instruction::Add)
2858 Value *Op =
nullptr;
2872 if (
auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
2875 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
2885 if (
auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
2888 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
2895 unsigned Depth,
const Query &Q) {
2901 bool UsedFullRecursion =
false;
2903 if (!VisitedBBs.
insert(IncomBB).second)
2907 const APInt *C1, *C2;
2912 if (UsedFullRecursion)
2916 RecQ.CxtI = IncomBB->getTerminator();
2919 UsedFullRecursion =
true;
2929 if (V1->
getType() != V2->getType())
2939 auto *O1 = dyn_cast<Operator>(V1);
2940 auto *O2 = dyn_cast<Operator>(V2);
2941 if (O1 && O2 && O1->getOpcode() == O2->getOpcode()) {
2945 if (
const PHINode *PN1 = dyn_cast<PHINode>(V1)) {
2946 const PHINode *PN2 = cast<PHINode>(V2);
2989 return Mask.isSubsetOf(Known.
Zero);
2998 "Input should be a Select!");
3008 const Value *LHS2 =
nullptr, *RHS2 =
nullptr;
3020 return CLow->
sle(*CHigh);
3025 const APInt *&CHigh) {
3027 II->
getIntrinsicID() == Intrinsic::smax) &&
"Must be smin/smax");
3030 auto *InnerII = dyn_cast<IntrinsicInst>(II->
getArgOperand(0));
3031 if (!InnerII || InnerII->getIntrinsicID() != InverseID ||
3038 return CLow->
sle(*CHigh);
3046 const APInt &DemandedElts,
3048 const auto *CV = dyn_cast<Constant>(V);
3049 if (!CV || !isa<FixedVectorType>(CV->getType()))
3052 unsigned MinSignBits = TyBits;
3053 unsigned NumElts = cast<FixedVectorType>(CV->getType())->getNumElements();
3054 for (
unsigned i = 0; i != NumElts; ++i) {
3055 if (!DemandedElts[i])
3058 auto *Elt = dyn_cast_or_null<ConstantInt>(CV->getAggregateElement(i));
3062 MinSignBits = std::min(MinSignBits, Elt->getValue().getNumSignBits());
3069 const APInt &DemandedElts,
3070 unsigned Depth,
const Query &Q);
3073 unsigned Depth,
const Query &Q) {
3075 assert(Result > 0 &&
"At least one sign bit needs to be present!");
3087 const APInt &DemandedElts,
3088 unsigned Depth,
const Query &Q) {
3093 if (
auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
3095 FVTy->getNumElements() == DemandedElts.
getBitWidth() &&
3096 "DemandedElt width should equal the fixed vector number of elements");
3099 "DemandedElt width should be 1 for scalars");
3109 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
3110 Q.DL.getTypeSizeInBits(ScalarTy);
3113 unsigned FirstAnswer = 1;
3121 if (
auto *U = dyn_cast<Operator>(V)) {
3124 case Instruction::SExt:
3125 Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits();
3128 case Instruction::SDiv: {
3129 const APInt *Denominator;
3141 return std::min(TyBits, NumBits + Denominator->
logBase2());
3146 case Instruction::SRem: {
3149 const APInt *Denominator;
3170 unsigned ResBits = TyBits - Denominator->
ceilLogBase2();
3171 Tmp = std::max(Tmp, ResBits);
3177 case Instruction::AShr: {
3182 if (ShAmt->
uge(TyBits))
3185 Tmp += ShAmtLimited;
3186 if (Tmp > TyBits) Tmp = TyBits;
3190 case Instruction::Shl: {
3195 if (ShAmt->
uge(TyBits) ||
3196 ShAmt->
uge(Tmp))
break;
3202 case Instruction::And:
3203 case Instruction::Or:
3204 case Instruction::Xor:
3209 FirstAnswer = std::min(Tmp, Tmp2);
3216 case Instruction::Select: {
3220 const APInt *CLow, *CHigh;
3225 if (Tmp == 1)
break;
3227 return std::min(Tmp, Tmp2);
3230 case Instruction::Add:
3234 if (Tmp == 1)
break;
3237 if (
const auto *CRHS = dyn_cast<Constant>(U->getOperand(1)))
3238 if (CRHS->isAllOnesValue()) {
3244 if ((Known.
Zero | 1).isAllOnes())
3254 if (Tmp2 == 1)
break;
3255 return std::min(Tmp, Tmp2) - 1;
3257 case Instruction::Sub:
3259 if (Tmp2 == 1)
break;
3262 if (
const auto *CLHS = dyn_cast<Constant>(U->getOperand(0)))
3263 if (CLHS->isNullValue()) {
3268 if ((Known.
Zero | 1).isAllOnes())
3283 if (Tmp == 1)
break;
3284 return std::min(Tmp, Tmp2) - 1;
3286 case Instruction::Mul: {
3290 if (SignBitsOp0 == 1)
break;
3292 if (SignBitsOp1 == 1)
break;
3293 unsigned OutValidBits =
3294 (TyBits - SignBitsOp0 + 1) + (TyBits - SignBitsOp1 + 1);
3295 return OutValidBits > TyBits ? 1 : TyBits - OutValidBits + 1;
3298 case Instruction::PHI: {
3299 const PHINode *PN = cast<PHINode>(U);
3302 if (NumIncomingValues > 4)
break;
3304 if (NumIncomingValues == 0)
break;
3310 for (
unsigned i = 0, e = NumIncomingValues; i != e; ++i) {
3311 if (Tmp == 1)
return Tmp;
3319 case Instruction::Trunc: {
3324 unsigned OperandTyBits = U->getOperand(0)->getType()->getScalarSizeInBits();
3325 if (Tmp > (OperandTyBits - TyBits))
3326 return Tmp - (OperandTyBits - TyBits);
3331 case Instruction::ExtractElement:
3338 case Instruction::ShuffleVector: {
3341 auto *Shuf = dyn_cast<ShuffleVectorInst>(U);
3346 APInt DemandedLHS, DemandedRHS;
3351 Tmp = std::numeric_limits<unsigned>::max();
3352 if (!!DemandedLHS) {
3353 const Value *
LHS = Shuf->getOperand(0);
3360 if (!!DemandedRHS) {
3361 const Value *
RHS = Shuf->getOperand(1);
3363 Tmp = std::min(Tmp, Tmp2);
3369 assert(Tmp <= TyBits &&
"Failed to determine minimum sign bits");
3372 case Instruction::Call: {
3373 if (
const auto *II = dyn_cast<IntrinsicInst>(U)) {
3374 switch (II->getIntrinsicID()) {
3376 case Intrinsic::abs:
3378 if (Tmp == 1)
break;
3382 case Intrinsic::smin:
3383 case Intrinsic::smax: {
3384 const APInt *CLow, *CHigh;
3399 if (
unsigned VecSignBits =
3417 if (
F->isIntrinsic())
3418 return F->getIntrinsicID();
3424 if (
F->hasLocalLinkage() || !TLI || !TLI->
getLibFunc(CB, Func) ||
3434 return Intrinsic::sin;
3438 return Intrinsic::cos;
3442 return Intrinsic::exp;
3446 return Intrinsic::exp2;
3450 return Intrinsic::log;
3452 case LibFunc_log10f:
3453 case LibFunc_log10l:
3454 return Intrinsic::log10;
3458 return Intrinsic::log2;
3462 return Intrinsic::fabs;
3466 return Intrinsic::minnum;
3470 return Intrinsic::maxnum;
3471 case LibFunc_copysign:
3472 case LibFunc_copysignf:
3473 case LibFunc_copysignl:
3474 return Intrinsic::copysign;
3476 case LibFunc_floorf:
3477 case LibFunc_floorl:
3478 return Intrinsic::floor;
3482 return Intrinsic::ceil;
3484 case LibFunc_truncf:
3485 case LibFunc_truncl:
3486 return Intrinsic::trunc;
3490 return Intrinsic::rint;
3491 case LibFunc_nearbyint:
3492 case LibFunc_nearbyintf:
3493 case LibFunc_nearbyintl:
3494 return Intrinsic::nearbyint;
3496 case LibFunc_roundf:
3497 case LibFunc_roundl:
3498 return Intrinsic::round;
3499 case LibFunc_roundeven:
3500 case LibFunc_roundevenf:
3501 case LibFunc_roundevenl:
3502 return Intrinsic::roundeven;
3506 return Intrinsic::pow;
3510 return Intrinsic::sqrt;
3523 if (
auto *CFP = dyn_cast<ConstantFP>(V))
3524 return !CFP->getValueAPF().isNegZero();
3529 auto *Op = dyn_cast<Operator>(V);
3538 if (isa<SIToFPInst>(Op) || isa<UIToFPInst>(Op))
3541 if (
auto *Call = dyn_cast<CallInst>(Op)) {
3547 case Intrinsic::sqrt:
3548 case Intrinsic::canonicalize:
3550 case Intrinsic::experimental_constrained_sqrt: {
3552 const auto *CI = cast<ConstrainedFPIntrinsic>(Call);
3553 if (CI->getRoundingMode() == RoundingMode::NearestTiesToEven)
3559 case Intrinsic::fabs:
3562 case Intrinsic::experimental_constrained_sitofp:
3563 case Intrinsic::experimental_constrained_uitofp:
3583 if (
const ConstantFP *CFP = dyn_cast<ConstantFP>(V)) {
3584 return !CFP->getValueAPF().isNegative() ||
3585 (!SignBitOnly && CFP->getValueAPF().isZero());
3589 if (
auto *CV = dyn_cast<Constant>(V)) {
3590 if (
auto *CVFVTy = dyn_cast<FixedVectorType>(CV->getType())) {
3591 unsigned NumElts = CVFVTy->getNumElements();
3592 for (
unsigned i = 0; i != NumElts; ++i) {
3593 auto *CFP = dyn_cast_or_null<ConstantFP>(CV->getAggregateElement(i));
3596 if (CFP->getValueAPF().isNegative() &&
3597 (SignBitOnly || !CFP->getValueAPF().isZero()))
3609 const Operator *
I = dyn_cast<Operator>(V);
3613 switch (
I->getOpcode()) {
3617 case Instruction::UIToFP:
3619 case Instruction::FDiv:
3621 if (
I->getOperand(0) ==
I->getOperand(1) &&
3622 (!SignBitOnly || cast<FPMathOperator>(
I)->hasNoNaNs()))
3630 case Instruction::FMul:
3632 if (
I->getOperand(0) ==
I->getOperand(1) &&
3633 (!SignBitOnly || cast<FPMathOperator>(
I)->hasNoNaNs()))
3637 case Instruction::FAdd:
3638 case Instruction::FRem:
3643 case Instruction::Select:
3648 case Instruction::FPExt:
3649 case Instruction::FPTrunc:
3653 case Instruction::ExtractElement:
3659 case Instruction::Call:
3660 const auto *CI = cast<CallInst>(
I);
3665 case Intrinsic::canonicalize:
3666 case Intrinsic::arithmetic_fence:
3667 case Intrinsic::floor:
3668 case Intrinsic::ceil:
3669 case Intrinsic::trunc:
3670 case Intrinsic::rint:
3671 case Intrinsic::nearbyint:
3672 case Intrinsic::round:
3673 case Intrinsic::roundeven:
3674 case Intrinsic::fptrunc_round:
3676 case Intrinsic::maxnum: {
3677 Value *V0 =
I->getOperand(0), *V1 =
I->getOperand(1);
3678 auto isPositiveNum = [&](
Value *V) {
3696 return isPositiveNum(V0) || isPositiveNum(V1);
3699 case Intrinsic::maximum:
3704 case Intrinsic::minnum:
3705 case Intrinsic::minimum:
3710 case Intrinsic::exp:
3711 case Intrinsic::exp2:
3712 case Intrinsic::fabs:
3714 case Intrinsic::copysign:
3718 case Intrinsic::sqrt:
3722 return CI->hasNoNaNs() && (CI->hasNoSignedZeros() ||
3725 case Intrinsic::powi:
3747 case Intrinsic::fma:
3748 case Intrinsic::fmuladd:
3750 return I->getOperand(0) ==
I->getOperand(1) &&
3751 (!SignBitOnly || cast<FPMathOperator>(
I)->hasNoNaNs()) &&
3774 if (
auto *FPMathOp = dyn_cast<FPMathOperator>(V))
3775 if (FPMathOp->hasNoInfs())
3779 if (
auto *CFP = dyn_cast<ConstantFP>(V))
3780 return !CFP->isInfinity();
3785 if (
auto *Inst = dyn_cast<Instruction>(V)) {
3786 switch (Inst->getOpcode()) {
3787 case Instruction::Select: {
3791 case Instruction::SIToFP:
3792 case Instruction::UIToFP: {
3796 int IntSize = Inst->getOperand(0)->getType()->getScalarSizeInBits();
3797 if (Inst->getOpcode() == Instruction::SIToFP)
3805 case Instruction::FNeg:
3806 case Instruction::FPExt: {
3810 case Instruction::FPTrunc: {
3818 if (
const auto *II = dyn_cast<IntrinsicInst>(V)) {
3819 switch (II->getIntrinsicID()) {
3820 case Intrinsic::sin:
3821 case Intrinsic::cos:
3824 case Intrinsic::fabs:
3825 case Intrinsic::sqrt:
3826 case Intrinsic::canonicalize:
3827 case Intrinsic::copysign:
3828 case Intrinsic::arithmetic_fence:
3829 case Intrinsic::trunc:
3831 case Intrinsic::floor:
3832 case Intrinsic::ceil:
3833 case Intrinsic::rint:
3834 case Intrinsic::nearbyint:
3835 case Intrinsic::round:
3836 case Intrinsic::roundeven:
3841 case Intrinsic::fptrunc_round:
3844 case Intrinsic::minnum:
3845 case Intrinsic::maxnum:
3846 case Intrinsic::minimum:
3847 case Intrinsic::maximum:
3850 case Intrinsic::log:
3851 case Intrinsic::log10:
3852 case Intrinsic::log2:
3859 case Intrinsic::exp:
3860 case Intrinsic::exp2:
3861 case Intrinsic::pow:
3862 case Intrinsic::powi:
3863 case Intrinsic::fma:
3864 case Intrinsic::fmuladd:
3875 auto *VFVTy = dyn_cast<FixedVectorType>(V->
getType());
3876 if (VFVTy && isa<Constant>(V)) {
3878 unsigned NumElts = VFVTy->getNumElements();
3879 for (
unsigned i = 0; i != NumElts; ++i) {
3883 if (isa<UndefValue>(Elt))
3885 auto *CElt = dyn_cast<ConstantFP>(Elt);
3886 if (!CElt || CElt->isInfinity())
3902 if (
auto *FPMathOp = dyn_cast<FPMathOperator>(V))
3903 if (FPMathOp->hasNoNaNs())
3907 if (
auto *CFP = dyn_cast<ConstantFP>(V))
3908 return !CFP->isNaN();
3913 if (
auto *Inst = dyn_cast<Instruction>(V)) {
3914 switch (Inst->getOpcode()) {
3915 case Instruction::FAdd:
3916 case Instruction::FSub:
3923 case Instruction::FMul:
3931 case Instruction::FDiv:
3932 case Instruction::FRem:
3936 case Instruction::Select: {
3940 case Instruction::SIToFP:
3941 case Instruction::UIToFP:
3943 case Instruction::FPTrunc:
3944 case Instruction::FPExt:
3945 case Instruction::FNeg:
3952 if (
const auto *II = dyn_cast<IntrinsicInst>(V)) {
3953 switch (II->getIntrinsicID()) {
3954 case Intrinsic::canonicalize:
3955 case Intrinsic::fabs:
3956 case Intrinsic::copysign:
3957 case Intrinsic::exp:
3958 case Intrinsic::exp2:
3959 case Intrinsic::floor:
3960 case Intrinsic::ceil:
3961 case Intrinsic::trunc:
3962 case Intrinsic::rint:
3963 case Intrinsic::nearbyint:
3964 case Intrinsic::round:
3965 case Intrinsic::roundeven:
3966 case Intrinsic::arithmetic_fence:
3968 case Intrinsic::sqrt:
3971 case Intrinsic::minnum:
3972 case Intrinsic::maxnum:
3982 auto *VFVTy = dyn_cast<FixedVectorType>(V->
getType());
3983 if (VFVTy && isa<Constant>(V)) {
3985 unsigned NumElts = VFVTy->getNumElements();
3986 for (
unsigned i = 0; i != NumElts; ++i) {
3990 if (isa<UndefValue>(Elt))
3992 auto *CElt = dyn_cast<ConstantFP>(Elt);
3993 if (!CElt || CElt->isNaN())
4014 if (isa<UndefValue>(V))
4018 if (!
DL.getTypeStoreSize(V->
getType()).isNonZero())
4033 if (
C->isNullValue())
4040 if (CFP->getType()->isHalfTy())
4042 else if (CFP->getType()->isFloatTy())
4044 else if (CFP->getType()->isDoubleTy())
4053 if (CI->getBitWidth() % 8 == 0) {
4054 assert(CI->getBitWidth() > 8 &&
"8 bits should be handled above!");
4055 if (!CI->getValue().isSplat(8))
4061 if (
auto *CE = dyn_cast<ConstantExpr>(
C)) {
4062 if (CE->getOpcode() == Instruction::IntToPtr) {
4063 if (
auto *PtrTy = dyn_cast<PointerType>(CE->getType())) {
4064 unsigned BitWidth =
DL.getPointerSizeInBits(PtrTy->getAddressSpace());
4078 if (
LHS == UndefInt8)
4080 if (
RHS == UndefInt8)
4086 Value *Val = UndefInt8;
4087 for (
unsigned I = 0,
E = CA->getNumElements();
I !=
E; ++
I)
4093 if (isa<ConstantAggregate>(
C)) {
4094 Value *Val = UndefInt8;
4095 for (
unsigned I = 0,
E =
C->getNumOperands();
I !=
E; ++
I)
4115 StructType *STy = dyn_cast<StructType>(IndexedType);
4129 while (PrevTo != OrigTo) {
4172 assert(InsertBefore &&
"Must have someplace to insert!");
4177 unsigned IdxSkip = Idxs.
size();
4192 if (idx_range.
empty())
4196 "Not looking at a struct or array?");
4198 "Invalid indices for type?");
4200 if (
Constant *
C = dyn_cast<Constant>(V)) {
4201 C =
C->getAggregateElement(idx_range[0]);
4202 if (!
C)
return nullptr;
4209 const unsigned *req_idx = idx_range.
begin();
4210 for (
const unsigned *i =
I->idx_begin(), *e =
I->idx_end();
4211 i != e; ++i, ++req_idx) {
4212 if (req_idx == idx_range.
end()) {
4242 ArrayRef(req_idx, idx_range.
end()), InsertBefore);
4251 unsigned size =
I->getNumIndices() + idx_range.
size();
4256 Idxs.
append(
I->idx_begin(),
I->idx_end());
4262 &&
"Number of indices added not correct?");
4272 unsigned CharSize) {
4274 if (
GEP->getNumOperands() != 3)
4279 ArrayType *AT = dyn_cast<ArrayType>(
GEP->getSourceElementType());
4285 const ConstantInt *FirstIdx = dyn_cast<ConstantInt>(
GEP->getOperand(1));
4286 if (!FirstIdx || !FirstIdx->
isZero())
4300 assert(V &&
"V should not be null.");
4301 assert((ElementSize % 8) == 0 &&
4302 "ElementSize expected to be a multiple of the size of a byte.");
4303 unsigned ElementSizeInBytes = ElementSize / 8;
4322 uint64_t StartIdx = Off.getLimitedValue();
4329 if ((StartIdx % ElementSizeInBytes) != 0)
4332 Offset += StartIdx / ElementSizeInBytes;
4338 uint64_t SizeInBytes =
DL.getTypeStoreSize(GVTy).getFixedValue();
4341 Slice.
Array =
nullptr;
4352 if (
auto *ArrayInit = dyn_cast<ConstantDataArray>(
Init)) {
4353 Type *InitElTy = ArrayInit->getElementType();
4358 ArrayTy = ArrayInit->getType();
4363 if (ElementSize != 8)
4374 Array = dyn_cast<ConstantDataArray>(
Init);
4375 ArrayTy = dyn_cast<ArrayType>(
Init->getType());
4382 Slice.
Array = Array;
4398 if (Slice.
Array ==
nullptr) {
4421 Str = Str.substr(Slice.
Offset);
4427 Str = Str.substr(0, Str.find(
'\0'));
4440 unsigned CharSize) {
4446 if (
const PHINode *PN = dyn_cast<PHINode>(V)) {
4447 if (!PHIs.
insert(PN).second)
4452 for (
Value *IncValue : PN->incoming_values()) {
4454 if (Len == 0)
return 0;
4456 if (Len == ~0ULL)
continue;
4458 if (Len != LenSoFar && LenSoFar != ~0ULL)
4470 if (Len1 == 0)
return 0;
4472 if (Len2 == 0)
return 0;
4473 if (Len1 == ~0ULL)
return Len2;
4474 if (Len2 == ~0ULL)
return Len1;
4475 if (Len1 != Len2)
return 0;
4484 if (Slice.
Array ==
nullptr)
4492 unsigned NullIndex = 0;
4493 for (
unsigned E = Slice.
Length; NullIndex <
E; ++NullIndex) {
4498 return NullIndex + 1;
4511 return Len == ~0ULL ? 1 : Len;
4516 bool MustPreserveNullness) {
4518 "getArgumentAliasingToReturnedPointer only works on nonnull calls");
4519 if (
const Value *RV = Call->getReturnedArgOperand())
4523 Call, MustPreserveNullness))
4524 return Call->getArgOperand(0);
4529 const CallBase *Call,
bool MustPreserveNullness) {
4530 switch (Call->getIntrinsicID()) {
4531 case Intrinsic::launder_invariant_group:
4532 case Intrinsic::strip_invariant_group:
4533 case Intrinsic::aarch64_irg:
4534 case Intrinsic::aarch64_tagp:
4536 case Intrinsic::ptrmask:
4537 return !MustPreserveNullness;
4554 if (!PrevValue || LI->
getLoopFor(PrevValue->getParent()) != L)
4556 if (!PrevValue || LI->
getLoopFor(PrevValue->getParent()) != L)
4564 if (
auto *Load = dyn_cast<LoadInst>(PrevValue))
4573 for (
unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) {
4574 if (
auto *
GEP = dyn_cast<GEPOperator>(V)) {
4575 V =
GEP->getPointerOperand();
4578 V = cast<Operator>(V)->getOperand(0);
4581 }
else if (
auto *GA = dyn_cast<GlobalAlias>(V)) {
4582 if (GA->isInterposable())
4584 V = GA->getAliasee();
4586 if (
auto *
PHI = dyn_cast<PHINode>(V)) {
4588 if (
PHI->getNumIncomingValues() == 1) {
4589 V =
PHI->getIncomingValue(0);
4592 }
else if (
auto *Call = dyn_cast<CallBase>(V)) {
4617 LoopInfo *LI,
unsigned MaxLookup) {
4625 if (!Visited.
insert(
P).second)
4628 if (
auto *
SI = dyn_cast<SelectInst>(
P)) {
4634 if (
auto *PN = dyn_cast<PHINode>(
P)) {
4652 }
while (!Worklist.
empty());
4659 if (
const Operator *U = dyn_cast<Operator>(V)) {
4662 if (U->getOpcode() == Instruction::PtrToInt)
4663 return U->getOperand(0);
4670 if (U->getOpcode() != Instruction::Add ||
4671 (!isa<ConstantInt>(U->getOperand(1)) &&
4673 !isa<PHINode>(U->getOperand(1))))
4675 V = U->getOperand(0);