Attributes.h 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  1. //===- llvm/Attributes.h - Container for Attributes -------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. /// \file
  10. /// This file contains the simple types necessary to represent the
  11. /// attributes associated with functions and their calls.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_IR_ATTRIBUTES_H
  15. #define LLVM_IR_ATTRIBUTES_H
  16. #include "llvm-c/Types.h"
  17. #include "llvm/ADT/ArrayRef.h"
  18. #include "llvm/ADT/Optional.h"
  19. #include "llvm/ADT/SmallString.h"
  20. #include "llvm/ADT/StringRef.h"
  21. #include "llvm/ADT/iterator_range.h"
  22. #include "llvm/Config/llvm-config.h"
  23. #include "llvm/Support/Alignment.h"
  24. #include "llvm/Support/PointerLikeTypeTraits.h"
  25. #include <bitset>
  26. #include <cassert>
  27. #include <cstdint>
  28. #include <map>
  29. #include <string>
  30. #include <utility>
  31. namespace llvm {
  32. class AttrBuilder;
  33. class AttributeImpl;
  34. class AttributeListImpl;
  35. class AttributeSetNode;
  36. template<typename T> struct DenseMapInfo;
  37. class FoldingSetNodeID;
  38. class Function;
  39. class LLVMContext;
  40. class Type;
  41. //===----------------------------------------------------------------------===//
  42. /// \class
  43. /// Functions, function parameters, and return types can have attributes
  44. /// to indicate how they should be treated by optimizations and code
  45. /// generation. This class represents one of those attributes. It's light-weight
  46. /// and should be passed around by-value.
  47. class Attribute {
  48. public:
  49. /// This enumeration lists the attributes that can be associated with
  50. /// parameters, function results, or the function itself.
  51. ///
  52. /// Note: The `uwtable' attribute is about the ABI or the user mandating an
  53. /// entry in the unwind table. The `nounwind' attribute is about an exception
  54. /// passing by the function.
  55. ///
  56. /// In a theoretical system that uses tables for profiling and SjLj for
  57. /// exceptions, they would be fully independent. In a normal system that uses
  58. /// tables for both, the semantics are:
  59. ///
  60. /// nil = Needs an entry because an exception might pass by.
  61. /// nounwind = No need for an entry
  62. /// uwtable = Needs an entry because the ABI says so and because
  63. /// an exception might pass by.
  64. /// uwtable + nounwind = Needs an entry because the ABI says so.
  65. enum AttrKind {
  66. // IR-Level Attributes
  67. None, ///< No attributes have been set
  68. #define GET_ATTR_NAMES
  69. #define ATTRIBUTE_ENUM(ENUM_NAME, OTHER) ENUM_NAME,
  70. #include "llvm/IR/Attributes.inc"
  71. EndAttrKinds, ///< Sentinal value useful for loops
  72. EmptyKey, ///< Use as Empty key for DenseMap of AttrKind
  73. TombstoneKey, ///< Use as Tombstone key for DenseMap of AttrKind
  74. };
  75. private:
  76. AttributeImpl *pImpl = nullptr;
  77. Attribute(AttributeImpl *A) : pImpl(A) {}
  78. public:
  79. Attribute() = default;
  80. //===--------------------------------------------------------------------===//
  81. // Attribute Construction
  82. //===--------------------------------------------------------------------===//
  83. /// Return a uniquified Attribute object.
  84. static Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val = 0);
  85. static Attribute get(LLVMContext &Context, StringRef Kind,
  86. StringRef Val = StringRef());
  87. static Attribute get(LLVMContext &Context, AttrKind Kind, Type *Ty);
  88. /// Return a uniquified Attribute object that has the specific
  89. /// alignment set.
  90. static Attribute getWithAlignment(LLVMContext &Context, Align Alignment);
  91. static Attribute getWithStackAlignment(LLVMContext &Context, Align Alignment);
  92. static Attribute getWithDereferenceableBytes(LLVMContext &Context,
  93. uint64_t Bytes);
  94. static Attribute getWithDereferenceableOrNullBytes(LLVMContext &Context,
  95. uint64_t Bytes);
  96. static Attribute getWithAllocSizeArgs(LLVMContext &Context,
  97. unsigned ElemSizeArg,
  98. const Optional<unsigned> &NumElemsArg);
  99. static Attribute getWithVScaleRangeArgs(LLVMContext &Context,
  100. unsigned MinValue, unsigned MaxValue);
  101. static Attribute getWithByValType(LLVMContext &Context, Type *Ty);
  102. static Attribute getWithStructRetType(LLVMContext &Context, Type *Ty);
  103. static Attribute getWithByRefType(LLVMContext &Context, Type *Ty);
  104. static Attribute getWithPreallocatedType(LLVMContext &Context, Type *Ty);
  105. static Attribute getWithInAllocaType(LLVMContext &Context, Type *Ty);
  106. /// For a typed attribute, return the equivalent attribute with the type
  107. /// changed to \p ReplacementTy.
  108. Attribute getWithNewType(LLVMContext &Context, Type *ReplacementTy) {
  109. assert(isTypeAttribute() && "this requires a typed attribute");
  110. return get(Context, getKindAsEnum(), ReplacementTy);
  111. }
  112. static Attribute::AttrKind getAttrKindFromName(StringRef AttrName);
  113. static StringRef getNameFromAttrKind(Attribute::AttrKind AttrKind);
  114. /// Return true if and only if the attribute has an Argument.
  115. static bool doesAttrKindHaveArgument(Attribute::AttrKind AttrKind);
  116. /// Return true if the provided string matches the IR name of an attribute.
  117. /// example: "noalias" return true but not "NoAlias"
  118. static bool isExistingAttribute(StringRef Name);
  119. //===--------------------------------------------------------------------===//
  120. // Attribute Accessors
  121. //===--------------------------------------------------------------------===//
  122. /// Return true if the attribute is an Attribute::AttrKind type.
  123. bool isEnumAttribute() const;
  124. /// Return true if the attribute is an integer attribute.
  125. bool isIntAttribute() const;
  126. /// Return true if the attribute is a string (target-dependent)
  127. /// attribute.
  128. bool isStringAttribute() const;
  129. /// Return true if the attribute is a type attribute.
  130. bool isTypeAttribute() const;
  131. /// Return true if the attribute is any kind of attribute.
  132. bool isValid() const { return pImpl; }
  133. /// Return true if the attribute is present.
  134. bool hasAttribute(AttrKind Val) const;
  135. /// Return true if the target-dependent attribute is present.
  136. bool hasAttribute(StringRef Val) const;
  137. /// Return the attribute's kind as an enum (Attribute::AttrKind). This
  138. /// requires the attribute to be an enum, integer, or type attribute.
  139. Attribute::AttrKind getKindAsEnum() const;
  140. /// Return the attribute's value as an integer. This requires that the
  141. /// attribute be an integer attribute.
  142. uint64_t getValueAsInt() const;
  143. /// Return the attribute's value as a boolean. This requires that the
  144. /// attribute be a string attribute.
  145. bool getValueAsBool() const;
  146. /// Return the attribute's kind as a string. This requires the
  147. /// attribute to be a string attribute.
  148. StringRef getKindAsString() const;
  149. /// Return the attribute's value as a string. This requires the
  150. /// attribute to be a string attribute.
  151. StringRef getValueAsString() const;
  152. /// Return the attribute's value as a Type. This requires the attribute to be
  153. /// a type attribute.
  154. Type *getValueAsType() const;
  155. /// Returns the alignment field of an attribute as a byte alignment
  156. /// value.
  157. MaybeAlign getAlignment() const;
  158. /// Returns the stack alignment field of an attribute as a byte
  159. /// alignment value.
  160. MaybeAlign getStackAlignment() const;
  161. /// Returns the number of dereferenceable bytes from the
  162. /// dereferenceable attribute.
  163. uint64_t getDereferenceableBytes() const;
  164. /// Returns the number of dereferenceable_or_null bytes from the
  165. /// dereferenceable_or_null attribute.
  166. uint64_t getDereferenceableOrNullBytes() const;
  167. /// Returns the argument numbers for the allocsize attribute (or pair(0, 0)
  168. /// if not known).
  169. std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
  170. /// Returns the argument numbers for the vscale_range attribute (or pair(0, 0)
  171. /// if not known).
  172. std::pair<unsigned, unsigned> getVScaleRangeArgs() const;
  173. /// The Attribute is converted to a string of equivalent mnemonic. This
  174. /// is, presumably, for writing out the mnemonics for the assembly writer.
  175. std::string getAsString(bool InAttrGrp = false) const;
  176. /// Return true if this attribute belongs to the LLVMContext.
  177. bool hasParentContext(LLVMContext &C) const;
  178. /// Equality and non-equality operators.
  179. bool operator==(Attribute A) const { return pImpl == A.pImpl; }
  180. bool operator!=(Attribute A) const { return pImpl != A.pImpl; }
  181. /// Less-than operator. Useful for sorting the attributes list.
  182. bool operator<(Attribute A) const;
  183. void Profile(FoldingSetNodeID &ID) const;
  184. /// Return a raw pointer that uniquely identifies this attribute.
  185. void *getRawPointer() const {
  186. return pImpl;
  187. }
  188. /// Get an attribute from a raw pointer created by getRawPointer.
  189. static Attribute fromRawPointer(void *RawPtr) {
  190. return Attribute(reinterpret_cast<AttributeImpl*>(RawPtr));
  191. }
  192. };
  193. // Specialized opaque value conversions.
  194. inline LLVMAttributeRef wrap(Attribute Attr) {
  195. return reinterpret_cast<LLVMAttributeRef>(Attr.getRawPointer());
  196. }
  197. // Specialized opaque value conversions.
  198. inline Attribute unwrap(LLVMAttributeRef Attr) {
  199. return Attribute::fromRawPointer(Attr);
  200. }
  201. //===----------------------------------------------------------------------===//
  202. /// \class
  203. /// This class holds the attributes for a particular argument, parameter,
  204. /// function, or return value. It is an immutable value type that is cheap to
  205. /// copy. Adding and removing enum attributes is intended to be fast, but adding
  206. /// and removing string or integer attributes involves a FoldingSet lookup.
  207. class AttributeSet {
  208. friend AttributeListImpl;
  209. template <typename Ty> friend struct DenseMapInfo;
  210. // TODO: Extract AvailableAttrs from AttributeSetNode and store them here.
  211. // This will allow an efficient implementation of addAttribute and
  212. // removeAttribute for enum attrs.
  213. /// Private implementation pointer.
  214. AttributeSetNode *SetNode = nullptr;
  215. private:
  216. explicit AttributeSet(AttributeSetNode *ASN) : SetNode(ASN) {}
  217. public:
  218. /// AttributeSet is a trivially copyable value type.
  219. AttributeSet() = default;
  220. AttributeSet(const AttributeSet &) = default;
  221. ~AttributeSet() = default;
  222. static AttributeSet get(LLVMContext &C, const AttrBuilder &B);
  223. static AttributeSet get(LLVMContext &C, ArrayRef<Attribute> Attrs);
  224. bool operator==(const AttributeSet &O) const { return SetNode == O.SetNode; }
  225. bool operator!=(const AttributeSet &O) const { return !(*this == O); }
  226. /// Add an argument attribute. Returns a new set because attribute sets are
  227. /// immutable.
  228. LLVM_NODISCARD AttributeSet addAttribute(LLVMContext &C,
  229. Attribute::AttrKind Kind) const;
  230. /// Add a target-dependent attribute. Returns a new set because attribute sets
  231. /// are immutable.
  232. LLVM_NODISCARD AttributeSet addAttribute(LLVMContext &C, StringRef Kind,
  233. StringRef Value = StringRef()) const;
  234. /// Add attributes to the attribute set. Returns a new set because attribute
  235. /// sets are immutable.
  236. LLVM_NODISCARD AttributeSet addAttributes(LLVMContext &C,
  237. AttributeSet AS) const;
  238. /// Remove the specified attribute from this set. Returns a new set because
  239. /// attribute sets are immutable.
  240. LLVM_NODISCARD AttributeSet removeAttribute(LLVMContext &C,
  241. Attribute::AttrKind Kind) const;
  242. /// Remove the specified attribute from this set. Returns a new set because
  243. /// attribute sets are immutable.
  244. LLVM_NODISCARD AttributeSet removeAttribute(LLVMContext &C,
  245. StringRef Kind) const;
  246. /// Remove the specified attributes from this set. Returns a new set because
  247. /// attribute sets are immutable.
  248. LLVM_NODISCARD AttributeSet
  249. removeAttributes(LLVMContext &C, const AttrBuilder &AttrsToRemove) const;
  250. /// Return the number of attributes in this set.
  251. unsigned getNumAttributes() const;
  252. /// Return true if attributes exists in this set.
  253. bool hasAttributes() const { return SetNode != nullptr; }
  254. /// Return true if the attribute exists in this set.
  255. bool hasAttribute(Attribute::AttrKind Kind) const;
  256. /// Return true if the attribute exists in this set.
  257. bool hasAttribute(StringRef Kind) const;
  258. /// Return the attribute object.
  259. Attribute getAttribute(Attribute::AttrKind Kind) const;
  260. /// Return the target-dependent attribute object.
  261. Attribute getAttribute(StringRef Kind) const;
  262. MaybeAlign getAlignment() const;
  263. MaybeAlign getStackAlignment() const;
  264. uint64_t getDereferenceableBytes() const;
  265. uint64_t getDereferenceableOrNullBytes() const;
  266. Type *getByValType() const;
  267. Type *getStructRetType() const;
  268. Type *getByRefType() const;
  269. Type *getPreallocatedType() const;
  270. Type *getInAllocaType() const;
  271. std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
  272. std::pair<unsigned, unsigned> getVScaleRangeArgs() const;
  273. std::string getAsString(bool InAttrGrp = false) const;
  274. /// Return true if this attribute set belongs to the LLVMContext.
  275. bool hasParentContext(LLVMContext &C) const;
  276. using iterator = const Attribute *;
  277. iterator begin() const;
  278. iterator end() const;
  279. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  280. void dump() const;
  281. #endif
  282. };
  283. //===----------------------------------------------------------------------===//
  284. /// \class
  285. /// Provide DenseMapInfo for AttributeSet.
  286. template <> struct DenseMapInfo<AttributeSet> {
  287. static AttributeSet getEmptyKey() {
  288. auto Val = static_cast<uintptr_t>(-1);
  289. Val <<= PointerLikeTypeTraits<void *>::NumLowBitsAvailable;
  290. return AttributeSet(reinterpret_cast<AttributeSetNode *>(Val));
  291. }
  292. static AttributeSet getTombstoneKey() {
  293. auto Val = static_cast<uintptr_t>(-2);
  294. Val <<= PointerLikeTypeTraits<void *>::NumLowBitsAvailable;
  295. return AttributeSet(reinterpret_cast<AttributeSetNode *>(Val));
  296. }
  297. static unsigned getHashValue(AttributeSet AS) {
  298. return (unsigned((uintptr_t)AS.SetNode) >> 4) ^
  299. (unsigned((uintptr_t)AS.SetNode) >> 9);
  300. }
  301. static bool isEqual(AttributeSet LHS, AttributeSet RHS) { return LHS == RHS; }
  302. };
  303. //===----------------------------------------------------------------------===//
  304. /// \class
  305. /// This class holds the attributes for a function, its return value, and
  306. /// its parameters. You access the attributes for each of them via an index into
  307. /// the AttributeList object. The function attributes are at index
  308. /// `AttributeList::FunctionIndex', the return value is at index
  309. /// `AttributeList::ReturnIndex', and the attributes for the parameters start at
  310. /// index `AttributeList::FirstArgIndex'.
  311. class AttributeList {
  312. public:
  313. enum AttrIndex : unsigned {
  314. ReturnIndex = 0U,
  315. FunctionIndex = ~0U,
  316. FirstArgIndex = 1,
  317. };
  318. private:
  319. friend class AttrBuilder;
  320. friend class AttributeListImpl;
  321. friend class AttributeSet;
  322. friend class AttributeSetNode;
  323. template <typename Ty> friend struct DenseMapInfo;
  324. /// The attributes that we are managing. This can be null to represent
  325. /// the empty attributes list.
  326. AttributeListImpl *pImpl = nullptr;
  327. public:
  328. /// Create an AttributeList with the specified parameters in it.
  329. static AttributeList get(LLVMContext &C,
  330. ArrayRef<std::pair<unsigned, Attribute>> Attrs);
  331. static AttributeList get(LLVMContext &C,
  332. ArrayRef<std::pair<unsigned, AttributeSet>> Attrs);
  333. /// Create an AttributeList from attribute sets for a function, its
  334. /// return value, and all of its arguments.
  335. static AttributeList get(LLVMContext &C, AttributeSet FnAttrs,
  336. AttributeSet RetAttrs,
  337. ArrayRef<AttributeSet> ArgAttrs);
  338. private:
  339. explicit AttributeList(AttributeListImpl *LI) : pImpl(LI) {}
  340. static AttributeList getImpl(LLVMContext &C, ArrayRef<AttributeSet> AttrSets);
  341. AttributeList setAttributes(LLVMContext &C, unsigned Index,
  342. AttributeSet Attrs) const;
  343. public:
  344. AttributeList() = default;
  345. //===--------------------------------------------------------------------===//
  346. // AttributeList Construction and Mutation
  347. //===--------------------------------------------------------------------===//
  348. /// Return an AttributeList with the specified parameters in it.
  349. static AttributeList get(LLVMContext &C, ArrayRef<AttributeList> Attrs);
  350. static AttributeList get(LLVMContext &C, unsigned Index,
  351. ArrayRef<Attribute::AttrKind> Kinds);
  352. static AttributeList get(LLVMContext &C, unsigned Index,
  353. ArrayRef<Attribute::AttrKind> Kinds,
  354. ArrayRef<uint64_t> Values);
  355. static AttributeList get(LLVMContext &C, unsigned Index,
  356. ArrayRef<StringRef> Kind);
  357. static AttributeList get(LLVMContext &C, unsigned Index,
  358. const AttrBuilder &B);
  359. /// Add an attribute to the attribute set at the given index.
  360. /// Returns a new list because attribute lists are immutable.
  361. LLVM_NODISCARD AttributeList addAttribute(LLVMContext &C, unsigned Index,
  362. Attribute::AttrKind Kind) const;
  363. /// Add an attribute to the attribute set at the given index.
  364. /// Returns a new list because attribute lists are immutable.
  365. LLVM_NODISCARD AttributeList
  366. addAttribute(LLVMContext &C, unsigned Index, StringRef Kind,
  367. StringRef Value = StringRef()) const;
  368. /// Add an attribute to the attribute set at the given index.
  369. /// Returns a new list because attribute lists are immutable.
  370. LLVM_NODISCARD AttributeList addAttribute(LLVMContext &C, unsigned Index,
  371. Attribute A) const;
  372. /// Add attributes to the attribute set at the given index.
  373. /// Returns a new list because attribute lists are immutable.
  374. LLVM_NODISCARD AttributeList addAttributes(LLVMContext &C, unsigned Index,
  375. const AttrBuilder &B) const;
  376. /// Add an argument attribute to the list. Returns a new list because
  377. /// attribute lists are immutable.
  378. LLVM_NODISCARD AttributeList addParamAttribute(
  379. LLVMContext &C, unsigned ArgNo, Attribute::AttrKind Kind) const {
  380. return addAttribute(C, ArgNo + FirstArgIndex, Kind);
  381. }
  382. /// Add an argument attribute to the list. Returns a new list because
  383. /// attribute lists are immutable.
  384. LLVM_NODISCARD AttributeList
  385. addParamAttribute(LLVMContext &C, unsigned ArgNo, StringRef Kind,
  386. StringRef Value = StringRef()) const {
  387. return addAttribute(C, ArgNo + FirstArgIndex, Kind, Value);
  388. }
  389. /// Add an attribute to the attribute list at the given arg indices. Returns a
  390. /// new list because attribute lists are immutable.
  391. LLVM_NODISCARD AttributeList addParamAttribute(LLVMContext &C,
  392. ArrayRef<unsigned> ArgNos,
  393. Attribute A) const;
  394. /// Add an argument attribute to the list. Returns a new list because
  395. /// attribute lists are immutable.
  396. LLVM_NODISCARD AttributeList addParamAttributes(LLVMContext &C,
  397. unsigned ArgNo,
  398. const AttrBuilder &B) const {
  399. return addAttributes(C, ArgNo + FirstArgIndex, B);
  400. }
  401. /// Remove the specified attribute at the specified index from this
  402. /// attribute list. Returns a new list because attribute lists are immutable.
  403. LLVM_NODISCARD AttributeList removeAttribute(LLVMContext &C, unsigned Index,
  404. Attribute::AttrKind Kind) const;
  405. /// Remove the specified attribute at the specified index from this
  406. /// attribute list. Returns a new list because attribute lists are immutable.
  407. LLVM_NODISCARD AttributeList removeAttribute(LLVMContext &C, unsigned Index,
  408. StringRef Kind) const;
  409. /// Remove the specified attributes at the specified index from this
  410. /// attribute list. Returns a new list because attribute lists are immutable.
  411. LLVM_NODISCARD AttributeList removeAttributes(
  412. LLVMContext &C, unsigned Index, const AttrBuilder &AttrsToRemove) const;
  413. /// Remove all attributes at the specified index from this
  414. /// attribute list. Returns a new list because attribute lists are immutable.
  415. LLVM_NODISCARD AttributeList removeAttributes(LLVMContext &C,
  416. unsigned Index) const;
  417. /// Remove the specified attribute at the specified arg index from this
  418. /// attribute list. Returns a new list because attribute lists are immutable.
  419. LLVM_NODISCARD AttributeList removeParamAttribute(
  420. LLVMContext &C, unsigned ArgNo, Attribute::AttrKind Kind) const {
  421. return removeAttribute(C, ArgNo + FirstArgIndex, Kind);
  422. }
  423. /// Remove the specified attribute at the specified arg index from this
  424. /// attribute list. Returns a new list because attribute lists are immutable.
  425. LLVM_NODISCARD AttributeList removeParamAttribute(LLVMContext &C,
  426. unsigned ArgNo,
  427. StringRef Kind) const {
  428. return removeAttribute(C, ArgNo + FirstArgIndex, Kind);
  429. }
  430. /// Remove the specified attribute at the specified arg index from this
  431. /// attribute list. Returns a new list because attribute lists are immutable.
  432. LLVM_NODISCARD AttributeList removeParamAttributes(
  433. LLVMContext &C, unsigned ArgNo, const AttrBuilder &AttrsToRemove) const {
  434. return removeAttributes(C, ArgNo + FirstArgIndex, AttrsToRemove);
  435. }
  436. /// Remove noundef attribute and other attributes that imply undefined
  437. /// behavior if a `undef` or `poison` value is passed from this attribute
  438. /// list. Returns a new list because attribute lists are immutable.
  439. LLVM_NODISCARD AttributeList
  440. removeParamUndefImplyingAttributes(LLVMContext &C, unsigned ArgNo) const;
  441. /// Remove all attributes at the specified arg index from this
  442. /// attribute list. Returns a new list because attribute lists are immutable.
  443. LLVM_NODISCARD AttributeList removeParamAttributes(LLVMContext &C,
  444. unsigned ArgNo) const {
  445. return removeAttributes(C, ArgNo + FirstArgIndex);
  446. }
  447. /// Replace the type contained by attribute \p AttrKind at index \p ArgNo wih
  448. /// \p ReplacementTy, preserving all other attributes.
  449. LLVM_NODISCARD AttributeList replaceAttributeType(LLVMContext &C,
  450. unsigned ArgNo,
  451. Attribute::AttrKind Kind,
  452. Type *ReplacementTy) const {
  453. Attribute Attr = getAttribute(ArgNo, Kind);
  454. auto Attrs = removeAttribute(C, ArgNo, Kind);
  455. return Attrs.addAttribute(C, ArgNo, Attr.getWithNewType(C, ReplacementTy));
  456. }
  457. /// \brief Add the dereferenceable attribute to the attribute set at the given
  458. /// index. Returns a new list because attribute lists are immutable.
  459. LLVM_NODISCARD AttributeList addDereferenceableAttr(LLVMContext &C,
  460. unsigned Index,
  461. uint64_t Bytes) const;
  462. /// \brief Add the dereferenceable attribute to the attribute set at the given
  463. /// arg index. Returns a new list because attribute lists are immutable.
  464. LLVM_NODISCARD AttributeList addDereferenceableParamAttr(
  465. LLVMContext &C, unsigned ArgNo, uint64_t Bytes) const {
  466. return addDereferenceableAttr(C, ArgNo + FirstArgIndex, Bytes);
  467. }
  468. /// Add the dereferenceable_or_null attribute to the attribute set at
  469. /// the given index. Returns a new list because attribute lists are immutable.
  470. LLVM_NODISCARD AttributeList addDereferenceableOrNullAttr(
  471. LLVMContext &C, unsigned Index, uint64_t Bytes) const;
  472. /// Add the dereferenceable_or_null attribute to the attribute set at
  473. /// the given arg index. Returns a new list because attribute lists are
  474. /// immutable.
  475. LLVM_NODISCARD AttributeList addDereferenceableOrNullParamAttr(
  476. LLVMContext &C, unsigned ArgNo, uint64_t Bytes) const {
  477. return addDereferenceableOrNullAttr(C, ArgNo + FirstArgIndex, Bytes);
  478. }
  479. /// Add the allocsize attribute to the attribute set at the given index.
  480. /// Returns a new list because attribute lists are immutable.
  481. LLVM_NODISCARD AttributeList
  482. addAllocSizeAttr(LLVMContext &C, unsigned Index, unsigned ElemSizeArg,
  483. const Optional<unsigned> &NumElemsArg);
  484. /// Add the allocsize attribute to the attribute set at the given arg index.
  485. /// Returns a new list because attribute lists are immutable.
  486. LLVM_NODISCARD AttributeList
  487. addAllocSizeParamAttr(LLVMContext &C, unsigned ArgNo, unsigned ElemSizeArg,
  488. const Optional<unsigned> &NumElemsArg) {
  489. return addAllocSizeAttr(C, ArgNo + FirstArgIndex, ElemSizeArg, NumElemsArg);
  490. }
  491. /// Add the vscale_range attribute to the attribute set at the given index.
  492. /// Returns a new list because attribute lists are immutable.
  493. LLVM_NODISCARD AttributeList addVScaleRangeAttr(LLVMContext &C,
  494. unsigned Index,
  495. unsigned MinValue,
  496. unsigned MaxValue);
  497. //===--------------------------------------------------------------------===//
  498. // AttributeList Accessors
  499. //===--------------------------------------------------------------------===//
  500. /// The attributes for the specified index are returned.
  501. AttributeSet getAttributes(unsigned Index) const;
  502. /// The attributes for the argument or parameter at the given index are
  503. /// returned.
  504. AttributeSet getParamAttributes(unsigned ArgNo) const;
  505. /// The attributes for the ret value are returned.
  506. AttributeSet getRetAttributes() const;
  507. /// The function attributes are returned.
  508. AttributeSet getFnAttributes() const;
  509. /// Return true if the attribute exists at the given index.
  510. bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const;
  511. /// Return true if the attribute exists at the given index.
  512. bool hasAttribute(unsigned Index, StringRef Kind) const;
  513. /// Return true if attribute exists at the given index.
  514. bool hasAttributes(unsigned Index) const;
  515. /// Return true if the attribute exists for the given argument
  516. bool hasParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const {
  517. return hasAttribute(ArgNo + FirstArgIndex, Kind);
  518. }
  519. /// Return true if the attribute exists for the given argument
  520. bool hasParamAttr(unsigned ArgNo, StringRef Kind) const {
  521. return hasAttribute(ArgNo + FirstArgIndex, Kind);
  522. }
  523. /// Return true if attributes exists for the given argument
  524. bool hasParamAttrs(unsigned ArgNo) const {
  525. return hasAttributes(ArgNo + FirstArgIndex);
  526. }
  527. /// Equivalent to hasAttribute(AttributeList::FunctionIndex, Kind) but
  528. /// may be faster.
  529. bool hasFnAttribute(Attribute::AttrKind Kind) const;
  530. /// Equivalent to hasAttribute(AttributeList::FunctionIndex, Kind) but
  531. /// may be faster.
  532. bool hasFnAttribute(StringRef Kind) const;
  533. /// Equivalent to hasAttribute(ArgNo + FirstArgIndex, Kind).
  534. bool hasParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const;
  535. /// Return true if the specified attribute is set for at least one
  536. /// parameter or for the return value. If Index is not nullptr, the index
  537. /// of a parameter with the specified attribute is provided.
  538. bool hasAttrSomewhere(Attribute::AttrKind Kind,
  539. unsigned *Index = nullptr) const;
  540. /// Return the attribute object that exists at the given index.
  541. Attribute getAttribute(unsigned Index, Attribute::AttrKind Kind) const;
  542. /// Return the attribute object that exists at the given index.
  543. Attribute getAttribute(unsigned Index, StringRef Kind) const;
  544. /// Return the attribute object that exists at the arg index.
  545. Attribute getParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const {
  546. return getAttribute(ArgNo + FirstArgIndex, Kind);
  547. }
  548. /// Return the attribute object that exists at the given index.
  549. Attribute getParamAttr(unsigned ArgNo, StringRef Kind) const {
  550. return getAttribute(ArgNo + FirstArgIndex, Kind);
  551. }
  552. /// Return the alignment of the return value.
  553. MaybeAlign getRetAlignment() const;
  554. /// Return the alignment for the specified function parameter.
  555. MaybeAlign getParamAlignment(unsigned ArgNo) const;
  556. /// Return the stack alignment for the specified function parameter.
  557. MaybeAlign getParamStackAlignment(unsigned ArgNo) const;
  558. /// Return the byval type for the specified function parameter.
  559. Type *getParamByValType(unsigned ArgNo) const;
  560. /// Return the sret type for the specified function parameter.
  561. Type *getParamStructRetType(unsigned ArgNo) const;
  562. /// Return the byref type for the specified function parameter.
  563. Type *getParamByRefType(unsigned ArgNo) const;
  564. /// Return the preallocated type for the specified function parameter.
  565. Type *getParamPreallocatedType(unsigned ArgNo) const;
  566. /// Return the inalloca type for the specified function parameter.
  567. Type *getParamInAllocaType(unsigned ArgNo) const;
  568. /// Get the stack alignment.
  569. MaybeAlign getStackAlignment(unsigned Index) const;
  570. /// Get the number of dereferenceable bytes (or zero if unknown).
  571. uint64_t getDereferenceableBytes(unsigned Index) const;
  572. /// Get the number of dereferenceable bytes (or zero if unknown) of an
  573. /// arg.
  574. uint64_t getParamDereferenceableBytes(unsigned ArgNo) const {
  575. return getDereferenceableBytes(ArgNo + FirstArgIndex);
  576. }
  577. /// Get the number of dereferenceable_or_null bytes (or zero if
  578. /// unknown).
  579. uint64_t getDereferenceableOrNullBytes(unsigned Index) const;
  580. /// Get the number of dereferenceable_or_null bytes (or zero if
  581. /// unknown) of an arg.
  582. uint64_t getParamDereferenceableOrNullBytes(unsigned ArgNo) const {
  583. return getDereferenceableOrNullBytes(ArgNo + FirstArgIndex);
  584. }
  585. /// Get the allocsize argument numbers (or pair(0, 0) if unknown).
  586. std::pair<unsigned, Optional<unsigned>>
  587. getAllocSizeArgs(unsigned Index) const;
  588. /// Get the vscale_range argument numbers (or pair(0, 0) if unknown).
  589. std::pair<unsigned, unsigned> getVScaleRangeArgs(unsigned Index) const;
  590. /// Return the attributes at the index as a string.
  591. std::string getAsString(unsigned Index, bool InAttrGrp = false) const;
  592. /// Return true if this attribute list belongs to the LLVMContext.
  593. bool hasParentContext(LLVMContext &C) const;
  594. //===--------------------------------------------------------------------===//
  595. // AttributeList Introspection
  596. //===--------------------------------------------------------------------===//
  597. using iterator = const AttributeSet *;
  598. iterator begin() const;
  599. iterator end() const;
  600. unsigned getNumAttrSets() const;
  601. /// Use these to iterate over the valid attribute indices.
  602. unsigned index_begin() const { return AttributeList::FunctionIndex; }
  603. unsigned index_end() const { return getNumAttrSets() - 1; }
  604. /// operator==/!= - Provide equality predicates.
  605. bool operator==(const AttributeList &RHS) const { return pImpl == RHS.pImpl; }
  606. bool operator!=(const AttributeList &RHS) const { return pImpl != RHS.pImpl; }
  607. /// Return a raw pointer that uniquely identifies this attribute list.
  608. void *getRawPointer() const {
  609. return pImpl;
  610. }
  611. /// Return true if there are no attributes.
  612. bool isEmpty() const { return pImpl == nullptr; }
  613. void print(raw_ostream &O) const;
  614. void dump() const;
  615. };
  616. //===----------------------------------------------------------------------===//
  617. /// \class
  618. /// Provide DenseMapInfo for AttributeList.
  619. template <> struct DenseMapInfo<AttributeList> {
  620. static AttributeList getEmptyKey() {
  621. auto Val = static_cast<uintptr_t>(-1);
  622. Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
  623. return AttributeList(reinterpret_cast<AttributeListImpl *>(Val));
  624. }
  625. static AttributeList getTombstoneKey() {
  626. auto Val = static_cast<uintptr_t>(-2);
  627. Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
  628. return AttributeList(reinterpret_cast<AttributeListImpl *>(Val));
  629. }
  630. static unsigned getHashValue(AttributeList AS) {
  631. return (unsigned((uintptr_t)AS.pImpl) >> 4) ^
  632. (unsigned((uintptr_t)AS.pImpl) >> 9);
  633. }
  634. static bool isEqual(AttributeList LHS, AttributeList RHS) {
  635. return LHS == RHS;
  636. }
  637. };
  638. //===----------------------------------------------------------------------===//
  639. /// \class
  640. /// This class is used in conjunction with the Attribute::get method to
  641. /// create an Attribute object. The object itself is uniquified. The Builder's
  642. /// value, however, is not. So this can be used as a quick way to test for
  643. /// equality, presence of attributes, etc.
  644. class AttrBuilder {
  645. std::bitset<Attribute::EndAttrKinds> Attrs;
  646. std::map<SmallString<32>, SmallString<32>, std::less<>> TargetDepAttrs;
  647. MaybeAlign Alignment;
  648. MaybeAlign StackAlignment;
  649. uint64_t DerefBytes = 0;
  650. uint64_t DerefOrNullBytes = 0;
  651. uint64_t AllocSizeArgs = 0;
  652. uint64_t VScaleRangeArgs = 0;
  653. Type *ByValType = nullptr;
  654. Type *StructRetType = nullptr;
  655. Type *ByRefType = nullptr;
  656. Type *PreallocatedType = nullptr;
  657. Type *InAllocaType = nullptr;
  658. public:
  659. AttrBuilder() = default;
  660. AttrBuilder(const Attribute &A) {
  661. addAttribute(A);
  662. }
  663. AttrBuilder(AttributeList AS, unsigned Idx);
  664. AttrBuilder(AttributeSet AS);
  665. void clear();
  666. /// Add an attribute to the builder.
  667. AttrBuilder &addAttribute(Attribute::AttrKind Val) {
  668. assert((unsigned)Val < Attribute::EndAttrKinds &&
  669. "Attribute out of range!");
  670. assert(!Attribute::doesAttrKindHaveArgument(Val) &&
  671. "Adding integer attribute without adding a value!");
  672. Attrs[Val] = true;
  673. return *this;
  674. }
  675. /// Add the Attribute object to the builder.
  676. AttrBuilder &addAttribute(Attribute A);
  677. /// Add the target-dependent attribute to the builder.
  678. AttrBuilder &addAttribute(StringRef A, StringRef V = StringRef());
  679. /// Remove an attribute from the builder.
  680. AttrBuilder &removeAttribute(Attribute::AttrKind Val);
  681. /// Remove the attributes from the builder.
  682. AttrBuilder &removeAttributes(AttributeList A, uint64_t WithoutIndex);
  683. /// Remove the target-dependent attribute to the builder.
  684. AttrBuilder &removeAttribute(StringRef A);
  685. /// Add the attributes from the builder.
  686. AttrBuilder &merge(const AttrBuilder &B);
  687. /// Remove the attributes from the builder.
  688. AttrBuilder &remove(const AttrBuilder &B);
  689. /// Return true if the builder has any attribute that's in the
  690. /// specified builder.
  691. bool overlaps(const AttrBuilder &B) const;
  692. /// Return true if the builder has the specified attribute.
  693. bool contains(Attribute::AttrKind A) const {
  694. assert((unsigned)A < Attribute::EndAttrKinds && "Attribute out of range!");
  695. return Attrs[A];
  696. }
  697. /// Return true if the builder has the specified target-dependent
  698. /// attribute.
  699. bool contains(StringRef A) const;
  700. /// Return true if the builder has IR-level attributes.
  701. bool hasAttributes() const;
  702. /// Return true if the builder has any attribute that's in the
  703. /// specified attribute.
  704. bool hasAttributes(AttributeList A, uint64_t Index) const;
  705. /// Return true if the builder has an alignment attribute.
  706. bool hasAlignmentAttr() const;
  707. /// Retrieve the alignment attribute, if it exists.
  708. MaybeAlign getAlignment() const { return Alignment; }
  709. /// Retrieve the stack alignment attribute, if it exists.
  710. MaybeAlign getStackAlignment() const { return StackAlignment; }
  711. /// Retrieve the number of dereferenceable bytes, if the
  712. /// dereferenceable attribute exists (zero is returned otherwise).
  713. uint64_t getDereferenceableBytes() const { return DerefBytes; }
  714. /// Retrieve the number of dereferenceable_or_null bytes, if the
  715. /// dereferenceable_or_null attribute exists (zero is returned otherwise).
  716. uint64_t getDereferenceableOrNullBytes() const { return DerefOrNullBytes; }
  717. /// Retrieve the byval type.
  718. Type *getByValType() const { return ByValType; }
  719. /// Retrieve the sret type.
  720. Type *getStructRetType() const { return StructRetType; }
  721. /// Retrieve the byref type.
  722. Type *getByRefType() const { return ByRefType; }
  723. /// Retrieve the preallocated type.
  724. Type *getPreallocatedType() const { return PreallocatedType; }
  725. /// Retrieve the inalloca type.
  726. Type *getInAllocaType() const { return InAllocaType; }
  727. /// Retrieve the allocsize args, if the allocsize attribute exists. If it
  728. /// doesn't exist, pair(0, 0) is returned.
  729. std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
  730. /// Retrieve the vscale_range args, if the vscale_range attribute exists. If
  731. /// it doesn't exist, pair(0, 0) is returned.
  732. std::pair<unsigned, unsigned> getVScaleRangeArgs() const;
  733. /// This turns an alignment into the form used internally in Attribute.
  734. /// This call has no effect if Align is not set.
  735. AttrBuilder &addAlignmentAttr(MaybeAlign Align);
  736. /// This turns an int alignment (which must be a power of 2) into the
  737. /// form used internally in Attribute.
  738. /// This call has no effect if Align is 0.
  739. /// Deprecated, use the version using a MaybeAlign.
  740. inline AttrBuilder &addAlignmentAttr(unsigned Align) {
  741. return addAlignmentAttr(MaybeAlign(Align));
  742. }
  743. /// This turns a stack alignment into the form used internally in Attribute.
  744. /// This call has no effect if Align is not set.
  745. AttrBuilder &addStackAlignmentAttr(MaybeAlign Align);
  746. /// This turns an int stack alignment (which must be a power of 2) into
  747. /// the form used internally in Attribute.
  748. /// This call has no effect if Align is 0.
  749. /// Deprecated, use the version using a MaybeAlign.
  750. inline AttrBuilder &addStackAlignmentAttr(unsigned Align) {
  751. return addStackAlignmentAttr(MaybeAlign(Align));
  752. }
  753. /// This turns the number of dereferenceable bytes into the form used
  754. /// internally in Attribute.
  755. AttrBuilder &addDereferenceableAttr(uint64_t Bytes);
  756. /// This turns the number of dereferenceable_or_null bytes into the
  757. /// form used internally in Attribute.
  758. AttrBuilder &addDereferenceableOrNullAttr(uint64_t Bytes);
  759. /// This turns one (or two) ints into the form used internally in Attribute.
  760. AttrBuilder &addAllocSizeAttr(unsigned ElemSizeArg,
  761. const Optional<unsigned> &NumElemsArg);
  762. /// This turns two ints into the form used internally in Attribute.
  763. AttrBuilder &addVScaleRangeAttr(unsigned MinValue, unsigned MaxValue);
  764. /// This turns a byval type into the form used internally in Attribute.
  765. AttrBuilder &addByValAttr(Type *Ty);
  766. /// This turns a sret type into the form used internally in Attribute.
  767. AttrBuilder &addStructRetAttr(Type *Ty);
  768. /// This turns a byref type into the form used internally in Attribute.
  769. AttrBuilder &addByRefAttr(Type *Ty);
  770. /// This turns a preallocated type into the form used internally in Attribute.
  771. AttrBuilder &addPreallocatedAttr(Type *Ty);
  772. /// This turns an inalloca type into the form used internally in Attribute.
  773. AttrBuilder &addInAllocaAttr(Type *Ty);
  774. /// Add an allocsize attribute, using the representation returned by
  775. /// Attribute.getIntValue().
  776. AttrBuilder &addAllocSizeAttrFromRawRepr(uint64_t RawAllocSizeRepr);
  777. /// Add a vscale_range attribute, using the representation returned by
  778. /// Attribute.getIntValue().
  779. AttrBuilder &addVScaleRangeAttrFromRawRepr(uint64_t RawVScaleRangeRepr);
  780. /// Return true if the builder contains no target-independent
  781. /// attributes.
  782. bool empty() const { return Attrs.none(); }
  783. // Iterators for target-dependent attributes.
  784. using td_type = decltype(TargetDepAttrs)::value_type;
  785. using td_iterator = decltype(TargetDepAttrs)::iterator;
  786. using td_const_iterator = decltype(TargetDepAttrs)::const_iterator;
  787. using td_range = iterator_range<td_iterator>;
  788. using td_const_range = iterator_range<td_const_iterator>;
  789. td_iterator td_begin() { return TargetDepAttrs.begin(); }
  790. td_iterator td_end() { return TargetDepAttrs.end(); }
  791. td_const_iterator td_begin() const { return TargetDepAttrs.begin(); }
  792. td_const_iterator td_end() const { return TargetDepAttrs.end(); }
  793. td_range td_attrs() { return td_range(td_begin(), td_end()); }
  794. td_const_range td_attrs() const {
  795. return td_const_range(td_begin(), td_end());
  796. }
  797. bool td_empty() const { return TargetDepAttrs.empty(); }
  798. bool operator==(const AttrBuilder &B) const;
  799. bool operator!=(const AttrBuilder &B) const { return !(*this == B); }
  800. };
  801. namespace AttributeFuncs {
  802. /// Which attributes cannot be applied to a type.
  803. AttrBuilder typeIncompatible(Type *Ty);
  804. /// \returns Return true if the two functions have compatible target-independent
  805. /// attributes for inlining purposes.
  806. bool areInlineCompatible(const Function &Caller, const Function &Callee);
  807. /// Checks if there are any incompatible function attributes between
  808. /// \p A and \p B.
  809. ///
  810. /// \param [in] A - The first function to be compared with.
  811. /// \param [in] B - The second function to be compared with.
  812. /// \returns true if the functions have compatible attributes.
  813. bool areOutlineCompatible(const Function &A, const Function &B);
  814. /// Merge caller's and callee's attributes.
  815. void mergeAttributesForInlining(Function &Caller, const Function &Callee);
  816. /// Merges the functions attributes from \p ToMerge into function \p Base.
  817. ///
  818. /// \param [in,out] Base - The function being merged into.
  819. /// \param [in] ToMerge - The function to merge attributes from.
  820. void mergeAttributesForOutlining(Function &Base, const Function &ToMerge);
  821. } // end namespace AttributeFuncs
  822. } // end namespace llvm
  823. #endif // LLVM_IR_ATTRIBUTES_H