aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/IR/ConstantRange.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'llvm/lib/IR/ConstantRange.cpp')
-rw-r--r--llvm/lib/IR/ConstantRange.cpp27
1 files changed, 12 insertions, 15 deletions
diff --git a/llvm/lib/IR/ConstantRange.cpp b/llvm/lib/IR/ConstantRange.cpp
index 9d239101d8fd..0dbccaa1a66a 100644
--- a/llvm/lib/IR/ConstantRange.cpp
+++ b/llvm/lib/IR/ConstantRange.cpp
@@ -37,6 +37,7 @@
#include <algorithm>
#include <cassert>
#include <cstdint>
+#include <optional>
using namespace llvm;
@@ -85,7 +86,7 @@ KnownBits ConstantRange::toKnownBits() const {
APInt Min = getUnsignedMin();
APInt Max = getUnsignedMax();
KnownBits Known = KnownBits::makeConstant(Min);
- if (Optional<unsigned> DifferentBit =
+ if (std::optional<unsigned> DifferentBit =
APIntOps::GetMostSignificantDifferentBit(Min, Max)) {
Known.Zero.clearLowBits(*DifferentBit + 1);
Known.One.clearLowBits(*DifferentBit + 1);
@@ -258,10 +259,9 @@ static ConstantRange makeExactMulNUWRegion(const APInt &V) {
/// Exact mul nsw region for single element RHS.
static ConstantRange makeExactMulNSWRegion(const APInt &V) {
- // Handle special case for 0, -1 and 1. See the last for reason why we
- // specialize -1 and 1.
+ // Handle 0 and -1 separately to avoid division by zero or overflow.
unsigned BitWidth = V.getBitWidth();
- if (V == 0 || V.isOne())
+ if (V == 0)
return ConstantRange::getFull(BitWidth);
APInt MinValue = APInt::getSignedMinValue(BitWidth);
@@ -278,10 +278,7 @@ static ConstantRange makeExactMulNSWRegion(const APInt &V) {
Lower = APIntOps::RoundingSDiv(MinValue, V, APInt::Rounding::UP);
Upper = APIntOps::RoundingSDiv(MaxValue, V, APInt::Rounding::DOWN);
}
- // ConstantRange ctor take a half inclusive interval [Lower, Upper + 1).
- // Upper + 1 is guaranteed not to overflow, because |divisor| > 1. 0, -1,
- // and 1 are already handled as special cases.
- return ConstantRange(Lower, Upper + 1);
+ return ConstantRange::getNonEmpty(Lower, Upper + 1);
}
ConstantRange
@@ -699,22 +696,22 @@ ConstantRange ConstantRange::unionWith(const ConstantRange &CR,
return ConstantRange(std::move(L), std::move(U));
}
-Optional<ConstantRange>
+std::optional<ConstantRange>
ConstantRange::exactIntersectWith(const ConstantRange &CR) const {
// TODO: This can be implemented more efficiently.
ConstantRange Result = intersectWith(CR);
if (Result == inverse().unionWith(CR.inverse()).inverse())
return Result;
- return None;
+ return std::nullopt;
}
-Optional<ConstantRange>
+std::optional<ConstantRange>
ConstantRange::exactUnionWith(const ConstantRange &CR) const {
// TODO: This can be implemented more efficiently.
ConstantRange Result = unionWith(CR);
if (Result == inverse().intersectWith(CR.inverse()).inverse())
return Result;
- return None;
+ return std::nullopt;
}
ConstantRange ConstantRange::castOp(Instruction::CastOps CastOp,
@@ -1659,15 +1656,15 @@ ConstantRange ConstantRange::abs(bool IntMinIsPoison) const {
// All non-negative.
if (SMin.isNonNegative())
- return *this;
+ return ConstantRange(SMin, SMax + 1);
// All negative.
if (SMax.isNegative())
return ConstantRange(-SMax, -SMin + 1);
// Range crosses zero.
- return ConstantRange(APInt::getZero(getBitWidth()),
- APIntOps::umax(-SMin, SMax) + 1);
+ return ConstantRange::getNonEmpty(APInt::getZero(getBitWidth()),
+ APIntOps::umax(-SMin, SMax) + 1);
}
ConstantRange::OverflowResult ConstantRange::unsignedAddMayOverflow(