[InstCombine] (-NSW x) ==/!= x --> x ==/!= 0 (PR39480)
authorRoman Lebedev <lebedev.ri@gmail.com>
Thu, 6 Aug 2020 08:02:29 +0000 (11:02 +0300)
committerRoman Lebedev <lebedev.ri@gmail.com>
Thu, 6 Aug 2020 08:50:34 +0000 (11:50 +0300)
Name: (-x) == x  -->  x == 0
%neg_x = sub nsw i8 0, %x ; %x must not be INT_MIN
%r = icmp eq i8 %neg_x, %x
  =>
%r = icmp eq i8 %x, 0

Name: (-x) != x  -->  x != 0
%neg_x = sub nsw i8 0, %x ; %x must not be INT_MIN
%r = icmp ne i8 %neg_x, %x
  =>
%r = icmp ne i8 %x, 0

https://rise4fun.com/Alive/4slH

https://bugs.llvm.org/show_bug.cgi?id=39480

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
llvm/test/Transforms/InstCombine/cmp-x-vs-neg-x.ll

index 0032b65..ce75a36 100644 (file)
@@ -3733,6 +3733,12 @@ Instruction *foldICmpXNegX(ICmpInst &I) {
     NewRHS = Constant::getNullValue(X->getType());
     break;
 
+  case ICmpInst::ICMP_EQ:
+  case ICmpInst::ICMP_NE:
+    NewPred = Pred;
+    NewRHS = Constant::getNullValue(X->getType());
+    break;
+
   default:
     return nullptr;
   }
index 485e329..a2e39f1 100644 (file)
@@ -118,8 +118,7 @@ define i1 @t7(i8 %x) {
 
 define i1 @t8(i8 %x) {
 ; CHECK-LABEL: @t8(
-; CHECK-NEXT:    [[NEG_X:%.*]] = sub nsw i8 0, [[X:%.*]]
-; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i8 [[NEG_X]], [[X]]
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i8 [[X:%.*]], 0
 ; CHECK-NEXT:    ret i1 [[CMP]]
 ;
   %neg_x = sub nsw i8 0, %x
@@ -129,8 +128,7 @@ define i1 @t8(i8 %x) {
 
 define i1 @t9(i8 %x) {
 ; CHECK-LABEL: @t9(
-; CHECK-NEXT:    [[NEG_X:%.*]] = sub nsw i8 0, [[X:%.*]]
-; CHECK-NEXT:    [[CMP:%.*]] = icmp ne i8 [[NEG_X]], [[X]]
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ne i8 [[X:%.*]], 0
 ; CHECK-NEXT:    ret i1 [[CMP]]
 ;
   %neg_x = sub nsw i8 0, %x