Attributes.h 39 KB

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