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/Function.h"
17 #include "llvm/IR/Module.h"
18 #include "llvm/IR/PassManager.h"
19 #include "llvm/Pass.h"
22 template <typename T> class ArrayRef;
24 /// Describes a possible vectorization of a function.
25 /// Function 'VectorFnName' is equivalent to 'ScalarFnName' vectorized
26 /// by a factor 'VectorizationFactor'.
28 StringRef ScalarFnName;
29 StringRef VectorFnName;
30 unsigned VectorizationFactor;
35 #define TLI_DEFINE_ENUM
36 #include "llvm/Analysis/TargetLibraryInfo.def"
42 /// Implementation of the target library information.
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;
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;
56 enum AvailabilityState {
57 StandardName = 3, // (memset to all ones)
59 Unavailable = 0 // (memset to all zeros)
61 void setState(LibFunc::Func F, AvailabilityState State) {
62 AvailableArray[F/4] &= ~(3 << 2*(F&3));
63 AvailableArray[F/4] |= State << 2*(F&3);
65 AvailabilityState getState(LibFunc::Func F) const {
66 return static_cast<AvailabilityState>((AvailableArray[F/4] >> 2*(F&3)) & 3);
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;
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;
81 /// List of known vector-functions libraries.
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.
89 NoLibrary, // Don't use any vector library.
90 Accelerate, // Use Accelerate framework.
91 SVML // Intel short vector math library.
94 TargetLibraryInfoImpl();
95 explicit TargetLibraryInfoImpl(const Triple &T);
97 // Provide value semantics.
98 TargetLibraryInfoImpl(const TargetLibraryInfoImpl &TLI);
99 TargetLibraryInfoImpl(TargetLibraryInfoImpl &&TLI);
100 TargetLibraryInfoImpl &operator=(const TargetLibraryInfoImpl &TLI);
101 TargetLibraryInfoImpl &operator=(TargetLibraryInfoImpl &&TLI);
103 /// Searches for a particular function name.
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;
109 /// Searches for a particular function name, also checking that its type is
110 /// valid for the library function matching that name.
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;
116 /// Forces a function to be marked as unavailable.
117 void setUnavailable(LibFunc::Func F) {
118 setState(F, Unavailable);
121 /// Forces a function to be marked as available.
122 void setAvailable(LibFunc::Func F) {
123 setState(F, StandardName);
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());
134 setState(F, StandardName);
138 /// Disables all builtins.
140 /// This can be used for options like -fno-builtin.
141 void disableAllFunctions();
143 /// Add a set of scalar -> vector mappings, queryable via
144 /// getVectorizedFunction and getScalarizedFunction.
145 void addVectorizableFunctions(ArrayRef<VecDesc> Fns);
147 /// Calls addVectorizableFunctions with a known preset of functions for the
148 /// given vector library.
149 void addVectorizableFunctionsFromVecLib(enum VectorLibrary VecLib);
151 /// Return true if the function F has a vector equivalent with vectorization
153 bool isFunctionVectorizable(StringRef F, unsigned VF) const {
154 return !getVectorizedFunction(F, VF).empty();
157 /// Return true if the function F has a vector equivalent with any
158 /// vectorization factor.
159 bool isFunctionVectorizable(StringRef F) const;
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;
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();
171 /// Return the name of the equivalent of F, scalarized. If no such mapping
172 /// exists, return the empty string.
174 /// Set VF to the vectorization factor.
175 StringRef getScalarizedFunction(StringRef F, unsigned &VF) const;
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,
180 void setShouldExtI32Param(bool Val) {
181 ShouldExtI32Param = Val;
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,
187 void setShouldExtI32Return(bool Val) {
188 ShouldExtI32Return = Val;
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;
198 /// Provides information about what library functions are available for
199 /// the current target.
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;
207 const TargetLibraryInfoImpl *Impl;
210 explicit TargetLibraryInfo(const TargetLibraryInfoImpl &Impl) : Impl(&Impl) {}
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) {
219 TargetLibraryInfo &operator=(TargetLibraryInfo &&TLI) {
224 /// Searches for a particular function name.
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);
232 bool getLibFunc(const Function &FDecl, LibFunc::Func &F) const {
233 return Impl->getLibFunc(FDecl, F);
236 /// Tests whether a library function is available.
237 bool has(LibFunc::Func F) const {
238 return Impl->getState(F) != TargetLibraryInfoImpl::Unavailable;
240 bool isFunctionVectorizable(StringRef F, unsigned VF) const {
241 return Impl->isFunctionVectorizable(F, VF);
243 bool isFunctionVectorizable(StringRef F) const {
244 return Impl->isFunctionVectorizable(F);
246 StringRef getVectorizedFunction(StringRef F, unsigned VF) const {
247 return Impl->getVectorizedFunction(F, VF);
250 /// Tests if the function is both available and a candidate for optimized code
252 bool hasOptimizedCodeGen(LibFunc::Func F) const {
253 if (Impl->getState(F) == TargetLibraryInfoImpl::Unavailable)
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:
282 StringRef getName(LibFunc::Func F) const {
283 auto State = Impl->getState(F);
284 if (State == TargetLibraryInfoImpl::Unavailable)
286 if (State == TargetLibraryInfoImpl::StandardName)
287 return Impl->StandardNames[F];
288 assert(State == TargetLibraryInfoImpl::CustomName);
289 return Impl->CustomNames.find(F)->second;
292 /// Returns extension attribute kind to be used for i32 parameters
293 /// corresponding to C-level int or unsigned int. May be zeroext, signext,
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;
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,
306 Attribute::AttrKind getExtAttrForI32Return(bool Signed = true) const {
307 if (Impl->ShouldExtI32Return)
308 return Signed ? Attribute::SExt : Attribute::ZExt;
309 return Attribute::None;
312 /// Handle invalidation from the pass manager.
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 &) {
320 bool invalidate(Function &, const PreservedAnalyses &,
321 FunctionAnalysisManager::Invalidator &) {
326 /// Analysis pass providing the \c TargetLibraryInfo.
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> {
332 typedef TargetLibraryInfo Result;
334 /// Default construct the library analysis.
336 /// This will use the module's triple to construct the library info for that
338 TargetLibraryAnalysis() {}
340 /// Construct a library analysis with preset info.
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)) {}
347 TargetLibraryInfo run(Module &M, ModuleAnalysisManager &);
348 TargetLibraryInfo run(Function &F, FunctionAnalysisManager &);
351 friend AnalysisInfoMixin<TargetLibraryAnalysis>;
352 static AnalysisKey Key;
354 Optional<TargetLibraryInfoImpl> PresetInfoImpl;
356 StringMap<std::unique_ptr<TargetLibraryInfoImpl>> Impls;
358 TargetLibraryInfoImpl &lookupInfoImpl(const Triple &T);
361 class TargetLibraryInfoWrapperPass : public ImmutablePass {
362 TargetLibraryInfoImpl TLIImpl;
363 TargetLibraryInfo TLI;
365 virtual void anchor();
369 TargetLibraryInfoWrapperPass();
370 explicit TargetLibraryInfoWrapperPass(const Triple &T);
371 explicit TargetLibraryInfoWrapperPass(const TargetLibraryInfoImpl &TLI);
373 TargetLibraryInfo &getTLI() { return TLI; }
374 const TargetLibraryInfo &getTLI() const { return TLI; }
377 } // end namespace llvm