[PATCH] D82778: [InstCombine] fma x, y, 0 -> fmul x, y

Dave Green via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 29 09:10:18 PDT 2020


dmgreen created this revision.
dmgreen added reviewers: spatel, arsenm, lebedev.ri, fhahn.
Herald added subscribers: hiraditya, wdng.
Herald added a project: LLVM.

If the addend of the fma is zero, common sense would suggest that we can convert `fma x, y, 0.0` to `fmul x, y`. This comes up with some user code that was expecting the first fma in an unrolled loop to simplify to a fmul.

Floating point often does not follow naive common sense though. Alive suggests that this should be guarded by nsz (as `fadd -0.0, 0.0 = 0.0`). However it also did not complete running, so I have only validated this against `fadd nsz (fmul(x,y), 0) -> fmul nsz (x,y)`, not with `fma nsz (x, y, 0.0) -> fmul nsz (x, y)`.


https://reviews.llvm.org/D82778

Files:
  llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
  llvm/test/Transforms/InstCombine/fma.ll


Index: llvm/test/Transforms/InstCombine/fma.ll
===================================================================
--- llvm/test/Transforms/InstCombine/fma.ll
+++ llvm/test/Transforms/InstCombine/fma.ll
@@ -372,7 +372,7 @@
 
 define float @fma_x_y_0_nsz(float %x, float %y) {
 ; CHECK-LABEL: @fma_x_y_0_nsz(
-; CHECK-NEXT:    [[FMA:%.*]] = call nsz float @llvm.fma.f32(float [[X:%.*]], float [[Y:%.*]], float 0.000000e+00)
+; CHECK-NEXT:    [[FMA:%.*]] = fmul nsz float [[X:%.*]], [[Y:%.*]]
 ; CHECK-NEXT:    ret float [[FMA]]
 ;
   %fma = call nsz float @llvm.fma.f32(float %x, float %y, float 0.0)
@@ -390,7 +390,7 @@
 
 define <8 x half> @fma_x_y_0_nsz_v(<8 x half> %x, <8 x half> %y) {
 ; CHECK-LABEL: @fma_x_y_0_nsz_v(
-; CHECK-NEXT:    [[FMA:%.*]] = call nsz <8 x half> @llvm.fma.v8f16(<8 x half> [[X:%.*]], <8 x half> [[Y:%.*]], <8 x half> zeroinitializer)
+; CHECK-NEXT:    [[FMA:%.*]] = fmul nsz <8 x half> [[X:%.*]], [[Y:%.*]]
 ; CHECK-NEXT:    ret <8 x half> [[FMA]]
 ;
   %fma = call nsz <8 x half> @llvm.fma.v8f16(<8 x half> %x, <8 x half> %y, <8 x half> zeroinitializer)
Index: llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -2379,6 +2379,14 @@
       return FAdd;
     }
 
+    // fma x, y, 0 -> fmul x, y
+    if (II->getFastMathFlags().noSignedZeros() &&
+        match(II->getArgOperand(2), m_Zero())) {
+      auto *FMul = BinaryOperator::CreateFMul(Src0, Src1);
+      FMul->copyFastMathFlags(II);
+      return FMul;
+    }
+
     break;
   }
   case Intrinsic::copysign: {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D82778.274101.patch
Type: text/x-patch
Size: 1686 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200629/f6ac8a40/attachment-0001.bin>


More information about the llvm-commits mailing list