[llvm] [InstCombine] Ensure Safe Handling of Flags in foldFNegIntoConstant (PR #94148)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 30 06:39:22 PDT 2024


================
@@ -2757,8 +2757,13 @@ static Instruction *foldFNegIntoConstant(Instruction &I, const DataLayout &DL) {
   // Fold negation into constant operand.
   // -(X * C) --> X * (-C)
   if (match(FNegOp, m_FMul(m_Value(X), m_Constant(C))))
-    if (Constant *NegC = ConstantFoldUnaryOpOperand(Instruction::FNeg, C, DL))
-      return BinaryOperator::CreateFMulFMF(X, NegC, &I);
+    if (Constant *NegC = ConstantFoldUnaryOpOperand(Instruction::FNeg, C, DL)) {
+      FastMathFlags FMF = I.getFastMathFlags();
+      FastMathFlags OpFMF = FNegOp->getFastMathFlags();
+      FastMathFlags Flag = FMF | OpFMF;
+      Flag.setNoInfs(FMF.noInfs() && OpFMF.noInfs());
----------------
arsenm wrote:

Should and the rewrite flags too (reassoc, contract, afn). We probably should have a helper for the rewrite flags 

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


More information about the llvm-commits mailing list