diff options
Diffstat (limited to 'llvm')
| -rw-r--r-- | llvm/include/llvm/IR/ConstantRange.h | 8 | ||||
| -rw-r--r-- | llvm/lib/IR/ConstantRange.cpp | 20 | ||||
| -rw-r--r-- | llvm/unittests/IR/ConstantRangeTest.cpp | 16 |
3 files changed, 44 insertions, 0 deletions
diff --git a/llvm/include/llvm/IR/ConstantRange.h b/llvm/include/llvm/IR/ConstantRange.h index ec73b514ba7..0a07aa5afb3 100644 --- a/llvm/include/llvm/IR/ConstantRange.h +++ b/llvm/include/llvm/IR/ConstantRange.h @@ -434,6 +434,14 @@ public: /// Perform a signed saturating subtraction of two constant ranges. ConstantRange ssub_sat(const ConstantRange &Other) const; + /// Perform an unsigned saturating left shift of this constant range by a + /// value in \p Other. + ConstantRange ushl_sat(const ConstantRange &Other) const; + + /// Perform a signed saturating left shift of this constant range by a + /// value in \p Other. + ConstantRange sshl_sat(const ConstantRange &Other) const; + /// Return a new range that is the logical not of the current set. ConstantRange inverse() const; diff --git a/llvm/lib/IR/ConstantRange.cpp b/llvm/lib/IR/ConstantRange.cpp index 18d8e9d42c6..63a6494fc17 100644 --- a/llvm/lib/IR/ConstantRange.cpp +++ b/llvm/lib/IR/ConstantRange.cpp @@ -1333,6 +1333,26 @@ ConstantRange ConstantRange::ssub_sat(const ConstantRange &Other) const { return getNonEmpty(std::move(NewL), std::move(NewU)); } +ConstantRange ConstantRange::ushl_sat(const ConstantRange &Other) const { + if (isEmptySet() || Other.isEmptySet()) + return getEmpty(); + + APInt NewL = getUnsignedMin().ushl_sat(Other.getUnsignedMin()); + APInt NewU = getUnsignedMax().ushl_sat(Other.getUnsignedMax()) + 1; + return getNonEmpty(std::move(NewL), std::move(NewU)); +} + +ConstantRange ConstantRange::sshl_sat(const ConstantRange &Other) const { + if (isEmptySet() || Other.isEmptySet()) + return getEmpty(); + + APInt Min = getSignedMin(), Max = getSignedMax(); + APInt ShAmtMin = Other.getUnsignedMin(), ShAmtMax = Other.getUnsignedMax(); + APInt NewL = Min.sshl_sat(isAllNonNegative() ? ShAmtMin : ShAmtMax); + APInt NewU = Max.sshl_sat(isAllNegative() ? ShAmtMin : ShAmtMax) + 1; + return getNonEmpty(std::move(NewL), std::move(NewU)); +} + ConstantRange ConstantRange::inverse() const { if (isFullSet()) return getEmpty(); diff --git a/llvm/unittests/IR/ConstantRangeTest.cpp b/llvm/unittests/IR/ConstantRangeTest.cpp index 4bc620f3ce8..7440574cee4 100644 --- a/llvm/unittests/IR/ConstantRangeTest.cpp +++ b/llvm/unittests/IR/ConstantRangeTest.cpp @@ -2217,6 +2217,14 @@ TEST_F(ConstantRangeTest, USubSat) { }); } +TEST_F(ConstantRangeTest, UShlSat) { + TestUnsignedBinOpExhaustive( + [](const ConstantRange &CR1, const ConstantRange &CR2) { + return CR1.ushl_sat(CR2); + }, + [](const APInt &N1, const APInt &N2) { return N1.ushl_sat(N2); }); +} + TEST_F(ConstantRangeTest, SAddSat) { TestSignedBinOpExhaustive( [](const ConstantRange &CR1, const ConstantRange &CR2) { @@ -2237,6 +2245,14 @@ TEST_F(ConstantRangeTest, SSubSat) { }); } +TEST_F(ConstantRangeTest, SShlSat) { + TestSignedBinOpExhaustive( + [](const ConstantRange &CR1, const ConstantRange &CR2) { + return CR1.sshl_sat(CR2); + }, + [](const APInt &N1, const APInt &N2) { return N1.sshl_sat(N2); }); +} + TEST_F(ConstantRangeTest, Abs) { unsigned Bits = 4; EnumerateConstantRanges(Bits, [&](const ConstantRange &CR) { |

