[llvm] [KnownBits] Add setAllConflict to set all bits in Zero and One. NFC (PR #159815)
Craig Topper via llvm-commits
llvm-commits at lists.llvm.org
Fri Sep 19 10:20:37 PDT 2025
https://github.com/topperc created https://github.com/llvm/llvm-project/pull/159815
This is a common pattern to initialize Knownbits that occurs before loops that call intersectWith.
>From 6696e5228b3990e78fd7133c1a0424ccb8028a25 Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Fri, 19 Sep 2025 10:19:01 -0700
Subject: [PATCH] [KnownBits] Add setAllConflict to set all bits in Zero and
One. NFC
This is a common pattern to initialize Knownbits that occurs before
loops that call intersectWith.
---
llvm/include/llvm/Support/KnownBits.h | 7 ++++++
llvm/lib/Analysis/ValueTracking.cpp | 22 +++++++------------
.../lib/CodeGen/SelectionDAG/SelectionDAG.cpp | 17 ++++++--------
.../CodeGen/SelectionDAG/TargetLowering.cpp | 9 +++-----
llvm/lib/Support/KnownBits.cpp | 9 +++-----
5 files changed, 28 insertions(+), 36 deletions(-)
diff --git a/llvm/include/llvm/Support/KnownBits.h b/llvm/include/llvm/Support/KnownBits.h
index 5b693ee587453..bff944325880b 100644
--- a/llvm/include/llvm/Support/KnownBits.h
+++ b/llvm/include/llvm/Support/KnownBits.h
@@ -94,6 +94,13 @@ struct KnownBits {
One.setAllBits();
}
+ /// Make all bits known to be both zero and one. Useful before a loop that
+ /// calls intersectWith.
+ void setAllConflict() {
+ Zero.setAllBits();
+ One.setAllBits();
+ }
+
/// Returns true if this value is known to be negative.
bool isNegative() const { return One.isSignBitSet(); }
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index bb0db2d31971d..b6a80fef42174 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -441,8 +441,7 @@ void llvm::computeKnownBitsFromRangeMetadata(const MDNode &Ranges,
unsigned NumRanges = Ranges.getNumOperands() / 2;
assert(NumRanges >= 1);
- Known.Zero.setAllBits();
- Known.One.setAllBits();
+ Known.setAllConflict();
for (unsigned i = 0; i < NumRanges; ++i) {
ConstantInt *Lower =
@@ -1328,8 +1327,7 @@ static void computeKnownBitsFromOperator(const Operator *I,
break;
if (Result.isKnownNever(fcNormal | fcSubnormal | fcNan)) {
- Known.Zero.setAllBits();
- Known.One.setAllBits();
+ Known.setAllConflict();
if (FPClasses & fcInf)
Known = Known.intersectWith(KnownBits::makeConstant(
@@ -1405,8 +1403,7 @@ static void computeKnownBitsFromOperator(const Operator *I,
computeKnownBits(I->getOperand(0), SubDemandedElts, KnownSrc, Q,
Depth + 1);
- Known.Zero.setAllBits();
- Known.One.setAllBits();
+ Known.setAllConflict();
for (unsigned i = 0; i != NumElts; ++i) {
if (DemandedElts[i]) {
unsigned Shifts = IsLE ? i : NumElts - 1 - i;
@@ -1738,8 +1735,7 @@ static void computeKnownBitsFromOperator(const Operator *I,
if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
break;
- Known.Zero.setAllBits();
- Known.One.setAllBits();
+ Known.setAllConflict();
for (const Use &U : P->operands()) {
Value *IncValue;
const PHINode *CxtPhi;
@@ -2083,8 +2079,7 @@ static void computeKnownBitsFromOperator(const Operator *I,
Known.resetAll();
return;
}
- Known.One.setAllBits();
- Known.Zero.setAllBits();
+ Known.setAllConflict();
if (!!DemandedLHS) {
const Value *LHS = Shuf->getOperand(0);
computeKnownBits(LHS, DemandedLHS, Known, Q, Depth + 1);
@@ -2116,8 +2111,7 @@ static void computeKnownBitsFromOperator(const Operator *I,
NeedsElt = DemandedElts[CIdx->getZExtValue()];
}
- Known.One.setAllBits();
- Known.Zero.setAllBits();
+ Known.setAllConflict();
if (NeedsElt) {
computeKnownBits(Elt, Known, Q, Depth + 1);
// If we don't know any bits, early out.
@@ -2273,7 +2267,7 @@ void computeKnownBits(const Value *V, const APInt &DemandedElts,
assert(!isa<ScalableVectorType>(V->getType()));
// We know that CDV must be a vector of integers. Take the intersection of
// each element.
- Known.Zero.setAllBits(); Known.One.setAllBits();
+ Known.setAllConflict();
for (unsigned i = 0, e = CDV->getNumElements(); i != e; ++i) {
if (!DemandedElts[i])
continue;
@@ -2290,7 +2284,7 @@ void computeKnownBits(const Value *V, const APInt &DemandedElts,
assert(!isa<ScalableVectorType>(V->getType()));
// We know that CV must be a vector of integers. Take the intersection of
// each element.
- Known.Zero.setAllBits(); Known.One.setAllBits();
+ Known.setAllConflict();
for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
if (!DemandedElts[i])
continue;
diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
index 2dab927d2648c..aa7f885539518 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
@@ -3457,7 +3457,7 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
case ISD::BUILD_VECTOR:
assert(!Op.getValueType().isScalableVector());
// Collect the known bits that are shared by every demanded vector element.
- Known.Zero.setAllBits(); Known.One.setAllBits();
+ Known.setAllConflict();
for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
if (!DemandedElts[i])
continue;
@@ -3492,7 +3492,7 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
break;
// Known bits are the values that are shared by every demanded element.
- Known.Zero.setAllBits(); Known.One.setAllBits();
+ Known.setAllConflict();
if (!!DemandedLHS) {
SDValue LHS = Op.getOperand(0);
Known2 = computeKnownBits(LHS, DemandedLHS, Depth + 1);
@@ -3518,7 +3518,7 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
if (Op.getValueType().isScalableVector())
break;
// Split DemandedElts and test each of the demanded subvectors.
- Known.Zero.setAllBits(); Known.One.setAllBits();
+ Known.setAllConflict();
EVT SubVectorVT = Op.getOperand(0).getValueType();
unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
unsigned NumSubVectors = Op.getNumOperands();
@@ -3549,8 +3549,7 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
APInt DemandedSrcElts = DemandedElts;
DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
- Known.One.setAllBits();
- Known.Zero.setAllBits();
+ Known.setAllConflict();
if (!!DemandedSubElts) {
Known = computeKnownBits(Sub, DemandedSubElts, Depth + 1);
if (Known.isUnknown())
@@ -3643,7 +3642,7 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
Known2 = computeKnownBits(N0, SubDemandedElts, Depth + 1);
- Known.Zero.setAllBits(); Known.One.setAllBits();
+ Known.setAllConflict();
for (unsigned i = 0; i != NumElts; ++i)
if (DemandedElts[i]) {
unsigned Shifts = IsLE ? i : NumElts - 1 - i;
@@ -3991,8 +3990,7 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
// TODO - do we need to handle different bitwidths?
if (CstTy->isVectorTy() && BitWidth == CstTy->getScalarSizeInBits()) {
// Iterate across all vector elements finding common known bits.
- Known.One.setAllBits();
- Known.Zero.setAllBits();
+ Known.setAllConflict();
for (unsigned i = 0; i != NumElts; ++i) {
if (!DemandedElts[i])
continue;
@@ -4277,8 +4275,7 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
DemandedVal = !!DemandedElts[EltIdx];
DemandedVecElts.clearBit(EltIdx);
}
- Known.One.setAllBits();
- Known.Zero.setAllBits();
+ Known.setAllConflict();
if (DemandedVal) {
Known2 = computeKnownBits(InVal, Depth + 1);
Known = Known.intersectWith(Known2.zextOrTrunc(BitWidth));
diff --git a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
index e73f82f3786b8..80500e48351e4 100644
--- a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
@@ -1331,8 +1331,7 @@ bool TargetLowering::SimplifyDemandedBits(
Depth + 1))
return true;
- Known.Zero.setAllBits();
- Known.One.setAllBits();
+ Known.setAllConflict();
if (!!DemandedSubElts)
Known = Known.intersectWith(KnownSub);
if (!!DemandedSrcElts)
@@ -1385,8 +1384,7 @@ bool TargetLowering::SimplifyDemandedBits(
case ISD::CONCAT_VECTORS: {
if (VT.isScalableVector())
return false;
- Known.Zero.setAllBits();
- Known.One.setAllBits();
+ Known.setAllConflict();
EVT SubVT = Op.getOperand(0).getValueType();
unsigned NumSubVecs = Op.getNumOperands();
unsigned NumSubElts = SubVT.getVectorNumElements();
@@ -1416,8 +1414,7 @@ bool TargetLowering::SimplifyDemandedBits(
SDValue Op0 = Op.getOperand(0);
SDValue Op1 = Op.getOperand(1);
- Known.Zero.setAllBits();
- Known.One.setAllBits();
+ Known.setAllConflict();
if (!!DemandedLHS) {
if (SimplifyDemandedBits(Op0, DemandedBits, DemandedLHS, Known2, TLO,
Depth + 1))
diff --git a/llvm/lib/Support/KnownBits.cpp b/llvm/lib/Support/KnownBits.cpp
index 8d91f0e95d227..7db8e1641462e 100644
--- a/llvm/lib/Support/KnownBits.cpp
+++ b/llvm/lib/Support/KnownBits.cpp
@@ -348,8 +348,7 @@ KnownBits KnownBits::shl(const KnownBits &LHS, const KnownBits &RHS, bool NUW,
// Find the common bits from all possible shifts.
unsigned ShiftAmtZeroMask = RHS.Zero.zextOrTrunc(32).getZExtValue();
unsigned ShiftAmtOneMask = RHS.One.zextOrTrunc(32).getZExtValue();
- Known.Zero.setAllBits();
- Known.One.setAllBits();
+ Known.setAllConflict();
for (unsigned ShiftAmt = MinShiftAmount; ShiftAmt <= MaxShiftAmount;
++ShiftAmt) {
// Skip if the shift amount is impossible.
@@ -405,8 +404,7 @@ KnownBits KnownBits::lshr(const KnownBits &LHS, const KnownBits &RHS,
unsigned ShiftAmtZeroMask = RHS.Zero.zextOrTrunc(32).getZExtValue();
unsigned ShiftAmtOneMask = RHS.One.zextOrTrunc(32).getZExtValue();
- Known.Zero.setAllBits();
- Known.One.setAllBits();
+ Known.setAllConflict();
for (unsigned ShiftAmt = MinShiftAmount; ShiftAmt <= MaxShiftAmount;
++ShiftAmt) {
// Skip if the shift amount is impossible.
@@ -465,8 +463,7 @@ KnownBits KnownBits::ashr(const KnownBits &LHS, const KnownBits &RHS,
unsigned ShiftAmtZeroMask = RHS.Zero.zextOrTrunc(32).getZExtValue();
unsigned ShiftAmtOneMask = RHS.One.zextOrTrunc(32).getZExtValue();
- Known.Zero.setAllBits();
- Known.One.setAllBits();
+ Known.setAllConflict();
for (unsigned ShiftAmt = MinShiftAmount; ShiftAmt <= MaxShiftAmount;
++ShiftAmt) {
// Skip if the shift amount is impossible.
More information about the llvm-commits
mailing list