});
if (const Expr *InitExpr = EnumConst->getInitExpr()) {
- return EnumConst->getInitVal().countTrailingOnes() ==
+ return EnumConst->getInitVal().countr_one() ==
EnumConst->getInitVal().getActiveBits() &&
isa<IntegerLiteral>(InitExpr->IgnoreImpCasts());
}
// E1 x 2^E2 module 2^N.
if (LHS.isNegative())
Info.CCEDiag(E, diag::note_constexpr_lshift_of_negative) << LHS;
- else if (LHS.countLeadingZeros() < SA)
+ else if (LHS.countl_zero() < SA)
Info.CCEDiag(E, diag::note_constexpr_lshift_discards);
}
Result = LHS << SA;
if (!Val)
return Error(E);
- return Success(Val.countLeadingZeros(), E);
+ return Success(Val.countl_zero(), E);
}
case Builtin::BI__builtin_constant_p: {
if (!Val)
return Error(E);
- return Success(Val.countTrailingZeros(), E);
+ return Success(Val.countr_zero(), E);
}
case Builtin::BI__builtin_eh_return_data_regno: {
if (!EvaluateInteger(E->getArg(0), Val, Info))
return false;
- unsigned N = Val.countTrailingZeros();
+ unsigned N = Val.countr_zero();
return Success(N == Val.getBitWidth() ? 0 : N + 1, E);
}
// wider than that, check whether it's already too big, and if so,
// overflow.
else if (numElementsWidth > sizeWidth &&
- numElementsWidth - sizeWidth > count.countLeadingZeros())
+ numElementsWidth - sizeWidth > count.countl_zero())
hasAnyOverflow = true;
// Okay, compute a count at the right width.
LitVal = 0;
for (size_t i = 0; i < NumCharsSoFar; ++i) {
// check for enough leading zeros to shift into
- multi_char_too_long |= (LitVal.countLeadingZeros() < 8);
+ multi_char_too_long |= (LitVal.countl_zero() < 8);
LitVal <<= 8;
LitVal = LitVal + (codepoint_buffer[i] & 0xFF);
}
const llvm::APSInt *LHS = SB.getKnownValue(State, C.getSVal(B->getLHS()));
const llvm::APSInt *RHS = SB.getKnownValue(State, C.getSVal(B->getRHS()));
assert(LHS && RHS && "Values unknown, inconsistent state");
- return (unsigned)RHS->getZExtValue() > LHS->countLeadingZeros();
+ return (unsigned)RHS->getZExtValue() > LHS->countl_zero();
}
void UndefResultChecker::checkPostStmt(const BinaryOperator *B,
if (V1.isSigned() && V1.isNegative())
return nullptr;
- if (V1.isSigned() && Amt > V1.countLeadingZeros())
+ if (V1.isSigned() && Amt > V1.countl_zero())
return nullptr;
}
if (isNonNegative())
return false;
// NegatedPowerOf2 - shifted mask in the top bits.
- unsigned LO = countLeadingOnes();
- unsigned TZ = countTrailingZeros();
+ unsigned LO = countl_one();
+ unsigned TZ = countr_zero();
return (LO + TZ) == BitWidth;
}
return isShiftedMask_64(U.VAL);
unsigned Ones = countPopulationSlowCase();
unsigned LeadZ = countLeadingZerosSlowCase();
- return (Ones + LeadZ + countTrailingZeros()) == BitWidth;
+ return (Ones + LeadZ + countr_zero()) == BitWidth;
}
/// Return true if this APInt value contains a non-empty sequence of ones with
/// This function returns the number of active bits which is defined as the
/// bit width minus the number of leading zeros. This is used in several
/// computations to see how "wide" the value is.
- unsigned getActiveBits() const { return BitWidth - countLeadingZeros(); }
+ unsigned getActiveBits() const { return BitWidth - countl_zero(); }
/// Compute the number of active words in the value of this APInt.
///
/// Computes the number of leading bits of this APInt that are equal to its
/// sign bit.
unsigned getNumSignBits() const {
- return isNegative() ? countLeadingOnes() : countLeadingZeros();
+ return isNegative() ? countl_one() : countl_zero();
}
/// Count the number of trailing zero bits.
KnownBits makeGE(const APInt &Val) const;
/// Returns the minimum number of trailing zero bits.
- unsigned countMinTrailingZeros() const {
- return Zero.countTrailingOnes();
- }
+ unsigned countMinTrailingZeros() const { return Zero.countr_one(); }
/// Returns the minimum number of trailing one bits.
- unsigned countMinTrailingOnes() const {
- return One.countTrailingOnes();
- }
+ unsigned countMinTrailingOnes() const { return One.countr_one(); }
/// Returns the minimum number of leading zero bits.
- unsigned countMinLeadingZeros() const {
- return Zero.countLeadingOnes();
- }
+ unsigned countMinLeadingZeros() const { return Zero.countl_one(); }
/// Returns the minimum number of leading one bits.
- unsigned countMinLeadingOnes() const {
- return One.countLeadingOnes();
- }
+ unsigned countMinLeadingOnes() const { return One.countl_one(); }
/// Returns the number of times the sign bit is replicated into the other
/// bits.
}
/// Returns the maximum number of trailing zero bits possible.
- unsigned countMaxTrailingZeros() const {
- return One.countTrailingZeros();
- }
+ unsigned countMaxTrailingZeros() const { return One.countr_zero(); }
/// Returns the maximum number of trailing one bits possible.
- unsigned countMaxTrailingOnes() const {
- return Zero.countTrailingZeros();
- }
+ unsigned countMaxTrailingOnes() const { return Zero.countr_zero(); }
/// Returns the maximum number of leading zero bits possible.
- unsigned countMaxLeadingZeros() const {
- return One.countLeadingZeros();
- }
+ unsigned countMaxLeadingZeros() const { return One.countl_zero(); }
/// Returns the maximum number of leading one bits possible.
- unsigned countMaxLeadingOnes() const {
- return Zero.countLeadingZeros();
- }
+ unsigned countMaxLeadingOnes() const { return Zero.countl_zero(); }
/// Returns the number of bits known to be one.
unsigned countMinPopulation() const { return One.popcount(); }
const APInt &Scale = Index.Scale;
APInt ScaleForGCD = Scale;
if (!Index.IsNSW)
- ScaleForGCD = APInt::getOneBitSet(Scale.getBitWidth(),
- Scale.countTrailingZeros());
+ ScaleForGCD =
+ APInt::getOneBitSet(Scale.getBitWidth(), Scale.countr_zero());
if (i == 0)
GCD = ScaleForGCD.abs();
if (!C0)
return Constant::getNullValue(Ty);
if (IntrinsicID == Intrinsic::cttz)
- return ConstantInt::get(Ty, C0->countTrailingZeros());
+ return ConstantInt::get(Ty, C0->countr_zero());
else
- return ConstantInt::get(Ty, C0->countLeadingZeros());
+ return ConstantInt::get(Ty, C0->countl_zero());
case Intrinsic::abs:
assert(C1 && "Must be constant int");
case Intrinsic::smin:
// If low bits of result are not demanded, they are also not demanded
// for the min/max operands.
- AB = APInt::getBitsSetFrom(BitWidth, AOut.countTrailingZeros());
+ AB = APInt::getBitsSetFrom(BitWidth, AOut.countr_zero());
break;
}
}
// must be positive (i.e., IsSigned = false), because if this were not the
// case, the sign bit would have been demanded.
auto Mask = DB->getDemandedBits(Exit);
- MaxBitWidth = Mask.getBitWidth() - Mask.countLeadingZeros();
+ MaxBitWidth = Mask.getBitWidth() - Mask.countl_zero();
}
if (MaxBitWidth == DL.getTypeSizeInBits(Exit->getType()) && AC && DT) {
// at least as many trailing zeros as the divisor to divide evenly. If it has
// less trailing zeros, then the result must be poison.
const APInt *DivC;
- if (IsExact && match(Op1, m_APInt(DivC)) && DivC->countTrailingZeros()) {
+ if (IsExact && match(Op1, m_APInt(DivC)) && DivC->countr_zero()) {
KnownBits KnownOp0 = computeKnownBits(Op0, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
- if (KnownOp0.countMaxTrailingZeros() < DivC->countTrailingZeros())
+ if (KnownOp0.countMaxTrailingZeros() < DivC->countr_zero())
return PoisonValue::get(Op0->getType());
}
// bit of Mask.
if (EdgePred == ICmpInst::ICMP_NE && !Mask->isZero() && C->isZero()) {
return ValueLatticeElement::getRange(ConstantRange::getNonEmpty(
- APInt::getOneBitSet(BitWidth, Mask->countTrailingZeros()),
+ APInt::getOneBitSet(BitWidth, Mask->countr_zero()),
APInt::getZero(BitWidth)));
}
}
unsigned T = 1;
for (unsigned i = 3; i <= K; ++i) {
APInt Mult(W, i);
- unsigned TwoFactors = Mult.countTrailingZeros();
+ unsigned TwoFactors = Mult.countr_zero();
T += TwoFactors;
Mult.lshrInPlace(TwoFactors);
OddFactorial *= Mult;
// its operands.
// TODO: Generalize this to non-constants by using known-bits information.
Type *Ty = LHS->getType();
- unsigned LZ = RHSC->getAPInt().countLeadingZeros();
+ unsigned LZ = RHSC->getAPInt().countl_zero();
unsigned MaxShiftAmt = getTypeSizeInBits(Ty) - LZ - 1;
// For non-power-of-two values, effectively round the value up to the
// nearest power of two.
uint32_t ScalarEvolution::GetMinTrailingZerosImpl(const SCEV *S) {
switch (S->getSCEVType()) {
case scConstant:
- return cast<SCEVConstant>(S)->getAPInt().countTrailingZeros();
+ return cast<SCEVConstant>(S)->getAPInt().countr_zero();
case scTruncate: {
const SCEVTruncateExpr *T = cast<SCEVTruncateExpr>(S);
return std::min(GetMinTrailingZeros(T->getOperand()),
// constants, obscuring what would otherwise be a low-bits mask.
// Use computeKnownBits to compute what ShrinkDemandedConstant
// knew about to reconstruct a low-bits mask value.
- unsigned LZ = A.countLeadingZeros();
- unsigned TZ = A.countTrailingZeros();
+ unsigned LZ = A.countl_zero();
+ unsigned TZ = A.countr_zero();
unsigned BitWidth = A.getBitWidth();
KnownBits Known(BitWidth);
computeKnownBits(BO->LHS, Known, getDataLayout(),
if (auto *LHSMul = dyn_cast<SCEVMulExpr>(LHS)) {
if (auto *OpC = dyn_cast<SCEVConstant>(LHSMul->getOperand(0))) {
// For an expression like (x * 8) & 8, simplify the multiply.
- unsigned MulZeros = OpC->getAPInt().countTrailingZeros();
+ unsigned MulZeros = OpC->getAPInt().countr_zero();
unsigned GCD = std::min(MulZeros, TZ);
APInt DivAmt = APInt::getOneBitSet(BitWidth, TZ - GCD);
SmallVector<const SCEV*, 4> MulOps;
//
// The gcd of A and N may have only one prime factor: 2. The number of
// trailing zeros in A is its multiplicity
- uint32_t Mult2 = A.countTrailingZeros();
+ uint32_t Mult2 = A.countr_zero();
// D = 2^Mult2
// 2. Check if B is divisible by D.
// The first CommonPrefixBits of all values in Range are equal.
unsigned CommonPrefixBits =
- (Range.getUnsignedMax() ^ Range.getUnsignedMin()).countLeadingZeros();
+ (Range.getUnsignedMax() ^ Range.getUnsignedMin()).countl_zero();
APInt Mask = APInt::getHighBitsSet(BitWidth, CommonPrefixBits);
APInt UnsignedMax = Range.getUnsignedMax().zextOrTrunc(BitWidth);
Known.One &= UnsignedMax & Mask;
// Trailing zeros of a right-shifted constant never decrease.
const APInt *C;
if (match(I->getOperand(0), m_APInt(C)))
- Known.Zero.setLowBits(C->countTrailingZeros());
+ Known.Zero.setLowBits(C->countr_zero());
break;
}
case Instruction::LShr: {
// Leading zeros of a left-shifted constant never decrease.
const APInt *C;
if (match(I->getOperand(0), m_APInt(C)))
- Known.Zero.setHighBits(C->countLeadingZeros());
+ Known.Zero.setHighBits(C->countl_zero());
break;
}
case Instruction::AShr: {
Known2 = KnownBits::makeConstant(*RHSC);
break;
case CmpInst::Predicate::ICMP_ULE:
- Known2.Zero.setHighBits(RHSC->countLeadingZeros());
+ Known2.Zero.setHighBits(RHSC->countl_zero());
break;
case CmpInst::Predicate::ICMP_ULT:
- Known2.Zero.setHighBits((*RHSC - 1).countLeadingZeros());
+ Known2.Zero.setHighBits((*RHSC - 1).countl_zero());
break;
default:
// TODO - add additional integer predicate handling.
} else if (match(BO.getOperand(0), m_APInt(C))) {
unsigned ShiftAmount = Width - 1;
if (!C->isZero() && IIQ.isExact(&BO))
- ShiftAmount = C->countTrailingZeros();
+ ShiftAmount = C->countr_zero();
if (C->isNegative()) {
// 'ashr C, x' produces [C, C >> (Width-1)]
Lower = *C;
// 'lshr C, x' produces [C >> (Width-1), C].
unsigned ShiftAmount = Width - 1;
if (!C->isZero() && IIQ.isExact(&BO))
- ShiftAmount = C->countTrailingZeros();
+ ShiftAmount = C->countr_zero();
Lower = C->lshr(ShiftAmount);
Upper = *C + 1;
}
if (IIQ.hasNoUnsignedWrap(&BO)) {
// 'shl nuw C, x' produces [C, C << CLZ(C)]
Lower = *C;
- Upper = Lower.shl(Lower.countLeadingZeros()) + 1;
+ Upper = Lower.shl(Lower.countl_zero()) + 1;
} else if (BO.hasNoSignedWrap()) { // TODO: What if both nuw+nsw?
if (C->isNegative()) {
// 'shl nsw C, x' produces [C << CLO(C)-1, C]
- unsigned ShiftAmount = C->countLeadingOnes() - 1;
+ unsigned ShiftAmount = C->countl_one() - 1;
Lower = C->shl(ShiftAmount);
Upper = *C + 1;
} else {
// 'shl nsw C, x' produces [C, C << CLZ(C)-1]
- unsigned ShiftAmount = C->countLeadingZeros() - 1;
+ unsigned ShiftAmount = C->countl_zero() - 1;
Lower = *C;
Upper = C->shl(ShiftAmount) + 1;
}
// where C != -1 and C != 0 and C != 1
Lower = IntMin + 1;
Upper = IntMax + 1;
- } else if (C->countLeadingZeros() < Width - 1) {
+ } else if (C->countl_zero() < Width - 1) {
// 'sdiv x, C' produces [INT_MIN / C, INT_MAX / C]
// where C != -1 and C != 0 and C != 1
Lower = IntMin.sdiv(*C);
Register PtrReg = LoadMI->getPointerReg();
unsigned RegSize = RegTy.getSizeInBits();
uint64_t LoadSizeBits = LoadMI->getMemSizeInBits();
- unsigned MaskSizeBits = MaskVal.countTrailingOnes();
+ unsigned MaskSizeBits = MaskVal.countr_one();
// The mask may not be larger than the in-memory type, as it might cover sign
// extended bits
MatchData.Reg = ExtSrc;
MatchData.Imm = ShiftAmt;
- unsigned MinLeadingZeros = KB->getKnownZeroes(ExtSrc).countLeadingOnes();
+ unsigned MinLeadingZeros = KB->getKnownZeroes(ExtSrc).countl_one();
return MinLeadingZeros >= ShiftAmt;
}
if (static_cast<uint64_t>(LSBImm) >= Size)
return false;
- uint64_t Width = APInt(Size, AndImm).countTrailingOnes();
+ uint64_t Width = APInt(Size, AndImm).countr_one();
MatchInfo = [=](MachineIRBuilder &B) {
auto WidthCst = B.buildConstant(ExtractTy, Width);
auto LSBCst = B.buildConstant(ExtractTy, LSBImm);
return false;
// No point in combining if there's nothing to truncate.
- unsigned NarrowWidth = Mask.countTrailingOnes();
+ unsigned NarrowWidth = Mask.countr_one();
if (NarrowWidth == WideTy.getSizeInBits())
return false;
LLT NarrowTy = LLT::scalar(NarrowWidth);
auto *CI = cast<ConstantInt>(C);
APInt Divisor = CI->getValue();
- unsigned Shift = Divisor.countTrailingZeros();
+ unsigned Shift = Divisor.countr_zero();
if (Shift) {
Divisor.ashrInPlace(Shift);
UseSRA = true;
// Okay, we know that the sign bit in Mask is set. Use CLO to determine
// the number of identical bits in the top of the input value.
Mask <<= Mask.getBitWidth() - TyBits;
- return std::max(FirstAnswer, Mask.countLeadingOnes());
+ return std::max(FirstAnswer, Mask.countl_one());
}
unsigned GISelKnownBits::computeNumSignBits(Register R, unsigned Depth) {
auto MaybeCst = getIConstantVRegVal(R, MRI);
if (!MaybeCst)
return std::nullopt;
- return MaybeCst->countLeadingZeros();
+ return MaybeCst->countl_zero();
};
if (Ty.isVector()) {
// Try to constant fold each element.
// See Proof(2): Trailing zero bits indicate a left shift. This removes
// leading bits from the result even if they are undefined.
- decErrorMSBs(C.countTrailingZeros());
+ decErrorMSBs(C.countr_zero());
A *= C;
pushBOperation(Mul, C);
//
// If this can be proven add shiftAmt to the error counter
// `ErrorMSBs`. Otherwise set all bits as undefined.
- if (A.countTrailingZeros() < shiftAmt)
+ if (A.countr_zero() < shiftAmt)
ErrorMSBs = A.getBitWidth();
else
incErrorMSBs(shiftAmt);
unsigned MathOp = ISD::DELETED_NODE;
APInt MulC = ConstValue1.abs();
// The constant `2` should be treated as (2^0 + 1).
- unsigned TZeros = MulC == 2 ? 0 : MulC.countTrailingZeros();
+ unsigned TZeros = MulC == 2 ? 0 : MulC.countr_zero();
MulC.lshrInPlace(TZeros);
if ((MulC - 1).isPowerOf2())
MathOp = ISD::ADD;
if (ShiftBits == 0)
return SDValue();
- unsigned MaskBits = AndMask.countTrailingOnes();
+ unsigned MaskBits = AndMask.countr_one();
EVT HalfVT = EVT::getIntegerVT(*DAG.getContext(), Size / 2);
if (AndMask.isMask() &&
if (!AndC->getAPIntValue().isMask())
return false;
- unsigned ActiveBits = AndC->getAPIntValue().countTrailingOnes();
+ unsigned ActiveBits = AndC->getAPIntValue().countr_one();
ExtVT = EVT::getIntegerVT(*DAG.getContext(), ActiveBits);
EVT LoadedVT = LoadN->getMemoryVT();
}
case ISD::ZERO_EXTEND:
case ISD::AssertZext: {
- unsigned ActiveBits = Mask->getAPIntValue().countTrailingOnes();
+ unsigned ActiveBits = Mask->getAPIntValue().countr_one();
EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), ActiveBits);
EVT VT = Op.getOpcode() == ISD::AssertZext ?
cast<VTSDNode>(Op.getOperand(1))->getVT() :
// could be set on input to the CTLZ node. If this bit is set, the SRL
// will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
// to an SRL/XOR pair, which is likely to simplify more.
- unsigned ShAmt = UnknownBits.countTrailingZeros();
+ unsigned ShAmt = UnknownBits.countr_zero();
SDValue Op = N0.getOperand(0);
if (ShAmt) {
const APInt &Mask = AndC->getAPIntValue();
unsigned ActiveBits = 0;
if (Mask.isMask()) {
- ActiveBits = Mask.countTrailingOnes();
+ ActiveBits = Mask.countr_one();
} else if (Mask.isShiftedMask(ShAmt, ActiveBits)) {
HasShiftedOffset = true;
} else {
isa<ConstantSDNode>(Mask->getOperand(1))) {
const APInt& ShiftMask = Mask->getConstantOperandAPInt(1);
if (ShiftMask.isMask()) {
- EVT MaskedVT = EVT::getIntegerVT(*DAG.getContext(),
- ShiftMask.countTrailingOnes());
+ EVT MaskedVT =
+ EVT::getIntegerVT(*DAG.getContext(), ShiftMask.countr_one());
// If the mask is smaller, recompute the type.
if ((ExtVT.getScalarSizeInBits() > MaskedVT.getScalarSizeInBits()) &&
TLI.isLoadExtLegal(ExtType, SRL.getValueType(), MaskedVT))
return true;
// Get rid of the unused bits on the right.
- APInt NarrowedUsedBits = UsedBits.lshr(UsedBits.countTrailingZeros());
+ APInt NarrowedUsedBits = UsedBits.lshr(UsedBits.countr_zero());
// Get rid of the unused bits on the left.
- if (NarrowedUsedBits.countLeadingZeros())
+ if (NarrowedUsedBits.countl_zero())
NarrowedUsedBits = NarrowedUsedBits.trunc(NarrowedUsedBits.getActiveBits());
// Check that the chunk of bits is completely used.
return NarrowedUsedBits.isAllOnes();
Imm ^= APInt::getAllOnes(BitWidth);
if (Imm == 0 || Imm.isAllOnes())
return SDValue();
- unsigned ShAmt = Imm.countTrailingZeros();
- unsigned MSB = BitWidth - Imm.countLeadingZeros() - 1;
+ unsigned ShAmt = Imm.countr_zero();
+ unsigned MSB = BitWidth - Imm.countl_zero() - 1;
unsigned NewBW = NextPowerOf2(MSB - ShAmt);
EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
// The narrowing should be profitable, the load/store operation should be
unsigned ShCt = AndMask.getBitWidth() - 1;
if (!TLI.shouldAvoidTransformToShift(VT, ShCt)) {
SDValue ShlAmt =
- DAG.getConstant(AndMask.countLeadingZeros(), SDLoc(AndLHS),
- getShiftAmountTy(AndLHS.getValueType()));
+ DAG.getConstant(AndMask.countl_zero(), SDLoc(AndLHS),
+ getShiftAmountTy(AndLHS.getValueType()));
SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(N0), VT, AndLHS, ShlAmt);
// Now arithmetic right shift it all the way over, so the result is
SDValue NotZero = N->getOperand(i);
unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(NotZero)) {
- if (CN->getAPIntValue().countTrailingOnes() < EltSize)
+ if (CN->getAPIntValue().countr_one() < EltSize)
return false;
} else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(NotZero)) {
- if (CFPN->getValueAPF().bitcastToAPInt().countTrailingOnes() < EltSize)
+ if (CFPN->getValueAPF().bitcastToAPInt().countr_one() < EltSize)
return false;
} else
return false;
// constants are.
unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op)) {
- if (CN->getAPIntValue().countTrailingZeros() < EltSize)
+ if (CN->getAPIntValue().countr_zero() < EltSize)
return false;
} else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(Op)) {
- if (CFPN->getValueAPF().bitcastToAPInt().countTrailingZeros() < EltSize)
+ if (CFPN->getValueAPF().bitcastToAPInt().countr_zero() < EltSize)
return false;
} else
return false;
SplatIdx = 0;
return getUNDEF(VT);
}
- SplatIdx = (UndefElts & DemandedElts).countTrailingOnes();
+ SplatIdx = (UndefElts & DemandedElts).countr_one();
}
return V;
}
C->isOpaque());
case ISD::CTLZ:
case ISD::CTLZ_ZERO_UNDEF:
- return getConstant(Val.countLeadingZeros(), DL, VT, C->isTargetOpcode(),
+ return getConstant(Val.countl_zero(), DL, VT, C->isTargetOpcode(),
C->isOpaque());
case ISD::CTTZ:
case ISD::CTTZ_ZERO_UNDEF:
- return getConstant(Val.countTrailingZeros(), DL, VT, C->isTargetOpcode(),
+ return getConstant(Val.countr_zero(), DL, VT, C->isTargetOpcode(),
C->isOpaque());
case ISD::FP16_TO_FP:
case ISD::BF16_TO_FP: {
unsigned NumBits = V.getScalarValueSizeInBits();
ConstantSDNode *C =
isConstOrConstSplat(V, AllowUndefs, /*AllowTruncation*/ true);
- return C && (C->getAPIntValue().countTrailingOnes() >= NumBits);
+ return C && (C->getAPIntValue().countr_one() >= NumBits);
}
ConstantSDNode *llvm::isConstOrConstSplat(SDValue N, bool AllowUndefs,
}
if (!Splatted) {
- unsigned FirstDemandedIdx = DemandedElts.countTrailingZeros();
+ unsigned FirstDemandedIdx = DemandedElts.countr_zero();
assert(getOperand(FirstDemandedIdx).isUndef() &&
"Can only have a splat without a constant for all undefs.");
return getOperand(FirstDemandedIdx);
unsigned ShAmt = MaxSA->getZExtValue();
unsigned NumSignBits =
DAG.ComputeNumSignBits(Op0, DemandedElts, Depth + 1);
- unsigned UpperDemandedBits = BitWidth - DemandedBits.countTrailingZeros();
+ unsigned UpperDemandedBits = BitWidth - DemandedBits.countr_zero();
if (NumSignBits > ShAmt && (NumSignBits - ShAmt) >= (UpperDemandedBits))
return Op0;
}
if (InnerShAmt < ShAmt && InnerShAmt < InnerBits &&
DemandedBits.getActiveBits() <=
(InnerBits - InnerShAmt + ShAmt) &&
- DemandedBits.countTrailingZeros() >= ShAmt) {
+ DemandedBits.countr_zero() >= ShAmt) {
SDValue NewSA =
TLO.DAG.getConstant(ShAmt - InnerShAmt, dl, ShiftVT);
SDValue NewExt = TLO.DAG.getNode(ISD::ANY_EXTEND, dl, VT,
// This is a variable shift, so we can't shift the demand mask by a known
// amount. But if we are not demanding high bits, then we are not
// demanding those bits from the pre-shifted operand either.
- if (unsigned CTLZ = DemandedBits.countLeadingZeros()) {
+ if (unsigned CTLZ = DemandedBits.countl_zero()) {
APInt DemandedFromOp(APInt::getLowBitsSet(BitWidth, BitWidth - CTLZ));
if (SimplifyDemandedBits(Op0, DemandedFromOp, DemandedElts, Known, TLO,
Depth + 1)) {
unsigned ShAmt = MaxSA->getZExtValue();
unsigned NumSignBits =
TLO.DAG.ComputeNumSignBits(Op0, DemandedElts, Depth + 1);
- unsigned UpperDemandedBits = BitWidth - DemandedBits.countTrailingZeros();
+ unsigned UpperDemandedBits = BitWidth - DemandedBits.countr_zero();
if (NumSignBits > ShAmt && (NumSignBits - ShAmt) >= (UpperDemandedBits))
return TLO.CombineTo(Op, Op0);
}
// If we only want bits that already match the signbit then we don't need
// to shift.
- unsigned NumHiDemandedBits = BitWidth - DemandedBits.countTrailingZeros();
+ unsigned NumHiDemandedBits = BitWidth - DemandedBits.countr_zero();
if (TLO.DAG.ComputeNumSignBits(Op0, DemandedElts, Depth + 1) >=
NumHiDemandedBits)
return TLO.CombineTo(Op, Op0);
// If any of the demanded bits are produced by the sign extension, we also
// demand the input sign bit.
- if (DemandedBits.countLeadingZeros() < ShAmt)
+ if (DemandedBits.countl_zero() < ShAmt)
InDemandedMask.setSignBit();
if (SimplifyDemandedBits(Op0, InDemandedMask, DemandedElts, Known, TLO,
// If the input sign bit is known to be zero, or if none of the top bits
// are demanded, turn this into an unsigned shift right.
if (Known.Zero[BitWidth - ShAmt - 1] ||
- DemandedBits.countLeadingZeros() >= ShAmt) {
+ DemandedBits.countl_zero() >= ShAmt) {
SDNodeFlags Flags;
Flags.setExact(Op->getFlags().hasExact());
return TLO.CombineTo(
// See if we don't demand either half of the rotated bits.
if ((!TLO.LegalOperations() || isOperationLegal(ISD::SHL, VT)) &&
- DemandedBits.countTrailingZeros() >= (IsROTL ? Amt : RevAmt)) {
+ DemandedBits.countr_zero() >= (IsROTL ? Amt : RevAmt)) {
Op1 = TLO.DAG.getConstant(IsROTL ? Amt : RevAmt, dl, Op1.getValueType());
return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SHL, dl, VT, Op0, Op1));
}
if ((!TLO.LegalOperations() || isOperationLegal(ISD::SRL, VT)) &&
- DemandedBits.countLeadingZeros() >= (IsROTL ? RevAmt : Amt)) {
+ DemandedBits.countl_zero() >= (IsROTL ? RevAmt : Amt)) {
Op1 = TLO.DAG.getConstant(IsROTL ? RevAmt : Amt, dl, Op1.getValueType());
return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, dl, VT, Op0, Op1));
}
// If the only bits demanded come from one byte of the bswap result,
// just shift the input byte into position to eliminate the bswap.
- unsigned NLZ = DemandedBits.countLeadingZeros();
- unsigned NTZ = DemandedBits.countTrailingZeros();
+ unsigned NLZ = DemandedBits.countl_zero();
+ unsigned NTZ = DemandedBits.countr_zero();
// Round NTZ down to the next byte. If we have 11 trailing zeros, then
// we need all the bits down to bit 8. Likewise, round NLZ. If we
// The LSB of X*Y is set only if (X & 1) == 1 and (Y & 1) == 1.
// If we demand exactly one bit N and we have "X * (C' << N)" where C' is
// odd (has LSB set), then the left-shifted low bit of X is the answer.
- unsigned CTZ = DemandedBits.countTrailingZeros();
+ unsigned CTZ = DemandedBits.countr_zero();
ConstantSDNode *C = isConstOrConstSplat(Op.getOperand(1), DemandedElts);
- if (C && C->getAPIntValue().countTrailingZeros() == CTZ) {
+ if (C && C->getAPIntValue().countr_zero() == CTZ) {
EVT ShiftAmtTy = getShiftAmountTy(VT, TLO.DAG.getDataLayout());
SDValue AmtC = TLO.DAG.getConstant(CTZ, dl, ShiftAmtTy);
SDValue Shl = TLO.DAG.getNode(ISD::SHL, dl, VT, Op.getOperand(0), AmtC);
// of the highest bit demanded of them.
SDValue Op0 = Op.getOperand(0), Op1 = Op.getOperand(1);
SDNodeFlags Flags = Op.getNode()->getFlags();
- unsigned DemandedBitsLZ = DemandedBits.countLeadingZeros();
+ unsigned DemandedBitsLZ = DemandedBits.countl_zero();
APInt LoMask = APInt::getLowBitsSet(BitWidth, BitWidth - DemandedBitsLZ);
if (SimplifyDemandedBits(Op0, LoMask, DemandedElts, Known2, TLO,
Depth + 1) ||
// DAGCombine turns costly ZExts into ANDs
if (auto *C = dyn_cast<ConstantSDNode>(N0->getOperand(1)))
if ((C->getAPIntValue()+1).isPowerOf2()) {
- MinBits = C->getAPIntValue().countTrailingOnes();
+ MinBits = C->getAPIntValue().countr_one();
PreExt = N0->getOperand(0);
}
} else if (N0->getOpcode() == ISD::SIGN_EXTEND) {
if (auto *AndRHS = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
const APInt &AndRHSC = AndRHS->getAPIntValue();
if (AndRHSC.isNegatedPowerOf2() && (AndRHSC & C1) == C1) {
- unsigned ShiftBits = AndRHSC.countTrailingZeros();
+ unsigned ShiftBits = AndRHSC.countr_zero();
if (!TLI.shouldAvoidTransformToShift(ShValTy, ShiftBits)) {
SDValue Shift =
DAG.getNode(ISD::SRL, dl, ShValTy, N0.getOperand(0),
APInt NewC = C1;
ISD::CondCode NewCond = Cond;
if (AdjOne) {
- ShiftBits = C1.countTrailingOnes();
+ ShiftBits = C1.countr_one();
NewC = NewC + 1;
NewCond = (Cond == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
} else {
- ShiftBits = C1.countTrailingZeros();
+ ShiftBits = C1.countr_zero();
}
NewC.lshrInPlace(ShiftBits);
if (ShiftBits && NewC.getMinSignedBits() <= 64 &&
if (C->isZero())
return false;
APInt Divisor = C->getAPIntValue();
- unsigned Shift = Divisor.countTrailingZeros();
+ unsigned Shift = Divisor.countr_zero();
if (Shift) {
Divisor.ashrInPlace(Shift);
UseSRA = true;
LeadingZeros = DAG.computeKnownBits(N0).countMinLeadingZeros();
// UnsignedDivisionByConstantInfo doesn't work correctly if leading zeros in
// the dividend exceeds the leading zeros for the divisor.
- LeadingZeros =
- std::min(LeadingZeros,
- cast<ConstantSDNode>(N1)->getAPIntValue().countLeadingZeros());
+ LeadingZeros = std::min(
+ LeadingZeros, cast<ConstantSDNode>(N1)->getAPIntValue().countl_zero());
}
bool UseNPQ = false, UsePreShift = false, UsePostShift = false;
AllComparisonsWithNonZerosAreTautological &= TautologicalLane;
// Decompose D into D0 * 2^K
- unsigned K = D.countTrailingZeros();
+ unsigned K = D.countr_zero();
assert((!D.isOne() || (K == 0)) && "For divisor '1' we won't rotate.");
APInt D0 = D.lshr(K);
AllDivisorsAreOnes &= D.isOne();
// Decompose D into D0 * 2^K
- unsigned K = D.countTrailingZeros();
+ unsigned K = D.countr_zero();
assert((!D.isOne() || (K == 0)) && "For divisor '1' we won't rotate.");
APInt D0 = D.lshr(K);
// If the divisor is even, shift it until it becomes odd.
unsigned TrailingZeros = 0;
if (!Divisor[0]) {
- TrailingZeros = Divisor.countTrailingZeros();
+ TrailingZeros = Divisor.countr_zero();
Divisor.lshrInPlace(TrailingZeros);
}
if (isUpperWrapped()) {
// If Upper is greater than or equal to MaxValue(DstTy), it covers the whole
// truncated range.
- if (Upper.getActiveBits() > DstTySize ||
- Upper.countTrailingOnes() == DstTySize)
+ if (Upper.getActiveBits() > DstTySize || Upper.countr_one() == DstTySize)
return getFull(DstTySize);
Union = ConstantRange(APInt::getMaxValue(DstTySize),Upper.trunc(DstTySize));
if (RHS->uge(BW))
return getEmpty();
- unsigned EqualLeadingBits = (Min ^ Max).countLeadingZeros();
+ unsigned EqualLeadingBits = (Min ^ Max).countl_zero();
if (RHS->ule(EqualLeadingBits))
return getNonEmpty(Min << *RHS, (Max << *RHS) + 1);
APInt OtherMax = Other.getUnsignedMax();
// There's overflow!
- if (OtherMax.ugt(Max.countLeadingZeros()))
+ if (OtherMax.ugt(Max.countl_zero()))
return getFull();
// FIXME: implement the other tricky cases
// Compute the resulting range by excluding zero from Lower.
return ConstantRange(
- APInt(getBitWidth(), (getUpper() - 1).countLeadingZeros()),
- APInt(getBitWidth(), (getLower() + 1).countLeadingZeros() + 1));
+ APInt(getBitWidth(), (getUpper() - 1).countl_zero()),
+ APInt(getBitWidth(), (getLower() + 1).countl_zero() + 1));
} else if ((getUpper() - 1).isZero()) {
// Compute the resulting range by excluding zero from Upper.
- return ConstantRange(
- Zero, APInt(getBitWidth(), getLower().countLeadingZeros() + 1));
+ return ConstantRange(Zero,
+ APInt(getBitWidth(), getLower().countl_zero() + 1));
} else {
return ConstantRange(Zero, APInt(getBitWidth(), getBitWidth()));
}
// Zero is either safe or not in the range. The output range is composed by
// the result of countLeadingZero of the two extremes.
- return getNonEmpty(
- APInt(getBitWidth(), getUnsignedMax().countLeadingZeros()),
- APInt(getBitWidth(), getUnsignedMin().countLeadingZeros() + 1));
+ return getNonEmpty(APInt(getBitWidth(), getUnsignedMax().countl_zero()),
+ APInt(getBitWidth(), getUnsignedMin().countl_zero() + 1));
}
ConstantRange::OverflowResult ConstantRange::unsignedAddMayOverflow(
// Determine lo/hi span ranges.
// TODO: How should we handle undefs at the start of subvector insertions?
- int Src0Lo = Src0Elts.countTrailingZeros();
- int Src1Lo = Src1Elts.countTrailingZeros();
- int Src0Hi = NumMaskElts - Src0Elts.countLeadingZeros();
- int Src1Hi = NumMaskElts - Src1Elts.countLeadingZeros();
+ int Src0Lo = Src0Elts.countr_zero();
+ int Src1Lo = Src1Elts.countr_zero();
+ int Src0Hi = NumMaskElts - Src0Elts.countl_zero();
+ int Src1Hi = NumMaskElts - Src1Elts.countl_zero();
// If src0 is in place, see if the src1 elements is inplace within its own
// span.
if (auto *CstInt = dyn_cast_or_null<ConstantInt>(ConstantExpr::getPtrToInt(
const_cast<Constant *>(CstPtr), DL.getIntPtrType(getType()),
/*OnlyIfReduced=*/true))) {
- size_t TrailingZeros = CstInt->getValue().countTrailingZeros();
+ size_t TrailingZeros = CstInt->getValue().countr_zero();
// While the actual alignment may be large, elsewhere we have
// an arbitrary upper alignmet limit, so let's clamp to it.
return Align(TrailingZeros < Value::MaxAlignmentExponent
}
// Ignore trailing binary zeros.
- int trailingZeros = significand.countTrailingZeros();
+ int trailingZeros = significand.countr_zero();
exp += trailingZeros;
significand.lshrInPlace(trailingZeros);
// Count common powers of 2 and remove all other powers of 2.
unsigned Pow2;
{
- unsigned Pow2_A = A.countTrailingZeros();
- unsigned Pow2_B = B.countTrailingZeros();
+ unsigned Pow2_A = A.countr_zero();
+ unsigned Pow2_B = B.countr_zero();
if (Pow2_A > Pow2_B) {
A.lshrInPlace(Pow2_A - Pow2_B);
Pow2 = Pow2_B;
while (A != B) {
if (A.ugt(B)) {
A -= B;
- A.lshrInPlace(A.countTrailingZeros() - Pow2);
+ A.lshrInPlace(A.countr_zero() - Pow2);
} else {
B -= A;
- B.lshrInPlace(B.countTrailingZeros() - Pow2);
+ B.lshrInPlace(B.countr_zero() - Pow2);
}
}
}
APInt APInt::umul_ov(const APInt &RHS, bool &Overflow) const {
- if (countLeadingZeros() + RHS.countLeadingZeros() + 2 <= BitWidth) {
+ if (countl_zero() + RHS.countl_zero() + 2 <= BitWidth) {
Overflow = true;
return *this * RHS;
}
return APInt(BitWidth, 0);
if (isNonNegative()) // Don't allow sign change.
- Overflow = ShAmt.uge(countLeadingZeros());
+ Overflow = ShAmt.uge(countl_zero());
else
- Overflow = ShAmt.uge(countLeadingOnes());
+ Overflow = ShAmt.uge(countl_one());
return *this << ShAmt;
}
if (Overflow)
return APInt(BitWidth, 0);
- Overflow = ShAmt.ugt(countLeadingZeros());
+ Overflow = ShAmt.ugt(countl_zero());
return *this << ShAmt;
}
assert(A.getBitWidth() == B.getBitWidth() && "Must have the same bitwidth");
if (A == B)
return std::nullopt;
- return A.getBitWidth() - ((A ^ B).countLeadingZeros() + 1);
+ return A.getBitWidth() - ((A ^ B).countl_zero() + 1);
}
APInt llvm::APIntOps::ScaleBitMask(const APInt &A, unsigned NewBitWidth,
(Q1.ult(Delta) || (Q1 == Delta && R1.isZero())));
if (Retval.IsAdd && !D[0] && AllowEvenDivisorOptimization) {
- unsigned PreShift = D.countTrailingZeros();
+ unsigned PreShift = D.countr_zero();
APInt ShiftedD = D.lshr(PreShift);
Retval =
UnsignedDivisionByConstantInfo::get(ShiftedD, LeadingZeros + PreShift);
KnownBits KnownBits::makeGE(const APInt &Val) const {
// Count the number of leading bit positions where our underlying value is
// known to be less than or equal to Val.
- unsigned N = (Zero | Val).countLeadingOnes();
+ unsigned N = (Zero | Val).countl_one();
// For each of those bit positions, if Val has a 1 in that bit then our
// underlying value must also have a 1.
// fit in the bitwidth (it must not overflow).
bool HasOverflow;
APInt UMaxResult = UMaxLHS.umul_ov(UMaxRHS, HasOverflow);
- unsigned LeadZ = HasOverflow ? 0 : UMaxResult.countLeadingZeros();
+ unsigned LeadZ = HasOverflow ? 0 : UMaxResult.countl_zero();
// The result of the bottom bits of an integer multiply can be
// inferred by looking at the bottom bits of both operands and
// How many times we'd be able to divide each argument by 2 (shr by 1).
// This gives us the number of trailing zeros on the multiplication result.
- unsigned TrailBitsKnown0 = (LHS.Zero | LHS.One).countTrailingOnes();
- unsigned TrailBitsKnown1 = (RHS.Zero | RHS.One).countTrailingOnes();
+ unsigned TrailBitsKnown0 = (LHS.Zero | LHS.One).countr_one();
+ unsigned TrailBitsKnown1 = (RHS.Zero | RHS.One).countr_one();
unsigned TrailZero0 = LHS.countMinTrailingZeros();
unsigned TrailZero1 = RHS.countMinTrailingZeros();
unsigned TrailZ = TrailZero0 + TrailZero1;
!(C.isPowerOf2() || C.isNegatedPowerOf2()))
return selectBinaryOp(I, ISD::SDIV);
- unsigned Lg2 = C.countTrailingZeros();
+ unsigned Lg2 = C.countr_zero();
Register Src0Reg = getRegForValue(I->getOperand(0));
if (!Src0Reg)
return false;
// Because of simplify-demanded-bits in DAGCombine, involved masks may not
// have the expected shape. Try to undo that.
- unsigned NumberOfIgnoredLowBits = UsefulBits.countTrailingZeros();
- unsigned NumberOfIgnoredHighBits = UsefulBits.countLeadingZeros();
+ unsigned NumberOfIgnoredLowBits = UsefulBits.countr_zero();
+ unsigned NumberOfIgnoredHighBits = UsefulBits.countl_zero();
// Given a OR operation, check if we have the following pattern
// ubfm c, b, imm, imm2 (or something that does the same jobs, see
SDLoc DL(N);
SDValue N0 = N->getOperand(0);
- unsigned Lg2 = Divisor.countTrailingZeros();
+ unsigned Lg2 = Divisor.countr_zero();
SDValue Zero = DAG.getConstant(0, DL, VT);
SDValue Pow2MinusOne = DAG.getConstant((1ULL << Lg2) - 1, DL, VT);
!(Divisor.isPowerOf2() || Divisor.isNegatedPowerOf2()))
return SDValue();
- unsigned Lg2 = Divisor.countTrailingZeros();
+ unsigned Lg2 = Divisor.countr_zero();
if (Lg2 == 0)
return SDValue();
// TrailingZeroes is used to test if the mul can be lowered to
// shift+add+shift.
- unsigned TrailingZeroes = ConstValue.countTrailingZeros();
+ unsigned TrailingZeroes = ConstValue.countr_zero();
if (TrailingZeroes) {
// Conservatively do not lower to shift+add+shift if the mul might be
// folded into smul or umul.
// which equals to (1+2)*16-(1+2).
// TrailingZeroes is used to test if the mul can be lowered to
// shift+add+shift.
- unsigned TrailingZeroes = ConstValue.countTrailingZeros();
+ unsigned TrailingZeroes = ConstValue.countr_zero();
if (TrailingZeroes) {
// Conservatively do not lower to shift+add+shift if the mul might be
// folded into smul or umul.
assert(N->getOpcode() == ISD::AND);
const APInt &RHS = cast<ConstantSDNode>(N->getOperand(1))->getAPIntValue();
- if (RHS.countTrailingOnes() >= ShAmtBits)
+ if (RHS.countr_one() >= ShAmtBits)
return true;
const APInt &LHSKnownZeros = CurDAG->computeKnownBits(N->getOperand(0)).Zero;
- return (LHSKnownZeros | RHS).countTrailingOnes() >= ShAmtBits;
+ return (LHSKnownZeros | RHS).countr_one() >= ShAmtBits;
}
static bool getBaseWithOffsetUsingSplitOR(SelectionDAG &DAG, SDValue Addr,
// Buffer case.
const unsigned ActiveBits = DemandedElts.getActiveBits();
- const unsigned UnusedComponentsAtFront = DemandedElts.countTrailingZeros();
+ const unsigned UnusedComponentsAtFront = DemandedElts.countr_zero();
// Start assuming the prefix of elements is demanded, but possibly clear
// some other bits if there are trailing zeros (unused components at front)
if (NewNumElts == 1) {
return IC.Builder.CreateInsertElement(UndefValue::get(IIVTy), NewCall,
- DemandedElts.countTrailingZeros());
+ DemandedElts.countr_zero());
}
SmallVector<int, 8> EltMask;
if (!RHS)
return false;
- if (RHS->countTrailingOnes() >= ShAmtBits)
+ if (RHS->countr_one() >= ShAmtBits)
return true;
const APInt &LHSKnownZeros =
KnownBits->getKnownZeroes(MI.getOperand(1).getReg());
- return (LHSKnownZeros | *RHS).countTrailingOnes() >= ShAmtBits;
+ return (LHSKnownZeros | *RHS).countr_one() >= ShAmtBits;
}
// Return the wave level SGPR base address if this is a wave address.
(Bits == 8 || Bits == 16) && isShiftedMask_64(Mask) && !(Mask & 1)) {
if (auto *CShift = dyn_cast<ConstantSDNode>(LHS->getOperand(1))) {
unsigned Shift = CShift->getZExtValue();
- unsigned NB = CRHS->getAPIntValue().countTrailingZeros();
+ unsigned NB = CRHS->getAPIntValue().countr_zero();
unsigned Offset = NB + Shift;
if ((Offset & (Bits - 1)) == 0) { // Starts at a byte or word boundary.
SDLoc SL(N);
static std::optional<std::pair<unsigned, unsigned>>
getContiguousRangeOfSetBits(const APInt &A) {
- unsigned FirstOne = A.getBitWidth() - A.countLeadingZeros() - 1;
- unsigned LastOne = A.countTrailingZeros();
+ unsigned FirstOne = A.getBitWidth() - A.countl_zero() - 1;
+ unsigned LastOne = A.countr_zero();
if (A.popcount() != (FirstOne - LastOne + 1))
return std::nullopt;
return std::make_pair(FirstOne, LastOne);
// The immediates are encoded as an 8-bit value that can be rotated.
auto LargeImm = [](const APInt &Imm) {
- unsigned Zeros = Imm.countLeadingZeros() + Imm.countTrailingZeros();
+ unsigned Zeros = Imm.countl_zero() + Imm.countr_zero();
return Imm.getBitWidth() - Zeros > 8;
};
//
// Neither A nor B must be zero.
static bool BitsProperlyConcatenate(const APInt &A, const APInt &B) {
- unsigned LastActiveBitInA = A.countTrailingZeros();
- unsigned FirstActiveBitInB = B.getBitWidth() - B.countLeadingZeros() - 1;
+ unsigned LastActiveBitInA = A.countr_zero();
+ unsigned FirstActiveBitInB = B.getBitWidth() - B.countl_zero() - 1;
return LastActiveBitInA - 1 == FirstActiveBitInB;
}
SDLoc dl(N);
if (NewFromMask[0] == 0)
- From1 = DAG.getNode(
- ISD::SRL, dl, VT, From1,
- DAG.getConstant(NewFromMask.countTrailingZeros(), dl, VT));
+ From1 = DAG.getNode(ISD::SRL, dl, VT, From1,
+ DAG.getConstant(NewFromMask.countr_zero(), dl, VT));
return DAG.getNode(ARMISD::BFI, dl, VT, CombineBFI.getOperand(0), From1,
DAG.getConstant(~NewToMask, dl, VT));
}
APInt ToMask2 = ~N0.getConstantOperandAPInt(2);
if (!N0.hasOneUse() || (ToMask1 & ToMask2) != 0 ||
- ToMask1.countLeadingZeros() < ToMask2.countLeadingZeros())
+ ToMask1.countl_zero() < ToMask2.countl_zero())
return SDValue();
EVT VT = N->getValueType(0);
SDLoc DL(Op);
if (MinC == ~MaxC)
return DAG.getNode(ARMISD::SSAT, DL, VT, Input,
- DAG.getConstant(MinC.countTrailingOnes(), DL, VT));
+ DAG.getConstant(MinC.countr_one(), DL, VT));
if (MaxC == 0)
return DAG.getNode(ARMISD::USAT, DL, VT, Input,
- DAG.getConstant(MinC.countTrailingOnes(), DL, VT));
+ DAG.getConstant(MinC.countr_one(), DL, VT));
return SDValue();
}
return false;
unsigned Count = 0;
if (Zeros && (Count == 0))
- Count = A1.countLeadingZeros();
+ Count = A1.countl_zero();
if (Ones && (Count == 0))
- Count = A1.countLeadingOnes();
+ Count = A1.countl_one();
Result = APInt(BW, static_cast<uint64_t>(Count), false);
return true;
}
return false;
unsigned Count = 0;
if (Zeros && (Count == 0))
- Count = A1.countTrailingZeros();
+ Count = A1.countr_zero();
if (Ones && (Count == 0))
- Count = A1.countTrailingOnes();
+ Count = A1.countr_one();
Result = APInt(BW, static_cast<uint64_t>(Count), false);
return true;
}
// CM is the shifted-left mask. Shift it back right to remove the zero
// bits on least-significant positions.
APInt M = CM->getValue().lshr(SL);
- uint32_t T = M.countTrailingOnes();
+ uint32_t T = M.countr_one();
// During the shifts some of the bits will be lost. Calculate how many
// of the original value will remain after shift right and then left.
continue;
const APInt &CInt = C->getAPIntValue();
if (CInt.getBoolValue())
- MaxFactor = CInt.countTrailingZeros();
+ MaxFactor = CInt.countr_zero();
}
return MaxFactor;
}
// Assume that all inputs to the value have the high bits zero.
// Check if the value itself preserves the zeros in the high bits.
if (auto *C = dyn_cast<ConstantInt>(V))
- return C->getValue().countLeadingZeros() >= IterCount;
+ return C->getValue().countl_zero() >= IterCount;
if (auto *I = dyn_cast<Instruction>(V)) {
switch (I->getOpcode()) {
SDValue N0 = N->getOperand(0);
bool IsNegPow2 = Divisor.isNegatedPowerOf2();
- unsigned Lg2 = (IsNegPow2 ? -Divisor : Divisor).countTrailingZeros();
+ unsigned Lg2 = (IsNegPow2 ? -Divisor : Divisor).countr_zero();
SDValue ShiftAmt = DAG.getConstant(Lg2, DL, VT);
SDValue Op = DAG.getNode(PPCISD::SRA_ADDZE, DL, VT, N0, ShiftAmt);
return SDValue();
// AddC needs to have at least 32 trailing zeros.
- if (AddC->getAPIntValue().countTrailingZeros() < 32)
+ if (AddC->getAPIntValue().countr_zero() < 32)
return SDValue();
// All users should be a shift by constant less than or equal to 32. This
return false;
// Break the MUL to two SLLI instructions and an ADD/SUB, if Imm needs
// a pair of LUI/ADDI.
- if (!Imm.isSignedIntN(12) && Imm.countTrailingZeros() < 12) {
- APInt ImmS = Imm.ashr(Imm.countTrailingZeros());
+ if (!Imm.isSignedIntN(12) && Imm.countr_zero() < 12) {
+ APInt ImmS = Imm.ashr(Imm.countr_zero());
if ((ImmS + 1).isPowerOf2() || (ImmS - 1).isPowerOf2() ||
(1 - ImmS).isPowerOf2())
return true;
assert(N->getOpcode() == ISD::AND && "Unexpected opcode");
const APInt &Val = cast<ConstantSDNode>(N->getOperand(1))->getAPIntValue();
- if (Val.countTrailingOnes() >= Width)
+ if (Val.countr_one() >= Width)
return true;
APInt Mask = Val | CurDAG->computeKnownBits(N->getOperand(0)).Zero;
- return Mask.countTrailingOnes() >= Width;
+ return Mask.countr_one() >= Width;
}
/// Return an SDNode that returns the value of the global base register.
// implicit zeroing of 32 bit ops. So we should check if the lower 32 bits
// are negative too.
APInt MaskVal = And1C->getAPIntValue();
- unsigned MaskLZ = MaskVal.countLeadingZeros();
+ unsigned MaskLZ = MaskVal.countl_zero();
if (!MaskLZ || (VT == MVT::i64 && MaskLZ == 32))
return false;
: DAG.getConstantFP(0.0, DL, VT);
const TargetLowering &TLI = DAG.getTargetLoweringInfo();
- int FirstLoadedElt = LoadMask.countTrailingZeros();
+ int FirstLoadedElt = LoadMask.countr_zero();
SDValue EltBase = peekThroughBitcasts(Elts[FirstLoadedElt]);
EVT EltBaseVT = EltBase.getValueType();
assert(EltBaseVT.getSizeInBits() == EltBaseVT.getStoreSizeInBits() &&
!isFoldableUseOfShuffle(BV)) {
unsigned UpperElems = NumElems / 2;
APInt UndefOrZeroMask = UndefMask | ZeroMask;
- unsigned NumUpperUndefsOrZeros = UndefOrZeroMask.countLeadingOnes();
+ unsigned NumUpperUndefsOrZeros = UndefOrZeroMask.countl_one();
if (NumUpperUndefsOrZeros >= UpperElems) {
if (VT.is512BitVector() &&
NumUpperUndefsOrZeros >= (NumElems - (NumElems / 4)))
UpperElems = NumElems - (NumElems / 4);
- bool UndefUpper = UndefMask.countLeadingOnes() >= UpperElems;
+ bool UndefUpper = UndefMask.countl_one() >= UpperElems;
MVT LowerVT = MVT::getVectorVT(EltVT, NumElems - UpperElems);
SDValue NewBV =
DAG.getBuildVector(LowerVT, dl, Op->ops().drop_back(UpperElems));
// Special case for single non-zero, non-undef, element.
if (NumNonZero == 1) {
- unsigned Idx = NonZeroMask.countTrailingZeros();
+ unsigned Idx = NonZeroMask.countr_zero();
SDValue Item = Op.getOperand(Idx);
// If we have a constant or non-constant insertion into the low element of
// shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
// Check if it's possible to issue this instead.
// shuffle (vload ptr)), undef, <1, 1, 1, 1>
- unsigned Idx = NonZeroMask.countTrailingZeros();
+ unsigned Idx = NonZeroMask.countr_zero();
SDValue Item = Op.getOperand(Idx);
if (Op.getNode()->isOnlyUserOf(Item.getNode()))
return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
if (EVTBits == 64) {
if (NumNonZero == 1) {
// One half is zero or undef.
- unsigned Idx = NonZeroMask.countTrailingZeros();
+ unsigned Idx = NonZeroMask.countr_zero();
SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Op.getOperand(Idx));
return getShuffleVectorZeroOrUndef(V2, Idx, true, Subtarget, DAG);
// We need a shuffle that has zeros at one/both ends and a sequential
// shuffle from one source within.
- unsigned ZeroLo = Zeroable.countTrailingOnes();
- unsigned ZeroHi = Zeroable.countLeadingOnes();
+ unsigned ZeroLo = Zeroable.countr_one();
+ unsigned ZeroHi = Zeroable.countl_one();
if (!ZeroLo && !ZeroHi)
return SDValue();
return SDValue();
// Remaining elements need to be zeroable.
- if (Zeroable.countLeadingOnes() < (Mask.size() - 8))
+ if (Zeroable.countl_one() < (Mask.size() - 8))
return SDValue();
V1 = DAG.getBitcast(MVT::v4i64, V1);
// Make sure the number of zeroable bits in the top at least covers the bits
// not covered by the subvector.
- if ((int)Zeroable.countLeadingOnes() >= (NumElts - SubvecElts)) {
+ if ((int)Zeroable.countl_one() >= (NumElts - SubvecElts)) {
assert(Src >= 0 && "Expected a source!");
MVT ExtractVT = MVT::getVectorVT(MVT::i1, SubvecElts);
SDValue Extract = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, ExtractVT,
!(Subtarget.is64Bit() && VT == MVT::i64))
return SDValue();
- unsigned Lg2 = Divisor.countTrailingZeros();
+ unsigned Lg2 = Divisor.countr_zero();
// If the divisor is 2 or -2, the default expansion is better.
if (Lg2 == 1)
EVT SrcVT = Op.getOperand(0).getValueType();
if (SrcVT.isVector()) {
unsigned NumSrcElts = SrcVT.getVectorNumElements();
- if (NumElts > NumSrcElts &&
- DemandedElts.countTrailingZeros() >= NumSrcElts)
+ if (NumElts > NumSrcElts && DemandedElts.countr_zero() >= NumSrcElts)
Known.setAllZero();
}
break;
EVT SrcVT = Op.getOperand(1).getValueType();
if (SrcVT.isVector()) {
unsigned NumSrcElts = SrcVT.getVectorNumElements();
- if (NumElts > NumSrcElts &&
- DemandedElts.countTrailingZeros() >= NumSrcElts)
+ if (NumElts > NumSrcElts && DemandedElts.countr_zero() >= NumSrcElts)
Known.setAllZero();
}
break;
}
case X86ISD::MOVQ2DQ: {
// Move from MMX to XMM. Upper half of XMM should be 0.
- if (DemandedElts.countTrailingZeros() >= (NumElts / 2))
+ if (DemandedElts.countr_zero() >= (NumElts / 2))
Known.setAllZero();
break;
}
// single shift. We can do this if the bottom bits (which are shifted
// out) are never demanded.
if (Op0.getOpcode() == X86ISD::VSRLI &&
- OriginalDemandedBits.countTrailingZeros() >= ShAmt) {
+ OriginalDemandedBits.countr_zero() >= ShAmt) {
unsigned Shift2Amt = Op0.getConstantOperandVal(1);
if (Shift2Amt < BitWidth) {
int Diff = ShAmt - Shift2Amt;
// If we are only demanding sign bits then we can use the shift source directly.
unsigned NumSignBits =
TLO.DAG.ComputeNumSignBits(Op0, OriginalDemandedElts, Depth + 1);
- unsigned UpperDemandedBits =
- BitWidth - OriginalDemandedBits.countTrailingZeros();
+ unsigned UpperDemandedBits = BitWidth - OriginalDemandedBits.countr_zero();
if (NumSignBits > ShAmt && (NumSignBits - ShAmt) >= UpperDemandedBits)
return TLO.CombineTo(Op, Op0);
// If any of the demanded bits are produced by the sign extension, we also
// demand the input sign bit.
- if (OriginalDemandedBits.countLeadingZeros() < ShAmt)
+ if (OriginalDemandedBits.countl_zero() < ShAmt)
DemandedMask.setSignBit();
if (SimplifyDemandedBits(Op0, DemandedMask, OriginalDemandedElts, Known,
// If the input sign bit is known to be zero, or if none of the top bits
// are demanded, turn this into an unsigned shift right.
if (Known.Zero[BitWidth - ShAmt - 1] ||
- OriginalDemandedBits.countLeadingZeros() >= ShAmt)
+ OriginalDemandedBits.countl_zero() >= ShAmt)
return TLO.CombineTo(
Op, TLO.DAG.getNode(X86ISD::VSRLI, SDLoc(Op), VT, Op0, Op1));
// Don't attempt this on AVX512 as it might affect broadcast folding.
// TODO: Should we attempt this for i32/i16 splats? They tend to be slower.
if ((BitWidth == 64) && SrcVT.isScalarInteger() && !Subtarget.hasAVX512() &&
- OriginalDemandedBits.countLeadingZeros() >= (BitWidth / 2) &&
+ OriginalDemandedBits.countl_zero() >= (BitWidth / 2) &&
Src->hasOneUse()) {
MVT NewSrcVT = MVT::getIntegerVT(BitWidth / 2);
SDValue NewSrc =
unsigned NumElts = SrcVT.getVectorNumElements();
// If we don't need the sign bits at all just return zero.
- if (OriginalDemandedBits.countTrailingZeros() >= NumElts)
+ if (OriginalDemandedBits.countr_zero() >= NumElts)
return TLO.CombineTo(Op, TLO.DAG.getConstant(0, SDLoc(Op), VT));
// See if we only demand bits from the lower 128-bit vector.
SDValue Op0 = Op.getOperand(0);
SDValue Op1 = Op.getOperand(1);
- unsigned DemandedBitsLZ = OriginalDemandedBits.countLeadingZeros();
+ unsigned DemandedBitsLZ = OriginalDemandedBits.countl_zero();
APInt LoMask = APInt::getLowBitsSet(BitWidth, BitWidth - DemandedBitsLZ);
// If the demanded bits has leading zeroes, we don't demand those from the
unsigned ShAmt = Op.getConstantOperandVal(1);
unsigned BitWidth = DemandedBits.getBitWidth();
unsigned NumSignBits = DAG.ComputeNumSignBits(Op0, DemandedElts, Depth + 1);
- unsigned UpperDemandedBits = BitWidth - DemandedBits.countTrailingZeros();
+ unsigned UpperDemandedBits = BitWidth - DemandedBits.countr_zero();
if (NumSignBits > ShAmt && (NumSignBits - ShAmt) >= UpperDemandedBits)
return Op0;
break;
"Multiple identity shuffles detected");
if (IdentityOp != 0)
- return DAG.getBitcast(VT, ShuffleOps[IdentityOp.countTrailingZeros()]);
+ return DAG.getBitcast(VT, ShuffleOps[IdentityOp.countr_zero()]);
}
}
if (getTargetConstantBitsFromNode(N0, 64, UndefElts, EltBits)) {
SDLoc DL(N0);
// Handle zero-extension of i32 with MOVD.
- if (EltBits[0].countLeadingZeros() >= 32)
+ if (EltBits[0].countl_zero() >= 32)
return DAG.getNode(X86ISD::MMX_MOVW2D, DL, VT,
DAG.getConstant(EltBits[0].trunc(32), DL, MVT::i32));
// Else, bitcast to a double.
// If this can be matched by a zero extend, don't optimize.
if (MaskVal.isMask()) {
- unsigned TO = MaskVal.countTrailingOnes();
+ unsigned TO = MaskVal.countr_one();
if (TO >= 8 && isPowerOf2_32(TO))
return SDValue();
}
return SDValue();
SDLoc DL(N);
- unsigned ShiftVal = SplatVal.countTrailingOnes();
+ unsigned ShiftVal = SplatVal.countr_one();
SDValue ShAmt = DAG.getTargetConstant(EltBitWidth - ShiftVal, DL, MVT::i8);
SDValue Shift = DAG.getNode(X86ISD::VSRLI, DL, VT, Op0, ShAmt);
return DAG.getBitcast(N->getValueType(0), Shift);
auto *ConstNode = dyn_cast<ConstantSDNode>(Op);
if (!ConstNode)
return -1;
- if (ConstNode->getAPIntValue().countTrailingOnes() >= 1) {
+ if (ConstNode->getAPIntValue().countr_one() >= 1) {
// If we already found a one, this is too many.
if (TrueIndex >= 0)
return -1;
ShiftVT = MVT::getVectorVT(MVT::i16, NumElts / 2);
LHS = DAG.getBitcast(ShiftVT, LHS);
}
- unsigned ShiftAmt = KnownRHS.getConstant().countLeadingZeros();
+ unsigned ShiftAmt = KnownRHS.getConstant().countl_zero();
LHS = getTargetVShiftByConstNode(X86ISD::VSHLI, DL, ShiftVT, LHS,
ShiftAmt, DAG);
LHS = DAG.getNOT(DL, DAG.getBitcast(SrcVT, LHS), SrcVT);
def BTRXForm : SDNodeXForm<imm, [{
// Transformation function: Find the lowest 0.
- return getI64Imm((uint8_t)N->getAPIntValue().countTrailingOnes(), SDLoc(N));
+ return getI64Imm((uint8_t)N->getAPIntValue().countr_one(), SDLoc(N));
}]>;
def BTCBTSXForm : SDNodeXForm<imm, [{
// Transformation function: Find the lowest 1.
- return getI64Imm((uint8_t)N->getAPIntValue().countTrailingZeros(), SDLoc(N));
+ return getI64Imm((uint8_t)N->getAPIntValue().countr_zero(), SDLoc(N));
}]>;
def BTRMask64 : ImmLeaf<i64, [{
// LHS = XOR(Y, C1), Y = AND(Z, C2), C1 == (C2 + 1) => LHS == NEG(OR(Z, ~C2))
// ADD(LHS, RHS) == SUB(RHS, OR(Z, ~C2))
if (match(LHS, m_Xor(m_Value(Y), m_APInt(C1))))
- if (C1->countTrailingZeros() == 0)
+ if (C1->countr_zero() == 0)
if (match(Y, m_And(m_Value(Z), m_APInt(C2))) && *C1 == (*C2 + 1)) {
Value *NewOr = Builder.CreateOr(Z, ~(*C2));
return Builder.CreateSub(RHS, NewOr, "sub");
// (A & 2^C1) + A => A & (2^C1 - 1) iff bit C1 in A is a sign bit
if (match(&I, m_c_Add(m_And(m_Value(A), m_APInt(C1)), m_Deferred(A))) &&
- C1->isPowerOf2() && (ComputeNumSignBits(A) > C1->countLeadingZeros())) {
+ C1->isPowerOf2() && (ComputeNumSignBits(A) > C1->countl_zero())) {
Constant *NewMask = ConstantInt::get(RHS->getType(), *C1 - 1);
return BinaryOperator::CreateAnd(A, NewMask);
}
const APInt *NegPow2C;
if (match(&I, m_c_Add(m_OneUse(m_Mul(m_Value(A), m_NegatedPower2(NegPow2C))),
m_Value(B)))) {
- Constant *ShiftAmtC = ConstantInt::get(Ty, NegPow2C->countTrailingZeros());
+ Constant *ShiftAmtC = ConstantInt::get(Ty, NegPow2C->countr_zero());
Value *Shl = Builder.CreateShl(A, ShiftAmtC);
return BinaryOperator::CreateSub(B, Shl);
}
const APInt *AddC, *AndC;
if (match(Op0, m_Add(m_Value(X), m_APInt(AddC))) &&
match(Op1, m_And(m_Specific(X), m_APInt(AndC)))) {
- unsigned Cttz = AddC->countTrailingZeros();
+ unsigned Cttz = AddC->countr_zero();
APInt HighMask(APInt::getHighBitsSet(BitWidth, BitWidth - Cttz));
if ((HighMask & *AndC).isZero())
return BinaryOperator::CreateAnd(Op0, ConstantInt::get(Ty, ~(*AndC)));
return nullptr;
unsigned Width = Ty->getScalarSizeInBits();
- unsigned LastOneMath = Width - C2->countTrailingZeros();
+ unsigned LastOneMath = Width - C2->countr_zero();
switch (OpC) {
case Instruction::And:
- if (C->countLeadingOnes() < LastOneMath)
+ if (C->countl_one() < LastOneMath)
return nullptr;
break;
case Instruction::Xor:
case Instruction::Or:
- if (C->countLeadingZeros() < LastOneMath)
+ if (C->countl_zero() < LastOneMath)
return nullptr;
break;
default:
if (match(Op0, m_Add(m_Value(X), m_APInt(AddC)))) {
// If we add zeros to every bit below a mask, the add has no effect:
// (X + AddC) & LowMaskC --> X & LowMaskC
- unsigned Ctlz = C->countLeadingZeros();
+ unsigned Ctlz = C->countl_zero();
APInt LowMask(APInt::getLowBitsSet(Width, Width - Ctlz));
if ((*AddC & LowMask).isZero())
return BinaryOperator::CreateAnd(X, Op1);
const APInt *C3 = C;
Value *X;
if (C3->isPowerOf2()) {
- Constant *Log2C3 = ConstantInt::get(Ty, C3->countTrailingZeros());
+ Constant *Log2C3 = ConstantInt::get(Ty, C3->countr_zero());
if (match(Op0, m_OneUse(m_LShr(m_Shl(m_ImmConstant(C1), m_Value(X)),
m_ImmConstant(C2)))) &&
match(C1, m_Power2())) {
if (!Op1C->isZero() == (Pred == ICmpInst::ICMP_NE)) {
// sext ((x & 2^n) == 0) -> (x >> n) - 1
// sext ((x & 2^n) != 2^n) -> (x >> n) - 1
- unsigned ShiftAmt = KnownZeroMask.countTrailingZeros();
+ unsigned ShiftAmt = KnownZeroMask.countr_zero();
// Perform a right shift to place the desired bit in the LSB.
if (ShiftAmt)
In = Builder.CreateLShr(In,
} else {
// sext ((x & 2^n) != 0) -> (x << bitwidth-n) a>> bitwidth-1
// sext ((x & 2^n) == 2^n) -> (x << bitwidth-n) a>> bitwidth-1
- unsigned ShiftAmt = KnownZeroMask.countLeadingZeros();
+ unsigned ShiftAmt = KnownZeroMask.countl_zero();
// Perform a left shift to place the desired bit in the MSB.
if (ShiftAmt)
In = Builder.CreateShl(In,
int Shift;
if (IsAShr && AP1.isNegative())
- Shift = AP1.countLeadingOnes() - AP2.countLeadingOnes();
+ Shift = AP1.countl_one() - AP2.countl_one();
else
- Shift = AP1.countLeadingZeros() - AP2.countLeadingZeros();
+ Shift = AP1.countl_zero() - AP2.countl_zero();
if (Shift > 0) {
if (IsAShr && AP1 == AP2.ashr(Shift)) {
if (AP2.isZero())
return nullptr;
- unsigned AP2TrailingZeros = AP2.countTrailingZeros();
+ unsigned AP2TrailingZeros = AP2.countr_zero();
if (!AP1 && AP2TrailingZeros != 0)
return getICmp(
return getICmp(I.ICMP_EQ, A, ConstantInt::getNullValue(A->getType()));
// Get the distance between the lowest bits that are set.
- int Shift = AP1.countTrailingZeros() - AP2TrailingZeros;
+ int Shift = AP1.countr_zero() - AP2TrailingZeros;
if (Shift > 0 && AP2.shl(Shift) == AP1)
return getICmp(I.ICMP_EQ, A, ConstantInt::get(A->getType(), Shift));
// If CI2 is 2^7, 2^15, 2^31, then it might be an sadd.with.overflow.
if (!CI2->getValue().isPowerOf2())
return nullptr;
- unsigned NewWidth = CI2->getValue().countTrailingZeros();
+ unsigned NewWidth = CI2->getValue().countr_zero();
if (NewWidth != 7 && NewWidth != 15 && NewWidth != 31)
return nullptr;
KnownBits Known = computeKnownBits(X, 0, &Cmp);
// If all the high bits are known, we can do this xform.
- if ((Known.Zero | Known.One).countLeadingOnes() >= SrcBits - DstBits) {
+ if ((Known.Zero | Known.One).countl_one() >= SrcBits - DstBits) {
// Pull in the high bits from known-ones set.
APInt NewRHS = C.zext(SrcBits);
NewRHS |= Known.One & APInt::getHighBitsSet(SrcBits, SrcBits - DstBits);
// free on the target. It has the additional benefit of comparing to a
// smaller constant that may be more target-friendly.
unsigned Amt = ShiftAmt->getLimitedValue(TypeBits - 1);
- if (Shl->hasOneUse() && Amt != 0 && C.countTrailingZeros() >= Amt &&
+ if (Shl->hasOneUse() && Amt != 0 && C.countr_zero() >= Amt &&
DL.isLegalInteger(TypeBits - Amt)) {
Type *TruncTy = IntegerType::get(Cmp.getContext(), TypeBits - Amt);
if (auto *ShVTy = dyn_cast<VectorType>(ShType))
assert(ShiftValC->uge(C) && "Expected simplify of compare");
assert((IsUGT || !C.isZero()) && "Expected X u< 0 to simplify");
- unsigned CmpLZ =
- IsUGT ? C.countLeadingZeros() : (C - 1).countLeadingZeros();
- unsigned ShiftLZ = ShiftValC->countLeadingZeros();
+ unsigned CmpLZ = IsUGT ? C.countl_zero() : (C - 1).countl_zero();
+ unsigned ShiftLZ = ShiftValC->countl_zero();
Constant *NewC = ConstantInt::get(Shr->getType(), CmpLZ - ShiftLZ);
auto NewPred = IsUGT ? CmpInst::ICMP_ULT : CmpInst::ICMP_UGE;
return new ICmpInst(NewPred, Shr->getOperand(1), NewC);
!C->isOne()) {
// icmp eq/ne (X * C), (Y * C) --> icmp (X & Mask), (Y & Mask)
// Mask = -1 >> count-trailing-zeros(C).
- if (unsigned TZs = C->countTrailingZeros()) {
+ if (unsigned TZs = C->countr_zero()) {
Constant *Mask = ConstantInt::get(
BO0->getType(),
APInt::getLowBitsSet(C->getBitWidth(), C->getBitWidth() - TZs));
// (B & (Pow2C-1)) != zext A --> A != trunc B
const APInt *MaskC;
if (match(Op0, m_And(m_Value(B), m_LowBitMask(MaskC))) &&
- MaskC->countTrailingOnes() == A->getType()->getScalarSizeInBits())
+ MaskC->countr_one() == A->getType()->getScalarSizeInBits())
return new ICmpInst(Pred, A, Builder.CreateTrunc(B, A->getType()));
}
return nullptr;
if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->getOperand(1))) {
const APInt &CVal = CI->getValue();
- if (CVal.getBitWidth() - CVal.countLeadingZeros() > MulWidth)
+ if (CVal.getBitWidth() - CVal.countl_zero() > MulWidth)
return nullptr;
} else {
// In this case we could have the operand of the binary operation
// bits doesn't impact the outcome of the comparison, because any value
// greater than the RHS must differ in a bit higher than these due to carry.
case ICmpInst::ICMP_UGT:
- return APInt::getBitsSetFrom(BitWidth, RHS->countTrailingOnes());
+ return APInt::getBitsSetFrom(BitWidth, RHS->countr_one());
// Similarly, for a ULT comparison, we don't care about the trailing zeros.
// Any value less than the RHS must differ in a higher bit because of carries.
case ICmpInst::ICMP_ULT:
- return APInt::getBitsSetFrom(BitWidth, RHS->countTrailingZeros());
+ return APInt::getBitsSetFrom(BitWidth, RHS->countr_zero());
default:
return APInt::getAllOnes(BitWidth);
const APInt *C1;
if (match(LHS, m_Shl(m_Power2(C1), m_Value(X)))) {
Type *XTy = X->getType();
- unsigned Log2C1 = C1->countTrailingZeros();
+ unsigned Log2C1 = C1->countr_zero();
APInt C2 = Op0KnownZeroInverted;
APInt C2Pow2 = (C2 & ~(*C1 - 1)) + *C1;
if (C2Pow2.isPowerOf2()) {
// iff (C1 is pow2) & ((C2 & ~(C1-1)) + C1) is pow2):
// ((C1 << X) & C2) == 0 -> X >= (Log2(C2+C1) - Log2(C1))
// ((C1 << X) & C2) != 0 -> X < (Log2(C2+C1) - Log2(C1))
- unsigned Log2C2 = C2Pow2.countTrailingZeros();
+ unsigned Log2C2 = C2Pow2.countr_zero();
auto *CmpC = ConstantInt::get(XTy, Log2C2 - Log2C1);
auto NewPred =
Pred == CmpInst::ICMP_EQ ? CmpInst::ICMP_UGE : CmpInst::ICMP_ULT;
if (match(Op0, m_ZExtOrSExt(m_Value(X))) &&
match(Op1, m_APIntAllowUndef(NegPow2C))) {
unsigned SrcWidth = X->getType()->getScalarSizeInBits();
- unsigned ShiftAmt = NegPow2C->countTrailingZeros();
+ unsigned ShiftAmt = NegPow2C->countr_zero();
if (ShiftAmt >= BitWidth - SrcWidth) {
Value *N = Builder.CreateNeg(X, X->getName() + ".neg");
Value *Z = Builder.CreateZExt(N, Ty, N->getName() + ".z");
KnownBits KnownDividend = computeKnownBits(Op0, 0, &I);
if (!I.isExact() &&
(match(Op1, m_Power2(Op1C)) || match(Op1, m_NegatedPower2(Op1C))) &&
- KnownDividend.countMinTrailingZeros() >= Op1C->countTrailingZeros()) {
+ KnownDividend.countMinTrailingZeros() >= Op1C->countr_zero()) {
I.setIsExact();
return &I;
}
const APInt *MulConst;
// We can fold (shr (mul X, -(1 << C)), C) -> (and (neg X), C`)
return !IsLeftShift && match(I->getOperand(1), m_APInt(MulConst)) &&
- MulConst->isNegatedPowerOf2() &&
- MulConst->countTrailingZeros() == NumBits;
+ MulConst->isNegatedPowerOf2() && MulConst->countr_zero() == NumBits;
}
}
}
// If the high-bits of an ADD/SUB/MUL are not demanded, then we do not care
// about the high bits of the operands.
auto simplifyOperandsBasedOnUnusedHighBits = [&](APInt &DemandedFromOps) {
- unsigned NLZ = DemandedMask.countLeadingZeros();
+ unsigned NLZ = DemandedMask.countl_zero();
// Right fill the mask of bits for the operands to demand the most
// significant bit and all those below it.
DemandedFromOps = APInt::getLowBitsSet(BitWidth, BitWidth - NLZ);
// The shift amount must be valid (not poison) in the narrow type, and
// it must not be greater than the high bits demanded of the result.
if (C->ult(VTy->getScalarSizeInBits()) &&
- C->ule(DemandedMask.countLeadingZeros())) {
+ C->ule(DemandedMask.countl_zero())) {
// trunc (lshr X, C) --> lshr (trunc X), C
IRBuilderBase::InsertPointGuard Guard(Builder);
Builder.SetInsertPoint(I);
// Right fill the mask of bits for the operands to demand the most
// significant bit and all those below it.
- unsigned NLZ = DemandedMask.countLeadingZeros();
+ unsigned NLZ = DemandedMask.countl_zero();
APInt DemandedFromOps = APInt::getLowBitsSet(BitWidth, BitWidth - NLZ);
if (ShrinkDemandedConstant(I, 1, DemandedFromOps) ||
SimplifyDemandedBits(I, 1, DemandedFromOps, RHSKnown, Depth + 1))
// If low order bits are not demanded and known to be zero in one operand,
// then we don't need to demand them from the other operand, since they
// can't cause overflow into any bits that are demanded in the result.
- unsigned NTZ = (~DemandedMask & RHSKnown.Zero).countTrailingOnes();
+ unsigned NTZ = (~DemandedMask & RHSKnown.Zero).countr_one();
APInt DemandedFromLHS = DemandedFromOps;
DemandedFromLHS.clearLowBits(NTZ);
if (ShrinkDemandedConstant(I, 0, DemandedFromLHS) ||
case Instruction::Sub: {
// Right fill the mask of bits for the operands to demand the most
// significant bit and all those below it.
- unsigned NLZ = DemandedMask.countLeadingZeros();
+ unsigned NLZ = DemandedMask.countl_zero();
APInt DemandedFromOps = APInt::getLowBitsSet(BitWidth, BitWidth - NLZ);
if (ShrinkDemandedConstant(I, 1, DemandedFromOps) ||
SimplifyDemandedBits(I, 1, DemandedFromOps, RHSKnown, Depth + 1))
// If low order bits are not demanded and are known to be zero in RHS,
// then we don't need to demand them from LHS, since they can't cause a
// borrow from any bits that are demanded in the result.
- unsigned NTZ = (~DemandedMask & RHSKnown.Zero).countTrailingOnes();
+ unsigned NTZ = (~DemandedMask & RHSKnown.Zero).countr_one();
APInt DemandedFromLHS = DemandedFromOps;
DemandedFromLHS.clearLowBits(NTZ);
if (ShrinkDemandedConstant(I, 0, DemandedFromLHS) ||
// The LSB of X*Y is set only if (X & 1) == 1 and (Y & 1) == 1.
// If we demand exactly one bit N and we have "X * (C' << N)" where C' is
// odd (has LSB set), then the left-shifted low bit of X is the answer.
- unsigned CTZ = DemandedMask.countTrailingZeros();
+ unsigned CTZ = DemandedMask.countr_zero();
const APInt *C;
- if (match(I->getOperand(1), m_APInt(C)) &&
- C->countTrailingZeros() == CTZ) {
+ if (match(I->getOperand(1), m_APInt(C)) && C->countr_zero() == CTZ) {
Constant *ShiftC = ConstantInt::get(VTy, CTZ);
Instruction *Shl = BinaryOperator::CreateShl(I->getOperand(0), ShiftC);
return InsertNewInstWith(Shl, *I);
uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
Value *X;
Constant *C;
- if (DemandedMask.countTrailingZeros() >= ShiftAmt &&
+ if (DemandedMask.countr_zero() >= ShiftAmt &&
match(I->getOperand(0), m_LShr(m_ImmConstant(C), m_Value(X)))) {
Constant *LeftShiftAmtC = ConstantInt::get(VTy, ShiftAmt);
Constant *NewC = ConstantExpr::getShl(C, LeftShiftAmtC);
// This is a variable shift, so we can't shift the demand mask by a known
// amount. But if we are not demanding high bits, then we are not
// demanding those bits from the pre-shifted operand either.
- if (unsigned CTLZ = DemandedMask.countLeadingZeros()) {
+ if (unsigned CTLZ = DemandedMask.countl_zero()) {
APInt DemandedFromOp(APInt::getLowBitsSet(BitWidth, BitWidth - CTLZ));
if (SimplifyDemandedBits(I, 0, DemandedFromOp, Known, Depth + 1)) {
// We can't guarantee that nsw/nuw hold after simplifying the operand.
// If we are just demanding the shifted sign bit and below, then this can
// be treated as an ASHR in disguise.
- if (DemandedMask.countLeadingZeros() >= ShiftAmt) {
+ if (DemandedMask.countl_zero() >= ShiftAmt) {
// If we only want bits that already match the signbit then we don't
// need to shift.
- unsigned NumHiDemandedBits =
- BitWidth - DemandedMask.countTrailingZeros();
+ unsigned NumHiDemandedBits = BitWidth - DemandedMask.countr_zero();
unsigned SignBits =
ComputeNumSignBits(I->getOperand(0), Depth + 1, CxtI);
if (SignBits >= NumHiDemandedBits)
// If we only want bits that already match the signbit then we don't need
// to shift.
- unsigned NumHiDemandedBits = BitWidth - DemandedMask.countTrailingZeros();
+ unsigned NumHiDemandedBits = BitWidth - DemandedMask.countr_zero();
if (SignBits >= NumHiDemandedBits)
return I->getOperand(0);
APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
// If any of the high bits are demanded, we should set the sign bit as
// demanded.
- if (DemandedMask.countLeadingZeros() <= ShiftAmt)
+ if (DemandedMask.countl_zero() <= ShiftAmt)
DemandedMaskIn.setSignBit();
// If the shift is exact, then it does demand the low bits (and knows that
const APInt *SA;
if (match(I->getOperand(1), m_APInt(SA))) {
// TODO: Take the demanded mask of the result into account.
- unsigned RHSTrailingZeros = SA->countTrailingZeros();
+ unsigned RHSTrailingZeros = SA->countr_zero();
APInt DemandedMaskIn =
APInt::getHighBitsSet(BitWidth, BitWidth - RHSTrailingZeros);
if (SimplifyDemandedBits(I, 0, DemandedMaskIn, LHSKnown, Depth + 1)) {
}
// Increase high zero bits from the input.
- Known.Zero.setHighBits(std::min(
- BitWidth, LHSKnown.Zero.countLeadingOnes() + RHSTrailingZeros));
+ Known.Zero.setHighBits(
+ std::min(BitWidth, LHSKnown.Zero.countl_one() + RHSTrailingZeros));
} else {
computeKnownBits(I, Known, Depth, CxtI);
}
case Intrinsic::bswap: {
// If the only bits demanded come from one byte of the bswap result,
// just shift the input byte into position to eliminate the bswap.
- unsigned NLZ = DemandedMask.countLeadingZeros();
- unsigned NTZ = DemandedMask.countTrailingZeros();
+ unsigned NLZ = DemandedMask.countl_zero();
+ unsigned NTZ = DemandedMask.countr_zero();
// Round NTZ down to the next byte. If we have 11 trailing zeros, then
// we need all the bits down to bit 8. Likewise, round NLZ. If we
// The lowest non-zero bit of DemandMask is higher than the highest
// non-zero bit of C.
const APInt *C;
- unsigned CTZ = DemandedMask.countTrailingZeros();
+ unsigned CTZ = DemandedMask.countr_zero();
if (match(II->getArgOperand(1), m_APInt(C)) &&
CTZ >= C->getActiveBits())
return II->getArgOperand(0);
// non-one bit of C.
// This comes from using DeMorgans on the above umax example.
const APInt *C;
- unsigned CTZ = DemandedMask.countTrailingZeros();
+ unsigned CTZ = DemandedMask.countr_zero();
if (match(II->getArgOperand(1), m_APInt(C)) &&
- CTZ >= C->getBitWidth() - C->countLeadingOnes())
+ CTZ >= C->getBitWidth() - C->countl_one())
return II->getArgOperand(0);
break;
}
break;
}
case Instruction::Add: {
- unsigned NLZ = DemandedMask.countLeadingZeros();
+ unsigned NLZ = DemandedMask.countl_zero();
APInt DemandedFromOps = APInt::getLowBitsSet(BitWidth, BitWidth - NLZ);
// If an operand adds zeros to every bit below the highest demanded bit,
break;
}
case Instruction::Sub: {
- unsigned NLZ = DemandedMask.countLeadingZeros();
+ unsigned NLZ = DemandedMask.countl_zero();
APInt DemandedFromOps = APInt::getLowBitsSet(BitWidth, BitWidth - NLZ);
// If an operand subtracts zeros from every bit below the highest demanded
// Compute the number of leading bits we can ignore.
// TODO: A better way to determine this would use ComputeNumSignBits().
for (const auto &C : SI.cases()) {
- LeadingKnownZeros = std::min(
- LeadingKnownZeros, C.getCaseValue()->getValue().countLeadingZeros());
- LeadingKnownOnes = std::min(
- LeadingKnownOnes, C.getCaseValue()->getValue().countLeadingOnes());
+ LeadingKnownZeros =
+ std::min(LeadingKnownZeros, C.getCaseValue()->getValue().countl_zero());
+ LeadingKnownOnes =
+ std::min(LeadingKnownOnes, C.getCaseValue()->getValue().countl_one());
}
unsigned NewWidth = Known.getBitWidth() - std::max(LeadingKnownZeros, LeadingKnownOnes);
if (ConstantInt *Elt =
dyn_cast<ConstantInt>(ConstArg->getAggregateElement(Idx))) {
const APInt &V = Elt->getValue();
- APInt V2 = APInt(V.getBitWidth(), 1) << V.countTrailingZeros();
+ APInt V2 = APInt(V.getBitWidth(), 1) << V.countr_zero();
Elements.push_back(ConstantInt::get(EltTy, V2));
} else {
Elements.push_back(ConstantInt::get(EltTy, 1));
} else {
if (ConstantInt *Elt = dyn_cast<ConstantInt>(ConstArg)) {
const APInt &V = Elt->getValue();
- APInt V2 = APInt(V.getBitWidth(), 1) << V.countTrailingZeros();
+ APInt V2 = APInt(V.getBitWidth(), 1) << V.countr_zero();
ShadowMul = ConstantInt::get(Ty, V2);
} else {
ShadowMul = ConstantInt::get(Ty, 1);
const uint32_t SrcBitSize = SE->getSrcTy()->getScalarSizeInBits();
auto *const DstTy = SE->getDestTy();
const uint32_t DestBitSize = DstTy->getScalarSizeInBits();
- if (Demanded.countLeadingZeros() >= (DestBitSize - SrcBitSize)) {
+ if (Demanded.countl_zero() >= (DestBitSize - SrcBitSize)) {
clearAssumptionsOfUsers(SE, DB);
IRBuilder<> Builder(SE);
I.replaceAllUsesWith(
if ((C->getAPInt() + NewF.BaseOffset)
.abs()
.slt(std::abs(NewF.BaseOffset)) &&
- (C->getAPInt() + NewF.BaseOffset).countTrailingZeros() >=
+ (C->getAPInt() + NewF.BaseOffset).countr_zero() >=
(unsigned)llvm::countr_zero<uint64_t>(NewF.BaseOffset))
goto skip_formula;
// we can truncate the roots to this narrower type.
for (auto *Root : TreeRoot) {
auto Mask = DB->getDemandedBits(cast<Instruction>(Root));
- MaxBitWidth = std::max<unsigned>(
- Mask.getBitWidth() - Mask.countLeadingZeros(), MaxBitWidth);
+ MaxBitWidth = std::max<unsigned>(Mask.getBitWidth() - Mask.countl_zero(),
+ MaxBitWidth);
}
// True if the roots can be zero-extended back to their original type, rather
auto CheckBits = [&](unsigned NumBits, unsigned Idx) {
Res = GetKB(Idx);
EXPECT_EQ(64u, Res.getBitWidth());
- EXPECT_EQ(NumBits - 1, Res.Zero.countTrailingOnes());
- EXPECT_EQ(64u, Res.One.countTrailingZeros());
+ EXPECT_EQ(NumBits - 1, Res.Zero.countr_one());
+ EXPECT_EQ(64u, Res.One.countr_zero());
EXPECT_EQ(Align(1ull << (NumBits - 1)), Info.computeKnownAlignment(Copies[Idx]));
};
GISelKnownBits Info(*MF);
KnownBits Res = Info.getKnownBits(CopyOverflow);
EXPECT_EQ(0u, Res.One.getZExtValue());
- EXPECT_EQ(31u, Res.Zero.countLeadingOnes());
+ EXPECT_EQ(31u, Res.Zero.countl_one());
}
}
TEST_F(ConstantRangeTest, Ctlz) {
- TestUnaryOpExhaustive([](const ConstantRange &CR) { return CR.ctlz(); },
- [](const APInt &N) {
- return APInt(N.getBitWidth(), N.countLeadingZeros());
- });
+ TestUnaryOpExhaustive(
+ [](const ConstantRange &CR) { return CR.ctlz(); },
+ [](const APInt &N) { return APInt(N.getBitWidth(), N.countl_zero()); });
TestUnaryOpExhaustive(
[](const ConstantRange &CR) { return CR.ctlz(/*ZeroIsPoison=*/true); },
[](const APInt &N) -> std::optional<APInt> {
if (N.isZero())
return std::nullopt;
- return APInt(N.getBitWidth(), N.countLeadingZeros());
+ return APInt(N.getBitWidth(), N.countl_zero());
});
}
TEST_F(DIEnumeratorTest, getWithLargeValues) {
auto *N = DIEnumerator::get(Context, APInt::getMaxValue(128), false, "val");
- EXPECT_EQ(128U, N->getValue().countPopulation());
+ EXPECT_EQ(128U, N->getValue().popcount());
EXPECT_EQ(N,
DIEnumerator::get(Context, APInt::getMaxValue(128), false, "val"));
EXPECT_NE(N,
unsigned Bits = Numerator.getBitWidth();
if (LZOptimization) {
- unsigned LeadingZeros = Numerator.countLeadingZeros();
+ unsigned LeadingZeros = Numerator.countl_zero();
// Clip to the number of leading zeros in the divisor.
- LeadingZeros = std::min(LeadingZeros, Divisor.countLeadingZeros());
+ LeadingZeros = std::min(LeadingZeros, Divisor.countl_zero());
if (LeadingZeros > 0) {
Magics = UnsignedDivisionByConstantInfo::get(
Divisor, LeadingZeros, AllowEvenDivisorOptimization);
unsigned RhsCountPopulation = Rhs.popcount();
return ((LhsCountPopulation < RhsCountPopulation) ||
((LhsCountPopulation == RhsCountPopulation) &&
- (Lhs.countLeadingZeros() > Rhs.countLeadingZeros())));
+ (Lhs.countl_zero() > Rhs.countl_zero())));
};
if (LhsMasks.first != RhsMasks.first)
} else if (width < result.getBitWidth()) {
// The parser can return an unnecessarily wide result with leading zeros.
// This isn't a problem, but truncating off bits is bad.
- if (result.countLeadingZeros() < result.getBitWidth() - width)
+ if (result.countl_zero() < result.getBitWidth() - width)
return std::nullopt;
result = result.trunc(width);
OpFoldResult math::CountLeadingZerosOp::fold(FoldAdaptor adaptor) {
return constFoldUnaryOp<IntegerAttr>(
- adaptor.getOperands(), [](const APInt &a) {
- return APInt(a.getBitWidth(), a.countLeadingZeros());
- });
+ adaptor.getOperands(),
+ [](const APInt &a) { return APInt(a.getBitWidth(), a.countl_zero()); });
}
//===----------------------------------------------------------------------===//
OpFoldResult math::CountTrailingZerosOp::fold(FoldAdaptor adaptor) {
return constFoldUnaryOp<IntegerAttr>(
- adaptor.getOperands(), [](const APInt &a) {
- return APInt(a.getBitWidth(), a.countTrailingZeros());
- });
+ adaptor.getOperands(),
+ [](const APInt &a) { return APInt(a.getBitWidth(), a.countr_zero()); });
}
//===----------------------------------------------------------------------===//
widenBitwiseBounds(const ConstantIntRanges &bound) {
APInt leftVal = bound.umin(), rightVal = bound.umax();
unsigned bitwidth = leftVal.getBitWidth();
- unsigned differingBits = bitwidth - (leftVal ^ rightVal).countLeadingZeros();
+ unsigned differingBits = bitwidth - (leftVal ^ rightVal).countl_zero();
leftVal.clearLowBits(differingBits);
rightVal.setLowBits(differingBits);
return std::make_tuple(std::move(leftVal), std::move(rightVal));