[llvm] b89ae10 - [InstSimplify] fold fcmp using isKnownNeverInfinity + isKnownNeverNaN

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 26 06:04:52 PDT 2020


Author: Sanjay Patel
Date: 2020-07-26T09:04:37-04:00
New Revision: b89ae102e6f5ed3760f1ae5788bd76ef8e9d9490

URL: https://github.com/llvm/llvm-project/commit/b89ae102e6f5ed3760f1ae5788bd76ef8e9d9490
DIFF: https://github.com/llvm/llvm-project/commit/b89ae102e6f5ed3760f1ae5788bd76ef8e9d9490.diff

LOG: [InstSimplify] fold fcmp using isKnownNeverInfinity + isKnownNeverNaN

Follow-up to D84035 / rG7393d7574c09.
This sidesteps a question of FMF/poison on fcmp raised in PR46077:
http://bugs.llvm.org/PR46077

https://alive2.llvm.org/ce/z/TCsyzD
  define i1 @src(float %x) {
  %0:
    %x42 = fadd nnan ninf float %x, 42.000000
    %r = fcmp ueq float %x42, inf
    ret i1 %r
  }
  =>
  define i1 @tgt(float %x) {
  %0:
    ret i1 0
  }
  Transformation seems to be correct!

https://alive2.llvm.org/ce/z/FQaH7a
  define i1 @src(i8 %x) {
  %0:
    %cast = uitofp i8 %x to float
    %r = fcmp one float inf, %cast
    ret i1 %r
  }
  =>
  define i1 @tgt(i8 %x) {
  %0:
    ret i1 1
  }
  Transformation seems to be correct!

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 49346860f251..396fc22920cd 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -3701,6 +3701,14 @@ static Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
       // LHS != Inf
       if (Pred == FCmpInst::FCMP_UNE && isKnownNeverInfinity(LHS, Q.TLI))
         return getTrue(RetTy);
+      // LHS == Inf || LHS == NaN
+      if (Pred == FCmpInst::FCMP_UEQ && isKnownNeverInfinity(LHS, Q.TLI) &&
+          isKnownNeverNaN(LHS, Q.TLI))
+        return getFalse(RetTy);
+      // LHS != Inf && LHS != NaN
+      if (Pred == FCmpInst::FCMP_ONE && isKnownNeverInfinity(LHS, Q.TLI) &&
+          isKnownNeverNaN(LHS, Q.TLI))
+        return getTrue(RetTy);
     }
     if (C->isNegative() && !C->isNegZero()) {
       assert(!C->isNaN() && "Unexpected NaN constant!");

diff  --git a/llvm/test/Transforms/InstSimplify/floating-point-compare.ll b/llvm/test/Transforms/InstSimplify/floating-point-compare.ll
index 6ce5c5e67b08..d0864e78570b 100644
--- a/llvm/test/Transforms/InstSimplify/floating-point-compare.ll
+++ b/llvm/test/Transforms/InstSimplify/floating-point-compare.ll
@@ -1072,9 +1072,7 @@ define i1 @is_infinite_or_nan(float %x) {
 
 define i1 @is_infinite_or_nan2(float %x) {
 ; CHECK-LABEL: @is_infinite_or_nan2(
-; CHECK-NEXT:    [[XABS:%.*]] = call nnan ninf float @llvm.fabs.f32(float [[X:%.*]])
-; CHECK-NEXT:    [[R:%.*]] = fcmp ueq float [[XABS]], 0x7FF0000000000000
-; CHECK-NEXT:    ret i1 [[R]]
+; CHECK-NEXT:    ret i1 false
 ;
   %xabs = call nnan ninf float @llvm.fabs.f32(float %x)
   %r = fcmp ueq float %xabs, 0x7FF0000000000000
@@ -1083,9 +1081,7 @@ define i1 @is_infinite_or_nan2(float %x) {
 
 define <2 x i1> @is_infinite_neg_or_nan(<2 x float> %x) {
 ; CHECK-LABEL: @is_infinite_neg_or_nan(
-; CHECK-NEXT:    [[X42:%.*]] = fadd nnan ninf <2 x float> [[X:%.*]], <float 4.200000e+01, float 4.200000e+01>
-; CHECK-NEXT:    [[R:%.*]] = fcmp ueq <2 x float> [[X42]], <float 0xFFF0000000000000, float 0xFFF0000000000000>
-; CHECK-NEXT:    ret <2 x i1> [[R]]
+; CHECK-NEXT:    ret <2 x i1> zeroinitializer
 ;
   %x42 = fadd nnan ninf <2 x float> %x, <float 42.0, float 42.0>
   %r = fcmp ueq <2 x float> %x42, <float 0xFFF0000000000000, float 0xFFF0000000000000>
@@ -1126,10 +1122,7 @@ define i1 @is_finite_and_ordered(double %x) {
 
 define i1 @is_finite(i1 %c, double %x) {
 ; CHECK-LABEL: @is_finite(
-; CHECK-NEXT:    [[XX:%.*]] = fmul nnan ninf double [[X:%.*]], [[X]]
-; CHECK-NEXT:    [[S:%.*]] = select i1 [[C:%.*]], double 4.200000e+01, double [[XX]]
-; CHECK-NEXT:    [[R:%.*]] = fcmp one double [[S]], 0x7FF0000000000000
-; CHECK-NEXT:    ret i1 [[R]]
+; CHECK-NEXT:    ret i1 true
 ;
   %xx = fmul nnan ninf double %x, %x
   %s = select i1 %c, double 42.0, double %xx
@@ -1139,9 +1132,7 @@ define i1 @is_finite(i1 %c, double %x) {
 
 define <2 x i1> @is_finite_commute(<2 x i8> %x) {
 ; CHECK-LABEL: @is_finite_commute(
-; CHECK-NEXT:    [[CAST:%.*]] = uitofp <2 x i8> [[X:%.*]] to <2 x float>
-; CHECK-NEXT:    [[R:%.*]] = fcmp one <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 one <2 x float> <float 0x7FF0000000000000, float 0x7FF0000000000000>, %cast


        


More information about the llvm-commits mailing list