blob: 1b355b6478be71d8fbd043d935447ec7c696a061 [file] [log] [blame]
John Criswellbd9d3702005-10-27 16:00:10 +00001//===-- ConstantFolding.cpp - Analyze constant folding possibilities ------===//
2//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
9//
10// This family of functions determines the possibility of performing constant
11// folding.
12//
13//===----------------------------------------------------------------------===//
14
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"
22#include <cerrno>
John Criswellbd9d3702005-10-27 16:00:10 +000023using namespace llvm;
24
25//===----------------------------------------------------------------------===//
26// Constant Folding ...
27//
28
29
30/// canConstantFoldCallTo - Return true if its even possible to fold a call to
31/// the specified function.
32bool
33llvm::canConstantFoldCallTo(Function *F) {
34 const std::string &Name = F->getName();
35
36 switch (F->getIntrinsicID()) {
Reid Spencer0b118202006-01-16 21:12:35 +000037 case Intrinsic::isunordered_f32:
38 case Intrinsic::isunordered_f64:
39 case Intrinsic::sqrt_f32:
40 case Intrinsic::sqrt_f64:
Nate Begeman6fb3bd62006-01-14 01:25:24 +000041 case Intrinsic::bswap_i16:
42 case Intrinsic::bswap_i32:
43 case Intrinsic::bswap_i64:
44 // FIXME: these should be constant folded as well
Reid Spencer0b118202006-01-16 21:12:35 +000045 //case Intrinsic::ctpop_i8:
46 //case Intrinsic::ctpop_i16:
47 //case Intrinsic::ctpop_i32:
48 //case Intrinsic::ctpop_i64:
49 //case Intrinsic::ctlz_i8:
50 //case Intrinsic::ctlz_i16:
51 //case Intrinsic::ctlz_i32:
52 //case Intrinsic::ctlz_i64:
53 //case Intrinsic::cttz_i8:
54 //case Intrinsic::cttz_i16:
55 //case Intrinsic::cttz_i32:
56 //case Intrinsic::cttz_i64:
John Criswellbd9d3702005-10-27 16:00:10 +000057 return true;
58 default: break;
59 }
60
61 switch (Name[0])
62 {
63 case 'a':
64 return Name == "acos" || Name == "asin" || Name == "atan" ||
65 Name == "atan2";
66 case 'c':
67 return Name == "ceil" || Name == "cos" || Name == "cosf" ||
68 Name == "cosh";
69 case 'e':
70 return Name == "exp";
71 case 'f':
72 return Name == "fabs" || Name == "fmod" || Name == "floor";
73 case 'l':
74 return Name == "log" || Name == "log10";
75 case 'p':
76 return Name == "pow";
77 case 's':
Chris Lattner5cff2672006-06-17 18:17:52 +000078 return Name == "sin" || Name == "sinh" ||
79 Name == "sqrt" || Name == "sqrtf";
John Criswellbd9d3702005-10-27 16:00:10 +000080 case 't':
81 return Name == "tan" || Name == "tanh";
82 default:
83 return false;
84 }
85}
86
87Constant *
88llvm::ConstantFoldFP(double (*NativeFP)(double), double V, const Type *Ty) {
89 errno = 0;
90 V = NativeFP(V);
91 if (errno == 0)
92 return ConstantFP::get(Ty, V);
93 return 0;
94}
95
96/// ConstantFoldCall - Attempt to constant fold a call to the specified function
97/// with the specified arguments, returning null if unsuccessful.
98Constant *
99llvm::ConstantFoldCall(Function *F, const std::vector<Constant*> &Operands) {
100 const std::string &Name = F->getName();
101 const Type *Ty = F->getReturnType();
102
103 if (Operands.size() == 1) {
104 if (ConstantFP *Op = dyn_cast<ConstantFP>(Operands[0])) {
105 double V = Op->getValue();
106 switch (Name[0])
107 {
108 case 'a':
109 if (Name == "acos")
110 return ConstantFoldFP(acos, V, Ty);
111 else if (Name == "asin")
112 return ConstantFoldFP(asin, V, Ty);
113 else if (Name == "atan")
114 return ConstantFP::get(Ty, atan(V));
115 break;
116 case 'c':
117 if (Name == "ceil")
118 return ConstantFoldFP(ceil, V, Ty);
119 else if (Name == "cos")
120 return ConstantFP::get(Ty, cos(V));
121 else if (Name == "cosh")
122 return ConstantFP::get(Ty, cosh(V));
123 break;
124 case 'e':
125 if (Name == "exp")
126 return ConstantFP::get(Ty, exp(V));
127 break;
128 case 'f':
129 if (Name == "fabs")
130 return ConstantFP::get(Ty, fabs(V));
131 else if (Name == "floor")
132 return ConstantFoldFP(floor, V, Ty);
133 break;
134 case 'l':
135 if (Name == "log" && V > 0)
136 return ConstantFP::get(Ty, log(V));
137 else if (Name == "log10" && V > 0)
138 return ConstantFoldFP(log10, V, Ty);
Reid Spencer0b118202006-01-16 21:12:35 +0000139 else if (Name == "llvm.sqrt.f32" || Name == "llvm.sqrt.f64") {
John Criswellbd9d3702005-10-27 16:00:10 +0000140 if (V >= -0.0)
141 return ConstantFP::get(Ty, sqrt(V));
142 else // Undefined
143 return ConstantFP::get(Ty, 0.0);
144 }
145 break;
146 case 's':
147 if (Name == "sin")
148 return ConstantFP::get(Ty, sin(V));
149 else if (Name == "sinh")
150 return ConstantFP::get(Ty, sinh(V));
151 else if (Name == "sqrt" && V >= 0)
152 return ConstantFP::get(Ty, sqrt(V));
Chris Lattner5cff2672006-06-17 18:17:52 +0000153 else if (Name == "sqrtf" && V >= 0)
154 return ConstantFP::get(Ty, sqrt((float)V));
John Criswellbd9d3702005-10-27 16:00:10 +0000155 break;
156 case 't':
157 if (Name == "tan")
158 return ConstantFP::get(Ty, tan(V));
159 else if (Name == "tanh")
160 return ConstantFP::get(Ty, tanh(V));
161 break;
162 default:
163 break;
164 }
Reid Spencerb83eb642006-10-20 07:07:24 +0000165 } else if (ConstantInt *Op = dyn_cast<ConstantInt>(Operands[0])) {
166 assert(Op->getType()->isUnsigned() && "bswap args must be unsigned");
167 uint64_t V = Op->getZExtValue();
Nate Begeman6fb3bd62006-01-14 01:25:24 +0000168 if (Name == "llvm.bswap.i16")
Reid Spencerb83eb642006-10-20 07:07:24 +0000169 return ConstantInt::get(Ty, ByteSwap_16(V));
Nate Begeman6fb3bd62006-01-14 01:25:24 +0000170 else if (Name == "llvm.bswap.i32")
Reid Spencerb83eb642006-10-20 07:07:24 +0000171 return ConstantInt::get(Ty, ByteSwap_32(V));
Nate Begeman6fb3bd62006-01-14 01:25:24 +0000172 else if (Name == "llvm.bswap.i64")
Reid Spencerb83eb642006-10-20 07:07:24 +0000173 return ConstantInt::get(Ty, ByteSwap_64(V));
John Criswellbd9d3702005-10-27 16:00:10 +0000174 }
175 } else if (Operands.size() == 2) {
176 if (ConstantFP *Op1 = dyn_cast<ConstantFP>(Operands[0])) {
177 double Op1V = Op1->getValue();
178 if (ConstantFP *Op2 = dyn_cast<ConstantFP>(Operands[1])) {
179 double Op2V = Op2->getValue();
180
Reid Spencer0b118202006-01-16 21:12:35 +0000181 if (Name == "llvm.isunordered.f32" || Name == "llvm.isunordered.f64")
John Criswellbd9d3702005-10-27 16:00:10 +0000182 return ConstantBool::get(IsNAN(Op1V) || IsNAN(Op2V));
183 else
184 if (Name == "pow") {
185 errno = 0;
186 double V = pow(Op1V, Op2V);
187 if (errno == 0)
188 return ConstantFP::get(Ty, V);
189 } else if (Name == "fmod") {
190 errno = 0;
191 double V = fmod(Op1V, Op2V);
192 if (errno == 0)
193 return ConstantFP::get(Ty, V);
194 } else if (Name == "atan2")
195 return ConstantFP::get(Ty, atan2(Op1V,Op2V));
196 }
197 }
198 }
199 return 0;
200}
201