Attributes.h 49 KB

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