[PATCH] D53874: [InstSimplify] fold 'fcmp nnan oge X, 0.0' when X is not negative

Sanjay Patel via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 30 10:23:08 PDT 2018


spatel created this revision.
spatel added reviewers: efriedma, arsenm, mcberg2017.
Herald added subscribers: wdng, mcrosier.

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, I think this is the correct way to predicate the transform.

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


https://reviews.llvm.org/D53874

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


Index: test/Transforms/InstSimplify/floating-point-compare.ll
===================================================================
--- test/Transforms/InstSimplify/floating-point-compare.ll
+++ test/Transforms/InstSimplify/floating-point-compare.ll
@@ -254,20 +254,16 @@
 
 define i1 @orderedLessZeroUIToFP_nnan(i32 %x) {
 ; CHECK-LABEL: @orderedLessZeroUIToFP_nnan(
-; CHECK-NEXT:    [[A:%.*]] = uitofp i32 [[X:%.*]] to float
-; CHECK-NEXT:    [[UGE:%.*]] = fcmp nnan oge float [[A]], 0.000000e+00
-; CHECK-NEXT:    ret i1 [[UGE]]
+; CHECK-NEXT:    ret i1 true
 ;
   %a = uitofp i32 %x to float
   %uge = fcmp nnan oge float %a, 0.000000e+00
   ret i1 %uge
 }
 
 define <2 x i1> @orderedLessZeroUIToFP_nnan_vec(<2 x i32> %x) {
 ; CHECK-LABEL: @orderedLessZeroUIToFP_nnan_vec(
-; CHECK-NEXT:    [[A:%.*]] = uitofp <2 x i32> [[X:%.*]] to <2 x float>
-; CHECK-NEXT:    [[UGE:%.*]] = fcmp nnan oge <2 x float> [[A]], zeroinitializer
-; CHECK-NEXT:    ret <2 x i1> [[UGE]]
+; CHECK-NEXT:    ret <2 x i1> <i1 true, i1 true>
 ;
   %a = uitofp <2 x i32> %x to <2 x float>
   %uge = fcmp nnan oge <2 x float> %a, zeroinitializer
@@ -294,20 +290,16 @@
 
 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
   ret i1 %cmp
 }
 
 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
Index: lib/Transforms/InstCombine/InstCombineCompares.cpp
===================================================================
--- lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -5435,15 +5435,16 @@
           break;
         // fabs(x) < 0 --> false
         case FCmpInst::FCMP_OLT:
-          llvm_unreachable("handled by SimplifyFCmpInst");
+          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
         case FCmpInst::FCMP_OLE:
           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
Index: lib/Analysis/InstructionSimplify.cpp
===================================================================
--- lib/Analysis/InstructionSimplify.cpp
+++ lib/Analysis/InstructionSimplify.cpp
@@ -3570,6 +3570,10 @@
     }
     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);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D53874.171727.patch
Type: text/x-patch
Size: 3714 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181030/5db580f8/attachment.bin>


More information about the llvm-commits mailing list