TargetLibraryInfo.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. #ifndef LLVM_ANALYSIS_TARGETLIBRARYINFO_H
  10. #define LLVM_ANALYSIS_TARGETLIBRARYINFO_H
  11. #include "llvm/ADT/DenseMap.h"
  12. #include "llvm/ADT/ArrayRef.h"
  13. #include "llvm/ADT/Optional.h"
  14. #include "llvm/ADT/Triple.h"
  15. #include "llvm/IR/Function.h"
  16. #include "llvm/IR/Module.h"
  17. #include "llvm/Pass.h"
  18. namespace llvm {
  19. /// VecDesc - Describes a possible vectorization of a function.
  20. /// Function 'VectorFnName' is equivalent to 'ScalarFnName' vectorized
  21. /// by a factor 'VectorizationFactor'.
  22. struct VecDesc {
  23. const char *ScalarFnName;
  24. const char *VectorFnName;
  25. unsigned VectorizationFactor;
  26. };
  27. class PreservedAnalyses;
  28. namespace LibFunc {
  29. enum Func {
  30. #define TLI_DEFINE_ENUM
  31. #include "llvm/Analysis/TargetLibraryInfo.def"
  32. NumLibFuncs
  33. };
  34. }
  35. /// \brief Implementation of the target library information.
  36. ///
  37. /// This class constructs tables that hold the target library information and
  38. /// make it available. However, it is somewhat expensive to compute and only
  39. /// depends on the triple. So users typicaly interact with the \c
  40. /// TargetLibraryInfo wrapper below.
  41. class TargetLibraryInfoImpl {
  42. friend class TargetLibraryInfo;
  43. unsigned char AvailableArray[(LibFunc::NumLibFuncs+3)/4];
  44. llvm::DenseMap<unsigned, std::string> CustomNames;
  45. static const char *const StandardNames[LibFunc::NumLibFuncs];
  46. enum AvailabilityState {
  47. StandardName = 3, // (memset to all ones)
  48. CustomName = 1,
  49. Unavailable = 0 // (memset to all zeros)
  50. };
  51. void setState(LibFunc::Func F, AvailabilityState State) {
  52. AvailableArray[F/4] &= ~(3 << 2*(F&3));
  53. AvailableArray[F/4] |= State << 2*(F&3);
  54. }
  55. AvailabilityState getState(LibFunc::Func F) const {
  56. return static_cast<AvailabilityState>((AvailableArray[F/4] >> 2*(F&3)) & 3);
  57. }
  58. /// Vectorization descriptors - sorted by ScalarFnName.
  59. std::vector<VecDesc> VectorDescs;
  60. /// Scalarization descriptors - same content as VectorDescs but sorted based
  61. /// on VectorFnName rather than ScalarFnName.
  62. std::vector<VecDesc> ScalarDescs;
  63. public:
  64. /// \brief List of known vector-functions libraries.
  65. ///
  66. /// The vector-functions library defines, which functions are vectorizable
  67. /// and with which factor. The library can be specified by either frontend,
  68. /// or a commandline option, and then used by
  69. /// addVectorizableFunctionsFromVecLib for filling up the tables of
  70. /// vectorizable functions.
  71. enum VectorLibrary {
  72. NoLibrary, // Don't use any vector library.
  73. Accelerate // Use Accelerate framework.
  74. };
  75. TargetLibraryInfoImpl();
  76. explicit TargetLibraryInfoImpl(const Triple &T);
  77. // Provide value semantics.
  78. TargetLibraryInfoImpl(const TargetLibraryInfoImpl &TLI);
  79. TargetLibraryInfoImpl(TargetLibraryInfoImpl &&TLI);
  80. TargetLibraryInfoImpl &operator=(const TargetLibraryInfoImpl &TLI);
  81. TargetLibraryInfoImpl &operator=(TargetLibraryInfoImpl &&TLI);
  82. /// \brief Searches for a particular function name.
  83. ///
  84. /// If it is one of the known library functions, return true and set F to the
  85. /// corresponding value.
  86. bool getLibFunc(StringRef funcName, LibFunc::Func &F) const;
  87. /// \brief Forces a function to be marked as unavailable.
  88. void setUnavailable(LibFunc::Func F) {
  89. setState(F, Unavailable);
  90. }
  91. /// \brief Forces a function to be marked as available.
  92. void setAvailable(LibFunc::Func F) {
  93. setState(F, StandardName);
  94. }
  95. /// \brief Forces a function to be marked as available and provide an
  96. /// alternate name that must be used.
  97. void setAvailableWithName(LibFunc::Func F, StringRef Name) {
  98. if (StandardNames[F] != Name) {
  99. setState(F, CustomName);
  100. CustomNames[F] = Name;
  101. assert(CustomNames.find(F) != CustomNames.end());
  102. } else {
  103. setState(F, StandardName);
  104. }
  105. }
  106. /// \brief Disables all builtins.
  107. ///
  108. /// This can be used for options like -fno-builtin.
  109. void disableAllFunctions();
  110. /// addVectorizableFunctions - Add a set of scalar -> vector mappings,
  111. /// queryable via getVectorizedFunction and getScalarizedFunction.
  112. void addVectorizableFunctions(ArrayRef<VecDesc> Fns);
  113. /// Calls addVectorizableFunctions with a known preset of functions for the
  114. /// given vector library.
  115. void addVectorizableFunctionsFromVecLib(enum VectorLibrary VecLib);
  116. /// isFunctionVectorizable - Return true if the function F has a
  117. /// vector equivalent with vectorization factor VF.
  118. bool isFunctionVectorizable(StringRef F, unsigned VF) const {
  119. return !getVectorizedFunction(F, VF).empty();
  120. }
  121. /// isFunctionVectorizable - Return true if the function F has a
  122. /// vector equivalent with any vectorization factor.
  123. bool isFunctionVectorizable(StringRef F) const;
  124. /// getVectorizedFunction - Return the name of the equivalent of
  125. /// F, vectorized with factor VF. If no such mapping exists,
  126. /// return the empty string.
  127. StringRef getVectorizedFunction(StringRef F, unsigned VF) const;
  128. /// isFunctionScalarizable - Return true if the function F has a
  129. /// scalar equivalent, and set VF to be the vectorization factor.
  130. bool isFunctionScalarizable(StringRef F, unsigned &VF) const {
  131. return !getScalarizedFunction(F, VF).empty();
  132. }
  133. /// getScalarizedFunction - Return the name of the equivalent of
  134. /// F, scalarized. If no such mapping exists, return the empty string.
  135. ///
  136. /// Set VF to the vectorization factor.
  137. StringRef getScalarizedFunction(StringRef F, unsigned &VF) const;
  138. };
  139. /// \brief Provides information about what library functions are available for
  140. /// the current target.
  141. ///
  142. /// This both allows optimizations to handle them specially and frontends to
  143. /// disable such optimizations through -fno-builtin etc.
  144. class TargetLibraryInfo {
  145. friend class TargetLibraryAnalysis;
  146. friend class TargetLibraryInfoWrapperPass;
  147. const TargetLibraryInfoImpl *Impl;
  148. public:
  149. explicit TargetLibraryInfo(const TargetLibraryInfoImpl &Impl) : Impl(&Impl) {}
  150. // Provide value semantics.
  151. TargetLibraryInfo(const TargetLibraryInfo &TLI) : Impl(TLI.Impl) {}
  152. TargetLibraryInfo(TargetLibraryInfo &&TLI) : Impl(TLI.Impl) {}
  153. TargetLibraryInfo &operator=(const TargetLibraryInfo &TLI) {
  154. Impl = TLI.Impl;
  155. return *this;
  156. }
  157. TargetLibraryInfo &operator=(TargetLibraryInfo &&TLI) {
  158. Impl = TLI.Impl;
  159. return *this;
  160. }
  161. /// \brief Searches for a particular function name.
  162. ///
  163. /// If it is one of the known library functions, return true and set F to the
  164. /// corresponding value.
  165. bool getLibFunc(StringRef funcName, LibFunc::Func &F) const {
  166. return Impl->getLibFunc(funcName, F);
  167. }
  168. /// \brief Tests whether a library function is available.
  169. bool has(LibFunc::Func F) const {
  170. return Impl->getState(F) != TargetLibraryInfoImpl::Unavailable;
  171. }
  172. bool isFunctionVectorizable(StringRef F, unsigned VF) const {
  173. return Impl->isFunctionVectorizable(F, VF);
  174. };
  175. bool isFunctionVectorizable(StringRef F) const {
  176. return Impl->isFunctionVectorizable(F);
  177. };
  178. StringRef getVectorizedFunction(StringRef F, unsigned VF) const {
  179. return Impl->getVectorizedFunction(F, VF);
  180. };
  181. /// \brief Tests if the function is both available and a candidate for
  182. /// optimized code generation.
  183. bool hasOptimizedCodeGen(LibFunc::Func F) const {
  184. if (Impl->getState(F) == TargetLibraryInfoImpl::Unavailable)
  185. return false;
  186. switch (F) {
  187. default: break;
  188. case LibFunc::copysign: case LibFunc::copysignf: case LibFunc::copysignl:
  189. case LibFunc::fabs: case LibFunc::fabsf: case LibFunc::fabsl:
  190. case LibFunc::sin: case LibFunc::sinf: case LibFunc::sinl:
  191. case LibFunc::cos: case LibFunc::cosf: case LibFunc::cosl:
  192. case LibFunc::sqrt: case LibFunc::sqrtf: case LibFunc::sqrtl:
  193. case LibFunc::sqrt_finite: case LibFunc::sqrtf_finite:
  194. case LibFunc::sqrtl_finite:
  195. case LibFunc::fmax: case LibFunc::fmaxf: case LibFunc::fmaxl:
  196. case LibFunc::fmin: case LibFunc::fminf: case LibFunc::fminl:
  197. case LibFunc::floor: case LibFunc::floorf: case LibFunc::floorl:
  198. case LibFunc::nearbyint: case LibFunc::nearbyintf: case LibFunc::nearbyintl:
  199. case LibFunc::ceil: case LibFunc::ceilf: case LibFunc::ceill:
  200. case LibFunc::rint: case LibFunc::rintf: case LibFunc::rintl:
  201. case LibFunc::round: case LibFunc::roundf: case LibFunc::roundl:
  202. case LibFunc::trunc: case LibFunc::truncf: case LibFunc::truncl:
  203. case LibFunc::log2: case LibFunc::log2f: case LibFunc::log2l:
  204. case LibFunc::exp2: case LibFunc::exp2f: case LibFunc::exp2l:
  205. case LibFunc::memcmp: case LibFunc::strcmp: case LibFunc::strcpy:
  206. case LibFunc::stpcpy: case LibFunc::strlen: case LibFunc::strnlen:
  207. case LibFunc::memchr:
  208. return true;
  209. }
  210. return false;
  211. }
  212. StringRef getName(LibFunc::Func F) const {
  213. auto State = Impl->getState(F);
  214. if (State == TargetLibraryInfoImpl::Unavailable)
  215. return StringRef();
  216. if (State == TargetLibraryInfoImpl::StandardName)
  217. return Impl->StandardNames[F];
  218. assert(State == TargetLibraryInfoImpl::CustomName);
  219. return Impl->CustomNames.find(F)->second;
  220. }
  221. /// \brief Handle invalidation from the pass manager.
  222. ///
  223. /// If we try to invalidate this info, just return false. It cannot become
  224. /// invalid even if the module changes.
  225. bool invalidate(Module &, const PreservedAnalyses &) { return false; }
  226. };
  227. /// \brief Analysis pass providing the \c TargetLibraryInfo.
  228. ///
  229. /// Note that this pass's result cannot be invalidated, it is immutable for the
  230. /// life of the module.
  231. class TargetLibraryAnalysis {
  232. public:
  233. typedef TargetLibraryInfo Result;
  234. /// \brief Opaque, unique identifier for this analysis pass.
  235. static void *ID() { return (void *)&PassID; }
  236. /// \brief Default construct the library analysis.
  237. ///
  238. /// This will use the module's triple to construct the library info for that
  239. /// module.
  240. TargetLibraryAnalysis() {}
  241. /// \brief Construct a library analysis with preset info.
  242. ///
  243. /// This will directly copy the preset info into the result without
  244. /// consulting the module's triple.
  245. TargetLibraryAnalysis(TargetLibraryInfoImpl PresetInfoImpl)
  246. : PresetInfoImpl(std::move(PresetInfoImpl)) {}
  247. // Move semantics. We spell out the constructors for MSVC.
  248. TargetLibraryAnalysis(TargetLibraryAnalysis &&Arg)
  249. : PresetInfoImpl(std::move(Arg.PresetInfoImpl)), Impls(std::move(Arg.Impls)) {}
  250. TargetLibraryAnalysis &operator=(TargetLibraryAnalysis &&RHS) {
  251. PresetInfoImpl = std::move(RHS.PresetInfoImpl);
  252. Impls = std::move(RHS.Impls);
  253. return *this;
  254. }
  255. TargetLibraryInfo run(Module &M);
  256. TargetLibraryInfo run(Function &F);
  257. /// \brief Provide access to a name for this pass for debugging purposes.
  258. static StringRef name() { return "TargetLibraryAnalysis"; }
  259. private:
  260. static char PassID;
  261. Optional<TargetLibraryInfoImpl> PresetInfoImpl;
  262. StringMap<std::unique_ptr<TargetLibraryInfoImpl>> Impls;
  263. TargetLibraryInfoImpl &lookupInfoImpl(Triple T);
  264. };
  265. class TargetLibraryInfoWrapperPass : public ImmutablePass {
  266. TargetLibraryInfoImpl TLIImpl;
  267. TargetLibraryInfo TLI;
  268. virtual void anchor();
  269. public:
  270. static char ID;
  271. TargetLibraryInfoWrapperPass();
  272. explicit TargetLibraryInfoWrapperPass(const Triple &T);
  273. explicit TargetLibraryInfoWrapperPass(const TargetLibraryInfoImpl &TLI);
  274. TargetLibraryInfo &getTLI() { return TLI; }
  275. const TargetLibraryInfo &getTLI() const { return TLI; }
  276. };
  277. } // end namespace llvm
  278. #endif