diff --git a/llvm/include/llvm/Analysis/ValueTracking.h b/llvm/include/llvm/Analysis/ValueTracking.h --- a/llvm/include/llvm/Analysis/ValueTracking.h +++ b/llvm/include/llvm/Analysis/ValueTracking.h @@ -62,11 +62,11 @@ /// Determine which bits of V are known to be either zero or one and return /// them in the KnownZero/KnownOne bit sets. /// - /// This function is defined on values with integer type, values with pointer - /// type, and vectors of integers. In the case - /// where V is a vector, the known zero and known one values are the - /// same width as the vector element, and the bit is set only if it is true - /// for all of the demanded elements in the vector. + /// This overload is defined on vectors of integer or pointer type. + /// DemandedElts has the width of the vector; each bit indicates whether that + /// element of the vector is demanded. The known zero and known one values + /// are the same width as the vector element, and the bit is set only if it + /// is true for all of the demanded elements in the vector. void computeKnownBits(const Value *V, const APInt &DemandedElts, KnownBits &Known, const DataLayout &DL, unsigned Depth = 0, AssumptionCache *AC = nullptr, diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp --- a/llvm/lib/Analysis/ValueTracking.cpp +++ b/llvm/lib/Analysis/ValueTracking.cpp @@ -201,16 +201,16 @@ return true; } -static void computeKnownBits(const Value *V, const APInt &DemandedElts, +static void computeKnownBits(const Value *V, const Optional &DemandedElts, KnownBits &Known, unsigned Depth, const Query &Q); static void computeKnownBits(const Value *V, KnownBits &Known, unsigned Depth, const Query &Q) { Type *Ty = V->getType(); - APInt DemandedElts = + Optional DemandedElts = Ty->isVectorTy() ? APInt::getAllOnesValue(cast(Ty)->getNumElements()) - : APInt(1, 1); + : Optional(); computeKnownBits(V, DemandedElts, Known, Depth, Q); } @@ -232,7 +232,8 @@ Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo, ORE)); } -static KnownBits computeKnownBits(const Value *V, const APInt &DemandedElts, +static KnownBits computeKnownBits(const Value *V, + const Optional &DemandedElts, unsigned Depth, const Query &Q); static KnownBits computeKnownBits(const Value *V, unsigned Depth, @@ -306,7 +307,7 @@ V, OrZero, Depth, Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo)); } -static bool isKnownNonZero(const Value *V, const APInt &DemandedElts, +static bool isKnownNonZero(const Value *V, const Optional &DemandedElts, unsigned Depth, const Query &Q); static bool isKnownNonZero(const Value *V, unsigned Depth, const Query &Q); @@ -391,7 +392,7 @@ } static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1, - bool NSW, const APInt &DemandedElts, + bool NSW, const Optional &DemandedElts, KnownBits &KnownOut, KnownBits &Known2, unsigned Depth, const Query &Q) { computeKnownBits(Op1, DemandedElts, KnownOut, Depth + 1, Q); @@ -406,7 +407,8 @@ } static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW, - const APInt &DemandedElts, KnownBits &Known, + const Optional &DemandedElts, + KnownBits &Known, KnownBits &Known2, unsigned Depth, const Query &Q) { unsigned BitWidth = Known.getBitWidth(); @@ -1041,7 +1043,7 @@ /// amount. The results from calling KZF and KOF are conservatively combined for /// all permitted shift amounts. static void computeKnownBitsFromShiftOperator( - const Operator *I, const APInt &DemandedElts, KnownBits &Known, + const Operator *I, const Optional &DemandedElts, KnownBits &Known, KnownBits &Known2, unsigned Depth, const Query &Q, function_ref KZF, function_ref KOF) { @@ -1129,7 +1131,7 @@ } static void computeKnownBitsFromOperator(const Operator *I, - const APInt &DemandedElts, + const Optional &DemandedElts, KnownBits &Known, unsigned Depth, const Query &Q) { unsigned BitWidth = Known.getBitWidth(); @@ -1747,7 +1749,7 @@ // For undef elements, we don't know anything about the common state of // the shuffle result. APInt DemandedLHS, DemandedRHS; - if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS)) { + if (!getShuffleDemandedElts(Shuf, *DemandedElts, DemandedLHS, DemandedRHS)) { Known.resetAll(); return; } @@ -1773,7 +1775,7 @@ const Value *Elt = I->getOperand(1); auto *CIdx = dyn_cast(I->getOperand(2)); // Early out if the index is non-constant or out-of-range. - unsigned NumElts = DemandedElts.getBitWidth(); + unsigned NumElts = DemandedElts->getBitWidth(); if (!CIdx || CIdx->getValue().uge(NumElts)) { Known.resetAll(); return; @@ -1782,14 +1784,14 @@ Known.Zero.setAllBits(); unsigned EltIdx = CIdx->getZExtValue(); // Do we demand the inserted element? - if (DemandedElts[EltIdx]) { + if ((*DemandedElts)[EltIdx]) { computeKnownBits(Elt, Known, Depth + 1, Q); // If we don't know any bits, early out. if (Known.isUnknown()) break; } // We don't need the base vector element that has been inserted. - APInt DemandedVecElts = DemandedElts; + APInt DemandedVecElts = *DemandedElts; DemandedVecElts.clearBit(EltIdx); if (!!DemandedVecElts) { computeKnownBits(Vec, DemandedVecElts, Known2, Depth + 1, Q); @@ -1844,7 +1846,7 @@ /// Determine which bits of V are known to be either zero or one and return /// them. -KnownBits computeKnownBits(const Value *V, const APInt &DemandedElts, +KnownBits computeKnownBits(const Value *V, const Optional &DemandedElts, unsigned Depth, const Query &Q) { KnownBits Known(getBitWidth(V->getType(), Q.DL)); computeKnownBits(V, DemandedElts, Known, Depth, Q); @@ -1874,7 +1876,7 @@ /// where V is a vector, known zero, and known one values are the /// same width as the vector element, and the bit is set only if it is true /// for all of the demanded elements in the vector specified by DemandedElts. -void computeKnownBits(const Value *V, const APInt &DemandedElts, +void computeKnownBits(const Value *V, const Optional &DemandedElts, KnownBits &Known, unsigned Depth, const Query &Q) { assert(V && "No Value?"); assert(Depth <= MaxDepth && "Limit Search Depth"); @@ -1884,8 +1886,8 @@ assert((Ty->isIntOrIntVectorTy(BitWidth) || Ty->isPtrOrPtrVectorTy()) && "Not integer or pointer type!"); assert(((Ty->isVectorTy() && cast(Ty)->getNumElements() == - DemandedElts.getBitWidth()) || - (!Ty->isVectorTy() && DemandedElts == APInt(1, 1))) && + DemandedElts->getBitWidth()) || + (!Ty->isVectorTy() && !DemandedElts)) && "Unexpected vector size"); Type *ScalarTy = Ty->getScalarType(); @@ -1917,13 +1919,13 @@ // each element. if (const ConstantDataSequential *CDS = dyn_cast(V)) { assert((!Ty->isVectorTy() || - CDS->getNumElements() == DemandedElts.getBitWidth()) && + CDS->getNumElements() == DemandedElts->getBitWidth()) && "Unexpected vector size"); // We know that CDS must be a vector of integers. Take the intersection of // each element. Known.Zero.setAllBits(); Known.One.setAllBits(); for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) { - if (Ty->isVectorTy() && !DemandedElts[i]) + if (Ty->isVectorTy() && !(*DemandedElts)[i]) continue; APInt Elt = CDS->getElementAsAPInt(i); Known.Zero &= ~Elt; @@ -1933,13 +1935,13 @@ } if (const auto *CV = dyn_cast(V)) { - assert(CV->getNumOperands() == DemandedElts.getBitWidth() && + assert(CV->getNumOperands() == DemandedElts->getBitWidth() && "Unexpected vector size"); // We know that CV must be a vector of integers. Take the intersection of // each element. Known.Zero.setAllBits(); Known.One.setAllBits(); for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) { - if (!DemandedElts[i]) + if (!(*DemandedElts)[i]) continue; Constant *Element = CV->getAggregateElement(i); auto *ElementCI = dyn_cast_or_null(Element); @@ -2268,8 +2270,8 @@ /// specified, perform context-sensitive analysis and return true if the /// pointer couldn't possibly be null at the specified instruction. /// Supports values with integer or pointer type and vectors of integers. -bool isKnownNonZero(const Value *V, const APInt &DemandedElts, unsigned Depth, - const Query &Q) { +bool isKnownNonZero(const Value *V, const Optional &DemandedElts, + unsigned Depth, const Query &Q) { if (auto *C = dyn_cast(V)) { if (C->isNullValue()) return false; @@ -2290,7 +2292,7 @@ // non-zero to determine that the whole vector is known non-zero. if (auto *VecTy = dyn_cast(C->getType())) { for (unsigned i = 0, e = VecTy->getNumElements(); i != e; ++i) { - if (!DemandedElts[i]) + if (!(*DemandedElts)[i]) continue; Constant *Elt = C->getAggregateElement(i); if (!Elt || Elt->isNullValue()) @@ -2537,10 +2539,10 @@ bool isKnownNonZero(const Value* V, unsigned Depth, const Query& Q) { Type *Ty = V->getType(); - APInt DemandedElts = + Optional DemandedElts = Ty->isVectorTy() ? APInt::getAllOnesValue(cast(Ty)->getNumElements()) - : APInt(1, 1); + : Optional(); return isKnownNonZero(V, DemandedElts, Depth, Q); }