[llvm] r345725 - [InstSimplify] fold 'fcmp nnan oge X, 0.0' when X is not negative

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 31 07:57:23 PDT 2018


Author: spatel
Date: Wed Oct 31 07:57:23 2018
New Revision: 345725

URL: http://llvm.org/viewvc/llvm-project?rev=345725&view=rev
Log:
[InstSimplify] fold 'fcmp nnan oge X, 0.0' when X is not negative

This re-raises some of the open questions about how to apply and use fast-math-flags in IR from PR38086:
https://bugs.llvm.org/show_bug.cgi?id=38086
...but given the current implementation (no FMF on casts), this is likely the only way to predicate the 
transform.

This is part of solving PR39475:
https://bugs.llvm.org/show_bug.cgi?id=39475

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

Modified:
    llvm/trunk/lib/Analysis/InstructionSimplify.cpp
    llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
    llvm/trunk/test/Transforms/InstSimplify/floating-point-compare.ll

Modified: llvm/trunk/lib/Analysis/InstructionSimplify.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/InstructionSimplify.cpp?rev=345725&r1=345724&r2=345725&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/InstructionSimplify.cpp (original)
+++ llvm/trunk/lib/Analysis/InstructionSimplify.cpp Wed Oct 31 07:57:23 2018
@@ -3612,6 +3612,10 @@ static Value *SimplifyFCmpInst(unsigned
     }
     if (C->isZero()) {
       switch (Pred) {
+      case FCmpInst::FCMP_OGE:
+        if (FMF.noNaNs() && CannotBeOrderedLessThanZero(LHS, Q.TLI))
+          return getTrue(RetTy);
+        break;
       case FCmpInst::FCMP_UGE:
         if (CannotBeOrderedLessThanZero(LHS, Q.TLI))
           return getTrue(RetTy);

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp?rev=345725&r1=345724&r2=345725&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp Wed Oct 31 07:57:23 2018
@@ -5433,8 +5433,8 @@ Instruction *InstCombiner::visitFCmpInst
         break;
       // fabs(x) < 0 --> false
       case FCmpInst::FCMP_OLT:
-        llvm_unreachable("handled by SimplifyFCmpInst");
-      // fabs(x) > 0 --> x != 0
+        llvm_unreachable("fcmp should have simplified");
+          // fabs(x) > 0 --> x != 0
       case FCmpInst::FCMP_OGT:
         return new FCmpInst(FCmpInst::FCMP_ONE, CI->getArgOperand(0), RHSC);
       // fabs(x) <= 0 --> x == 0
@@ -5442,6 +5442,7 @@ Instruction *InstCombiner::visitFCmpInst
         return new FCmpInst(FCmpInst::FCMP_OEQ, CI->getArgOperand(0), RHSC);
       // fabs(x) >= 0 --> !isnan(x)
       case FCmpInst::FCMP_OGE:
+        assert(!I.hasNoNaNs() && "fcmp should have simplified");
         return new FCmpInst(FCmpInst::FCMP_ORD, CI->getArgOperand(0), RHSC);
       // fabs(x) == 0 --> x == 0
       // fabs(x) != 0 --> x != 0

Modified: llvm/trunk/test/Transforms/InstSimplify/floating-point-compare.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstSimplify/floating-point-compare.ll?rev=345725&r1=345724&r2=345725&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstSimplify/floating-point-compare.ll (original)
+++ llvm/trunk/test/Transforms/InstSimplify/floating-point-compare.ll Wed Oct 31 07:57:23 2018
@@ -254,9 +254,7 @@ define <2 x i1> @UIToFP_is_nan_or_positi
 
 define i1 @UIToFP_nnan_is_positive_or_zero(i32 %x) {
 ; CHECK-LABEL: @UIToFP_nnan_is_positive_or_zero(
-; CHECK-NEXT:    [[A:%.*]] = uitofp i32 [[X:%.*]] to float
-; CHECK-NEXT:    [[R:%.*]] = fcmp nnan oge float [[A]], 0.000000e+00
-; CHECK-NEXT:    ret i1 [[R]]
+; CHECK-NEXT:    ret i1 true
 ;
   %a = uitofp i32 %x to float
   %r = fcmp nnan oge float %a, 0.000000e+00
@@ -265,9 +263,7 @@ define i1 @UIToFP_nnan_is_positive_or_ze
 
 define <2 x i1> @UIToFP_nnan_is_positive_or_zero_vec(<2 x i32> %x) {
 ; CHECK-LABEL: @UIToFP_nnan_is_positive_or_zero_vec(
-; CHECK-NEXT:    [[A:%.*]] = uitofp <2 x i32> [[X:%.*]] to <2 x float>
-; CHECK-NEXT:    [[R:%.*]] = fcmp nnan oge <2 x float> [[A]], zeroinitializer
-; CHECK-NEXT:    ret <2 x i1> [[R]]
+; CHECK-NEXT:    ret <2 x i1> <i1 true, i1 true>
 ;
   %a = uitofp <2 x i32> %x to <2 x float>
   %r = fcmp nnan oge <2 x float> %a, zeroinitializer
@@ -334,9 +330,7 @@ define <2 x i1> @fabs_is_nan_or_positive
 
 define i1 @fabs_nnan_is_positive_or_zero(double %x) {
 ; CHECK-LABEL: @fabs_nnan_is_positive_or_zero(
-; CHECK-NEXT:    [[FABS:%.*]] = tail call double @llvm.fabs.f64(double [[X:%.*]])
-; CHECK-NEXT:    [[CMP:%.*]] = fcmp nnan oge double [[FABS]], 0.000000e+00
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %fabs = tail call double @llvm.fabs.f64(double %x)
   %cmp = fcmp nnan oge double %fabs, 0.0
@@ -345,9 +339,7 @@ define i1 @fabs_nnan_is_positive_or_zero
 
 define <2 x i1> @fabs_nnan_is_positive_or_zero_vec(<2 x double> %x) {
 ; CHECK-LABEL: @fabs_nnan_is_positive_or_zero_vec(
-; CHECK-NEXT:    [[FABS:%.*]] = tail call <2 x double> @llvm.fabs.v2f64(<2 x double> [[X:%.*]])
-; CHECK-NEXT:    [[CMP:%.*]] = fcmp nnan oge <2 x double> [[FABS]], zeroinitializer
-; CHECK-NEXT:    ret <2 x i1> [[CMP]]
+; CHECK-NEXT:    ret <2 x i1> <i1 true, i1 true>
 ;
   %fabs = tail call <2 x double> @llvm.fabs.v2f64(<2 x double> %x)
   %cmp = fcmp nnan oge <2 x double> %fabs, zeroinitializer




More information about the llvm-commits mailing list