Index: llvm/lib/Analysis/InstructionSimplify.cpp =================================================================== --- llvm/lib/Analysis/InstructionSimplify.cpp +++ llvm/lib/Analysis/InstructionSimplify.cpp @@ -4890,6 +4890,11 @@ return ConstantFP::get(Op0->getType(), -1.0); } + // X/sqrt(X) = sqrt(X) + if (match(Op1, m_Intrinsic(m_Specific(Op0))) && + FMF.allowReassoc() && FMF.noNaNs() && FMF.noSignedZeros()) + return Op1; + return nullptr; } Index: llvm/test/Transforms/InstSimplify/fdiv.ll =================================================================== --- llvm/test/Transforms/InstSimplify/fdiv.ll +++ llvm/test/Transforms/InstSimplify/fdiv.ll @@ -26,6 +26,17 @@ ret double %d } +declare double @llvm.sqrt.f64(double) #1 +define double @sqrt_fdiv_common_operand(double %x) { +; CHECK-LABEL: @sqrt_fdiv_common_operand( +; CHECK-NEXT: %1 = tail call fast double @llvm.sqrt.f64(double %x) +; CHECK-NEXT: ret double %1 +; + %1 = tail call fast double @llvm.sqrt.f64(double %x) + %2 = fdiv reassoc nnan fast double %x, %1 + ret double %1 +} + ; Negative test - the fdiv must be reassociative and not allow NaNs. define double @fmul_fdiv_common_operand_too_strict(double %x, double %y) {