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,17 @@ 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 +233,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 +308,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); @@ -369,16 +371,17 @@ V, Mask, Depth, Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo)); } -static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts, +static unsigned ComputeNumSignBits(const Value *V, + const Optional &DemandedElts, unsigned Depth, const Query &Q); static unsigned ComputeNumSignBits(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 ComputeNumSignBits(V, DemandedElts, Depth, Q); } @@ -391,7 +394,8 @@ } 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,9 +410,9 @@ } static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW, - const APInt &DemandedElts, KnownBits &Known, - KnownBits &Known2, unsigned Depth, - const Query &Q) { + const Optional &DemandedElts, + KnownBits &Known, KnownBits &Known2, + unsigned Depth, const Query &Q) { unsigned BitWidth = Known.getBitWidth(); computeKnownBits(Op1, DemandedElts, Known, Depth + 1, Q); computeKnownBits(Op0, DemandedElts, Known2, Depth + 1, Q); @@ -1041,7 +1045,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 +1133,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 +1751,8 @@ // 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 +1778,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 +1787,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 +1849,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 +1879,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 +1889,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(); @@ -1895,7 +1900,7 @@ (void)BitWidth; (void)ExpectedWidth; - if (!DemandedElts) { + if (DemandedElts && !*DemandedElts) { // No demanded elts, better to assume we don't know anything. Known.resetAll(); return; @@ -1917,13 +1922,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 +1938,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 +2273,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 +2295,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 +2542,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); } @@ -2633,9 +2638,8 @@ /// minimum number of sign bits. Return 0 if the value is not a vector constant /// or if any element was not analyzed; otherwise, return the count for the /// element with the minimum number of sign bits. -static unsigned computeNumSignBitsVectorConstant(const Value *V, - const APInt &DemandedElts, - unsigned TyBits) { +static unsigned computeNumSignBitsVectorConstant( + const Value *V, const Optional &DemandedElts, unsigned TyBits) { const auto *CV = dyn_cast(V); if (!CV || !CV->getType()->isVectorTy()) return 0; @@ -2643,7 +2647,7 @@ unsigned MinSignBits = TyBits; unsigned NumElts = cast(CV->getType())->getNumElements(); for (unsigned i = 0; i != NumElts; ++i) { - if (!DemandedElts[i]) + if (!(*DemandedElts)[i]) continue; // If we find a non-ConstantInt, bail out. auto *Elt = dyn_cast_or_null(CV->getAggregateElement(i)); @@ -2657,10 +2661,11 @@ } static unsigned ComputeNumSignBitsImpl(const Value *V, - const APInt &DemandedElts, + const Optional &DemandedElts, unsigned Depth, const Query &Q); -static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts, +static unsigned ComputeNumSignBits(const Value *V, + const Optional &DemandedElts, unsigned Depth, const Query &Q) { unsigned Result = ComputeNumSignBitsImpl(V, DemandedElts, Depth, Q); assert(Result > 0 && "At least one sign bit needs to be present!"); @@ -2675,7 +2680,7 @@ /// vector element with the minimum number of known sign bits of the demanded /// elements in the vector specified by DemandedElts. static unsigned ComputeNumSignBitsImpl(const Value *V, - const APInt &DemandedElts, + const Optional &DemandedElts, unsigned Depth, const Query &Q) { assert(Depth <= MaxDepth && "Limit Search Depth"); @@ -2685,8 +2690,8 @@ Type *Ty = V->getType(); 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(); @@ -2925,7 +2930,8 @@ APInt DemandedLHS, DemandedRHS; // For undef elements, we don't know anything about the common state of // the shuffle result. - if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS)) + if (!getShuffleDemandedElts(Shuf, *DemandedElts, DemandedLHS, + DemandedRHS)) return 1; Tmp = std::numeric_limits::max(); if (!!DemandedLHS) {