Skip to content

Commit ef1372a

Browse files
authored
[KnownBits] Add setAllConflict to set all bits in Zero and One. NFC (llvm#159815)
This is a common pattern to initialize Knownbits that occurs before loops that call intersectWith.
1 parent 81c0c73 commit ef1372a

File tree

5 files changed

+28
-36
lines changed

5 files changed

+28
-36
lines changed

llvm/include/llvm/Support/KnownBits.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ struct KnownBits {
9494
One.setAllBits();
9595
}
9696

97+
/// Make all bits known to be both zero and one. Useful before a loop that
98+
/// calls intersectWith.
99+
void setAllConflict() {
100+
Zero.setAllBits();
101+
One.setAllBits();
102+
}
103+
97104
/// Returns true if this value is known to be negative.
98105
bool isNegative() const { return One.isSignBitSet(); }
99106

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -441,8 +441,7 @@ void llvm::computeKnownBitsFromRangeMetadata(const MDNode &Ranges,
441441
unsigned NumRanges = Ranges.getNumOperands() / 2;
442442
assert(NumRanges >= 1);
443443

444-
Known.Zero.setAllBits();
445-
Known.One.setAllBits();
444+
Known.setAllConflict();
446445

447446
for (unsigned i = 0; i < NumRanges; ++i) {
448447
ConstantInt *Lower =
@@ -1328,8 +1327,7 @@ static void computeKnownBitsFromOperator(const Operator *I,
13281327
break;
13291328

13301329
if (Result.isKnownNever(fcNormal | fcSubnormal | fcNan)) {
1331-
Known.Zero.setAllBits();
1332-
Known.One.setAllBits();
1330+
Known.setAllConflict();
13331331

13341332
if (FPClasses & fcInf)
13351333
Known = Known.intersectWith(KnownBits::makeConstant(
@@ -1405,8 +1403,7 @@ static void computeKnownBitsFromOperator(const Operator *I,
14051403
computeKnownBits(I->getOperand(0), SubDemandedElts, KnownSrc, Q,
14061404
Depth + 1);
14071405

1408-
Known.Zero.setAllBits();
1409-
Known.One.setAllBits();
1406+
Known.setAllConflict();
14101407
for (unsigned i = 0; i != NumElts; ++i) {
14111408
if (DemandedElts[i]) {
14121409
unsigned Shifts = IsLE ? i : NumElts - 1 - i;
@@ -1738,8 +1735,7 @@ static void computeKnownBitsFromOperator(const Operator *I,
17381735
if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
17391736
break;
17401737

1741-
Known.Zero.setAllBits();
1742-
Known.One.setAllBits();
1738+
Known.setAllConflict();
17431739
for (const Use &U : P->operands()) {
17441740
Value *IncValue;
17451741
const PHINode *CxtPhi;
@@ -2083,8 +2079,7 @@ static void computeKnownBitsFromOperator(const Operator *I,
20832079
Known.resetAll();
20842080
return;
20852081
}
2086-
Known.One.setAllBits();
2087-
Known.Zero.setAllBits();
2082+
Known.setAllConflict();
20882083
if (!!DemandedLHS) {
20892084
const Value *LHS = Shuf->getOperand(0);
20902085
computeKnownBits(LHS, DemandedLHS, Known, Q, Depth + 1);
@@ -2116,8 +2111,7 @@ static void computeKnownBitsFromOperator(const Operator *I,
21162111
NeedsElt = DemandedElts[CIdx->getZExtValue()];
21172112
}
21182113

2119-
Known.One.setAllBits();
2120-
Known.Zero.setAllBits();
2114+
Known.setAllConflict();
21212115
if (NeedsElt) {
21222116
computeKnownBits(Elt, Known, Q, Depth + 1);
21232117
// If we don't know any bits, early out.
@@ -2273,7 +2267,7 @@ void computeKnownBits(const Value *V, const APInt &DemandedElts,
22732267
assert(!isa<ScalableVectorType>(V->getType()));
22742268
// We know that CDV must be a vector of integers. Take the intersection of
22752269
// each element.
2276-
Known.Zero.setAllBits(); Known.One.setAllBits();
2270+
Known.setAllConflict();
22772271
for (unsigned i = 0, e = CDV->getNumElements(); i != e; ++i) {
22782272
if (!DemandedElts[i])
22792273
continue;
@@ -2290,7 +2284,7 @@ void computeKnownBits(const Value *V, const APInt &DemandedElts,
22902284
assert(!isa<ScalableVectorType>(V->getType()));
22912285
// We know that CV must be a vector of integers. Take the intersection of
22922286
// each element.
2293-
Known.Zero.setAllBits(); Known.One.setAllBits();
2287+
Known.setAllConflict();
22942288
for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
22952289
if (!DemandedElts[i])
22962290
continue;

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3457,7 +3457,7 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
34573457
case ISD::BUILD_VECTOR:
34583458
assert(!Op.getValueType().isScalableVector());
34593459
// Collect the known bits that are shared by every demanded vector element.
3460-
Known.Zero.setAllBits(); Known.One.setAllBits();
3460+
Known.setAllConflict();
34613461
for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
34623462
if (!DemandedElts[i])
34633463
continue;
@@ -3492,7 +3492,7 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
34923492
break;
34933493

34943494
// Known bits are the values that are shared by every demanded element.
3495-
Known.Zero.setAllBits(); Known.One.setAllBits();
3495+
Known.setAllConflict();
34963496
if (!!DemandedLHS) {
34973497
SDValue LHS = Op.getOperand(0);
34983498
Known2 = computeKnownBits(LHS, DemandedLHS, Depth + 1);
@@ -3518,7 +3518,7 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
35183518
if (Op.getValueType().isScalableVector())
35193519
break;
35203520
// Split DemandedElts and test each of the demanded subvectors.
3521-
Known.Zero.setAllBits(); Known.One.setAllBits();
3521+
Known.setAllConflict();
35223522
EVT SubVectorVT = Op.getOperand(0).getValueType();
35233523
unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
35243524
unsigned NumSubVectors = Op.getNumOperands();
@@ -3549,8 +3549,7 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
35493549
APInt DemandedSrcElts = DemandedElts;
35503550
DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
35513551

3552-
Known.One.setAllBits();
3553-
Known.Zero.setAllBits();
3552+
Known.setAllConflict();
35543553
if (!!DemandedSubElts) {
35553554
Known = computeKnownBits(Sub, DemandedSubElts, Depth + 1);
35563555
if (Known.isUnknown())
@@ -3643,7 +3642,7 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
36433642
APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
36443643
Known2 = computeKnownBits(N0, SubDemandedElts, Depth + 1);
36453644

3646-
Known.Zero.setAllBits(); Known.One.setAllBits();
3645+
Known.setAllConflict();
36473646
for (unsigned i = 0; i != NumElts; ++i)
36483647
if (DemandedElts[i]) {
36493648
unsigned Shifts = IsLE ? i : NumElts - 1 - i;
@@ -3991,8 +3990,7 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
39913990
// TODO - do we need to handle different bitwidths?
39923991
if (CstTy->isVectorTy() && BitWidth == CstTy->getScalarSizeInBits()) {
39933992
// Iterate across all vector elements finding common known bits.
3994-
Known.One.setAllBits();
3995-
Known.Zero.setAllBits();
3993+
Known.setAllConflict();
39963994
for (unsigned i = 0; i != NumElts; ++i) {
39973995
if (!DemandedElts[i])
39983996
continue;
@@ -4277,8 +4275,7 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
42774275
DemandedVal = !!DemandedElts[EltIdx];
42784276
DemandedVecElts.clearBit(EltIdx);
42794277
}
4280-
Known.One.setAllBits();
4281-
Known.Zero.setAllBits();
4278+
Known.setAllConflict();
42824279
if (DemandedVal) {
42834280
Known2 = computeKnownBits(InVal, Depth + 1);
42844281
Known = Known.intersectWith(Known2.zextOrTrunc(BitWidth));

llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,8 +1331,7 @@ bool TargetLowering::SimplifyDemandedBits(
13311331
Depth + 1))
13321332
return true;
13331333

1334-
Known.Zero.setAllBits();
1335-
Known.One.setAllBits();
1334+
Known.setAllConflict();
13361335
if (!!DemandedSubElts)
13371336
Known = Known.intersectWith(KnownSub);
13381337
if (!!DemandedSrcElts)
@@ -1385,8 +1384,7 @@ bool TargetLowering::SimplifyDemandedBits(
13851384
case ISD::CONCAT_VECTORS: {
13861385
if (VT.isScalableVector())
13871386
return false;
1388-
Known.Zero.setAllBits();
1389-
Known.One.setAllBits();
1387+
Known.setAllConflict();
13901388
EVT SubVT = Op.getOperand(0).getValueType();
13911389
unsigned NumSubVecs = Op.getNumOperands();
13921390
unsigned NumSubElts = SubVT.getVectorNumElements();
@@ -1416,8 +1414,7 @@ bool TargetLowering::SimplifyDemandedBits(
14161414
SDValue Op0 = Op.getOperand(0);
14171415
SDValue Op1 = Op.getOperand(1);
14181416

1419-
Known.Zero.setAllBits();
1420-
Known.One.setAllBits();
1417+
Known.setAllConflict();
14211418
if (!!DemandedLHS) {
14221419
if (SimplifyDemandedBits(Op0, DemandedBits, DemandedLHS, Known2, TLO,
14231420
Depth + 1))

llvm/lib/Support/KnownBits.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,7 @@ KnownBits KnownBits::shl(const KnownBits &LHS, const KnownBits &RHS, bool NUW,
348348
// Find the common bits from all possible shifts.
349349
unsigned ShiftAmtZeroMask = RHS.Zero.zextOrTrunc(32).getZExtValue();
350350
unsigned ShiftAmtOneMask = RHS.One.zextOrTrunc(32).getZExtValue();
351-
Known.Zero.setAllBits();
352-
Known.One.setAllBits();
351+
Known.setAllConflict();
353352
for (unsigned ShiftAmt = MinShiftAmount; ShiftAmt <= MaxShiftAmount;
354353
++ShiftAmt) {
355354
// Skip if the shift amount is impossible.
@@ -405,8 +404,7 @@ KnownBits KnownBits::lshr(const KnownBits &LHS, const KnownBits &RHS,
405404

406405
unsigned ShiftAmtZeroMask = RHS.Zero.zextOrTrunc(32).getZExtValue();
407406
unsigned ShiftAmtOneMask = RHS.One.zextOrTrunc(32).getZExtValue();
408-
Known.Zero.setAllBits();
409-
Known.One.setAllBits();
407+
Known.setAllConflict();
410408
for (unsigned ShiftAmt = MinShiftAmount; ShiftAmt <= MaxShiftAmount;
411409
++ShiftAmt) {
412410
// Skip if the shift amount is impossible.
@@ -465,8 +463,7 @@ KnownBits KnownBits::ashr(const KnownBits &LHS, const KnownBits &RHS,
465463

466464
unsigned ShiftAmtZeroMask = RHS.Zero.zextOrTrunc(32).getZExtValue();
467465
unsigned ShiftAmtOneMask = RHS.One.zextOrTrunc(32).getZExtValue();
468-
Known.Zero.setAllBits();
469-
Known.One.setAllBits();
466+
Known.setAllConflict();
470467
for (unsigned ShiftAmt = MinShiftAmount; ShiftAmt <= MaxShiftAmount;
471468
++ShiftAmt) {
472469
// Skip if the shift amount is impossible.

0 commit comments

Comments
 (0)