diff options
author | Nikita Popov <nikita.ppv@gmail.com> | 2021-10-27 15:12:02 +0200 |
---|---|---|
committer | Nikita Popov <nikita.ppv@gmail.com> | 2021-10-27 21:01:09 +0200 |
commit | ea7be26045e2f4fbf0a3ceb8842b024e3eb6e8a5 (patch) | |
tree | 0a412df782cf385bfb427776d18fd4f756962092 /llvm/lib/IR/ConstantRange.cpp | |
parent | b7e12ca7aab73a00d7a7a2fbbd5214dde9353335 (diff) | |
download | llvm-ea7be26045e2f4fbf0a3ceb8842b024e3eb6e8a5.zip llvm-ea7be26045e2f4fbf0a3ceb8842b024e3eb6e8a5.tar.gz llvm-ea7be26045e2f4fbf0a3ceb8842b024e3eb6e8a5.tar.bz2 |
[ConstantRange] Optimize smul_sat() (NFC)
Base the implementation on the APInt smul_sat() implementation,
which is much more efficient than performing calculations in
double the bitwidth.
Diffstat (limited to 'llvm/lib/IR/ConstantRange.cpp')
-rw-r--r-- | llvm/lib/IR/ConstantRange.cpp | 19 |
1 files changed, 7 insertions, 12 deletions
diff --git a/llvm/lib/IR/ConstantRange.cpp b/llvm/lib/IR/ConstantRange.cpp index 6877a5d..a37790d 100644 --- a/llvm/lib/IR/ConstantRange.cpp +++ b/llvm/lib/IR/ConstantRange.cpp @@ -1510,20 +1510,15 @@ ConstantRange ConstantRange::smul_sat(const ConstantRange &Other) const { // [-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); + APInt Min = getSignedMin(); + APInt Max = getSignedMax(); + APInt OtherMin = Other.getSignedMin(); + APInt OtherMax = Other.getSignedMax(); - auto L = {this_min * Other_min, this_min * Other_max, this_max * Other_min, - this_max * Other_max}; + auto L = {Min.smul_sat(OtherMin), Min.smul_sat(OtherMax), + Max.smul_sat(OtherMin), Max.smul_sat(OtherMax)}; 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); + return getNonEmpty(std::min(L, Compare), std::max(L, Compare) + 1); } ConstantRange ConstantRange::ushl_sat(const ConstantRange &Other) const { |