1 //===-- TargetLibraryInfo.h - Library information ---------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_ANALYSIS_TARGETLIBRARYINFO_H
10 #define LLVM_ANALYSIS_TARGETLIBRARYINFO_H
12 #include "llvm/ADT/BitVector.h"
13 #include "llvm/ADT/DenseMap.h"
14 #include "llvm/ADT/Optional.h"
15 #include "llvm/IR/Function.h"
16 #include "llvm/IR/InstrTypes.h"
17 #include "llvm/IR/Module.h"
18 #include "llvm/IR/PassManager.h"
19 #include "llvm/Pass.h"
22 template <typename T> class ArrayRef;
25 /// Describes a possible vectorization of a function.
26 /// Function 'VectorFnName' is equivalent to 'ScalarFnName' vectorized
27 /// by a factor 'VectorizationFactor'.
29 StringRef ScalarFnName;
30 StringRef VectorFnName;
31 unsigned VectorizationFactor;
34 enum LibFunc : unsigned {
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[(NumLibFuncs+3)/4];
52 llvm::DenseMap<unsigned, std::string> CustomNames;
53 static StringLiteral const StandardNames[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 F, AvailabilityState State) {
62 AvailableArray[F/4] &= ~(3 << 2*(F&3));
63 AvailableArray[F/4] |= State << 2*(F&3);
65 AvailabilityState getState(LibFunc 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 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 MASSV, // IBM MASS vector library.
92 SVML // Intel short vector math library.
95 TargetLibraryInfoImpl();
96 explicit TargetLibraryInfoImpl(const Triple &T);
98 // Provide value semantics.
99 TargetLibraryInfoImpl(const TargetLibraryInfoImpl &TLI);
100 TargetLibraryInfoImpl(TargetLibraryInfoImpl &&TLI);
101 TargetLibraryInfoImpl &operator=(const TargetLibraryInfoImpl &TLI);
102 TargetLibraryInfoImpl &operator=(TargetLibraryInfoImpl &&TLI);
104 /// Searches for a particular function name.
106 /// If it is one of the known library functions, return true and set F to the
107 /// corresponding value.
108 bool getLibFunc(StringRef funcName, LibFunc &F) const;
110 /// Searches for a particular function name, also checking that its type is
111 /// valid for the library function matching that name.
113 /// If it is one of the known library functions, return true and set F to the
114 /// corresponding value.
115 bool getLibFunc(const Function &FDecl, LibFunc &F) const;
117 /// Forces a function to be marked as unavailable.
118 void setUnavailable(LibFunc F) {
119 setState(F, Unavailable);
122 /// Forces a function to be marked as available.
123 void setAvailable(LibFunc F) {
124 setState(F, StandardName);
127 /// Forces a function to be marked as available and provide an alternate name
128 /// that must be used.
129 void setAvailableWithName(LibFunc F, StringRef Name) {
130 if (StandardNames[F] != Name) {
131 setState(F, CustomName);
132 CustomNames[F] = std::string(Name);
133 assert(CustomNames.find(F) != CustomNames.end());
135 setState(F, StandardName);
139 /// Disables all builtins.
141 /// This can be used for options like -fno-builtin.
142 void disableAllFunctions();
144 /// Add a set of scalar -> vector mappings, queryable via
145 /// getVectorizedFunction and getScalarizedFunction.
146 void addVectorizableFunctions(ArrayRef<VecDesc> Fns);
148 /// Calls addVectorizableFunctions with a known preset of functions for the
149 /// given vector library.
150 void addVectorizableFunctionsFromVecLib(enum VectorLibrary VecLib);
152 /// Return true if the function F has a vector equivalent with vectorization
154 bool isFunctionVectorizable(StringRef F, unsigned VF) const {
155 return !getVectorizedFunction(F, VF).empty();
158 /// Return true if the function F has a vector equivalent with any
159 /// vectorization factor.
160 bool isFunctionVectorizable(StringRef F) const;
162 /// Return the name of the equivalent of F, vectorized with factor VF. If no
163 /// such mapping exists, return the empty string.
164 StringRef getVectorizedFunction(StringRef F, unsigned VF) const;
166 /// Return true if the function F has a scalar equivalent, and set VF to be
167 /// the vectorization factor.
168 bool isFunctionScalarizable(StringRef F, unsigned &VF) const {
169 return !getScalarizedFunction(F, VF).empty();
172 /// Return the name of the equivalent of F, scalarized. If no such mapping
173 /// exists, return the empty string.
175 /// Set VF to the vectorization factor.
176 StringRef getScalarizedFunction(StringRef F, unsigned &VF) const;
178 /// Set to true iff i32 parameters to library functions should have signext
179 /// or zeroext attributes if they correspond to C-level int or unsigned int,
181 void setShouldExtI32Param(bool Val) {
182 ShouldExtI32Param = Val;
185 /// Set to true iff i32 results from library functions should have signext
186 /// or zeroext attributes if they correspond to C-level int or unsigned int,
188 void setShouldExtI32Return(bool Val) {
189 ShouldExtI32Return = Val;
192 /// Set to true iff i32 parameters to library functions should have signext
193 /// attribute if they correspond to C-level int or unsigned int.
194 void setShouldSignExtI32Param(bool Val) {
195 ShouldSignExtI32Param = Val;
198 /// Returns the size of the wchar_t type in bytes or 0 if the size is unknown.
199 /// This queries the 'wchar_size' metadata.
200 unsigned getWCharSize(const Module &M) const;
202 /// Returns the largest vectorization factor used in the list of
203 /// vector functions.
204 unsigned getWidestVF(StringRef ScalarF) const;
207 /// Provides information about what library functions are available for
208 /// the current target.
210 /// This both allows optimizations to handle them specially and frontends to
211 /// disable such optimizations through -fno-builtin etc.
212 class TargetLibraryInfo {
213 friend class TargetLibraryAnalysis;
214 friend class TargetLibraryInfoWrapperPass;
216 /// The global (module level) TLI info.
217 const TargetLibraryInfoImpl *Impl;
219 /// Support for -fno-builtin* options as function attributes, overrides
220 /// information in global TargetLibraryInfoImpl.
221 BitVector OverrideAsUnavailable;
224 explicit TargetLibraryInfo(const TargetLibraryInfoImpl &Impl,
225 Optional<const Function *> F = None)
226 : Impl(&Impl), OverrideAsUnavailable(NumLibFuncs) {
229 if ((*F)->hasFnAttribute("no-builtins"))
230 disableAllFunctions();
232 // Disable individual libc/libm calls in TargetLibraryInfo.
234 AttributeSet FnAttrs = (*F)->getAttributes().getFnAttributes();
235 for (const Attribute &Attr : FnAttrs) {
236 if (!Attr.isStringAttribute())
238 auto AttrStr = Attr.getKindAsString();
239 if (!AttrStr.consume_front("no-builtin-"))
241 if (getLibFunc(AttrStr, LF))
247 // Provide value semantics.
248 TargetLibraryInfo(const TargetLibraryInfo &TLI)
249 : Impl(TLI.Impl), OverrideAsUnavailable(TLI.OverrideAsUnavailable) {}
250 TargetLibraryInfo(TargetLibraryInfo &&TLI)
251 : Impl(TLI.Impl), OverrideAsUnavailable(TLI.OverrideAsUnavailable) {}
252 TargetLibraryInfo &operator=(const TargetLibraryInfo &TLI) {
254 OverrideAsUnavailable = TLI.OverrideAsUnavailable;
257 TargetLibraryInfo &operator=(TargetLibraryInfo &&TLI) {
259 OverrideAsUnavailable = TLI.OverrideAsUnavailable;
263 /// Determine whether a callee with the given TLI can be inlined into
264 /// caller with this TLI, based on 'nobuiltin' attributes. When requested,
265 /// allow inlining into a caller with a superset of the callee's nobuiltin
266 /// attributes, which is conservatively correct.
267 bool areInlineCompatible(const TargetLibraryInfo &CalleeTLI,
268 bool AllowCallerSuperset) const {
269 if (!AllowCallerSuperset)
270 return OverrideAsUnavailable == CalleeTLI.OverrideAsUnavailable;
271 BitVector B = OverrideAsUnavailable;
272 B |= CalleeTLI.OverrideAsUnavailable;
273 // We can inline if the union of the caller and callee's nobuiltin
274 // attributes is no stricter than the caller's nobuiltin attributes.
275 return B == OverrideAsUnavailable;
278 /// Searches for a particular function name.
280 /// If it is one of the known library functions, return true and set F to the
281 /// corresponding value.
282 bool getLibFunc(StringRef funcName, LibFunc &F) const {
283 return Impl->getLibFunc(funcName, F);
286 bool getLibFunc(const Function &FDecl, LibFunc &F) const {
287 return Impl->getLibFunc(FDecl, F);
290 /// If a callbase does not have the 'nobuiltin' attribute, return if the
291 /// called function is a known library function and set F to that function.
292 bool getLibFunc(const CallBase &CB, LibFunc &F) const {
293 return !CB.isNoBuiltin() && CB.getCalledFunction() &&
294 getLibFunc(*(CB.getCalledFunction()), F);
297 /// Disables all builtins.
299 /// This can be used for options like -fno-builtin.
300 void disableAllFunctions() LLVM_ATTRIBUTE_UNUSED {
301 OverrideAsUnavailable.set();
304 /// Forces a function to be marked as unavailable.
305 void setUnavailable(LibFunc F) LLVM_ATTRIBUTE_UNUSED {
306 OverrideAsUnavailable.set(F);
309 TargetLibraryInfoImpl::AvailabilityState getState(LibFunc F) const {
310 if (OverrideAsUnavailable[F])
311 return TargetLibraryInfoImpl::Unavailable;
312 return Impl->getState(F);
315 /// Tests whether a library function is available.
316 bool has(LibFunc F) const {
317 return getState(F) != TargetLibraryInfoImpl::Unavailable;
319 bool isFunctionVectorizable(StringRef F, unsigned VF) const {
320 return Impl->isFunctionVectorizable(F, VF);
322 bool isFunctionVectorizable(StringRef F) const {
323 return Impl->isFunctionVectorizable(F);
325 StringRef getVectorizedFunction(StringRef F, unsigned VF) const {
326 return Impl->getVectorizedFunction(F, VF);
329 /// Tests if the function is both available and a candidate for optimized code
331 bool hasOptimizedCodeGen(LibFunc F) const {
332 if (getState(F) == TargetLibraryInfoImpl::Unavailable)
336 case LibFunc_copysign: case LibFunc_copysignf: case LibFunc_copysignl:
337 case LibFunc_fabs: case LibFunc_fabsf: case LibFunc_fabsl:
338 case LibFunc_sin: case LibFunc_sinf: case LibFunc_sinl:
339 case LibFunc_cos: case LibFunc_cosf: case LibFunc_cosl:
340 case LibFunc_sqrt: case LibFunc_sqrtf: case LibFunc_sqrtl:
341 case LibFunc_sqrt_finite: case LibFunc_sqrtf_finite:
342 case LibFunc_sqrtl_finite:
343 case LibFunc_fmax: case LibFunc_fmaxf: case LibFunc_fmaxl:
344 case LibFunc_fmin: case LibFunc_fminf: case LibFunc_fminl:
345 case LibFunc_floor: case LibFunc_floorf: case LibFunc_floorl:
346 case LibFunc_nearbyint: case LibFunc_nearbyintf: case LibFunc_nearbyintl:
347 case LibFunc_ceil: case LibFunc_ceilf: case LibFunc_ceill:
348 case LibFunc_rint: case LibFunc_rintf: case LibFunc_rintl:
349 case LibFunc_round: case LibFunc_roundf: case LibFunc_roundl:
350 case LibFunc_trunc: case LibFunc_truncf: case LibFunc_truncl:
351 case LibFunc_log2: case LibFunc_log2f: case LibFunc_log2l:
352 case LibFunc_exp2: case LibFunc_exp2f: case LibFunc_exp2l:
353 case LibFunc_memcpy: case LibFunc_memset: case LibFunc_memmove:
354 case LibFunc_memcmp: case LibFunc_bcmp: case LibFunc_strcmp:
355 case LibFunc_strcpy: case LibFunc_stpcpy: case LibFunc_strlen:
356 case LibFunc_strnlen: case LibFunc_memchr: case LibFunc_mempcpy:
362 StringRef getName(LibFunc F) const {
363 auto State = getState(F);
364 if (State == TargetLibraryInfoImpl::Unavailable)
366 if (State == TargetLibraryInfoImpl::StandardName)
367 return Impl->StandardNames[F];
368 assert(State == TargetLibraryInfoImpl::CustomName);
369 return Impl->CustomNames.find(F)->second;
372 /// Returns extension attribute kind to be used for i32 parameters
373 /// corresponding to C-level int or unsigned int. May be zeroext, signext,
375 Attribute::AttrKind getExtAttrForI32Param(bool Signed = true) const {
376 if (Impl->ShouldExtI32Param)
377 return Signed ? Attribute::SExt : Attribute::ZExt;
378 if (Impl->ShouldSignExtI32Param)
379 return Attribute::SExt;
380 return Attribute::None;
383 /// Returns extension attribute kind to be used for i32 return values
384 /// corresponding to C-level int or unsigned int. May be zeroext, signext,
386 Attribute::AttrKind getExtAttrForI32Return(bool Signed = true) const {
387 if (Impl->ShouldExtI32Return)
388 return Signed ? Attribute::SExt : Attribute::ZExt;
389 return Attribute::None;
392 /// \copydoc TargetLibraryInfoImpl::getWCharSize()
393 unsigned getWCharSize(const Module &M) const {
394 return Impl->getWCharSize(M);
397 /// Handle invalidation from the pass manager.
399 /// If we try to invalidate this info, just return false. It cannot become
400 /// invalid even if the module or function changes.
401 bool invalidate(Module &, const PreservedAnalyses &,
402 ModuleAnalysisManager::Invalidator &) {
405 bool invalidate(Function &, const PreservedAnalyses &,
406 FunctionAnalysisManager::Invalidator &) {
409 /// Returns the largest vectorization factor used in the list of
410 /// vector functions.
411 unsigned getWidestVF(StringRef ScalarF) const {
412 return Impl->getWidestVF(ScalarF);
415 /// Check if the function "F" is listed in a library known to LLVM.
416 bool isKnownVectorFunctionInLibrary(StringRef F) const {
417 return this->isFunctionVectorizable(F);
421 /// Analysis pass providing the \c TargetLibraryInfo.
423 /// Note that this pass's result cannot be invalidated, it is immutable for the
424 /// life of the module.
425 class TargetLibraryAnalysis : public AnalysisInfoMixin<TargetLibraryAnalysis> {
427 typedef TargetLibraryInfo Result;
429 /// Default construct the library analysis.
431 /// This will use the module's triple to construct the library info for that
433 TargetLibraryAnalysis() {}
435 /// Construct a library analysis with baseline Module-level info.
437 /// This will be supplemented with Function-specific info in the Result.
438 TargetLibraryAnalysis(TargetLibraryInfoImpl BaselineInfoImpl)
439 : BaselineInfoImpl(std::move(BaselineInfoImpl)) {}
441 TargetLibraryInfo run(const Function &F, FunctionAnalysisManager &);
444 friend AnalysisInfoMixin<TargetLibraryAnalysis>;
445 static AnalysisKey Key;
447 Optional<TargetLibraryInfoImpl> BaselineInfoImpl;
450 class TargetLibraryInfoWrapperPass : public ImmutablePass {
451 TargetLibraryAnalysis TLA;
452 Optional<TargetLibraryInfo> TLI;
454 virtual void anchor();
458 TargetLibraryInfoWrapperPass();
459 explicit TargetLibraryInfoWrapperPass(const Triple &T);
460 explicit TargetLibraryInfoWrapperPass(const TargetLibraryInfoImpl &TLI);
462 TargetLibraryInfo &getTLI(const Function &F) {
463 FunctionAnalysisManager DummyFAM;
464 TLI = TLA.run(F, DummyFAM);
469 } // end namespace llvm