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

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 15 12:22:23 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);
----------------
arsenm wrote:

This API is somewhat unfortunate. Is there a way to use the FastMathFlagGuard to avoid repeating this 3x? Probably not given CreateFDivFMF wants to take flags from an existing instruction 

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


More information about the llvm-commits mailing list