[llvm] [InstCombineCompares] Replace the sqrt in if-condition (PR #91707)
Matt Arsenault via llvm-commits
llvm-commits at lists.llvm.org
Fri May 10 04:18:51 PDT 2024
================
@@ -8068,6 +8073,50 @@ Instruction *InstCombinerImpl::visitFCmpInst(FCmpInst &I) {
return new FCmpInst(I.getSwappedPredicate(), X, NegC, "", &I);
}
+ /// Try to sink the sqrt in this following case:
+ /// \code
+ /// tmp = sqrt(input)
+ /// if (tmp > cond)
+ /// branch_without_tmp
+ /// else
+ /// branch_with_tmp
+ /// ...
+ /// \endcode
+ /// Is optimized to:
+ /// \code
+ /// if (input > copysign(cond*cond, cond))
+ /// branch_without_tmp
+ /// else
+ /// tmp = sqrt(input)
+ /// branch_with_tmp
+ /// ...
+ /// \endcode
+ /// Only call sqrt in the branch that needs the sqrt result,
+ /// not if it is not needed, reducing the number of calls to sqrt.
+ if (I.isFast() && ReplaceSqrtInIfCondition) {
+ // fcmp sqrt(X), sqrt(Y) => fcmp X, Y
+ if (match(Op0, m_Intrinsic<Intrinsic::sqrt>(m_Value(X))) &&
+ match(Op1, m_Intrinsic<Intrinsic::sqrt>(m_Value(Y)))) {
+ auto CIX = cast<CallInst>(Op0);
+ auto CIY = cast<CallInst>(Op1);
+ return new FCmpInst(Pred, CIX->getOperand(0), CIY->getOperand(0), "", &I);
+ } else if (match(Op0, m_Intrinsic<Intrinsic::sqrt>(m_Value(X)))) {
+ // fcmp sqrt(X), Y => fcmp X, copysing(Y*Y, Y)
+ auto CI = cast<CallInst>(Op0);
+ Value *YY = Builder.CreateFMulFMF(Op1, Op1, &I);
+ Value *SYY =
+ Builder.CreateBinaryIntrinsic(Intrinsic::copysign, YY, Op1, &I);
+ return new FCmpInst(Pred, CI->getOperand(0), SYY, "", &I);
+ } else if (match(Op1, m_Intrinsic<Intrinsic::sqrt>(m_Value(Y)))) {
----------------
arsenm wrote:
No else after return
https://github.com/llvm/llvm-project/pull/91707
More information about the llvm-commits
mailing list