oops, forgot to load GP for indirect calls, though the old code now commented
[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:
39   case Intrinsic::sqrt:
40     return true;
41   default: break;
42   }
43
44   switch (Name[0])
45   {
46     case 'a':
47       return Name == "acos" || Name == "asin" || Name == "atan" ||
48              Name == "atan2";
49     case 'c':
50       return Name == "ceil" || Name == "cos" || Name == "cosf" ||
51              Name == "cosh";
52     case 'e':
53       return Name == "exp";
54     case 'f':
55       return Name == "fabs" || Name == "fmod" || Name == "floor";
56     case 'l':
57       return Name == "log" || Name == "log10";
58     case 'p':
59       return Name == "pow";
60     case 's':
61       return Name == "sin" || Name == "sinh" || Name == "sqrt";
62     case 't':
63       return Name == "tan" || Name == "tanh";
64     default:
65       return false;
66   }
67 }
68
69 Constant *
70 llvm::ConstantFoldFP(double (*NativeFP)(double), double V, const Type *Ty) {
71   errno = 0;
72   V = NativeFP(V);
73   if (errno == 0)
74     return ConstantFP::get(Ty, V);
75   return 0;
76 }
77
78 /// ConstantFoldCall - Attempt to constant fold a call to the specified function
79 /// with the specified arguments, returning null if unsuccessful.
80 Constant *
81 llvm::ConstantFoldCall(Function *F, const std::vector<Constant*> &Operands) {
82   const std::string &Name = F->getName();
83   const Type *Ty = F->getReturnType();
84
85   if (Operands.size() == 1) {
86     if (ConstantFP *Op = dyn_cast<ConstantFP>(Operands[0])) {
87       double V = Op->getValue();
88       switch (Name[0])
89       {
90         case 'a':
91           if (Name == "acos")
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));
97           break;
98         case 'c':
99           if (Name == "ceil")
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));
105           break;
106         case 'e':
107           if (Name == "exp")
108             return ConstantFP::get(Ty, exp(V));
109           break;
110         case 'f':
111           if (Name == "fabs")
112             return ConstantFP::get(Ty, fabs(V));
113           else if (Name == "floor")
114             return ConstantFoldFP(floor, V, Ty);
115           break;
116         case 'l':
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") {
122             if (V >= -0.0)
123               return ConstantFP::get(Ty, sqrt(V));
124             else // Undefined
125               return ConstantFP::get(Ty, 0.0);
126           }
127           break;
128         case 's':
129           if (Name == "sin")
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));
135           break;
136         case 't':
137           if (Name == "tan")
138             return ConstantFP::get(Ty, tan(V));
139           else if (Name == "tanh")
140             return ConstantFP::get(Ty, tanh(V));
141           break;
142         default:
143           break;
144       }
145     }
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();
151
152         if (Name == "llvm.isunordered")
153           return ConstantBool::get(IsNAN(Op1V) || IsNAN(Op2V));
154         else
155         if (Name == "pow") {
156           errno = 0;
157           double V = pow(Op1V, Op2V);
158           if (errno == 0)
159             return ConstantFP::get(Ty, V);
160         } else if (Name == "fmod") {
161           errno = 0;
162           double V = fmod(Op1V, Op2V);
163           if (errno == 0)
164             return ConstantFP::get(Ty, V);
165         } else if (Name == "atan2")
166           return ConstantFP::get(Ty, atan2(Op1V,Op2V));
167       }
168     }
169   }
170   return 0;
171 }
172