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/ArrayRef.h"
15 #include "llvm/ADT/Optional.h"
16 #include "llvm/ADT/Triple.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/IR/Module.h"
19 #include "llvm/Pass.h"
21 // BEGIN ANDROID-SPECIFIC
30 // END ANDROID-SPECIFIC
33 /// VecDesc - Describes a possible vectorization of a function.
34 /// Function 'VectorFnName' is equivalent to 'ScalarFnName' vectorized
35 /// by a factor 'VectorizationFactor'.
37 const char *ScalarFnName;
38 const char *VectorFnName;
39 unsigned VectorizationFactor;
41 class PreservedAnalyses;
45 #define TLI_DEFINE_ENUM
46 #include "llvm/Analysis/TargetLibraryInfo.def"
52 /// \brief Implementation of the target library information.
54 /// This class constructs tables that hold the target library information and
55 /// make it available. However, it is somewhat expensive to compute and only
56 /// depends on the triple. So users typicaly interact with the \c
57 /// TargetLibraryInfo wrapper below.
58 class TargetLibraryInfoImpl {
59 friend class TargetLibraryInfo;
61 unsigned char AvailableArray[(LibFunc::NumLibFuncs+3)/4];
62 llvm::DenseMap<unsigned, std::string> CustomNames;
63 static const char *const StandardNames[LibFunc::NumLibFuncs];
65 enum AvailabilityState {
66 StandardName = 3, // (memset to all ones)
68 Unavailable = 0 // (memset to all zeros)
70 void setState(LibFunc::Func F, AvailabilityState State) {
71 AvailableArray[F/4] &= ~(3 << 2*(F&3));
72 AvailableArray[F/4] |= State << 2*(F&3);
74 AvailabilityState getState(LibFunc::Func F) const {
75 return static_cast<AvailabilityState>((AvailableArray[F/4] >> 2*(F&3)) & 3);
78 /// Vectorization descriptors - sorted by ScalarFnName.
79 std::vector<VecDesc> VectorDescs;
80 /// Scalarization descriptors - same content as VectorDescs but sorted based
81 /// on VectorFnName rather than ScalarFnName.
82 std::vector<VecDesc> ScalarDescs;
85 /// \brief List of known vector-functions libraries.
87 /// The vector-functions library defines, which functions are vectorizable
88 /// and with which factor. The library can be specified by either frontend,
89 /// or a commandline option, and then used by
90 /// addVectorizableFunctionsFromVecLib for filling up the tables of
91 /// vectorizable functions.
93 NoLibrary, // Don't use any vector library.
94 Accelerate // Use Accelerate framework.
97 TargetLibraryInfoImpl();
98 explicit TargetLibraryInfoImpl(const Triple &T);
100 // Provide value semantics.
101 TargetLibraryInfoImpl(const TargetLibraryInfoImpl &TLI);
102 TargetLibraryInfoImpl(TargetLibraryInfoImpl &&TLI);
103 TargetLibraryInfoImpl &operator=(const TargetLibraryInfoImpl &TLI);
104 TargetLibraryInfoImpl &operator=(TargetLibraryInfoImpl &&TLI);
106 /// \brief Searches for a particular function name.
108 /// If it is one of the known library functions, return true and set F to the
109 /// corresponding value.
110 bool getLibFunc(StringRef funcName, LibFunc::Func &F) const;
112 /// \brief Forces a function to be marked as unavailable.
113 void setUnavailable(LibFunc::Func F) {
114 setState(F, Unavailable);
117 /// \brief Forces a function to be marked as available.
118 void setAvailable(LibFunc::Func F) {
119 setState(F, StandardName);
122 /// \brief Forces a function to be marked as available and provide an
123 /// alternate name that must be used.
124 void setAvailableWithName(LibFunc::Func F, StringRef Name) {
125 if (StandardNames[F] != Name) {
126 setState(F, CustomName);
127 CustomNames[F] = Name;
128 assert(CustomNames.find(F) != CustomNames.end());
130 setState(F, StandardName);
134 /// \brief Disables all builtins.
136 /// This can be used for options like -fno-builtin.
137 void disableAllFunctions();
139 /// addVectorizableFunctions - Add a set of scalar -> vector mappings,
140 /// queryable via getVectorizedFunction and getScalarizedFunction.
141 void addVectorizableFunctions(ArrayRef<VecDesc> Fns);
143 /// Calls addVectorizableFunctions with a known preset of functions for the
144 /// given vector library.
145 void addVectorizableFunctionsFromVecLib(enum VectorLibrary VecLib);
147 /// isFunctionVectorizable - Return true if the function F has a
148 /// vector equivalent with vectorization factor VF.
149 bool isFunctionVectorizable(StringRef F, unsigned VF) const {
150 return !getVectorizedFunction(F, VF).empty();
153 /// isFunctionVectorizable - Return true if the function F has a
154 /// vector equivalent with any vectorization factor.
155 bool isFunctionVectorizable(StringRef F) const;
157 /// getVectorizedFunction - Return the name of the equivalent of
158 /// F, vectorized with factor VF. If no such mapping exists,
159 /// return the empty string.
160 StringRef getVectorizedFunction(StringRef F, unsigned VF) const;
162 /// isFunctionScalarizable - Return true if the function F has a
163 /// scalar equivalent, and set VF to be the vectorization factor.
164 bool isFunctionScalarizable(StringRef F, unsigned &VF) const {
165 return !getScalarizedFunction(F, VF).empty();
168 /// getScalarizedFunction - Return the name of the equivalent of
169 /// F, scalarized. If no such mapping exists, return the empty string.
171 /// Set VF to the vectorization factor.
172 StringRef getScalarizedFunction(StringRef F, unsigned &VF) const;
175 /// \brief Provides information about what library functions are available for
176 /// the current target.
178 /// This both allows optimizations to handle them specially and frontends to
179 /// disable such optimizations through -fno-builtin etc.
180 class TargetLibraryInfo {
181 friend class TargetLibraryAnalysis;
182 friend class TargetLibraryInfoWrapperPass;
184 const TargetLibraryInfoImpl *Impl;
187 explicit TargetLibraryInfo(const TargetLibraryInfoImpl &Impl) : Impl(&Impl) {}
189 // Provide value semantics.
190 TargetLibraryInfo(const TargetLibraryInfo &TLI) : Impl(TLI.Impl) {}
191 TargetLibraryInfo(TargetLibraryInfo &&TLI) : Impl(TLI.Impl) {}
192 TargetLibraryInfo &operator=(const TargetLibraryInfo &TLI) {
196 TargetLibraryInfo &operator=(TargetLibraryInfo &&TLI) {
201 /// \brief Searches for a particular function name.
203 /// If it is one of the known library functions, return true and set F to the
204 /// corresponding value.
205 bool getLibFunc(StringRef funcName, LibFunc::Func &F) const {
206 return Impl->getLibFunc(funcName, F);
209 /// \brief Tests whether a library function is available.
210 bool has(LibFunc::Func F) const {
211 return Impl->getState(F) != TargetLibraryInfoImpl::Unavailable;
213 bool isFunctionVectorizable(StringRef F, unsigned VF) const {
214 return Impl->isFunctionVectorizable(F, VF);
216 bool isFunctionVectorizable(StringRef F) const {
217 return Impl->isFunctionVectorizable(F);
219 StringRef getVectorizedFunction(StringRef F, unsigned VF) const {
220 return Impl->getVectorizedFunction(F, VF);
223 /// \brief Tests if the function is both available and a candidate for
224 /// optimized code generation.
225 bool hasOptimizedCodeGen(LibFunc::Func F) const {
226 if (Impl->getState(F) == TargetLibraryInfoImpl::Unavailable)
230 case LibFunc::copysign: case LibFunc::copysignf: case LibFunc::copysignl:
231 case LibFunc::fabs: case LibFunc::fabsf: case LibFunc::fabsl:
232 case LibFunc::sin: case LibFunc::sinf: case LibFunc::sinl:
233 case LibFunc::cos: case LibFunc::cosf: case LibFunc::cosl:
234 case LibFunc::sqrt: case LibFunc::sqrtf: case LibFunc::sqrtl:
235 case LibFunc::sqrt_finite: case LibFunc::sqrtf_finite:
236 case LibFunc::sqrtl_finite:
237 case LibFunc::fmax: case LibFunc::fmaxf: case LibFunc::fmaxl:
238 case LibFunc::fmin: case LibFunc::fminf: case LibFunc::fminl:
239 case LibFunc::floor: case LibFunc::floorf: case LibFunc::floorl:
240 case LibFunc::nearbyint: case LibFunc::nearbyintf: case LibFunc::nearbyintl:
241 case LibFunc::ceil: case LibFunc::ceilf: case LibFunc::ceill:
242 case LibFunc::rint: case LibFunc::rintf: case LibFunc::rintl:
243 case LibFunc::round: case LibFunc::roundf: case LibFunc::roundl:
244 case LibFunc::trunc: case LibFunc::truncf: case LibFunc::truncl:
245 case LibFunc::log2: case LibFunc::log2f: case LibFunc::log2l:
246 case LibFunc::exp2: case LibFunc::exp2f: case LibFunc::exp2l:
247 case LibFunc::memcmp: case LibFunc::strcmp: case LibFunc::strcpy:
248 case LibFunc::stpcpy: case LibFunc::strlen: case LibFunc::strnlen:
249 case LibFunc::memchr:
255 StringRef getName(LibFunc::Func F) const {
256 auto State = Impl->getState(F);
257 if (State == TargetLibraryInfoImpl::Unavailable)
259 if (State == TargetLibraryInfoImpl::StandardName)
260 return Impl->StandardNames[F];
261 assert(State == TargetLibraryInfoImpl::CustomName);
262 return Impl->CustomNames.find(F)->second;
265 /// \brief Handle invalidation from the pass manager.
267 /// If we try to invalidate this info, just return false. It cannot become
268 /// invalid even if the module changes.
269 bool invalidate(Module &, const PreservedAnalyses &) { return false; }
272 /// \brief Analysis pass providing the \c TargetLibraryInfo.
274 /// Note that this pass's result cannot be invalidated, it is immutable for the
275 /// life of the module.
276 class TargetLibraryAnalysis {
278 typedef TargetLibraryInfo Result;
280 /// \brief Opaque, unique identifier for this analysis pass.
281 static void *ID() { return (void *)&PassID; }
283 /// \brief Default construct the library analysis.
285 /// This will use the module's triple to construct the library info for that
287 TargetLibraryAnalysis() {}
289 /// \brief Construct a library analysis with preset info.
291 /// This will directly copy the preset info into the result without
292 /// consulting the module's triple.
293 TargetLibraryAnalysis(TargetLibraryInfoImpl PresetInfoImpl)
294 : PresetInfoImpl(std::move(PresetInfoImpl)) {}
296 // Move semantics. We spell out the constructors for MSVC.
297 TargetLibraryAnalysis(TargetLibraryAnalysis &&Arg)
298 : PresetInfoImpl(std::move(Arg.PresetInfoImpl)), Impls(std::move(Arg.Impls)) {}
299 TargetLibraryAnalysis &operator=(TargetLibraryAnalysis &&RHS) {
300 PresetInfoImpl = std::move(RHS.PresetInfoImpl);
301 Impls = std::move(RHS.Impls);
305 TargetLibraryInfo run(Module &M);
306 TargetLibraryInfo run(Function &F);
308 /// \brief Provide access to a name for this pass for debugging purposes.
309 static StringRef name() { return "TargetLibraryAnalysis"; }
314 Optional<TargetLibraryInfoImpl> PresetInfoImpl;
316 StringMap<std::unique_ptr<TargetLibraryInfoImpl>> Impls;
318 TargetLibraryInfoImpl &lookupInfoImpl(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