LLVM  4.0.0
TargetLibraryInfo.h
Go to the documentation of this file.
1 //===-- TargetLibraryInfo.h - Library information ---------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_ANALYSIS_TARGETLIBRARYINFO_H
11 #define LLVM_ANALYSIS_TARGETLIBRARYINFO_H
12 
13 #include "llvm/ADT/DenseMap.h"
14 #include "llvm/ADT/Optional.h"
15 #include "llvm/ADT/Triple.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  namespace LibFunc {
34  enum Func {
35 #define TLI_DEFINE_ENUM
36 #include "llvm/Analysis/TargetLibraryInfo.def"
37 
39  };
40  }
41 
42 /// Implementation of the target library information.
43 ///
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.
49  friend class TargetLibraryInfo;
50 
51  unsigned char AvailableArray[(LibFunc::NumLibFuncs+3)/4];
53  static StringRef const StandardNames[LibFunc::NumLibFuncs];
54  bool ShouldExtI32Param, ShouldExtI32Return, ShouldSignExtI32Param;
55 
56  enum AvailabilityState {
57  StandardName = 3, // (memset to all ones)
58  CustomName = 1,
59  Unavailable = 0 // (memset to all zeros)
60  };
61  void setState(LibFunc::Func F, AvailabilityState State) {
62  AvailableArray[F/4] &= ~(3 << 2*(F&3));
63  AvailableArray[F/4] |= State << 2*(F&3);
64  }
65  AvailabilityState getState(LibFunc::Func F) const {
66  return static_cast<AvailabilityState>((AvailableArray[F/4] >> 2*(F&3)) & 3);
67  }
68 
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;
74 
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::Func F,
78  const DataLayout *DL) const;
79 
80 public:
81  /// List of known vector-functions libraries.
82  ///
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  SVML // Intel short vector math library.
92  };
93 
95  explicit TargetLibraryInfoImpl(const Triple &T);
96 
97  // Provide value semantics.
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::Func &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::Func &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.
146 
147  /// Calls addVectorizableFunctions with a known preset of functions for the
148  /// given vector library.
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 
198 /// Provides information about what library functions are available for
199 /// the current target.
200 ///
201 /// This both allows optimizations to handle them specially and frontends to
202 /// disable such optimizations through -fno-builtin etc.
204  friend class TargetLibraryAnalysis;
206 
207  const TargetLibraryInfoImpl *Impl;
208 
209 public:
210  explicit TargetLibraryInfo(const TargetLibraryInfoImpl &Impl) : Impl(&Impl) {}
211 
212  // Provide value semantics.
213  TargetLibraryInfo(const TargetLibraryInfo &TLI) : Impl(TLI.Impl) {}
214  TargetLibraryInfo(TargetLibraryInfo &&TLI) : Impl(TLI.Impl) {}
216  Impl = TLI.Impl;
217  return *this;
218  }
220  Impl = TLI.Impl;
221  return *this;
222  }
223 
224  /// Searches for a particular function name.
225  ///
226  /// If it is one of the known library functions, return true and set F to the
227  /// corresponding value.
228  bool getLibFunc(StringRef funcName, LibFunc::Func &F) const {
229  return Impl->getLibFunc(funcName, F);
230  }
231 
232  bool getLibFunc(const Function &FDecl, LibFunc::Func &F) const {
233  return Impl->getLibFunc(FDecl, F);
234  }
235 
236  /// Tests whether a library function is available.
237  bool has(LibFunc::Func F) const {
238  return Impl->getState(F) != TargetLibraryInfoImpl::Unavailable;
239  }
240  bool isFunctionVectorizable(StringRef F, unsigned VF) const {
241  return Impl->isFunctionVectorizable(F, VF);
242  }
244  return Impl->isFunctionVectorizable(F);
245  }
247  return Impl->getVectorizedFunction(F, VF);
248  }
249 
250  /// Tests if the function is both available and a candidate for optimized code
251  /// generation.
253  if (Impl->getState(F) == TargetLibraryInfoImpl::Unavailable)
254  return false;
255  switch (F) {
256  default: break;
257  case LibFunc::copysign: case LibFunc::copysignf: case LibFunc::copysignl:
258  case LibFunc::fabs: case LibFunc::fabsf: case LibFunc::fabsl:
259  case LibFunc::sin: case LibFunc::sinf: case LibFunc::sinl:
260  case LibFunc::cos: case LibFunc::cosf: case LibFunc::cosl:
261  case LibFunc::sqrt: case LibFunc::sqrtf: case LibFunc::sqrtl:
262  case LibFunc::sqrt_finite: case LibFunc::sqrtf_finite:
263  case LibFunc::sqrtl_finite:
264  case LibFunc::fmax: case LibFunc::fmaxf: case LibFunc::fmaxl:
265  case LibFunc::fmin: case LibFunc::fminf: case LibFunc::fminl:
266  case LibFunc::floor: case LibFunc::floorf: case LibFunc::floorl:
267  case LibFunc::nearbyint: case LibFunc::nearbyintf: case LibFunc::nearbyintl:
268  case LibFunc::ceil: case LibFunc::ceilf: case LibFunc::ceill:
269  case LibFunc::rint: case LibFunc::rintf: case LibFunc::rintl:
270  case LibFunc::round: case LibFunc::roundf: case LibFunc::roundl:
271  case LibFunc::trunc: case LibFunc::truncf: case LibFunc::truncl:
272  case LibFunc::log2: case LibFunc::log2f: case LibFunc::log2l:
273  case LibFunc::exp2: case LibFunc::exp2f: case LibFunc::exp2l:
274  case LibFunc::memcmp: case LibFunc::strcmp: case LibFunc::strcpy:
275  case LibFunc::stpcpy: case LibFunc::strlen: case LibFunc::strnlen:
276  case LibFunc::memchr: case LibFunc::mempcpy:
277  return true;
278  }
279  return false;
280  }
281 
283  auto State = Impl->getState(F);
284  if (State == TargetLibraryInfoImpl::Unavailable)
285  return StringRef();
286  if (State == TargetLibraryInfoImpl::StandardName)
287  return Impl->StandardNames[F];
288  assert(State == TargetLibraryInfoImpl::CustomName);
289  return Impl->CustomNames.find(F)->second;
290  }
291 
292  /// Returns extension attribute kind to be used for i32 parameters
293  /// corresponding to C-level int or unsigned int. May be zeroext, signext,
294  /// or none.
296  if (Impl->ShouldExtI32Param)
297  return Signed ? Attribute::SExt : Attribute::ZExt;
298  if (Impl->ShouldSignExtI32Param)
299  return Attribute::SExt;
300  return Attribute::None;
301  }
302 
303  /// Returns extension attribute kind to be used for i32 return values
304  /// corresponding to C-level int or unsigned int. May be zeroext, signext,
305  /// or none.
307  if (Impl->ShouldExtI32Return)
308  return Signed ? Attribute::SExt : Attribute::ZExt;
309  return Attribute::None;
310  }
311 
312  /// Handle invalidation from the pass manager.
313  ///
314  /// If we try to invalidate this info, just return false. It cannot become
315  /// invalid even if the module or function changes.
318  return false;
319  }
322  return false;
323  }
324 };
325 
326 /// Analysis pass providing the \c TargetLibraryInfo.
327 ///
328 /// Note that this pass's result cannot be invalidated, it is immutable for the
329 /// life of the module.
330 class TargetLibraryAnalysis : public AnalysisInfoMixin<TargetLibraryAnalysis> {
331 public:
333 
334  /// Default construct the library analysis.
335  ///
336  /// This will use the module's triple to construct the library info for that
337  /// module.
339 
340  /// Construct a library analysis with preset info.
341  ///
342  /// This will directly copy the preset info into the result without
343  /// consulting the module's triple.
345  : PresetInfoImpl(std::move(PresetInfoImpl)) {}
346 
349 
350 private:
352  static AnalysisKey Key;
353 
354  Optional<TargetLibraryInfoImpl> PresetInfoImpl;
355 
357 
358  TargetLibraryInfoImpl &lookupInfoImpl(const Triple &T);
359 };
360 
362  TargetLibraryInfoImpl TLIImpl;
363  TargetLibraryInfo TLI;
364 
365  virtual void anchor();
366 
367 public:
368  static char ID;
370  explicit TargetLibraryInfoWrapperPass(const Triple &T);
372 
373  TargetLibraryInfo &getTLI() { return TLI; }
374  const TargetLibraryInfo &getTLI() const { return TLI; }
375 };
376 
377 } // end namespace llvm
378 
379 #endif
bool getLibFunc(const Function &FDecl, LibFunc::Func &F) const
void setShouldExtI32Return(bool Val)
Set to true iff i32 results from library functions should have signext or zeroext attributes if they ...
void addVectorizableFunctions(ArrayRef< VecDesc > Fns)
Add a set of scalar -> vector mappings, queryable via getVectorizedFunction and getScalarizedFunction...
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:52
VectorLibrary
List of known vector-functions libraries.
bool isFunctionVectorizable(StringRef F) const
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...
static uint64_t round(uint64_t Acc, uint64_t Input)
Definition: xxhash.cpp:57
void disableAllFunctions()
Disables all builtins.
Implementation of the target library information.
bool isFunctionVectorizable(StringRef F, unsigned VF) const
Return true if the function F has a vector equivalent with vectorization factor VF.
TargetLibraryInfo & operator=(const TargetLibraryInfo &TLI)
void setAvailableWithName(LibFunc::Func F, StringRef Name)
Forces a function to be marked as available and provide an alternate name that must be used...
Maximum length of the test input libFuzzer tries to guess a good value based on the corpus and reports it always prefer smaller inputs during the corpus shuffle When libFuzzer itself reports a bug this exit code will be used If indicates the maximal total time in seconds to run the fuzzer minimizes the provided crash input Use with strcmp
TargetLibraryInfo & operator=(TargetLibraryInfo &&TLI)
StringRef getVectorizedFunction(StringRef F, unsigned VF) const
Return the name of the equivalent of F, vectorized with factor VF.
TargetLibraryAnalysis()
Default construct the library analysis.
StringRef ScalarFnName
bool has(LibFunc::Func F) const
Tests whether a library function is available.
bool hasOptimizedCodeGen(LibFunc::Func F) const
Tests if the function is both available and a candidate for optimized code generation.
No attributes have been set.
Definition: Attributes.h:69
void setUnavailable(LibFunc::Func F)
Forces a function to be marked as unavailable.
bool getLibFunc(StringRef funcName, LibFunc::Func &F) const
Searches for a particular function name.
const TargetLibraryInfo & getTLI() const
#define F(x, y, z)
Definition: MD5.cpp:51
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
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 ...
Attribute::AttrKind getExtAttrForI32Param(bool Signed=true) const
Returns extension attribute kind to be used for i32 parameters corresponding to C-level int or unsign...
TargetLibraryInfo run(Module &M, ModuleAnalysisManager &)
void setAvailable(LibFunc::Func F)
Forces a function to be marked as available.
bool getLibFunc(StringRef funcName, LibFunc::Func &F) const
Searches for a particular function name.
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:107
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...
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:328
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
unsigned VectorizationFactor
ImmutablePass class - This class is used to provide information that does not need to be run...
Definition: Pass.h:266
Describes a possible vectorization of a function.
Module.h This file contains the declarations for the Module class.
Provides information about what library functions are available for the current target.
StringRef getScalarizedFunction(StringRef F, unsigned &VF) const
Return the name of the equivalent of F, scalarized.
TargetLibraryInfoImpl & operator=(const TargetLibraryInfoImpl &TLI)
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Definition: StringMap.h:223
void addVectorizableFunctionsFromVecLib(enum VectorLibrary VecLib)
Calls addVectorizableFunctions with a known preset of functions for the given vector library...
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:130
StringRef getName(LibFunc::Func F) const
bool isFunctionVectorizable(StringRef F, unsigned VF) const
iterator end()
Definition: DenseMap.h:69
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...
iterator find(const KeyT &Val)
Definition: DenseMap.h:127
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:525
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())
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
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)
TargetLibraryInfo(const TargetLibraryInfoImpl &Impl)
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:64
AttrKind
This enumeration lists the attributes that can be associated with parameters, function results...
Definition: Attributes.h:67
StringRef getVectorizedFunction(StringRef F, unsigned VF) const