[PATCH] D59450: [ValueTracking] Use ConstantRange overflow check for signed add (NFC)
Nikita Popov via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Sun Mar 17 14:27:41 PDT 2019
This revision was automatically updated to reflect the committed changes.
Closed by commit rL356345: [ValueTracking] Use ConstantRange overflow check for signed add; NFC (authored by nikic, committed by ).
Changed prior to commit:
https://reviews.llvm.org/D59450?vs=191041&id=191047#toc
Repository:
rL LLVM
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D59450/new/
https://reviews.llvm.org/D59450
Files:
llvm/trunk/lib/Analysis/ValueTracking.cpp
Index: llvm/trunk/lib/Analysis/ValueTracking.cpp
===================================================================
--- llvm/trunk/lib/Analysis/ValueTracking.cpp
+++ llvm/trunk/lib/Analysis/ValueTracking.cpp
@@ -4092,51 +4092,6 @@
return mapOverflowResult(LHSRange.unsignedAddMayOverflow(RHSRange));
}
-/// Return true if we can prove that adding the two values of the
-/// knownbits will not overflow.
-/// Otherwise return false.
-static bool checkRippleForSignedAdd(const KnownBits &LHSKnown,
- const KnownBits &RHSKnown) {
- // Addition of two 2's complement numbers having opposite signs will never
- // overflow.
- if ((LHSKnown.isNegative() && RHSKnown.isNonNegative()) ||
- (LHSKnown.isNonNegative() && RHSKnown.isNegative()))
- return true;
-
- // If either of the values is known to be non-negative, adding them can only
- // overflow if the second is also non-negative, so we can assume that.
- // Two non-negative numbers will only overflow if there is a carry to the
- // sign bit, so we can check if even when the values are as big as possible
- // there is no overflow to the sign bit.
- if (LHSKnown.isNonNegative() || RHSKnown.isNonNegative()) {
- APInt MaxLHS = ~LHSKnown.Zero;
- MaxLHS.clearSignBit();
- APInt MaxRHS = ~RHSKnown.Zero;
- MaxRHS.clearSignBit();
- APInt Result = std::move(MaxLHS) + std::move(MaxRHS);
- return Result.isSignBitClear();
- }
-
- // If either of the values is known to be negative, adding them can only
- // overflow if the second is also negative, so we can assume that.
- // Two negative number will only overflow if there is no carry to the sign
- // bit, so we can check if even when the values are as small as possible
- // there is overflow to the sign bit.
- if (LHSKnown.isNegative() || RHSKnown.isNegative()) {
- APInt MinLHS = LHSKnown.One;
- MinLHS.clearSignBit();
- APInt MinRHS = RHSKnown.One;
- MinRHS.clearSignBit();
- APInt Result = std::move(MinLHS) + std::move(MinRHS);
- return Result.isSignBitSet();
- }
-
- // If we reached here it means that we know nothing about the sign bits.
- // In this case we can't know if there will be an overflow, since by
- // changing the sign bits any two values can be made to overflow.
- return false;
-}
-
static OverflowResult computeOverflowForSignedAdd(const Value *LHS,
const Value *RHS,
const AddOperator *Add,
@@ -4168,9 +4123,14 @@
KnownBits LHSKnown = computeKnownBits(LHS, DL, /*Depth=*/0, AC, CxtI, DT);
KnownBits RHSKnown = computeKnownBits(RHS, DL, /*Depth=*/0, AC, CxtI, DT);
-
- if (checkRippleForSignedAdd(LHSKnown, RHSKnown))
- return OverflowResult::NeverOverflows;
+ ConstantRange LHSRange =
+ ConstantRange::fromKnownBits(LHSKnown, /*signed*/ true);
+ ConstantRange RHSRange =
+ ConstantRange::fromKnownBits(RHSKnown, /*signed*/ true);
+ OverflowResult OR =
+ mapOverflowResult(LHSRange.signedAddMayOverflow(RHSRange));
+ if (OR != OverflowResult::MayOverflow)
+ return OR;
// The remaining code needs Add to be available. Early returns if not so.
if (!Add)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D59450.191047.patch
Type: text/x-patch
Size: 3231 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190317/0942c928/attachment.bin>
More information about the llvm-commits
mailing list