[llvm] 14ef87b - [ValueTracking] usub(a, b) cannot overflow if a >= b.
Florian Hahn via llvm-commits
llvm-commits at lists.llvm.org
Fri Feb 7 02:43:25 PST 2020
Author: Florian Hahn
Date: 2020-02-07T10:41:18Z
New Revision: 14ef87bda67d53f351217c4522150e41fb5695e8
URL: https://github.com/llvm/llvm-project/commit/14ef87bda67d53f351217c4522150e41fb5695e8
DIFF: https://github.com/llvm/llvm-project/commit/14ef87bda67d53f351217c4522150e41fb5695e8.diff
LOG: [ValueTracking] usub(a, b) cannot overflow if a >= b.
If we know that a >= b (unsigned), usub.with.overflow(a, b) cannot
overflow. Similarly, if b > a, the same expression overflows.
Reviewers: nikic, RKSimon, lebedev.ri, spatel
Reviewed By: nikic, Gerolf
Differential Revision: https://reviews.llvm.org/D74066
Added:
Modified:
llvm/lib/Analysis/ValueTracking.cpp
llvm/test/Transforms/InstCombine/usub-overflow-known-by-implied-cond.ll
Removed:
################################################################################
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 70d2bb696600..3d3c91242155 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -4439,6 +4439,16 @@ OverflowResult llvm::computeOverflowForUnsignedSub(const Value *LHS,
AssumptionCache *AC,
const Instruction *CxtI,
const DominatorTree *DT) {
+ // Checking for conditions implied by dominating conditions may be expensive.
+ // Limit it to usub_with_overflow calls for now.
+ if (match(CxtI,
+ m_Intrinsic<Intrinsic::usub_with_overflow>(m_Value(), m_Value())))
+ if (auto C =
+ isImpliedByDomCondition(CmpInst::ICMP_UGE, LHS, RHS, CxtI, DL)) {
+ if (*C)
+ return OverflowResult::NeverOverflows;
+ return OverflowResult::AlwaysOverflowsLow;
+ }
ConstantRange LHSRange = computeConstantRangeIncludingKnownBits(
LHS, /*ForSigned=*/false, DL, /*Depth=*/0, AC, CxtI, DT);
ConstantRange RHSRange = computeConstantRangeIncludingKnownBits(
diff --git a/llvm/test/Transforms/InstCombine/usub-overflow-known-by-implied-cond.ll b/llvm/test/Transforms/InstCombine/usub-overflow-known-by-implied-cond.ll
index 951cfc364490..c51ce4fdce5f 100644
--- a/llvm/test/Transforms/InstCombine/usub-overflow-known-by-implied-cond.ll
+++ b/llvm/test/Transforms/InstCombine/usub-overflow-known-by-implied-cond.ll
@@ -9,12 +9,9 @@ define i32 @test1(i32 %a, i32 %b) {
; CHECK-NEXT: [[COND:%.*]] = icmp ult i32 [[A:%.*]], [[B:%.*]]
; CHECK-NEXT: br i1 [[COND]], label [[BB3:%.*]], label [[BB1:%.*]]
; CHECK: bb1:
-; CHECK-NEXT: [[SUB1:%.*]] = call { i32, i1 } @llvm.usub.with.overflow.i32(i32 [[A]], i32 [[B]])
-; CHECK-NEXT: [[C1:%.*]] = extractvalue { i32, i1 } [[SUB1]], 1
-; CHECK-NEXT: br i1 [[C1]], label [[BB2:%.*]], label [[BB3]]
+; CHECK-NEXT: br i1 false, label [[BB2:%.*]], label [[BB3]]
; CHECK: bb2:
-; CHECK-NEXT: [[R1:%.*]] = extractvalue { i32, i1 } [[SUB1]], 0
-; CHECK-NEXT: ret i32 [[R1]]
+; CHECK-NEXT: ret i32 undef
; CHECK: bb3:
; CHECK-NEXT: ret i32 0
;
@@ -39,12 +36,10 @@ define i32 @test2(i32 %a, i32 %b) {
; CHECK-NEXT: [[COND:%.*]] = icmp ult i32 [[A:%.*]], [[B:%.*]]
; CHECK-NEXT: br i1 [[COND]], label [[BB3:%.*]], label [[BB1:%.*]]
; CHECK: bb1:
-; CHECK-NEXT: [[SUB1:%.*]] = call { i32, i1 } @llvm.usub.with.overflow.i32(i32 [[A]], i32 [[B]])
-; CHECK-NEXT: [[C1:%.*]] = extractvalue { i32, i1 } [[SUB1]], 1
-; CHECK-NEXT: br i1 [[C1]], label [[BB3]], label [[BB2:%.*]]
+; CHECK-NEXT: br i1 false, label [[BB3]], label [[BB2:%.*]]
; CHECK: bb2:
-; CHECK-NEXT: [[R1:%.*]] = extractvalue { i32, i1 } [[SUB1]], 0
-; CHECK-NEXT: ret i32 [[R1]]
+; CHECK-NEXT: [[SUB1:%.*]] = sub nuw i32 [[A]], [[B]]
+; CHECK-NEXT: ret i32 [[SUB1]]
; CHECK: bb3:
; CHECK-NEXT: ret i32 0
;
@@ -70,12 +65,9 @@ define i32 @test3(i32 %a, i32 %b) {
; CHECK-NEXT: [[COND:%.*]] = icmp ugt i32 [[A:%.*]], [[B:%.*]]
; CHECK-NEXT: br i1 [[COND]], label [[BB1:%.*]], label [[BB3:%.*]]
; CHECK: bb1:
-; CHECK-NEXT: [[SUB1:%.*]] = call { i32, i1 } @llvm.usub.with.overflow.i32(i32 [[A]], i32 [[B]])
-; CHECK-NEXT: [[C1:%.*]] = extractvalue { i32, i1 } [[SUB1]], 1
-; CHECK-NEXT: br i1 [[C1]], label [[BB2:%.*]], label [[BB3]]
+; CHECK-NEXT: br i1 false, label [[BB2:%.*]], label [[BB3]]
; CHECK: bb2:
-; CHECK-NEXT: [[R1:%.*]] = extractvalue { i32, i1 } [[SUB1]], 0
-; CHECK-NEXT: ret i32 [[R1]]
+; CHECK-NEXT: ret i32 undef
; CHECK: bb3:
; CHECK-NEXT: ret i32 0
;
@@ -100,12 +92,10 @@ define i32 @test4(i32 %a, i32 %b) {
; CHECK-NEXT: [[COND:%.*]] = icmp ugt i32 [[A:%.*]], [[B:%.*]]
; CHECK-NEXT: br i1 [[COND]], label [[BB1:%.*]], label [[BB3:%.*]]
; CHECK: bb1:
-; CHECK-NEXT: [[SUB1:%.*]] = call { i32, i1 } @llvm.usub.with.overflow.i32(i32 [[A]], i32 [[B]])
-; CHECK-NEXT: [[C1:%.*]] = extractvalue { i32, i1 } [[SUB1]], 1
-; CHECK-NEXT: br i1 [[C1]], label [[BB3]], label [[BB2:%.*]]
+; CHECK-NEXT: br i1 false, label [[BB3]], label [[BB2:%.*]]
; CHECK: bb2:
-; CHECK-NEXT: [[R1:%.*]] = extractvalue { i32, i1 } [[SUB1]], 0
-; CHECK-NEXT: ret i32 [[R1]]
+; CHECK-NEXT: [[SUB1:%.*]] = sub nuw i32 [[A]], [[B]]
+; CHECK-NEXT: ret i32 [[SUB1]]
; CHECK: bb3:
; CHECK-NEXT: ret i32 0
;
@@ -131,12 +121,10 @@ define i32 @test5(i32 %a, i32 %b) {
; CHECK-NEXT: [[COND:%.*]] = icmp eq i32 [[A:%.*]], [[B:%.*]]
; CHECK-NEXT: br i1 [[COND]], label [[BB1:%.*]], label [[BB3:%.*]]
; CHECK: bb1:
-; CHECK-NEXT: [[SUB1:%.*]] = call { i32, i1 } @llvm.usub.with.overflow.i32(i32 [[A]], i32 [[B]])
-; CHECK-NEXT: [[C1:%.*]] = extractvalue { i32, i1 } [[SUB1]], 1
-; CHECK-NEXT: br i1 [[C1]], label [[BB3]], label [[BB2:%.*]]
+; CHECK-NEXT: br i1 false, label [[BB3]], label [[BB2:%.*]]
; CHECK: bb2:
-; CHECK-NEXT: [[R1:%.*]] = extractvalue { i32, i1 } [[SUB1]], 0
-; CHECK-NEXT: ret i32 [[R1]]
+; CHECK-NEXT: [[SUB1:%.*]] = sub nuw i32 [[A]], [[B]]
+; CHECK-NEXT: ret i32 [[SUB1]]
; CHECK: bb3:
; CHECK-NEXT: ret i32 0
;
@@ -161,12 +149,9 @@ define i32 @test6(i32 %a, i32 %b) {
; CHECK-NEXT: [[COND:%.*]] = icmp ult i32 [[A:%.*]], [[B:%.*]]
; CHECK-NEXT: br i1 [[COND]], label [[BB1:%.*]], label [[BB3:%.*]]
; CHECK: bb1:
-; CHECK-NEXT: [[SUB1:%.*]] = call { i32, i1 } @llvm.usub.with.overflow.i32(i32 [[A]], i32 [[B]])
-; CHECK-NEXT: [[C1:%.*]] = extractvalue { i32, i1 } [[SUB1]], 1
-; CHECK-NEXT: br i1 [[C1]], label [[BB3]], label [[BB2:%.*]]
+; CHECK-NEXT: br i1 true, label [[BB3]], label [[BB2:%.*]]
; CHECK: bb2:
-; CHECK-NEXT: [[R1:%.*]] = extractvalue { i32, i1 } [[SUB1]], 0
-; CHECK-NEXT: ret i32 [[R1]]
+; CHECK-NEXT: ret i32 undef
; CHECK: bb3:
; CHECK-NEXT: ret i32 0
;
@@ -252,12 +237,10 @@ define i32 @test9(i32 %a, i32 %b, i1 %cond2) {
; CHECK-NEXT: [[AND:%.*]] = and i1 [[COND]], [[COND2:%.*]]
; CHECK-NEXT: br i1 [[AND]], label [[BB1:%.*]], label [[BB3:%.*]]
; CHECK: bb1:
-; CHECK-NEXT: [[SUB1:%.*]] = call { i32, i1 } @llvm.usub.with.overflow.i32(i32 [[A]], i32 [[B]])
-; CHECK-NEXT: [[C1:%.*]] = extractvalue { i32, i1 } [[SUB1]], 1
-; CHECK-NEXT: br i1 [[C1]], label [[BB3]], label [[BB2:%.*]]
+; CHECK-NEXT: br i1 false, label [[BB3]], label [[BB2:%.*]]
; CHECK: bb2:
-; CHECK-NEXT: [[R1:%.*]] = extractvalue { i32, i1 } [[SUB1]], 0
-; CHECK-NEXT: ret i32 [[R1]]
+; CHECK-NEXT: [[SUB1:%.*]] = sub nuw i32 [[A]], [[B]]
+; CHECK-NEXT: ret i32 [[SUB1]]
; CHECK: bb3:
; CHECK-NEXT: ret i32 0
;
More information about the llvm-commits
mailing list