return One;
}
+ /// Return the minimal signed value possible given these KnownBits.
+ APInt getSignedMinValue() const {
+ // Assume that all bits that aren't known-ones are zeros.
+ APInt Min = One;
+ // Sign bit is unknown.
+ if (Zero.isSignBitClear() && One.isSignBitClear())
+ Min.setSignBit();
+ return Min;
+ }
+
/// Return the maximal unsigned value possible given these KnownBits.
APInt getMaxValue() const {
// Assume that all bits that aren't known-zeros are ones.
return ~Zero;
}
+ /// Return the maximal signed value possible given these KnownBits.
+ APInt getSignedMaxValue() const {
+ // Assume that all bits that aren't known-zeros are ones.
+ APInt Max = ~Zero;
+ // Sign bit is unknown.
+ if (Zero.isSignBitClear() && One.isSignBitClear())
+ Max.clearSignBit();
+ return Max;
+ }
+
/// Return known bits for a truncation of the value we're tracking.
KnownBits trunc(unsigned BitWidth) const {
return KnownBits(Zero.trunc(BitWidth), One.trunc(BitWidth));
});
}
+TEST(KnownBitsTest, GetSignedMinMaxVal) {
+ unsigned Bits = 4;
+ ForeachKnownBits(Bits, [&](const KnownBits &Known) {
+ APInt Min = APInt::getSignedMaxValue(Bits);
+ APInt Max = APInt::getSignedMinValue(Bits);
+ ForeachNumInKnownBits(Known, [&](const APInt &N) {
+ Min = APIntOps::smin(Min, N);
+ Max = APIntOps::smax(Max, N);
+ });
+ EXPECT_EQ(Min, Known.getSignedMinValue());
+ EXPECT_EQ(Max, Known.getSignedMaxValue());
+ });
+}
+
TEST(KnownBitsTest, SExtOrTrunc) {
const unsigned NarrowerSize = 4;
const unsigned BaseSize = 6;