[InstCombine] (-NSW x) s<= x --> x s>= 0 (PR39480)
authorRoman Lebedev <lebedev.ri@gmail.com>
Thu, 6 Aug 2020 08:14:28 +0000 (11:14 +0300)
committerRoman Lebedev <lebedev.ri@gmail.com>
Thu, 6 Aug 2020 08:50:35 +0000 (11:50 +0300)
Name: (-x) s<= x  -->  x >= 0
%neg_x = sub nsw i8 0, %x ; %x must not be INT_MIN
%r = icmp sle i8 %neg_x, %x
  =>
%r = icmp sge i8 %x, 0

https://rise4fun.com/Alive/91k

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 0a8ed3c..a2c1816 100644 (file)
@@ -3743,6 +3743,11 @@ Instruction *foldICmpXNegX(ICmpInst &I) {
     NewRHS = Constant::getNullValue(X->getType());
     break;
 
+  case ICmpInst::ICMP_SLE:
+    NewPred = ICmpInst::ICMP_SGE;
+    NewRHS = Constant::getNullValue(X->getType());
+    break;
+
   case ICmpInst::ICMP_EQ:
   case ICmpInst::ICMP_NE:
     NewPred = Pred;
index 9a32a24..594e84b 100644 (file)
@@ -61,8 +61,7 @@ define i1 @t2(i8 %x) {
 
 define i1 @t3(i8 %x) {
 ; CHECK-LABEL: @t3(
-; CHECK-NEXT:    [[NEG_X:%.*]] = sub nsw i8 0, [[X:%.*]]
-; CHECK-NEXT:    [[CMP:%.*]] = icmp sle i8 [[NEG_X]], [[X]]
+; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt i8 [[X:%.*]], -1
 ; CHECK-NEXT:    ret i1 [[CMP]]
 ;
   %neg_x = sub nsw i8 0, %x