[PATCH] D60483: [InstCombine] Handle usubo always overflow

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 9 13:07:44 PDT 2019


nikic created this revision.
nikic added reviewers: lebedev.ri, spatel.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.

Check AlwaysOverflow condition for usubo. The implementation is the same as the existing handling for uaddo and umulo. Handling for saddo and ssubo will follow (smulo doesn't have the necessary ValueTracking support).


Repository:
  rL LLVM

https://reviews.llvm.org/D60483

Files:
  llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
  llvm/test/Transforms/InstCombine/with_overflow.ll


Index: llvm/test/Transforms/InstCombine/with_overflow.ll
===================================================================
--- llvm/test/Transforms/InstCombine/with_overflow.ll
+++ llvm/test/Transforms/InstCombine/with_overflow.ll
@@ -544,8 +544,9 @@
 define { i8, i1 } @usub_always_overflow(i8 %x) nounwind {
 ; CHECK-LABEL: @usub_always_overflow(
 ; CHECK-NEXT:    [[Y:%.*]] = or i8 [[X:%.*]], 64
-; CHECK-NEXT:    [[A:%.*]] = call { i8, i1 } @llvm.usub.with.overflow.i8(i8 63, i8 [[Y]])
-; CHECK-NEXT:    ret { i8, i1 } [[A]]
+; CHECK-NEXT:    [[A:%.*]] = sub nsw i8 63, [[Y]]
+; CHECK-NEXT:    [[TMP1:%.*]] = insertvalue { i8, i1 } { i8 undef, i1 true }, i8 [[A]], 0
+; CHECK-NEXT:    ret { i8, i1 } [[TMP1]]
 ;
   %y = or i8 %x, 64
   %a = call { i8, i1 } @llvm.usub.with.overflow.i8(i8 63, i8 %y)
Index: llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -3990,9 +3990,13 @@
       return SetResult(LHS, Builder.getFalse(), false);
 
     if (OCF == OCF_UNSIGNED_SUB) {
-      if (willNotOverflowUnsignedSub(LHS, RHS, OrigI))
+      OverflowResult OR = computeOverflowForUnsignedSub(LHS, RHS, &OrigI);
+      if (OR == OverflowResult::NeverOverflows)
         return SetResult(Builder.CreateNUWSub(LHS, RHS), Builder.getFalse(),
                          true);
+
+      if (OR == OverflowResult::AlwaysOverflows)
+        return SetResult(Builder.CreateSub(LHS, RHS), Builder.getTrue(), true);
     } else {
       if (willNotOverflowSignedSub(LHS, RHS, OrigI))
         return SetResult(Builder.CreateNSWSub(LHS, RHS), Builder.getFalse(),


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D60483.194384.patch
Type: text/x-patch
Size: 1745 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190409/6bcbd290/attachment.bin>


More information about the llvm-commits mailing list