From a43312d30b545b1bc2720af343f90a95a11629e7 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Fri, 16 Apr 2004 22:35:33 +0000 Subject: Add support for evaluation of exp/log/log10/pow llvm-svn: 13011 --- llvm/lib/Transforms/Utils/Local.cpp | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'llvm/lib/Transforms') diff --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp index 76a22a44dd0..6a1688a4254 100644 --- a/llvm/lib/Transforms/Utils/Local.cpp +++ b/llvm/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; } -- cgit v1.2.3