[llvm] r325965 - [InstSimplify] sqrt(X) * sqrt(X) --> X
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Fri Feb 23 14:20:13 PST 2018
Author: spatel
Date: Fri Feb 23 14:20:13 2018
New Revision: 325965
URL: http://llvm.org/viewvc/llvm-project?rev=325965&view=rev
Log:
[InstSimplify] sqrt(X) * sqrt(X) --> X
This was misplaced in InstCombine. We can loosen the FMF as a follow-up step.
Modified:
llvm/trunk/lib/Analysis/InstructionSimplify.cpp
llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
llvm/trunk/test/Transforms/InstCombine/fmul.ll
llvm/trunk/test/Transforms/InstSimplify/fast-math.ll
Modified: llvm/trunk/lib/Analysis/InstructionSimplify.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/InstructionSimplify.cpp?rev=325965&r1=325964&r2=325965&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/InstructionSimplify.cpp (original)
+++ llvm/trunk/lib/Analysis/InstructionSimplify.cpp Fri Feb 23 14:20:13 2018
@@ -4251,6 +4251,12 @@ static Value *SimplifyFMulInst(Value *Op
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;
}
Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp?rev=325965&r1=325964&r2=325965&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp Fri Feb 23 14:20:13 2018
@@ -620,10 +620,6 @@ Instruction *InstCombiner::visitFMul(Bin
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),
Modified: llvm/trunk/test/Transforms/InstCombine/fmul.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/fmul.ll?rev=325965&r1=325964&r2=325965&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/fmul.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/fmul.ll Fri Feb 23 14:20:13 2018
@@ -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'
Modified: llvm/trunk/test/Transforms/InstSimplify/fast-math.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstSimplify/fast-math.ll?rev=325965&r1=325964&r2=325965&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstSimplify/fast-math.ll (original)
+++ llvm/trunk/test/Transforms/InstSimplify/fast-math.ll Fri Feb 23 14:20:13 2018
@@ -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
+}
+
More information about the llvm-commits
mailing list