c2ece8cf3bac49f07fc4aa05aca953ef5eb5a6dd
[oota-llvm.git] / include / llvm / Analysis / TargetLibraryInfo.h
1 //===-- TargetLibraryInfo.h - Library information ---------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_ANALYSIS_TARGETLIBRARYINFO_H
11 #define LLVM_ANALYSIS_TARGETLIBRARYINFO_H
12
13 #include "llvm/ADT/DenseMap.h"
14 #include "llvm/ADT/ArrayRef.h"
15 #include "llvm/ADT/Optional.h"
16 #include "llvm/ADT/Triple.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/IR/Module.h"
19 #include "llvm/Pass.h"
20
21 namespace llvm {
22 /// VecDesc - Describes a possible vectorization of a function.
23 /// Function 'VectorFnName' is equivalent to 'ScalarFnName' vectorized
24 /// by a factor 'VectorizationFactor'.
25 struct VecDesc {
26   const char *ScalarFnName;
27   const char *VectorFnName;
28   unsigned VectorizationFactor;
29 };
30 class PreservedAnalyses;
31
32   namespace LibFunc {
33     enum Func {
34 #define TLI_DEFINE_ENUM
35 #include "llvm/Analysis/TargetLibraryInfo.def"
36
37       NumLibFuncs
38     };
39   }
40
41 /// \brief Implementation of the target library information.
42 ///
43 /// This class constructs tables that hold the target library information and
44 /// make it available. However, it is somewhat expensive to compute and only
45 /// depends on the triple. So users typicaly interact with the \c
46 /// TargetLibraryInfo wrapper below.
47 class TargetLibraryInfoImpl {
48   friend class TargetLibraryInfo;
49
50   unsigned char AvailableArray[(LibFunc::NumLibFuncs+3)/4];
51   llvm::DenseMap<unsigned, std::string> CustomNames;
52   static const char *const StandardNames[LibFunc::NumLibFuncs];
53
54   enum AvailabilityState {
55     StandardName = 3, // (memset to all ones)
56     CustomName = 1,
57     Unavailable = 0  // (memset to all zeros)
58   };
59   void setState(LibFunc::Func F, AvailabilityState State) {
60     AvailableArray[F/4] &= ~(3 << 2*(F&3));
61     AvailableArray[F/4] |= State << 2*(F&3);
62   }
63   AvailabilityState getState(LibFunc::Func F) const {
64     return static_cast<AvailabilityState>((AvailableArray[F/4] >> 2*(F&3)) & 3);
65   }
66
67   /// Vectorization descriptors - sorted by ScalarFnName.
68   std::vector<VecDesc> VectorDescs;
69   /// Scalarization descriptors - same content as VectorDescs but sorted based
70   /// on VectorFnName rather than ScalarFnName.
71   std::vector<VecDesc> ScalarDescs;
72
73 public:
74   TargetLibraryInfoImpl();
75   explicit TargetLibraryInfoImpl(const Triple &T);
76
77   // Provide value semantics.
78   TargetLibraryInfoImpl(const TargetLibraryInfoImpl &TLI);
79   TargetLibraryInfoImpl(TargetLibraryInfoImpl &&TLI);
80   TargetLibraryInfoImpl &operator=(const TargetLibraryInfoImpl &TLI);
81   TargetLibraryInfoImpl &operator=(TargetLibraryInfoImpl &&TLI);
82
83   /// \brief Searches for a particular function name.
84   ///
85   /// If it is one of the known library functions, return true and set F to the
86   /// corresponding value.
87   bool getLibFunc(StringRef funcName, LibFunc::Func &F) const;
88
89   /// \brief Forces a function to be marked as unavailable.
90   void setUnavailable(LibFunc::Func F) {
91     setState(F, Unavailable);
92   }
93
94   /// \brief Forces a function to be marked as available.
95   void setAvailable(LibFunc::Func F) {
96     setState(F, StandardName);
97   }
98
99   /// \brief Forces a function to be marked as available and provide an
100   /// alternate name that must be used.
101   void setAvailableWithName(LibFunc::Func F, StringRef Name) {
102     if (StandardNames[F] != Name) {
103       setState(F, CustomName);
104       CustomNames[F] = Name;
105       assert(CustomNames.find(F) != CustomNames.end());
106     } else {
107       setState(F, StandardName);
108     }
109   }
110
111   /// \brief Disables all builtins.
112   ///
113   /// This can be used for options like -fno-builtin.
114   void disableAllFunctions();
115
116   /// addVectorizableFunctions - Add a set of scalar -> vector mappings,
117   /// queryable via getVectorizedFunction and getScalarizedFunction.
118   void addVectorizableFunctions(ArrayRef<VecDesc> Fns);
119
120   /// isFunctionVectorizable - Return true if the function F has a
121   /// vector equivalent with vectorization factor VF.
122   bool isFunctionVectorizable(StringRef F, unsigned VF) const {
123     return !getVectorizedFunction(F, VF).empty();
124   }
125
126   /// isFunctionVectorizable - Return true if the function F has a
127   /// vector equivalent with any vectorization factor.
128   bool isFunctionVectorizable(StringRef F) const;
129
130   /// getVectorizedFunction - Return the name of the equivalent of
131   /// F, vectorized with factor VF. If no such mapping exists,
132   /// return the empty string.
133   StringRef getVectorizedFunction(StringRef F, unsigned VF) const;
134
135   /// isFunctionScalarizable - Return true if the function F has a
136   /// scalar equivalent, and set VF to be the vectorization factor.
137   bool isFunctionScalarizable(StringRef F, unsigned &VF) const {
138     return !getScalarizedFunction(F, VF).empty();
139   }
140
141   /// getScalarizedFunction - Return the name of the equivalent of
142   /// F, scalarized. If no such mapping exists, return the empty string.
143   ///
144   /// Set VF to the vectorization factor.
145   StringRef getScalarizedFunction(StringRef F, unsigned &VF) const;
146 };
147
148 /// \brief Provides information about what library functions are available for
149 /// the current target.
150 ///
151 /// This both allows optimizations to handle them specially and frontends to
152 /// disable such optimizations through -fno-builtin etc.
153 class TargetLibraryInfo {
154   friend class TargetLibraryAnalysis;
155   friend class TargetLibraryInfoWrapperPass;
156
157   const TargetLibraryInfoImpl *Impl;
158
159 public:
160   explicit TargetLibraryInfo(const TargetLibraryInfoImpl &Impl) : Impl(&Impl) {}
161
162   // Provide value semantics.
163   TargetLibraryInfo(const TargetLibraryInfo &TLI) : Impl(TLI.Impl) {}
164   TargetLibraryInfo(TargetLibraryInfo &&TLI) : Impl(TLI.Impl) {}
165   TargetLibraryInfo &operator=(const TargetLibraryInfo &TLI) {
166     Impl = TLI.Impl;
167     return *this;
168   }
169   TargetLibraryInfo &operator=(TargetLibraryInfo &&TLI) {
170     Impl = TLI.Impl;
171     return *this;
172   }
173
174   /// \brief Searches for a particular function name.
175   ///
176   /// If it is one of the known library functions, return true and set F to the
177   /// corresponding value.
178   bool getLibFunc(StringRef funcName, LibFunc::Func &F) const {
179     return Impl->getLibFunc(funcName, F);
180   }
181
182   /// \brief Tests whether a library function is available.
183   bool has(LibFunc::Func F) const {
184     return Impl->getState(F) != TargetLibraryInfoImpl::Unavailable;
185   }
186   bool isFunctionVectorizable(StringRef F, unsigned VF) const {
187     return Impl->isFunctionVectorizable(F, VF);
188   };
189   bool isFunctionVectorizable(StringRef F) const {
190     return Impl->isFunctionVectorizable(F);
191   };
192   StringRef getVectorizedFunction(StringRef F, unsigned VF) const {
193     return Impl->getVectorizedFunction(F, VF);
194   };
195
196   /// \brief Tests if the function is both available and a candidate for
197   /// optimized code generation.
198   bool hasOptimizedCodeGen(LibFunc::Func F) const {
199     if (Impl->getState(F) == TargetLibraryInfoImpl::Unavailable)
200       return false;
201     switch (F) {
202     default: break;
203     case LibFunc::copysign:  case LibFunc::copysignf:  case LibFunc::copysignl:
204     case LibFunc::fabs:      case LibFunc::fabsf:      case LibFunc::fabsl:
205     case LibFunc::sin:       case LibFunc::sinf:       case LibFunc::sinl:
206     case LibFunc::cos:       case LibFunc::cosf:       case LibFunc::cosl:
207     case LibFunc::sqrt:      case LibFunc::sqrtf:      case LibFunc::sqrtl:
208     case LibFunc::sqrt_finite: case LibFunc::sqrtf_finite:
209                                                   case LibFunc::sqrtl_finite:
210     case LibFunc::fmax:      case LibFunc::fmaxf:      case LibFunc::fmaxl:
211     case LibFunc::fmin:      case LibFunc::fminf:      case LibFunc::fminl:
212     case LibFunc::floor:     case LibFunc::floorf:     case LibFunc::floorl:
213     case LibFunc::nearbyint: case LibFunc::nearbyintf: case LibFunc::nearbyintl:
214     case LibFunc::ceil:      case LibFunc::ceilf:      case LibFunc::ceill:
215     case LibFunc::rint:      case LibFunc::rintf:      case LibFunc::rintl:
216     case LibFunc::round:     case LibFunc::roundf:     case LibFunc::roundl:
217     case LibFunc::trunc:     case LibFunc::truncf:     case LibFunc::truncl:
218     case LibFunc::log2:      case LibFunc::log2f:      case LibFunc::log2l:
219     case LibFunc::exp2:      case LibFunc::exp2f:      case LibFunc::exp2l:
220     case LibFunc::memcmp:    case LibFunc::strcmp:     case LibFunc::strcpy:
221     case LibFunc::stpcpy:    case LibFunc::strlen:     case LibFunc::strnlen:
222     case LibFunc::memchr:
223       return true;
224     }
225     return false;
226   }
227
228   StringRef getName(LibFunc::Func F) const {
229     auto State = Impl->getState(F);
230     if (State == TargetLibraryInfoImpl::Unavailable)
231       return StringRef();
232     if (State == TargetLibraryInfoImpl::StandardName)
233       return Impl->StandardNames[F];
234     assert(State == TargetLibraryInfoImpl::CustomName);
235     return Impl->CustomNames.find(F)->second;
236   }
237
238   /// \brief Handle invalidation from the pass manager.
239   ///
240   /// If we try to invalidate this info, just return false. It cannot become
241   /// invalid even if the module changes.
242   bool invalidate(Module &, const PreservedAnalyses &) { return false; }
243 };
244
245 /// \brief Analysis pass providing the \c TargetLibraryInfo.
246 ///
247 /// Note that this pass's result cannot be invalidated, it is immutable for the
248 /// life of the module.
249 class TargetLibraryAnalysis {
250 public:
251   typedef TargetLibraryInfo Result;
252
253   /// \brief Opaque, unique identifier for this analysis pass.
254   static void *ID() { return (void *)&PassID; }
255
256   /// \brief Default construct the library analysis.
257   ///
258   /// This will use the module's triple to construct the library info for that
259   /// module.
260   TargetLibraryAnalysis() {}
261
262   /// \brief Construct a library analysis with preset info.
263   ///
264   /// This will directly copy the preset info into the result without
265   /// consulting the module's triple.
266   TargetLibraryAnalysis(TargetLibraryInfoImpl PresetInfoImpl)
267       : PresetInfoImpl(std::move(PresetInfoImpl)) {}
268
269   // Move semantics. We spell out the constructors for MSVC.
270   TargetLibraryAnalysis(TargetLibraryAnalysis &&Arg)
271       : PresetInfoImpl(std::move(Arg.PresetInfoImpl)), Impls(std::move(Arg.Impls)) {}
272   TargetLibraryAnalysis &operator=(TargetLibraryAnalysis &&RHS) {
273     PresetInfoImpl = std::move(RHS.PresetInfoImpl);
274     Impls = std::move(RHS.Impls);
275     return *this;
276   }
277
278   TargetLibraryInfo run(Module &M);
279   TargetLibraryInfo run(Function &F);
280
281   /// \brief Provide access to a name for this pass for debugging purposes.
282   static StringRef name() { return "TargetLibraryAnalysis"; }
283
284 private:
285   static char PassID;
286
287   Optional<TargetLibraryInfoImpl> PresetInfoImpl;
288
289   StringMap<std::unique_ptr<TargetLibraryInfoImpl>> Impls;
290
291   TargetLibraryInfoImpl &lookupInfoImpl(Triple T);
292 };
293
294 class TargetLibraryInfoWrapperPass : public ImmutablePass {
295   TargetLibraryInfoImpl TLIImpl;
296   TargetLibraryInfo TLI;
297
298   virtual void anchor();
299
300 public:
301   static char ID;
302   TargetLibraryInfoWrapperPass();
303   explicit TargetLibraryInfoWrapperPass(const Triple &T);
304   explicit TargetLibraryInfoWrapperPass(const TargetLibraryInfoImpl &TLI);
305
306   TargetLibraryInfo &getTLI() { return TLI; }
307   const TargetLibraryInfo &getTLI() const { return TLI; }
308 };
309
310 } // end namespace llvm
311
312 #endif