Skip to content

Commit 9eb8040

Browse files
committed
[ConstantRange] Support checking optimality for subset of inputs (NFC)
We always want to check correctness, but for some operations we can only guarantee optimality for a subset of inputs. Accept an additional predicate that determines whether optimality for a given pair of ranges should be checked.
1 parent 53fc510 commit 9eb8040

File tree

1 file changed

+30
-23
lines changed

1 file changed

+30
-23
lines changed

llvm/unittests/IR/ConstantRangeTest.cpp

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,18 @@ testing::AssertionResult rangeContains(const ConstantRange &CR, const APInt &N,
111111
// Elems under the given PreferenceFn. The preference function should return
112112
// true if the first range argument is strictly preferred to the second one.
113113
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) {
115116
unsigned BitWidth = CR.getBitWidth();
116117

117118
// Check conservative correctness.
118119
for (unsigned Elem : Elems.set_bits()) {
119120
EXPECT_TRUE(rangeContains(CR, APInt(BitWidth, Elem), Inputs));
120121
}
121122

123+
if (!CheckOptimality)
124+
return;
125+
122126
// Make sure we have at least one element for the code below.
123127
if (Elems.none()) {
124128
EXPECT_TRUE(CR.isEmptySet());
@@ -182,9 +186,23 @@ using BinaryRangeFn = llvm::function_ref<ConstantRange(const ConstantRange &,
182186
const ConstantRange &)>;
183187
using BinaryIntFn = llvm::function_ref<Optional<APInt>(const APInt &,
184188
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+
}
185200

201+
// CheckFn determines whether optimality is checked for a given range pair.
202+
// Correctness is always checked.
186203
static void TestBinaryOpExhaustive(BinaryRangeFn RangeFn, BinaryIntFn IntFn,
187-
PreferFn PreferenceFn = PreferSmallest) {
204+
PreferFn PreferenceFn = PreferSmallest,
205+
BinaryCheckFn CheckFn = CheckAll) {
188206
unsigned Bits = 4;
189207
EnumerateTwoConstantRanges(
190208
Bits, [&](const ConstantRange &CR1, const ConstantRange &CR2) {
@@ -195,23 +213,8 @@ static void TestBinaryOpExhaustive(BinaryRangeFn RangeFn, BinaryIntFn IntFn,
195213
Elems.set(ResultN->getZExtValue());
196214
});
197215
});
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));
215218
});
216219
}
217220

@@ -1303,15 +1306,17 @@ TEST_F(ConstantRangeTest, URem) {
13031306
.urem(ConstantRange(APInt(16, 10))),
13041307
ConstantRange(APInt(16, 0), APInt(16, 10)));
13051308

1306-
TestBinaryOpExhaustiveCorrectnessOnly(
1309+
TestBinaryOpExhaustive(
13071310
[](const ConstantRange &CR1, const ConstantRange &CR2) {
13081311
return CR1.urem(CR2);
13091312
},
13101313
[](const APInt &N1, const APInt &N2) -> Optional<APInt> {
13111314
if (N2.isZero())
13121315
return None;
13131316
return N1.urem(N2);
1314-
});
1317+
},
1318+
PreferSmallest,
1319+
CheckSingleElementsOnly);
13151320
}
13161321

13171322
TEST_F(ConstantRangeTest, SRem) {
@@ -1377,15 +1382,17 @@ TEST_F(ConstantRangeTest, SRem) {
13771382
.srem(ConstantRange(APInt(16, 10))),
13781383
ConstantRange(APInt(16, 0), APInt(16, 10)));
13791384

1380-
TestBinaryOpExhaustiveCorrectnessOnly(
1385+
TestBinaryOpExhaustive(
13811386
[](const ConstantRange &CR1, const ConstantRange &CR2) {
13821387
return CR1.srem(CR2);
13831388
},
13841389
[](const APInt &N1, const APInt &N2) -> Optional<APInt> {
13851390
if (N2.isZero())
13861391
return None;
13871392
return N1.srem(N2);
1388-
});
1393+
},
1394+
PreferSmallest,
1395+
CheckSingleElementsOnly);
13891396
}
13901397

13911398
TEST_F(ConstantRangeTest, Shl) {

0 commit comments

Comments
 (0)