summaryrefslogtreecommitdiff
path: root/lib/Transforms/Utils/Local.cpp
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2004-04-13 19:28:52 +0000
committerChris Lattner <sabre@nondot.org>2004-04-13 19:28:52 +0000
commit25b8390bf23ed35640664d244951d684c5a7b132 (patch)
tree6da6cd016829205da4c723d911f1b1e089a16f4c /lib/Transforms/Utils/Local.cpp
parenta2631b0225321363368519f1b9b533b2f226faa7 (diff)
downloadllvm-25b8390bf23ed35640664d244951d684c5a7b132.tar.gz
llvm-25b8390bf23ed35640664d244951d684c5a7b132.tar.bz2
llvm-25b8390bf23ed35640664d244951d684c5a7b132.tar.xz
Add a simple call constant propagation interface.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@12919 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Transforms/Utils/Local.cpp')
-rw-r--r--lib/Transforms/Utils/Local.cpp51
1 files changed, 51 insertions, 0 deletions
diff --git a/lib/Transforms/Utils/Local.cpp b/lib/Transforms/Utils/Local.cpp
index c54765b427..29132c0680 100644
--- a/lib/Transforms/Utils/Local.cpp
+++ b/lib/Transforms/Utils/Local.cpp
@@ -57,6 +57,18 @@ Constant *llvm::ConstantFoldInstruction(Instruction *I) {
// If we reach here, all incoming values are the same constant.
return Result;
+ } else if (CallInst *CI = dyn_cast<CallInst>(I)) {
+ if (Function *F = CI->getCalledFunction())
+ if (canConstantFoldCallTo(F)) {
+ std::vector<Constant*> Args;
+ for (unsigned i = 1, e = CI->getNumOperands(); i != e; ++i)
+ if (Constant *Op = dyn_cast<Constant>(CI->getOperand(i)))
+ Args.push_back(Op);
+ else
+ return 0;
+ return ConstantFoldCall(F, Args);
+ }
+ return 0;
}
Constant *Op0 = 0, *Op1 = 0;
@@ -216,6 +228,45 @@ bool llvm::ConstantFoldTerminator(BasicBlock *BB) {
return false;
}
+/// canConstantFoldCallTo - Return true if its even possible to fold a call to
+/// the specified function.
+bool llvm::canConstantFoldCallTo(Function *F) {
+ const std::string &Name = F->getName();
+ return Name == "sin" || Name == "cos" || Name == "tan" || Name == "sqrt";
+}
+
+/// ConstantFoldCall - Attempt to constant fold a call to the specified function
+/// with the specified arguments, returning null if unsuccessful.
+Constant *llvm::ConstantFoldCall(Function *F,
+ const std::vector<Constant*> &Operands) {
+ const std::string &Name = F->getName();
+ const Type *Ty = F->getReturnType();
+
+ if (Name == "sin") {
+ if (Operands.size() == 1)
+ if (ConstantFP *CFP = dyn_cast<ConstantFP>(Operands[0]))
+ return ConstantFP::get(Ty, sin(CFP->getValue()));
+
+ } else if (Name == "cos") {
+ if (Operands.size() == 1)
+ if (ConstantFP *CFP = dyn_cast<ConstantFP>(Operands[0]))
+ return ConstantFP::get(Ty, cos(CFP->getValue()));
+
+ } else if (Name == "tan") {
+ if (Operands.size() == 1)
+ if (ConstantFP *CFP = dyn_cast<ConstantFP>(Operands[0]))
+ return ConstantFP::get(Ty, tan(CFP->getValue()));
+
+ } else if (Name == "sqrt") {
+ if (Operands.size() == 1)
+ if (ConstantFP *CFP = dyn_cast<ConstantFP>(Operands[0]))
+ if (CFP->getValue() >= 0)
+ return ConstantFP::get(Ty, sqrt(CFP->getValue()));
+ }
+ return 0;
+}
+
+
//===----------------------------------------------------------------------===//