[InstCombine] allow fdiv folds with less than fully 'fast' ops

Note: gcc appears to allow this fold with -freciprocal-math alone, 
but clang/llvm require more than that with this patch. The wording
in the definitions seems fuzzy enough that it could go either way,
but we'll err on the conservative side of FMF interpretation.

This patch also changes the newly created fmul to have FMF propagated
by the last fdiv rather than intersecting the FMF of the fdivs. This
matches the behavior of other folds near here. The new fmul is only 
used to produce an intermediate op for the final fdiv result, so it
shouldn't be any stricter than that result. The previous behavior
could result in dropping FMF via other folds in instcombine or CSE.

Differential Revision: https://reviews.llvm.org/D43398

llvm-svn: 326098
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
index 412af4b..815af57 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -1347,28 +1347,18 @@
       if (Instruction *R = FoldOpIntoSelect(I, SI))
         return R;
 
-  if (I.isFast()) {
+  if (I.hasAllowReassoc() && I.hasAllowReciprocal()) {
     Value *X, *Y;
     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);
-      }
+      Value *YZ = Builder.CreateFMulFMF(Y, Op1, &I);
       return BinaryOperator::CreateFDivFMF(X, YZ, &I);
     }
     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);
-      }
+      Value *YZ = Builder.CreateFMulFMF(Y, Op0, &I);
       return BinaryOperator::CreateFDivFMF(YZ, X, &I);
     }
   }