]> granicus.if.org Git - llvm/commitdiff
[InstSimplify] enhance fcmp fold with never-nan operand
authorSanjay Patel <spatel@rotateright.com>
Sat, 8 Jun 2019 15:12:33 +0000 (15:12 +0000)
committerSanjay Patel <spatel@rotateright.com>
Sat, 8 Jun 2019 15:12:33 +0000 (15:12 +0000)
This is 1 step towards correcting our usage of fast-math-flags when applied on an fcmp.
In this case, we are checking for 'nnan' on the fcmp itself rather than the operand of
the fcmp. But I'm leaving that clause in until we're more confident that we can stop
relying on fcmp's FMF.

By using the more general "isKnownNeverNaN()", we gain a simplification shown on the
tests with 'uitofp' regardless of the FMF on the fcmp (uitofp never produces a NaN).
On the tests with 'fabs', we are now relying on the FMF for the call fabs instruction
in addition to the FMF on the fcmp.

I'll update the 'ult' case below here as a follow-up assuming no problems here.

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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@362879 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/InstructionSimplify.cpp
test/Transforms/InstSimplify/floating-point-compare.ll

index 9eb0e908aab43eac620d7863758633d31b313bd6..47369db4b461cda96161cfce9f64bd2f96c685a9 100644 (file)
@@ -3477,7 +3477,8 @@ static Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
   if (match(RHS, m_AnyZeroFP())) {
     switch (Pred) {
     case FCmpInst::FCMP_OGE:
-      if (FMF.noNaNs() && CannotBeOrderedLessThanZero(LHS, Q.TLI))
+      if ((FMF.noNaNs() || isKnownNeverNaN(LHS, Q.TLI)) &&
+          CannotBeOrderedLessThanZero(LHS, Q.TLI))
         return getTrue(RetTy);
       break;
     case FCmpInst::FCMP_UGE:
@@ -3485,6 +3486,7 @@ static Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
         return getTrue(RetTy);
       break;
     case FCmpInst::FCMP_ULT:
+      // TODO: This should match 'oge'.
       if (FMF.noNaNs() && CannotBeOrderedLessThanZero(LHS, Q.TLI))
         return getFalse(RetTy);
       break;
index 3ca66c9a447dad7d01298d156e1a6984b9e9838b..cefcff1dac7656c989d2569c51f8e3e63fe7a2db 100644 (file)
@@ -257,9 +257,7 @@ define <2 x i1> @UIToFP_is_nan_or_positive_or_zero_vec(<2 x i32> %x) {
 
 define i1 @UIToFP_is_positive_or_zero(i32 %x) {
 ; CHECK-LABEL: @UIToFP_is_positive_or_zero(
-; CHECK-NEXT:    [[A:%.*]] = uitofp i32 [[X:%.*]] to float
-; CHECK-NEXT:    [[R:%.*]] = fcmp oge float [[A]], 0.000000e+00
-; CHECK-NEXT:    ret i1 [[R]]
+; CHECK-NEXT:    ret i1 true
 ;
   %a = uitofp i32 %x to float
   %r = fcmp oge float %a, 0.000000e+00
@@ -268,9 +266,7 @@ define i1 @UIToFP_is_positive_or_zero(i32 %x) {
 
 define <2 x i1> @UIToFP_is_positive_or_zero_vec(<2 x i32> %x) {
 ; CHECK-LABEL: @UIToFP_is_positive_or_zero_vec(
-; CHECK-NEXT:    [[A:%.*]] = uitofp <2 x i32> [[X:%.*]] to <2 x float>
-; CHECK-NEXT:    [[R:%.*]] = fcmp 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 oge <2 x float> %a, zeroinitializer
@@ -351,9 +347,7 @@ define <2 x i1> @fabs_is_nan_or_positive_or_zero_vec(<2 x double> %x) {
 
 define i1 @fabs_nnan_is_positive_or_zero(double %x) {
 ; CHECK-LABEL: @fabs_nnan_is_positive_or_zero(
-; CHECK-NEXT:    [[FABS:%.*]] = tail call nnan double @llvm.fabs.f64(double [[X:%.*]])
-; CHECK-NEXT:    [[CMP:%.*]] = fcmp oge double [[FABS]], 0.000000e+00
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %fabs = tail call nnan double @llvm.fabs.f64(double %x)
   %cmp = fcmp oge double %fabs, 0.0
@@ -362,9 +356,7 @@ define i1 @fabs_nnan_is_positive_or_zero(double %x) {
 
 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 nnan <2 x double> @llvm.fabs.v2f64(<2 x double> [[X:%.*]])
-; CHECK-NEXT:    [[CMP:%.*]] = fcmp 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 nnan <2 x double> @llvm.fabs.v2f64(<2 x double> %x)
   %cmp = fcmp oge <2 x double> %fabs, zeroinitializer