[llvm] 926e731 - [InstCombine] fold usub.with.overflow to icmp when there's no use of the math value

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 9 10:13:53 PDT 2022


Author: Sanjay Patel
Date: 2022-08-09T13:13:48-04:00
New Revision: 926e7312b2f20f2f7b0a3d5ddbd29da5625507f3

URL: https://github.com/llvm/llvm-project/commit/926e7312b2f20f2f7b0a3d5ddbd29da5625507f3
DIFF: https://github.com/llvm/llvm-project/commit/926e7312b2f20f2f7b0a3d5ddbd29da5625507f3.diff

LOG: [InstCombine] fold usub.with.overflow to icmp when there's no use of the math value

https://alive2.llvm.org/ce/z/UE48FH

This is part of solving issue #56926.

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
    llvm/test/Transforms/InstCombine/usubo.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index dc1e904c3f57e..dd03f98c5be78 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -3281,6 +3281,10 @@ InstCombinerImpl::foldExtractOfOverflowIntrinsic(ExtractValueInst &EV) {
 
   assert(*EV.idx_begin() == 1 && "Unexpected extract index for overflow inst");
 
+  // (usub LHS, RHS) overflows when LHS is unsigned-less-than RHS.
+  if (OvID == Intrinsic::usub_with_overflow)
+    return new ICmpInst(ICmpInst::ICMP_ULT, WO->getLHS(), WO->getRHS());
+
   // If only the overflow result is used, and the right hand side is a
   // constant (or constant splat), we can remove the intrinsic by directly
   // checking for overflow.

diff  --git a/llvm/test/Transforms/InstCombine/usubo.ll b/llvm/test/Transforms/InstCombine/usubo.ll
index dc21a4f550644..d45c30a09ad38 100644
--- a/llvm/test/Transforms/InstCombine/usubo.ll
+++ b/llvm/test/Transforms/InstCombine/usubo.ll
@@ -6,8 +6,7 @@ declare { i8, i1 } @llvm.usub.with.overflow.i8(i8, i8)
 
 define i1 @test_generic(i64 %a, i64 %b) {
 ; CHECK-LABEL: @test_generic(
-; CHECK-NEXT:    [[RES:%.*]] = tail call { i64, i1 } @llvm.usub.with.overflow.i64(i64 [[A:%.*]], i64 [[B:%.*]])
-; CHECK-NEXT:    [[OVERFLOW:%.*]] = extractvalue { i64, i1 } [[RES]], 1
+; CHECK-NEXT:    [[OVERFLOW:%.*]] = icmp ult i64 [[A:%.*]], [[B:%.*]]
 ; CHECK-NEXT:    ret i1 [[OVERFLOW]]
 ;
   %res = tail call { i64, i1 } @llvm.usub.with.overflow.i64(i64 %a, i64 %b)


        


More information about the llvm-commits mailing list