Skip to content

Commit

Permalink
[KnownBits] Remove hasConflict() assertions (#94568)
Browse files Browse the repository at this point in the history
Allow KnownBits to represent "always poison" values via conflict.

close: #94436
  • Loading branch information
c8ef authored Jun 7, 2024
1 parent fc95645 commit b25b1db
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 124 deletions.
11 changes: 2 additions & 9 deletions llvm/include/llvm/Support/KnownBits.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ struct KnownBits {

/// Returns true if we know the value of all bits.
bool isConstant() const {
assert(!hasConflict() && "KnownBits conflict!");
return Zero.popcount() + One.popcount() == getBitWidth();
}

Expand All @@ -74,16 +73,10 @@ struct KnownBits {
}

/// Returns true if value is all zero.
bool isZero() const {
assert(!hasConflict() && "KnownBits conflict!");
return Zero.isAllOnes();
}
bool isZero() const { return Zero.isAllOnes(); }

/// Returns true if value is all one bits.
bool isAllOnes() const {
assert(!hasConflict() && "KnownBits conflict!");
return One.isAllOnes();
}
bool isAllOnes() const { return One.isAllOnes(); }

/// Make all bits known to be zero and discard any previous information.
void setAllZero() {
Expand Down
3 changes: 0 additions & 3 deletions llvm/lib/Analysis/ValueTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1158,7 +1158,6 @@ static void computeKnownBitsFromOperator(const Operator *I,
Known.makeNonNegative();
}

assert(!Known.hasConflict() && "Bits known to be one AND zero?");
break;
}

Expand Down Expand Up @@ -2055,8 +2054,6 @@ void computeKnownBits(const Value *V, const APInt &DemandedElts,

// Check whether we can determine known bits from context such as assumes.
computeKnownBitsFromContext(V, Known, Depth, Q);

assert((Known.Zero & Known.One) == 0 && "Bits known to be one AND zero?");
}

/// Try to detect a recurrence that the value of the induction variable is
Expand Down
1 change: 0 additions & 1 deletion llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,6 @@ void GISelKnownBits::computeKnownBitsImpl(Register R, KnownBits &Known,
}
}

assert(!Known.hasConflict() && "Bits known to be one AND zero?");
LLVM_DEBUG(dumpResult(MI, Known, Depth));

// Update the cache.
Expand Down
1 change: 0 additions & 1 deletion llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4212,7 +4212,6 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
break;
}

assert(!Known.hasConflict() && "Bits known to be one AND zero?");
return Known;
}

Expand Down
22 changes: 1 addition & 21 deletions llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1436,11 +1436,9 @@ bool TargetLowering::SimplifyDemandedBits(
if (SimplifyDemandedBits(Op1, DemandedBits, DemandedElts, Known, TLO,
Depth + 1))
return true;
assert(!Known.hasConflict() && "Bits known to be one AND zero?");
if (SimplifyDemandedBits(Op0, ~Known.Zero & DemandedBits, DemandedElts,
Known2, TLO, Depth + 1))
return true;
assert(!Known2.hasConflict() && "Bits known to be one AND zero?");

// If all of the demanded bits are known one on one side, return the other.
// These bits cannot contribute to the result of the 'and'.
Expand Down Expand Up @@ -1488,7 +1486,7 @@ bool TargetLowering::SimplifyDemandedBits(
}
return true;
}
assert(!Known.hasConflict() && "Bits known to be one AND zero?");

if (SimplifyDemandedBits(Op0, ~Known.One & DemandedBits, DemandedElts,
Known2, TLO, Depth + 1)) {
if (Flags.hasDisjoint()) {
Expand All @@ -1497,7 +1495,6 @@ bool TargetLowering::SimplifyDemandedBits(
}
return true;
}
assert(!Known2.hasConflict() && "Bits known to be one AND zero?");

// If all of the demanded bits are known zero on one side, return the other.
// These bits cannot contribute to the result of the 'or'.
Expand Down Expand Up @@ -1563,11 +1560,9 @@ bool TargetLowering::SimplifyDemandedBits(
if (SimplifyDemandedBits(Op1, DemandedBits, DemandedElts, Known, TLO,
Depth + 1))
return true;
assert(!Known.hasConflict() && "Bits known to be one AND zero?");
if (SimplifyDemandedBits(Op0, DemandedBits, DemandedElts, Known2, TLO,
Depth + 1))
return true;
assert(!Known2.hasConflict() && "Bits known to be one AND zero?");

// If all of the demanded bits are known zero on one side, return the other.
// These bits cannot contribute to the result of the 'xor'.
Expand Down Expand Up @@ -1663,8 +1658,6 @@ bool TargetLowering::SimplifyDemandedBits(
if (SimplifyDemandedBits(Op.getOperand(1), DemandedBits, DemandedElts,
Known2, TLO, Depth + 1))
return true;
assert(!Known.hasConflict() && "Bits known to be one AND zero?");
assert(!Known2.hasConflict() && "Bits known to be one AND zero?");

// If the operands are constants, see if we can simplify them.
if (ShrinkDemandedConstant(Op, DemandedBits, DemandedElts, TLO))
Expand All @@ -1680,8 +1673,6 @@ bool TargetLowering::SimplifyDemandedBits(
if (SimplifyDemandedBits(Op.getOperand(1), DemandedBits, DemandedElts,
Known2, TLO, Depth + 1))
return true;
assert(!Known.hasConflict() && "Bits known to be one AND zero?");
assert(!Known2.hasConflict() && "Bits known to be one AND zero?");

// Only known if known in both the LHS and RHS.
Known = Known.intersectWith(Known2);
Expand All @@ -1693,8 +1684,6 @@ bool TargetLowering::SimplifyDemandedBits(
if (SimplifyDemandedBits(Op.getOperand(2), DemandedBits, DemandedElts,
Known2, TLO, Depth + 1))
return true;
assert(!Known.hasConflict() && "Bits known to be one AND zero?");
assert(!Known2.hasConflict() && "Bits known to be one AND zero?");

// If the operands are constants, see if we can simplify them.
if (ShrinkDemandedConstant(Op, DemandedBits, DemandedElts, TLO))
Expand Down Expand Up @@ -1819,7 +1808,6 @@ bool TargetLowering::SimplifyDemandedBits(
}
return true;
}
assert(!Known.hasConflict() && "Bits known to be one AND zero?");
Known.Zero <<= ShAmt;
Known.One <<= ShAmt;
// low bits known zero.
Expand Down Expand Up @@ -1993,7 +1981,6 @@ bool TargetLowering::SimplifyDemandedBits(
if (SimplifyDemandedBits(Op0, InDemandedMask, DemandedElts, Known, TLO,
Depth + 1))
return true;
assert(!Known.hasConflict() && "Bits known to be one AND zero?");
Known.Zero.lshrInPlace(ShAmt);
Known.One.lshrInPlace(ShAmt);
// High bits known zero.
Expand Down Expand Up @@ -2090,7 +2077,6 @@ bool TargetLowering::SimplifyDemandedBits(
if (SimplifyDemandedBits(Op0, InDemandedMask, DemandedElts, Known, TLO,
Depth + 1))
return true;
assert(!Known.hasConflict() && "Bits known to be one AND zero?");
Known.Zero.lshrInPlace(ShAmt);
Known.One.lshrInPlace(ShAmt);

Expand Down Expand Up @@ -2385,7 +2371,6 @@ bool TargetLowering::SimplifyDemandedBits(
if (SimplifyDemandedBits(Op0, InputDemandedBits, DemandedElts, Known, TLO,
Depth + 1))
return true;
assert(!Known.hasConflict() && "Bits known to be one AND zero?");

// If the sign bit of the input is known set or clear, then we know the
// top bits of the result.
Expand Down Expand Up @@ -2458,7 +2443,6 @@ bool TargetLowering::SimplifyDemandedBits(
}
return true;
}
assert(!Known.hasConflict() && "Bits known to be one AND zero?");
assert(Known.getBitWidth() == InBits && "Src width has changed?");
Known = Known.zext(BitWidth);

Expand Down Expand Up @@ -2508,7 +2492,6 @@ bool TargetLowering::SimplifyDemandedBits(
if (SimplifyDemandedBits(Src, InDemandedBits, InDemandedElts, Known, TLO,
Depth + 1))
return true;
assert(!Known.hasConflict() && "Bits known to be one AND zero?");
assert(Known.getBitWidth() == InBits && "Src width has changed?");

// If the sign bit is known one, the top bits match.
Expand Down Expand Up @@ -2554,7 +2537,6 @@ bool TargetLowering::SimplifyDemandedBits(
if (SimplifyDemandedBits(Src, InDemandedBits, InDemandedElts, Known, TLO,
Depth + 1))
return true;
assert(!Known.hasConflict() && "Bits known to be one AND zero?");
assert(Known.getBitWidth() == InBits && "Src width has changed?");
Known = Known.anyext(BitWidth);

Expand Down Expand Up @@ -2620,7 +2602,6 @@ bool TargetLowering::SimplifyDemandedBits(
break;
}

assert(!Known.hasConflict() && "Bits known to be one AND zero?");
break;
}
case ISD::AssertZext: {
Expand All @@ -2631,7 +2612,6 @@ bool TargetLowering::SimplifyDemandedBits(
if (SimplifyDemandedBits(Op.getOperand(0), ~InMask | DemandedBits, Known,
TLO, Depth + 1))
return true;
assert(!Known.hasConflict() && "Bits known to be one AND zero?");

Known.Zero |= ~InMask;
Known.One &= (~Known.Zero);
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/IR/ConstantRange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ ConstantRange::ConstantRange(APInt L, APInt U)

ConstantRange ConstantRange::fromKnownBits(const KnownBits &Known,
bool IsSigned) {
assert(!Known.hasConflict() && "Expected valid KnownBits");

if (Known.hasConflict())
return getEmpty(Known.getBitWidth());
if (Known.isUnknown())
return getFull(Known.getBitWidth());

Expand Down
33 changes: 5 additions & 28 deletions llvm/lib/Support/KnownBits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@

using namespace llvm;

static KnownBits computeForAddCarry(
const KnownBits &LHS, const KnownBits &RHS,
bool CarryZero, bool CarryOne) {
assert(!(CarryZero && CarryOne) &&
"Carry can't be zero and one at the same time");
static KnownBits computeForAddCarry(const KnownBits &LHS, const KnownBits &RHS,
bool CarryZero, bool CarryOne) {

APInt PossibleSumZero = LHS.getMaxValue() + RHS.getMaxValue() + !CarryZero;
APInt PossibleSumOne = LHS.getMinValue() + RHS.getMinValue() + CarryOne;
Expand All @@ -37,9 +34,6 @@ static KnownBits computeForAddCarry(
APInt CarryKnownUnion = std::move(CarryKnownZero) | CarryKnownOne;
APInt Known = std::move(LHSKnownUnion) & RHSKnownUnion & CarryKnownUnion;

assert((PossibleSumZero & Known) == (PossibleSumOne & Known) &&
"known bits of sum differ");

// Compute known bits of the result.
KnownBits KnownOut;
KnownOut.Zero = ~std::move(PossibleSumZero) & Known;
Expand Down Expand Up @@ -608,14 +602,12 @@ KnownBits KnownBits::abs(bool IntMinIsPoison) const {
}
}

assert(!KnownAbs.hasConflict() && "Bad Output");
return KnownAbs;
}

static KnownBits computeForSatAddSub(bool Add, bool Signed,
const KnownBits &LHS,
const KnownBits &RHS) {
assert(!LHS.hasConflict() && !RHS.hasConflict() && "Bad inputs");
// We don't see NSW even for sadd/ssub as we want to check if the result has
// signed overflow.
KnownBits Res =
Expand Down Expand Up @@ -715,7 +707,6 @@ static KnownBits computeForSatAddSub(bool Add, bool Signed,
// We know whether or not we overflowed.
if (!(*Overflow)) {
// No overflow.
assert(!Res.hasConflict() && "Bad Output");
return Res;
}

Expand All @@ -737,7 +728,6 @@ static KnownBits computeForSatAddSub(bool Add, bool Signed,

Res.One = C;
Res.Zero = ~C;
assert(!Res.hasConflict() && "Bad Output");
return Res;
}

Expand All @@ -757,7 +747,6 @@ static KnownBits computeForSatAddSub(bool Add, bool Signed,
Res.One.clearAllBits();
}

assert(!Res.hasConflict() && "Bad Output");
return Res;
}

Expand Down Expand Up @@ -808,8 +797,7 @@ KnownBits KnownBits::avgCeilU(const KnownBits &LHS, const KnownBits &RHS) {
KnownBits KnownBits::mul(const KnownBits &LHS, const KnownBits &RHS,
bool NoUndefSelfMultiply) {
unsigned BitWidth = LHS.getBitWidth();
assert(BitWidth == RHS.getBitWidth() && !LHS.hasConflict() &&
!RHS.hasConflict() && "Operand mismatch");
assert(BitWidth == RHS.getBitWidth() && "Operand mismatch");
assert((!NoUndefSelfMultiply || LHS == RHS) &&
"Self multiplication knownbits mismatch");

Expand Down Expand Up @@ -905,17 +893,15 @@ KnownBits KnownBits::mul(const KnownBits &LHS, const KnownBits &RHS,

KnownBits KnownBits::mulhs(const KnownBits &LHS, const KnownBits &RHS) {
unsigned BitWidth = LHS.getBitWidth();
assert(BitWidth == RHS.getBitWidth() && !LHS.hasConflict() &&
!RHS.hasConflict() && "Operand mismatch");
assert(BitWidth == RHS.getBitWidth() && "Operand mismatch");
KnownBits WideLHS = LHS.sext(2 * BitWidth);
KnownBits WideRHS = RHS.sext(2 * BitWidth);
return mul(WideLHS, WideRHS).extractBits(BitWidth, BitWidth);
}

KnownBits KnownBits::mulhu(const KnownBits &LHS, const KnownBits &RHS) {
unsigned BitWidth = LHS.getBitWidth();
assert(BitWidth == RHS.getBitWidth() && !LHS.hasConflict() &&
!RHS.hasConflict() && "Operand mismatch");
assert(BitWidth == RHS.getBitWidth() && "Operand mismatch");
KnownBits WideLHS = LHS.zext(2 * BitWidth);
KnownBits WideRHS = RHS.zext(2 * BitWidth);
return mul(WideLHS, WideRHS).extractBits(BitWidth, BitWidth);
Expand Down Expand Up @@ -964,7 +950,6 @@ KnownBits KnownBits::sdiv(const KnownBits &LHS, const KnownBits &RHS,
return udiv(LHS, RHS, Exact);

unsigned BitWidth = LHS.getBitWidth();
assert(!LHS.hasConflict() && !RHS.hasConflict() && "Bad inputs");
KnownBits Known(BitWidth);

if (LHS.isZero() || RHS.isZero()) {
Expand Down Expand Up @@ -1011,15 +996,12 @@ KnownBits KnownBits::sdiv(const KnownBits &LHS, const KnownBits &RHS,
}

Known = divComputeLowBit(Known, LHS, RHS, Exact);

assert(!Known.hasConflict() && "Bad Output");
return Known;
}

KnownBits KnownBits::udiv(const KnownBits &LHS, const KnownBits &RHS,
bool Exact) {
unsigned BitWidth = LHS.getBitWidth();
assert(!LHS.hasConflict() && !RHS.hasConflict());
KnownBits Known(BitWidth);

if (LHS.isZero() || RHS.isZero()) {
Expand All @@ -1041,7 +1023,6 @@ KnownBits KnownBits::udiv(const KnownBits &LHS, const KnownBits &RHS,
Known.Zero.setHighBits(LeadZ);
Known = divComputeLowBit(Known, LHS, RHS, Exact);

assert(!Known.hasConflict() && "Bad Output");
return Known;
}

Expand All @@ -1059,8 +1040,6 @@ KnownBits KnownBits::remGetLowBits(const KnownBits &LHS, const KnownBits &RHS) {
}

KnownBits KnownBits::urem(const KnownBits &LHS, const KnownBits &RHS) {
assert(!LHS.hasConflict() && !RHS.hasConflict());

KnownBits Known = remGetLowBits(LHS, RHS);
if (RHS.isConstant() && RHS.getConstant().isPowerOf2()) {
// NB: Low bits set in `remGetLowBits`.
Expand All @@ -1078,8 +1057,6 @@ KnownBits KnownBits::urem(const KnownBits &LHS, const KnownBits &RHS) {
}

KnownBits KnownBits::srem(const KnownBits &LHS, const KnownBits &RHS) {
assert(!LHS.hasConflict() && !RHS.hasConflict());

KnownBits Known = remGetLowBits(LHS, RHS);
if (RHS.isConstant() && RHS.getConstant().isPowerOf2()) {
// NB: Low bits are set in `remGetLowBits`.
Expand Down
5 changes: 0 additions & 5 deletions llvm/lib/Target/X86/X86ISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42452,12 +42452,10 @@ bool X86TargetLowering::SimplifyDemandedBitsForTargetNode(
if (SimplifyDemandedBits(Op1, OriginalDemandedBits, OriginalDemandedElts,
Known, TLO, Depth + 1))
return true;
assert(!Known.hasConflict() && "Bits known to be one AND zero?");

if (SimplifyDemandedBits(Op0, ~Known.Zero & OriginalDemandedBits,
OriginalDemandedElts, Known2, TLO, Depth + 1))
return true;
assert(!Known2.hasConflict() && "Bits known to be one AND zero?");

// If the RHS is a constant, see if we can simplify it.
if (ShrinkDemandedConstant(Op, ~Known2.One & OriginalDemandedBits,
Expand Down Expand Up @@ -42508,7 +42506,6 @@ bool X86TargetLowering::SimplifyDemandedBitsForTargetNode(
TLO, Depth + 1))
return true;

assert(!Known.hasConflict() && "Bits known to be one AND zero?");
Known.Zero <<= ShAmt;
Known.One <<= ShAmt;

Expand All @@ -42527,7 +42524,6 @@ bool X86TargetLowering::SimplifyDemandedBitsForTargetNode(
OriginalDemandedElts, Known, TLO, Depth + 1))
return true;

assert(!Known.hasConflict() && "Bits known to be one AND zero?");
Known.Zero.lshrInPlace(ShAmt);
Known.One.lshrInPlace(ShAmt);

Expand Down Expand Up @@ -42568,7 +42564,6 @@ bool X86TargetLowering::SimplifyDemandedBitsForTargetNode(
TLO, Depth + 1))
return true;

assert(!Known.hasConflict() && "Bits known to be one AND zero?");
Known.Zero.lshrInPlace(ShAmt);
Known.One.lshrInPlace(ShAmt);

Expand Down
Loading

0 comments on commit b25b1db

Please sign in to comment.