Index: lib/Transforms/InstCombine/InstCombineMulDivRem.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineMulDivRem.cpp +++ lib/Transforms/InstCombine/InstCombineMulDivRem.cpp @@ -531,6 +531,11 @@ } } + // sqrt(X) * sqrt(X) -> X + if (AllowReassociate && (Op0 == Op1)) + if (IntrinsicInst *II = dyn_cast(Op0)) + if (II->getIntrinsicID() == Intrinsic::sqrt) + return ReplaceInstUsesWith(I, II->getOperand(0)); // Under unsafe algebra do: // X * log2(0.5*Y) = X*log2(Y) - X Index: test/Transforms/InstCombine/fmul.ll =================================================================== --- test/Transforms/InstCombine/fmul.ll +++ test/Transforms/InstCombine/fmul.ll @@ -123,3 +123,29 @@ ; CHECK-NOT: fadd float ; CHECK: fadd fast float } + +; PR21126: http://llvm.org/bugs/show_bug.cgi?id=21126 +; With unsafe math, sqrt(X) * sqrt(X) is just X. +declare double @llvm.sqrt.f64(double) +attributes #0 = { "unsafe-fp-math"="true" } + +define double @sqrt_squared1(double %f) #0 { + %sqrt = call double @llvm.sqrt.f64(double %f) + %mul = fmul fast double %sqrt, %sqrt + ret double %mul +; CHECK-LABEL: @sqrt_squared1( +; CHECK-NEXT: ret double %f +} + +; With unsafe math, sqrt(X) * sqrt(X) is just X, +; but make sure another use of the sqrt is intact. +define double @sqrt_squared2(double %f) #0 { + %sqrt = call double @llvm.sqrt.f64(double %f) + %mul1 = fmul fast double %sqrt, %sqrt + %mul2 = fmul fast double %mul1, %sqrt + ret double %mul2 +; CHECK-LABEL: @sqrt_squared2( +; CHECK-NEXT: %sqrt = call double @llvm.sqrt.f64(double %f) +; CHECK-NEXT: %mul2 = fmul fast double %sqrt, %f +; CHECK-NEXT: ret double %mul2 +}