]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/Analysis/TargetLibraryInfo.h
Merge llvm, clang, lld and lldb trunk r291476.
[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   StringRef ScalarFnName;
29   StringRef 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 StringRef const StandardNames[LibFunc::NumLibFuncs];
54   bool ShouldExtI32Param, ShouldExtI32Return, ShouldSignExtI32Param;
55
56   enum AvailabilityState {
57     StandardName = 3, // (memset to all ones)
58     CustomName = 1,
59     Unavailable = 0  // (memset to all zeros)
60   };
61   void setState(LibFunc::Func F, AvailabilityState State) {
62     AvailableArray[F/4] &= ~(3 << 2*(F&3));
63     AvailableArray[F/4] |= State << 2*(F&3);
64   }
65   AvailabilityState getState(LibFunc::Func F) const {
66     return static_cast<AvailabilityState>((AvailableArray[F/4] >> 2*(F&3)) & 3);
67   }
68
69   /// Vectorization descriptors - sorted by ScalarFnName.
70   std::vector<VecDesc> VectorDescs;
71   /// Scalarization descriptors - same content as VectorDescs but sorted based
72   /// on VectorFnName rather than ScalarFnName.
73   std::vector<VecDesc> ScalarDescs;
74
75   /// Return true if the function type FTy is valid for the library function
76   /// F, regardless of whether the function is available.
77   bool isValidProtoForLibFunc(const FunctionType &FTy, LibFunc::Func F,
78                               const DataLayout *DL) const;
79
80 public:
81   /// List of known vector-functions libraries.
82   ///
83   /// The vector-functions library defines, which functions are vectorizable
84   /// and with which factor. The library can be specified by either frontend,
85   /// or a commandline option, and then used by
86   /// addVectorizableFunctionsFromVecLib for filling up the tables of
87   /// vectorizable functions.
88   enum VectorLibrary {
89     NoLibrary,  // Don't use any vector library.
90     Accelerate, // Use Accelerate framework.
91     SVML        // Intel short vector math library.
92   };
93
94   TargetLibraryInfoImpl();
95   explicit TargetLibraryInfoImpl(const Triple &T);
96
97   // Provide value semantics.
98   TargetLibraryInfoImpl(const TargetLibraryInfoImpl &TLI);
99   TargetLibraryInfoImpl(TargetLibraryInfoImpl &&TLI);
100   TargetLibraryInfoImpl &operator=(const TargetLibraryInfoImpl &TLI);
101   TargetLibraryInfoImpl &operator=(TargetLibraryInfoImpl &&TLI);
102
103   /// Searches for a particular function name.
104   ///
105   /// If it is one of the known library functions, return true and set F to the
106   /// corresponding value.
107   bool getLibFunc(StringRef funcName, LibFunc::Func &F) const;
108
109   /// Searches for a particular function name, also checking that its type is
110   /// valid for the library function matching that name.
111   ///
112   /// If it is one of the known library functions, return true and set F to the
113   /// corresponding value.
114   bool getLibFunc(const Function &FDecl, LibFunc::Func &F) const;
115
116   /// Forces a function to be marked as unavailable.
117   void setUnavailable(LibFunc::Func F) {
118     setState(F, Unavailable);
119   }
120
121   /// Forces a function to be marked as available.
122   void setAvailable(LibFunc::Func F) {
123     setState(F, StandardName);
124   }
125
126   /// Forces a function to be marked as available and provide an alternate name
127   /// that must be used.
128   void setAvailableWithName(LibFunc::Func F, StringRef Name) {
129     if (StandardNames[F] != Name) {
130       setState(F, CustomName);
131       CustomNames[F] = Name;
132       assert(CustomNames.find(F) != CustomNames.end());
133     } else {
134       setState(F, StandardName);
135     }
136   }
137
138   /// Disables all builtins.
139   ///
140   /// This can be used for options like -fno-builtin.
141   void disableAllFunctions();
142
143   /// Add a set of scalar -> vector mappings, queryable via
144   /// getVectorizedFunction and getScalarizedFunction.
145   void addVectorizableFunctions(ArrayRef<VecDesc> Fns);
146
147   /// Calls addVectorizableFunctions with a known preset of functions for the
148   /// given vector library.
149   void addVectorizableFunctionsFromVecLib(enum VectorLibrary VecLib);
150
151   /// Return true if the function F has a vector equivalent with vectorization
152   /// factor VF.
153   bool isFunctionVectorizable(StringRef F, unsigned VF) const {
154     return !getVectorizedFunction(F, VF).empty();
155   }
156
157   /// Return true if the function F has a vector equivalent with any
158   /// vectorization factor.
159   bool isFunctionVectorizable(StringRef F) const;
160
161   /// Return the name of the equivalent of F, vectorized with factor VF. If no
162   /// such mapping exists, return the empty string.
163   StringRef getVectorizedFunction(StringRef F, unsigned VF) const;
164
165   /// Return true if the function F has a scalar equivalent, and set VF to be
166   /// the vectorization factor.
167   bool isFunctionScalarizable(StringRef F, unsigned &VF) const {
168     return !getScalarizedFunction(F, VF).empty();
169   }
170
171   /// Return the name of the equivalent of F, scalarized. If no such mapping
172   /// exists, return the empty string.
173   ///
174   /// Set VF to the vectorization factor.
175   StringRef getScalarizedFunction(StringRef F, unsigned &VF) const;
176
177   /// Set to true iff i32 parameters to library functions should have signext
178   /// or zeroext attributes if they correspond to C-level int or unsigned int,
179   /// respectively.
180   void setShouldExtI32Param(bool Val) {
181     ShouldExtI32Param = Val;
182   }
183
184   /// Set to true iff i32 results from library functions should have signext
185   /// or zeroext attributes if they correspond to C-level int or unsigned int,
186   /// respectively.
187   void setShouldExtI32Return(bool Val) {
188     ShouldExtI32Return = Val;
189   }
190
191   /// Set to true iff i32 parameters to library functions should have signext
192   /// attribute if they correspond to C-level int or unsigned int.
193   void setShouldSignExtI32Param(bool Val) {
194     ShouldSignExtI32Param = Val;
195   }
196 };
197
198 /// Provides information about what library functions are available for
199 /// the current target.
200 ///
201 /// This both allows optimizations to handle them specially and frontends to
202 /// disable such optimizations through -fno-builtin etc.
203 class TargetLibraryInfo {
204   friend class TargetLibraryAnalysis;
205   friend class TargetLibraryInfoWrapperPass;
206
207   const TargetLibraryInfoImpl *Impl;
208
209 public:
210   explicit TargetLibraryInfo(const TargetLibraryInfoImpl &Impl) : Impl(&Impl) {}
211
212   // Provide value semantics.
213   TargetLibraryInfo(const TargetLibraryInfo &TLI) : Impl(TLI.Impl) {}
214   TargetLibraryInfo(TargetLibraryInfo &&TLI) : Impl(TLI.Impl) {}
215   TargetLibraryInfo &operator=(const TargetLibraryInfo &TLI) {
216     Impl = TLI.Impl;
217     return *this;
218   }
219   TargetLibraryInfo &operator=(TargetLibraryInfo &&TLI) {
220     Impl = TLI.Impl;
221     return *this;
222   }
223
224   /// Searches for a particular function name.
225   ///
226   /// If it is one of the known library functions, return true and set F to the
227   /// corresponding value.
228   bool getLibFunc(StringRef funcName, LibFunc::Func &F) const {
229     return Impl->getLibFunc(funcName, F);
230   }
231
232   bool getLibFunc(const Function &FDecl, LibFunc::Func &F) const {
233     return Impl->getLibFunc(FDecl, F);
234   }
235
236   /// Tests whether a library function is available.
237   bool has(LibFunc::Func F) const {
238     return Impl->getState(F) != TargetLibraryInfoImpl::Unavailable;
239   }
240   bool isFunctionVectorizable(StringRef F, unsigned VF) const {
241     return Impl->isFunctionVectorizable(F, VF);
242   }
243   bool isFunctionVectorizable(StringRef F) const {
244     return Impl->isFunctionVectorizable(F);
245   }
246   StringRef getVectorizedFunction(StringRef F, unsigned VF) const {
247     return Impl->getVectorizedFunction(F, VF);
248   }
249
250   /// Tests if the function is both available and a candidate for optimized code
251   /// generation.
252   bool hasOptimizedCodeGen(LibFunc::Func F) const {
253     if (Impl->getState(F) == TargetLibraryInfoImpl::Unavailable)
254       return false;
255     switch (F) {
256     default: break;
257     case LibFunc::copysign:  case LibFunc::copysignf:  case LibFunc::copysignl:
258     case LibFunc::fabs:      case LibFunc::fabsf:      case LibFunc::fabsl:
259     case LibFunc::sin:       case LibFunc::sinf:       case LibFunc::sinl:
260     case LibFunc::cos:       case LibFunc::cosf:       case LibFunc::cosl:
261     case LibFunc::sqrt:      case LibFunc::sqrtf:      case LibFunc::sqrtl:
262     case LibFunc::sqrt_finite: case LibFunc::sqrtf_finite:
263                                                   case LibFunc::sqrtl_finite:
264     case LibFunc::fmax:      case LibFunc::fmaxf:      case LibFunc::fmaxl:
265     case LibFunc::fmin:      case LibFunc::fminf:      case LibFunc::fminl:
266     case LibFunc::floor:     case LibFunc::floorf:     case LibFunc::floorl:
267     case LibFunc::nearbyint: case LibFunc::nearbyintf: case LibFunc::nearbyintl:
268     case LibFunc::ceil:      case LibFunc::ceilf:      case LibFunc::ceill:
269     case LibFunc::rint:      case LibFunc::rintf:      case LibFunc::rintl:
270     case LibFunc::round:     case LibFunc::roundf:     case LibFunc::roundl:
271     case LibFunc::trunc:     case LibFunc::truncf:     case LibFunc::truncl:
272     case LibFunc::log2:      case LibFunc::log2f:      case LibFunc::log2l:
273     case LibFunc::exp2:      case LibFunc::exp2f:      case LibFunc::exp2l:
274     case LibFunc::memcmp:    case LibFunc::strcmp:     case LibFunc::strcpy:
275     case LibFunc::stpcpy:    case LibFunc::strlen:     case LibFunc::strnlen:
276     case LibFunc::memchr:    case LibFunc::mempcpy:
277       return true;
278     }
279     return false;
280   }
281
282   StringRef getName(LibFunc::Func F) const {
283     auto State = Impl->getState(F);
284     if (State == TargetLibraryInfoImpl::Unavailable)
285       return StringRef();
286     if (State == TargetLibraryInfoImpl::StandardName)
287       return Impl->StandardNames[F];
288     assert(State == TargetLibraryInfoImpl::CustomName);
289     return Impl->CustomNames.find(F)->second;
290   }
291
292   /// Returns extension attribute kind to be used for i32 parameters
293   /// corresponding to C-level int or unsigned int.  May be zeroext, signext,
294   /// or none.
295   Attribute::AttrKind getExtAttrForI32Param(bool Signed = true) const {
296     if (Impl->ShouldExtI32Param)
297       return Signed ? Attribute::SExt : Attribute::ZExt;
298     if (Impl->ShouldSignExtI32Param)
299       return Attribute::SExt;
300     return Attribute::None;
301   }
302
303   /// Returns extension attribute kind to be used for i32 return values
304   /// corresponding to C-level int or unsigned int.  May be zeroext, signext,
305   /// or none.
306   Attribute::AttrKind getExtAttrForI32Return(bool Signed = true) const {
307     if (Impl->ShouldExtI32Return)
308       return Signed ? Attribute::SExt : Attribute::ZExt;
309     return Attribute::None;
310   }
311
312   /// Handle invalidation from the pass manager.
313   ///
314   /// If we try to invalidate this info, just return false. It cannot become
315   /// invalid even if the module or function changes.
316   bool invalidate(Module &, const PreservedAnalyses &,
317                   ModuleAnalysisManager::Invalidator &) {
318     return false;
319   }
320   bool invalidate(Function &, const PreservedAnalyses &,
321                   FunctionAnalysisManager::Invalidator &) {
322     return false;
323   }
324 };
325
326 /// Analysis pass providing the \c TargetLibraryInfo.
327 ///
328 /// Note that this pass's result cannot be invalidated, it is immutable for the
329 /// life of the module.
330 class TargetLibraryAnalysis : public AnalysisInfoMixin<TargetLibraryAnalysis> {
331 public:
332   typedef TargetLibraryInfo Result;
333
334   /// Default construct the library analysis.
335   ///
336   /// This will use the module's triple to construct the library info for that
337   /// module.
338   TargetLibraryAnalysis() {}
339
340   /// Construct a library analysis with preset info.
341   ///
342   /// This will directly copy the preset info into the result without
343   /// consulting the module's triple.
344   TargetLibraryAnalysis(TargetLibraryInfoImpl PresetInfoImpl)
345       : PresetInfoImpl(std::move(PresetInfoImpl)) {}
346
347   TargetLibraryInfo run(Module &M, ModuleAnalysisManager &);
348   TargetLibraryInfo run(Function &F, FunctionAnalysisManager &);
349
350 private:
351   friend AnalysisInfoMixin<TargetLibraryAnalysis>;
352   static AnalysisKey Key;
353
354   Optional<TargetLibraryInfoImpl> PresetInfoImpl;
355
356   StringMap<std::unique_ptr<TargetLibraryInfoImpl>> Impls;
357
358   TargetLibraryInfoImpl &lookupInfoImpl(const Triple &T);
359 };
360
361 class TargetLibraryInfoWrapperPass : public ImmutablePass {
362   TargetLibraryInfoImpl TLIImpl;
363   TargetLibraryInfo TLI;
364
365   virtual void anchor();
366
367 public:
368   static char ID;
369   TargetLibraryInfoWrapperPass();
370   explicit TargetLibraryInfoWrapperPass(const Triple &T);
371   explicit TargetLibraryInfoWrapperPass(const TargetLibraryInfoImpl &TLI);
372
373   TargetLibraryInfo &getTLI() { return TLI; }
374   const TargetLibraryInfo &getTLI() const { return TLI; }
375 };
376
377 } // end namespace llvm
378
379 #endif