@@ -111,14 +111,18 @@ testing::AssertionResult rangeContains(const ConstantRange &CR, const APInt &N,
111
111
// Elems under the given PreferenceFn. The preference function should return
112
112
// true if the first range argument is strictly preferred to the second one.
113
113
static void TestRange (const ConstantRange &CR, const SmallBitVector &Elems,
114
- PreferFn PreferenceFn, ArrayRef<ConstantRange> Inputs) {
114
+ PreferFn PreferenceFn, ArrayRef<ConstantRange> Inputs,
115
+ bool CheckOptimality = true ) {
115
116
unsigned BitWidth = CR.getBitWidth ();
116
117
117
118
// Check conservative correctness.
118
119
for (unsigned Elem : Elems.set_bits ()) {
119
120
EXPECT_TRUE (rangeContains (CR, APInt (BitWidth, Elem), Inputs));
120
121
}
121
122
123
+ if (!CheckOptimality)
124
+ return ;
125
+
122
126
// Make sure we have at least one element for the code below.
123
127
if (Elems.none ()) {
124
128
EXPECT_TRUE (CR.isEmptySet ());
@@ -182,9 +186,23 @@ using BinaryRangeFn = llvm::function_ref<ConstantRange(const ConstantRange &,
182
186
const ConstantRange &)>;
183
187
using BinaryIntFn = llvm::function_ref<Optional<APInt>(const APInt &,
184
188
const APInt &)>;
189
+ using BinaryCheckFn = llvm::function_ref<bool (const ConstantRange &,
190
+ const ConstantRange &)>;
191
+
192
+ static bool CheckAll (const ConstantRange &, const ConstantRange &) {
193
+ return true ;
194
+ }
195
+
196
+ static bool CheckSingleElementsOnly (const ConstantRange &CR1,
197
+ const ConstantRange &CR2) {
198
+ return CR1.isSingleElement () && CR2.isSingleElement ();
199
+ }
185
200
201
+ // CheckFn determines whether optimality is checked for a given range pair.
202
+ // Correctness is always checked.
186
203
static void TestBinaryOpExhaustive (BinaryRangeFn RangeFn, BinaryIntFn IntFn,
187
- PreferFn PreferenceFn = PreferSmallest) {
204
+ PreferFn PreferenceFn = PreferSmallest,
205
+ BinaryCheckFn CheckFn = CheckAll) {
188
206
unsigned Bits = 4 ;
189
207
EnumerateTwoConstantRanges (
190
208
Bits, [&](const ConstantRange &CR1, const ConstantRange &CR2) {
@@ -195,23 +213,8 @@ static void TestBinaryOpExhaustive(BinaryRangeFn RangeFn, BinaryIntFn IntFn,
195
213
Elems.set (ResultN->getZExtValue ());
196
214
});
197
215
});
198
- TestRange (RangeFn (CR1, CR2), Elems, PreferenceFn, {CR1, CR2});
199
- });
200
- }
201
-
202
- static void TestBinaryOpExhaustiveCorrectnessOnly (BinaryRangeFn RangeFn,
203
- BinaryIntFn IntFn) {
204
- unsigned Bits = 4 ;
205
- EnumerateTwoConstantRanges (
206
- Bits, [&](const ConstantRange &CR1, const ConstantRange &CR2) {
207
- ConstantRange ResultCR = RangeFn (CR1, CR2);
208
- ForeachNumInConstantRange (CR1, [&](const APInt &N1) {
209
- ForeachNumInConstantRange (CR2, [&](const APInt &N2) {
210
- if (Optional<APInt> ResultN = IntFn (N1, N2)) {
211
- EXPECT_TRUE (rangeContains (ResultCR, *ResultN, {CR1, CR2}));
212
- }
213
- });
214
- });
216
+ TestRange (RangeFn (CR1, CR2), Elems, PreferenceFn, {CR1, CR2},
217
+ CheckFn (CR1, CR2));
215
218
});
216
219
}
217
220
@@ -1303,15 +1306,17 @@ TEST_F(ConstantRangeTest, URem) {
1303
1306
.urem (ConstantRange (APInt (16 , 10 ))),
1304
1307
ConstantRange (APInt (16 , 0 ), APInt (16 , 10 )));
1305
1308
1306
- TestBinaryOpExhaustiveCorrectnessOnly (
1309
+ TestBinaryOpExhaustive (
1307
1310
[](const ConstantRange &CR1, const ConstantRange &CR2) {
1308
1311
return CR1.urem (CR2);
1309
1312
},
1310
1313
[](const APInt &N1, const APInt &N2) -> Optional<APInt> {
1311
1314
if (N2.isZero ())
1312
1315
return None;
1313
1316
return N1.urem (N2);
1314
- });
1317
+ },
1318
+ PreferSmallest,
1319
+ CheckSingleElementsOnly);
1315
1320
}
1316
1321
1317
1322
TEST_F (ConstantRangeTest, SRem) {
@@ -1377,15 +1382,17 @@ TEST_F(ConstantRangeTest, SRem) {
1377
1382
.srem (ConstantRange (APInt (16 , 10 ))),
1378
1383
ConstantRange (APInt (16 , 0 ), APInt (16 , 10 )));
1379
1384
1380
- TestBinaryOpExhaustiveCorrectnessOnly (
1385
+ TestBinaryOpExhaustive (
1381
1386
[](const ConstantRange &CR1, const ConstantRange &CR2) {
1382
1387
return CR1.srem (CR2);
1383
1388
},
1384
1389
[](const APInt &N1, const APInt &N2) -> Optional<APInt> {
1385
1390
if (N2.isZero ())
1386
1391
return None;
1387
1392
return N1.srem (N2);
1388
- });
1393
+ },
1394
+ PreferSmallest,
1395
+ CheckSingleElementsOnly);
1389
1396
}
1390
1397
1391
1398
TEST_F (ConstantRangeTest, Shl) {
0 commit comments