[llvm] [Reassociate] Resolve FIXME: No Lowering of FNeg to FMul unless it is safe (PR #85252)

via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 14 10:47:45 PDT 2024


https://github.com/AtariDreams updated https://github.com/llvm/llvm-project/pull/85252

>From 7c74256c0bff63392f18db18a86793de8c73d9cb Mon Sep 17 00:00:00 2001
From: Rose <gfunni234 at gmail.com>
Date: Thu, 14 Mar 2024 11:43:14 -0400
Subject: [PATCH 1/2] [Scalar] Resolve FIXME: No Lowering of FNeg to FMul
 unless it is safe or flags are specified

---
 llvm/lib/Transforms/Scalar/Reassociate.cpp | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/Transforms/Scalar/Reassociate.cpp b/llvm/lib/Transforms/Scalar/Reassociate.cpp
index d91320863e241d..ee4492c012382b 100644
--- a/llvm/lib/Transforms/Scalar/Reassociate.cpp
+++ b/llvm/lib/Transforms/Scalar/Reassociate.cpp
@@ -287,11 +287,16 @@ static Instruction *CreateNeg(Value *S1, const Twine &Name,
 static BinaryOperator *LowerNegateToMultiply(Instruction *Neg) {
   assert((isa<UnaryOperator>(Neg) || isa<BinaryOperator>(Neg)) &&
          "Expected a Negate!");
-  // FIXME: It's not safe to lower a unary FNeg into a FMul by -1.0.
   unsigned OpNo = isa<BinaryOperator>(Neg) ? 1 : 0;
   Type *Ty = Neg->getType();
-  Constant *NegOne = Ty->isIntOrIntVectorTy() ?
-    ConstantInt::getAllOnesValue(Ty) : ConstantFP::get(Ty, -1.0);
+  // Only lower to FMul if the operation is not a unary FNeg or Neg has the
+  // correct flags.
+  if (Ty->isFloatingPointTy() && isa<UnaryOperator>(Neg) &&
+      !hasFPAssociativeFlags(Neg))
+    return nullptr;
+
+  Constant *NegOne = Ty->isIntOrIntVectorTy() ? ConstantInt::getAllOnesValue(Ty)
+                                              : ConstantFP::get(Ty, -1.0);
 
   BinaryOperator *Res =
       CreateMul(Neg->getOperand(OpNo), NegOne, "", Neg->getIterator(), Neg);

>From 85edaf4b702e06240477db9c17ae181474288cde Mon Sep 17 00:00:00 2001
From: AtariDreams <83477269+AtariDreams at users.noreply.github.com>
Date: Thu, 14 Mar 2024 13:47:38 -0400
Subject: [PATCH 2/2] Update llvm/lib/Transforms/Scalar/Reassociate.cpp

Co-authored-by: Yingwei Zheng <dtcxzyw at qq.com>
---
 llvm/lib/Transforms/Scalar/Reassociate.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Transforms/Scalar/Reassociate.cpp b/llvm/lib/Transforms/Scalar/Reassociate.cpp
index ee4492c012382b..8e68d8cbe626bb 100644
--- a/llvm/lib/Transforms/Scalar/Reassociate.cpp
+++ b/llvm/lib/Transforms/Scalar/Reassociate.cpp
@@ -291,8 +291,8 @@ static BinaryOperator *LowerNegateToMultiply(Instruction *Neg) {
   Type *Ty = Neg->getType();
   // Only lower to FMul if the operation is not a unary FNeg or Neg has the
   // correct flags.
-  if (Ty->isFloatingPointTy() && isa<UnaryOperator>(Neg) &&
-      !hasFPAssociativeFlags(Neg))
+  if (Ty->isFPOrFPVectorTy() && isa<UnaryOperator>(Neg) &&
+      !Neg->hasNoNaNs())
     return nullptr;
 
   Constant *NegOne = Ty->isIntOrIntVectorTy() ? ConstantInt::getAllOnesValue(Ty)



More information about the llvm-commits mailing list