From 91bb7750870147c863145c7241c984b3d3505b74 Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Fri, 16 Feb 2018 17:52:32 +0000 Subject: [PATCH] [InstCombine] clean up fdiv-with-fdiv folds; NFCI llvm-svn: 325366 --- .../InstCombine/InstCombineMulDivRem.cpp | 51 +++++++++---------- 1 file changed, 23 insertions(+), 28 deletions(-) diff --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp index cec63957002e..08d83a1e4d0e 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp @@ -1426,36 +1426,31 @@ Instruction *InstCombiner::visitFDiv(BinaryOperator &I) { if (AllowReassociate) { Value *X, *Y; - Value *NewInst = nullptr; - Instruction *SimpR = nullptr; - - if (match(Op0, m_OneUse(m_FDiv(m_Value(X), m_Value(Y))))) { - // (X/Y) / Z => X / (Y*Z) - if (!isa(Y) || !isa(Op1)) { - NewInst = Builder.CreateFMul(Y, Op1); - if (Instruction *RI = dyn_cast(NewInst)) { - FastMathFlags Flags = I.getFastMathFlags(); - Flags &= cast(Op0)->getFastMathFlags(); - RI->setFastMathFlags(Flags); - } - SimpR = BinaryOperator::CreateFDiv(X, NewInst); - } - } else if (match(Op1, m_OneUse(m_FDiv(m_Value(X), m_Value(Y))))) { - // Z / (X/Y) => Z*Y / X - if (!isa(Y) || !isa(Op0)) { - NewInst = Builder.CreateFMul(Op0, Y); - if (Instruction *RI = dyn_cast(NewInst)) { - FastMathFlags Flags = I.getFastMathFlags(); - Flags &= cast(Op1)->getFastMathFlags(); - RI->setFastMathFlags(Flags); - } - SimpR = BinaryOperator::CreateFDiv(NewInst, X); + if (match(Op0, m_OneUse(m_FDiv(m_Value(X), m_Value(Y)))) && + (!isa(Y) || !isa(Op1))) { + // (X / Y) / Z => X / (Y * Z) + Value *YZ = Builder.CreateFMul(Y, Op1); + if (auto *YZInst = dyn_cast(YZ)) { + FastMathFlags FMFIntersect = I.getFastMathFlags(); + FMFIntersect &= cast(Op0)->getFastMathFlags(); + YZInst->setFastMathFlags(FMFIntersect); } + Instruction *NewDiv = BinaryOperator::CreateFDiv(X, YZ); + NewDiv->setFastMathFlags(I.getFastMathFlags()); + return NewDiv; } - - if (NewInst) { - SimpR->setFastMathFlags(I.getFastMathFlags()); - return SimpR; + if (match(Op1, m_OneUse(m_FDiv(m_Value(X), m_Value(Y)))) && + (!isa(Y) || !isa(Op0))) { + // Z / (X / Y) => (Y * Z) / X + Value *YZ = Builder.CreateFMul(Y, Op0); + if (auto *YZInst = dyn_cast(YZ)) { + FastMathFlags FMFIntersect = I.getFastMathFlags(); + FMFIntersect &= cast(Op1)->getFastMathFlags(); + YZInst->setFastMathFlags(FMFIntersect); + } + Instruction *NewDiv = BinaryOperator::CreateFDiv(YZ, X); + NewDiv->setFastMathFlags(I.getFastMathFlags()); + return NewDiv; } }