Attributes.h 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/Attributes.h - Container for Attributes -------------*- 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. //
  14. /// \file
  15. /// This file contains the simple types necessary to represent the
  16. /// attributes associated with functions and their calls.
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_IR_ATTRIBUTES_H
  20. #define LLVM_IR_ATTRIBUTES_H
  21. #include "llvm-c/Types.h"
  22. #include "llvm/ADT/ArrayRef.h"
  23. #include "llvm/ADT/BitmaskEnum.h"
  24. #include "llvm/ADT/SmallString.h"
  25. #include "llvm/ADT/StringRef.h"
  26. #include "llvm/Config/llvm-config.h"
  27. #include "llvm/Support/Alignment.h"
  28. #include "llvm/Support/CodeGen.h"
  29. #include "llvm/Support/PointerLikeTypeTraits.h"
  30. #include <bitset>
  31. #include <cassert>
  32. #include <cstdint>
  33. #include <optional>
  34. #include <set>
  35. #include <string>
  36. #include <utility>
  37. namespace llvm {
  38. class AttrBuilder;
  39. class AttributeMask;
  40. class AttributeImpl;
  41. class AttributeListImpl;
  42. class AttributeSetNode;
  43. class FoldingSetNodeID;
  44. class Function;
  45. class LLVMContext;
  46. class MemoryEffects;
  47. class Type;
  48. class raw_ostream;
  49. enum class AllocFnKind : uint64_t {
  50. Unknown = 0,
  51. Alloc = 1 << 0, // Allocator function returns a new allocation
  52. Realloc = 1 << 1, // Allocator function resizes the `allocptr` argument
  53. Free = 1 << 2, // Allocator function frees the `allocptr` argument
  54. Uninitialized = 1 << 3, // Allocator function returns uninitialized memory
  55. Zeroed = 1 << 4, // Allocator function returns zeroed memory
  56. Aligned = 1 << 5, // Allocator function aligns allocations per the
  57. // `allocalign` argument
  58. LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ Aligned)
  59. };
  60. //===----------------------------------------------------------------------===//
  61. /// \class
  62. /// Functions, function parameters, and return types can have attributes
  63. /// to indicate how they should be treated by optimizations and code
  64. /// generation. This class represents one of those attributes. It's light-weight
  65. /// and should be passed around by-value.
  66. class Attribute {
  67. public:
  68. /// This enumeration lists the attributes that can be associated with
  69. /// parameters, function results, or the function itself.
  70. ///
  71. /// Note: The `uwtable' attribute is about the ABI or the user mandating an
  72. /// entry in the unwind table. The `nounwind' attribute is about an exception
  73. /// passing by the function.
  74. ///
  75. /// In a theoretical system that uses tables for profiling and SjLj for
  76. /// exceptions, they would be fully independent. In a normal system that uses
  77. /// tables for both, the semantics are:
  78. ///
  79. /// nil = Needs an entry because an exception might pass by.
  80. /// nounwind = No need for an entry
  81. /// uwtable = Needs an entry because the ABI says so and because
  82. /// an exception might pass by.
  83. /// uwtable + nounwind = Needs an entry because the ABI says so.
  84. enum AttrKind {
  85. // IR-Level Attributes
  86. None, ///< No attributes have been set
  87. #define GET_ATTR_ENUM
  88. #include "llvm/IR/Attributes.inc"
  89. EndAttrKinds, ///< Sentinal value useful for loops
  90. EmptyKey, ///< Use as Empty key for DenseMap of AttrKind
  91. TombstoneKey, ///< Use as Tombstone key for DenseMap of AttrKind
  92. };
  93. static const unsigned NumIntAttrKinds = LastIntAttr - FirstIntAttr + 1;
  94. static const unsigned NumTypeAttrKinds = LastTypeAttr - FirstTypeAttr + 1;
  95. static bool isEnumAttrKind(AttrKind Kind) {
  96. return Kind >= FirstEnumAttr && Kind <= LastEnumAttr;
  97. }
  98. static bool isIntAttrKind(AttrKind Kind) {
  99. return Kind >= FirstIntAttr && Kind <= LastIntAttr;
  100. }
  101. static bool isTypeAttrKind(AttrKind Kind) {
  102. return Kind >= FirstTypeAttr && Kind <= LastTypeAttr;
  103. }
  104. static bool canUseAsFnAttr(AttrKind Kind);
  105. static bool canUseAsParamAttr(AttrKind Kind);
  106. static bool canUseAsRetAttr(AttrKind Kind);
  107. private:
  108. AttributeImpl *pImpl = nullptr;
  109. Attribute(AttributeImpl *A) : pImpl(A) {}
  110. public:
  111. Attribute() = default;
  112. //===--------------------------------------------------------------------===//
  113. // Attribute Construction
  114. //===--------------------------------------------------------------------===//
  115. /// Return a uniquified Attribute object.
  116. static Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val = 0);
  117. static Attribute get(LLVMContext &Context, StringRef Kind,
  118. StringRef Val = StringRef());
  119. static Attribute get(LLVMContext &Context, AttrKind Kind, Type *Ty);
  120. /// Return a uniquified Attribute object that has the specific
  121. /// alignment set.
  122. static Attribute getWithAlignment(LLVMContext &Context, Align Alignment);
  123. static Attribute getWithStackAlignment(LLVMContext &Context, Align Alignment);
  124. static Attribute getWithDereferenceableBytes(LLVMContext &Context,
  125. uint64_t Bytes);
  126. static Attribute getWithDereferenceableOrNullBytes(LLVMContext &Context,
  127. uint64_t Bytes);
  128. static Attribute getWithAllocSizeArgs(
  129. LLVMContext &Context, unsigned ElemSizeArg,
  130. const std::optional<unsigned> &NumElemsArg);
  131. static Attribute getWithVScaleRangeArgs(LLVMContext &Context,
  132. unsigned MinValue, unsigned MaxValue);
  133. static Attribute getWithByValType(LLVMContext &Context, Type *Ty);
  134. static Attribute getWithStructRetType(LLVMContext &Context, Type *Ty);
  135. static Attribute getWithByRefType(LLVMContext &Context, Type *Ty);
  136. static Attribute getWithPreallocatedType(LLVMContext &Context, Type *Ty);
  137. static Attribute getWithInAllocaType(LLVMContext &Context, Type *Ty);
  138. static Attribute getWithUWTableKind(LLVMContext &Context, UWTableKind Kind);
  139. static Attribute getWithMemoryEffects(LLVMContext &Context, MemoryEffects ME);
  140. /// For a typed attribute, return the equivalent attribute with the type
  141. /// changed to \p ReplacementTy.
  142. Attribute getWithNewType(LLVMContext &Context, Type *ReplacementTy) {
  143. assert(isTypeAttribute() && "this requires a typed attribute");
  144. return get(Context, getKindAsEnum(), ReplacementTy);
  145. }
  146. static Attribute::AttrKind getAttrKindFromName(StringRef AttrName);
  147. static StringRef getNameFromAttrKind(Attribute::AttrKind AttrKind);
  148. /// Return true if the provided string matches the IR name of an attribute.
  149. /// example: "noalias" return true but not "NoAlias"
  150. static bool isExistingAttribute(StringRef Name);
  151. //===--------------------------------------------------------------------===//
  152. // Attribute Accessors
  153. //===--------------------------------------------------------------------===//
  154. /// Return true if the attribute is an Attribute::AttrKind type.
  155. bool isEnumAttribute() const;
  156. /// Return true if the attribute is an integer attribute.
  157. bool isIntAttribute() const;
  158. /// Return true if the attribute is a string (target-dependent)
  159. /// attribute.
  160. bool isStringAttribute() const;
  161. /// Return true if the attribute is a type attribute.
  162. bool isTypeAttribute() const;
  163. /// Return true if the attribute is any kind of attribute.
  164. bool isValid() const { return pImpl; }
  165. /// Return true if the attribute is present.
  166. bool hasAttribute(AttrKind Val) const;
  167. /// Return true if the target-dependent attribute is present.
  168. bool hasAttribute(StringRef Val) const;
  169. /// Return the attribute's kind as an enum (Attribute::AttrKind). This
  170. /// requires the attribute to be an enum, integer, or type attribute.
  171. Attribute::AttrKind getKindAsEnum() const;
  172. /// Return the attribute's value as an integer. This requires that the
  173. /// attribute be an integer attribute.
  174. uint64_t getValueAsInt() const;
  175. /// Return the attribute's value as a boolean. This requires that the
  176. /// attribute be a string attribute.
  177. bool getValueAsBool() const;
  178. /// Return the attribute's kind as a string. This requires the
  179. /// attribute to be a string attribute.
  180. StringRef getKindAsString() const;
  181. /// Return the attribute's value as a string. This requires the
  182. /// attribute to be a string attribute.
  183. StringRef getValueAsString() const;
  184. /// Return the attribute's value as a Type. This requires the attribute to be
  185. /// a type attribute.
  186. Type *getValueAsType() const;
  187. /// Returns the alignment field of an attribute as a byte alignment
  188. /// value.
  189. MaybeAlign getAlignment() const;
  190. /// Returns the stack alignment field of an attribute as a byte
  191. /// alignment value.
  192. MaybeAlign getStackAlignment() const;
  193. /// Returns the number of dereferenceable bytes from the
  194. /// dereferenceable attribute.
  195. uint64_t getDereferenceableBytes() const;
  196. /// Returns the number of dereferenceable_or_null bytes from the
  197. /// dereferenceable_or_null attribute.
  198. uint64_t getDereferenceableOrNullBytes() const;
  199. /// Returns the argument numbers for the allocsize attribute.
  200. std::pair<unsigned, std::optional<unsigned>> getAllocSizeArgs() const;
  201. /// Returns the minimum value for the vscale_range attribute.
  202. unsigned getVScaleRangeMin() const;
  203. /// Returns the maximum value for the vscale_range attribute or std::nullopt
  204. /// when unknown.
  205. std::optional<unsigned> getVScaleRangeMax() const;
  206. // Returns the unwind table kind.
  207. UWTableKind getUWTableKind() const;
  208. // Returns the allocator function kind.
  209. AllocFnKind getAllocKind() const;
  210. /// Returns memory effects.
  211. MemoryEffects getMemoryEffects() const;
  212. /// The Attribute is converted to a string of equivalent mnemonic. This
  213. /// is, presumably, for writing out the mnemonics for the assembly writer.
  214. std::string getAsString(bool InAttrGrp = false) const;
  215. /// Return true if this attribute belongs to the LLVMContext.
  216. bool hasParentContext(LLVMContext &C) const;
  217. /// Equality and non-equality operators.
  218. bool operator==(Attribute A) const { return pImpl == A.pImpl; }
  219. bool operator!=(Attribute A) const { return pImpl != A.pImpl; }
  220. /// Less-than operator. Useful for sorting the attributes list.
  221. bool operator<(Attribute A) const;
  222. void Profile(FoldingSetNodeID &ID) const;
  223. /// Return a raw pointer that uniquely identifies this attribute.
  224. void *getRawPointer() const {
  225. return pImpl;
  226. }
  227. /// Get an attribute from a raw pointer created by getRawPointer.
  228. static Attribute fromRawPointer(void *RawPtr) {
  229. return Attribute(reinterpret_cast<AttributeImpl*>(RawPtr));
  230. }
  231. };
  232. // Specialized opaque value conversions.
  233. inline LLVMAttributeRef wrap(Attribute Attr) {
  234. return reinterpret_cast<LLVMAttributeRef>(Attr.getRawPointer());
  235. }
  236. // Specialized opaque value conversions.
  237. inline Attribute unwrap(LLVMAttributeRef Attr) {
  238. return Attribute::fromRawPointer(Attr);
  239. }
  240. //===----------------------------------------------------------------------===//
  241. /// \class
  242. /// This class holds the attributes for a particular argument, parameter,
  243. /// function, or return value. It is an immutable value type that is cheap to
  244. /// copy. Adding and removing enum attributes is intended to be fast, but adding
  245. /// and removing string or integer attributes involves a FoldingSet lookup.
  246. class AttributeSet {
  247. friend AttributeListImpl;
  248. template <typename Ty, typename Enable> friend struct DenseMapInfo;
  249. // TODO: Extract AvailableAttrs from AttributeSetNode and store them here.
  250. // This will allow an efficient implementation of addAttribute and
  251. // removeAttribute for enum attrs.
  252. /// Private implementation pointer.
  253. AttributeSetNode *SetNode = nullptr;
  254. private:
  255. explicit AttributeSet(AttributeSetNode *ASN) : SetNode(ASN) {}
  256. public:
  257. /// AttributeSet is a trivially copyable value type.
  258. AttributeSet() = default;
  259. AttributeSet(const AttributeSet &) = default;
  260. ~AttributeSet() = default;
  261. static AttributeSet get(LLVMContext &C, const AttrBuilder &B);
  262. static AttributeSet get(LLVMContext &C, ArrayRef<Attribute> Attrs);
  263. bool operator==(const AttributeSet &O) const { return SetNode == O.SetNode; }
  264. bool operator!=(const AttributeSet &O) const { return !(*this == O); }
  265. /// Add an argument attribute. Returns a new set because attribute sets are
  266. /// immutable.
  267. [[nodiscard]] AttributeSet addAttribute(LLVMContext &C,
  268. Attribute::AttrKind Kind) const;
  269. /// Add a target-dependent attribute. Returns a new set because attribute sets
  270. /// are immutable.
  271. [[nodiscard]] AttributeSet addAttribute(LLVMContext &C, StringRef Kind,
  272. StringRef Value = StringRef()) const;
  273. /// Add attributes to the attribute set. Returns a new set because attribute
  274. /// sets are immutable.
  275. [[nodiscard]] AttributeSet addAttributes(LLVMContext &C,
  276. AttributeSet AS) const;
  277. /// Remove the specified attribute from this set. Returns a new set because
  278. /// attribute sets are immutable.
  279. [[nodiscard]] AttributeSet removeAttribute(LLVMContext &C,
  280. Attribute::AttrKind Kind) const;
  281. /// Remove the specified attribute from this set. Returns a new set because
  282. /// attribute sets are immutable.
  283. [[nodiscard]] AttributeSet removeAttribute(LLVMContext &C,
  284. StringRef Kind) const;
  285. /// Remove the specified attributes from this set. Returns a new set because
  286. /// attribute sets are immutable.
  287. [[nodiscard]] AttributeSet
  288. removeAttributes(LLVMContext &C, const AttributeMask &AttrsToRemove) const;
  289. /// Return the number of attributes in this set.
  290. unsigned getNumAttributes() const;
  291. /// Return true if attributes exists in this set.
  292. bool hasAttributes() const { return SetNode != nullptr; }
  293. /// Return true if the attribute exists in this set.
  294. bool hasAttribute(Attribute::AttrKind Kind) const;
  295. /// Return true if the attribute exists in this set.
  296. bool hasAttribute(StringRef Kind) const;
  297. /// Return the attribute object.
  298. Attribute getAttribute(Attribute::AttrKind Kind) const;
  299. /// Return the target-dependent attribute object.
  300. Attribute getAttribute(StringRef Kind) const;
  301. MaybeAlign getAlignment() const;
  302. MaybeAlign getStackAlignment() const;
  303. uint64_t getDereferenceableBytes() const;
  304. uint64_t getDereferenceableOrNullBytes() const;
  305. Type *getByValType() const;
  306. Type *getStructRetType() const;
  307. Type *getByRefType() const;
  308. Type *getPreallocatedType() const;
  309. Type *getInAllocaType() const;
  310. Type *getElementType() const;
  311. std::optional<std::pair<unsigned, std::optional<unsigned>>> getAllocSizeArgs()
  312. const;
  313. unsigned getVScaleRangeMin() const;
  314. std::optional<unsigned> getVScaleRangeMax() const;
  315. UWTableKind getUWTableKind() const;
  316. AllocFnKind getAllocKind() const;
  317. MemoryEffects getMemoryEffects() const;
  318. std::string getAsString(bool InAttrGrp = false) const;
  319. /// Return true if this attribute set belongs to the LLVMContext.
  320. bool hasParentContext(LLVMContext &C) const;
  321. using iterator = const Attribute *;
  322. iterator begin() const;
  323. iterator end() const;
  324. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  325. void dump() const;
  326. #endif
  327. };
  328. //===----------------------------------------------------------------------===//
  329. /// \class
  330. /// Provide DenseMapInfo for AttributeSet.
  331. template <> struct DenseMapInfo<AttributeSet, void> {
  332. static AttributeSet getEmptyKey() {
  333. auto Val = static_cast<uintptr_t>(-1);
  334. Val <<= PointerLikeTypeTraits<void *>::NumLowBitsAvailable;
  335. return AttributeSet(reinterpret_cast<AttributeSetNode *>(Val));
  336. }
  337. static AttributeSet getTombstoneKey() {
  338. auto Val = static_cast<uintptr_t>(-2);
  339. Val <<= PointerLikeTypeTraits<void *>::NumLowBitsAvailable;
  340. return AttributeSet(reinterpret_cast<AttributeSetNode *>(Val));
  341. }
  342. static unsigned getHashValue(AttributeSet AS) {
  343. return (unsigned((uintptr_t)AS.SetNode) >> 4) ^
  344. (unsigned((uintptr_t)AS.SetNode) >> 9);
  345. }
  346. static bool isEqual(AttributeSet LHS, AttributeSet RHS) { return LHS == RHS; }
  347. };
  348. //===----------------------------------------------------------------------===//
  349. /// \class
  350. /// This class holds the attributes for a function, its return value, and
  351. /// its parameters. You access the attributes for each of them via an index into
  352. /// the AttributeList object. The function attributes are at index
  353. /// `AttributeList::FunctionIndex', the return value is at index
  354. /// `AttributeList::ReturnIndex', and the attributes for the parameters start at
  355. /// index `AttributeList::FirstArgIndex'.
  356. class AttributeList {
  357. public:
  358. enum AttrIndex : unsigned {
  359. ReturnIndex = 0U,
  360. FunctionIndex = ~0U,
  361. FirstArgIndex = 1,
  362. };
  363. private:
  364. friend class AttrBuilder;
  365. friend class AttributeListImpl;
  366. friend class AttributeSet;
  367. friend class AttributeSetNode;
  368. template <typename Ty, typename Enable> friend struct DenseMapInfo;
  369. /// The attributes that we are managing. This can be null to represent
  370. /// the empty attributes list.
  371. AttributeListImpl *pImpl = nullptr;
  372. public:
  373. /// Create an AttributeList with the specified parameters in it.
  374. static AttributeList get(LLVMContext &C,
  375. ArrayRef<std::pair<unsigned, Attribute>> Attrs);
  376. static AttributeList get(LLVMContext &C,
  377. ArrayRef<std::pair<unsigned, AttributeSet>> Attrs);
  378. /// Create an AttributeList from attribute sets for a function, its
  379. /// return value, and all of its arguments.
  380. static AttributeList get(LLVMContext &C, AttributeSet FnAttrs,
  381. AttributeSet RetAttrs,
  382. ArrayRef<AttributeSet> ArgAttrs);
  383. private:
  384. explicit AttributeList(AttributeListImpl *LI) : pImpl(LI) {}
  385. static AttributeList getImpl(LLVMContext &C, ArrayRef<AttributeSet> AttrSets);
  386. AttributeList setAttributesAtIndex(LLVMContext &C, unsigned Index,
  387. AttributeSet Attrs) const;
  388. public:
  389. AttributeList() = default;
  390. //===--------------------------------------------------------------------===//
  391. // AttributeList Construction and Mutation
  392. //===--------------------------------------------------------------------===//
  393. /// Return an AttributeList with the specified parameters in it.
  394. static AttributeList get(LLVMContext &C, ArrayRef<AttributeList> Attrs);
  395. static AttributeList get(LLVMContext &C, unsigned Index,
  396. ArrayRef<Attribute::AttrKind> Kinds);
  397. static AttributeList get(LLVMContext &C, unsigned Index,
  398. ArrayRef<Attribute::AttrKind> Kinds,
  399. ArrayRef<uint64_t> Values);
  400. static AttributeList get(LLVMContext &C, unsigned Index,
  401. ArrayRef<StringRef> Kind);
  402. static AttributeList get(LLVMContext &C, unsigned Index,
  403. AttributeSet Attrs);
  404. static AttributeList get(LLVMContext &C, unsigned Index,
  405. const AttrBuilder &B);
  406. // TODO: remove non-AtIndex versions of these methods.
  407. /// Add an attribute to the attribute set at the given index.
  408. /// Returns a new list because attribute lists are immutable.
  409. [[nodiscard]] AttributeList
  410. addAttributeAtIndex(LLVMContext &C, unsigned Index,
  411. Attribute::AttrKind Kind) const;
  412. /// Add an attribute to the attribute set at the given index.
  413. /// Returns a new list because attribute lists are immutable.
  414. [[nodiscard]] AttributeList
  415. addAttributeAtIndex(LLVMContext &C, unsigned Index, StringRef Kind,
  416. StringRef Value = StringRef()) const;
  417. /// Add an attribute to the attribute set at the given index.
  418. /// Returns a new list because attribute lists are immutable.
  419. [[nodiscard]] AttributeList
  420. addAttributeAtIndex(LLVMContext &C, unsigned Index, Attribute A) const;
  421. /// Add attributes to the attribute set at the given index.
  422. /// Returns a new list because attribute lists are immutable.
  423. [[nodiscard]] AttributeList addAttributesAtIndex(LLVMContext &C,
  424. unsigned Index,
  425. const AttrBuilder &B) const;
  426. /// Add a function attribute to the list. Returns a new list because
  427. /// attribute lists are immutable.
  428. [[nodiscard]] AttributeList addFnAttribute(LLVMContext &C,
  429. Attribute::AttrKind Kind) const {
  430. return addAttributeAtIndex(C, FunctionIndex, Kind);
  431. }
  432. /// Add a function attribute to the list. Returns a new list because
  433. /// attribute lists are immutable.
  434. [[nodiscard]] AttributeList addFnAttribute(LLVMContext &C,
  435. Attribute Attr) const {
  436. return addAttributeAtIndex(C, FunctionIndex, Attr);
  437. }
  438. /// Add a function attribute to the list. Returns a new list because
  439. /// attribute lists are immutable.
  440. [[nodiscard]] AttributeList
  441. addFnAttribute(LLVMContext &C, StringRef Kind,
  442. StringRef Value = StringRef()) const {
  443. return addAttributeAtIndex(C, FunctionIndex, Kind, Value);
  444. }
  445. /// Add function attribute to the list. Returns a new list because
  446. /// attribute lists are immutable.
  447. [[nodiscard]] AttributeList addFnAttributes(LLVMContext &C,
  448. const AttrBuilder &B) const {
  449. return addAttributesAtIndex(C, FunctionIndex, B);
  450. }
  451. /// Add a return value attribute to the list. Returns a new list because
  452. /// attribute lists are immutable.
  453. [[nodiscard]] AttributeList addRetAttribute(LLVMContext &C,
  454. Attribute::AttrKind Kind) const {
  455. return addAttributeAtIndex(C, ReturnIndex, Kind);
  456. }
  457. /// Add a return value attribute to the list. Returns a new list because
  458. /// attribute lists are immutable.
  459. [[nodiscard]] AttributeList addRetAttribute(LLVMContext &C,
  460. Attribute Attr) const {
  461. return addAttributeAtIndex(C, ReturnIndex, Attr);
  462. }
  463. /// Add a return value attribute to the list. Returns a new list because
  464. /// attribute lists are immutable.
  465. [[nodiscard]] AttributeList addRetAttributes(LLVMContext &C,
  466. const AttrBuilder &B) const {
  467. return addAttributesAtIndex(C, ReturnIndex, B);
  468. }
  469. /// Add an argument attribute to the list. Returns a new list because
  470. /// attribute lists are immutable.
  471. [[nodiscard]] AttributeList
  472. addParamAttribute(LLVMContext &C, unsigned ArgNo,
  473. Attribute::AttrKind Kind) const {
  474. return addAttributeAtIndex(C, ArgNo + FirstArgIndex, Kind);
  475. }
  476. /// Add an argument attribute to the list. Returns a new list because
  477. /// attribute lists are immutable.
  478. [[nodiscard]] AttributeList
  479. addParamAttribute(LLVMContext &C, unsigned ArgNo, StringRef Kind,
  480. StringRef Value = StringRef()) const {
  481. return addAttributeAtIndex(C, ArgNo + FirstArgIndex, Kind, Value);
  482. }
  483. /// Add an attribute to the attribute list at the given arg indices. Returns a
  484. /// new list because attribute lists are immutable.
  485. [[nodiscard]] AttributeList addParamAttribute(LLVMContext &C,
  486. ArrayRef<unsigned> ArgNos,
  487. Attribute A) const;
  488. /// Add an argument attribute to the list. Returns a new list because
  489. /// attribute lists are immutable.
  490. [[nodiscard]] AttributeList addParamAttributes(LLVMContext &C, unsigned ArgNo,
  491. const AttrBuilder &B) const {
  492. return addAttributesAtIndex(C, ArgNo + FirstArgIndex, B);
  493. }
  494. /// Remove the specified attribute at the specified index from this
  495. /// attribute list. Returns a new list because attribute lists are immutable.
  496. [[nodiscard]] AttributeList
  497. removeAttributeAtIndex(LLVMContext &C, unsigned Index,
  498. Attribute::AttrKind Kind) const;
  499. /// Remove the specified attribute at the specified index from this
  500. /// attribute list. Returns a new list because attribute lists are immutable.
  501. [[nodiscard]] AttributeList
  502. removeAttributeAtIndex(LLVMContext &C, unsigned Index, StringRef Kind) const;
  503. [[nodiscard]] AttributeList removeAttribute(LLVMContext &C, unsigned Index,
  504. StringRef Kind) const {
  505. return removeAttributeAtIndex(C, Index, Kind);
  506. }
  507. /// Remove the specified attributes at the specified index from this
  508. /// attribute list. Returns a new list because attribute lists are immutable.
  509. [[nodiscard]] AttributeList
  510. removeAttributesAtIndex(LLVMContext &C, unsigned Index,
  511. const AttributeMask &AttrsToRemove) const;
  512. /// Remove all attributes at the specified index from this
  513. /// attribute list. Returns a new list because attribute lists are immutable.
  514. [[nodiscard]] AttributeList removeAttributesAtIndex(LLVMContext &C,
  515. unsigned Index) const;
  516. /// Remove the specified attribute at the function index from this
  517. /// attribute list. Returns a new list because attribute lists are immutable.
  518. [[nodiscard]] AttributeList
  519. removeFnAttribute(LLVMContext &C, Attribute::AttrKind Kind) const {
  520. return removeAttributeAtIndex(C, FunctionIndex, Kind);
  521. }
  522. /// Remove the specified attribute at the function index from this
  523. /// attribute list. Returns a new list because attribute lists are immutable.
  524. [[nodiscard]] AttributeList removeFnAttribute(LLVMContext &C,
  525. StringRef Kind) const {
  526. return removeAttributeAtIndex(C, FunctionIndex, Kind);
  527. }
  528. /// Remove the specified attribute at the function index from this
  529. /// attribute list. Returns a new list because attribute lists are immutable.
  530. [[nodiscard]] AttributeList
  531. removeFnAttributes(LLVMContext &C, const AttributeMask &AttrsToRemove) const {
  532. return removeAttributesAtIndex(C, FunctionIndex, AttrsToRemove);
  533. }
  534. /// Remove the attributes at the function index from this
  535. /// attribute list. Returns a new list because attribute lists are immutable.
  536. [[nodiscard]] AttributeList removeFnAttributes(LLVMContext &C) const {
  537. return removeAttributesAtIndex(C, FunctionIndex);
  538. }
  539. /// Remove the specified attribute at the return value index from this
  540. /// attribute list. Returns a new list because attribute lists are immutable.
  541. [[nodiscard]] AttributeList
  542. removeRetAttribute(LLVMContext &C, Attribute::AttrKind Kind) const {
  543. return removeAttributeAtIndex(C, ReturnIndex, Kind);
  544. }
  545. /// Remove the specified attribute at the return value index from this
  546. /// attribute list. Returns a new list because attribute lists are immutable.
  547. [[nodiscard]] AttributeList removeRetAttribute(LLVMContext &C,
  548. StringRef Kind) const {
  549. return removeAttributeAtIndex(C, ReturnIndex, Kind);
  550. }
  551. /// Remove the specified attribute at the return value index from this
  552. /// attribute list. Returns a new list because attribute lists are immutable.
  553. [[nodiscard]] AttributeList
  554. removeRetAttributes(LLVMContext &C,
  555. const AttributeMask &AttrsToRemove) const {
  556. return removeAttributesAtIndex(C, ReturnIndex, AttrsToRemove);
  557. }
  558. /// Remove the specified attribute at the specified arg index from this
  559. /// attribute list. Returns a new list because attribute lists are immutable.
  560. [[nodiscard]] AttributeList
  561. removeParamAttribute(LLVMContext &C, unsigned ArgNo,
  562. Attribute::AttrKind Kind) const {
  563. return removeAttributeAtIndex(C, ArgNo + FirstArgIndex, Kind);
  564. }
  565. /// Remove the specified attribute at the specified arg index from this
  566. /// attribute list. Returns a new list because attribute lists are immutable.
  567. [[nodiscard]] AttributeList
  568. removeParamAttribute(LLVMContext &C, unsigned ArgNo, StringRef Kind) const {
  569. return removeAttributeAtIndex(C, ArgNo + FirstArgIndex, Kind);
  570. }
  571. /// Remove the specified attribute at the specified arg index from this
  572. /// attribute list. Returns a new list because attribute lists are immutable.
  573. [[nodiscard]] AttributeList
  574. removeParamAttributes(LLVMContext &C, unsigned ArgNo,
  575. const AttributeMask &AttrsToRemove) const {
  576. return removeAttributesAtIndex(C, ArgNo + FirstArgIndex, AttrsToRemove);
  577. }
  578. /// Remove all attributes at the specified arg index from this
  579. /// attribute list. Returns a new list because attribute lists are immutable.
  580. [[nodiscard]] AttributeList removeParamAttributes(LLVMContext &C,
  581. unsigned ArgNo) const {
  582. return removeAttributesAtIndex(C, ArgNo + FirstArgIndex);
  583. }
  584. /// Replace the type contained by attribute \p AttrKind at index \p ArgNo wih
  585. /// \p ReplacementTy, preserving all other attributes.
  586. [[nodiscard]] AttributeList
  587. replaceAttributeTypeAtIndex(LLVMContext &C, unsigned ArgNo,
  588. Attribute::AttrKind Kind,
  589. Type *ReplacementTy) const {
  590. Attribute Attr = getAttributeAtIndex(ArgNo, Kind);
  591. auto Attrs = removeAttributeAtIndex(C, ArgNo, Kind);
  592. return Attrs.addAttributeAtIndex(C, ArgNo,
  593. Attr.getWithNewType(C, ReplacementTy));
  594. }
  595. /// \brief Add the dereferenceable attribute to the attribute set at the given
  596. /// index. Returns a new list because attribute lists are immutable.
  597. [[nodiscard]] AttributeList addDereferenceableRetAttr(LLVMContext &C,
  598. uint64_t Bytes) const;
  599. /// \brief Add the dereferenceable attribute to the attribute set at the given
  600. /// arg index. Returns a new list because attribute lists are immutable.
  601. [[nodiscard]] AttributeList addDereferenceableParamAttr(LLVMContext &C,
  602. unsigned ArgNo,
  603. uint64_t Bytes) const;
  604. /// Add the dereferenceable_or_null attribute to the attribute set at
  605. /// the given arg index. Returns a new list because attribute lists are
  606. /// immutable.
  607. [[nodiscard]] AttributeList
  608. addDereferenceableOrNullParamAttr(LLVMContext &C, unsigned ArgNo,
  609. uint64_t Bytes) const;
  610. /// Add the allocsize attribute to the attribute set at the given arg index.
  611. /// Returns a new list because attribute lists are immutable.
  612. [[nodiscard]] AttributeList
  613. addAllocSizeParamAttr(LLVMContext &C, unsigned ArgNo, unsigned ElemSizeArg,
  614. const std::optional<unsigned> &NumElemsArg);
  615. //===--------------------------------------------------------------------===//
  616. // AttributeList Accessors
  617. //===--------------------------------------------------------------------===//
  618. /// The attributes for the specified index are returned.
  619. AttributeSet getAttributes(unsigned Index) const;
  620. /// The attributes for the argument or parameter at the given index are
  621. /// returned.
  622. AttributeSet getParamAttrs(unsigned ArgNo) const;
  623. /// The attributes for the ret value are returned.
  624. AttributeSet getRetAttrs() const;
  625. /// The function attributes are returned.
  626. AttributeSet getFnAttrs() const;
  627. /// Return true if the attribute exists at the given index.
  628. bool hasAttributeAtIndex(unsigned Index, Attribute::AttrKind Kind) const;
  629. /// Return true if the attribute exists at the given index.
  630. bool hasAttributeAtIndex(unsigned Index, StringRef Kind) const;
  631. /// Return true if attribute exists at the given index.
  632. bool hasAttributesAtIndex(unsigned Index) const;
  633. /// Return true if the attribute exists for the given argument
  634. bool hasParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const {
  635. return hasAttributeAtIndex(ArgNo + FirstArgIndex, Kind);
  636. }
  637. /// Return true if the attribute exists for the given argument
  638. bool hasParamAttr(unsigned ArgNo, StringRef Kind) const {
  639. return hasAttributeAtIndex(ArgNo + FirstArgIndex, Kind);
  640. }
  641. /// Return true if attributes exists for the given argument
  642. bool hasParamAttrs(unsigned ArgNo) const {
  643. return hasAttributesAtIndex(ArgNo + FirstArgIndex);
  644. }
  645. /// Return true if the attribute exists for the return value.
  646. bool hasRetAttr(Attribute::AttrKind Kind) const {
  647. return hasAttributeAtIndex(ReturnIndex, Kind);
  648. }
  649. /// Return true if the attribute exists for the return value.
  650. bool hasRetAttr(StringRef Kind) const {
  651. return hasAttributeAtIndex(ReturnIndex, Kind);
  652. }
  653. /// Return true if attributes exist for the return value.
  654. bool hasRetAttrs() const { return hasAttributesAtIndex(ReturnIndex); }
  655. /// Return true if the attribute exists for the function.
  656. bool hasFnAttr(Attribute::AttrKind Kind) const;
  657. /// Return true if the attribute exists for the function.
  658. bool hasFnAttr(StringRef Kind) const;
  659. /// Return true the attributes exist for the function.
  660. bool hasFnAttrs() const { return hasAttributesAtIndex(FunctionIndex); }
  661. /// Return true if the specified attribute is set for at least one
  662. /// parameter or for the return value. If Index is not nullptr, the index
  663. /// of a parameter with the specified attribute is provided.
  664. bool hasAttrSomewhere(Attribute::AttrKind Kind,
  665. unsigned *Index = nullptr) const;
  666. /// Return the attribute object that exists at the given index.
  667. Attribute getAttributeAtIndex(unsigned Index, Attribute::AttrKind Kind) const;
  668. /// Return the attribute object that exists at the given index.
  669. Attribute getAttributeAtIndex(unsigned Index, StringRef Kind) const;
  670. /// Return the attribute object that exists at the arg index.
  671. Attribute getParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const {
  672. return getAttributeAtIndex(ArgNo + FirstArgIndex, Kind);
  673. }
  674. /// Return the attribute object that exists at the given index.
  675. Attribute getParamAttr(unsigned ArgNo, StringRef Kind) const {
  676. return getAttributeAtIndex(ArgNo + FirstArgIndex, Kind);
  677. }
  678. /// Return the attribute object that exists for the function.
  679. Attribute getFnAttr(Attribute::AttrKind Kind) const {
  680. return getAttributeAtIndex(FunctionIndex, Kind);
  681. }
  682. /// Return the attribute object that exists for the function.
  683. Attribute getFnAttr(StringRef Kind) const {
  684. return getAttributeAtIndex(FunctionIndex, Kind);
  685. }
  686. /// Return the alignment of the return value.
  687. MaybeAlign getRetAlignment() const;
  688. /// Return the alignment for the specified function parameter.
  689. MaybeAlign getParamAlignment(unsigned ArgNo) const;
  690. /// Return the stack alignment for the specified function parameter.
  691. MaybeAlign getParamStackAlignment(unsigned ArgNo) const;
  692. /// Return the byval type for the specified function parameter.
  693. Type *getParamByValType(unsigned ArgNo) const;
  694. /// Return the sret type for the specified function parameter.
  695. Type *getParamStructRetType(unsigned ArgNo) const;
  696. /// Return the byref type for the specified function parameter.
  697. Type *getParamByRefType(unsigned ArgNo) const;
  698. /// Return the preallocated type for the specified function parameter.
  699. Type *getParamPreallocatedType(unsigned ArgNo) const;
  700. /// Return the inalloca type for the specified function parameter.
  701. Type *getParamInAllocaType(unsigned ArgNo) const;
  702. /// Return the elementtype type for the specified function parameter.
  703. Type *getParamElementType(unsigned ArgNo) const;
  704. /// Get the stack alignment of the function.
  705. MaybeAlign getFnStackAlignment() const;
  706. /// Get the stack alignment of the return value.
  707. MaybeAlign getRetStackAlignment() const;
  708. /// Get the number of dereferenceable bytes (or zero if unknown) of the return
  709. /// value.
  710. uint64_t getRetDereferenceableBytes() const;
  711. /// Get the number of dereferenceable bytes (or zero if unknown) of an arg.
  712. uint64_t getParamDereferenceableBytes(unsigned Index) const;
  713. /// Get the number of dereferenceable_or_null bytes (or zero if unknown) of
  714. /// the return value.
  715. uint64_t getRetDereferenceableOrNullBytes() const;
  716. /// Get the number of dereferenceable_or_null bytes (or zero if unknown) of an
  717. /// arg.
  718. uint64_t getParamDereferenceableOrNullBytes(unsigned ArgNo) const;
  719. /// Get the unwind table kind requested for the function.
  720. UWTableKind getUWTableKind() const;
  721. AllocFnKind getAllocKind() const;
  722. /// Returns memory effects of the function.
  723. MemoryEffects getMemoryEffects() const;
  724. /// Return the attributes at the index as a string.
  725. std::string getAsString(unsigned Index, bool InAttrGrp = false) const;
  726. /// Return true if this attribute list belongs to the LLVMContext.
  727. bool hasParentContext(LLVMContext &C) const;
  728. //===--------------------------------------------------------------------===//
  729. // AttributeList Introspection
  730. //===--------------------------------------------------------------------===//
  731. using iterator = const AttributeSet *;
  732. iterator begin() const;
  733. iterator end() const;
  734. unsigned getNumAttrSets() const;
  735. // Implementation of indexes(). Produces iterators that wrap an index. Mostly
  736. // to hide the awkwardness of unsigned wrapping when iterating over valid
  737. // indexes.
  738. struct index_iterator {
  739. unsigned NumAttrSets;
  740. index_iterator(int NumAttrSets) : NumAttrSets(NumAttrSets) {}
  741. struct int_wrapper {
  742. int_wrapper(unsigned i) : i(i) {}
  743. unsigned i;
  744. unsigned operator*() { return i; }
  745. bool operator!=(const int_wrapper &Other) { return i != Other.i; }
  746. int_wrapper &operator++() {
  747. // This is expected to undergo unsigned wrapping since FunctionIndex is
  748. // ~0 and that's where we start.
  749. ++i;
  750. return *this;
  751. }
  752. };
  753. int_wrapper begin() { return int_wrapper(AttributeList::FunctionIndex); }
  754. int_wrapper end() { return int_wrapper(NumAttrSets - 1); }
  755. };
  756. /// Use this to iterate over the valid attribute indexes.
  757. index_iterator indexes() const { return index_iterator(getNumAttrSets()); }
  758. /// operator==/!= - Provide equality predicates.
  759. bool operator==(const AttributeList &RHS) const { return pImpl == RHS.pImpl; }
  760. bool operator!=(const AttributeList &RHS) const { return pImpl != RHS.pImpl; }
  761. /// Return a raw pointer that uniquely identifies this attribute list.
  762. void *getRawPointer() const {
  763. return pImpl;
  764. }
  765. /// Return true if there are no attributes.
  766. bool isEmpty() const { return pImpl == nullptr; }
  767. void print(raw_ostream &O) const;
  768. void dump() const;
  769. };
  770. //===----------------------------------------------------------------------===//
  771. /// \class
  772. /// Provide DenseMapInfo for AttributeList.
  773. template <> struct DenseMapInfo<AttributeList, void> {
  774. static AttributeList getEmptyKey() {
  775. auto Val = static_cast<uintptr_t>(-1);
  776. Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
  777. return AttributeList(reinterpret_cast<AttributeListImpl *>(Val));
  778. }
  779. static AttributeList getTombstoneKey() {
  780. auto Val = static_cast<uintptr_t>(-2);
  781. Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
  782. return AttributeList(reinterpret_cast<AttributeListImpl *>(Val));
  783. }
  784. static unsigned getHashValue(AttributeList AS) {
  785. return (unsigned((uintptr_t)AS.pImpl) >> 4) ^
  786. (unsigned((uintptr_t)AS.pImpl) >> 9);
  787. }
  788. static bool isEqual(AttributeList LHS, AttributeList RHS) {
  789. return LHS == RHS;
  790. }
  791. };
  792. //===----------------------------------------------------------------------===//
  793. /// \class
  794. /// This class stores enough information to efficiently remove some attributes
  795. /// from an existing AttrBuilder, AttributeSet or AttributeList.
  796. class AttributeMask {
  797. std::bitset<Attribute::EndAttrKinds> Attrs;
  798. std::set<SmallString<32>, std::less<>> TargetDepAttrs;
  799. public:
  800. AttributeMask() = default;
  801. AttributeMask(const AttributeMask &) = delete;
  802. AttributeMask(AttributeMask &&) = default;
  803. AttributeMask(AttributeSet AS) {
  804. for (Attribute A : AS)
  805. addAttribute(A);
  806. }
  807. /// Add an attribute to the mask.
  808. AttributeMask &addAttribute(Attribute::AttrKind Val) {
  809. assert((unsigned)Val < Attribute::EndAttrKinds &&
  810. "Attribute out of range!");
  811. Attrs[Val] = true;
  812. return *this;
  813. }
  814. /// Add the Attribute object to the builder.
  815. AttributeMask &addAttribute(Attribute A) {
  816. if (A.isStringAttribute())
  817. addAttribute(A.getKindAsString());
  818. else
  819. addAttribute(A.getKindAsEnum());
  820. return *this;
  821. }
  822. /// Add the target-dependent attribute to the builder.
  823. AttributeMask &addAttribute(StringRef A) {
  824. TargetDepAttrs.insert(A);
  825. return *this;
  826. }
  827. /// Return true if the builder has the specified attribute.
  828. bool contains(Attribute::AttrKind A) const {
  829. assert((unsigned)A < Attribute::EndAttrKinds && "Attribute out of range!");
  830. return Attrs[A];
  831. }
  832. /// Return true if the builder has the specified target-dependent
  833. /// attribute.
  834. bool contains(StringRef A) const { return TargetDepAttrs.count(A); }
  835. /// Return true if the mask contains the specified attribute.
  836. bool contains(Attribute A) const {
  837. if (A.isStringAttribute())
  838. return contains(A.getKindAsString());
  839. return contains(A.getKindAsEnum());
  840. }
  841. };
  842. //===----------------------------------------------------------------------===//
  843. /// \class
  844. /// This class is used in conjunction with the Attribute::get method to
  845. /// create an Attribute object. The object itself is uniquified. The Builder's
  846. /// value, however, is not. So this can be used as a quick way to test for
  847. /// equality, presence of attributes, etc.
  848. class AttrBuilder {
  849. LLVMContext &Ctx;
  850. SmallVector<Attribute, 8> Attrs;
  851. public:
  852. AttrBuilder(LLVMContext &Ctx) : Ctx(Ctx) {}
  853. AttrBuilder(const AttrBuilder &) = delete;
  854. AttrBuilder(AttrBuilder &&) = default;
  855. AttrBuilder(LLVMContext &Ctx, const Attribute &A) : Ctx(Ctx) {
  856. addAttribute(A);
  857. }
  858. AttrBuilder(LLVMContext &Ctx, AttributeSet AS);
  859. void clear();
  860. /// Add an attribute to the builder.
  861. AttrBuilder &addAttribute(Attribute::AttrKind Val);
  862. /// Add the Attribute object to the builder.
  863. AttrBuilder &addAttribute(Attribute A);
  864. /// Add the target-dependent attribute to the builder.
  865. AttrBuilder &addAttribute(StringRef A, StringRef V = StringRef());
  866. /// Remove an attribute from the builder.
  867. AttrBuilder &removeAttribute(Attribute::AttrKind Val);
  868. /// Remove the target-dependent attribute from the builder.
  869. AttrBuilder &removeAttribute(StringRef A);
  870. /// Remove the target-dependent attribute from the builder.
  871. AttrBuilder &removeAttribute(Attribute A) {
  872. if (A.isStringAttribute())
  873. return removeAttribute(A.getKindAsString());
  874. else
  875. return removeAttribute(A.getKindAsEnum());
  876. }
  877. /// Add the attributes from the builder. Attributes in the passed builder
  878. /// overwrite attributes in this builder if they have the same key.
  879. AttrBuilder &merge(const AttrBuilder &B);
  880. /// Remove the attributes from the builder.
  881. AttrBuilder &remove(const AttributeMask &AM);
  882. /// Return true if the builder has any attribute that's in the
  883. /// specified builder.
  884. bool overlaps(const AttributeMask &AM) const;
  885. /// Return true if the builder has the specified attribute.
  886. bool contains(Attribute::AttrKind A) const;
  887. /// Return true if the builder has the specified target-dependent
  888. /// attribute.
  889. bool contains(StringRef A) const;
  890. /// Return true if the builder has IR-level attributes.
  891. bool hasAttributes() const { return !Attrs.empty(); }
  892. /// Return Attribute with the given Kind. The returned attribute will be
  893. /// invalid if the Kind is not present in the builder.
  894. Attribute getAttribute(Attribute::AttrKind Kind) const;
  895. /// Return Attribute with the given Kind. The returned attribute will be
  896. /// invalid if the Kind is not present in the builder.
  897. Attribute getAttribute(StringRef Kind) const;
  898. /// Return raw (possibly packed/encoded) value of integer attribute or
  899. /// std::nullopt if not set.
  900. std::optional<uint64_t> getRawIntAttr(Attribute::AttrKind Kind) const;
  901. /// Retrieve the alignment attribute, if it exists.
  902. MaybeAlign getAlignment() const {
  903. return MaybeAlign(getRawIntAttr(Attribute::Alignment).value_or(0));
  904. }
  905. /// Retrieve the stack alignment attribute, if it exists.
  906. MaybeAlign getStackAlignment() const {
  907. return MaybeAlign(getRawIntAttr(Attribute::StackAlignment).value_or(0));
  908. }
  909. /// Retrieve the number of dereferenceable bytes, if the
  910. /// dereferenceable attribute exists (zero is returned otherwise).
  911. uint64_t getDereferenceableBytes() const {
  912. return getRawIntAttr(Attribute::Dereferenceable).value_or(0);
  913. }
  914. /// Retrieve the number of dereferenceable_or_null bytes, if the
  915. /// dereferenceable_or_null attribute exists (zero is returned otherwise).
  916. uint64_t getDereferenceableOrNullBytes() const {
  917. return getRawIntAttr(Attribute::DereferenceableOrNull).value_or(0);
  918. }
  919. /// Retrieve type for the given type attribute.
  920. Type *getTypeAttr(Attribute::AttrKind Kind) const;
  921. /// Retrieve the byval type.
  922. Type *getByValType() const { return getTypeAttr(Attribute::ByVal); }
  923. /// Retrieve the sret type.
  924. Type *getStructRetType() const { return getTypeAttr(Attribute::StructRet); }
  925. /// Retrieve the byref type.
  926. Type *getByRefType() const { return getTypeAttr(Attribute::ByRef); }
  927. /// Retrieve the preallocated type.
  928. Type *getPreallocatedType() const {
  929. return getTypeAttr(Attribute::Preallocated);
  930. }
  931. /// Retrieve the inalloca type.
  932. Type *getInAllocaType() const { return getTypeAttr(Attribute::InAlloca); }
  933. /// Retrieve the allocsize args, or std::nullopt if the attribute does not
  934. /// exist.
  935. std::optional<std::pair<unsigned, std::optional<unsigned>>> getAllocSizeArgs()
  936. const;
  937. /// Add integer attribute with raw value (packed/encoded if necessary).
  938. AttrBuilder &addRawIntAttr(Attribute::AttrKind Kind, uint64_t Value);
  939. /// This turns an alignment into the form used internally in Attribute.
  940. /// This call has no effect if Align is not set.
  941. AttrBuilder &addAlignmentAttr(MaybeAlign Align);
  942. /// This turns an int alignment (which must be a power of 2) into the
  943. /// form used internally in Attribute.
  944. /// This call has no effect if Align is 0.
  945. /// Deprecated, use the version using a MaybeAlign.
  946. inline AttrBuilder &addAlignmentAttr(unsigned Align) {
  947. return addAlignmentAttr(MaybeAlign(Align));
  948. }
  949. /// This turns a stack alignment into the form used internally in Attribute.
  950. /// This call has no effect if Align is not set.
  951. AttrBuilder &addStackAlignmentAttr(MaybeAlign Align);
  952. /// This turns an int stack alignment (which must be a power of 2) into
  953. /// the form used internally in Attribute.
  954. /// This call has no effect if Align is 0.
  955. /// Deprecated, use the version using a MaybeAlign.
  956. inline AttrBuilder &addStackAlignmentAttr(unsigned Align) {
  957. return addStackAlignmentAttr(MaybeAlign(Align));
  958. }
  959. /// This turns the number of dereferenceable bytes into the form used
  960. /// internally in Attribute.
  961. AttrBuilder &addDereferenceableAttr(uint64_t Bytes);
  962. /// This turns the number of dereferenceable_or_null bytes into the
  963. /// form used internally in Attribute.
  964. AttrBuilder &addDereferenceableOrNullAttr(uint64_t Bytes);
  965. /// This turns one (or two) ints into the form used internally in Attribute.
  966. AttrBuilder &addAllocSizeAttr(unsigned ElemSizeArg,
  967. const std::optional<unsigned> &NumElemsArg);
  968. /// This turns two ints into the form used internally in Attribute.
  969. AttrBuilder &addVScaleRangeAttr(unsigned MinValue,
  970. std::optional<unsigned> MaxValue);
  971. /// Add a type attribute with the given type.
  972. AttrBuilder &addTypeAttr(Attribute::AttrKind Kind, Type *Ty);
  973. /// This turns a byval type into the form used internally in Attribute.
  974. AttrBuilder &addByValAttr(Type *Ty);
  975. /// This turns a sret type into the form used internally in Attribute.
  976. AttrBuilder &addStructRetAttr(Type *Ty);
  977. /// This turns a byref type into the form used internally in Attribute.
  978. AttrBuilder &addByRefAttr(Type *Ty);
  979. /// This turns a preallocated type into the form used internally in Attribute.
  980. AttrBuilder &addPreallocatedAttr(Type *Ty);
  981. /// This turns an inalloca type into the form used internally in Attribute.
  982. AttrBuilder &addInAllocaAttr(Type *Ty);
  983. /// Add an allocsize attribute, using the representation returned by
  984. /// Attribute.getIntValue().
  985. AttrBuilder &addAllocSizeAttrFromRawRepr(uint64_t RawAllocSizeRepr);
  986. /// Add a vscale_range attribute, using the representation returned by
  987. /// Attribute.getIntValue().
  988. AttrBuilder &addVScaleRangeAttrFromRawRepr(uint64_t RawVScaleRangeRepr);
  989. /// This turns the unwind table kind into the form used internally in
  990. /// Attribute.
  991. AttrBuilder &addUWTableAttr(UWTableKind Kind);
  992. // This turns the allocator kind into the form used internally in Attribute.
  993. AttrBuilder &addAllocKindAttr(AllocFnKind Kind);
  994. /// Add memory effect attribute.
  995. AttrBuilder &addMemoryAttr(MemoryEffects ME);
  996. ArrayRef<Attribute> attrs() const { return Attrs; }
  997. bool operator==(const AttrBuilder &B) const;
  998. bool operator!=(const AttrBuilder &B) const { return !(*this == B); }
  999. };
  1000. namespace AttributeFuncs {
  1001. enum AttributeSafetyKind : uint8_t {
  1002. ASK_SAFE_TO_DROP = 1,
  1003. ASK_UNSAFE_TO_DROP = 2,
  1004. ASK_ALL = ASK_SAFE_TO_DROP | ASK_UNSAFE_TO_DROP,
  1005. };
  1006. /// Which attributes cannot be applied to a type. The argument \p ASK indicates,
  1007. /// if only attributes that are known to be safely droppable are contained in
  1008. /// the mask; only attributes that might be unsafe to drop (e.g., ABI-related
  1009. /// attributes) are in the mask; or both.
  1010. AttributeMask typeIncompatible(Type *Ty, AttributeSafetyKind ASK = ASK_ALL);
  1011. /// Get param/return attributes which imply immediate undefined behavior if an
  1012. /// invalid value is passed. For example, this includes noundef (where undef
  1013. /// implies UB), but not nonnull (where null implies poison). It also does not
  1014. /// include attributes like nocapture, which constrain the function
  1015. /// implementation rather than the passed value.
  1016. AttributeMask getUBImplyingAttributes();
  1017. /// \returns Return true if the two functions have compatible target-independent
  1018. /// attributes for inlining purposes.
  1019. bool areInlineCompatible(const Function &Caller, const Function &Callee);
  1020. /// Checks if there are any incompatible function attributes between
  1021. /// \p A and \p B.
  1022. ///
  1023. /// \param [in] A - The first function to be compared with.
  1024. /// \param [in] B - The second function to be compared with.
  1025. /// \returns true if the functions have compatible attributes.
  1026. bool areOutlineCompatible(const Function &A, const Function &B);
  1027. /// Merge caller's and callee's attributes.
  1028. void mergeAttributesForInlining(Function &Caller, const Function &Callee);
  1029. /// Merges the functions attributes from \p ToMerge into function \p Base.
  1030. ///
  1031. /// \param [in,out] Base - The function being merged into.
  1032. /// \param [in] ToMerge - The function to merge attributes from.
  1033. void mergeAttributesForOutlining(Function &Base, const Function &ToMerge);
  1034. /// Update min-legal-vector-width if it is in Attribute and less than Width.
  1035. void updateMinLegalVectorWidthAttr(Function &Fn, uint64_t Width);
  1036. } // end namespace AttributeFuncs
  1037. } // end namespace llvm
  1038. #endif // LLVM_IR_ATTRIBUTES_H
  1039. #ifdef __GNUC__
  1040. #pragma GCC diagnostic pop
  1041. #endif