LLVM  10.0.0svn
TargetLibraryInfo.h
Go to the documentation of this file.
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 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_ANALYSIS_TARGETLIBRARYINFO_H
10 #define LLVM_ANALYSIS_TARGETLIBRARYINFO_H
11 
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"
20 
21 namespace llvm {
22 template <typename T> class ArrayRef;
23 
24 /// Describes a possible vectorization of a function.
25 /// Function 'VectorFnName' is equivalent to 'ScalarFnName' vectorized
26 /// by a factor 'VectorizationFactor'.
27 struct VecDesc {
31 };
32 
33  enum LibFunc : unsigned {
34 #define TLI_DEFINE_ENUM
35 #include "llvm/Analysis/TargetLibraryInfo.def"
36 
39  };
40 
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.
48  friend class TargetLibraryInfo;
49 
50  unsigned char AvailableArray[(NumLibFuncs+3)/4];
52  static StringLiteral const StandardNames[NumLibFuncs];
53  bool ShouldExtI32Param, ShouldExtI32Return, ShouldSignExtI32Param;
54 
55  enum AvailabilityState {
56  StandardName = 3, // (memset to all ones)
57  CustomName = 1,
58  Unavailable = 0 // (memset to all zeros)
59  };
60  void setState(LibFunc F, AvailabilityState State) {
61  AvailableArray[F/4] &= ~(3 << 2*(F&3));
62  AvailableArray[F/4] |= State << 2*(F&3);
63  }
64  AvailabilityState getState(LibFunc F) const {
65  return static_cast<AvailabilityState>((AvailableArray[F/4] >> 2*(F&3)) & 3);
66  }
67 
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;
73 
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;
78 
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.
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.
92  };
93 
95  explicit TargetLibraryInfoImpl(const Triple &T);
96 
97  // Provide value semantics.
100  TargetLibraryInfoImpl &operator=(const TargetLibraryInfoImpl &TLI);
102 
103  /// Searches for a particular function name.
104  ///
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;
108 
109  /// Searches for a particular function name, also checking that its type is
110  /// valid for the library function matching that name.
111  ///
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;
115 
116  /// Forces a function to be marked as unavailable.
118  setState(F, Unavailable);
119  }
120 
121  /// Forces a function to be marked as available.
123  setState(F, StandardName);
124  }
125 
126  /// Forces a function to be marked as available and provide an alternate name
127  /// that must be used.
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);
135  }
136  }
137 
138  /// Disables all builtins.
139  ///
140  /// This can be used for options like -fno-builtin.
141  void disableAllFunctions();
142 
143  /// Add a set of scalar -> vector mappings, queryable via
144  /// getVectorizedFunction and getScalarizedFunction.
145  void addVectorizableFunctions(ArrayRef<VecDesc> Fns);
146 
147  /// Calls addVectorizableFunctions with a known preset of functions for the
148  /// given vector library.
149  void addVectorizableFunctionsFromVecLib(enum VectorLibrary VecLib);
150 
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();
155  }
156 
157  /// Return true if the function F has a vector equivalent with any
158  /// vectorization factor.
159  bool isFunctionVectorizable(StringRef F) const;
160 
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;
164 
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();
169  }
170 
171  /// Return the name of the equivalent of F, scalarized. If no such mapping
172  /// exists, return the empty string.
173  ///
174  /// Set VF to the vectorization factor.
175  StringRef getScalarizedFunction(StringRef F, unsigned &VF) const;
176 
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;
182  }
183 
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;
189  }
190 
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;
195  }
196 
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;
200 };
201 
202 /// Provides information about what library functions are available for
203 /// the current target.
204 ///
205 /// This both allows optimizations to handle them specially and frontends to
206 /// disable such optimizations through -fno-builtin etc.
208  friend class TargetLibraryAnalysis;
210 
211  const TargetLibraryInfoImpl *Impl;
212 
213 public:
214  explicit TargetLibraryInfo(const TargetLibraryInfoImpl &Impl) : Impl(&Impl) {}
215 
216  // Provide value semantics.
217  TargetLibraryInfo(const TargetLibraryInfo &TLI) : Impl(TLI.Impl) {}
218  TargetLibraryInfo(TargetLibraryInfo &&TLI) : Impl(TLI.Impl) {}
220  Impl = TLI.Impl;
221  return *this;
222  }
224  Impl = TLI.Impl;
225  return *this;
226  }
227 
228  /// Searches for a particular function name.
229  ///
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);
234  }
235 
236  bool getLibFunc(const Function &FDecl, LibFunc &F) const {
237  return Impl->getLibFunc(FDecl, F);
238  }
239 
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.
243  return !CS.isNoBuiltin() && CS.getCalledFunction() &&
244  getLibFunc(*(CS.getCalledFunction()), F);
245  }
246 
247  /// Tests whether a library function is available.
248  bool has(LibFunc F) const {
249  return Impl->getState(F) != TargetLibraryInfoImpl::Unavailable;
250  }
251  bool isFunctionVectorizable(StringRef F, unsigned VF) const {
252  return Impl->isFunctionVectorizable(F, VF);
253  }
255  return Impl->isFunctionVectorizable(F);
256  }
258  return Impl->getVectorizedFunction(F, VF);
259  }
260 
261  /// Tests if the function is both available and a candidate for optimized code
262  /// generation.
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;
289  }
290  return false;
291  }
292 
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;
301  }
302 
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.
307  if (Impl->ShouldExtI32Param)
308  return Signed ? Attribute::SExt : Attribute::ZExt;
309  if (Impl->ShouldSignExtI32Param)
310  return Attribute::SExt;
311  return Attribute::None;
312  }
313 
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.
318  if (Impl->ShouldExtI32Return)
319  return Signed ? Attribute::SExt : Attribute::ZExt;
320  return Attribute::None;
321  }
322 
323  /// \copydoc TargetLibraryInfoImpl::getWCharSize()
324  unsigned getWCharSize(const Module &M) const {
325  return Impl->getWCharSize(M);
326  }
327 
328  /// Handle invalidation from the pass manager.
329  ///
330  /// If we try to invalidate this info, just return false. It cannot become
331  /// invalid even if the module or function changes.
334  return false;
335  }
338  return false;
339  }
340 };
341 
342 /// Analysis pass providing the \c TargetLibraryInfo.
343 ///
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:
349 
350  /// Default construct the library analysis.
351  ///
352  /// This will use the module's triple to construct the library info for that
353  /// module.
355 
356  /// Construct a library analysis with preset info.
357  ///
358  /// This will directly copy the preset info into the result without
359  /// consulting the module's triple.
361  : PresetInfoImpl(std::move(PresetInfoImpl)) {}
362 
364 
365 private:
367  static AnalysisKey Key;
368 
369  Optional<TargetLibraryInfoImpl> PresetInfoImpl;
370 
372 
373  TargetLibraryInfoImpl &lookupInfoImpl(const Triple &T);
374 };
375 
377  TargetLibraryInfoImpl TLIImpl;
378  TargetLibraryInfo TLI;
379 
380  virtual void anchor();
381 
382 public:
383  static char ID;
385  explicit TargetLibraryInfoWrapperPass(const Triple &T);
387 
389  return TLI;
390  }
391  const TargetLibraryInfo &
393  return TLI;
394  }
395 };
396 
397 } // end namespace llvm
398 
399 #endif
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:111
TargetLibraryInfo & getTLI(const Function &F LLVM_ATTRIBUTE_UNUSED)
This class represents lattice values for constants.
Definition: AllocatorList.h:23
void setShouldExtI32Return(bool Val)
Set to true iff i32 results from library functions should have signext or zeroext attributes if they ...
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
VectorLibrary
List of known vector-functions libraries.
StringRef VectorFnName
TargetLibraryInfo(const TargetLibraryInfo &TLI)
void setShouldExtI32Param(bool Val)
Set to true iff i32 parameters to library functions should have signext or zeroext attributes if they...
StringRef getName(LibFunc F) const
Implementation of the target library information.
F(f)
FunTy * getCalledFunction() const
Return the function being called if this is a direct call, otherwise return null (if it&#39;s an indirect...
Definition: CallSite.h:111
TargetLibraryInfo & operator=(const TargetLibraryInfo &TLI)
bool isFunctionScalarizable(StringRef F, unsigned &VF) const
Return true if the function F has a scalar equivalent, and set VF to be the vectorization factor...
bool getLibFunc(const Function &FDecl, LibFunc &F) const
TargetLibraryInfo & operator=(TargetLibraryInfo &&TLI)
Attribute::AttrKind getExtAttrForI32Param(bool Signed=true) const
Returns extension attribute kind to be used for i32 parameters corresponding to C-level int or unsign...
TargetLibraryAnalysis()
Default construct the library analysis.
StringRef ScalarFnName
Definition: BitVector.h:937
Attribute::AttrKind getExtAttrForI32Return(bool Signed=true) const
Returns extension attribute kind to be used for i32 return values corresponding to C-level int or uns...
No attributes have been set.
Definition: Attributes.h:72
Key
PAL metadata keys.
Class to represent function types.
Definition: DerivedTypes.h:103
bool getLibFunc(StringRef funcName, LibFunc &F) const
Searches for a particular function name.
bool has(LibFunc F) const
Tests whether a library function is available.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
bool invalidate(Function &, const PreservedAnalyses &, FunctionAnalysisManager::Invalidator &)
void setShouldSignExtI32Param(bool Val)
Set to true iff i32 parameters to library functions should have signext attribute if they correspond ...
void setUnavailable(LibFunc F)
Forces a function to be marked as unavailable.
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:150
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
Definition: StringRef.h:852
bool isNoBuiltin() const
Return true if the call should not be treated as a call to a builtin.
Definition: CallSite.h:441
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:153
bool isFunctionVectorizable(StringRef F, unsigned VF) const
Return true if the function F has a vector equivalent with vectorization factor VF.
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:389
#define LLVM_ATTRIBUTE_UNUSED
Definition: Compiler.h:172
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:43
unsigned VectorizationFactor
ImmutablePass class - This class is used to provide information that does not need to be run...
Definition: Pass.h:255
Describes a possible vectorization of a function.
bool isFunctionVectorizable(StringRef F, unsigned VF) const
Module.h This file contains the declarations for the Module class.
Provides information about what library functions are available for the current target.
StringRef getVectorizedFunction(StringRef F, unsigned VF) const
Return the name of the equivalent of F, vectorized with factor VF.
bool hasOptimizedCodeGen(LibFunc F) const
Tests if the function is both available and a candidate for optimized code generation.
StringRef getVectorizedFunction(StringRef F, unsigned VF) const
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Definition: StringMap.h:219
bool getLibFunc(ImmutableCallSite CS, LibFunc &F) const
If a callsite does not have the &#39;nobuiltin&#39; attribute, return if the called function is a known libra...
bool getLibFunc(StringRef funcName, LibFunc &F) const
Searches for a particular function name.
bool isFunctionVectorizable(StringRef F) const
Establish a view to a call site for examination.
Definition: CallSite.h:897
unsigned getWCharSize(const Module &M) const
Returns the size of the wchar_t type in bytes or 0 if the size is unknown.
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:648
TargetLibraryAnalysis(TargetLibraryInfoImpl PresetInfoImpl)
Construct a library analysis with preset info.
bool invalidate(Module &, const PreservedAnalyses &, ModuleAnalysisManager::Invalidator &)
Handle invalidation from the pass manager.
Analysis pass providing the TargetLibraryInfo.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void setAvailableWithName(LibFunc F, StringRef Name)
Forces a function to be marked as available and provide an alternate name that must be used...
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
A container for analyses that lazily runs them and caches their results.
This header defines various interfaces for pass management in LLVM.
TargetLibraryInfo(TargetLibraryInfo &&TLI)
void setAvailable(LibFunc F)
Forces a function to be marked as available.
TargetLibraryInfo(const TargetLibraryInfoImpl &Impl)
unsigned getWCharSize(const Module &M) const
Returns the size of the wchar_t type in bytes or 0 if the size is unknown.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:70
AttrKind
This enumeration lists the attributes that can be associated with parameters, function results...
Definition: Attributes.h:70
const TargetLibraryInfo & getTLI(const Function &F LLVM_ATTRIBUTE_UNUSED) const