[llvm] r347771 - [ValueTracking] Determine always-overflow condition for unsigned sub
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Wed Nov 28 08:37:04 PST 2018
Author: nikic
Date: Wed Nov 28 08:37:04 2018
New Revision: 347771
URL: http://llvm.org/viewvc/llvm-project?rev=347771&view=rev
Log:
[ValueTracking] Determine always-overflow condition for unsigned sub
Always-overflow was already determined for unsigned addition, but
not subtraction. This patch establishes parity.
This allows us to perform some additional simplifications for
signed saturating subtractions.
This change is part of https://reviews.llvm.org/D54534.
Modified:
llvm/trunk/lib/Analysis/ValueTracking.cpp
llvm/trunk/test/Transforms/InstCombine/saturating-add-sub.ll
Modified: llvm/trunk/lib/Analysis/ValueTracking.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/ValueTracking.cpp?rev=347771&r1=347770&r2=347771&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/ValueTracking.cpp (original)
+++ llvm/trunk/lib/Analysis/ValueTracking.cpp Wed Nov 28 08:37:04 2018
@@ -4001,13 +4001,11 @@ OverflowResult llvm::computeOverflowForU
if (LHSKnown.isNegative() && RHSKnown.isNegative()) {
// The sign bit is set in both cases: this MUST overflow.
- // Create a simple add instruction, and insert it into the struct.
return OverflowResult::AlwaysOverflows;
}
if (LHSKnown.isNonNegative() && RHSKnown.isNonNegative()) {
// The sign bit is clear in both cases: this CANNOT overflow.
- // Create a simple add instruction, and insert it into the struct.
return OverflowResult::NeverOverflows;
}
}
@@ -4124,11 +4122,18 @@ OverflowResult llvm::computeOverflowForU
AssumptionCache *AC,
const Instruction *CxtI,
const DominatorTree *DT) {
- // If the LHS is negative and the RHS is non-negative, no unsigned wrap.
KnownBits LHSKnown = computeKnownBits(LHS, DL, /*Depth=*/0, AC, CxtI, DT);
- KnownBits RHSKnown = computeKnownBits(RHS, DL, /*Depth=*/0, AC, CxtI, DT);
- if (LHSKnown.isNegative() && RHSKnown.isNonNegative())
- return OverflowResult::NeverOverflows;
+ 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;
+ }
return OverflowResult::MayOverflow;
}
Modified: llvm/trunk/test/Transforms/InstCombine/saturating-add-sub.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/saturating-add-sub.ll?rev=347771&r1=347770&r2=347771&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/saturating-add-sub.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/saturating-add-sub.ll Wed Nov 28 08:37:04 2018
@@ -529,9 +529,7 @@ define i8 @test_scalar_ssub_overflow(i8
; nneg usub neg always overflows.
define i8 @test_scalar_usub_nneg_neg(i8 %a) {
; CHECK-LABEL: @test_scalar_usub_nneg_neg(
-; CHECK-NEXT: [[A_NNEG:%.*]] = and i8 [[A:%.*]], 127
-; CHECK-NEXT: [[R:%.*]] = call i8 @llvm.usub.sat.i8(i8 [[A_NNEG]], i8 -10)
-; CHECK-NEXT: ret i8 [[R]]
+; CHECK-NEXT: ret i8 0
;
%a_nneg = and i8 %a, 127
%r = call i8 @llvm.usub.sat.i8(i8 %a_nneg, i8 -10)
@@ -540,9 +538,7 @@ define i8 @test_scalar_usub_nneg_neg(i8
define <2 x i8> @test_vector_usub_nneg_neg(<2 x i8> %a) {
; CHECK-LABEL: @test_vector_usub_nneg_neg(
-; CHECK-NEXT: [[A_NNEG:%.*]] = and <2 x i8> [[A:%.*]], <i8 127, i8 127>
-; CHECK-NEXT: [[R:%.*]] = call <2 x i8> @llvm.usub.sat.v2i8(<2 x i8> [[A_NNEG]], <2 x i8> <i8 -10, i8 -20>)
-; CHECK-NEXT: ret <2 x i8> [[R]]
+; CHECK-NEXT: ret <2 x i8> zeroinitializer
;
%a_nneg = and <2 x i8> %a, <i8 127, i8 127>
%r = call <2 x i8> @llvm.usub.sat.v2i8(<2 x i8> %a_nneg, <2 x i8> <i8 -10, i8 -20>)
More information about the llvm-commits
mailing list