[llvm] [Reassociate]Keep fmul/fadd pairs together for fma (PR #215873)

Krzysztof Drewniak via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 20 11:25:52 PDT 2026


================
@@ -178,6 +178,28 @@ static BinaryOperator *isReassociableOp(Value *V, unsigned Opcode1,
   return nullptr;
 }
 
+/// Return the fmul operand if V is a one-use fadd with a single one-use fmul
+/// operand, both allowing contraction. Such pairs can be fused into a single
+/// fma, so they are kept together as leaves of the enclosing expression tree
+/// instead of being linearized into it.
+static BinaryOperator *isFMulAddCandidate(Value *V) {
+  BinaryOperator *FAdd = isReassociableOp(V, Instruction::FAdd);
+  if (!FAdd || !FAdd->hasAllowContract())
+    return nullptr;
+  auto ContractableFMul = [](BinaryOperator *&FMul) {
+    return m_CombineAnd(m_AllowContract(m_OneUse(m_FMul(m_Value(), m_Value()))),
+                        m_BinOp(FMul));
+  };
+  BinaryOperator *Mul0 = nullptr, *Mul1 = nullptr;
+  match(FAdd->getOperand(0), ContractableFMul(Mul0));
+  match(FAdd->getOperand(1), ContractableFMul(Mul1));
----------------
krzysz00 wrote:

... isn't there a way to make this match commutative?

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


More information about the llvm-commits mailing list