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:
40 case Intrinsic::bswap_i16:
41 case Intrinsic::bswap_i32:
42 case Intrinsic::bswap_i64:
43 // FIXME: these should be constant folded as well
44 //case Intrinsic::ctpop:
45 //case Intrinsic::ctlz:
46 //case Intrinsic::cttz:
54 return Name == "acos" || Name == "asin" || Name == "atan" ||
57 return Name == "ceil" || Name == "cos" || Name == "cosf" ||
62 return Name == "fabs" || Name == "fmod" || Name == "floor";
64 return Name == "log" || Name == "log10";
68 return Name == "sin" || Name == "sinh" || Name == "sqrt";
70 return Name == "tan" || Name == "tanh";
77 llvm::ConstantFoldFP(double (*NativeFP)(double), double V, const Type *Ty) {
81 return ConstantFP::get(Ty, V);
85 /// ConstantFoldCall - Attempt to constant fold a call to the specified function
86 /// with the specified arguments, returning null if unsuccessful.
88 llvm::ConstantFoldCall(Function *F, const std::vector<Constant*> &Operands) {
89 const std::string &Name = F->getName();
90 const Type *Ty = F->getReturnType();
92 if (Operands.size() == 1) {
93 if (ConstantFP *Op = dyn_cast<ConstantFP>(Operands[0])) {
94 double V = Op->getValue();
99 return ConstantFoldFP(acos, V, Ty);
100 else if (Name == "asin")
101 return ConstantFoldFP(asin, V, Ty);
102 else if (Name == "atan")
103 return ConstantFP::get(Ty, atan(V));
107 return ConstantFoldFP(ceil, V, Ty);
108 else if (Name == "cos")
109 return ConstantFP::get(Ty, cos(V));
110 else if (Name == "cosh")
111 return ConstantFP::get(Ty, cosh(V));
115 return ConstantFP::get(Ty, exp(V));
119 return ConstantFP::get(Ty, fabs(V));
120 else if (Name == "floor")
121 return ConstantFoldFP(floor, V, Ty);
124 if (Name == "log" && V > 0)
125 return ConstantFP::get(Ty, log(V));
126 else if (Name == "log10" && V > 0)
127 return ConstantFoldFP(log10, V, Ty);
128 else if (Name == "llvm.sqrt") {
130 return ConstantFP::get(Ty, sqrt(V));
132 return ConstantFP::get(Ty, 0.0);
137 return ConstantFP::get(Ty, sin(V));
138 else if (Name == "sinh")
139 return ConstantFP::get(Ty, sinh(V));
140 else if (Name == "sqrt" && V >= 0)
141 return ConstantFP::get(Ty, sqrt(V));
145 return ConstantFP::get(Ty, tan(V));
146 else if (Name == "tanh")
147 return ConstantFP::get(Ty, tanh(V));
152 } else if (ConstantUInt *Op = dyn_cast<ConstantUInt>(Operands[0])) {
153 uint64_t V = Op->getValue();
154 if (Name == "llvm.bswap.i16")
155 return ConstantUInt::get(Ty, ByteSwap_16(V));
156 else if (Name == "llvm.bswap.i32")
157 return ConstantUInt::get(Ty, ByteSwap_32(V));
158 else if (Name == "llvm.bswap.i64")
159 return ConstantUInt::get(Ty, ByteSwap_64(V));
161 } else if (Operands.size() == 2) {
162 if (ConstantFP *Op1 = dyn_cast<ConstantFP>(Operands[0])) {
163 double Op1V = Op1->getValue();
164 if (ConstantFP *Op2 = dyn_cast<ConstantFP>(Operands[1])) {
165 double Op2V = Op2->getValue();
167 if (Name == "llvm.isunordered")
168 return ConstantBool::get(IsNAN(Op1V) || IsNAN(Op2V));
172 double V = pow(Op1V, Op2V);
174 return ConstantFP::get(Ty, V);
175 } else if (Name == "fmod") {
177 double V = fmod(Op1V, Op2V);
179 return ConstantFP::get(Ty, V);
180 } else if (Name == "atan2")
181 return ConstantFP::get(Ty, atan2(Op1V,Op2V));