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