@@ -51,16 +51,25 @@ static void EnumerateConstantRanges(unsigned Bits, Fn TestFn) {
51
51
}
52
52
}
53
53
54
- template <typename Fn>
55
- static void EnumerateTwoConstantRanges (unsigned Bits, Fn TestFn) {
56
- EnumerateConstantRanges (Bits, [&](const ConstantRange &CR1) {
57
- EnumerateConstantRanges (Bits, [&](const ConstantRange &CR2) {
58
- TestFn (CR1, CR2);
54
+ template <typename Fn>
55
+ static void EnumerateInterestingConstantRanges (Fn TestFn) {
56
+ // Check 1 bit ranges, because they may have special cases.
57
+ EnumerateConstantRanges (/* Bits */ 1 , TestFn);
58
+ // Check 4 bit ranges to have decent coverage without being too slow.
59
+ EnumerateConstantRanges (/* Bits */ 4 , TestFn);
60
+ }
61
+
62
+ template <typename Fn>
63
+ static void EnumerateTwoInterestingConstantRanges (Fn TestFn) {
64
+ for (unsigned Bits : {1 , 4 }) {
65
+ EnumerateConstantRanges (Bits, [&](const ConstantRange &CR1) {
66
+ EnumerateConstantRanges (
67
+ Bits, [&](const ConstantRange &CR2) { TestFn (CR1, CR2); });
59
68
});
60
- });
69
+ }
61
70
}
62
71
63
- template <typename Fn>
72
+ template <typename Fn>
64
73
static void ForeachNumInConstantRange (const ConstantRange &CR, Fn TestFn) {
65
74
if (!CR.isEmptySet ()) {
66
75
APInt N = CR.getLower ();
@@ -179,16 +188,14 @@ using UnaryIntFn = llvm::function_ref<std::optional<APInt>(const APInt &)>;
179
188
180
189
static void TestUnaryOpExhaustive (UnaryRangeFn RangeFn, UnaryIntFn IntFn,
181
190
PreferFn PreferenceFn = PreferSmallest) {
182
- for (unsigned Bits : {1 , 4 }) {
183
- EnumerateConstantRanges (Bits, [&](const ConstantRange &CR) {
184
- SmallBitVector Elems (1 << Bits);
185
- ForeachNumInConstantRange (CR, [&](const APInt &N) {
186
- if (std::optional<APInt> ResultN = IntFn (N))
187
- Elems.set (ResultN->getZExtValue ());
188
- });
189
- TestRange (RangeFn (CR), Elems, PreferenceFn, {CR});
191
+ EnumerateInterestingConstantRanges ([&](const ConstantRange &CR) {
192
+ SmallBitVector Elems (1 << CR.getBitWidth ());
193
+ ForeachNumInConstantRange (CR, [&](const APInt &N) {
194
+ if (std::optional<APInt> ResultN = IntFn (N))
195
+ Elems.set (ResultN->getZExtValue ());
190
196
});
191
- }
197
+ TestRange (RangeFn (CR), Elems, PreferenceFn, {CR});
198
+ });
192
199
}
193
200
194
201
using BinaryRangeFn = llvm::function_ref<ConstantRange(const ConstantRange &,
@@ -228,10 +235,9 @@ static bool CheckNonWrappedOrSignWrappedOnly(const ConstantRange &CR1,
228
235
static void TestBinaryOpExhaustive (BinaryRangeFn RangeFn, BinaryIntFn IntFn,
229
236
PreferFn PreferenceFn = PreferSmallest,
230
237
BinaryCheckFn CheckFn = CheckAll) {
231
- unsigned Bits = 4 ;
232
- EnumerateTwoConstantRanges (
233
- Bits, [&](const ConstantRange &CR1, const ConstantRange &CR2) {
234
- SmallBitVector Elems (1 << Bits);
238
+ EnumerateTwoInterestingConstantRanges (
239
+ [&](const ConstantRange &CR1, const ConstantRange &CR2) {
240
+ SmallBitVector Elems (1 << CR1.getBitWidth ());
235
241
ForeachNumInConstantRange (CR1, [&](const APInt &N1) {
236
242
ForeachNumInConstantRange (CR2, [&](const APInt &N2) {
237
243
if (std::optional<APInt> ResultN = IntFn (N1, N2))
@@ -535,11 +541,11 @@ TEST_F(ConstantRangeTest, IntersectWith) {
535
541
EXPECT_EQ (LHS.intersectWith (RHS), ConstantRange (APInt (32 , 15 ), APInt (32 , 0 )));
536
542
}
537
543
538
- template <typename Fn1, typename Fn2, typename Fn3>
544
+ template <typename Fn1, typename Fn2, typename Fn3>
539
545
void testBinarySetOperationExhaustive (Fn1 OpFn, Fn2 ExactOpFn, Fn3 InResultFn) {
540
- unsigned Bits = 4 ;
541
- EnumerateTwoConstantRanges (Bits,
546
+ EnumerateTwoInterestingConstantRanges (
542
547
[=](const ConstantRange &CR1, const ConstantRange &CR2) {
548
+ unsigned Bits = CR1.getBitWidth ();
543
549
SmallBitVector Elems (1 << Bits);
544
550
APInt Num (Bits, 0 );
545
551
for (unsigned I = 0 , Limit = 1 << Bits; I < Limit; ++I, ++Num)
@@ -630,8 +636,7 @@ TEST_F(ConstantRangeTest, SetDifference) {
630
636
}
631
637
632
638
TEST_F (ConstantRangeTest, getActiveBits) {
633
- unsigned Bits = 4 ;
634
- EnumerateConstantRanges (Bits, [&](const ConstantRange &CR) {
639
+ EnumerateInterestingConstantRanges ([&](const ConstantRange &CR) {
635
640
unsigned Exact = 0 ;
636
641
ForeachNumInConstantRange (CR, [&](const APInt &N) {
637
642
Exact = std::max (Exact, N.getActiveBits ());
@@ -642,8 +647,8 @@ TEST_F(ConstantRangeTest, getActiveBits) {
642
647
});
643
648
}
644
649
TEST_F (ConstantRangeTest, losslessUnsignedTruncationZeroext) {
645
- unsigned Bits = 4 ;
646
- EnumerateConstantRanges (Bits, [&]( const ConstantRange &CR) {
650
+ EnumerateInterestingConstantRanges ([&]( const ConstantRange &CR) {
651
+ unsigned Bits = CR. getBitWidth ();
647
652
unsigned MinBitWidth = CR.getActiveBits ();
648
653
if (MinBitWidth == 0 ) {
649
654
EXPECT_TRUE (CR.isEmptySet () ||
@@ -657,8 +662,7 @@ TEST_F(ConstantRangeTest, losslessUnsignedTruncationZeroext) {
657
662
}
658
663
659
664
TEST_F (ConstantRangeTest, getMinSignedBits) {
660
- unsigned Bits = 4 ;
661
- EnumerateConstantRanges (Bits, [&](const ConstantRange &CR) {
665
+ EnumerateInterestingConstantRanges ([&](const ConstantRange &CR) {
662
666
unsigned Exact = 0 ;
663
667
ForeachNumInConstantRange (CR, [&](const APInt &N) {
664
668
Exact = std::max (Exact, N.getMinSignedBits ());
@@ -669,8 +673,8 @@ TEST_F(ConstantRangeTest, getMinSignedBits) {
669
673
});
670
674
}
671
675
TEST_F (ConstantRangeTest, losslessSignedTruncationSignext) {
672
- unsigned Bits = 4 ;
673
- EnumerateConstantRanges (Bits, [&]( const ConstantRange &CR) {
676
+ EnumerateInterestingConstantRanges ([&]( const ConstantRange &CR) {
677
+ unsigned Bits = CR. getBitWidth ();
674
678
unsigned MinBitWidth = CR.getMinSignedBits ();
675
679
if (MinBitWidth == 0 ) {
676
680
EXPECT_TRUE (CR.isEmptySet ());
@@ -1155,11 +1159,11 @@ TEST_F(ConstantRangeTest, SDiv) {
1155
1159
ConstantRange OneBit = ConstantRange::getFull (1 );
1156
1160
EXPECT_EQ (OneBit.sdiv (OneBit), ConstantRange (APInt (1 , 0 )));
1157
1161
1158
- unsigned Bits = 4 ;
1159
- EnumerateTwoConstantRanges (Bits, [&](const ConstantRange &CR1,
1160
- const ConstantRange &CR2) {
1162
+ EnumerateTwoInterestingConstantRanges ([&](const ConstantRange &CR1,
1163
+ const ConstantRange &CR2) {
1161
1164
// Collect possible results in a bit vector. We store the signed value plus
1162
1165
// a bias to make it unsigned.
1166
+ unsigned Bits = CR1.getBitWidth ();
1163
1167
int Bias = 1 << (Bits - 1 );
1164
1168
BitVector Results (1 << Bits);
1165
1169
ForeachNumInConstantRange (CR1, [&](const APInt &N1) {
@@ -1473,9 +1477,8 @@ TEST(ConstantRange, MakeSatisfyingICmpRegion) {
1473
1477
}
1474
1478
1475
1479
void ICmpTestImpl (CmpInst::Predicate Pred) {
1476
- unsigned Bits = 4 ;
1477
- EnumerateTwoConstantRanges (
1478
- Bits, [&](const ConstantRange &CR1, const ConstantRange &CR2) {
1480
+ EnumerateTwoInterestingConstantRanges (
1481
+ [&](const ConstantRange &CR1, const ConstantRange &CR2) {
1479
1482
bool Exhaustive = true ;
1480
1483
ForeachNumInConstantRange (CR1, [&](const APInt &N1) {
1481
1484
ForeachNumInConstantRange (CR2, [&](const APInt &N2) {
@@ -1888,8 +1891,8 @@ TEST(ConstantRange, GetEquivalentICmp) {
1888
1891
EXPECT_EQ (Pred, CmpInst::ICMP_NE);
1889
1892
EXPECT_EQ (RHS, APInt (32 , -1 ));
1890
1893
1891
- unsigned Bits = 4 ;
1892
- EnumerateConstantRanges (Bits, [ Bits]( const ConstantRange &CR) {
1894
+ EnumerateInterestingConstantRanges ([]( const ConstantRange &CR) {
1895
+ unsigned Bits = CR. getBitWidth ();
1893
1896
CmpInst::Predicate Pred;
1894
1897
APInt RHS, Offset;
1895
1898
CR.getEquivalentICmp (Pred, RHS, Offset);
@@ -2079,12 +2082,11 @@ TEST_F(ConstantRangeTest, SignedSubOverflow) {
2079
2082
EXPECT_MAY_OVERFLOW (F.signedSubMayOverflow (F));
2080
2083
}
2081
2084
2082
- template <typename Fn1, typename Fn2>
2085
+ template <typename Fn1, typename Fn2>
2083
2086
static void TestOverflowExhaustive (Fn1 OverflowFn, Fn2 MayOverflowFn) {
2084
2087
// Constant range overflow checks are tested exhaustively on 4-bit numbers.
2085
- unsigned Bits = 4 ;
2086
- EnumerateTwoConstantRanges (Bits, [=](const ConstantRange &CR1,
2087
- const ConstantRange &CR2) {
2088
+ EnumerateTwoInterestingConstantRanges ([=](const ConstantRange &CR1,
2089
+ const ConstantRange &CR2) {
2088
2090
// Loop over all N1 in CR1 and N2 in CR2 and check whether any of the
2089
2091
// operations have overflow / have no overflow.
2090
2092
bool RangeHasOverflowLow = false ;
@@ -2259,10 +2261,9 @@ TEST_F(ConstantRangeTest, FromKnownBitsExhaustive) {
2259
2261
}
2260
2262
2261
2263
TEST_F (ConstantRangeTest, ToKnownBits) {
2262
- unsigned Bits = 4 ;
2263
- EnumerateConstantRanges (Bits, [&](const ConstantRange &CR) {
2264
+ EnumerateInterestingConstantRanges ([&](const ConstantRange &CR) {
2264
2265
KnownBits Known = CR.toKnownBits ();
2265
- KnownBits ExpectedKnown (Bits );
2266
+ KnownBits ExpectedKnown (CR. getBitWidth () );
2266
2267
ExpectedKnown.Zero .setAllBits ();
2267
2268
ExpectedKnown.One .setAllBits ();
2268
2269
ForeachNumInConstantRange (CR, [&](const APInt &N) {
@@ -2285,8 +2286,7 @@ TEST_F(ConstantRangeTest, Negative) {
2285
2286
EXPECT_FALSE (Full.isAllNegative ());
2286
2287
EXPECT_FALSE (Full.isAllNonNegative ());
2287
2288
2288
- unsigned Bits = 4 ;
2289
- EnumerateConstantRanges (Bits, [](const ConstantRange &CR) {
2289
+ EnumerateInterestingConstantRanges ([](const ConstantRange &CR) {
2290
2290
bool AllNegative = true ;
2291
2291
bool AllNonNegative = true ;
2292
2292
ForeachNumInConstantRange (CR, [&](const APInt &N) {
@@ -2534,9 +2534,8 @@ TEST_F(ConstantRangeTest, binaryNot) {
2534
2534
2535
2535
template <typename T>
2536
2536
void testConstantRangeICmpPredEquivalence (ICmpInst::Predicate SrcPred, T Func) {
2537
- unsigned Bits = 4 ;
2538
- EnumerateTwoConstantRanges (
2539
- Bits, [&](const ConstantRange &CR1, const ConstantRange &CR2) {
2537
+ EnumerateTwoInterestingConstantRanges (
2538
+ [&](const ConstantRange &CR1, const ConstantRange &CR2) {
2540
2539
ICmpInst::Predicate TgtPred;
2541
2540
bool ExpectedEquivalent;
2542
2541
std::tie (TgtPred, ExpectedEquivalent) = Func (CR1, CR2);
0 commit comments