Skip to content

Commit b25b1db

Browse files
authored
[KnownBits] Remove hasConflict() assertions (#94568)
Allow KnownBits to represent "always poison" values via conflict. close: #94436
1 parent fc95645 commit b25b1db

File tree

12 files changed

+61
-124
lines changed

12 files changed

+61
-124
lines changed

llvm/include/llvm/Support/KnownBits.h

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ struct KnownBits {
4848

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

@@ -74,16 +73,10 @@ struct KnownBits {
7473
}
7574

7675
/// Returns true if value is all zero.
77-
bool isZero() const {
78-
assert(!hasConflict() && "KnownBits conflict!");
79-
return Zero.isAllOnes();
80-
}
76+
bool isZero() const { return Zero.isAllOnes(); }
8177

8278
/// Returns true if value is all one bits.
83-
bool isAllOnes() const {
84-
assert(!hasConflict() && "KnownBits conflict!");
85-
return One.isAllOnes();
86-
}
79+
bool isAllOnes() const { return One.isAllOnes(); }
8780

8881
/// Make all bits known to be zero and discard any previous information.
8982
void setAllZero() {

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,7 +1158,6 @@ static void computeKnownBitsFromOperator(const Operator *I,
11581158
Known.makeNonNegative();
11591159
}
11601160

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

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

20562055
// Check whether we can determine known bits from context such as assumes.
20572056
computeKnownBitsFromContext(V, Known, Depth, Q);
2058-
2059-
assert((Known.Zero & Known.One) == 0 && "Bits known to be one AND zero?");
20602057
}
20612058

20622059
/// Try to detect a recurrence that the value of the induction variable is

llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,6 @@ void GISelKnownBits::computeKnownBitsImpl(Register R, KnownBits &Known,
607607
}
608608
}
609609

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

613612
// Update the cache.

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4212,7 +4212,6 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
42124212
break;
42134213
}
42144214

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

llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,11 +1436,9 @@ bool TargetLowering::SimplifyDemandedBits(
14361436
if (SimplifyDemandedBits(Op1, DemandedBits, DemandedElts, Known, TLO,
14371437
Depth + 1))
14381438
return true;
1439-
assert(!Known.hasConflict() && "Bits known to be one AND zero?");
14401439
if (SimplifyDemandedBits(Op0, ~Known.Zero & DemandedBits, DemandedElts,
14411440
Known2, TLO, Depth + 1))
14421441
return true;
1443-
assert(!Known2.hasConflict() && "Bits known to be one AND zero?");
14441442

14451443
// If all of the demanded bits are known one on one side, return the other.
14461444
// These bits cannot contribute to the result of the 'and'.
@@ -1488,7 +1486,7 @@ bool TargetLowering::SimplifyDemandedBits(
14881486
}
14891487
return true;
14901488
}
1491-
assert(!Known.hasConflict() && "Bits known to be one AND zero?");
1489+
14921490
if (SimplifyDemandedBits(Op0, ~Known.One & DemandedBits, DemandedElts,
14931491
Known2, TLO, Depth + 1)) {
14941492
if (Flags.hasDisjoint()) {
@@ -1497,7 +1495,6 @@ bool TargetLowering::SimplifyDemandedBits(
14971495
}
14981496
return true;
14991497
}
1500-
assert(!Known2.hasConflict() && "Bits known to be one AND zero?");
15011498

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

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

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

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

16991688
// If the operands are constants, see if we can simplify them.
17001689
if (ShrinkDemandedConstant(Op, DemandedBits, DemandedElts, TLO))
@@ -1819,7 +1808,6 @@ bool TargetLowering::SimplifyDemandedBits(
18191808
}
18201809
return true;
18211810
}
1822-
assert(!Known.hasConflict() && "Bits known to be one AND zero?");
18231811
Known.Zero <<= ShAmt;
18241812
Known.One <<= ShAmt;
18251813
// low bits known zero.
@@ -1993,7 +1981,6 @@ bool TargetLowering::SimplifyDemandedBits(
19931981
if (SimplifyDemandedBits(Op0, InDemandedMask, DemandedElts, Known, TLO,
19941982
Depth + 1))
19951983
return true;
1996-
assert(!Known.hasConflict() && "Bits known to be one AND zero?");
19971984
Known.Zero.lshrInPlace(ShAmt);
19981985
Known.One.lshrInPlace(ShAmt);
19991986
// High bits known zero.
@@ -2090,7 +2077,6 @@ bool TargetLowering::SimplifyDemandedBits(
20902077
if (SimplifyDemandedBits(Op0, InDemandedMask, DemandedElts, Known, TLO,
20912078
Depth + 1))
20922079
return true;
2093-
assert(!Known.hasConflict() && "Bits known to be one AND zero?");
20942080
Known.Zero.lshrInPlace(ShAmt);
20952081
Known.One.lshrInPlace(ShAmt);
20962082

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

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

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

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

@@ -2620,7 +2602,6 @@ bool TargetLowering::SimplifyDemandedBits(
26202602
break;
26212603
}
26222604

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

26362616
Known.Zero |= ~InMask;
26372617
Known.One &= (~Known.Zero);

llvm/lib/IR/ConstantRange.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ ConstantRange::ConstantRange(APInt L, APInt U)
5858

5959
ConstantRange ConstantRange::fromKnownBits(const KnownBits &Known,
6060
bool IsSigned) {
61-
assert(!Known.hasConflict() && "Expected valid KnownBits");
62-
61+
if (Known.hasConflict())
62+
return getEmpty(Known.getBitWidth());
6363
if (Known.isUnknown())
6464
return getFull(Known.getBitWidth());
6565

llvm/lib/Support/KnownBits.cpp

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,8 @@
1818

1919
using namespace llvm;
2020

21-
static KnownBits computeForAddCarry(
22-
const KnownBits &LHS, const KnownBits &RHS,
23-
bool CarryZero, bool CarryOne) {
24-
assert(!(CarryZero && CarryOne) &&
25-
"Carry can't be zero and one at the same time");
21+
static KnownBits computeForAddCarry(const KnownBits &LHS, const KnownBits &RHS,
22+
bool CarryZero, bool CarryOne) {
2623

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

40-
assert((PossibleSumZero & Known) == (PossibleSumOne & Known) &&
41-
"known bits of sum differ");
42-
4337
// Compute known bits of the result.
4438
KnownBits KnownOut;
4539
KnownOut.Zero = ~std::move(PossibleSumZero) & Known;
@@ -608,14 +602,12 @@ KnownBits KnownBits::abs(bool IntMinIsPoison) const {
608602
}
609603
}
610604

611-
assert(!KnownAbs.hasConflict() && "Bad Output");
612605
return KnownAbs;
613606
}
614607

615608
static KnownBits computeForSatAddSub(bool Add, bool Signed,
616609
const KnownBits &LHS,
617610
const KnownBits &RHS) {
618-
assert(!LHS.hasConflict() && !RHS.hasConflict() && "Bad inputs");
619611
// We don't see NSW even for sadd/ssub as we want to check if the result has
620612
// signed overflow.
621613
KnownBits Res =
@@ -715,7 +707,6 @@ static KnownBits computeForSatAddSub(bool Add, bool Signed,
715707
// We know whether or not we overflowed.
716708
if (!(*Overflow)) {
717709
// No overflow.
718-
assert(!Res.hasConflict() && "Bad Output");
719710
return Res;
720711
}
721712

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

738729
Res.One = C;
739730
Res.Zero = ~C;
740-
assert(!Res.hasConflict() && "Bad Output");
741731
return Res;
742732
}
743733

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

760-
assert(!Res.hasConflict() && "Bad Output");
761750
return Res;
762751
}
763752

@@ -808,8 +797,7 @@ KnownBits KnownBits::avgCeilU(const KnownBits &LHS, const KnownBits &RHS) {
808797
KnownBits KnownBits::mul(const KnownBits &LHS, const KnownBits &RHS,
809798
bool NoUndefSelfMultiply) {
810799
unsigned BitWidth = LHS.getBitWidth();
811-
assert(BitWidth == RHS.getBitWidth() && !LHS.hasConflict() &&
812-
!RHS.hasConflict() && "Operand mismatch");
800+
assert(BitWidth == RHS.getBitWidth() && "Operand mismatch");
813801
assert((!NoUndefSelfMultiply || LHS == RHS) &&
814802
"Self multiplication knownbits mismatch");
815803

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

906894
KnownBits KnownBits::mulhs(const KnownBits &LHS, const KnownBits &RHS) {
907895
unsigned BitWidth = LHS.getBitWidth();
908-
assert(BitWidth == RHS.getBitWidth() && !LHS.hasConflict() &&
909-
!RHS.hasConflict() && "Operand mismatch");
896+
assert(BitWidth == RHS.getBitWidth() && "Operand mismatch");
910897
KnownBits WideLHS = LHS.sext(2 * BitWidth);
911898
KnownBits WideRHS = RHS.sext(2 * BitWidth);
912899
return mul(WideLHS, WideRHS).extractBits(BitWidth, BitWidth);
913900
}
914901

915902
KnownBits KnownBits::mulhu(const KnownBits &LHS, const KnownBits &RHS) {
916903
unsigned BitWidth = LHS.getBitWidth();
917-
assert(BitWidth == RHS.getBitWidth() && !LHS.hasConflict() &&
918-
!RHS.hasConflict() && "Operand mismatch");
904+
assert(BitWidth == RHS.getBitWidth() && "Operand mismatch");
919905
KnownBits WideLHS = LHS.zext(2 * BitWidth);
920906
KnownBits WideRHS = RHS.zext(2 * BitWidth);
921907
return mul(WideLHS, WideRHS).extractBits(BitWidth, BitWidth);
@@ -964,7 +950,6 @@ KnownBits KnownBits::sdiv(const KnownBits &LHS, const KnownBits &RHS,
964950
return udiv(LHS, RHS, Exact);
965951

966952
unsigned BitWidth = LHS.getBitWidth();
967-
assert(!LHS.hasConflict() && !RHS.hasConflict() && "Bad inputs");
968953
KnownBits Known(BitWidth);
969954

970955
if (LHS.isZero() || RHS.isZero()) {
@@ -1011,15 +996,12 @@ KnownBits KnownBits::sdiv(const KnownBits &LHS, const KnownBits &RHS,
1011996
}
1012997

1013998
Known = divComputeLowBit(Known, LHS, RHS, Exact);
1014-
1015-
assert(!Known.hasConflict() && "Bad Output");
1016999
return Known;
10171000
}
10181001

10191002
KnownBits KnownBits::udiv(const KnownBits &LHS, const KnownBits &RHS,
10201003
bool Exact) {
10211004
unsigned BitWidth = LHS.getBitWidth();
1022-
assert(!LHS.hasConflict() && !RHS.hasConflict());
10231005
KnownBits Known(BitWidth);
10241006

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

1044-
assert(!Known.hasConflict() && "Bad Output");
10451026
return Known;
10461027
}
10471028

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

10611042
KnownBits KnownBits::urem(const KnownBits &LHS, const KnownBits &RHS) {
1062-
assert(!LHS.hasConflict() && !RHS.hasConflict());
1063-
10641043
KnownBits Known = remGetLowBits(LHS, RHS);
10651044
if (RHS.isConstant() && RHS.getConstant().isPowerOf2()) {
10661045
// NB: Low bits set in `remGetLowBits`.
@@ -1078,8 +1057,6 @@ KnownBits KnownBits::urem(const KnownBits &LHS, const KnownBits &RHS) {
10781057
}
10791058

10801059
KnownBits KnownBits::srem(const KnownBits &LHS, const KnownBits &RHS) {
1081-
assert(!LHS.hasConflict() && !RHS.hasConflict());
1082-
10831060
KnownBits Known = remGetLowBits(LHS, RHS);
10841061
if (RHS.isConstant() && RHS.getConstant().isPowerOf2()) {
10851062
// NB: Low bits are set in `remGetLowBits`.

llvm/lib/Target/X86/X86ISelLowering.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42452,12 +42452,10 @@ bool X86TargetLowering::SimplifyDemandedBitsForTargetNode(
4245242452
if (SimplifyDemandedBits(Op1, OriginalDemandedBits, OriginalDemandedElts,
4245342453
Known, TLO, Depth + 1))
4245442454
return true;
42455-
assert(!Known.hasConflict() && "Bits known to be one AND zero?");
4245642455

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

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

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

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

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

@@ -42568,7 +42564,6 @@ bool X86TargetLowering::SimplifyDemandedBitsForTargetNode(
4256842564
TLO, Depth + 1))
4256942565
return true;
4257042566

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

0 commit comments

Comments
 (0)