[Alignment][NFC] Support compile time constants
[llvm-core.git] / include / llvm / Analysis / TargetLibraryInfo.h
blobd4b223863c5490e0722d88a4d6f5495631687e90
1 //===-- TargetLibraryInfo.h - Library information ---------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_ANALYSIS_TARGETLIBRARYINFO_H
10 #define LLVM_ANALYSIS_TARGETLIBRARYINFO_H
12 #include "llvm/ADT/DenseMap.h"
13 #include "llvm/ADT/Optional.h"
14 #include "llvm/ADT/Triple.h"
15 #include "llvm/IR/CallSite.h"
16 #include "llvm/IR/Function.h"
17 #include "llvm/IR/Module.h"
18 #include "llvm/IR/PassManager.h"
19 #include "llvm/Pass.h"
21 namespace llvm {
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'.
27 struct VecDesc {
28 StringRef ScalarFnName;
29 StringRef VectorFnName;
30 unsigned VectorizationFactor;
33 enum LibFunc : unsigned {
34 #define TLI_DEFINE_ENUM
35 #include "llvm/Analysis/TargetLibraryInfo.def"
37 NumLibFuncs,
38 NotLibFunc
41 /// Implementation of the target library information.
42 ///
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 StringLiteral const StandardNames[NumLibFuncs];
53 bool ShouldExtI32Param, ShouldExtI32Return, ShouldSignExtI32Param;
55 enum AvailabilityState {
56 StandardName = 3, // (memset to all ones)
57 CustomName = 1,
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;
79 public:
80 /// List of known vector-functions libraries.
81 ///
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.
87 enum VectorLibrary {
88 NoLibrary, // Don't use any vector library.
89 Accelerate, // Use Accelerate framework.
90 MASSV, // IBM MASS vector library.
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 &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 &F) const;
116 /// Forces a function to be marked as unavailable.
117 void setUnavailable(LibFunc F) {
118 setState(F, Unavailable);
121 /// Forces a function to be marked as available.
122 void setAvailable(LibFunc 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 F, StringRef Name) {
129 if (StandardNames[F] != Name) {
130 setState(F, CustomName);
131 CustomNames[F] = Name;
132 assert(CustomNames.find(F) != CustomNames.end());
133 } else {
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
152 /// factor VF.
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,
179 /// respectively.
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,
186 /// respectively.
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;
197 /// Returns the size of the wchar_t type in bytes or 0 if the size is unknown.
198 /// This queries the 'wchar_size' metadata.
199 unsigned getWCharSize(const Module &M) const;
202 /// Provides information about what library functions are available for
203 /// the current target.
205 /// This both allows optimizations to handle them specially and frontends to
206 /// disable such optimizations through -fno-builtin etc.
207 class TargetLibraryInfo {
208 friend class TargetLibraryAnalysis;
209 friend class TargetLibraryInfoWrapperPass;
211 const TargetLibraryInfoImpl *Impl;
213 public:
214 explicit TargetLibraryInfo(const TargetLibraryInfoImpl &Impl) : Impl(&Impl) {}
216 // Provide value semantics.
217 TargetLibraryInfo(const TargetLibraryInfo &TLI) : Impl(TLI.Impl) {}
218 TargetLibraryInfo(TargetLibraryInfo &&TLI) : Impl(TLI.Impl) {}
219 TargetLibraryInfo &operator=(const TargetLibraryInfo &TLI) {
220 Impl = TLI.Impl;
221 return *this;
223 TargetLibraryInfo &operator=(TargetLibraryInfo &&TLI) {
224 Impl = TLI.Impl;
225 return *this;
228 /// Searches for a particular function name.
230 /// If it is one of the known library functions, return true and set F to the
231 /// corresponding value.
232 bool getLibFunc(StringRef funcName, LibFunc &F) const {
233 return Impl->getLibFunc(funcName, F);
236 bool getLibFunc(const Function &FDecl, LibFunc &F) const {
237 return Impl->getLibFunc(FDecl, F);
240 /// If a callsite does not have the 'nobuiltin' attribute, return if the
241 /// called function is a known library function and set F to that function.
242 bool getLibFunc(ImmutableCallSite CS, LibFunc &F) const {
243 return !CS.isNoBuiltin() && CS.getCalledFunction() &&
244 getLibFunc(*(CS.getCalledFunction()), F);
247 /// Tests whether a library function is available.
248 bool has(LibFunc F) const {
249 return Impl->getState(F) != TargetLibraryInfoImpl::Unavailable;
251 bool isFunctionVectorizable(StringRef F, unsigned VF) const {
252 return Impl->isFunctionVectorizable(F, VF);
254 bool isFunctionVectorizable(StringRef F) const {
255 return Impl->isFunctionVectorizable(F);
257 StringRef getVectorizedFunction(StringRef F, unsigned VF) const {
258 return Impl->getVectorizedFunction(F, VF);
261 /// Tests if the function is both available and a candidate for optimized code
262 /// generation.
263 bool hasOptimizedCodeGen(LibFunc F) const {
264 if (Impl->getState(F) == TargetLibraryInfoImpl::Unavailable)
265 return false;
266 switch (F) {
267 default: break;
268 case LibFunc_copysign: case LibFunc_copysignf: case LibFunc_copysignl:
269 case LibFunc_fabs: case LibFunc_fabsf: case LibFunc_fabsl:
270 case LibFunc_sin: case LibFunc_sinf: case LibFunc_sinl:
271 case LibFunc_cos: case LibFunc_cosf: case LibFunc_cosl:
272 case LibFunc_sqrt: case LibFunc_sqrtf: case LibFunc_sqrtl:
273 case LibFunc_sqrt_finite: case LibFunc_sqrtf_finite:
274 case LibFunc_sqrtl_finite:
275 case LibFunc_fmax: case LibFunc_fmaxf: case LibFunc_fmaxl:
276 case LibFunc_fmin: case LibFunc_fminf: case LibFunc_fminl:
277 case LibFunc_floor: case LibFunc_floorf: case LibFunc_floorl:
278 case LibFunc_nearbyint: case LibFunc_nearbyintf: case LibFunc_nearbyintl:
279 case LibFunc_ceil: case LibFunc_ceilf: case LibFunc_ceill:
280 case LibFunc_rint: case LibFunc_rintf: case LibFunc_rintl:
281 case LibFunc_round: case LibFunc_roundf: case LibFunc_roundl:
282 case LibFunc_trunc: case LibFunc_truncf: case LibFunc_truncl:
283 case LibFunc_log2: case LibFunc_log2f: case LibFunc_log2l:
284 case LibFunc_exp2: case LibFunc_exp2f: case LibFunc_exp2l:
285 case LibFunc_memcmp: case LibFunc_bcmp: case LibFunc_strcmp:
286 case LibFunc_strcpy: case LibFunc_stpcpy: case LibFunc_strlen:
287 case LibFunc_strnlen: case LibFunc_memchr: case LibFunc_mempcpy:
288 return true;
290 return false;
293 StringRef getName(LibFunc F) const {
294 auto State = Impl->getState(F);
295 if (State == TargetLibraryInfoImpl::Unavailable)
296 return StringRef();
297 if (State == TargetLibraryInfoImpl::StandardName)
298 return Impl->StandardNames[F];
299 assert(State == TargetLibraryInfoImpl::CustomName);
300 return Impl->CustomNames.find(F)->second;
303 /// Returns extension attribute kind to be used for i32 parameters
304 /// corresponding to C-level int or unsigned int. May be zeroext, signext,
305 /// or none.
306 Attribute::AttrKind getExtAttrForI32Param(bool Signed = true) const {
307 if (Impl->ShouldExtI32Param)
308 return Signed ? Attribute::SExt : Attribute::ZExt;
309 if (Impl->ShouldSignExtI32Param)
310 return Attribute::SExt;
311 return Attribute::None;
314 /// Returns extension attribute kind to be used for i32 return values
315 /// corresponding to C-level int or unsigned int. May be zeroext, signext,
316 /// or none.
317 Attribute::AttrKind getExtAttrForI32Return(bool Signed = true) const {
318 if (Impl->ShouldExtI32Return)
319 return Signed ? Attribute::SExt : Attribute::ZExt;
320 return Attribute::None;
323 /// \copydoc TargetLibraryInfoImpl::getWCharSize()
324 unsigned getWCharSize(const Module &M) const {
325 return Impl->getWCharSize(M);
328 /// Handle invalidation from the pass manager.
330 /// If we try to invalidate this info, just return false. It cannot become
331 /// invalid even if the module or function changes.
332 bool invalidate(Module &, const PreservedAnalyses &,
333 ModuleAnalysisManager::Invalidator &) {
334 return false;
336 bool invalidate(Function &, const PreservedAnalyses &,
337 FunctionAnalysisManager::Invalidator &) {
338 return false;
342 /// Analysis pass providing the \c TargetLibraryInfo.
344 /// Note that this pass's result cannot be invalidated, it is immutable for the
345 /// life of the module.
346 class TargetLibraryAnalysis : public AnalysisInfoMixin<TargetLibraryAnalysis> {
347 public:
348 typedef TargetLibraryInfo Result;
350 /// Default construct the library analysis.
352 /// This will use the module's triple to construct the library info for that
353 /// module.
354 TargetLibraryAnalysis() {}
356 /// Construct a library analysis with preset info.
358 /// This will directly copy the preset info into the result without
359 /// consulting the module's triple.
360 TargetLibraryAnalysis(TargetLibraryInfoImpl PresetInfoImpl)
361 : PresetInfoImpl(std::move(PresetInfoImpl)) {}
363 TargetLibraryInfo run(Function &F, FunctionAnalysisManager &);
365 private:
366 friend AnalysisInfoMixin<TargetLibraryAnalysis>;
367 static AnalysisKey Key;
369 Optional<TargetLibraryInfoImpl> PresetInfoImpl;
371 StringMap<std::unique_ptr<TargetLibraryInfoImpl>> Impls;
373 TargetLibraryInfoImpl &lookupInfoImpl(const Triple &T);
376 class TargetLibraryInfoWrapperPass : public ImmutablePass {
377 TargetLibraryInfoImpl TLIImpl;
378 TargetLibraryInfo TLI;
380 virtual void anchor();
382 public:
383 static char ID;
384 TargetLibraryInfoWrapperPass();
385 explicit TargetLibraryInfoWrapperPass(const Triple &T);
386 explicit TargetLibraryInfoWrapperPass(const TargetLibraryInfoImpl &TLI);
388 TargetLibraryInfo &getTLI(const Function &F LLVM_ATTRIBUTE_UNUSED) {
389 return TLI;
391 const TargetLibraryInfo &
392 getTLI(const Function &F LLVM_ATTRIBUTE_UNUSED) const {
393 return TLI;
397 } // end namespace llvm
399 #endif