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"
22 /// VecDesc - Describes a possible vectorization of a function.
23 /// Function 'VectorFnName' is equivalent to 'ScalarFnName' vectorized
24 /// by a factor 'VectorizationFactor'.
26 const char *ScalarFnName;
27 const char *VectorFnName;
28 unsigned VectorizationFactor;
30 class PreservedAnalyses;
34 #define TLI_DEFINE_ENUM
35 #include "llvm/Analysis/TargetLibraryInfo.def"
41 /// \brief 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 typicaly interact with the \c
46 /// TargetLibraryInfo wrapper below.
47 class TargetLibraryInfoImpl {
48 friend class TargetLibraryInfo;
50 unsigned char AvailableArray[(LibFunc::NumLibFuncs+3)/4];
51 llvm::DenseMap<unsigned, std::string> CustomNames;
52 static const char *const StandardNames[LibFunc::NumLibFuncs];
54 enum AvailabilityState {
55 StandardName = 3, // (memset to all ones)
57 Unavailable = 0 // (memset to all zeros)
59 void setState(LibFunc::Func F, AvailabilityState State) {
60 AvailableArray[F/4] &= ~(3 << 2*(F&3));
61 AvailableArray[F/4] |= State << 2*(F&3);
63 AvailabilityState getState(LibFunc::Func F) const {
64 return static_cast<AvailabilityState>((AvailableArray[F/4] >> 2*(F&3)) & 3);
67 /// Vectorization descriptors - sorted by ScalarFnName.
68 std::vector<VecDesc> VectorDescs;
69 /// Scalarization descriptors - same content as VectorDescs but sorted based
70 /// on VectorFnName rather than ScalarFnName.
71 std::vector<VecDesc> ScalarDescs;
74 TargetLibraryInfoImpl();
75 explicit TargetLibraryInfoImpl(const Triple &T);
77 // Provide value semantics.
78 TargetLibraryInfoImpl(const TargetLibraryInfoImpl &TLI);
79 TargetLibraryInfoImpl(TargetLibraryInfoImpl &&TLI);
80 TargetLibraryInfoImpl &operator=(const TargetLibraryInfoImpl &TLI);
81 TargetLibraryInfoImpl &operator=(TargetLibraryInfoImpl &&TLI);
83 /// \brief Searches for a particular function name.
85 /// If it is one of the known library functions, return true and set F to the
86 /// corresponding value.
87 bool getLibFunc(StringRef funcName, LibFunc::Func &F) const;
89 /// \brief Forces a function to be marked as unavailable.
90 void setUnavailable(LibFunc::Func F) {
91 setState(F, Unavailable);
94 /// \brief Forces a function to be marked as available.
95 void setAvailable(LibFunc::Func F) {
96 setState(F, StandardName);
99 /// \brief Forces a function to be marked as available and provide an
100 /// alternate name that must be used.
101 void setAvailableWithName(LibFunc::Func F, StringRef Name) {
102 if (StandardNames[F] != Name) {
103 setState(F, CustomName);
104 CustomNames[F] = Name;
105 assert(CustomNames.find(F) != CustomNames.end());
107 setState(F, StandardName);
111 /// \brief Disables all builtins.
113 /// This can be used for options like -fno-builtin.
114 void disableAllFunctions();
116 /// addVectorizableFunctions - Add a set of scalar -> vector mappings,
117 /// queryable via getVectorizedFunction and getScalarizedFunction.
118 void addVectorizableFunctions(ArrayRef<VecDesc> Fns);
120 /// isFunctionVectorizable - Return true if the function F has a
121 /// vector equivalent with vectorization factor VF.
122 bool isFunctionVectorizable(StringRef F, unsigned VF) const {
123 return !getVectorizedFunction(F, VF).empty();
126 /// isFunctionVectorizable - Return true if the function F has a
127 /// vector equivalent with any vectorization factor.
128 bool isFunctionVectorizable(StringRef F) const;
130 /// getVectorizedFunction - Return the name of the equivalent of
131 /// F, vectorized with factor VF. If no such mapping exists,
132 /// return the empty string.
133 StringRef getVectorizedFunction(StringRef F, unsigned VF) const;
135 /// isFunctionScalarizable - Return true if the function F has a
136 /// scalar equivalent, and set VF to be the vectorization factor.
137 bool isFunctionScalarizable(StringRef F, unsigned &VF) const {
138 return !getScalarizedFunction(F, VF).empty();
141 /// getScalarizedFunction - Return the name of the equivalent of
142 /// F, scalarized. If no such mapping exists, return the empty string.
144 /// Set VF to the vectorization factor.
145 StringRef getScalarizedFunction(StringRef F, unsigned &VF) const;
148 /// \brief Provides information about what library functions are available for
149 /// the current target.
151 /// This both allows optimizations to handle them specially and frontends to
152 /// disable such optimizations through -fno-builtin etc.
153 class TargetLibraryInfo {
154 friend class TargetLibraryAnalysis;
155 friend class TargetLibraryInfoWrapperPass;
157 const TargetLibraryInfoImpl *Impl;
160 explicit TargetLibraryInfo(const TargetLibraryInfoImpl &Impl) : Impl(&Impl) {}
162 // Provide value semantics.
163 TargetLibraryInfo(const TargetLibraryInfo &TLI) : Impl(TLI.Impl) {}
164 TargetLibraryInfo(TargetLibraryInfo &&TLI) : Impl(TLI.Impl) {}
165 TargetLibraryInfo &operator=(const TargetLibraryInfo &TLI) {
169 TargetLibraryInfo &operator=(TargetLibraryInfo &&TLI) {
174 /// \brief Searches for a particular function name.
176 /// If it is one of the known library functions, return true and set F to the
177 /// corresponding value.
178 bool getLibFunc(StringRef funcName, LibFunc::Func &F) const {
179 return Impl->getLibFunc(funcName, F);
182 /// \brief Tests whether a library function is available.
183 bool has(LibFunc::Func F) const {
184 return Impl->getState(F) != TargetLibraryInfoImpl::Unavailable;
186 bool isFunctionVectorizable(StringRef F, unsigned VF) const {
187 return Impl->isFunctionVectorizable(F, VF);
189 bool isFunctionVectorizable(StringRef F) const {
190 return Impl->isFunctionVectorizable(F);
192 StringRef getVectorizedFunction(StringRef F, unsigned VF) const {
193 return Impl->getVectorizedFunction(F, VF);
196 /// \brief Tests if the function is both available and a candidate for
197 /// optimized code generation.
198 bool hasOptimizedCodeGen(LibFunc::Func F) const {
199 if (Impl->getState(F) == TargetLibraryInfoImpl::Unavailable)
203 case LibFunc::copysign: case LibFunc::copysignf: case LibFunc::copysignl:
204 case LibFunc::fabs: case LibFunc::fabsf: case LibFunc::fabsl:
205 case LibFunc::sin: case LibFunc::sinf: case LibFunc::sinl:
206 case LibFunc::cos: case LibFunc::cosf: case LibFunc::cosl:
207 case LibFunc::sqrt: case LibFunc::sqrtf: case LibFunc::sqrtl:
208 case LibFunc::sqrt_finite: case LibFunc::sqrtf_finite:
209 case LibFunc::sqrtl_finite:
210 case LibFunc::fmax: case LibFunc::fmaxf: case LibFunc::fmaxl:
211 case LibFunc::fmin: case LibFunc::fminf: case LibFunc::fminl:
212 case LibFunc::floor: case LibFunc::floorf: case LibFunc::floorl:
213 case LibFunc::nearbyint: case LibFunc::nearbyintf: case LibFunc::nearbyintl:
214 case LibFunc::ceil: case LibFunc::ceilf: case LibFunc::ceill:
215 case LibFunc::rint: case LibFunc::rintf: case LibFunc::rintl:
216 case LibFunc::round: case LibFunc::roundf: case LibFunc::roundl:
217 case LibFunc::trunc: case LibFunc::truncf: case LibFunc::truncl:
218 case LibFunc::log2: case LibFunc::log2f: case LibFunc::log2l:
219 case LibFunc::exp2: case LibFunc::exp2f: case LibFunc::exp2l:
220 case LibFunc::memcmp: case LibFunc::strcmp: case LibFunc::strcpy:
221 case LibFunc::stpcpy: case LibFunc::strlen: case LibFunc::strnlen:
222 case LibFunc::memchr:
228 StringRef getName(LibFunc::Func F) const {
229 auto State = Impl->getState(F);
230 if (State == TargetLibraryInfoImpl::Unavailable)
232 if (State == TargetLibraryInfoImpl::StandardName)
233 return Impl->StandardNames[F];
234 assert(State == TargetLibraryInfoImpl::CustomName);
235 return Impl->CustomNames.find(F)->second;
238 /// \brief Handle invalidation from the pass manager.
240 /// If we try to invalidate this info, just return false. It cannot become
241 /// invalid even if the module changes.
242 bool invalidate(Module &, const PreservedAnalyses &) { return false; }
245 /// \brief Analysis pass providing the \c TargetLibraryInfo.
247 /// Note that this pass's result cannot be invalidated, it is immutable for the
248 /// life of the module.
249 class TargetLibraryAnalysis {
251 typedef TargetLibraryInfo Result;
253 /// \brief Opaque, unique identifier for this analysis pass.
254 static void *ID() { return (void *)&PassID; }
256 /// \brief Default construct the library analysis.
258 /// This will use the module's triple to construct the library info for that
260 TargetLibraryAnalysis() {}
262 /// \brief Construct a library analysis with preset info.
264 /// This will directly copy the preset info into the result without
265 /// consulting the module's triple.
266 TargetLibraryAnalysis(TargetLibraryInfoImpl PresetInfoImpl)
267 : PresetInfoImpl(std::move(PresetInfoImpl)) {}
269 // Move semantics. We spell out the constructors for MSVC.
270 TargetLibraryAnalysis(TargetLibraryAnalysis &&Arg)
271 : PresetInfoImpl(std::move(Arg.PresetInfoImpl)), Impls(std::move(Arg.Impls)) {}
272 TargetLibraryAnalysis &operator=(TargetLibraryAnalysis &&RHS) {
273 PresetInfoImpl = std::move(RHS.PresetInfoImpl);
274 Impls = std::move(RHS.Impls);
278 TargetLibraryInfo run(Module &M);
279 TargetLibraryInfo run(Function &F);
281 /// \brief Provide access to a name for this pass for debugging purposes.
282 static StringRef name() { return "TargetLibraryAnalysis"; }
287 Optional<TargetLibraryInfoImpl> PresetInfoImpl;
289 StringMap<std::unique_ptr<TargetLibraryInfoImpl>> Impls;
291 TargetLibraryInfoImpl &lookupInfoImpl(Triple T);
294 class TargetLibraryInfoWrapperPass : public ImmutablePass {
295 TargetLibraryInfoImpl TLIImpl;
296 TargetLibraryInfo TLI;
298 virtual void anchor();
302 TargetLibraryInfoWrapperPass();
303 explicit TargetLibraryInfoWrapperPass(const Triple &T);
304 explicit TargetLibraryInfoWrapperPass(const TargetLibraryInfoImpl &TLI);
306 TargetLibraryInfo &getTLI() { return TLI; }
307 const TargetLibraryInfo &getTLI() const { return TLI; }
310 } // end namespace llvm