[llvm] r325366 - [InstCombine] clean up fdiv-with-fdiv folds; NFCI

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 16 09:52:32 PST 2018


Author: spatel
Date: Fri Feb 16 09:52:32 2018
New Revision: 325366

URL: http://llvm.org/viewvc/llvm-project?rev=325366&view=rev
Log:
[InstCombine] clean up fdiv-with-fdiv folds; NFCI

Modified:
    llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp?rev=325366&r1=325365&r2=325366&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp Fri Feb 16 09:52:32 2018
@@ -1426,36 +1426,31 @@ Instruction *InstCombiner::visitFDiv(Bin
 
   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<Constant>(Y) || !isa<Constant>(Op1)) {
-        NewInst = Builder.CreateFMul(Y, Op1);
-        if (Instruction *RI = dyn_cast<Instruction>(NewInst)) {
-          FastMathFlags Flags = I.getFastMathFlags();
-          Flags &= cast<Instruction>(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<Constant>(Y) || !isa<Constant>(Op0)) {
-        NewInst = Builder.CreateFMul(Op0, Y);
-        if (Instruction *RI = dyn_cast<Instruction>(NewInst)) {
-          FastMathFlags Flags = I.getFastMathFlags();
-          Flags &= cast<Instruction>(Op1)->getFastMathFlags();
-          RI->setFastMathFlags(Flags);
-        }
-        SimpR = BinaryOperator::CreateFDiv(NewInst, X);
+    if (match(Op0, m_OneUse(m_FDiv(m_Value(X), m_Value(Y)))) &&
+        (!isa<Constant>(Y) || !isa<Constant>(Op1))) {
+      // (X / Y) / Z => X / (Y * Z)
+      Value *YZ = Builder.CreateFMul(Y, Op1);
+      if (auto *YZInst = dyn_cast<Instruction>(YZ)) {
+        FastMathFlags FMFIntersect = I.getFastMathFlags();
+        FMFIntersect &= cast<Instruction>(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<Constant>(Y) || !isa<Constant>(Op0))) {
+      // Z / (X / Y) => (Y * Z) / X
+      Value *YZ = Builder.CreateFMul(Y, Op0);
+      if (auto *YZInst = dyn_cast<Instruction>(YZ)) {
+        FastMathFlags FMFIntersect = I.getFastMathFlags();
+        FMFIntersect &= cast<Instruction>(Op1)->getFastMathFlags();
+        YZInst->setFastMathFlags(FMFIntersect);
+      }
+      Instruction *NewDiv = BinaryOperator::CreateFDiv(YZ, X);
+      NewDiv->setFastMathFlags(I.getFastMathFlags());
+      return NewDiv;
     }
   }
 




More information about the llvm-commits mailing list