[PATCH] Allow constant folding of fma and fmuladd

Matt Arsenault Matthew.Arsenault at amd.com
Tue Mar 4 15:49:15 PST 2014


  Use APFloat instead of host function

http://llvm-reviews.chandlerc.com/D2951

CHANGE SINCE LAST DIFF
  http://llvm-reviews.chandlerc.com/D2951?vs=7521&id=7522#toc

Files:
  lib/Analysis/ConstantFolding.cpp
  test/Transforms/InstCombine/constant-fold-math.ll

Index: lib/Analysis/ConstantFolding.cpp
===================================================================
--- lib/Analysis/ConstantFolding.cpp
+++ lib/Analysis/ConstantFolding.cpp
@@ -1193,6 +1193,8 @@
   case Intrinsic::ctpop:
   case Intrinsic::ctlz:
   case Intrinsic::cttz:
+  case Intrinsic::fma:
+  case Intrinsic::fmuladd:
   case Intrinsic::sadd_with_overflow:
   case Intrinsic::uadd_with_overflow:
   case Intrinsic::ssub_with_overflow:
@@ -1636,5 +1638,30 @@
     }
     return 0;
   }
+
+  if (Operands.size() == 3) {
+    if (const ConstantFP *Op1 = dyn_cast<ConstantFP>(Operands[0])) {
+      if (const ConstantFP *Op2 = dyn_cast<ConstantFP>(Operands[1])) {
+        if (const ConstantFP *Op3 = dyn_cast<ConstantFP>(Operands[2])) {
+        switch (F->getIntrinsicID()) {
+        default: break;
+        case Intrinsic::fma:
+        case Intrinsic::fmuladd: {
+          APFloat V = Op1->getValueAPF();
+
+          APFloat::opStatus s = V.fusedMultiplyAdd(Op2->getValueAPF(),
+                                                   Op3->getValueAPF(),
+                                                   APFloat::rmNearestTiesToEven);
+          if (s != APFloat::opInvalidOp)
+            return ConstantFP::get(Ty->getContext(), V);
+
+          return 0;
+        }
+        }
+        }
+      }
+    }
+  }
+
   return 0;
 }
Index: test/Transforms/InstCombine/constant-fold-math.ll
===================================================================
--- /dev/null
+++ test/Transforms/InstCombine/constant-fold-math.ll
@@ -0,0 +1,39 @@
+; RUN: opt -S -instcombine < %s | FileCheck %s
+
+declare float @llvm.fma.f32(float, float, float) #0
+declare float @llvm.fmuladd.f32(float, float, float) #0
+
+declare double @llvm.fma.f64(double, double, double) #0
+declare double @llvm.fmuladd.f64(double, double, double) #0
+
+
+
+; CHECK-LABEL: @constant_fold_fma_f32
+; CHECK-NEXT: ret float 6.000000e+00
+define float @constant_fold_fma_f32() #0 {
+  %x = call float @llvm.fma.f32(float 1.0, float 2.0, float 4.0) #0
+  ret float %x
+}
+
+; CHECK-LABEL: @constant_fold_fmuladd_f32
+; CHECK-NEXT: ret float 6.000000e+00
+define float @constant_fold_fmuladd_f32() #0 {
+  %x = call float @llvm.fmuladd.f32(float 1.0, float 2.0, float 4.0) #0
+  ret float %x
+}
+
+; CHECK-LABEL: @constant_fold_fma_f64
+; CHECK-NEXT: ret double 6.000000e+00
+define double @constant_fold_fma_f64() #0 {
+  %x = call double @llvm.fma.f64(double 1.0, double 2.0, double 4.0) #0
+  ret double %x
+}
+
+; CHECK-LABEL: @constant_fold_fmuladd_f64
+; CHECK-NEXT: ret double 6.000000e+00
+define double @constant_fold_fmuladd_f64() #0 {
+  %x = call double @llvm.fmuladd.f64(double 1.0, double 2.0, double 4.0) #0
+  ret double %x
+}
+
+attributes #0 = { nounwind readnone }
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D2951.2.patch
Type: text/x-patch
Size: 2763 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140304/44f88b4f/attachment.bin>


More information about the llvm-commits mailing list