aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Transforms
diff options
context:
space:
mode:
authorSanjay Patel <spatel@rotateright.com>2021-06-07 13:05:04 -0400
committerSanjay Patel <spatel@rotateright.com>2021-06-07 13:22:49 -0400
commit4675beaa2181b6ccb1a37fd49b51a45490023974 (patch)
treeec3b986de83567fb55958256e4408777c362676d /llvm/lib/Transforms
parent519e98cd9af0d4b47a2daf0899b69d1254750c13 (diff)
downloadllvm-4675beaa2181b6ccb1a37fd49b51a45490023974.zip
llvm-4675beaa2181b6ccb1a37fd49b51a45490023974.tar.gz
llvm-4675beaa2181b6ccb1a37fd49b51a45490023974.tar.bz2
[InstCombine] intersect nsz and ninf fast-math-flags (FMF) for fneg(fdiv) fold
https://alive2.llvm.org/ce/z/3KPvih https://llvm.org/PR49654
Diffstat (limited to 'llvm/lib/Transforms')
-rw-r--r--llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp16
1 files changed, 13 insertions, 3 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
index 2e0d3bf..2ed616f 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -2143,9 +2143,19 @@ static Instruction *foldFNegIntoConstant(Instruction &I) {
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(FNegOp, m_FDiv(m_Constant(C), m_Value(X))))
- return BinaryOperator::CreateFDivFMF(ConstantExpr::getFNeg(C), X, &I);
-
+ if (match(FNegOp, m_FDiv(m_Constant(C), m_Value(X)))) {
+ Instruction *FDiv =
+ BinaryOperator::CreateFDivFMF(ConstantExpr::getFNeg(C), X, &I);
+
+ // Intersect 'nsz' and 'ninf' because those special value exceptions may not
+ // apply to the fdiv. Everything else propagates from the fneg.
+ // TODO: We could propagate nsz/ninf from fdiv alone?
+ FastMathFlags FMF = I.getFastMathFlags();
+ FastMathFlags OpFMF = FNegOp->getFastMathFlags();
+ FDiv->setHasNoSignedZeros(FMF.noSignedZeros() & OpFMF.noSignedZeros());
+ FDiv->setHasNoInfs(FMF.noInfs() & OpFMF.noInfs());
+ return FDiv;
+ }
// 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(FNegOp, m_FAdd(m_Value(X), m_Constant(C))))