Skip to content

Commit 5a9fd76

Browse files
committed
[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
1 parent 9ca363d commit 5a9fd76

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

llvm/include/llvm/IR/ConstantRange.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,12 @@ class LLVM_NODISCARD ConstantRange {
434434
/// Perform a signed saturating subtraction of two constant ranges.
435435
ConstantRange ssub_sat(const ConstantRange &Other) const;
436436

437+
/// Perform an unsigned saturating multiplication of two constant ranges.
438+
ConstantRange umul_sat(const ConstantRange &Other) const;
439+
440+
/// Perform a signed saturating multiplication of two constant ranges.
441+
ConstantRange smul_sat(const ConstantRange &Other) const;
442+
437443
/// Perform an unsigned saturating left shift of this constant range by a
438444
/// value in \p Other.
439445
ConstantRange ushl_sat(const ConstantRange &Other) const;

llvm/lib/IR/ConstantRange.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,6 +1333,41 @@ ConstantRange ConstantRange::ssub_sat(const ConstantRange &Other) const {
13331333
return getNonEmpty(std::move(NewL), std::move(NewU));
13341334
}
13351335

1336+
ConstantRange ConstantRange::umul_sat(const ConstantRange &Other) const {
1337+
if (isEmptySet() || Other.isEmptySet())
1338+
return getEmpty();
1339+
1340+
APInt NewL = getUnsignedMin().umul_sat(Other.getUnsignedMin());
1341+
APInt NewU = getUnsignedMax().umul_sat(Other.getUnsignedMax()) + 1;
1342+
return getNonEmpty(std::move(NewL), std::move(NewU));
1343+
}
1344+
1345+
ConstantRange ConstantRange::smul_sat(const ConstantRange &Other) const {
1346+
if (isEmptySet() || Other.isEmptySet())
1347+
return getEmpty();
1348+
1349+
// Because we could be dealing with negative numbers here, the lower bound is
1350+
// the smallest of the cartesian product of the lower and upper ranges;
1351+
// for example:
1352+
// [-1,4) * [-2,3) = min(-1*-2, -1*2, 3*-2, 3*2) = -6.
1353+
// Similarly for the upper bound, swapping min for max.
1354+
1355+
APInt this_min = getSignedMin().sext(getBitWidth() * 2);
1356+
APInt this_max = getSignedMax().sext(getBitWidth() * 2);
1357+
APInt Other_min = Other.getSignedMin().sext(getBitWidth() * 2);
1358+
APInt Other_max = Other.getSignedMax().sext(getBitWidth() * 2);
1359+
1360+
auto L = {this_min * Other_min, this_min * Other_max, this_max * Other_min,
1361+
this_max * Other_max};
1362+
auto Compare = [](const APInt &A, const APInt &B) { return A.slt(B); };
1363+
1364+
// Note that we wanted to perform signed saturating multiplication,
1365+
// so since we performed plain multiplication in twice the bitwidth,
1366+
// we need to perform signed saturating truncation.
1367+
return getNonEmpty(std::min(L, Compare).truncSSat(getBitWidth()),
1368+
std::max(L, Compare).truncSSat(getBitWidth()) + 1);
1369+
}
1370+
13361371
ConstantRange ConstantRange::ushl_sat(const ConstantRange &Other) const {
13371372
if (isEmptySet() || Other.isEmptySet())
13381373
return getEmpty();

llvm/unittests/IR/ConstantRangeTest.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2217,6 +2217,14 @@ TEST_F(ConstantRangeTest, USubSat) {
22172217
});
22182218
}
22192219

2220+
TEST_F(ConstantRangeTest, UMulSat) {
2221+
TestUnsignedBinOpExhaustive(
2222+
[](const ConstantRange &CR1, const ConstantRange &CR2) {
2223+
return CR1.umul_sat(CR2);
2224+
},
2225+
[](const APInt &N1, const APInt &N2) { return N1.umul_sat(N2); });
2226+
}
2227+
22202228
TEST_F(ConstantRangeTest, UShlSat) {
22212229
TestUnsignedBinOpExhaustive(
22222230
[](const ConstantRange &CR1, const ConstantRange &CR2) {
@@ -2245,6 +2253,14 @@ TEST_F(ConstantRangeTest, SSubSat) {
22452253
});
22462254
}
22472255

2256+
TEST_F(ConstantRangeTest, SMulSat) {
2257+
TestSignedBinOpExhaustive(
2258+
[](const ConstantRange &CR1, const ConstantRange &CR2) {
2259+
return CR1.smul_sat(CR2);
2260+
},
2261+
[](const APInt &N1, const APInt &N2) { return N1.smul_sat(N2); });
2262+
}
2263+
22482264
TEST_F(ConstantRangeTest, SShlSat) {
22492265
TestSignedBinOpExhaustive(
22502266
[](const ConstantRange &CR1, const ConstantRange &CR2) {

0 commit comments

Comments
 (0)