TargetLibraryInfo.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- TargetLibraryInfo.h - Library information ---------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_ANALYSIS_TARGETLIBRARYINFO_H
  14. #define LLVM_ANALYSIS_TARGETLIBRARYINFO_H
  15. #include "llvm/ADT/BitVector.h"
  16. #include "llvm/ADT/DenseMap.h"
  17. #include "llvm/ADT/Optional.h"
  18. #include "llvm/IR/Function.h"
  19. #include "llvm/IR/InstrTypes.h"
  20. #include "llvm/IR/Module.h"
  21. #include "llvm/IR/PassManager.h"
  22. #include "llvm/Pass.h"
  23. namespace llvm {
  24. template <typename T> class ArrayRef;
  25. class Triple;
  26. /// Describes a possible vectorization of a function.
  27. /// Function 'VectorFnName' is equivalent to 'ScalarFnName' vectorized
  28. /// by a factor 'VectorizationFactor'.
  29. struct VecDesc {
  30. StringRef ScalarFnName;
  31. StringRef VectorFnName;
  32. ElementCount VectorizationFactor;
  33. };
  34. enum LibFunc : unsigned {
  35. #define TLI_DEFINE_ENUM
  36. #include "llvm/Analysis/TargetLibraryInfo.def"
  37. NumLibFuncs,
  38. NotLibFunc
  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.
  46. class TargetLibraryInfoImpl {
  47. friend class TargetLibraryInfo;
  48. unsigned char AvailableArray[(NumLibFuncs+3)/4];
  49. llvm::DenseMap<unsigned, std::string> CustomNames;
  50. static StringLiteral const StandardNames[NumLibFuncs];
  51. bool ShouldExtI32Param, ShouldExtI32Return, ShouldSignExtI32Param;
  52. unsigned SizeOfInt;
  53. enum AvailabilityState {
  54. StandardName = 3, // (memset to all ones)
  55. CustomName = 1,
  56. Unavailable = 0 // (memset to all zeros)
  57. };
  58. void setState(LibFunc F, AvailabilityState State) {
  59. AvailableArray[F/4] &= ~(3 << 2*(F&3));
  60. AvailableArray[F/4] |= State << 2*(F&3);
  61. }
  62. AvailabilityState getState(LibFunc F) const {
  63. return static_cast<AvailabilityState>((AvailableArray[F/4] >> 2*(F&3)) & 3);
  64. }
  65. /// Vectorization descriptors - sorted by ScalarFnName.
  66. std::vector<VecDesc> VectorDescs;
  67. /// Scalarization descriptors - same content as VectorDescs but sorted based
  68. /// on VectorFnName rather than ScalarFnName.
  69. std::vector<VecDesc> ScalarDescs;
  70. /// Return true if the function type FTy is valid for the library function
  71. /// F, regardless of whether the function is available.
  72. bool isValidProtoForLibFunc(const FunctionType &FTy, LibFunc F,
  73. const Module &M) const;
  74. public:
  75. /// List of known vector-functions libraries.
  76. ///
  77. /// The vector-functions library defines, which functions are vectorizable
  78. /// and with which factor. The library can be specified by either frontend,
  79. /// or a commandline option, and then used by
  80. /// addVectorizableFunctionsFromVecLib for filling up the tables of
  81. /// vectorizable functions.
  82. enum VectorLibrary {
  83. NoLibrary, // Don't use any vector library.
  84. Accelerate, // Use Accelerate framework.
  85. DarwinLibSystemM, // Use Darwin's libsystem_m.
  86. LIBMVEC_X86, // GLIBC Vector Math library.
  87. MASSV, // IBM MASS vector library.
  88. SVML // Intel short vector math library.
  89. };
  90. TargetLibraryInfoImpl();
  91. explicit TargetLibraryInfoImpl(const Triple &T);
  92. // Provide value semantics.
  93. TargetLibraryInfoImpl(const TargetLibraryInfoImpl &TLI);
  94. TargetLibraryInfoImpl(TargetLibraryInfoImpl &&TLI);
  95. TargetLibraryInfoImpl &operator=(const TargetLibraryInfoImpl &TLI);
  96. TargetLibraryInfoImpl &operator=(TargetLibraryInfoImpl &&TLI);
  97. /// Searches for a particular function name.
  98. ///
  99. /// If it is one of the known library functions, return true and set F to the
  100. /// corresponding value.
  101. bool getLibFunc(StringRef funcName, LibFunc &F) const;
  102. /// Searches for a particular function name, also checking that its type is
  103. /// valid for the library function matching that name.
  104. ///
  105. /// If it is one of the known library functions, return true and set F to the
  106. /// corresponding value.
  107. ///
  108. /// FDecl is assumed to have a parent Module when using this function.
  109. bool getLibFunc(const Function &FDecl, LibFunc &F) const;
  110. /// Forces a function to be marked as unavailable.
  111. void setUnavailable(LibFunc F) {
  112. setState(F, Unavailable);
  113. }
  114. /// Forces a function to be marked as available.
  115. void setAvailable(LibFunc F) {
  116. setState(F, StandardName);
  117. }
  118. /// Forces a function to be marked as available and provide an alternate name
  119. /// that must be used.
  120. void setAvailableWithName(LibFunc F, StringRef Name) {
  121. if (StandardNames[F] != Name) {
  122. setState(F, CustomName);
  123. CustomNames[F] = std::string(Name);
  124. assert(CustomNames.find(F) != CustomNames.end());
  125. } else {
  126. setState(F, StandardName);
  127. }
  128. }
  129. /// Disables all builtins.
  130. ///
  131. /// This can be used for options like -fno-builtin.
  132. void disableAllFunctions();
  133. /// Add a set of scalar -> vector mappings, queryable via
  134. /// getVectorizedFunction and getScalarizedFunction.
  135. void addVectorizableFunctions(ArrayRef<VecDesc> Fns);
  136. /// Calls addVectorizableFunctions with a known preset of functions for the
  137. /// given vector library.
  138. void addVectorizableFunctionsFromVecLib(enum VectorLibrary VecLib);
  139. /// Return true if the function F has a vector equivalent with vectorization
  140. /// factor VF.
  141. bool isFunctionVectorizable(StringRef F, const ElementCount &VF) const {
  142. return !getVectorizedFunction(F, VF).empty();
  143. }
  144. /// Return true if the function F has a vector equivalent with any
  145. /// vectorization factor.
  146. bool isFunctionVectorizable(StringRef F) const;
  147. /// Return the name of the equivalent of F, vectorized with factor VF. If no
  148. /// such mapping exists, return the empty string.
  149. StringRef getVectorizedFunction(StringRef F, const ElementCount &VF) const;
  150. /// Set to true iff i32 parameters to library functions should have signext
  151. /// or zeroext attributes if they correspond to C-level int or unsigned int,
  152. /// respectively.
  153. void setShouldExtI32Param(bool Val) {
  154. ShouldExtI32Param = Val;
  155. }
  156. /// Set to true iff i32 results from library functions should have signext
  157. /// or zeroext attributes if they correspond to C-level int or unsigned int,
  158. /// respectively.
  159. void setShouldExtI32Return(bool Val) {
  160. ShouldExtI32Return = Val;
  161. }
  162. /// Set to true iff i32 parameters to library functions should have signext
  163. /// attribute if they correspond to C-level int or unsigned int.
  164. void setShouldSignExtI32Param(bool Val) {
  165. ShouldSignExtI32Param = Val;
  166. }
  167. /// Returns the size of the wchar_t type in bytes or 0 if the size is unknown.
  168. /// This queries the 'wchar_size' metadata.
  169. unsigned getWCharSize(const Module &M) const;
  170. /// Get size of a C-level int or unsigned int, in bits.
  171. unsigned getIntSize() const {
  172. return SizeOfInt;
  173. }
  174. /// Initialize the C-level size of an integer.
  175. void setIntSize(unsigned Bits) {
  176. SizeOfInt = Bits;
  177. }
  178. /// Returns the largest vectorization factor used in the list of
  179. /// vector functions.
  180. void getWidestVF(StringRef ScalarF, ElementCount &FixedVF,
  181. ElementCount &Scalable) const;
  182. /// Returns true if call site / callee has cdecl-compatible calling
  183. /// conventions.
  184. static bool isCallingConvCCompatible(CallBase *CI);
  185. static bool isCallingConvCCompatible(Function *Callee);
  186. };
  187. /// Provides information about what library functions are available for
  188. /// the current target.
  189. ///
  190. /// This both allows optimizations to handle them specially and frontends to
  191. /// disable such optimizations through -fno-builtin etc.
  192. class TargetLibraryInfo {
  193. friend class TargetLibraryAnalysis;
  194. friend class TargetLibraryInfoWrapperPass;
  195. /// The global (module level) TLI info.
  196. const TargetLibraryInfoImpl *Impl;
  197. /// Support for -fno-builtin* options as function attributes, overrides
  198. /// information in global TargetLibraryInfoImpl.
  199. BitVector OverrideAsUnavailable;
  200. public:
  201. explicit TargetLibraryInfo(const TargetLibraryInfoImpl &Impl,
  202. Optional<const Function *> F = None)
  203. : Impl(&Impl), OverrideAsUnavailable(NumLibFuncs) {
  204. if (!F)
  205. return;
  206. if ((*F)->hasFnAttribute("no-builtins"))
  207. disableAllFunctions();
  208. else {
  209. // Disable individual libc/libm calls in TargetLibraryInfo.
  210. LibFunc LF;
  211. AttributeSet FnAttrs = (*F)->getAttributes().getFnAttrs();
  212. for (const Attribute &Attr : FnAttrs) {
  213. if (!Attr.isStringAttribute())
  214. continue;
  215. auto AttrStr = Attr.getKindAsString();
  216. if (!AttrStr.consume_front("no-builtin-"))
  217. continue;
  218. if (getLibFunc(AttrStr, LF))
  219. setUnavailable(LF);
  220. }
  221. }
  222. }
  223. // Provide value semantics.
  224. TargetLibraryInfo(const TargetLibraryInfo &TLI) = default;
  225. TargetLibraryInfo(TargetLibraryInfo &&TLI)
  226. : Impl(TLI.Impl), OverrideAsUnavailable(TLI.OverrideAsUnavailable) {}
  227. TargetLibraryInfo &operator=(const TargetLibraryInfo &TLI) = default;
  228. TargetLibraryInfo &operator=(TargetLibraryInfo &&TLI) {
  229. Impl = TLI.Impl;
  230. OverrideAsUnavailable = TLI.OverrideAsUnavailable;
  231. return *this;
  232. }
  233. /// Determine whether a callee with the given TLI can be inlined into
  234. /// caller with this TLI, based on 'nobuiltin' attributes. When requested,
  235. /// allow inlining into a caller with a superset of the callee's nobuiltin
  236. /// attributes, which is conservatively correct.
  237. bool areInlineCompatible(const TargetLibraryInfo &CalleeTLI,
  238. bool AllowCallerSuperset) const {
  239. if (!AllowCallerSuperset)
  240. return OverrideAsUnavailable == CalleeTLI.OverrideAsUnavailable;
  241. BitVector B = OverrideAsUnavailable;
  242. B |= CalleeTLI.OverrideAsUnavailable;
  243. // We can inline if the union of the caller and callee's nobuiltin
  244. // attributes is no stricter than the caller's nobuiltin attributes.
  245. return B == OverrideAsUnavailable;
  246. }
  247. /// Searches for a particular function name.
  248. ///
  249. /// If it is one of the known library functions, return true and set F to the
  250. /// corresponding value.
  251. bool getLibFunc(StringRef funcName, LibFunc &F) const {
  252. return Impl->getLibFunc(funcName, F);
  253. }
  254. bool getLibFunc(const Function &FDecl, LibFunc &F) const {
  255. return Impl->getLibFunc(FDecl, F);
  256. }
  257. /// If a callbase does not have the 'nobuiltin' attribute, return if the
  258. /// called function is a known library function and set F to that function.
  259. bool getLibFunc(const CallBase &CB, LibFunc &F) const {
  260. return !CB.isNoBuiltin() && CB.getCalledFunction() &&
  261. getLibFunc(*(CB.getCalledFunction()), F);
  262. }
  263. /// Disables all builtins.
  264. ///
  265. /// This can be used for options like -fno-builtin.
  266. void disableAllFunctions() LLVM_ATTRIBUTE_UNUSED {
  267. OverrideAsUnavailable.set();
  268. }
  269. /// Forces a function to be marked as unavailable.
  270. void setUnavailable(LibFunc F) LLVM_ATTRIBUTE_UNUSED {
  271. OverrideAsUnavailable.set(F);
  272. }
  273. TargetLibraryInfoImpl::AvailabilityState getState(LibFunc F) const {
  274. if (OverrideAsUnavailable[F])
  275. return TargetLibraryInfoImpl::Unavailable;
  276. return Impl->getState(F);
  277. }
  278. /// Tests whether a library function is available.
  279. bool has(LibFunc F) const {
  280. return getState(F) != TargetLibraryInfoImpl::Unavailable;
  281. }
  282. bool isFunctionVectorizable(StringRef F, const ElementCount &VF) const {
  283. return Impl->isFunctionVectorizable(F, VF);
  284. }
  285. bool isFunctionVectorizable(StringRef F) const {
  286. return Impl->isFunctionVectorizable(F);
  287. }
  288. StringRef getVectorizedFunction(StringRef F, const ElementCount &VF) const {
  289. return Impl->getVectorizedFunction(F, VF);
  290. }
  291. /// Tests if the function is both available and a candidate for optimized code
  292. /// generation.
  293. bool hasOptimizedCodeGen(LibFunc F) const {
  294. if (getState(F) == TargetLibraryInfoImpl::Unavailable)
  295. return false;
  296. switch (F) {
  297. default: break;
  298. case LibFunc_copysign: case LibFunc_copysignf: case LibFunc_copysignl:
  299. case LibFunc_fabs: case LibFunc_fabsf: case LibFunc_fabsl:
  300. case LibFunc_sin: case LibFunc_sinf: case LibFunc_sinl:
  301. case LibFunc_cos: case LibFunc_cosf: case LibFunc_cosl:
  302. case LibFunc_sqrt: case LibFunc_sqrtf: case LibFunc_sqrtl:
  303. case LibFunc_sqrt_finite: case LibFunc_sqrtf_finite:
  304. case LibFunc_sqrtl_finite:
  305. case LibFunc_fmax: case LibFunc_fmaxf: case LibFunc_fmaxl:
  306. case LibFunc_fmin: case LibFunc_fminf: case LibFunc_fminl:
  307. case LibFunc_floor: case LibFunc_floorf: case LibFunc_floorl:
  308. case LibFunc_nearbyint: case LibFunc_nearbyintf: case LibFunc_nearbyintl:
  309. case LibFunc_ceil: case LibFunc_ceilf: case LibFunc_ceill:
  310. case LibFunc_rint: case LibFunc_rintf: case LibFunc_rintl:
  311. case LibFunc_round: case LibFunc_roundf: case LibFunc_roundl:
  312. case LibFunc_trunc: case LibFunc_truncf: case LibFunc_truncl:
  313. case LibFunc_log2: case LibFunc_log2f: case LibFunc_log2l:
  314. case LibFunc_exp2: case LibFunc_exp2f: case LibFunc_exp2l:
  315. case LibFunc_memcpy: case LibFunc_memset: case LibFunc_memmove:
  316. case LibFunc_memcmp: case LibFunc_bcmp: case LibFunc_strcmp:
  317. case LibFunc_strcpy: case LibFunc_stpcpy: case LibFunc_strlen:
  318. case LibFunc_strnlen: case LibFunc_memchr: case LibFunc_mempcpy:
  319. return true;
  320. }
  321. return false;
  322. }
  323. StringRef getName(LibFunc F) const {
  324. auto State = getState(F);
  325. if (State == TargetLibraryInfoImpl::Unavailable)
  326. return StringRef();
  327. if (State == TargetLibraryInfoImpl::StandardName)
  328. return Impl->StandardNames[F];
  329. assert(State == TargetLibraryInfoImpl::CustomName);
  330. return Impl->CustomNames.find(F)->second;
  331. }
  332. /// Returns extension attribute kind to be used for i32 parameters
  333. /// corresponding to C-level int or unsigned int. May be zeroext, signext,
  334. /// or none.
  335. Attribute::AttrKind getExtAttrForI32Param(bool Signed = true) const {
  336. if (Impl->ShouldExtI32Param)
  337. return Signed ? Attribute::SExt : Attribute::ZExt;
  338. if (Impl->ShouldSignExtI32Param)
  339. return Attribute::SExt;
  340. return Attribute::None;
  341. }
  342. /// Returns extension attribute kind to be used for i32 return values
  343. /// corresponding to C-level int or unsigned int. May be zeroext, signext,
  344. /// or none.
  345. Attribute::AttrKind getExtAttrForI32Return(bool Signed = true) const {
  346. if (Impl->ShouldExtI32Return)
  347. return Signed ? Attribute::SExt : Attribute::ZExt;
  348. return Attribute::None;
  349. }
  350. /// \copydoc TargetLibraryInfoImpl::getWCharSize()
  351. unsigned getWCharSize(const Module &M) const {
  352. return Impl->getWCharSize(M);
  353. }
  354. /// \copydoc TargetLibraryInfoImpl::getIntSize()
  355. unsigned getIntSize() const {
  356. return Impl->getIntSize();
  357. }
  358. /// Handle invalidation from the pass manager.
  359. ///
  360. /// If we try to invalidate this info, just return false. It cannot become
  361. /// invalid even if the module or function changes.
  362. bool invalidate(Module &, const PreservedAnalyses &,
  363. ModuleAnalysisManager::Invalidator &) {
  364. return false;
  365. }
  366. bool invalidate(Function &, const PreservedAnalyses &,
  367. FunctionAnalysisManager::Invalidator &) {
  368. return false;
  369. }
  370. /// Returns the largest vectorization factor used in the list of
  371. /// vector functions.
  372. void getWidestVF(StringRef ScalarF, ElementCount &FixedVF,
  373. ElementCount &ScalableVF) const {
  374. Impl->getWidestVF(ScalarF, FixedVF, ScalableVF);
  375. }
  376. /// Check if the function "F" is listed in a library known to LLVM.
  377. bool isKnownVectorFunctionInLibrary(StringRef F) const {
  378. return this->isFunctionVectorizable(F);
  379. }
  380. };
  381. /// Analysis pass providing the \c TargetLibraryInfo.
  382. ///
  383. /// Note that this pass's result cannot be invalidated, it is immutable for the
  384. /// life of the module.
  385. class TargetLibraryAnalysis : public AnalysisInfoMixin<TargetLibraryAnalysis> {
  386. public:
  387. typedef TargetLibraryInfo Result;
  388. /// Default construct the library analysis.
  389. ///
  390. /// This will use the module's triple to construct the library info for that
  391. /// module.
  392. TargetLibraryAnalysis() = default;
  393. /// Construct a library analysis with baseline Module-level info.
  394. ///
  395. /// This will be supplemented with Function-specific info in the Result.
  396. TargetLibraryAnalysis(TargetLibraryInfoImpl BaselineInfoImpl)
  397. : BaselineInfoImpl(std::move(BaselineInfoImpl)) {}
  398. TargetLibraryInfo run(const Function &F, FunctionAnalysisManager &);
  399. private:
  400. friend AnalysisInfoMixin<TargetLibraryAnalysis>;
  401. static AnalysisKey Key;
  402. Optional<TargetLibraryInfoImpl> BaselineInfoImpl;
  403. };
  404. class TargetLibraryInfoWrapperPass : public ImmutablePass {
  405. TargetLibraryAnalysis TLA;
  406. Optional<TargetLibraryInfo> TLI;
  407. virtual void anchor();
  408. public:
  409. static char ID;
  410. TargetLibraryInfoWrapperPass();
  411. explicit TargetLibraryInfoWrapperPass(const Triple &T);
  412. explicit TargetLibraryInfoWrapperPass(const TargetLibraryInfoImpl &TLI);
  413. TargetLibraryInfo &getTLI(const Function &F) {
  414. FunctionAnalysisManager DummyFAM;
  415. TLI = TLA.run(F, DummyFAM);
  416. return *TLI;
  417. }
  418. };
  419. } // end namespace llvm
  420. #endif
  421. #ifdef __GNUC__
  422. #pragma GCC diagnostic pop
  423. #endif