[llvm] [InstCombine] Resolve TODO: nnan nsz X / -0.0 -> copysign(inf, X) (PR #79766)

via llvm-commits llvm-commits at lists.llvm.org
Sun Jan 28 12:15:07 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: AtariDreams (AtariDreams)

<details>
<summary>Changes</summary>



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


2 Files Affected:

- (modified) llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp (+11-1) 
- (modified) llvm/test/Transforms/InstCombine/fdiv.ll (+19) 


``````````diff
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
index 6c3adf00c189a8e..3f2ec4eb6e94bec 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -1600,7 +1600,17 @@ Instruction *InstCombinerImpl::foldFDivConstantDivisor(BinaryOperator &I) {
   // nnan X / +0.0 -> copysign(inf, X)
   if (I.hasNoNaNs() && match(I.getOperand(1), m_Zero())) {
     IRBuilder<> B(&I);
-    // TODO: nnan nsz X / -0.0 -> copysign(inf, X)
+    CallInst *CopySign = B.CreateIntrinsic(
+        Intrinsic::copysign, {C->getType()},
+        {ConstantFP::getInfinity(I.getType()), I.getOperand(0)}, &I);
+    CopySign->takeName(&I);
+    return replaceInstUsesWith(I, CopySign);
+  } else if (I.hasNoNaNs() && I.hasNoSignedZeros() &&
+             match(I.getOperand(1),
+                   m_Specific(ConstantFP::get(Type::getDoubleTy(I.getContext()),
+                                              -0.0)))) {
+    // Handle nnan nsz X / -0.0 -> copysign(inf, X)
+    IRBuilder<> B(&I);
     CallInst *CopySign = B.CreateIntrinsic(
         Intrinsic::copysign, {C->getType()},
         {ConstantFP::getInfinity(I.getType()), I.getOperand(0)}, &I);
diff --git a/llvm/test/Transforms/InstCombine/fdiv.ll b/llvm/test/Transforms/InstCombine/fdiv.ll
index c81a9ba6d4215d9..e0aeb6840223337 100644
--- a/llvm/test/Transforms/InstCombine/fdiv.ll
+++ b/llvm/test/Transforms/InstCombine/fdiv.ll
@@ -992,3 +992,22 @@ define float @fdiv_nnan_neg_zero_f32(float %x) {
   %fdiv = fdiv nnan float %x, -0.0
   ret float %fdiv
 }
+
+define double @test_positive_zero(double %X) {
+; CHECK-LABEL: @test_positive_zero(
+; CHECK-NEXT:    [[TMP1:%.*]] = call nnan nsz double @llvm.copysign.f64(double 0x7FF0000000000000, double [[X:%.*]])
+; CHECK-NEXT:    ret double [[TMP1]]
+;
+  %1 = fdiv nnan nsz double %X, 0.0
+  ret double %1
+}
+
+define double @test_negative_zero(double %X) {
+; CHECK-LABEL: @test_negative_zero(
+; CHECK-NEXT:    [[TMP1:%.*]] = call nnan nsz double @llvm.copysign.f64(double 0x7FF0000000000000, double [[X:%.*]])
+; CHECK-NEXT:    ret double [[TMP1]]
+;
+  %1 = fdiv nnan nsz double %X, -0.0
+  ret double %1
+}
+

``````````

</details>


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


More information about the llvm-commits mailing list