[llvm] [Scalar] 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 08:44:34 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: AtariDreams (AtariDreams)

<details>
<summary>Changes</summary>

Or association flags are specified.

---
Full diff: https://github.com/llvm/llvm-project/pull/85252.diff


1 Files Affected:

- (modified) llvm/lib/Transforms/Scalar/Reassociate.cpp (+9-3) 


``````````diff
diff --git a/llvm/lib/Transforms/Scalar/Reassociate.cpp b/llvm/lib/Transforms/Scalar/Reassociate.cpp
index d91320863e241d..f44608624cf3df 100644
--- a/llvm/lib/Transforms/Scalar/Reassociate.cpp
+++ b/llvm/lib/Transforms/Scalar/Reassociate.cpp
@@ -287,11 +287,17 @@ 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);

``````````

</details>


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


More information about the llvm-commits mailing list