[llvm] r290860 - InstCombine: Add fma with constant transforms

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 2 20:32:35 PST 2017


Author: arsenm
Date: Mon Jan  2 22:32:35 2017
New Revision: 290860

URL: http://llvm.org/viewvc/llvm-project?rev=290860&view=rev
Log:
InstCombine: Add fma with constant transforms

DAGCombine already does these.

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

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp?rev=290860&r1=290859&r2=290860&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp Mon Jan  2 22:32:35 2017
@@ -1583,12 +1583,19 @@ Instruction *InstCombiner::visitCallInst
   }
   case Intrinsic::fma:
   case Intrinsic::fmuladd: {
-    Value *LHS = nullptr;
-    Value *RHS = nullptr;
-
     Value *Src0 = II->getArgOperand(0);
     Value *Src1 = II->getArgOperand(1);
 
+    // Canonicalize constants into the RHS.
+    if (isa<Constant>(Src0) && !isa<Constant>(Src1)) {
+      II->setArgOperand(0, Src1);
+      II->setArgOperand(1, Src0);
+      std::swap(Src0, Src1);
+    }
+
+    Value *LHS = nullptr;
+    Value *RHS = nullptr;
+
     // fma fneg(x), fneg(y), z -> fma x, y, z
     if (match(Src0, m_FNeg(m_Value(LHS))) &&
         match(Src1, m_FNeg(m_Value(RHS)))) {
@@ -1609,6 +1616,13 @@ Instruction *InstCombiner::visitCallInst
       return replaceInstUsesWith(*II, NewCall);
     }
 
+    // fma x, 1, z -> fadd x, z
+    if (match(Src1, m_FPOne())) {
+      Instruction *RI = BinaryOperator::CreateFAdd(Src0, II->getArgOperand(2));
+      RI->copyFastMathFlags(II);
+      return RI;
+    }
+
     break;
   }
   case Intrinsic::ppc_altivec_lvx:

Modified: llvm/trunk/test/Transforms/InstCombine/fma.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/fma.ll?rev=290860&r1=290859&r2=290860&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/fma.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/fma.ll Mon Jan  2 22:32:35 2017
@@ -1,6 +1,7 @@
 ; RUN: opt -S -instcombine < %s | FileCheck %s
 
 declare float @llvm.fma.f32(float, float, float) #1
+declare <2 x float> @llvm.fma.v2f32(<2 x float>, <2 x float>, <2 x float>) #1
 declare float @llvm.fmuladd.f32(float, float, float) #1
 declare float @llvm.fabs.f32(float) #1
 
@@ -25,7 +26,7 @@ define float @fma_fneg_x_fneg_y_fast(flo
 }
 
 ; CHECK-LABEL: @fma_fneg_const_fneg_y(
-; CHECK: %fma = call float @llvm.fma.f32(float bitcast (i32 ptrtoint (i32* @external to i32) to float), float %y, float %z)
+; CHECK: %fma = call float @llvm.fma.f32(float %y, float bitcast (i32 ptrtoint (i32* @external to i32) to float), float %z)
 define float @fma_fneg_const_fneg_y(float %y, float %z) {
   %y.fneg = fsub float -0.0, %y
   %fma = call float @llvm.fma.f32(float fsub (float -0.0, float bitcast (i32 ptrtoint (i32* @external to i32) to float)), float %y.fneg, float %z)
@@ -86,7 +87,7 @@ define float @fmuladd_fneg_x_fneg_y_fast
 }
 
 ; CHECK-LABEL: @fmuladd_fneg_const_fneg_y(
-; CHECK: %fmuladd = call float @llvm.fmuladd.f32(float bitcast (i32 ptrtoint (i32* @external to i32) to float), float %y, float %z)
+; CHECK: %fmuladd = call float @llvm.fmuladd.f32(float %y, float bitcast (i32 ptrtoint (i32* @external to i32) to float), float %z)
 define float @fmuladd_fneg_const_fneg_y(float %y, float %z) {
   %y.fneg = fsub float -0.0, %y
   %fmuladd = call float @llvm.fmuladd.f32(float fsub (float -0.0, float bitcast (i32 ptrtoint (i32* @external to i32) to float)), float %y.fneg, float %z)
@@ -128,5 +129,75 @@ define float @fmuladd_fabs_x_fabs_x_fast
   ret float %fmuladd
 }
 
+; CHECK-LABEL: @fma_k_y_z(
+; CHECK: %fma = call float @llvm.fma.f32(float %y, float 4.000000e+00, float %z)
+define float @fma_k_y_z(float %y, float %z) {
+  %fma = call float @llvm.fma.f32(float 4.0, float %y, float %z)
+  ret float %fma
+}
+
+; CHECK-LABEL: @fma_k_y_z_fast(
+; CHECK: %fma = call fast float @llvm.fma.f32(float %y, float 4.000000e+00, float %z)
+define float @fma_k_y_z_fast(float %y, float %z) {
+  %fma = call fast float @llvm.fma.f32(float 4.0, float %y, float %z)
+  ret float %fma
+}
+
+; CHECK-LABEL: @fmuladd_k_y_z_fast(
+; CHECK: %fmuladd = call fast float @llvm.fmuladd.f32(float %y, float 4.000000e+00, float %z)
+define float @fmuladd_k_y_z_fast(float %y, float %z) {
+  %fmuladd = call fast float @llvm.fmuladd.f32(float 4.0, float %y, float %z)
+  ret float %fmuladd
+}
+
+; CHECK-LABEL: @fma_1_y_z(
+; CHECK: %fma = fadd float %y, %z
+define float @fma_1_y_z(float %y, float %z) {
+  %fma = call float @llvm.fma.f32(float 1.0, float %y, float %z)
+  ret float %fma
+}
+
+; CHECK-LABEL: @fma_x_1_z(
+; CHECK: %fma = fadd float %x, %z
+define float @fma_x_1_z(float %x, float %z) {
+  %fma = call float @llvm.fma.f32(float %x, float 1.0, float %z)
+  ret float %fma
+}
+
+; CHECK-LABEL: @fma_x_1_z_v2f32(
+; CHECK: %fma = fadd <2 x float> %x, %z
+define <2 x float> @fma_x_1_z_v2f32(<2 x float> %x, <2 x float> %z) {
+  %fma = call <2 x float> @llvm.fma.v2f32(<2 x float> %x, <2 x float> <float 1.0, float 1.0>, <2 x float> %z)
+  ret <2 x float> %fma
+}
+
+; CHECK-LABEL: @fma_x_1_2_z_v2f32(
+; CHECK: %fma = call <2 x float> @llvm.fma.v2f32(<2 x float> %x, <2 x float> <float 1.000000e+00, float 2.000000e+00>, <2 x float> %z)
+define <2 x float> @fma_x_1_2_z_v2f32(<2 x float> %x, <2 x float> %z) {
+  %fma = call <2 x float> @llvm.fma.v2f32(<2 x float> %x, <2 x float> <float 1.0, float 2.0>, <2 x float> %z)
+  ret <2 x float> %fma
+}
+
+; CHECK-LABEL: @fma_x_1_z_fast(
+; CHECK: %fma = fadd fast float %x, %z
+define float @fma_x_1_z_fast(float %x, float %z) {
+  %fma = call fast float @llvm.fma.f32(float %x, float 1.0, float %z)
+  ret float %fma
+}
+
+; CHECK-LABEL: @fma_1_1_z(
+; CHECK: %fma = fadd float %z, 1.0
+define float @fma_1_1_z(float %z) {
+  %fma = call float @llvm.fma.f32(float 1.0, float 1.0, float %z)
+  ret float %fma
+}
+
+; CHECK-LABEL: @fmuladd_x_1_z_fast(
+; CHECK: %fmuladd = fadd fast float %x, %z
+define float @fmuladd_x_1_z_fast(float %x, float %z) {
+  %fmuladd = call fast float @llvm.fmuladd.f32(float %x, float 1.0, float %z)
+  ret float %fmuladd
+}
+
 attributes #0 = { nounwind }
 attributes #1 = { nounwind readnone }




More information about the llvm-commits mailing list