Skip to content

Commit 280a6b0

Browse files
committed
[ValueTracking] Avoid redundant known bits calculation in computeOverflowForSignedAdd()
We're already computing the known bits of the operands here. If the known bits of the operands can determine the sign bit of the result, we'll already catch this in signedAddMayOverflow(). The only other way (and as the comment already indicates) we'll get new information from computing known bits on the whole add, is if there's an assumption on it. As such, we change the code to only compute known bits from assumptions, instead of computing full known bits on the add (which would unnecessarily recompute the known bits of the operands as well). Differential Revision: https://reviews.llvm.org/D59473 llvm-svn: 356785
1 parent 564392d commit 280a6b0

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4146,19 +4146,21 @@ static OverflowResult computeOverflowForSignedAdd(const Value *LHS,
41464146
return OverflowResult::MayOverflow;
41474147

41484148
// If the sign of Add is the same as at least one of the operands, this add
4149-
// CANNOT overflow. This is particularly useful when the sum is
4150-
// @llvm.assume'ed non-negative rather than proved so from analyzing its
4151-
// operands.
4149+
// CANNOT overflow. If this can be determined from the known bits of the
4150+
// operands the above signedAddMayOverflow() check will have already done so.
4151+
// The only other way to improve on the known bits is from an assumption, so
4152+
// call computeKnownBitsFromAssume() directly.
41524153
bool LHSOrRHSKnownNonNegative =
41534154
(LHSKnown.isNonNegative() || RHSKnown.isNonNegative());
41544155
bool LHSOrRHSKnownNegative =
41554156
(LHSKnown.isNegative() || RHSKnown.isNegative());
41564157
if (LHSOrRHSKnownNonNegative || LHSOrRHSKnownNegative) {
4157-
KnownBits AddKnown = computeKnownBits(Add, DL, /*Depth=*/0, AC, CxtI, DT);
4158+
KnownBits AddKnown(LHSKnown.getBitWidth());
4159+
computeKnownBitsFromAssume(
4160+
Add, AddKnown, /*Depth=*/0, Query(DL, AC, CxtI, DT, true));
41584161
if ((AddKnown.isNonNegative() && LHSOrRHSKnownNonNegative) ||
4159-
(AddKnown.isNegative() && LHSOrRHSKnownNegative)) {
4162+
(AddKnown.isNegative() && LHSOrRHSKnownNegative))
41604163
return OverflowResult::NeverOverflows;
4161-
}
41624164
}
41634165

41644166
return OverflowResult::MayOverflow;

0 commit comments

Comments
 (0)