1 //===-- ConstantFolding.cpp - Analyze constant folding possibilities ------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This family of functions determines the possibility of performing constant
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Analysis/ConstantFolding.h"
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/Instructions.h"
19 #include "llvm/Intrinsics.h"
20 #include "llvm/Support/GetElementPtrTypeIterator.h"
21 #include "llvm/Support/MathExtras.h"
26 //===----------------------------------------------------------------------===//
27 // Constant Folding ...
31 /// canConstantFoldCallTo - Return true if its even possible to fold a call to
32 /// the specified function.
34 llvm::canConstantFoldCallTo(Function *F) {
35 const std::string &Name = F->getName();
37 switch (F->getIntrinsicID()) {
38 case Intrinsic::isunordered:
47 return Name == "acos" || Name == "asin" || Name == "atan" ||
50 return Name == "ceil" || Name == "cos" || Name == "cosf" ||
55 return Name == "fabs" || Name == "fmod" || Name == "floor";
57 return Name == "log" || Name == "log10";
61 return Name == "sin" || Name == "sinh" || Name == "sqrt";
63 return Name == "tan" || Name == "tanh";
70 llvm::ConstantFoldFP(double (*NativeFP)(double), double V, const Type *Ty) {
74 return ConstantFP::get(Ty, V);
78 /// ConstantFoldCall - Attempt to constant fold a call to the specified function
79 /// with the specified arguments, returning null if unsuccessful.
81 llvm::ConstantFoldCall(Function *F, const std::vector<Constant*> &Operands) {
82 const std::string &Name = F->getName();
83 const Type *Ty = F->getReturnType();
85 if (Operands.size() == 1) {
86 if (ConstantFP *Op = dyn_cast<ConstantFP>(Operands[0])) {
87 double V = Op->getValue();
92 return ConstantFoldFP(acos, V, Ty);
93 else if (Name == "asin")
94 return ConstantFoldFP(asin, V, Ty);
95 else if (Name == "atan")
96 return ConstantFP::get(Ty, atan(V));
100 return ConstantFoldFP(ceil, V, Ty);
101 else if (Name == "cos")
102 return ConstantFP::get(Ty, cos(V));
103 else if (Name == "cosh")
104 return ConstantFP::get(Ty, cosh(V));
108 return ConstantFP::get(Ty, exp(V));
112 return ConstantFP::get(Ty, fabs(V));
113 else if (Name == "floor")
114 return ConstantFoldFP(floor, V, Ty);
117 if (Name == "log" && V > 0)
118 return ConstantFP::get(Ty, log(V));
119 else if (Name == "log10" && V > 0)
120 return ConstantFoldFP(log10, V, Ty);
121 else if (Name == "llvm.sqrt") {
123 return ConstantFP::get(Ty, sqrt(V));
125 return ConstantFP::get(Ty, 0.0);
130 return ConstantFP::get(Ty, sin(V));
131 else if (Name == "sinh")
132 return ConstantFP::get(Ty, sinh(V));
133 else if (Name == "sqrt" && V >= 0)
134 return ConstantFP::get(Ty, sqrt(V));
138 return ConstantFP::get(Ty, tan(V));
139 else if (Name == "tanh")
140 return ConstantFP::get(Ty, tanh(V));
146 } else if (Operands.size() == 2) {
147 if (ConstantFP *Op1 = dyn_cast<ConstantFP>(Operands[0])) {
148 double Op1V = Op1->getValue();
149 if (ConstantFP *Op2 = dyn_cast<ConstantFP>(Operands[1])) {
150 double Op2V = Op2->getValue();
152 if (Name == "llvm.isunordered")
153 return ConstantBool::get(IsNAN(Op1V) || IsNAN(Op2V));
157 double V = pow(Op1V, Op2V);
159 return ConstantFP::get(Ty, V);
160 } else if (Name == "fmod") {
162 double V = fmod(Op1V, Op2V);
164 return ConstantFP::get(Ty, V);
165 } else if (Name == "atan2")
166 return ConstantFP::get(Ty, atan2(Op1V,Op2V));