mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2025-01-24 21:05:23 +00:00
[ConstantRange] Add umul_sat()/smul_sat() methods
Summary: To be used in `ConstantRange::mulWithNoOverflow()`, may in future be useful for when saturating shift/mul ops are added. These are precise as far as i can tell. I initially though i will need `APInt::[us]mul_sat()` for these, but it turned out much simpler to do what `ConstantRange::multiply()` does - perform multiplication in twice the bitwidth, and then truncate. Though here we want saturating signed truncation. Reviewers: nikic, reames, spatel Reviewed By: nikic Subscribers: hiraditya, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D69994
This commit is contained in:
parent
5eb53ddf94
commit
6f6c808de5
@ -434,6 +434,12 @@ public:
|
||||
/// Perform a signed saturating subtraction of two constant ranges.
|
||||
ConstantRange ssub_sat(const ConstantRange &Other) const;
|
||||
|
||||
/// Perform an unsigned saturating multiplication of two constant ranges.
|
||||
ConstantRange umul_sat(const ConstantRange &Other) const;
|
||||
|
||||
/// Perform a signed saturating multiplication of two constant ranges.
|
||||
ConstantRange smul_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;
|
||||
|
@ -1333,6 +1333,41 @@ ConstantRange ConstantRange::ssub_sat(const ConstantRange &Other) const {
|
||||
return getNonEmpty(std::move(NewL), std::move(NewU));
|
||||
}
|
||||
|
||||
ConstantRange ConstantRange::umul_sat(const ConstantRange &Other) const {
|
||||
if (isEmptySet() || Other.isEmptySet())
|
||||
return getEmpty();
|
||||
|
||||
APInt NewL = getUnsignedMin().umul_sat(Other.getUnsignedMin());
|
||||
APInt NewU = getUnsignedMax().umul_sat(Other.getUnsignedMax()) + 1;
|
||||
return getNonEmpty(std::move(NewL), std::move(NewU));
|
||||
}
|
||||
|
||||
ConstantRange ConstantRange::smul_sat(const ConstantRange &Other) const {
|
||||
if (isEmptySet() || Other.isEmptySet())
|
||||
return getEmpty();
|
||||
|
||||
// Because we could be dealing with negative numbers here, the lower bound is
|
||||
// the smallest of the cartesian product of the lower and upper ranges;
|
||||
// for example:
|
||||
// [-1,4) * [-2,3) = min(-1*-2, -1*2, 3*-2, 3*2) = -6.
|
||||
// Similarly for the upper bound, swapping min for max.
|
||||
|
||||
APInt this_min = getSignedMin().sext(getBitWidth() * 2);
|
||||
APInt this_max = getSignedMax().sext(getBitWidth() * 2);
|
||||
APInt Other_min = Other.getSignedMin().sext(getBitWidth() * 2);
|
||||
APInt Other_max = Other.getSignedMax().sext(getBitWidth() * 2);
|
||||
|
||||
auto L = {this_min * Other_min, this_min * Other_max, this_max * Other_min,
|
||||
this_max * Other_max};
|
||||
auto Compare = [](const APInt &A, const APInt &B) { return A.slt(B); };
|
||||
|
||||
// Note that we wanted to perform signed saturating multiplication,
|
||||
// so since we performed plain multiplication in twice the bitwidth,
|
||||
// we need to perform signed saturating truncation.
|
||||
return getNonEmpty(std::min(L, Compare).truncSSat(getBitWidth()),
|
||||
std::max(L, Compare).truncSSat(getBitWidth()) + 1);
|
||||
}
|
||||
|
||||
ConstantRange ConstantRange::ushl_sat(const ConstantRange &Other) const {
|
||||
if (isEmptySet() || Other.isEmptySet())
|
||||
return getEmpty();
|
||||
|
@ -2217,6 +2217,14 @@ TEST_F(ConstantRangeTest, USubSat) {
|
||||
});
|
||||
}
|
||||
|
||||
TEST_F(ConstantRangeTest, UMulSat) {
|
||||
TestUnsignedBinOpExhaustive(
|
||||
[](const ConstantRange &CR1, const ConstantRange &CR2) {
|
||||
return CR1.umul_sat(CR2);
|
||||
},
|
||||
[](const APInt &N1, const APInt &N2) { return N1.umul_sat(N2); });
|
||||
}
|
||||
|
||||
TEST_F(ConstantRangeTest, UShlSat) {
|
||||
TestUnsignedBinOpExhaustive(
|
||||
[](const ConstantRange &CR1, const ConstantRange &CR2) {
|
||||
@ -2245,6 +2253,14 @@ TEST_F(ConstantRangeTest, SSubSat) {
|
||||
});
|
||||
}
|
||||
|
||||
TEST_F(ConstantRangeTest, SMulSat) {
|
||||
TestSignedBinOpExhaustive(
|
||||
[](const ConstantRange &CR1, const ConstantRange &CR2) {
|
||||
return CR1.smul_sat(CR2);
|
||||
},
|
||||
[](const APInt &N1, const APInt &N2) { return N1.smul_sat(N2); });
|
||||
}
|
||||
|
||||
TEST_F(ConstantRangeTest, SShlSat) {
|
||||
TestSignedBinOpExhaustive(
|
||||
[](const ConstantRange &CR1, const ConstantRange &CR2) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user