[PATCH] D136166: [InstCombine] fmul nnan X, 0.0 --> copysign(0.0, X)

Sanjay Patel via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 18 06:26:39 PDT 2022


spatel created this revision.
spatel added reviewers: arsenm, kpn.
Herald added subscribers: hiraditya, mcrosier.
Herald added a project: All.
spatel requested review of this revision.
Herald added subscribers: llvm-commits, wdng.
Herald added a project: LLVM.

I've had this Alive tab open in my browser for weeks, and the similar D136098 <https://reviews.llvm.org/D136098> reminded me of it:
https://alive2.llvm.org/ce/z/ybgM5F


https://reviews.llvm.org/D136166

Files:
  llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
  llvm/test/Transforms/InstCombine/fmul.ll


Index: llvm/test/Transforms/InstCombine/fmul.ll
===================================================================
--- llvm/test/Transforms/InstCombine/fmul.ll
+++ llvm/test/Transforms/InstCombine/fmul.ll
@@ -1203,22 +1203,26 @@
 
 define half @mul_zero_nnan(half %x) {
 ; CHECK-LABEL: @mul_zero_nnan(
-; CHECK-NEXT:    [[R:%.*]] = fmul nnan half [[X:%.*]], 0xH0000
-; CHECK-NEXT:    ret half [[R]]
+; CHECK-NEXT:    [[TMP1:%.*]] = call nnan half @llvm.copysign.f16(half 0xH0000, half [[X:%.*]])
+; CHECK-NEXT:    ret half [[TMP1]]
 ;
   %r = fmul nnan half %x, 0.0
   ret half %r
 }
 
+; poison propagates through vector elements
+
 define <2 x float> @mul_zero_nnan_vec_poison(<2 x float> %x) {
 ; CHECK-LABEL: @mul_zero_nnan_vec_poison(
-; CHECK-NEXT:    [[R:%.*]] = fmul nnan <2 x float> [[X:%.*]], <float 0.000000e+00, float poison>
-; CHECK-NEXT:    ret <2 x float> [[R]]
+; CHECK-NEXT:    [[TMP1:%.*]] = call nnan <2 x float> @llvm.copysign.v2f32(<2 x float> <float 0.000000e+00, float poison>, <2 x float> [[X:%.*]])
+; CHECK-NEXT:    ret <2 x float> [[TMP1]]
 ;
   %r = fmul nnan <2 x float> %x, <float 0.0, float poison>
   ret <2 x float> %r
 }
 
+; negative test - must have nnan
+
 define half @mul_zero(half %x) {
 ; CHECK-LABEL: @mul_zero(
 ; CHECK-NEXT:    [[R:%.*]] = fmul ninf nsz half [[X:%.*]], 0xH0000
@@ -1228,6 +1232,8 @@
   ret half %r
 }
 
+; TODO: This could be fneg+copysign.
+
 define half @mul_negzero_nnan(half %x) {
 ; CHECK-LABEL: @mul_negzero_nnan(
 ; CHECK-NEXT:    [[R:%.*]] = fmul nnan half [[X:%.*]], 0xH8000
Index: llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -521,6 +521,13 @@
   if (match(Op1, m_SpecificFP(-1.0)))
     return UnaryOperator::CreateFNegFMF(Op0, &I);
 
+  // With no-nans: X * 0.0 --> copysign(0.0, X)
+  if (I.hasNoNaNs() && match(Op1, m_PosZeroFP())) {
+    CallInst *CopySign = Builder.CreateIntrinsic(Intrinsic::copysign,
+                                                 {I.getType()}, {Op1, Op0}, &I);
+    return replaceInstUsesWith(I, CopySign);
+  }
+
   // -X * C --> X * -C
   Value *X, *Y;
   Constant *C;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D136166.468517.patch
Type: text/x-patch
Size: 2279 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221018/0a1bdfb4/attachment.bin>


More information about the llvm-commits mailing list