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 const char *ScalarFnName;
29 const char *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 const char *const StandardNames[LibFunc::NumLibFuncs];
55 enum AvailabilityState {
56 StandardName = 3, // (memset to all ones)
58 Unavailable = 0 // (memset to all zeros)
60 void setState(LibFunc::Func F, AvailabilityState State) {
61 AvailableArray[F/4] &= ~(3 << 2*(F&3));
62 AvailableArray[F/4] |= State << 2*(F&3);
64 AvailabilityState getState(LibFunc::Func 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::Func 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.
92 TargetLibraryInfoImpl();
93 explicit TargetLibraryInfoImpl(const Triple &T);
95 // Provide value semantics.
96 TargetLibraryInfoImpl(const TargetLibraryInfoImpl &TLI);
97 TargetLibraryInfoImpl(TargetLibraryInfoImpl &&TLI);
98 TargetLibraryInfoImpl &operator=(const TargetLibraryInfoImpl &TLI);
99 TargetLibraryInfoImpl &operator=(TargetLibraryInfoImpl &&TLI);
101 /// Searches for a particular function name.
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;
107 /// Searches for a particular function name, also checking that its type is
108 /// valid for the library function matching that name.
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;
114 /// Forces a function to be marked as unavailable.
115 void setUnavailable(LibFunc::Func F) {
116 setState(F, Unavailable);
119 /// Forces a function to be marked as available.
120 void setAvailable(LibFunc::Func F) {
121 setState(F, StandardName);
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());
132 setState(F, StandardName);
136 /// Disables all builtins.
138 /// This can be used for options like -fno-builtin.
139 void disableAllFunctions();
141 /// Add a set of scalar -> vector mappings, queryable via
142 /// getVectorizedFunction and getScalarizedFunction.
143 void addVectorizableFunctions(ArrayRef<VecDesc> Fns);
145 /// Calls addVectorizableFunctions with a known preset of functions for the
146 /// given vector library.
147 void addVectorizableFunctionsFromVecLib(enum VectorLibrary VecLib);
149 /// Return true if the function F has a vector equivalent with vectorization
151 bool isFunctionVectorizable(StringRef F, unsigned VF) const {
152 return !getVectorizedFunction(F, VF).empty();
155 /// Return true if the function F has a vector equivalent with any
156 /// vectorization factor.
157 bool isFunctionVectorizable(StringRef F) const;
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;
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();
169 /// Return the name of the equivalent of F, scalarized. If no such mapping
170 /// exists, return the empty string.
172 /// Set VF to the vectorization factor.
173 StringRef getScalarizedFunction(StringRef F, unsigned &VF) const;
176 /// Provides information about what library functions are available for
177 /// the current target.
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;
185 const TargetLibraryInfoImpl *Impl;
188 explicit TargetLibraryInfo(const TargetLibraryInfoImpl &Impl) : Impl(&Impl) {}
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) {
197 TargetLibraryInfo &operator=(TargetLibraryInfo &&TLI) {
202 /// Searches for a particular function name.
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);
210 bool getLibFunc(const Function &FDecl, LibFunc::Func &F) const {
211 return Impl->getLibFunc(FDecl, F);
214 /// Tests whether a library function is available.
215 bool has(LibFunc::Func F) const {
216 return Impl->getState(F) != TargetLibraryInfoImpl::Unavailable;
218 bool isFunctionVectorizable(StringRef F, unsigned VF) const {
219 return Impl->isFunctionVectorizable(F, VF);
221 bool isFunctionVectorizable(StringRef F) const {
222 return Impl->isFunctionVectorizable(F);
224 StringRef getVectorizedFunction(StringRef F, unsigned VF) const {
225 return Impl->getVectorizedFunction(F, VF);
228 /// Tests if the function is both available and a candidate for optimized code
230 bool hasOptimizedCodeGen(LibFunc::Func F) const {
231 if (Impl->getState(F) == TargetLibraryInfoImpl::Unavailable)
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:
260 StringRef getName(LibFunc::Func F) const {
261 auto State = Impl->getState(F);
262 if (State == TargetLibraryInfoImpl::Unavailable)
264 if (State == TargetLibraryInfoImpl::StandardName)
265 return Impl->StandardNames[F];
266 assert(State == TargetLibraryInfoImpl::CustomName);
267 return Impl->CustomNames.find(F)->second;
270 /// Handle invalidation from the pass manager.
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; }
277 /// Analysis pass providing the \c TargetLibraryInfo.
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> {
283 typedef TargetLibraryInfo Result;
285 /// Default construct the library analysis.
287 /// This will use the module's triple to construct the library info for that
289 TargetLibraryAnalysis() {}
291 /// Construct a library analysis with preset info.
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)) {}
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);
307 TargetLibraryInfo run(Module &M, ModuleAnalysisManager &);
308 TargetLibraryInfo run(Function &F, FunctionAnalysisManager &);
311 friend AnalysisInfoMixin<TargetLibraryAnalysis>;
314 Optional<TargetLibraryInfoImpl> PresetInfoImpl;
316 StringMap<std::unique_ptr<TargetLibraryInfoImpl>> Impls;
318 TargetLibraryInfoImpl &lookupInfoImpl(const Triple &T);
321 class TargetLibraryInfoWrapperPass : public ImmutablePass {
322 TargetLibraryInfoImpl TLIImpl;
323 TargetLibraryInfo TLI;
325 virtual void anchor();
329 TargetLibraryInfoWrapperPass();
330 explicit TargetLibraryInfoWrapperPass(const Triple &T);
331 explicit TargetLibraryInfoWrapperPass(const TargetLibraryInfoImpl &TLI);
333 TargetLibraryInfo &getTLI() { return TLI; }
334 const TargetLibraryInfo &getTLI() const { return TLI; }
337 } // end namespace llvm