[llvm] r361118 - [InstSimplify] fold fcmp (maxnum, X, C1), C2

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Sun May 19 07:26:39 PDT 2019


Author: spatel
Date: Sun May 19 07:26:39 2019
New Revision: 361118

URL: http://llvm.org/viewvc/llvm-project?rev=361118&view=rev
Log:
[InstSimplify] fold fcmp (maxnum, X, C1), C2

This is the sibling transform for rL360899 (D61691):

  maxnum(X, GreaterC) == C --> false
  maxnum(X, GreaterC) <= C --> false
  maxnum(X, GreaterC) <  C --> false
  maxnum(X, GreaterC) >= C --> true
  maxnum(X, GreaterC) >  C --> true
  maxnum(X, GreaterC) != C --> true

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=361118&r1=361117&r2=361118&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/InstructionSimplify.cpp (original)
+++ llvm/trunk/lib/Analysis/InstructionSimplify.cpp Sun May 19 07:26:39 2019
@@ -3435,28 +3435,38 @@ static Value *SimplifyFCmpInst(unsigned
     }
 
     // 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.
+    const APFloat *C2;
+    if ((match(LHS, m_Intrinsic<Intrinsic::minnum>(m_Value(), m_APFloat(C2))) &&
+         C2->compare(*C) == APFloat::cmpLessThan) ||
+        (match(LHS, m_Intrinsic<Intrinsic::maxnum>(m_Value(), m_APFloat(C2))) &&
+         C2->compare(*C) == APFloat::cmpGreaterThan)) {
+      bool IsMaxNum =
+          cast<IntrinsicInst>(LHS)->getIntrinsicID() == Intrinsic::maxnum;
+      // The ordered relationship and minnum/maxnum guarantee that we do not
+      // have NaN constants, so ordered/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
+        // minnum(X, LesserC)  == C --> false
+        // maxnum(X, GreaterC) == C --> false
         return getFalse(RetTy);
       case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_UNE:
+        // minnum(X, LesserC)  != C --> true
+        // maxnum(X, GreaterC) != C --> true
+        return getTrue(RetTy);
+      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
+        // maxnum(X, GreaterC) >= C --> true
+        // maxnum(X, GreaterC) >  C --> true
+        return ConstantInt::get(RetTy, IsMaxNum);
       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);
+        // minnum(X, LesserC)  <= C --> true
+        // minnum(X, LesserC)  <  C --> true
+        // maxnum(X, GreaterC) <= C --> false
+        // maxnum(X, GreaterC) <  C --> false
+        return ConstantInt::get(RetTy, !IsMaxNum);
       default:
         // TRUE/FALSE/ORD/UNO should be handled before this.
         llvm_unreachable("Unexpected fcmp predicate");

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=361118&r1=361117&r2=361118&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstSimplify/floating-point-compare.ll (original)
+++ llvm/trunk/test/Transforms/InstSimplify/floating-point-compare.ll Sun May 19 07:26:39 2019
@@ -682,9 +682,7 @@ define i1 @minnum_une_nan_min_constant(f
 
 define i1 @maxnum_oeq_large_max_constant(float %x) {
 ; CHECK-LABEL: @maxnum_oeq_large_max_constant(
-; CHECK-NEXT:    [[MAX:%.*]] = call float @llvm.maxnum.f32(float [[X:%.*]], float 1.500000e+00)
-; CHECK-NEXT:    [[CMP:%.*]] = fcmp oeq float [[MAX]], 1.000000e+00
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 false
 ;
   %max = call float @llvm.maxnum.f32(float %x, float 1.5)
   %cmp = fcmp oeq float %max, 1.0
@@ -695,9 +693,7 @@ define i1 @maxnum_oeq_large_max_constant
 
 define i1 @maxnum_olt_large_max_constant(float %x) {
 ; CHECK-LABEL: @maxnum_olt_large_max_constant(
-; CHECK-NEXT:    [[MAX:%.*]] = call float @llvm.maxnum.f32(float [[X:%.*]], float 1.500000e+00)
-; CHECK-NEXT:    [[CMP:%.*]] = fcmp olt float [[MAX]], 1.000000e+00
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 false
 ;
   %max = call float @llvm.maxnum.f32(float %x, float 1.5)
   %cmp = fcmp olt float %max, 1.0
@@ -708,9 +704,7 @@ define i1 @maxnum_olt_large_max_constant
 
 define i1 @maxnum_ole_large_max_constant(float %x) {
 ; CHECK-LABEL: @maxnum_ole_large_max_constant(
-; CHECK-NEXT:    [[MAX:%.*]] = call float @llvm.maxnum.f32(float [[X:%.*]], float 1.500000e+00)
-; CHECK-NEXT:    [[CMP:%.*]] = fcmp ole float [[MAX]], 1.000000e+00
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 false
 ;
   %max = call float @llvm.maxnum.f32(float %x, float 1.5)
   %cmp = fcmp ole float %max, 1.0
@@ -721,9 +715,7 @@ define i1 @maxnum_ole_large_max_constant
 
 define i1 @maxnum_ueq_large_max_constant(float %x) {
 ; CHECK-LABEL: @maxnum_ueq_large_max_constant(
-; CHECK-NEXT:    [[MAX:%.*]] = call float @llvm.maxnum.f32(float [[X:%.*]], float 1.500000e+00)
-; CHECK-NEXT:    [[CMP:%.*]] = fcmp ueq float [[MAX]], 1.000000e+00
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 false
 ;
   %max = call float @llvm.maxnum.f32(float %x, float 1.5)
   %cmp = fcmp ueq float %max, 1.0
@@ -734,9 +726,7 @@ define i1 @maxnum_ueq_large_max_constant
 
 define i1 @maxnum_ult_large_max_constant(float %x) {
 ; CHECK-LABEL: @maxnum_ult_large_max_constant(
-; CHECK-NEXT:    [[MAX:%.*]] = call float @llvm.maxnum.f32(float [[X:%.*]], float 1.500000e+00)
-; CHECK-NEXT:    [[CMP:%.*]] = fcmp ult float [[MAX]], 1.000000e+00
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 false
 ;
   %max = call float @llvm.maxnum.f32(float %x, float 1.5)
   %cmp = fcmp ult float %max, 1.0
@@ -747,9 +737,7 @@ define i1 @maxnum_ult_large_max_constant
 
 define <2 x i1> @maxnum_ule_large_max_constant(<2 x float> %x) {
 ; CHECK-LABEL: @maxnum_ule_large_max_constant(
-; CHECK-NEXT:    [[MAX:%.*]] = call <2 x float> @llvm.maxnum.v2f32(<2 x float> [[X:%.*]], <2 x float> <float 1.500000e+00, float 1.500000e+00>)
-; CHECK-NEXT:    [[CMP:%.*]] = fcmp ule <2 x float> [[MAX]], <float 1.000000e+00, float 1.000000e+00>
-; CHECK-NEXT:    ret <2 x i1> [[CMP]]
+; CHECK-NEXT:    ret <2 x i1> zeroinitializer
 ;
   %max = call <2 x float> @llvm.maxnum.v2f32(<2 x float> %x, <2 x float> <float 1.5, float 1.5>)
   %cmp = fcmp ule <2 x float> %max, <float 1.0, float 1.0>
@@ -760,9 +748,7 @@ define <2 x i1> @maxnum_ule_large_max_co
 
 define <2 x i1> @maxnum_ogt_large_max_constant(<2 x float> %x) {
 ; CHECK-LABEL: @maxnum_ogt_large_max_constant(
-; CHECK-NEXT:    [[MAX:%.*]] = call <2 x float> @llvm.maxnum.v2f32(<2 x float> [[X:%.*]], <2 x float> <float 1.500000e+00, float 1.500000e+00>)
-; CHECK-NEXT:    [[CMP:%.*]] = fcmp ogt <2 x float> [[MAX]], <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>
 ;
   %max = call <2 x float> @llvm.maxnum.v2f32(<2 x float> %x, <2 x float> <float 1.5, float 1.5>)
   %cmp = fcmp ogt <2 x float> %max, <float 1.0, float 1.0>
@@ -773,9 +759,7 @@ define <2 x i1> @maxnum_ogt_large_max_co
 
 define i1 @maxnum_oge_large_max_constant(float %x) {
 ; CHECK-LABEL: @maxnum_oge_large_max_constant(
-; CHECK-NEXT:    [[MAX:%.*]] = call float @llvm.maxnum.f32(float [[X:%.*]], float 1.500000e+00)
-; CHECK-NEXT:    [[CMP:%.*]] = fcmp oge float [[MAX]], 1.000000e+00
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %max = call float @llvm.maxnum.f32(float %x, float 1.5)
   %cmp = fcmp oge float %max, 1.0
@@ -786,9 +770,7 @@ define i1 @maxnum_oge_large_max_constant
 
 define i1 @maxnum_one_large_max_constant(float %x) {
 ; CHECK-LABEL: @maxnum_one_large_max_constant(
-; CHECK-NEXT:    [[MAX:%.*]] = call float @llvm.maxnum.f32(float [[X:%.*]], float 1.500000e+00)
-; CHECK-NEXT:    [[CMP:%.*]] = fcmp one float [[MAX]], 1.000000e+00
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %max = call float @llvm.maxnum.f32(float %x, float 1.5)
   %cmp = fcmp one float %max, 1.0
@@ -799,9 +781,7 @@ define i1 @maxnum_one_large_max_constant
 
 define i1 @maxnum_ugt_large_max_constant(float %x) {
 ; CHECK-LABEL: @maxnum_ugt_large_max_constant(
-; CHECK-NEXT:    [[MAX:%.*]] = call float @llvm.maxnum.f32(float [[X:%.*]], float 1.500000e+00)
-; CHECK-NEXT:    [[CMP:%.*]] = fcmp ugt float [[MAX]], 1.000000e+00
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %max = call float @llvm.maxnum.f32(float %x, float 1.5)
   %cmp = fcmp ugt float %max, 1.0
@@ -812,9 +792,7 @@ define i1 @maxnum_ugt_large_max_constant
 
 define i1 @maxnum_uge_large_max_constant(float %x) {
 ; CHECK-LABEL: @maxnum_uge_large_max_constant(
-; CHECK-NEXT:    [[MAX:%.*]] = call float @llvm.maxnum.f32(float [[X:%.*]], float 1.500000e+00)
-; CHECK-NEXT:    [[CMP:%.*]] = fcmp uge float [[MAX]], 1.000000e+00
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %max = call float @llvm.maxnum.f32(float %x, float 1.5)
   %cmp = fcmp uge float %max, 1.0
@@ -825,9 +803,7 @@ define i1 @maxnum_uge_large_max_constant
 
 define i1 @maxnum_une_large_max_constant(float %x) {
 ; CHECK-LABEL: @maxnum_une_large_max_constant(
-; CHECK-NEXT:    [[MAX:%.*]] = call float @llvm.maxnum.f32(float [[X:%.*]], float 1.500000e+00)
-; CHECK-NEXT:    [[CMP:%.*]] = fcmp une float [[MAX]], 1.000000e+00
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %max = call float @llvm.maxnum.f32(float %x, float 1.5)
   %cmp = fcmp une float %max, 1.0
@@ -862,7 +838,7 @@ define i1 @maxnum_une_small_max_constant
   ret i1 %cmp
 }
 
-; Partial negative test (the minnum simplifies):
+; Partial negative test (the maxnum simplifies):
 ; max(x, NaN) != 1.0 --> x != 1.0
 
 define i1 @maxnum_une_nan_max_constant(float %x) {




More information about the llvm-commits mailing list