From: Nikita Popov Date: Sun, 20 Oct 2019 18:59:14 +0000 (+0000) Subject: [ConstantRange] Optimize nowrap region test, remove redundant tests; NFC X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=467514942a5076757f238930fae0a7a6e9004af2;p=llvm [ConstantRange] Optimize nowrap region test, remove redundant tests; NFC Enumerate one less constant range in TestNoWrapRegionExhaustive, which was unnecessary. This allows us to bump the bit count from 3 to 5 while keeping reasonable timing. Drop four tests for multiply nowrap regions, as these cover subsets of the exhaustive test. They do use a wider bitwidth, but I don't think it's worthwhile to have them additionally now. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@375369 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/unittests/IR/ConstantRangeTest.cpp b/unittests/IR/ConstantRangeTest.cpp index 58a25166d83..fa67032551f 100644 --- a/unittests/IR/ConstantRangeTest.cpp +++ b/unittests/IR/ConstantRangeTest.cpp @@ -1537,32 +1537,31 @@ TEST(ConstantRange, MakeGuaranteedNoWrapRegion) { template 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 @@ -1694,85 +1693,6 @@ TEST(ConstantRange, GetEquivalentICmp) { EXPECT_EQ(RHS, APInt(32, -1)); } -TEST(ConstantRange, MakeGuaranteedNoWrapRegionMulUnsignedSingleValue) { - typedef OverflowingBinaryOperator OBO; - - for (uint64_t I = std::numeric_limits::min(); - I <= std::numeric_limits::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::min(); - V <= std::numeric_limits::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::min(); - I <= std::numeric_limits::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::min(); - V <= std::numeric_limits::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::min(); - Lo <= std::numeric_limits::max(); Lo++) { - for (uint64_t Hi = Lo; Hi <= std::numeric_limits::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::min(); - V <= std::numeric_limits::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) \