[llvm] a47b56f - [Instsimplfy] X == Y ? 0 : X - Y --> X - Y

Jun Zhang via llvm-commits llvm-commits at lists.llvm.org
Tue May 16 04:16:38 PDT 2023


Author: Jun Zhang
Date: 2023-05-16T19:15:47+08:00
New Revision: a47b56f4ef8355e7c2abf1f10ff2c317d9d6e54a

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

LOG: [Instsimplfy] X == Y ? 0 : X - Y --> X - Y

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

Depends on D150377

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

Differential Revision: https://reviews.llvm.org/D150378

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index fd0ba395b2937..50e39977af53d 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -4256,6 +4256,13 @@ static Value *simplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp,
           NewOps[0] == NewOps[1])
         return NewOps[0];
 
+      // x - x -> 0. This is non-refining, because x is non-poison by assumption
+      // and this case never wraps, so nowrap flags can be ignored.
+      if (Opcode == Instruction::Sub && NewOps[0] == NewOps[1]) {
+        assert(NewOps[0] == RepOp && "Precondition for non-poison assumption");
+        return Constant::getNullValue(I->getType());
+      }
+
       // If we are substituting an absorber constant into a binop and extra
       // poison can't leak if we remove the select -- because both operands of
       // the binop are based on the same value -- then it may be safe to replace

diff  --git a/llvm/test/Transforms/InstSimplify/select.ll b/llvm/test/Transforms/InstSimplify/select.ll
index 29c568d71fbec..a77e8df1b8b47 100644
--- a/llvm/test/Transforms/InstSimplify/select.ll
+++ b/llvm/test/Transforms/InstSimplify/select.ll
@@ -1441,10 +1441,8 @@ exit:
 
 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 i8 @select_sub_cmp(i8 %0, i8 %1) {
 
 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 <2 x i8> @select_sub_cmp_vec(<2 x i8> %0, <2 x i8> %1) {
 
 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 i8 @select_sub_cmp_swap(i8 %0, i8 %1) {
 
 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


        


More information about the llvm-commits mailing list