From: Florian Hahn Date: Fri, 7 Feb 2020 10:31:35 +0000 (+0000) Subject: [ValueTracking] usub(a, b) cannot overflow if a >= b. X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=14ef87bda67d53f351217c4522150e41fb5695e8;p=platform%2Fupstream%2Fllvm.git [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 --- diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp index 70d2bb6..3d3c912 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(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 951cfc3..c51ce4f 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 ;