diff options
author | Yingwei Zheng <dtcxzyw2333@gmail.com> | 2024-08-07 02:00:33 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-08-07 02:00:33 +0800 |
commit | 07b29fc808ca0842d02cf4e973381b974bfdf19f (patch) | |
tree | e424bcb408c8ab530ca84705747fee69227c0035 /llvm/unittests/IR/ConstantRangeTest.cpp | |
parent | 4dee6411e0d993fd17099bd7564276474412383e (diff) | |
download | llvm-07b29fc808ca0842d02cf4e973381b974bfdf19f.zip llvm-07b29fc808ca0842d02cf4e973381b974bfdf19f.tar.gz llvm-07b29fc808ca0842d02cf4e973381b974bfdf19f.tar.bz2 |
[ConstantRange] Improve `shlWithNoWrap` (#101800)
Closes https://github.com/dtcxzyw/llvm-tools/issues/22.
Diffstat (limited to 'llvm/unittests/IR/ConstantRangeTest.cpp')
-rw-r--r-- | llvm/unittests/IR/ConstantRangeTest.cpp | 39 |
1 files changed, 36 insertions, 3 deletions
diff --git a/llvm/unittests/IR/ConstantRangeTest.cpp b/llvm/unittests/IR/ConstantRangeTest.cpp index 1705f3e..4815117 100644 --- a/llvm/unittests/IR/ConstantRangeTest.cpp +++ b/llvm/unittests/IR/ConstantRangeTest.cpp @@ -228,6 +228,12 @@ static bool CheckNonSignWrappedOnly(const ConstantRange &CR1, return !CR1.isSignWrappedSet() && !CR2.isSignWrappedSet(); } +static bool +CheckNoSignedWrappedLHSAndNoWrappedRHSOnly(const ConstantRange &CR1, + const ConstantRange &CR2) { + return !CR1.isSignWrappedSet() && !CR2.isWrappedSet(); +} + static bool CheckNonWrappedOrSignWrappedOnly(const ConstantRange &CR1, const ConstantRange &CR2) { return !CR1.isWrappedSet() && !CR1.isSignWrappedSet() && @@ -1506,7 +1512,9 @@ TEST_F(ConstantRangeTest, ShlWithNoWrap) { using OBO = OverflowingBinaryOperator; TestBinaryOpExhaustive( [](const ConstantRange &CR1, const ConstantRange &CR2) { - return CR1.shlWithNoWrap(CR2, OBO::NoUnsignedWrap); + ConstantRange Res = CR1.shlWithNoWrap(CR2, OBO::NoUnsignedWrap); + EXPECT_TRUE(CR1.shl(CR2).contains(Res)); + return Res; }, [](const APInt &N1, const APInt &N2) -> std::optional<APInt> { bool IsOverflow; @@ -1515,7 +1523,7 @@ TEST_F(ConstantRangeTest, ShlWithNoWrap) { return std::nullopt; return Res; }, - PreferSmallest, CheckCorrectnessOnly); + PreferSmallest, CheckNonWrappedOnly); TestBinaryOpExhaustive( [](const ConstantRange &CR1, const ConstantRange &CR2) { return CR1.shlWithNoWrap(CR2, OBO::NoSignedWrap); @@ -1527,7 +1535,7 @@ TEST_F(ConstantRangeTest, ShlWithNoWrap) { return std::nullopt; return Res; }, - PreferSmallest, CheckCorrectnessOnly); + PreferSmallestSigned, CheckNoSignedWrappedLHSAndNoWrappedRHSOnly); TestBinaryOpExhaustive( [](const ConstantRange &CR1, const ConstantRange &CR2) { return CR1.shlWithNoWrap(CR2, OBO::NoUnsignedWrap | OBO::NoSignedWrap); @@ -1542,6 +1550,31 @@ TEST_F(ConstantRangeTest, ShlWithNoWrap) { return Res1; }, PreferSmallest, CheckCorrectnessOnly); + + EXPECT_EQ(One.shlWithNoWrap(Full, OBO::NoSignedWrap), + ConstantRange(APInt(16, 10), APInt(16, 20481))); + EXPECT_EQ(One.shlWithNoWrap(Full, OBO::NoUnsignedWrap), + ConstantRange(APInt(16, 10), APInt(16, -24575))); + EXPECT_EQ(One.shlWithNoWrap(Full, OBO::NoSignedWrap | OBO::NoUnsignedWrap), + ConstantRange(APInt(16, 10), APInt(16, 20481))); + ConstantRange NegOne(APInt(16, 0xffff)); + EXPECT_EQ(NegOne.shlWithNoWrap(Full, OBO::NoSignedWrap), + ConstantRange(APInt(16, -32768), APInt(16, 0))); + EXPECT_EQ(NegOne.shlWithNoWrap(Full, OBO::NoUnsignedWrap), NegOne); + EXPECT_EQ(ConstantRange(APInt(16, 768)) + .shlWithNoWrap(Full, OBO::NoSignedWrap | OBO::NoUnsignedWrap), + ConstantRange(APInt(16, 768), APInt(16, 24577))); + EXPECT_EQ(Full.shlWithNoWrap(ConstantRange(APInt(16, 1), APInt(16, 16)), + OBO::NoUnsignedWrap), + ConstantRange(APInt(16, 0), APInt(16, -1))); + EXPECT_EQ(ConstantRange(APInt(4, 3), APInt(4, -8)) + .shlWithNoWrap(ConstantRange(APInt(4, 0), APInt(4, 4)), + OBO::NoSignedWrap), + ConstantRange(APInt(4, 3), APInt(4, -8))); + EXPECT_EQ(ConstantRange(APInt(4, -1), APInt(4, 0)) + .shlWithNoWrap(ConstantRange(APInt(4, 1), APInt(4, 4)), + OBO::NoSignedWrap), + ConstantRange(APInt(4, -8), APInt(4, -1))); } TEST_F(ConstantRangeTest, Lshr) { |