Skip to content

Commit 365d729

Browse files
committed
[ConstantRange] Cleanup addWithNoWrap() by just piggybacking on sadd_sat()/uadd_sat()
As discussed in https://reviews.llvm.org/D69918 that happens to work as intended, and returns empty set if there is always an overflow because we get lucky with intersection. Since there's now an explicit test for that, let's prefer cleaner code.
1 parent b5ddcb9 commit 365d729

File tree

1 file changed

+8
-32
lines changed

1 file changed

+8
-32
lines changed

llvm/lib/IR/ConstantRange.cpp

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -864,41 +864,17 @@ ConstantRange ConstantRange::addWithNoWrap(const ConstantRange &Other,
864864
using OBO = OverflowingBinaryOperator;
865865
ConstantRange Result = add(Other);
866866

867-
auto addWithNoUnsignedWrap = [this](const ConstantRange &Other) {
868-
APInt LMin = getUnsignedMin(), LMax = getUnsignedMax();
869-
APInt RMin = Other.getUnsignedMin(), RMax = Other.getUnsignedMax();
870-
bool Overflow;
871-
APInt NewMin = LMin.uadd_ov(RMin, Overflow);
872-
if (Overflow)
873-
return getEmpty();
874-
APInt NewMax = LMax.uadd_sat(RMax);
875-
return getNonEmpty(std::move(NewMin), std::move(NewMax) + 1);
876-
};
877-
878-
auto addWithNoSignedWrap = [this](const ConstantRange &Other) {
879-
APInt LMin = getSignedMin(), LMax = getSignedMax();
880-
APInt RMin = Other.getSignedMin(), RMax = Other.getSignedMax();
881-
if (LMin.isNonNegative()) {
882-
bool Overflow;
883-
APInt Temp = LMin.sadd_ov(RMin, Overflow);
884-
if (Overflow)
885-
return getEmpty();
886-
}
887-
if (LMax.isNegative()) {
888-
bool Overflow;
889-
APInt Temp = LMax.sadd_ov(RMax, Overflow);
890-
if (Overflow)
891-
return getEmpty();
892-
}
893-
APInt NewMin = LMin.sadd_sat(RMin);
894-
APInt NewMax = LMax.sadd_sat(RMax);
895-
return getNonEmpty(std::move(NewMin), std::move(NewMax) + 1);
896-
};
867+
// If an overflow happens for every value pair in these two constant ranges,
868+
// we must return Empty set. In this case, we get that for free, because we
869+
// get lucky that intersection of add() with uadd_sat()/sadd_sat() results
870+
// in an empty set.
897871

898872
if (NoWrapKind & OBO::NoSignedWrap)
899-
Result = Result.intersectWith(addWithNoSignedWrap(Other), RangeType);
873+
Result = Result.intersectWith(sadd_sat(Other), RangeType);
874+
900875
if (NoWrapKind & OBO::NoUnsignedWrap)
901-
Result = Result.intersectWith(addWithNoUnsignedWrap(Other), RangeType);
876+
Result = Result.intersectWith(uadd_sat(Other), RangeType);
877+
902878
return Result;
903879
}
904880

0 commit comments

Comments
 (0)