[llvm] r360899 - [InstSimplify] fold fcmp (minnum, X, C1), C2

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Thu May 16 07:03:10 PDT 2019


Author: spatel
Date: Thu May 16 07:03:10 2019
New Revision: 360899

URL: http://llvm.org/viewvc/llvm-project?rev=360899&view=rev
Log:
[InstSimplify] fold fcmp (minnum, X, C1), C2
   minnum(X, LesserC) == C --> false
   minnum(X, LesserC) >= C --> false
   minnum(X, LesserC) >  C --> false
   minnum(X, LesserC) != C --> true
   minnum(X, LesserC) <= C --> true
   minnum(X, LesserC) <  C --> true

maxnum siblings will follow if there are no problems here.

We should be able to perform some other combines when the constants
are equal or greater-than too, but that would go in instcombine.

We might also generalize this by creating an FP ConstantRange
(similar to what we do for integers).

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

Modified:
    llvm/trunk/lib/Analysis/InstructionSimplify.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=360899&r1=360898&r2=360899&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/InstructionSimplify.cpp (original)
+++ llvm/trunk/lib/Analysis/InstructionSimplify.cpp Thu May 16 07:03:10 2019
@@ -3433,7 +3433,37 @@ static Value *SimplifyFCmpInst(unsigned
         break;
       }
     }
+
+    // Check comparison of constant with minnum with smaller constant.
+    // TODO: Add the related transform for maxnum.
+    const APFloat *MinC;
+    if (match(LHS,
+              m_Intrinsic<Intrinsic::minnum>(m_Value(), m_APFloat(MinC))) &&
+        MinC->compare(*C) == APFloat::cmpLessThan) {
+      // The 'less-than' relationship and minnum guarantee that we do not have
+      // NaN constants, so ordered and unordered preds are handled the same.
+      switch (Pred) {
+      case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_UEQ:
+      case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_UGE:
+      case FCmpInst::FCMP_OGT: case FCmpInst::FCMP_UGT:
+        // minnum(X, LesserC) == C --> false
+        // minnum(X, LesserC) >= C --> false
+        // minnum(X, LesserC) >  C --> false
+        return getFalse(RetTy);
+      case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_UNE:
+      case FCmpInst::FCMP_OLE: case FCmpInst::FCMP_ULE:
+      case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_ULT:
+        // minnum(X, LesserC) != C --> true
+        // minnum(X, LesserC) <= C --> true
+        // minnum(X, LesserC) <  C --> true
+        return getTrue(RetTy);
+      default:
+        // TRUE/FALSE/ORD/UNO should be handled before this.
+        llvm_unreachable("Unexpected fcmp predicate");
+      }
+    }
   }
+
   if (match(RHS, m_AnyZeroFP())) {
     switch (Pred) {
     case FCmpInst::FCMP_OGE:

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=360899&r1=360898&r2=360899&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstSimplify/floating-point-compare.ll (original)
+++ llvm/trunk/test/Transforms/InstSimplify/floating-point-compare.ll Thu May 16 07:03:10 2019
@@ -508,9 +508,7 @@ define i1 @maxnum_non_nan(float %x) {
 
 define i1 @minnum_oeq_small_min_constant(float %x) {
 ; CHECK-LABEL: @minnum_oeq_small_min_constant(
-; CHECK-NEXT:    [[MIN:%.*]] = call float @llvm.minnum.f32(float [[X:%.*]], float 5.000000e-01)
-; CHECK-NEXT:    [[CMP:%.*]] = fcmp oeq float [[MIN]], 1.000000e+00
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 false
 ;
   %min = call float @llvm.minnum.f32(float %x, float 0.5)
   %cmp = fcmp oeq float %min, 1.0
@@ -521,9 +519,7 @@ define i1 @minnum_oeq_small_min_constant
 
 define i1 @minnum_ogt_small_min_constant(float %x) {
 ; CHECK-LABEL: @minnum_ogt_small_min_constant(
-; CHECK-NEXT:    [[MIN:%.*]] = call float @llvm.minnum.f32(float [[X:%.*]], float 5.000000e-01)
-; CHECK-NEXT:    [[CMP:%.*]] = fcmp ogt float [[MIN]], 1.000000e+00
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 false
 ;
   %min = call float @llvm.minnum.f32(float %x, float 0.5)
   %cmp = fcmp ogt float %min, 1.0
@@ -534,9 +530,7 @@ define i1 @minnum_ogt_small_min_constant
 
 define i1 @minnum_oge_small_min_constant(float %x) {
 ; CHECK-LABEL: @minnum_oge_small_min_constant(
-; CHECK-NEXT:    [[MIN:%.*]] = call float @llvm.minnum.f32(float [[X:%.*]], float 5.000000e-01)
-; CHECK-NEXT:    [[CMP:%.*]] = fcmp oge float [[MIN]], 1.000000e+00
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 false
 ;
   %min = call float @llvm.minnum.f32(float %x, float 0.5)
   %cmp = fcmp oge float %min, 1.0
@@ -547,9 +541,7 @@ define i1 @minnum_oge_small_min_constant
 
 define i1 @minnum_ueq_small_min_constant(float %x) {
 ; CHECK-LABEL: @minnum_ueq_small_min_constant(
-; CHECK-NEXT:    [[MIN:%.*]] = call float @llvm.minnum.f32(float [[X:%.*]], float 5.000000e-01)
-; CHECK-NEXT:    [[CMP:%.*]] = fcmp ueq float [[MIN]], 1.000000e+00
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 false
 ;
   %min = call float @llvm.minnum.f32(float %x, float 0.5)
   %cmp = fcmp ueq float %min, 1.0
@@ -560,9 +552,7 @@ define i1 @minnum_ueq_small_min_constant
 
 define i1 @minnum_ugt_small_min_constant(float %x) {
 ; CHECK-LABEL: @minnum_ugt_small_min_constant(
-; CHECK-NEXT:    [[MIN:%.*]] = call float @llvm.minnum.f32(float [[X:%.*]], float 5.000000e-01)
-; CHECK-NEXT:    [[CMP:%.*]] = fcmp ugt float [[MIN]], 1.000000e+00
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 false
 ;
   %min = call float @llvm.minnum.f32(float %x, float 0.5)
   %cmp = fcmp ugt float %min, 1.0
@@ -573,9 +563,7 @@ define i1 @minnum_ugt_small_min_constant
 
 define <2 x i1> @minnum_uge_small_min_constant(<2 x float> %x) {
 ; CHECK-LABEL: @minnum_uge_small_min_constant(
-; CHECK-NEXT:    [[MIN:%.*]] = call <2 x float> @llvm.minnum.v2f32(<2 x float> [[X:%.*]], <2 x float> <float 5.000000e-01, float 5.000000e-01>)
-; CHECK-NEXT:    [[CMP:%.*]] = fcmp uge <2 x float> [[MIN]], <float 1.000000e+00, float 1.000000e+00>
-; CHECK-NEXT:    ret <2 x i1> [[CMP]]
+; CHECK-NEXT:    ret <2 x i1> zeroinitializer
 ;
   %min = call <2 x float> @llvm.minnum.v2f32(<2 x float> %x, <2 x float> <float 0.5, float 0.5>)
   %cmp = fcmp uge <2 x float> %min, <float 1.0, float 1.0>
@@ -586,9 +574,7 @@ define <2 x i1> @minnum_uge_small_min_co
 
 define <2 x i1> @minnum_olt_small_min_constant(<2 x float> %x) {
 ; CHECK-LABEL: @minnum_olt_small_min_constant(
-; CHECK-NEXT:    [[MIN:%.*]] = call <2 x float> @llvm.minnum.v2f32(<2 x float> [[X:%.*]], <2 x float> <float 5.000000e-01, float 5.000000e-01>)
-; CHECK-NEXT:    [[CMP:%.*]] = fcmp olt <2 x float> [[MIN]], <float 1.000000e+00, float 1.000000e+00>
-; CHECK-NEXT:    ret <2 x i1> [[CMP]]
+; CHECK-NEXT:    ret <2 x i1> <i1 true, i1 true>
 ;
   %min = call <2 x float> @llvm.minnum.v2f32(<2 x float> %x, <2 x float> <float 0.5, float 0.5>)
   %cmp = fcmp olt <2 x float> %min, <float 1.0, float 1.0>
@@ -599,9 +585,7 @@ define <2 x i1> @minnum_olt_small_min_co
 
 define i1 @minnum_ole_small_min_constant(float %x) {
 ; CHECK-LABEL: @minnum_ole_small_min_constant(
-; CHECK-NEXT:    [[MIN:%.*]] = call float @llvm.minnum.f32(float [[X:%.*]], float 5.000000e-01)
-; CHECK-NEXT:    [[CMP:%.*]] = fcmp ole float [[MIN]], 1.000000e+00
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %min = call float @llvm.minnum.f32(float %x, float 0.5)
   %cmp = fcmp ole float %min, 1.0
@@ -612,9 +596,7 @@ define i1 @minnum_ole_small_min_constant
 
 define i1 @minnum_one_small_min_constant(float %x) {
 ; CHECK-LABEL: @minnum_one_small_min_constant(
-; CHECK-NEXT:    [[MIN:%.*]] = call float @llvm.minnum.f32(float [[X:%.*]], float 5.000000e-01)
-; CHECK-NEXT:    [[CMP:%.*]] = fcmp one float [[MIN]], 1.000000e+00
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %min = call float @llvm.minnum.f32(float %x, float 0.5)
   %cmp = fcmp one float %min, 1.0
@@ -625,9 +607,7 @@ define i1 @minnum_one_small_min_constant
 
 define i1 @minnum_ult_small_min_constant(float %x) {
 ; CHECK-LABEL: @minnum_ult_small_min_constant(
-; CHECK-NEXT:    [[MIN:%.*]] = call float @llvm.minnum.f32(float [[X:%.*]], float 5.000000e-01)
-; CHECK-NEXT:    [[CMP:%.*]] = fcmp ult float [[MIN]], 1.000000e+00
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %min = call float @llvm.minnum.f32(float %x, float 0.5)
   %cmp = fcmp ult float %min, 1.0
@@ -638,9 +618,7 @@ define i1 @minnum_ult_small_min_constant
 
 define i1 @minnum_ule_small_min_constant(float %x) {
 ; CHECK-LABEL: @minnum_ule_small_min_constant(
-; CHECK-NEXT:    [[MIN:%.*]] = call float @llvm.minnum.f32(float [[X:%.*]], float 5.000000e-01)
-; CHECK-NEXT:    [[CMP:%.*]] = fcmp ule float [[MIN]], 1.000000e+00
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %min = call float @llvm.minnum.f32(float %x, float 0.5)
   %cmp = fcmp ule float %min, 1.0
@@ -651,11 +629,50 @@ define i1 @minnum_ule_small_min_constant
 
 define i1 @minnum_une_small_min_constant(float %x) {
 ; CHECK-LABEL: @minnum_une_small_min_constant(
-; CHECK-NEXT:    [[MIN:%.*]] = call float @llvm.minnum.f32(float [[X:%.*]], float 5.000000e-01)
+; CHECK-NEXT:    ret i1 true
+;
+  %min = call float @llvm.minnum.f32(float %x, float 0.5)
+  %cmp = fcmp une float %min, 1.0
+  ret i1 %cmp
+}
+
+; Negative test:
+; min(x, 1.0) != 1.0 --> ?
+
+define i1 @minnum_une_equal_min_constant(float %x) {
+; CHECK-LABEL: @minnum_une_equal_min_constant(
+; CHECK-NEXT:    [[MIN:%.*]] = call float @llvm.minnum.f32(float [[X:%.*]], float 1.000000e+00)
 ; CHECK-NEXT:    [[CMP:%.*]] = fcmp une float [[MIN]], 1.000000e+00
 ; CHECK-NEXT:    ret i1 [[CMP]]
 ;
-  %min = call float @llvm.minnum.f32(float %x, float 0.5)
+  %min = call float @llvm.minnum.f32(float %x, float 1.0)
+  %cmp = fcmp une float %min, 1.0
+  ret i1 %cmp
+}
+
+; Negative test:
+; min(x, 2.0) != 1.0 --> ?
+
+define i1 @minnum_une_large_min_constant(float %x) {
+; CHECK-LABEL: @minnum_une_large_min_constant(
+; CHECK-NEXT:    [[MIN:%.*]] = call float @llvm.minnum.f32(float [[X:%.*]], float 2.000000e+00)
+; CHECK-NEXT:    [[CMP:%.*]] = fcmp une float [[MIN]], 1.000000e+00
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %min = call float @llvm.minnum.f32(float %x, float 2.0)
+  %cmp = fcmp une float %min, 1.0
+  ret i1 %cmp
+}
+
+; Partial negative test (the minnum simplifies):
+; min(x, NaN) != 1.0 --> x != 1.0
+
+define i1 @minnum_une_nan_min_constant(float %x) {
+; CHECK-LABEL: @minnum_une_nan_min_constant(
+; CHECK-NEXT:    [[CMP:%.*]] = fcmp une float [[X:%.*]], 1.000000e+00
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %min = call float @llvm.minnum.f32(float %x, float 0x7FF8000000000000)
   %cmp = fcmp une float %min, 1.0
   ret i1 %cmp
 }




More information about the llvm-commits mailing list