LLVM  9.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 {
34 #define TLI_DEFINE_ENUM
35 #include "llvm/Analysis/TargetLibraryInfo.def"
36 
38  };
39 
40 /// Implementation of the target library information.
41 ///
42 /// This class constructs tables that hold the target library information and
43 /// make it available. However, it is somewhat expensive to compute and only
44 /// depends on the triple. So users typically interact with the \c
45 /// TargetLibraryInfo wrapper below.
47  friend class TargetLibraryInfo;
48 
49  unsigned char AvailableArray[(NumLibFuncs+3)/4];
51  static StringRef const StandardNames[NumLibFuncs];
52  bool ShouldExtI32Param, ShouldExtI32Return, ShouldSignExtI32Param;
53 
54  enum AvailabilityState {
55  StandardName = 3, // (memset to all ones)
56  CustomName = 1,
57  Unavailable = 0 // (memset to all zeros)
58  };
59  void setState(LibFunc F, AvailabilityState State) {
60  AvailableArray[F/4] &= ~(3 << 2*(F&3));
61  AvailableArray[F/4] |= State << 2*(F&3);
62  }
63  AvailabilityState getState(LibFunc F) const {
64  return static_cast<AvailabilityState>((AvailableArray[F/4] >> 2*(F&3)) & 3);
65  }
66 
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;
72 
73  /// Return true if the function type FTy is valid for the library function
74  /// F, regardless of whether the function is available.
75  bool isValidProtoForLibFunc(const FunctionType &FTy, LibFunc F,
76  const DataLayout *DL) const;
77 
78 public:
79  /// List of known vector-functions libraries.
80  ///
81  /// The vector-functions library defines, which functions are vectorizable
82  /// and with which factor. The library can be specified by either frontend,
83  /// or a commandline option, and then used by
84  /// addVectorizableFunctionsFromVecLib for filling up the tables of
85  /// vectorizable functions.
87  NoLibrary, // Don't use any vector library.
88  Accelerate, // Use Accelerate framework.
89  SVML // Intel short vector math library.
90  };
91 
93  explicit TargetLibraryInfoImpl(const Triple &T);
94 
95  // Provide value semantics.
98  TargetLibraryInfoImpl &operator=(const TargetLibraryInfoImpl &TLI);
100 
101  /// Searches for a particular function name.
102  ///
103  /// If it is one of the known library functions, return true and set F to the
104  /// corresponding value.
105  bool getLibFunc(StringRef funcName, LibFunc &F) const;
106 
107  /// Searches for a particular function name, also checking that its type is
108  /// valid for the library function matching that name.
109  ///
110  /// If it is one of the known library functions, return true and set F to the
111  /// corresponding value.
112  bool getLibFunc(const Function &FDecl, LibFunc &F) const;
113 
114  /// Forces a function to be marked as unavailable.
116  setState(F, Unavailable);
117  }
118 
119  /// Forces a function to be marked as available.
121  setState(F, StandardName);
122  }
123 
124  /// Forces a function to be marked as available and provide an alternate name
125  /// that must be used.
127  if (StandardNames[F] != Name) {
128  setState(F, CustomName);
129  CustomNames[F] = Name;
130  assert(CustomNames.find(F) != CustomNames.end());
131  } else {
132  setState(F, StandardName);
133  }
134  }
135 
136  /// Disables all builtins.
137  ///
138  /// This can be used for options like -fno-builtin.
139  void disableAllFunctions();
140 
141  /// Add a set of scalar -> vector mappings, queryable via
142  /// getVectorizedFunction and getScalarizedFunction.
143  void addVectorizableFunctions(ArrayRef<VecDesc> Fns);
144 
145  /// Calls addVectorizableFunctions with a known preset of functions for the
146  /// given vector library.
147  void addVectorizableFunctionsFromVecLib(enum VectorLibrary VecLib);
148 
149  /// Return true if the function F has a vector equivalent with vectorization
150  /// factor VF.
151  bool isFunctionVectorizable(StringRef F, unsigned VF) const {
152  return !getVectorizedFunction(F, VF).empty();
153  }
154 
155  /// Return true if the function F has a vector equivalent with any
156  /// vectorization factor.
157  bool isFunctionVectorizable(StringRef F) const;
158 
159  /// Return the name of the equivalent of F, vectorized with factor VF. If no
160  /// such mapping exists, return the empty string.
161  StringRef getVectorizedFunction(StringRef F, unsigned VF) const;
162 
163  /// Return true if the function F has a scalar equivalent, and set VF to be
164  /// the vectorization factor.
165  bool isFunctionScalarizable(StringRef F, unsigned &VF) const {
166  return !getScalarizedFunction(F, VF).empty();
167  }
168 
169  /// Return the name of the equivalent of F, scalarized. If no such mapping
170  /// exists, return the empty string.
171  ///
172  /// Set VF to the vectorization factor.
173  StringRef getScalarizedFunction(StringRef F, unsigned &VF) const;
174 
175  /// Set to true iff i32 parameters to library functions should have signext
176  /// or zeroext attributes if they correspond to C-level int or unsigned int,
177  /// respectively.
178  void setShouldExtI32Param(bool Val) {
179  ShouldExtI32Param = Val;
180  }
181 
182  /// Set to true iff i32 results from library functions should have signext
183  /// or zeroext attributes if they correspond to C-level int or unsigned int,
184  /// respectively.
185  void setShouldExtI32Return(bool Val) {
186  ShouldExtI32Return = Val;
187  }
188 
189  /// Set to true iff i32 parameters to library functions should have signext
190  /// attribute if they correspond to C-level int or unsigned int.
191  void setShouldSignExtI32Param(bool Val) {
192  ShouldSignExtI32Param = Val;
193  }
194 
195  /// Returns the size of the wchar_t type in bytes or 0 if the size is unknown.
196  /// This queries the 'wchar_size' metadata.
197  unsigned getWCharSize(const Module &M) const;
198 };
199 
200 /// Provides information about what library functions are available for
201 /// the current target.
202 ///
203 /// This both allows optimizations to handle them specially and frontends to
204 /// disable such optimizations through -fno-builtin etc.
206  friend class TargetLibraryAnalysis;
208 
209  const TargetLibraryInfoImpl *Impl;
210 
211 public:
212  explicit TargetLibraryInfo(const TargetLibraryInfoImpl &Impl) : Impl(&Impl) {}
213 
214  // Provide value semantics.
215  TargetLibraryInfo(const TargetLibraryInfo &TLI) : Impl(TLI.Impl) {}
216  TargetLibraryInfo(TargetLibraryInfo &&TLI) : Impl(TLI.Impl) {}
218  Impl = TLI.Impl;
219  return *this;
220  }
222  Impl = TLI.Impl;
223  return *this;
224  }
225 
226  /// Searches for a particular function name.
227  ///
228  /// If it is one of the known library functions, return true and set F to the
229  /// corresponding value.
230  bool getLibFunc(StringRef funcName, LibFunc &F) const {
231  return Impl->getLibFunc(funcName, F);
232  }
233 
234  bool getLibFunc(const Function &FDecl, LibFunc &F) const {
235  return Impl->getLibFunc(FDecl, F);
236  }
237 
238  /// If a callsite does not have the 'nobuiltin' attribute, return if the
239  /// called function is a known library function and set F to that function.
241  return !CS.isNoBuiltin() && CS.getCalledFunction() &&
242  getLibFunc(*(CS.getCalledFunction()), F);
243  }
244 
245  /// Tests whether a library function is available.
246  bool has(LibFunc F) const {
247  return Impl->getState(F) != TargetLibraryInfoImpl::Unavailable;
248  }
249  bool isFunctionVectorizable(StringRef F, unsigned VF) const {
250  return Impl->isFunctionVectorizable(F, VF);
251  }
253  return Impl->isFunctionVectorizable(F);
254  }
256  return Impl->getVectorizedFunction(F, VF);
257  }
258 
259  /// Tests if the function is both available and a candidate for optimized code
260  /// generation.
262  if (Impl->getState(F) == TargetLibraryInfoImpl::Unavailable)
263  return false;
264  switch (F) {
265  default: break;
266  case LibFunc_copysign: case LibFunc_copysignf: case LibFunc_copysignl:
267  case LibFunc_fabs: case LibFunc_fabsf: case LibFunc_fabsl:
268  case LibFunc_sin: case LibFunc_sinf: case LibFunc_sinl:
269  case LibFunc_cos: case LibFunc_cosf: case LibFunc_cosl:
270  case LibFunc_sqrt: case LibFunc_sqrtf: case LibFunc_sqrtl:
271  case LibFunc_sqrt_finite: case LibFunc_sqrtf_finite:
272  case LibFunc_sqrtl_finite:
273  case LibFunc_fmax: case LibFunc_fmaxf: case LibFunc_fmaxl:
274  case LibFunc_fmin: case LibFunc_fminf: case LibFunc_fminl:
275  case LibFunc_floor: case LibFunc_floorf: case LibFunc_floorl:
276  case LibFunc_nearbyint: case LibFunc_nearbyintf: case LibFunc_nearbyintl:
277  case LibFunc_ceil: case LibFunc_ceilf: case LibFunc_ceill:
278  case LibFunc_rint: case LibFunc_rintf: case LibFunc_rintl:
279  case LibFunc_round: case LibFunc_roundf: case LibFunc_roundl:
280  case LibFunc_trunc: case LibFunc_truncf: case LibFunc_truncl:
281  case LibFunc_log2: case LibFunc_log2f: case LibFunc_log2l:
282  case LibFunc_exp2: case LibFunc_exp2f: case LibFunc_exp2l:
283  case LibFunc_memcmp: case LibFunc_bcmp: case LibFunc_strcmp:
284  case LibFunc_strcpy: case LibFunc_stpcpy: case LibFunc_strlen:
285  case LibFunc_strnlen: case LibFunc_memchr: case LibFunc_mempcpy:
286  return true;
287  }
288  return false;
289  }
290 
292  auto State = Impl->getState(F);
293  if (State == TargetLibraryInfoImpl::Unavailable)
294  return StringRef();
295  if (State == TargetLibraryInfoImpl::StandardName)
296  return Impl->StandardNames[F];
297  assert(State == TargetLibraryInfoImpl::CustomName);
298  return Impl->CustomNames.find(F)->second;
299  }
300 
301  /// Returns extension attribute kind to be used for i32 parameters
302  /// corresponding to C-level int or unsigned int. May be zeroext, signext,
303  /// or none.
305  if (Impl->ShouldExtI32Param)
306  return Signed ? Attribute::SExt : Attribute::ZExt;
307  if (Impl->ShouldSignExtI32Param)
308  return Attribute::SExt;
309  return Attribute::None;
310  }
311 
312  /// Returns extension attribute kind to be used for i32 return values
313  /// corresponding to C-level int or unsigned int. May be zeroext, signext,
314  /// or none.
316  if (Impl->ShouldExtI32Return)
317  return Signed ? Attribute::SExt : Attribute::ZExt;
318  return Attribute::None;
319  }
320 
321  /// \copydoc TargetLibraryInfoImpl::getWCharSize()
322  unsigned getWCharSize(const Module &M) const {
323  return Impl->getWCharSize(M);
324  }
325 
326  /// Handle invalidation from the pass manager.
327  ///
328  /// If we try to invalidate this info, just return false. It cannot become
329  /// invalid even if the module or function changes.
332  return false;
333  }
336  return false;
337  }
338 };
339 
340 /// Analysis pass providing the \c TargetLibraryInfo.
341 ///
342 /// Note that this pass's result cannot be invalidated, it is immutable for the
343 /// life of the module.
344 class TargetLibraryAnalysis : public AnalysisInfoMixin<TargetLibraryAnalysis> {
345 public:
347 
348  /// Default construct the library analysis.
349  ///
350  /// This will use the module's triple to construct the library info for that
351  /// module.
353 
354  /// Construct a library analysis with preset info.
355  ///
356  /// This will directly copy the preset info into the result without
357  /// consulting the module's triple.
359  : PresetInfoImpl(std::move(PresetInfoImpl)) {}
360 
363 
364 private:
366  static AnalysisKey Key;
367 
368  Optional<TargetLibraryInfoImpl> PresetInfoImpl;
369 
371 
372  TargetLibraryInfoImpl &lookupInfoImpl(const Triple &T);
373 };
374 
376  TargetLibraryInfoImpl TLIImpl;
377  TargetLibraryInfo TLI;
378 
379  virtual void anchor();
380 
381 public:
382  static char ID;
384  explicit TargetLibraryInfoWrapperPass(const Triple &T);
386 
387  TargetLibraryInfo &getTLI() { return TLI; }
388  const TargetLibraryInfo &getTLI() const { return TLI; }
389 };
390 
391 } // end namespace llvm
392 
393 #endif
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:110
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:71
Key
PAL metadata keys.
Class to represent function types.
Definition: DerivedTypes.h:102
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:176
bool isNoBuiltin() const
Return true if the call should not be treated as a call to a builtin.
Definition: CallSite.h:436
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:382
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
const TargetLibraryInfo & getTLI() 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:892
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:641
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:69