[llvm] [InstCombine] Optimise x / sqrt(y / z) with fast-math pattern. (PR #76737)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Tue Jan 2 09:50:38 PST 2024
================
@@ -1701,6 +1701,31 @@ static Instruction *foldFDivPowDivisor(BinaryOperator &I,
return BinaryOperator::CreateFMulFMF(Op0, Pow, &I);
}
+/// Convert div to mul if we have an sqrt divisor iff sqrt's operand is a fdiv
+/// instruction.
+static Instruction *foldFDivSqrtDivisor(BinaryOperator &I,
+ InstCombiner::BuilderTy &Builder) {
+ // X / sqrt(Y / Z) --> X * sqrt(Z / Y)
+ Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
+ auto *II = dyn_cast<IntrinsicInst>(Op1);
+ if (!II || II->getIntrinsicID() != Intrinsic::sqrt || !II->hasOneUse() ||
+ !I.hasAllowReassoc() || !I.hasAllowReciprocal())
+ return nullptr;
+
+ Value *Y, *Z;
+ auto *DivOp = dyn_cast<Instruction>(II->getOperand(0));
+ if (!DivOp || !DivOp->hasOneUse() || !DivOp->hasAllowReassoc() ||
+ !I.hasAllowReciprocal())
+ return nullptr;
+ if (match(DivOp, m_FDiv(m_Value(Y), m_Value(Z)))) {
+ Value *SwapDiv = Builder.CreateFDivFMF(Z, Y, DivOp);
+ Value *NewSqrt = Builder.CreateIntrinsic(II->getIntrinsicID(),
+ II->getType(), {SwapDiv}, II);
----------------
dtcxzyw wrote:
```suggestion
Value *NewSqrt = Builder.CreateUnaryIntrinsic(Intrinsic::sqrt, SwapDiv, II);
```
https://github.com/llvm/llvm-project/pull/76737
More information about the llvm-commits
mailing list