[llvm] Update foldFMulReassoc to respect absent fast-math flags (PR #88589)

Andy Kaylor via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 15 14:31:34 PDT 2024


================
@@ -636,26 +636,43 @@ Instruction *InstCombinerImpl::foldFMulReassoc(BinaryOperator &I) {
   // expression.
   if (match(Op1, m_Constant(C)) && C->isFiniteNonZeroFP()) {
     Constant *C1;
-    if (match(Op0, m_OneUse(m_FDiv(m_Constant(C1), m_Value(X))))) {
+    if (match(Op0,
+              m_AllowReassoc(m_OneUse(m_FDiv(m_Constant(C1), m_Value(X)))))) {
       // (C1 / X) * C --> (C * C1) / X
       Constant *CC1 =
           ConstantFoldBinaryOpOperands(Instruction::FMul, C, C1, DL);
-      if (CC1 && CC1->isNormalFP())
-        return BinaryOperator::CreateFDivFMF(CC1, X, &I);
+      if (CC1 && CC1->isNormalFP()) {
+        // Preserve only fast-math flags that were set on both of the original
+        // instructions
+        auto *NewDiv = BinaryOperator::CreateFDivFMF(CC1, X, &I);
+        NewDiv->andIRFlags(Op0);
----------------
andykaylor wrote:

Maybe it would be better to have a version of CreateFDivFMF and similar that took a FastMathFlags argument rather than an instruction to copy them from. Then we could have utility functions to create a union of fast-math flags from two or more FPMathOperator objects. I'll see what I can do with that.

https://github.com/llvm/llvm-project/pull/88589


More information about the llvm-commits mailing list