blob: b4c41373cb886bd6e5d180ebf431e204ee24b545 [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>
Jeff Cohen97af7512006-12-02 02:22:01 +000023#include <cmath>
John Criswellbd9d3702005-10-27 16:00:10 +000024using namespace llvm;
25
26//===----------------------------------------------------------------------===//
27// Constant Folding ...
28//
29
30
31/// canConstantFoldCallTo - Return true if its even possible to fold a call to
32/// the specified function.
33bool
34llvm::canConstantFoldCallTo(Function *F) {
35 const std::string &Name = F->getName();
36
37 switch (F->getIntrinsicID()) {
Reid Spencer0b118202006-01-16 21:12:35 +000038 case Intrinsic::sqrt_f32:
39 case Intrinsic::sqrt_f64:
Nate Begeman6fb3bd62006-01-14 01:25:24 +000040 case Intrinsic::bswap_i16:
41 case Intrinsic::bswap_i32:
42 case Intrinsic::bswap_i64:
43 // FIXME: these should be constant folded as well
Reid Spencer0b118202006-01-16 21:12:35 +000044 //case Intrinsic::ctpop_i8:
45 //case Intrinsic::ctpop_i16:
46 //case Intrinsic::ctpop_i32:
47 //case Intrinsic::ctpop_i64:
48 //case Intrinsic::ctlz_i8:
49 //case Intrinsic::ctlz_i16:
50 //case Intrinsic::ctlz_i32:
51 //case Intrinsic::ctlz_i64:
52 //case Intrinsic::cttz_i8:
53 //case Intrinsic::cttz_i16:
54 //case Intrinsic::cttz_i32:
55 //case Intrinsic::cttz_i64:
John Criswellbd9d3702005-10-27 16:00:10 +000056 return true;
57 default: break;
58 }
59
60 switch (Name[0])
61 {
62 case 'a':
63 return Name == "acos" || Name == "asin" || Name == "atan" ||
64 Name == "atan2";
65 case 'c':
66 return Name == "ceil" || Name == "cos" || Name == "cosf" ||
67 Name == "cosh";
68 case 'e':
69 return Name == "exp";
70 case 'f':
71 return Name == "fabs" || Name == "fmod" || Name == "floor";
72 case 'l':
73 return Name == "log" || Name == "log10";
74 case 'p':
75 return Name == "pow";
76 case 's':
Chris Lattner5cff2672006-06-17 18:17:52 +000077 return Name == "sin" || Name == "sinh" ||
78 Name == "sqrt" || Name == "sqrtf";
John Criswellbd9d3702005-10-27 16:00:10 +000079 case 't':
80 return Name == "tan" || Name == "tanh";
81 default:
82 return false;
83 }
84}
85
86Constant *
87llvm::ConstantFoldFP(double (*NativeFP)(double), double V, const Type *Ty) {
88 errno = 0;
89 V = NativeFP(V);
90 if (errno == 0)
91 return ConstantFP::get(Ty, V);
92 return 0;
93}
94
95/// ConstantFoldCall - Attempt to constant fold a call to the specified function
96/// with the specified arguments, returning null if unsuccessful.
97Constant *
98llvm::ConstantFoldCall(Function *F, const std::vector<Constant*> &Operands) {
99 const std::string &Name = F->getName();
100 const Type *Ty = F->getReturnType();
101
102 if (Operands.size() == 1) {
103 if (ConstantFP *Op = dyn_cast<ConstantFP>(Operands[0])) {
104 double V = Op->getValue();
105 switch (Name[0])
106 {
107 case 'a':
108 if (Name == "acos")
109 return ConstantFoldFP(acos, V, Ty);
110 else if (Name == "asin")
111 return ConstantFoldFP(asin, V, Ty);
112 else if (Name == "atan")
113 return ConstantFP::get(Ty, atan(V));
114 break;
115 case 'c':
116 if (Name == "ceil")
117 return ConstantFoldFP(ceil, V, Ty);
118 else if (Name == "cos")
119 return ConstantFP::get(Ty, cos(V));
120 else if (Name == "cosh")
121 return ConstantFP::get(Ty, cosh(V));
122 break;
123 case 'e':
124 if (Name == "exp")
125 return ConstantFP::get(Ty, exp(V));
126 break;
127 case 'f':
128 if (Name == "fabs")
129 return ConstantFP::get(Ty, fabs(V));
130 else if (Name == "floor")
131 return ConstantFoldFP(floor, V, Ty);
132 break;
133 case 'l':
134 if (Name == "log" && V > 0)
135 return ConstantFP::get(Ty, log(V));
136 else if (Name == "log10" && V > 0)
137 return ConstantFoldFP(log10, V, Ty);
Reid Spencer0b118202006-01-16 21:12:35 +0000138 else if (Name == "llvm.sqrt.f32" || Name == "llvm.sqrt.f64") {
John Criswellbd9d3702005-10-27 16:00:10 +0000139 if (V >= -0.0)
140 return ConstantFP::get(Ty, sqrt(V));
141 else // Undefined
142 return ConstantFP::get(Ty, 0.0);
143 }
144 break;
145 case 's':
146 if (Name == "sin")
147 return ConstantFP::get(Ty, sin(V));
148 else if (Name == "sinh")
149 return ConstantFP::get(Ty, sinh(V));
150 else if (Name == "sqrt" && V >= 0)
151 return ConstantFP::get(Ty, sqrt(V));
Chris Lattner5cff2672006-06-17 18:17:52 +0000152 else if (Name == "sqrtf" && V >= 0)
153 return ConstantFP::get(Ty, sqrt((float)V));
John Criswellbd9d3702005-10-27 16:00:10 +0000154 break;
155 case 't':
156 if (Name == "tan")
157 return ConstantFP::get(Ty, tan(V));
158 else if (Name == "tanh")
159 return ConstantFP::get(Ty, tanh(V));
160 break;
161 default:
162 break;
163 }
Reid Spencerb83eb642006-10-20 07:07:24 +0000164 } else if (ConstantInt *Op = dyn_cast<ConstantInt>(Operands[0])) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000165 uint64_t V = Op->getZExtValue();
Nate Begeman6fb3bd62006-01-14 01:25:24 +0000166 if (Name == "llvm.bswap.i16")
Reid Spencerb83eb642006-10-20 07:07:24 +0000167 return ConstantInt::get(Ty, ByteSwap_16(V));
Nate Begeman6fb3bd62006-01-14 01:25:24 +0000168 else if (Name == "llvm.bswap.i32")
Reid Spencerb83eb642006-10-20 07:07:24 +0000169 return ConstantInt::get(Ty, ByteSwap_32(V));
Nate Begeman6fb3bd62006-01-14 01:25:24 +0000170 else if (Name == "llvm.bswap.i64")
Reid Spencerb83eb642006-10-20 07:07:24 +0000171 return ConstantInt::get(Ty, ByteSwap_64(V));
John Criswellbd9d3702005-10-27 16:00:10 +0000172 }
173 } else if (Operands.size() == 2) {
174 if (ConstantFP *Op1 = dyn_cast<ConstantFP>(Operands[0])) {
175 double Op1V = Op1->getValue();
176 if (ConstantFP *Op2 = dyn_cast<ConstantFP>(Operands[1])) {
177 double Op2V = Op2->getValue();
178
John Criswellbd9d3702005-10-27 16:00:10 +0000179 if (Name == "pow") {
180 errno = 0;
181 double V = pow(Op1V, Op2V);
182 if (errno == 0)
183 return ConstantFP::get(Ty, V);
184 } else if (Name == "fmod") {
185 errno = 0;
186 double V = fmod(Op1V, Op2V);
187 if (errno == 0)
188 return ConstantFP::get(Ty, V);
189 } else if (Name == "atan2")
190 return ConstantFP::get(Ty, atan2(Op1V,Op2V));
191 }
192 }
193 }
194 return 0;
195}
196