From f5b9eb37a92a36e80bacc21a89f538731ed63aa2 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Fri, 16 Apr 2004 22:35:33 +0000 Subject: [PATCH] Add support for evaluation of exp/log/log10/pow git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@13011 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Utils/Local.cpp | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/Transforms/Utils/Local.cpp b/lib/Transforms/Utils/Local.cpp index 76a22a44dd0..6a1688a4254 100644 --- a/lib/Transforms/Utils/Local.cpp +++ b/lib/Transforms/Utils/Local.cpp @@ -233,7 +233,8 @@ bool llvm::ConstantFoldTerminator(BasicBlock *BB) { /// the specified function. bool llvm::canConstantFoldCallTo(Function *F) { const std::string &Name = F->getName(); - return Name == "sin" || Name == "cos" || Name == "tan" || Name == "sqrt"; + return Name == "sin" || Name == "cos" || Name == "tan" || Name == "sqrt" || + Name == "log" || Name == "log10" || Name == "exp" || Name == "pow"; } /// ConstantFoldCall - Attempt to constant fold a call to the specified function @@ -263,6 +264,29 @@ Constant *llvm::ConstantFoldCall(Function *F, if (ConstantFP *CFP = dyn_cast(Operands[0])) if (CFP->getValue() >= 0) return ConstantFP::get(Ty, sqrt(CFP->getValue())); + } else if (Name == "exp") { + if (Operands.size() == 1) + if (ConstantFP *CFP = dyn_cast(Operands[0])) + return ConstantFP::get(Ty, exp(CFP->getValue())); + } else if (Name == "log") { + if (Operands.size() == 1) + if (ConstantFP *CFP = dyn_cast(Operands[0])) + if (CFP->getValue() > 0) + return ConstantFP::get(Ty, log(CFP->getValue())); + } else if (Name == "log10") { + if (Operands.size() == 1) + if (ConstantFP *CFP = dyn_cast(Operands[0])) + if (CFP->getValue() > 0) + return ConstantFP::get(Ty, log10(CFP->getValue())); + } else if (Name == "pow") { + if (Operands.size() == 2) + if (ConstantFP *Op1 = dyn_cast(Operands[0])) + if (ConstantFP *Op2 = dyn_cast(Operands[1])) { + errno = 0; + double V = pow(Op1->getValue(), Op2->getValue()); + if (errno == 0) + return ConstantFP::get(Ty, V); + } } return 0; }