1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-10-19 02:52:53 +02:00

[InstSimplify] sqrt(X) * sqrt(X) --> X

This was misplaced in InstCombine. We can loosen the FMF as a follow-up step.

llvm-svn: 325965
This commit is contained in:
Sanjay Patel 2018-02-23 22:20:13 +00:00
parent a7e76ec2f3
commit cba594407d
4 changed files with 21 additions and 15 deletions

View File

@ -4251,6 +4251,12 @@ static Value *SimplifyFMulInst(Value *Op0, Value *Op1, FastMathFlags FMF,
if (FMF.noNaNs() && FMF.noSignedZeros() && match(Op1, m_AnyZero()))
return Op1;
// sqrt(X) * sqrt(X) --> X
Value *X;
if (FMF.isFast() && Op0 == Op1 &&
match(Op0, m_Intrinsic<Intrinsic::sqrt>(m_Value(X))))
return X;
return nullptr;
}

View File

@ -620,10 +620,6 @@ Instruction *InstCombiner::visitFMul(BinaryOperator &I) {
if (Op0 == Op1) {
if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Op0)) {
// sqrt(X) * sqrt(X) -> X
if (AllowReassociate && II->getIntrinsicID() == Intrinsic::sqrt)
return replaceInstUsesWith(I, II->getOperand(0));
// fabs(X) * fabs(X) -> X * X
if (II->getIntrinsicID() == Intrinsic::fabs) {
Instruction *FMulVal = BinaryOperator::CreateFMul(II->getOperand(0),

View File

@ -172,19 +172,8 @@ define float @test11(float %x, float %y) {
ret float %c
}
; PR21126: http://llvm.org/bugs/show_bug.cgi?id=21126
; With unsafe/fast math, sqrt(X) * sqrt(X) is just X.
declare double @llvm.sqrt.f64(double)
define double @sqrt_squared1(double %f) {
; CHECK-LABEL: @sqrt_squared1(
; CHECK-NEXT: ret double [[F:%.*]]
;
%sqrt = call double @llvm.sqrt.f64(double %f)
%mul = fmul fast double %sqrt, %sqrt
ret double %mul
}
; With unsafe/fast math, sqrt(X) * sqrt(X) is just X,
; but make sure another use of the sqrt is intact.
; Note that the remaining fmul is altered but is not 'fast'

View File

@ -203,3 +203,18 @@ define float @fdiv_neg_swapped2(float %f) {
%div = fdiv nnan float %f, %neg
ret float %div
}
; PR21126: http://llvm.org/bugs/show_bug.cgi?id=21126
; With unsafe/fast math, sqrt(X) * sqrt(X) is just X.
declare double @llvm.sqrt.f64(double)
define double @sqrt_squared(double %f) {
; CHECK-LABEL: @sqrt_squared(
; CHECK-NEXT: ret double [[F:%.*]]
;
%sqrt = call double @llvm.sqrt.f64(double %f)
%mul = fmul fast double %sqrt, %sqrt
ret double %mul
}