[PATCH] D69244: [InstCombine] Extra combine for uadd_sat

Dave Green via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 21 02:24:23 PDT 2019


dmgreen created this revision.
dmgreen added reviewers: nikic, lebedev.ri, spatel.
Herald added a subscriber: hiraditya.
Herald added a project: LLVM.

This is an extra fold for a canonical for of uadd_sat, as shown in D68651 <https://reviews.llvm.org/D68651>. Signed patterns for these are a little more involved, but unsigned is a simple enough extension to what was already present.

  Name: uadd_sat_canon             
    %3 = add i8 %0, %1
    %4 = icmp ult i8 %3, %0
    %5 = select i1 %4, i8 -1, i8 %3
  =>
    %5 = uadd_sat %1, %0


https://reviews.llvm.org/D69244

Files:
  llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
  llvm/test/Transforms/InstCombine/saturating-add-sub.ll


Index: llvm/test/Transforms/InstCombine/saturating-add-sub.ll
===================================================================
--- llvm/test/Transforms/InstCombine/saturating-add-sub.ll
+++ llvm/test/Transforms/InstCombine/saturating-add-sub.ll
@@ -1486,10 +1486,8 @@
 
 define i32 @uadd_sat_canon(i32 %x, i32 %y) {
 ; CHECK-LABEL: @uadd_sat_canon(
-; CHECK-NEXT:    [[A:%.*]] = add i32 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT:    [[C:%.*]] = icmp ult i32 [[A]], [[X]]
-; CHECK-NEXT:    [[R:%.*]] = select i1 [[C]], i32 -1, i32 [[A]]
-; CHECK-NEXT:    ret i32 [[R]]
+; CHECK-NEXT:    [[TMP1:%.*]] = call i32 @llvm.uadd.sat.i32(i32 [[X:%.*]], i32 [[Y:%.*]])
+; CHECK-NEXT:    ret i32 [[TMP1]]
 ;
   %a = add i32 %x, %y
   %c = icmp ult i32 %a, %x
@@ -1499,10 +1497,8 @@
 
 define i32 @uadd_sat_canon_y(i32 %x, i32 %y) {
 ; CHECK-LABEL: @uadd_sat_canon_y(
-; CHECK-NEXT:    [[A:%.*]] = add i32 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT:    [[C:%.*]] = icmp ult i32 [[A]], [[Y]]
-; CHECK-NEXT:    [[R:%.*]] = select i1 [[C]], i32 -1, i32 [[A]]
-; CHECK-NEXT:    ret i32 [[R]]
+; CHECK-NEXT:    [[TMP1:%.*]] = call i32 @llvm.uadd.sat.i32(i32 [[X:%.*]], i32 [[Y:%.*]])
+; CHECK-NEXT:    ret i32 [[TMP1]]
 ;
   %a = add i32 %x, %y
   %c = icmp ult i32 %a, %y
Index: llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -781,6 +781,14 @@
     return Builder.CreateBinaryIntrinsic(
         Intrinsic::uadd_sat, BO->getOperand(0), BO->getOperand(1));
   }
+  // With an overflowing add
+  if (match(Cmp0, m_c_Add(m_Value(X), m_Value(Y))) &&
+      (X == Cmp1 || Y == Cmp1) &&
+      match(FVal, m_c_Add(m_Specific(X), m_Specific(Y)))) {
+    // ((X + Y) u< X) ? -1 : (X + Y) --> uadd.sat(X, Y)
+    // ((X + Y) u< Y) ? -1 : (X + Y) --> uadd.sat(X, Y)
+    return Builder.CreateBinaryIntrinsic(Intrinsic::uadd_sat, X, Y);
+  }
 
   return nullptr;
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D69244.225839.patch
Type: text/x-patch
Size: 2021 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191021/803ae197/attachment.bin>


More information about the llvm-commits mailing list