summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--llvm/lib/Analysis/InstructionSimplify.cpp6
-rw-r--r--llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp4
-rw-r--r--llvm/test/Transforms/InstCombine/fmul.ll11
-rw-r--r--llvm/test/Transforms/InstSimplify/fast-math.ll15
4 files changed, 21 insertions, 15 deletions
diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index a424e693842..eee6d531643 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -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;
}
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
index d5456cc532f..48fba0319fd 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -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),
diff --git a/llvm/test/Transforms/InstCombine/fmul.ll b/llvm/test/Transforms/InstCombine/fmul.ll
index 1c05475ba07..2eaa7c3e761 100644
--- a/llvm/test/Transforms/InstCombine/fmul.ll
+++ b/llvm/test/Transforms/InstCombine/fmul.ll
@@ -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'
diff --git a/llvm/test/Transforms/InstSimplify/fast-math.ll b/llvm/test/Transforms/InstSimplify/fast-math.ll
index f4f31236e5c..dbf17a2fcee 100644
--- a/llvm/test/Transforms/InstSimplify/fast-math.ll
+++ b/llvm/test/Transforms/InstSimplify/fast-math.ll
@@ -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
+}
+
OpenPOWER on IntegriCloud