[llvm] r369541 - [instcombine] icmp eq/ne (sub C, Y), C -> icmp eq/ne Y, 0

Philip Reames via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 21 08:51:57 PDT 2019


Author: reames
Date: Wed Aug 21 08:51:57 2019
New Revision: 369541

URL: http://llvm.org/viewvc/llvm-project?rev=369541&view=rev
Log:
[instcombine] icmp eq/ne (sub C, Y), C -> icmp eq/ne Y, 0

Noticed while looking at pr43028.  


Modified:
    llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
    llvm/trunk/test/Transforms/InstCombine/icmp-sub.ll

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp?rev=369541&r1=369540&r2=369541&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp Wed Aug 21 08:51:57 2019
@@ -2411,6 +2411,11 @@ Instruction *InstCombiner::foldICmpSubCo
   const APInt *C2;
   APInt SubResult;
 
+  // icmp eq/ne (sub C, Y), C -> icmp eq/ne Y, 0
+  if (match(X, m_APInt(C2)) && *C2 == C && Cmp.isEquality())
+    return new ICmpInst(Cmp.getPredicate(), Y,
+                        ConstantInt::get(Y->getType(), 0));
+
   // (icmp P (sub nuw|nsw C2, Y), C) -> (icmp swap(P) Y, C2-C)
   if (match(X, m_APInt(C2)) &&
       ((Cmp.isUnsigned() && Sub->hasNoUnsignedWrap()) ||

Modified: llvm/trunk/test/Transforms/InstCombine/icmp-sub.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/icmp-sub.ll?rev=369541&r1=369540&r2=369541&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/icmp-sub.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/icmp-sub.ll Wed Aug 21 08:51:57 2019
@@ -84,3 +84,43 @@ define i1 @test_negative_combined_sub_si
   %z = icmp slt i8 %y, -1
   ret i1 %z
 }
+
+define i1 @test_sub_0_Y_eq_0(i8 %y) {
+; CHECK-LABEL: @test_sub_0_Y_eq_0(
+; CHECK-NEXT:    [[Z:%.*]] = icmp eq i8 [[Y:%.*]], 0
+; CHECK-NEXT:    ret i1 [[Z]]
+;
+  %s = sub i8 0, %y
+  %z = icmp eq i8 %s, 0
+  ret i1 %z
+}
+
+define i1 @test_sub_0_Y_ne_0(i8 %y) {
+; CHECK-LABEL: @test_sub_0_Y_ne_0(
+; CHECK-NEXT:    [[Z:%.*]] = icmp ne i8 [[Y:%.*]], 0
+; CHECK-NEXT:    ret i1 [[Z]]
+;
+  %s = sub i8 0, %y
+  %z = icmp ne i8 %s, 0
+  ret i1 %z
+}
+
+define i1 @test_sub_4_Y_ne_4(i8 %y) {
+; CHECK-LABEL: @test_sub_4_Y_ne_4(
+; CHECK-NEXT:    [[Z:%.*]] = icmp ne i8 [[Y:%.*]], 0
+; CHECK-NEXT:    ret i1 [[Z]]
+;
+  %s = sub i8 4, %y
+  %z = icmp ne i8 %s, 4
+  ret i1 %z
+}
+
+define i1 @test_sub_127_Y_eq_127(i8 %y) {
+; CHECK-LABEL: @test_sub_127_Y_eq_127(
+; CHECK-NEXT:    [[Z:%.*]] = icmp eq i8 [[Y:%.*]], 0
+; CHECK-NEXT:    ret i1 [[Z]]
+;
+  %s = sub i8 127, %y
+  %z = icmp eq i8 %s, 127
+  ret i1 %z
+}




More information about the llvm-commits mailing list