/// This eliminates floating-point negation in either 'fneg(X)' or
/// 'fsub(-0.0, X)' form by combining into a constant operand.
static Instruction *foldFNegIntoConstant(Instruction &I) {
+ // This is limited with one-use because fneg is assumed better for
+ // reassociation and cheaper in codegen than fmul/fdiv.
+ // TODO: Should the m_OneUse restriction be removed?
+ Instruction *FNegOp;
+ if (!match(&I, m_FNeg(m_OneUse(m_Instruction(FNegOp)))))
+ return nullptr;
+
Value *X;
Constant *C;
- // Fold negation into constant operand. This is limited with one-use because
- // fneg is assumed better for analysis and cheaper in codegen than fmul/fdiv.
+ // Fold negation into constant operand.
// -(X * C) --> X * (-C)
- // FIXME: It's arguable whether these should be m_OneUse or not. The current
- // belief is that the FNeg allows for better reassociation opportunities.
- if (match(&I, m_FNeg(m_OneUse(m_FMul(m_Value(X), m_Constant(C))))))
+ if (match(FNegOp, m_FMul(m_Value(X), m_Constant(C))))
return BinaryOperator::CreateFMulFMF(X, ConstantExpr::getFNeg(C), &I);
// -(X / C) --> X / (-C)
- if (match(&I, m_FNeg(m_OneUse(m_FDiv(m_Value(X), m_Constant(C))))))
+ if (match(FNegOp, m_FDiv(m_Value(X), m_Constant(C))))
return BinaryOperator::CreateFDivFMF(X, ConstantExpr::getFNeg(C), &I);
// -(C / X) --> (-C) / X
- if (match(&I, m_FNeg(m_OneUse(m_FDiv(m_Constant(C), m_Value(X))))))
+ if (match(FNegOp, m_FDiv(m_Constant(C), m_Value(X))))
return BinaryOperator::CreateFDivFMF(ConstantExpr::getFNeg(C), X, &I);
// With NSZ [ counter-example with -0.0: -(-0.0 + 0.0) != 0.0 + -0.0 ]:
// -(X + C) --> -X + -C --> -C - X
- if (I.hasNoSignedZeros() &&
- match(&I, m_FNeg(m_OneUse(m_FAdd(m_Value(X), m_Constant(C))))))
+ if (I.hasNoSignedZeros() && match(FNegOp, m_FAdd(m_Value(X), m_Constant(C))))
return BinaryOperator::CreateFSubFMF(ConstantExpr::getFNeg(C), X, &I);
return nullptr;