summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Analysis
diff options
context:
space:
mode:
authorNikita Popov <nikita.ppv@gmail.com>2019-02-28 18:04:20 +0000
committerNikita Popov <nikita.ppv@gmail.com>2019-02-28 18:04:20 +0000
commitaf2b0bef4361e8308697022f55209199190f08a3 (patch)
tree66f859ef2698c06846dbb9d0a788008934e1ba60 /llvm/lib/Analysis
parentab10947b345e7b97c194dc33a36cce5043e7cd5f (diff)
downloadbcm5719-llvm-af2b0bef4361e8308697022f55209199190f08a3.tar.gz
bcm5719-llvm-af2b0bef4361e8308697022f55209199190f08a3.zip
[ValueTracking] More accurate unsigned sub overflow detection
Second part of D58593. Compute precise overflow conditions based on all known bits, rather than just the sign bits. Unsigned a - b overflows iff a < b, and we can determine whether this always/never happens based on the minimal and maximal values achievable for a and b subject to the known bits constraint. llvm-svn: 355109
Diffstat (limited to 'llvm/lib/Analysis')
-rw-r--r--llvm/lib/Analysis/ValueTracking.cpp20
1 files changed, 9 insertions, 11 deletions
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index f6e3886f652..8b3d16a5125 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -4165,18 +4165,16 @@ OverflowResult llvm::computeOverflowForUnsignedSub(const Value *LHS,
const Instruction *CxtI,
const DominatorTree *DT) {
KnownBits LHSKnown = computeKnownBits(LHS, DL, /*Depth=*/0, AC, CxtI, DT);
- if (LHSKnown.isNonNegative() || LHSKnown.isNegative()) {
- KnownBits RHSKnown = computeKnownBits(RHS, DL, /*Depth=*/0, AC, CxtI, DT);
-
- // If the LHS is negative and the RHS is non-negative, no unsigned wrap.
- if (LHSKnown.isNegative() && RHSKnown.isNonNegative())
- return OverflowResult::NeverOverflows;
-
- // If the LHS is non-negative and the RHS negative, we always wrap.
- if (LHSKnown.isNonNegative() && RHSKnown.isNegative())
- return OverflowResult::AlwaysOverflows;
- }
+ KnownBits RHSKnown = computeKnownBits(RHS, DL, /*Depth=*/0, AC, CxtI, DT);
+ // a - b overflows iff a < b. Determine whether this is never/always true
+ // based on the min/max values achievable under the known bits constraint.
+ APInt MinLHS = LHSKnown.One, MaxLHS = ~LHSKnown.Zero;
+ APInt MinRHS = RHSKnown.One, MaxRHS = ~RHSKnown.Zero;
+ if (MinLHS.uge(MaxRHS))
+ return OverflowResult::NeverOverflows;
+ if (MaxLHS.ult(MinRHS))
+ return OverflowResult::AlwaysOverflows;
return OverflowResult::MayOverflow;
}
OpenPOWER on IntegriCloud