For PR411:
[oota-llvm.git] / lib / Analysis / ConstantFolding.cpp
1 //===-- 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>
23 #include <cmath>
24 using 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.
33 bool
34 llvm::canConstantFoldCallTo(Function *F) {
35   const std::string &Name = F->getName();
36
37   switch (F->getIntrinsicID()) {
38   case Intrinsic::isunordered_f32:
39   case Intrinsic::isunordered_f64:
40   case Intrinsic::sqrt_f32:
41   case Intrinsic::sqrt_f64:
42   case Intrinsic::bswap_i16:
43   case Intrinsic::bswap_i32:
44   case Intrinsic::bswap_i64:
45   // FIXME: these should be constant folded as well
46   //case Intrinsic::ctpop_i8:
47   //case Intrinsic::ctpop_i16:
48   //case Intrinsic::ctpop_i32:
49   //case Intrinsic::ctpop_i64:
50   //case Intrinsic::ctlz_i8:
51   //case Intrinsic::ctlz_i16:
52   //case Intrinsic::ctlz_i32:
53   //case Intrinsic::ctlz_i64:
54   //case Intrinsic::cttz_i8:
55   //case Intrinsic::cttz_i16:
56   //case Intrinsic::cttz_i32:
57   //case Intrinsic::cttz_i64:
58     return true;
59   default: break;
60   }
61
62   switch (Name[0])
63   {
64     case 'a':
65       return Name == "acos" || Name == "asin" || Name == "atan" ||
66              Name == "atan2";
67     case 'c':
68       return Name == "ceil" || Name == "cos" || Name == "cosf" ||
69              Name == "cosh";
70     case 'e':
71       return Name == "exp";
72     case 'f':
73       return Name == "fabs" || Name == "fmod" || Name == "floor";
74     case 'l':
75       return Name == "log" || Name == "log10";
76     case 'p':
77       return Name == "pow";
78     case 's':
79       return Name == "sin" || Name == "sinh" || Name == "sqrt";
80     case 't':
81       return Name == "tan" || Name == "tanh";
82     default:
83       return false;
84   }
85 }
86
87 Constant *
88 llvm::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.
98 Constant *
99 llvm::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);
139           else if (Name == "llvm.sqrt.f32" || Name == "llvm.sqrt.f64") {
140             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));
153           break;
154         case 't':
155           if (Name == "tan")
156             return ConstantFP::get(Ty, tan(V));
157           else if (Name == "tanh")
158             return ConstantFP::get(Ty, tanh(V));
159           break;
160         default:
161           break;
162       }
163     } else if (ConstantUInt *Op = dyn_cast<ConstantUInt>(Operands[0])) {
164       uint64_t V = Op->getValue();
165       if (Name == "llvm.bswap.i16")
166         return ConstantUInt::get(Ty, ByteSwap_16(V));
167       else if (Name == "llvm.bswap.i32")
168         return ConstantUInt::get(Ty, ByteSwap_32(V));
169       else if (Name == "llvm.bswap.i64")
170         return ConstantUInt::get(Ty, ByteSwap_64(V));
171     }
172   } else if (Operands.size() == 2) {
173     if (ConstantFP *Op1 = dyn_cast<ConstantFP>(Operands[0])) {
174       double Op1V = Op1->getValue();
175       if (ConstantFP *Op2 = dyn_cast<ConstantFP>(Operands[1])) {
176         double Op2V = Op2->getValue();
177
178         if (Name == "llvm.isunordered.f32" || Name == "llvm.isunordered.f64")
179           return ConstantBool::get(IsNAN(Op1V) || IsNAN(Op2V));
180         else
181         if (Name == "pow") {
182           errno = 0;
183           double V = pow(Op1V, Op2V);
184           if (errno == 0)
185             return ConstantFP::get(Ty, V);
186         } else if (Name == "fmod") {
187           errno = 0;
188           double V = fmod(Op1V, Op2V);
189           if (errno == 0)
190             return ConstantFP::get(Ty, V);
191         } else if (Name == "atan2")
192           return ConstantFP::get(Ty, atan2(Op1V,Op2V));
193       }
194     }
195   }
196   return 0;
197 }
198