[PATCH] D126190: [InstCombine] Add combine for fcmp sqrt(x),C --> fcmp x,C*C

Bradley Smith via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon May 23 07:52:26 PDT 2022


bsmith updated this revision to Diff 431375.
bsmith added a comment.

- Be stricter around fast math flags and require 'fast' not just 'nnan'.
- Propagate fast math flags through combine.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D126190/new/

https://reviews.llvm.org/D126190

Files:
  llvm/include/llvm/IR/PatternMatch.h
  llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
  llvm/test/Transforms/InstCombine/fcmp.ll


Index: llvm/test/Transforms/InstCombine/fcmp.ll
===================================================================
--- llvm/test/Transforms/InstCombine/fcmp.ll
+++ llvm/test/Transforms/InstCombine/fcmp.ll
@@ -3,6 +3,7 @@
 
 declare half @llvm.fabs.f16(half)
 declare double @llvm.fabs.f64(double)
+declare double @llvm.sqrt.f64(double)
 declare <2 x float> @llvm.fabs.v2f32(<2 x float>)
 declare double @llvm.copysign.f64(double, double)
 declare <2 x double> @llvm.copysign.v2f64(<2 x double>, <2 x double>)
@@ -1210,3 +1211,38 @@
   %cmp = fcmp ninf une float %a, %fneg
   ret i1 %cmp
 }
+
+; fcmp sqrt(X),C --> fcmp X,C*C
+define i1 @fcmp_fsqrt_test1(double %v) {
+; CHECK-LABEL: @fcmp_fsqrt_test1(
+; CHECK-NEXT:    [[CMP:%.*]] = fcmp fast ogt double [[V:%.*]], 4.000000e+00
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %sqrt = call double @llvm.sqrt.f64(double %v)
+  %cmp = fcmp fast ogt double %sqrt, 2.000000e+00
+  ret i1 %cmp
+}
+
+; ensure we preserve sqrts when compared against negative numbers.
+define i1 @fcmp_fsqrt_test2(double %v) {
+; CHECK-LABEL: @fcmp_fsqrt_test2(
+; CHECK-NEXT:    [[SQRT:%.*]] = call double @llvm.sqrt.f64(double [[V:%.*]])
+; CHECK-NEXT:    [[CMP:%.*]] = fcmp fast ogt double [[SQRT]], -2.000000e+00
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %sqrt = call double @llvm.sqrt.f64(double %v)
+  %cmp = fcmp fast ogt double %sqrt, -2.000000e+00
+  ret i1 %cmp
+}
+
+; ensure we maintain sqrts when preserving NaNs.
+define i1 @fcmp_fsqrt_test3(double %v) {
+; CHECK-LABEL: @fcmp_fsqrt_test3(
+; CHECK-NEXT:    [[SQRT:%.*]] = call double @llvm.sqrt.f64(double [[V:%.*]])
+; CHECK-NEXT:    [[CMP:%.*]] = fcmp ogt double [[SQRT]], 2.000000e+00
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %sqrt = call double @llvm.sqrt.f64(double %v)
+  %cmp = fcmp ogt double %sqrt, 2.000000e+00
+  ret i1 %cmp
+}
Index: llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -6754,6 +6754,17 @@
     }
   }
 
+  if (match(Op0, m_Sqrt(m_Value(X)))) {
+    // fcmp sqrt(x),C -> fcmp x,C*C
+    const APFloat *CF;
+    if (match(Op1, m_APFloat(CF)) && !CF->isNegative() && I.isFast()) {
+      Constant *C = ConstantFP::get(X->getType(), *CF);
+      Instruction *FCmp = new FCmpInst(Pred, X, ConstantExpr::getFMul(C, C));
+      FCmp->setFastMathFlags(I.getFastMathFlags());
+      return FCmp;
+    }
+  }
+
   if (match(Op0, m_FPExt(m_Value(X)))) {
     // fcmp (fpext X), (fpext Y) -> fcmp X, Y
     if (match(Op1, m_FPExt(m_Value(Y))) && X->getType() == Y->getType())
Index: llvm/include/llvm/IR/PatternMatch.h
===================================================================
--- llvm/include/llvm/IR/PatternMatch.h
+++ llvm/include/llvm/IR/PatternMatch.h
@@ -2200,6 +2200,11 @@
   return m_Intrinsic<Intrinsic::maxnum>(Op0, Op1);
 }
 
+template <typename Opnd0>
+inline typename m_Intrinsic_Ty<Opnd0>::Ty m_Sqrt(const Opnd0 &Op0) {
+  return m_Intrinsic<Intrinsic::sqrt>(Op0);
+}
+
 template <typename Opnd0, typename Opnd1, typename Opnd2>
 inline typename m_Intrinsic_Ty<Opnd0, Opnd1, Opnd2>::Ty
 m_FShl(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D126190.431375.patch
Type: text/x-patch
Size: 3265 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220523/fddfa70d/attachment.bin>


More information about the llvm-commits mailing list