From 384152444d22d356f7b4090a158da4cbc1f79464 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Mon, 16 Jul 2007 15:26:22 +0000 Subject: [PATCH] Use ConstantFoldFP for folding all unary floating-point operations which may have an error, and refector out the code for binary operators into ConstantFoldBinaryFP and use it for all binary floating-point operations which may have an error. These functions still rely exclusively on errno to detect errors though. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@39923 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Analysis/ConstantFolding.cpp | 45 ++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/lib/Analysis/ConstantFolding.cpp b/lib/Analysis/ConstantFolding.cpp index 8991c246586..e85d150204e 100644 --- a/lib/Analysis/ConstantFolding.cpp +++ b/lib/Analysis/ConstantFolding.cpp @@ -363,6 +363,17 @@ static Constant *ConstantFoldFP(double (*NativeFP)(double), double V, return 0; } +static Constant *ConstantFoldBinaryFP(double (*NativeFP)(double, double), + double V, double W, + const Type *Ty) { + errno = 0; + V = NativeFP(V, W); + if (errno == 0) + return ConstantFP::get(Ty, V); + errno = 0; + return 0; +} + /// ConstantFoldCall - Attempt to constant fold a call to the specified function /// with the specified arguments, returning null if unsuccessful. Constant * @@ -381,19 +392,19 @@ llvm::ConstantFoldCall(Function *F, Constant** Operands, unsigned NumOperands) { else if (Name == "asin") return ConstantFoldFP(asin, V, Ty); else if (Name == "atan") - return ConstantFP::get(Ty, atan(V)); + return ConstantFoldFP(atan, V, Ty); break; case 'c': if (Name == "ceil") return ConstantFoldFP(ceil, V, Ty); else if (Name == "cos") - return ConstantFP::get(Ty, cos(V)); + return ConstantFoldFP(cos, V, Ty); else if (Name == "cosh") - return ConstantFP::get(Ty, cosh(V)); + return ConstantFoldFP(cosh, V, Ty); break; case 'e': if (Name == "exp") - return ConstantFP::get(Ty, exp(V)); + return ConstantFoldFP(exp, V, Ty); break; case 'f': if (Name == "fabs") @@ -403,7 +414,7 @@ llvm::ConstantFoldCall(Function *F, Constant** Operands, unsigned NumOperands) { break; case 'l': if (Name == "log" && V > 0) - return ConstantFP::get(Ty, log(V)); + return ConstantFoldFP(log, V, Ty); else if (Name == "log10" && V > 0) return ConstantFoldFP(log10, V, Ty); else if (Name == "llvm.sqrt.f32" || Name == "llvm.sqrt.f64") { @@ -415,19 +426,19 @@ llvm::ConstantFoldCall(Function *F, Constant** Operands, unsigned NumOperands) { break; case 's': if (Name == "sin") - return ConstantFP::get(Ty, sin(V)); + return ConstantFoldFP(sin, V, Ty); else if (Name == "sinh") - return ConstantFP::get(Ty, sinh(V)); + return ConstantFoldFP(sinh, V, Ty); else if (Name == "sqrt" && V >= 0) - return ConstantFP::get(Ty, sqrt(V)); + return ConstantFoldFP(sqrt, V, Ty); else if (Name == "sqrtf" && V >= 0) - return ConstantFP::get(Ty, sqrt((float)V)); + return ConstantFoldFP(sqrt, V, Ty); break; case 't': if (Name == "tan") - return ConstantFP::get(Ty, tan(V)); + return ConstantFoldFP(tan, V, Ty); else if (Name == "tanh") - return ConstantFP::get(Ty, tanh(V)); + return ConstantFoldFP(tanh, V, Ty); break; default: break; @@ -453,17 +464,11 @@ llvm::ConstantFoldCall(Function *F, Constant** Operands, unsigned NumOperands) { double Op2V = Op2->getValue(); if (Name == "pow") { - errno = 0; - double V = pow(Op1V, Op2V); - if (errno == 0) - return ConstantFP::get(Ty, V); + return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty); } else if (Name == "fmod") { - errno = 0; - double V = fmod(Op1V, Op2V); - if (errno == 0) - return ConstantFP::get(Ty, V); + return ConstantFoldBinaryFP(fmod, Op1V, Op2V, Ty); } else if (Name == "atan2") { - return ConstantFP::get(Ty, atan2(Op1V,Op2V)); + return ConstantFoldBinaryFP(atan2, Op1V, Op2V, Ty); } } else if (ConstantInt *Op2C = dyn_cast(Operands[1])) { if (Name == "llvm.powi.f32") {