1 //===-- TargetLibraryInfo.h - Library information ---------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_ANALYSIS_TARGETLIBRARYINFO_H
11 #define LLVM_ANALYSIS_TARGETLIBRARYINFO_H
13 #include "llvm/ADT/DenseMap.h"
14 #include "llvm/ADT/Optional.h"
15 #include "llvm/ADT/Triple.h"
16 #include "llvm/IR/CallSite.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/IR/Module.h"
19 #include "llvm/IR/PassManager.h"
20 #include "llvm/Pass.h"
23 template <typename T> class ArrayRef;
25 /// Describes a possible vectorization of a function.
26 /// Function 'VectorFnName' is equivalent to 'ScalarFnName' vectorized
27 /// by a factor 'VectorizationFactor'.
29 StringRef ScalarFnName;
30 StringRef VectorFnName;
31 unsigned VectorizationFactor;
35 #define TLI_DEFINE_ENUM
36 #include "llvm/Analysis/TargetLibraryInfo.def"
41 /// Implementation of the target library information.
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 typically interact with the \c
46 /// TargetLibraryInfo wrapper below.
47 class TargetLibraryInfoImpl {
48 friend class TargetLibraryInfo;
50 unsigned char AvailableArray[(NumLibFuncs+3)/4];
51 llvm::DenseMap<unsigned, std::string> CustomNames;
52 static StringRef const StandardNames[NumLibFuncs];
53 bool ShouldExtI32Param, ShouldExtI32Return, ShouldSignExtI32Param;
55 enum AvailabilityState {
56 StandardName = 3, // (memset to all ones)
58 Unavailable = 0 // (memset to all zeros)
60 void setState(LibFunc F, AvailabilityState State) {
61 AvailableArray[F/4] &= ~(3 << 2*(F&3));
62 AvailableArray[F/4] |= State << 2*(F&3);
64 AvailabilityState getState(LibFunc F) const {
65 return static_cast<AvailabilityState>((AvailableArray[F/4] >> 2*(F&3)) & 3);
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;
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 F,
77 const DataLayout *DL) const;
80 /// List of known vector-functions libraries.
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.
88 NoLibrary, // Don't use any vector library.
89 Accelerate, // Use Accelerate framework.
90 SVML // Intel short vector math library.
93 TargetLibraryInfoImpl();
94 explicit TargetLibraryInfoImpl(const Triple &T);
96 // Provide value semantics.
97 TargetLibraryInfoImpl(const TargetLibraryInfoImpl &TLI);
98 TargetLibraryInfoImpl(TargetLibraryInfoImpl &&TLI);
99 TargetLibraryInfoImpl &operator=(const TargetLibraryInfoImpl &TLI);
100 TargetLibraryInfoImpl &operator=(TargetLibraryInfoImpl &&TLI);
102 /// Searches for a particular function name.
104 /// If it is one of the known library functions, return true and set F to the
105 /// corresponding value.
106 bool getLibFunc(StringRef funcName, LibFunc &F) const;
108 /// Searches for a particular function name, also checking that its type is
109 /// valid for the library function matching that name.
111 /// If it is one of the known library functions, return true and set F to the
112 /// corresponding value.
113 bool getLibFunc(const Function &FDecl, LibFunc &F) const;
115 /// Forces a function to be marked as unavailable.
116 void setUnavailable(LibFunc F) {
117 setState(F, Unavailable);
120 /// Forces a function to be marked as available.
121 void setAvailable(LibFunc F) {
122 setState(F, StandardName);
125 /// Forces a function to be marked as available and provide an alternate name
126 /// that must be used.
127 void setAvailableWithName(LibFunc F, StringRef Name) {
128 if (StandardNames[F] != Name) {
129 setState(F, CustomName);
130 CustomNames[F] = Name;
131 assert(CustomNames.find(F) != CustomNames.end());
133 setState(F, StandardName);
137 /// Disables all builtins.
139 /// This can be used for options like -fno-builtin.
140 void disableAllFunctions();
142 /// Add a set of scalar -> vector mappings, queryable via
143 /// getVectorizedFunction and getScalarizedFunction.
144 void addVectorizableFunctions(ArrayRef<VecDesc> Fns);
146 /// Calls addVectorizableFunctions with a known preset of functions for the
147 /// given vector library.
148 void addVectorizableFunctionsFromVecLib(enum VectorLibrary VecLib);
150 /// Return true if the function F has a vector equivalent with vectorization
152 bool isFunctionVectorizable(StringRef F, unsigned VF) const {
153 return !getVectorizedFunction(F, VF).empty();
156 /// Return true if the function F has a vector equivalent with any
157 /// vectorization factor.
158 bool isFunctionVectorizable(StringRef F) const;
160 /// Return the name of the equivalent of F, vectorized with factor VF. If no
161 /// such mapping exists, return the empty string.
162 StringRef getVectorizedFunction(StringRef F, unsigned VF) const;
164 /// Return true if the function F has a scalar equivalent, and set VF to be
165 /// the vectorization factor.
166 bool isFunctionScalarizable(StringRef F, unsigned &VF) const {
167 return !getScalarizedFunction(F, VF).empty();
170 /// Return the name of the equivalent of F, scalarized. If no such mapping
171 /// exists, return the empty string.
173 /// Set VF to the vectorization factor.
174 StringRef getScalarizedFunction(StringRef F, unsigned &VF) const;
176 /// Set to true iff i32 parameters to library functions should have signext
177 /// or zeroext attributes if they correspond to C-level int or unsigned int,
179 void setShouldExtI32Param(bool Val) {
180 ShouldExtI32Param = Val;
183 /// Set to true iff i32 results from library functions should have signext
184 /// or zeroext attributes if they correspond to C-level int or unsigned int,
186 void setShouldExtI32Return(bool Val) {
187 ShouldExtI32Return = Val;
190 /// Set to true iff i32 parameters to library functions should have signext
191 /// attribute if they correspond to C-level int or unsigned int.
192 void setShouldSignExtI32Param(bool Val) {
193 ShouldSignExtI32Param = Val;
196 /// Returns the size of the wchar_t type in bytes.
197 unsigned getWCharSize(const Module &M) const;
199 /// Returns size of the default wchar_t type on target \p T. This is mostly
200 /// intended to verify that the size in the frontend matches LLVM. All other
201 /// queries should use getWCharSize() instead.
202 static unsigned getTargetWCharSize(const Triple &T);
205 /// Provides information about what library functions are available for
206 /// the current target.
208 /// This both allows optimizations to handle them specially and frontends to
209 /// disable such optimizations through -fno-builtin etc.
210 class TargetLibraryInfo {
211 friend class TargetLibraryAnalysis;
212 friend class TargetLibraryInfoWrapperPass;
214 const TargetLibraryInfoImpl *Impl;
217 explicit TargetLibraryInfo(const TargetLibraryInfoImpl &Impl) : Impl(&Impl) {}
219 // Provide value semantics.
220 TargetLibraryInfo(const TargetLibraryInfo &TLI) : Impl(TLI.Impl) {}
221 TargetLibraryInfo(TargetLibraryInfo &&TLI) : Impl(TLI.Impl) {}
222 TargetLibraryInfo &operator=(const TargetLibraryInfo &TLI) {
226 TargetLibraryInfo &operator=(TargetLibraryInfo &&TLI) {
231 /// Searches for a particular function name.
233 /// If it is one of the known library functions, return true and set F to the
234 /// corresponding value.
235 bool getLibFunc(StringRef funcName, LibFunc &F) const {
236 return Impl->getLibFunc(funcName, F);
239 bool getLibFunc(const Function &FDecl, LibFunc &F) const {
240 return Impl->getLibFunc(FDecl, F);
243 /// If a callsite does not have the 'nobuiltin' attribute, return if the
244 /// called function is a known library function and set F to that function.
245 bool getLibFunc(ImmutableCallSite CS, LibFunc &F) const {
246 return !CS.isNoBuiltin() && CS.getCalledFunction() &&
247 getLibFunc(*(CS.getCalledFunction()), F);
250 /// Tests whether a library function is available.
251 bool has(LibFunc F) const {
252 return Impl->getState(F) != TargetLibraryInfoImpl::Unavailable;
254 bool isFunctionVectorizable(StringRef F, unsigned VF) const {
255 return Impl->isFunctionVectorizable(F, VF);
257 bool isFunctionVectorizable(StringRef F) const {
258 return Impl->isFunctionVectorizable(F);
260 StringRef getVectorizedFunction(StringRef F, unsigned VF) const {
261 return Impl->getVectorizedFunction(F, VF);
264 /// Tests if the function is both available and a candidate for optimized code
266 bool hasOptimizedCodeGen(LibFunc F) const {
267 if (Impl->getState(F) == TargetLibraryInfoImpl::Unavailable)
271 case LibFunc_copysign: case LibFunc_copysignf: case LibFunc_copysignl:
272 case LibFunc_fabs: case LibFunc_fabsf: case LibFunc_fabsl:
273 case LibFunc_sin: case LibFunc_sinf: case LibFunc_sinl:
274 case LibFunc_cos: case LibFunc_cosf: case LibFunc_cosl:
275 case LibFunc_sqrt: case LibFunc_sqrtf: case LibFunc_sqrtl:
276 case LibFunc_sqrt_finite: case LibFunc_sqrtf_finite:
277 case LibFunc_sqrtl_finite:
278 case LibFunc_fmax: case LibFunc_fmaxf: case LibFunc_fmaxl:
279 case LibFunc_fmin: case LibFunc_fminf: case LibFunc_fminl:
280 case LibFunc_floor: case LibFunc_floorf: case LibFunc_floorl:
281 case LibFunc_nearbyint: case LibFunc_nearbyintf: case LibFunc_nearbyintl:
282 case LibFunc_ceil: case LibFunc_ceilf: case LibFunc_ceill:
283 case LibFunc_rint: case LibFunc_rintf: case LibFunc_rintl:
284 case LibFunc_round: case LibFunc_roundf: case LibFunc_roundl:
285 case LibFunc_trunc: case LibFunc_truncf: case LibFunc_truncl:
286 case LibFunc_log2: case LibFunc_log2f: case LibFunc_log2l:
287 case LibFunc_exp2: case LibFunc_exp2f: case LibFunc_exp2l:
288 case LibFunc_memcmp: case LibFunc_strcmp: case LibFunc_strcpy:
289 case LibFunc_stpcpy: case LibFunc_strlen: case LibFunc_strnlen:
290 case LibFunc_memchr: case LibFunc_mempcpy:
296 StringRef getName(LibFunc F) const {
297 auto State = Impl->getState(F);
298 if (State == TargetLibraryInfoImpl::Unavailable)
300 if (State == TargetLibraryInfoImpl::StandardName)
301 return Impl->StandardNames[F];
302 assert(State == TargetLibraryInfoImpl::CustomName);
303 return Impl->CustomNames.find(F)->second;
306 /// Returns extension attribute kind to be used for i32 parameters
307 /// corresponding to C-level int or unsigned int. May be zeroext, signext,
309 Attribute::AttrKind getExtAttrForI32Param(bool Signed = true) const {
310 if (Impl->ShouldExtI32Param)
311 return Signed ? Attribute::SExt : Attribute::ZExt;
312 if (Impl->ShouldSignExtI32Param)
313 return Attribute::SExt;
314 return Attribute::None;
317 /// Returns extension attribute kind to be used for i32 return values
318 /// corresponding to C-level int or unsigned int. May be zeroext, signext,
320 Attribute::AttrKind getExtAttrForI32Return(bool Signed = true) const {
321 if (Impl->ShouldExtI32Return)
322 return Signed ? Attribute::SExt : Attribute::ZExt;
323 return Attribute::None;
326 /// \copydoc TargetLibraryInfoImpl::getWCharSize()
327 unsigned getWCharSize(const Module &M) const {
328 return Impl->getWCharSize(M);
331 /// Handle invalidation from the pass manager.
333 /// If we try to invalidate this info, just return false. It cannot become
334 /// invalid even if the module or function changes.
335 bool invalidate(Module &, const PreservedAnalyses &,
336 ModuleAnalysisManager::Invalidator &) {
339 bool invalidate(Function &, const PreservedAnalyses &,
340 FunctionAnalysisManager::Invalidator &) {
345 /// Analysis pass providing the \c TargetLibraryInfo.
347 /// Note that this pass's result cannot be invalidated, it is immutable for the
348 /// life of the module.
349 class TargetLibraryAnalysis : public AnalysisInfoMixin<TargetLibraryAnalysis> {
351 typedef TargetLibraryInfo Result;
353 /// Default construct the library analysis.
355 /// This will use the module's triple to construct the library info for that
357 TargetLibraryAnalysis() {}
359 /// Construct a library analysis with preset info.
361 /// This will directly copy the preset info into the result without
362 /// consulting the module's triple.
363 TargetLibraryAnalysis(TargetLibraryInfoImpl PresetInfoImpl)
364 : PresetInfoImpl(std::move(PresetInfoImpl)) {}
366 TargetLibraryInfo run(Module &M, ModuleAnalysisManager &);
367 TargetLibraryInfo run(Function &F, FunctionAnalysisManager &);
370 friend AnalysisInfoMixin<TargetLibraryAnalysis>;
371 static AnalysisKey Key;
373 Optional<TargetLibraryInfoImpl> PresetInfoImpl;
375 StringMap<std::unique_ptr<TargetLibraryInfoImpl>> Impls;
377 TargetLibraryInfoImpl &lookupInfoImpl(const Triple &T);
380 class TargetLibraryInfoWrapperPass : public ImmutablePass {
381 TargetLibraryInfoImpl TLIImpl;
382 TargetLibraryInfo TLI;
384 virtual void anchor();
388 TargetLibraryInfoWrapperPass();
389 explicit TargetLibraryInfoWrapperPass(const Triple &T);
390 explicit TargetLibraryInfoWrapperPass(const TargetLibraryInfoImpl &TLI);
392 TargetLibraryInfo &getTLI() { return TLI; }
393 const TargetLibraryInfo &getTLI() const { return TLI; }
396 } // end namespace llvm