summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp6
-rw-r--r--llvm/test/Transforms/InstCombine/fmul.ll13
2 files changed, 17 insertions, 2 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
index a9e3e5f00f6..73172877d9e 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -374,11 +374,13 @@ Instruction *InstCombiner::visitFMul(BinaryOperator &I) {
return BinaryOperator::CreateFMulFMF(X, ConstantExpr::getFNeg(C), &I);
// Sink negation: -X * Y --> -(X * Y)
- if (match(Op0, m_OneUse(m_FNeg(m_Value(X)))))
+ // But don't transform constant expressions because there's an inverse fold.
+ if (match(Op0, m_OneUse(m_FNeg(m_Value(X)))) && !isa<ConstantExpr>(Op0))
return BinaryOperator::CreateFNegFMF(Builder.CreateFMulFMF(X, Op1, &I), &I);
// Sink negation: Y * -X --> -(X * Y)
- if (match(Op1, m_OneUse(m_FNeg(m_Value(X)))))
+ // But don't transform constant expressions because there's an inverse fold.
+ if (match(Op1, m_OneUse(m_FNeg(m_Value(X)))) && !isa<ConstantExpr>(Op1))
return BinaryOperator::CreateFNegFMF(Builder.CreateFMulFMF(X, Op0, &I), &I);
// fabs(X) * fabs(X) -> X * X
diff --git a/llvm/test/Transforms/InstCombine/fmul.ll b/llvm/test/Transforms/InstCombine/fmul.ll
index fcf0941c867..99b67dfa845 100644
--- a/llvm/test/Transforms/InstCombine/fmul.ll
+++ b/llvm/test/Transforms/InstCombine/fmul.ll
@@ -981,3 +981,16 @@ define float @fmul_fdiv_factor_extra_use(float %x, float %y) {
%mul = fmul reassoc float %div, %y
ret float %mul
}
+
+; Avoid infinite looping by moving negation out of a constant expression.
+
+@g = external global {[2 x i8*]}, align 1
+
+define double @fmul_negated_constant_expression(double %x) {
+; CHECK-LABEL: @fmul_negated_constant_expression(
+; CHECK-NEXT: [[R:%.*]] = fmul double [[X:%.*]], fsub (double -0.000000e+00, double bitcast (i64 ptrtoint (i8** getelementptr inbounds ({ [2 x i8*] }, { [2 x i8*] }* @g, i64 0, inrange i32 0, i64 2) to i64) to double))
+; CHECK-NEXT: ret double [[R]]
+;
+ %r = fmul double %x, fsub (double -0.000000e+00, double bitcast (i64 ptrtoint (i8** getelementptr inbounds ({ [2 x i8*] }, { [2 x i8*] }* @g, i64 0, inrange i32 0, i64 2) to i64) to double))
+ ret double %r
+}
OpenPOWER on IntegriCloud