[IR] add and use pattern match specialization for sqrt intrinsic; NFC

This was included in D126190 originally, but it's
independent and a useful change for readability.
This commit is contained in:
Sanjay Patel 2022-05-23 13:31:00 -04:00
parent 569d8945f3
commit e8c20d995b
3 changed files with 13 additions and 11 deletions

View File

@ -2212,6 +2212,11 @@ m_FShr(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
return m_Intrinsic<Intrinsic::fshr>(Op0, Op1, Op2);
}
template <typename Opnd0>
inline typename m_Intrinsic_Ty<Opnd0>::Ty m_Sqrt(const Opnd0 &Op0) {
return m_Intrinsic<Intrinsic::sqrt>(Op0);
}
//===----------------------------------------------------------------------===//
// Matchers for two-operands operators with the operators in either order
//

View File

@ -5290,8 +5290,8 @@ static Value *SimplifyFMAFMul(Value *Op0, Value *Op1, FastMathFlags FMF,
// 2. Ignore non-zero negative numbers because sqrt would produce NAN.
// 3. Ignore -0.0 because sqrt(-0.0) == -0.0, but -0.0 * -0.0 == 0.0.
Value *X;
if (Op0 == Op1 && match(Op0, m_Intrinsic<Intrinsic::sqrt>(m_Value(X))) &&
FMF.allowReassoc() && FMF.noNaNs() && FMF.noSignedZeros())
if (Op0 == Op1 && match(Op0, m_Sqrt(m_Value(X))) && FMF.allowReassoc() &&
FMF.noNaNs() && FMF.noSignedZeros())
return X;
return nullptr;

View File

@ -529,9 +529,8 @@ Instruction *InstCombinerImpl::visitFMul(BinaryOperator &I) {
// sqrt(X) * sqrt(Y) -> sqrt(X * Y)
// nnan disallows the possibility of returning a number if both operands are
// negative (in that case, we should return NaN).
if (I.hasNoNaNs() &&
match(Op0, m_OneUse(m_Intrinsic<Intrinsic::sqrt>(m_Value(X)))) &&
match(Op1, m_OneUse(m_Intrinsic<Intrinsic::sqrt>(m_Value(Y))))) {
if (I.hasNoNaNs() && match(Op0, m_OneUse(m_Sqrt(m_Value(X)))) &&
match(Op1, m_OneUse(m_Sqrt(m_Value(Y))))) {
Value *XY = Builder.CreateFMulFMF(X, Y, &I);
Value *Sqrt = Builder.CreateUnaryIntrinsic(Intrinsic::sqrt, XY, &I);
return replaceInstUsesWith(I, Sqrt);
@ -545,11 +544,11 @@ Instruction *InstCombinerImpl::visitFMul(BinaryOperator &I) {
// has the necessary (reassoc) fast-math-flags.
if (I.hasNoSignedZeros() &&
match(Op0, (m_FDiv(m_SpecificFP(1.0), m_Value(Y)))) &&
match(Y, m_Intrinsic<Intrinsic::sqrt>(m_Value(X))) && Op1 == X)
match(Y, m_Sqrt(m_Value(X))) && Op1 == X)
return BinaryOperator::CreateFDivFMF(X, Y, &I);
if (I.hasNoSignedZeros() &&
match(Op1, (m_FDiv(m_SpecificFP(1.0), m_Value(Y)))) &&
match(Y, m_Intrinsic<Intrinsic::sqrt>(m_Value(X))) && Op0 == X)
match(Y, m_Sqrt(m_Value(X))) && Op0 == X)
return BinaryOperator::CreateFDivFMF(X, Y, &I);
// Like the similar transform in instsimplify, this requires 'nsz' because
@ -558,14 +557,12 @@ Instruction *InstCombinerImpl::visitFMul(BinaryOperator &I) {
Op0->hasNUses(2)) {
// Peek through fdiv to find squaring of square root:
// (X / sqrt(Y)) * (X / sqrt(Y)) --> (X * X) / Y
if (match(Op0, m_FDiv(m_Value(X),
m_Intrinsic<Intrinsic::sqrt>(m_Value(Y))))) {
if (match(Op0, m_FDiv(m_Value(X), m_Sqrt(m_Value(Y))))) {
Value *XX = Builder.CreateFMulFMF(X, X, &I);
return BinaryOperator::CreateFDivFMF(XX, Y, &I);
}
// (sqrt(Y) / X) * (sqrt(Y) / X) --> Y / (X * X)
if (match(Op0, m_FDiv(m_Intrinsic<Intrinsic::sqrt>(m_Value(Y)),
m_Value(X)))) {
if (match(Op0, m_FDiv(m_Sqrt(m_Value(Y)), m_Value(X)))) {
Value *XX = Builder.CreateFMulFMF(X, X, &I);
return BinaryOperator::CreateFDivFMF(Y, XX, &I);
}