Restructure call constant folding code a bit to make it simpler

Add support for acos/asin/atan.  188.ammp contains three calls to acos with
constant arguments.  Constant folding it allows elimination of those 3 calls
and three FP divisions of the results.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@13821 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2004-05-27 06:26:28 +00:00
parent d9572118eb
commit b18b9d7374

View File

@ -235,7 +235,17 @@ bool llvm::ConstantFoldTerminator(BasicBlock *BB) {
bool llvm::canConstantFoldCallTo(Function *F) { bool llvm::canConstantFoldCallTo(Function *F) {
const std::string &Name = F->getName(); 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"; Name == "log" || Name == "log10" || Name == "exp" || Name == "pow" ||
Name == "acos" || Name == "asin";
}
static Constant *ConstantFoldFP(double (*NativeFP)(double), double V,
const Type *Ty) {
errno = 0;
V = NativeFP(V);
if (errno == 0)
return ConstantFP::get(Ty, V);
return 0;
} }
/// ConstantFoldCall - Attempt to constant fold a call to the specified function /// ConstantFoldCall - Attempt to constant fold a call to the specified function
@ -245,50 +255,41 @@ Constant *llvm::ConstantFoldCall(Function *F,
const std::string &Name = F->getName(); const std::string &Name = F->getName();
const Type *Ty = F->getReturnType(); const Type *Ty = F->getReturnType();
if (Name == "sin") { if (Operands.size() == 1) {
if (Operands.size() == 1) if (ConstantFP *Op = dyn_cast<ConstantFP>(Operands[0])) {
if (ConstantFP *CFP = dyn_cast<ConstantFP>(Operands[0])) double V = Op->getValue();
return ConstantFP::get(Ty, sin(CFP->getValue())); if (Name == "sin")
return ConstantFP::get(Ty, sin(V));
} else if (Name == "cos") { else if (Name == "cos")
if (Operands.size() == 1) return ConstantFP::get(Ty, cos(V));
if (ConstantFP *CFP = dyn_cast<ConstantFP>(Operands[0])) else if (Name == "tan")
return ConstantFP::get(Ty, cos(CFP->getValue())); return ConstantFP::get(Ty, tan(V));
else if (Name == "sqrt" && V >= 0)
} else if (Name == "tan") { return ConstantFP::get(Ty, sqrt(V));
if (Operands.size() == 1) else if (Name == "exp")
if (ConstantFP *CFP = dyn_cast<ConstantFP>(Operands[0])) return ConstantFP::get(Ty, exp(V));
return ConstantFP::get(Ty, tan(CFP->getValue())); else if (Name == "log" && V > 0)
return ConstantFP::get(Ty, log(V));
} else if (Name == "sqrt") { else if (Name == "log10")
if (Operands.size() == 1) return ConstantFoldFP(log10, V, Ty);
if (ConstantFP *CFP = dyn_cast<ConstantFP>(Operands[0])) else if (Name == "acos")
if (CFP->getValue() >= 0) return ConstantFoldFP(acos, V, Ty);
return ConstantFP::get(Ty, sqrt(CFP->getValue())); else if (Name == "asin")
} else if (Name == "exp") { return ConstantFoldFP(asin, V, Ty);
if (Operands.size() == 1) else if (Name == "atan")
if (ConstantFP *CFP = dyn_cast<ConstantFP>(Operands[0])) return ConstantFP::get(Ty, atan(V));
return ConstantFP::get(Ty, exp(CFP->getValue())); }
} else if (Name == "log") { } else if (Operands.size() == 2) {
if (Operands.size() == 1)
if (ConstantFP *CFP = dyn_cast<ConstantFP>(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<ConstantFP>(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<ConstantFP>(Operands[0])) if (ConstantFP *Op1 = dyn_cast<ConstantFP>(Operands[0]))
if (ConstantFP *Op2 = dyn_cast<ConstantFP>(Operands[1])) { if (ConstantFP *Op2 = dyn_cast<ConstantFP>(Operands[1])) {
if (Name == "pow") {
errno = 0; errno = 0;
double V = pow(Op1->getValue(), Op2->getValue()); double V = pow(Op1->getValue(), Op2->getValue());
if (errno == 0) if (errno == 0)
return ConstantFP::get(Ty, V); return ConstantFP::get(Ty, V);
} }
} }
}
return 0; return 0;
} }