Skip to content

Commit 9a20c79

Browse files
committed
[NFC][KnownBits] Add getMinValue() / getMaxValue() methods
As it can be seen from accompanying cleanup, it is not unheard of to write `~Known.Zero` meaning "what maximal value can this KnownBits produce". But i think `~Known.Zero` isn't *that* self-explanatory, as compared to a method with a name. Note that not all `~Known.Zero` places were cleaned up, only those where this arguably improves things.
1 parent 372ad32 commit 9a20c79

File tree

8 files changed

+40
-13
lines changed

8 files changed

+40
-13
lines changed

llvm/include/llvm/Support/KnownBits.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,18 @@ struct KnownBits {
107107
Zero.setSignBit();
108108
}
109109

110+
/// Return the minimal value possible given these KnownBits.
111+
APInt getMinValue() const {
112+
// Assume that all bits that aren't known-ones are zeros.
113+
return One;
114+
}
115+
116+
/// Return the maximal value possible given these KnownBits.
117+
APInt getMaxValue() const {
118+
// Assume that all bits that aren't known-zeros are ones.
119+
return ~Zero;
120+
}
121+
110122
/// Truncate the underlying known Zero and One bits. This is equivalent
111123
/// to truncating the value we're tracking.
112124
KnownBits trunc(unsigned BitWidth) const {

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5717,10 +5717,11 @@ ScalarEvolution::getRangeRef(const SCEV *S,
57175717
if (SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED) {
57185718
// For a SCEVUnknown, ask ValueTracking.
57195719
KnownBits Known = computeKnownBits(U->getValue(), DL, 0, &AC, nullptr, &DT);
5720-
if (Known.One != ~Known.Zero + 1)
5721-
ConservativeResult =
5722-
ConservativeResult.intersectWith(
5723-
ConstantRange(Known.One, ~Known.Zero + 1), RangeType);
5720+
// If Known does not result in full-set, intersect with it.
5721+
if (Known.getMinValue() != Known.getMaxValue() + 1)
5722+
ConservativeResult = ConservativeResult.intersectWith(
5723+
ConstantRange(Known.getMinValue(), Known.getMaxValue() + 1),
5724+
RangeType);
57245725
} else {
57255726
assert(SignHint == ScalarEvolution::HINT_RANGE_SIGNED &&
57265727
"generalize as needed!");

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,7 @@ static void computeKnownBitsFromShiftOperator(
915915
// If the shift amount could be greater than or equal to the bit-width of the
916916
// LHS, the value could be poison, but bail out because the check below is
917917
// expensive. TODO: Should we just carry on?
918-
if ((~Known.Zero).uge(BitWidth)) {
918+
if (Known.getMaxValue().uge(BitWidth)) {
919919
Known.resetAll();
920920
return;
921921
}

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3350,20 +3350,20 @@ SelectionDAG::OverflowKind SelectionDAG::computeOverflowKind(SDValue N0,
33503350
KnownBits N0Known = computeKnownBits(N0);
33513351

33523352
bool overflow;
3353-
(void)(~N0Known.Zero).uadd_ov(~N1Known.Zero, overflow);
3353+
(void)N0Known.getMaxValue().uadd_ov(N1Known.getMaxValue(), overflow);
33543354
if (!overflow)
33553355
return OFK_Never;
33563356
}
33573357

33583358
// mulhi + 1 never overflow
33593359
if (N0.getOpcode() == ISD::UMUL_LOHI && N0.getResNo() == 1 &&
3360-
(~N1Known.Zero & 0x01) == ~N1Known.Zero)
3360+
(N1Known.getMaxValue() & 0x01) == N1Known.getMaxValue())
33613361
return OFK_Never;
33623362

33633363
if (N1.getOpcode() == ISD::UMUL_LOHI && N1.getResNo() == 1) {
33643364
KnownBits N0Known = computeKnownBits(N0);
33653365

3366-
if ((~N0Known.Zero & 0x01) == ~N0Known.Zero)
3366+
if ((N0Known.getMaxValue() & 0x01) == N0Known.getMaxValue())
33673367
return OFK_Never;
33683368
}
33693369

llvm/lib/IR/ConstantRange.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ ConstantRange ConstantRange::fromKnownBits(const KnownBits &Known,
6464
// For unsigned ranges, or signed ranges with known sign bit, create a simple
6565
// range between the smallest and largest possible value.
6666
if (!IsSigned || Known.isNegative() || Known.isNonNegative())
67-
return ConstantRange(Known.One, ~Known.Zero + 1);
67+
return ConstantRange(Known.getMinValue(), Known.getMaxValue() + 1);
6868

6969
// If we don't know the sign bit, pick the lower bound as a negative number
7070
// and the upper bound as a non-negative one.
71-
APInt Lower = Known.One, Upper = ~Known.Zero;
71+
APInt Lower = Known.getMinValue(), Upper = Known.getMaxValue();
7272
Lower.setSignBit();
7373
Upper.clearSignBit();
7474
return ConstantRange(Lower, Upper + 1);

llvm/lib/Support/KnownBits.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ static KnownBits computeForAddCarry(
2121
assert(!(CarryZero && CarryOne) &&
2222
"Carry can't be zero and one at the same time");
2323

24-
APInt PossibleSumZero = ~LHS.Zero + ~RHS.Zero + !CarryZero;
25-
APInt PossibleSumOne = LHS.One + RHS.One + CarryOne;
24+
APInt PossibleSumZero = LHS.getMaxValue() + RHS.getMaxValue() + !CarryZero;
25+
APInt PossibleSumOne = LHS.getMinValue() + RHS.getMinValue() + CarryOne;
2626

2727
// Compute known bits of the carry.
2828
APInt CarryKnownZero = ~(PossibleSumZero ^ LHS.Zero ^ RHS.Zero);

llvm/lib/Target/SystemZ/SystemZISelLowering.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3609,7 +3609,7 @@ SDValue SystemZTargetLowering::lowerCTPOP(SDValue Op,
36093609

36103610
// Get the known-zero mask for the operand.
36113611
KnownBits Known = DAG.computeKnownBits(Op);
3612-
unsigned NumSignificantBits = (~Known.Zero).getActiveBits();
3612+
unsigned NumSignificantBits = Known.getMaxValue().getActiveBits();
36133613
if (NumSignificantBits == 0)
36143614
return DAG.getConstant(0, DL, VT);
36153615

llvm/unittests/Support/KnownBitsTest.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,18 @@ TEST(KnownBitsTest, AddSubExhaustive) {
127127
TestAddSubExhaustive(false);
128128
}
129129

130+
TEST(KnownBitsTest, GetMinMaxVal) {
131+
unsigned Bits = 4;
132+
ForeachKnownBits(Bits, [&](const KnownBits &Known) {
133+
APInt Min = APInt::getMaxValue(Bits);
134+
APInt Max = APInt::getMinValue(Bits);
135+
ForeachNumInKnownBits(Known, [&](const APInt &N) {
136+
Min = APIntOps::umin(Min, N);
137+
Max = APIntOps::umax(Max, N);
138+
});
139+
EXPECT_EQ(Min, Known.getMinValue());
140+
EXPECT_EQ(Max, Known.getMaxValue());
141+
});
142+
}
143+
130144
} // end anonymous namespace

0 commit comments

Comments
 (0)