[PATCH] D84035: [InstSimplify] fold fcmp with infinity constant using isKnownNeverInfinity

Sanjay Patel via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 17 08:25:39 PDT 2020


spatel created this revision.
spatel added reviewers: efriedma, arsenm, bkramer, hfinkel.
Herald added subscribers: hiraditya, wdng, mcrosier.
Herald added a project: LLVM.

This is a step towards trying to remove unnecessary FP compares with infinity when compiling with -ffinite-math-only or similar.
I'm intentionally not checking FMF on the fcmp itself because I'm assuming that will go away eventually.
The analysis part of this was added with rGcd481136 <https://reviews.llvm.org/rGcd4811360e2a1d23578073c6c99b2ef8ba276289> for use with isKnownNeverNaN. Similarly, that could be an enhancement here to get predicates like 'one' and 'ueq'.


https://reviews.llvm.org/D84035

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


Index: llvm/test/Transforms/InstSimplify/floating-point-compare.ll
===================================================================
--- llvm/test/Transforms/InstSimplify/floating-point-compare.ll
+++ llvm/test/Transforms/InstSimplify/floating-point-compare.ll
@@ -1041,9 +1041,7 @@
 
 define i1 @is_infinite(float %x) {
 ; CHECK-LABEL: @is_infinite(
-; CHECK-NEXT:    [[XABS:%.*]] = call ninf float @llvm.fabs.f32(float [[X:%.*]])
-; CHECK-NEXT:    [[R:%.*]] = fcmp oeq float [[XABS]], 0x7FF0000000000000
-; CHECK-NEXT:    ret i1 [[R]]
+; CHECK-NEXT:    ret i1 false
 ;
   %xabs = call ninf float @llvm.fabs.f32(float %x)
   %r = fcmp oeq float %xabs, 0x7FF0000000000000
@@ -1052,15 +1050,15 @@
 
 define <2 x i1> @is_infinite_neg(<2 x float> %x) {
 ; CHECK-LABEL: @is_infinite_neg(
-; CHECK-NEXT:    [[X42:%.*]] = fadd ninf <2 x float> [[X:%.*]], <float 4.200000e+01, float 4.200000e+01>
-; CHECK-NEXT:    [[R:%.*]] = fcmp oeq <2 x float> [[X42]], <float 0xFFF0000000000000, float 0xFFF0000000000000>
-; CHECK-NEXT:    ret <2 x i1> [[R]]
+; CHECK-NEXT:    ret <2 x i1> zeroinitializer
 ;
   %x42 = fadd ninf <2 x float> %x, <float 42.0, float 42.0>
   %r = fcmp oeq <2 x float> %x42, <float 0xFFF0000000000000, float 0xFFF0000000000000>
   ret <2 x i1> %r
 }
 
+; Negative test - but this could be reduced to 'uno' outside of instsimplify.
+
 define i1 @is_infinite_or_nan(float %x) {
 ; CHECK-LABEL: @is_infinite_or_nan(
 ; CHECK-NEXT:    [[X42:%.*]] = fadd ninf float [[X:%.*]], 4.200000e+01
@@ -1074,10 +1072,7 @@
 
 define i1 @is_finite_or_nan(i1 %c, double %x) {
 ; CHECK-LABEL: @is_finite_or_nan(
-; CHECK-NEXT:    [[XX:%.*]] = fmul ninf double [[X:%.*]], [[X]]
-; CHECK-NEXT:    [[S:%.*]] = select i1 [[C:%.*]], double 4.200000e+01, double [[XX]]
-; CHECK-NEXT:    [[R:%.*]] = fcmp une double [[S]], 0x7FF0000000000000
-; CHECK-NEXT:    ret i1 [[R]]
+; CHECK-NEXT:    ret i1 true
 ;
   %xx = fmul ninf double %x, %x
   %s = select i1 %c, double 42.0, double %xx
@@ -1087,15 +1082,15 @@
 
 define <2 x i1> @is_finite_or_nan_commute(<2 x i8> %x) {
 ; CHECK-LABEL: @is_finite_or_nan_commute(
-; CHECK-NEXT:    [[CAST:%.*]] = uitofp <2 x i8> [[X:%.*]] to <2 x float>
-; CHECK-NEXT:    [[R:%.*]] = fcmp une <2 x float> <float 0x7FF0000000000000, float 0x7FF0000000000000>, [[CAST]]
-; CHECK-NEXT:    ret <2 x i1> [[R]]
+; CHECK-NEXT:    ret <2 x i1> <i1 true, i1 true>
 ;
   %cast = uitofp <2 x i8> %x to <2 x float>
   %r = fcmp une <2 x float> <float 0x7FF0000000000000, float 0x7FF0000000000000>, %cast
   ret <2 x i1> %r
 }
 
+; Negative test - but this could be reduced to 'ord' outside of instsimplify.
+
 define i1 @is_finite_and_ordered(double %x) {
 ; CHECK-LABEL: @is_finite_and_ordered(
 ; CHECK-NEXT:    [[XX:%.*]] = fmul ninf double [[X:%.*]], [[X]]
Index: llvm/lib/Analysis/InstructionSimplify.cpp
===================================================================
--- llvm/lib/Analysis/InstructionSimplify.cpp
+++ llvm/lib/Analysis/InstructionSimplify.cpp
@@ -3703,6 +3703,13 @@
           break;
         }
       }
+
+      // LHS == Inf
+      if (Pred == FCmpInst::FCMP_OEQ && isKnownNeverInfinity(LHS, Q.TLI))
+        return getFalse(RetTy);
+      // LHS != Inf
+      if (Pred == FCmpInst::FCMP_UNE && isKnownNeverInfinity(LHS, Q.TLI))
+        return getTrue(RetTy);
     }
     if (C->isNegative() && !C->isNegZero()) {
       assert(!C->isNaN() && "Unexpected NaN constant!");


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D84035.278775.patch
Type: text/x-patch
Size: 3410 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200717/c70b1aa8/attachment.bin>


More information about the llvm-commits mailing list