template<typename Fn>
void TestNoWrapRegionExhaustive(Instruction::BinaryOps BinOp,
unsigned NoWrapKind, Fn OverflowFn) {
- // When using 4 bits this test needs ~3s on a debug build.
- unsigned Bits = 3;
- EnumerateTwoConstantRanges(Bits,
- [&](const ConstantRange &CR1, const ConstantRange &CR2) {
- if (CR2.isEmptySet())
- return;
-
- ConstantRange NoWrap =
- ConstantRange::makeGuaranteedNoWrapRegion(BinOp, CR2, NoWrapKind);
- ForeachNumInConstantRange(CR1, [&](const APInt &N1) {
- bool NoOverflow = true;
- bool Overflow = true;
- ForeachNumInConstantRange(CR2, [&](const APInt &N2) {
- if (OverflowFn(N1, N2))
- NoOverflow = false;
- else
- Overflow = false;
- });
- EXPECT_EQ(NoOverflow, NoWrap.contains(N1));
+ unsigned Bits = 5;
+ EnumerateConstantRanges(Bits, [&](const ConstantRange &CR) {
+ if (CR.isEmptySet())
+ return;
- // The no-wrap range is exact for single-element ranges.
- if (CR2.isSingleElement()) {
- EXPECT_EQ(Overflow, !NoWrap.contains(N1));
- }
- });
+ ConstantRange NoWrap =
+ ConstantRange::makeGuaranteedNoWrapRegion(BinOp, CR, NoWrapKind);
+ ConstantRange Full = ConstantRange::getFull(Bits);
+ ForeachNumInConstantRange(Full, [&](const APInt &N1) {
+ bool NoOverflow = true;
+ bool Overflow = true;
+ ForeachNumInConstantRange(CR, [&](const APInt &N2) {
+ if (OverflowFn(N1, N2))
+ NoOverflow = false;
+ else
+ Overflow = false;
});
+ EXPECT_EQ(NoOverflow, NoWrap.contains(N1));
+
+ // The no-wrap range is exact for single-element ranges.
+ if (CR.isSingleElement()) {
+ EXPECT_EQ(Overflow, !NoWrap.contains(N1));
+ }
+ });
+ });
}
// Show that makeGuaranteedNoWrapRegion() is maximal, and for single-element
EXPECT_EQ(RHS, APInt(32, -1));
}
-TEST(ConstantRange, MakeGuaranteedNoWrapRegionMulUnsignedSingleValue) {
- typedef OverflowingBinaryOperator OBO;
-
- for (uint64_t I = std::numeric_limits<uint8_t>::min();
- I <= std::numeric_limits<uint8_t>::max(); I++) {
- auto Range = ConstantRange::makeGuaranteedNoWrapRegion(
- Instruction::Mul, ConstantRange(APInt(8, I), APInt(8, I + 1)),
- OBO::NoUnsignedWrap);
-
- for (uint64_t V = std::numeric_limits<uint8_t>::min();
- V <= std::numeric_limits<uint8_t>::max(); V++) {
- bool Overflow;
- (void)APInt(8, I).umul_ov(APInt(8, V), Overflow);
- EXPECT_EQ(!Overflow, Range.contains(APInt(8, V)));
- }
- }
-}
-
-TEST(ConstantRange, MakeGuaranteedNoWrapRegionMulSignedSingleValue) {
- typedef OverflowingBinaryOperator OBO;
-
- for (int64_t I = std::numeric_limits<int8_t>::min();
- I <= std::numeric_limits<int8_t>::max(); I++) {
- auto Range = ConstantRange::makeGuaranteedNoWrapRegion(
- Instruction::Mul,
- ConstantRange(APInt(8, I, /*isSigned=*/true),
- APInt(8, I + 1, /*isSigned=*/true)),
- OBO::NoSignedWrap);
-
- for (int64_t V = std::numeric_limits<int8_t>::min();
- V <= std::numeric_limits<int8_t>::max(); V++) {
- bool Overflow;
- (void)APInt(8, I, /*isSigned=*/true)
- .smul_ov(APInt(8, V, /*isSigned=*/true), Overflow);
- EXPECT_EQ(!Overflow, Range.contains(APInt(8, V, /*isSigned=*/true)));
- }
- }
-}
-
-TEST(ConstantRange, MakeGuaranteedNoWrapRegionMulUnsignedRange) {
- typedef OverflowingBinaryOperator OBO;
-
- for (uint64_t Lo = std::numeric_limits<uint8_t>::min();
- Lo <= std::numeric_limits<uint8_t>::max(); Lo++) {
- for (uint64_t Hi = Lo; Hi <= std::numeric_limits<uint8_t>::max(); Hi++) {
- EXPECT_EQ(
- ConstantRange::makeGuaranteedNoWrapRegion(
- Instruction::Mul, ConstantRange(APInt(8, Lo), APInt(8, Hi + 1)),
- OBO::NoUnsignedWrap),
- ConstantRange::makeGuaranteedNoWrapRegion(
- Instruction::Mul, ConstantRange(APInt(8, Hi), APInt(8, Hi + 1)),
- OBO::NoUnsignedWrap));
- }
- }
-}
-
-TEST(ConstantRange, MakeGuaranteedNoWrapRegionMulSignedRange) {
- typedef OverflowingBinaryOperator OBO;
-
- int Lo = -12, Hi = 16;
- auto Range = ConstantRange::makeGuaranteedNoWrapRegion(
- Instruction::Mul,
- ConstantRange(APInt(8, Lo, /*isSigned=*/true),
- APInt(8, Hi + 1, /*isSigned=*/true)),
- OBO::NoSignedWrap);
-
- for (int64_t V = std::numeric_limits<int8_t>::min();
- V <= std::numeric_limits<int8_t>::max(); V++) {
- bool AnyOverflow = false;
- for (int64_t I = Lo; I <= Hi; I++) {
- bool Overflow;
- (void)APInt(8, I, /*isSigned=*/true)
- .smul_ov(APInt(8, V, /*isSigned=*/true), Overflow);
- AnyOverflow |= Overflow;
- }
- EXPECT_EQ(!AnyOverflow, Range.contains(APInt(8, V, /*isSigned=*/true)));
- }
-}
-
#define EXPECT_MAY_OVERFLOW(op) \
EXPECT_EQ(ConstantRange::OverflowResult::MayOverflow, (op))
#define EXPECT_ALWAYS_OVERFLOWS_LOW(op) \