From 8377858c553bd208eb8f505c010d65e698cc9452 Mon Sep 17 00:00:00 2001 From: Matt Arsenault Date: Wed, 5 Mar 2014 00:02:00 +0000 Subject: Allow constant folding of fma and fmuladd llvm-svn: 202914 --- llvm/lib/Analysis/ConstantFolding.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'llvm/lib/Analysis/ConstantFolding.cpp') diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp index 10f8e4ed684..254f2d9f50b 100644 --- a/llvm/lib/Analysis/ConstantFolding.cpp +++ b/llvm/lib/Analysis/ConstantFolding.cpp @@ -1193,6 +1193,8 @@ bool llvm::canConstantFoldCallTo(const Function *F) { 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: @@ -1615,5 +1617,30 @@ llvm::ConstantFoldCall(Function *F, ArrayRef Operands, } return 0; } + + if (Operands.size() != 3) + return 0; + + if (const ConstantFP *Op1 = dyn_cast(Operands[0])) { + if (const ConstantFP *Op2 = dyn_cast(Operands[1])) { + if (const ConstantFP *Op3 = dyn_cast(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; } -- cgit v1.2.3