[PATCH] D150378: [Instsimplfy] X == Y ? 0 : X - Y --> X - Y

Jun Zhang via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu May 11 08:47:21 PDT 2023


junaire created this revision.
Herald added a subscriber: hiraditya.
Herald added a project: All.
junaire requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Alive2: https://alive2.llvm.org/ce/z/rPN1GB
Fixes: https://github.com/llvm/llvm-project/issues/62238

Depends on D150377 <https://reviews.llvm.org/D150377>

Signed-off-by: Jun Zhang <jun at junz.org>


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D150378

Files:
  llvm/lib/Analysis/InstructionSimplify.cpp
  llvm/test/Transforms/InstSimplify/select.ll


Index: llvm/test/Transforms/InstSimplify/select.ll
===================================================================
--- llvm/test/Transforms/InstSimplify/select.ll
+++ llvm/test/Transforms/InstSimplify/select.ll
@@ -1441,10 +1441,8 @@
 
 define i8 @select_sub_cmp(i8 %0, i8 %1) {
 ; CHECK-LABEL: @select_sub_cmp(
-; CHECK-NEXT:    [[TMP3:%.*]] = icmp eq i8 [[TMP1:%.*]], [[TMP0:%.*]]
-; CHECK-NEXT:    [[TMP4:%.*]] = sub nsw i8 [[TMP1]], [[TMP0]]
-; CHECK-NEXT:    [[TMP5:%.*]] = select i1 [[TMP3]], i8 0, i8 [[TMP4]]
-; CHECK-NEXT:    ret i8 [[TMP5]]
+; CHECK-NEXT:    [[TMP3:%.*]] = sub nsw i8 [[TMP1:%.*]], [[TMP0:%.*]]
+; CHECK-NEXT:    ret i8 [[TMP3]]
 ;
   %3 = icmp eq i8 %1, %0
   %4 = sub nsw i8 %1, %0
@@ -1454,10 +1452,8 @@
 
 define <2 x i8> @select_sub_cmp_vec(<2 x i8> %0, <2 x i8> %1) {
 ; CHECK-LABEL: @select_sub_cmp_vec(
-; CHECK-NEXT:    [[TMP3:%.*]] = icmp eq <2 x i8> [[TMP1:%.*]], [[TMP0:%.*]]
-; CHECK-NEXT:    [[TMP4:%.*]] = sub nsw <2 x i8> [[TMP1]], [[TMP0]]
-; CHECK-NEXT:    [[TMP5:%.*]] = select <2 x i1> [[TMP3]], <2 x i8> zeroinitializer, <2 x i8> [[TMP4]]
-; CHECK-NEXT:    ret <2 x i8> [[TMP5]]
+; CHECK-NEXT:    [[TMP3:%.*]] = sub nsw <2 x i8> [[TMP1:%.*]], [[TMP0:%.*]]
+; CHECK-NEXT:    ret <2 x i8> [[TMP3]]
 ;
   %3 = icmp eq <2 x i8> %1, %0
   %4 = sub nsw <2 x i8> %1, %0
@@ -1467,10 +1463,8 @@
 
 define i8 @select_sub_cmp_swap(i8 %0, i8 %1) {
 ; CHECK-LABEL: @select_sub_cmp_swap(
-; CHECK-NEXT:    [[TMP3:%.*]] = icmp eq i8 [[TMP1:%.*]], [[TMP0:%.*]]
-; CHECK-NEXT:    [[TMP4:%.*]] = sub nsw i8 [[TMP0]], [[TMP1]]
-; CHECK-NEXT:    [[TMP5:%.*]] = select i1 [[TMP3]], i8 0, i8 [[TMP4]]
-; CHECK-NEXT:    ret i8 [[TMP5]]
+; CHECK-NEXT:    [[TMP3:%.*]] = sub nsw i8 [[TMP0:%.*]], [[TMP1:%.*]]
+; CHECK-NEXT:    ret i8 [[TMP3]]
 ;
   %3 = icmp eq i8 %1, %0
   %4 = sub nsw i8 %0, %1
@@ -1480,10 +1474,8 @@
 
 define <2 x i8> @select_sub_cmp_vec_swap(<2 x i8> %0, <2 x i8> %1) {
 ; CHECK-LABEL: @select_sub_cmp_vec_swap(
-; CHECK-NEXT:    [[TMP3:%.*]] = icmp eq <2 x i8> [[TMP1:%.*]], [[TMP0:%.*]]
-; CHECK-NEXT:    [[TMP4:%.*]] = sub nsw <2 x i8> [[TMP0]], [[TMP1]]
-; CHECK-NEXT:    [[TMP5:%.*]] = select <2 x i1> [[TMP3]], <2 x i8> zeroinitializer, <2 x i8> [[TMP4]]
-; CHECK-NEXT:    ret <2 x i8> [[TMP5]]
+; CHECK-NEXT:    [[TMP3:%.*]] = sub nsw <2 x i8> [[TMP0:%.*]], [[TMP1:%.*]]
+; CHECK-NEXT:    ret <2 x i8> [[TMP3]]
 ;
   %3 = icmp eq <2 x i8> %1, %0
   %4 = sub nsw <2 x i8> %0, %1
Index: llvm/lib/Analysis/InstructionSimplify.cpp
===================================================================
--- llvm/lib/Analysis/InstructionSimplify.cpp
+++ llvm/lib/Analysis/InstructionSimplify.cpp
@@ -4574,6 +4574,14 @@
                                               MaxRecurse))
         return V;
     }
+
+    // select((X == Y) ? 0 : X - Y) --> X - Y
+    if (match(TrueVal, m_Zero()) &&
+        match(FalseVal, m_Sub(m_Specific(CmpLHS), m_Specific(CmpRHS))))
+      return FalseVal;
+    if (match(TrueVal, m_Zero()) &&
+        match(FalseVal, m_Sub(m_Specific(CmpRHS), m_Specific(CmpLHS))))
+      return FalseVal;
   }
 
   return nullptr;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D150378.521333.patch
Type: text/x-patch
Size: 3096 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230511/110c027c/attachment.bin>


More information about the llvm-commits mailing list