AttributeImpl.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. //===- AttributeImpl.h - Attribute Internals --------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. ///
  9. /// \file
  10. /// This file defines various helper methods and classes used by
  11. /// LLVMContextImpl for creating and managing attributes.
  12. ///
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_LIB_IR_ATTRIBUTEIMPL_H
  15. #define LLVM_LIB_IR_ATTRIBUTEIMPL_H
  16. #include "llvm/ADT/ArrayRef.h"
  17. #include "llvm/ADT/DenseMap.h"
  18. #include "llvm/ADT/FoldingSet.h"
  19. #include "llvm/ADT/StringRef.h"
  20. #include "llvm/IR/Attributes.h"
  21. #include "llvm/Support/TrailingObjects.h"
  22. #include <cassert>
  23. #include <cstddef>
  24. #include <cstdint>
  25. #include <optional>
  26. #include <string>
  27. #include <utility>
  28. namespace llvm {
  29. class LLVMContext;
  30. class Type;
  31. //===----------------------------------------------------------------------===//
  32. /// \class
  33. /// This class represents a single, uniqued attribute. That attribute
  34. /// could be a single enum, a tuple, or a string.
  35. class AttributeImpl : public FoldingSetNode {
  36. unsigned char KindID; ///< Holds the AttrEntryKind of the attribute
  37. protected:
  38. enum AttrEntryKind {
  39. EnumAttrEntry,
  40. IntAttrEntry,
  41. StringAttrEntry,
  42. TypeAttrEntry,
  43. };
  44. AttributeImpl(AttrEntryKind KindID) : KindID(KindID) {}
  45. public:
  46. // AttributesImpl is uniqued, these should not be available.
  47. AttributeImpl(const AttributeImpl &) = delete;
  48. AttributeImpl &operator=(const AttributeImpl &) = delete;
  49. bool isEnumAttribute() const { return KindID == EnumAttrEntry; }
  50. bool isIntAttribute() const { return KindID == IntAttrEntry; }
  51. bool isStringAttribute() const { return KindID == StringAttrEntry; }
  52. bool isTypeAttribute() const { return KindID == TypeAttrEntry; }
  53. bool hasAttribute(Attribute::AttrKind A) const;
  54. bool hasAttribute(StringRef Kind) const;
  55. Attribute::AttrKind getKindAsEnum() const;
  56. uint64_t getValueAsInt() const;
  57. bool getValueAsBool() const;
  58. StringRef getKindAsString() const;
  59. StringRef getValueAsString() const;
  60. Type *getValueAsType() const;
  61. /// Used when sorting the attributes.
  62. bool operator<(const AttributeImpl &AI) const;
  63. void Profile(FoldingSetNodeID &ID) const {
  64. if (isEnumAttribute())
  65. Profile(ID, getKindAsEnum());
  66. else if (isIntAttribute())
  67. Profile(ID, getKindAsEnum(), getValueAsInt());
  68. else if (isStringAttribute())
  69. Profile(ID, getKindAsString(), getValueAsString());
  70. else
  71. Profile(ID, getKindAsEnum(), getValueAsType());
  72. }
  73. static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind) {
  74. assert(Attribute::isEnumAttrKind(Kind) && "Expected enum attribute");
  75. ID.AddInteger(Kind);
  76. }
  77. static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind,
  78. uint64_t Val) {
  79. assert(Attribute::isIntAttrKind(Kind) && "Expected int attribute");
  80. ID.AddInteger(Kind);
  81. ID.AddInteger(Val);
  82. }
  83. static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values) {
  84. ID.AddString(Kind);
  85. if (!Values.empty()) ID.AddString(Values);
  86. }
  87. static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind,
  88. Type *Ty) {
  89. ID.AddInteger(Kind);
  90. ID.AddPointer(Ty);
  91. }
  92. };
  93. static_assert(std::is_trivially_destructible<AttributeImpl>::value,
  94. "AttributeImpl should be trivially destructible");
  95. //===----------------------------------------------------------------------===//
  96. /// \class
  97. /// A set of classes that contain the value of the
  98. /// attribute object. There are three main categories: enum attribute entries,
  99. /// represented by Attribute::AttrKind; alignment attribute entries; and string
  100. /// attribute enties, which are for target-dependent attributes.
  101. class EnumAttributeImpl : public AttributeImpl {
  102. Attribute::AttrKind Kind;
  103. protected:
  104. EnumAttributeImpl(AttrEntryKind ID, Attribute::AttrKind Kind)
  105. : AttributeImpl(ID), Kind(Kind) {}
  106. public:
  107. EnumAttributeImpl(Attribute::AttrKind Kind)
  108. : AttributeImpl(EnumAttrEntry), Kind(Kind) {
  109. assert(Kind != Attribute::AttrKind::None &&
  110. "Can't create a None attribute!");
  111. }
  112. Attribute::AttrKind getEnumKind() const { return Kind; }
  113. };
  114. class IntAttributeImpl : public EnumAttributeImpl {
  115. uint64_t Val;
  116. public:
  117. IntAttributeImpl(Attribute::AttrKind Kind, uint64_t Val)
  118. : EnumAttributeImpl(IntAttrEntry, Kind), Val(Val) {
  119. assert(Attribute::isIntAttrKind(Kind) &&
  120. "Wrong kind for int attribute!");
  121. }
  122. uint64_t getValue() const { return Val; }
  123. };
  124. class StringAttributeImpl final
  125. : public AttributeImpl,
  126. private TrailingObjects<StringAttributeImpl, char> {
  127. friend TrailingObjects;
  128. unsigned KindSize;
  129. unsigned ValSize;
  130. size_t numTrailingObjects(OverloadToken<char>) const {
  131. return KindSize + 1 + ValSize + 1;
  132. }
  133. public:
  134. StringAttributeImpl(StringRef Kind, StringRef Val = StringRef())
  135. : AttributeImpl(StringAttrEntry), KindSize(Kind.size()),
  136. ValSize(Val.size()) {
  137. char *TrailingString = getTrailingObjects<char>();
  138. // Some users rely on zero-termination.
  139. llvm::copy(Kind, TrailingString);
  140. TrailingString[KindSize] = '\0';
  141. llvm::copy(Val, &TrailingString[KindSize + 1]);
  142. TrailingString[KindSize + 1 + ValSize] = '\0';
  143. }
  144. StringRef getStringKind() const {
  145. return StringRef(getTrailingObjects<char>(), KindSize);
  146. }
  147. StringRef getStringValue() const {
  148. return StringRef(getTrailingObjects<char>() + KindSize + 1, ValSize);
  149. }
  150. static size_t totalSizeToAlloc(StringRef Kind, StringRef Val) {
  151. return TrailingObjects::totalSizeToAlloc<char>(Kind.size() + 1 +
  152. Val.size() + 1);
  153. }
  154. };
  155. class TypeAttributeImpl : public EnumAttributeImpl {
  156. Type *Ty;
  157. public:
  158. TypeAttributeImpl(Attribute::AttrKind Kind, Type *Ty)
  159. : EnumAttributeImpl(TypeAttrEntry, Kind), Ty(Ty) {}
  160. Type *getTypeValue() const { return Ty; }
  161. };
  162. class AttributeBitSet {
  163. /// Bitset with a bit for each available attribute Attribute::AttrKind.
  164. uint8_t AvailableAttrs[12] = {};
  165. static_assert(Attribute::EndAttrKinds <= sizeof(AvailableAttrs) * CHAR_BIT,
  166. "Too many attributes");
  167. public:
  168. bool hasAttribute(Attribute::AttrKind Kind) const {
  169. return AvailableAttrs[Kind / 8] & (1 << (Kind % 8));
  170. }
  171. void addAttribute(Attribute::AttrKind Kind) {
  172. AvailableAttrs[Kind / 8] |= 1 << (Kind % 8);
  173. }
  174. };
  175. //===----------------------------------------------------------------------===//
  176. /// \class
  177. /// This class represents a group of attributes that apply to one
  178. /// element: function, return type, or parameter.
  179. class AttributeSetNode final
  180. : public FoldingSetNode,
  181. private TrailingObjects<AttributeSetNode, Attribute> {
  182. friend TrailingObjects;
  183. unsigned NumAttrs; ///< Number of attributes in this node.
  184. AttributeBitSet AvailableAttrs; ///< Available enum attributes.
  185. DenseMap<StringRef, Attribute> StringAttrs;
  186. AttributeSetNode(ArrayRef<Attribute> Attrs);
  187. static AttributeSetNode *getSorted(LLVMContext &C,
  188. ArrayRef<Attribute> SortedAttrs);
  189. std::optional<Attribute> findEnumAttribute(Attribute::AttrKind Kind) const;
  190. public:
  191. // AttributesSetNode is uniqued, these should not be available.
  192. AttributeSetNode(const AttributeSetNode &) = delete;
  193. AttributeSetNode &operator=(const AttributeSetNode &) = delete;
  194. void operator delete(void *p) { ::operator delete(p); }
  195. static AttributeSetNode *get(LLVMContext &C, const AttrBuilder &B);
  196. static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs);
  197. /// Return the number of attributes this AttributeList contains.
  198. unsigned getNumAttributes() const { return NumAttrs; }
  199. bool hasAttribute(Attribute::AttrKind Kind) const {
  200. return AvailableAttrs.hasAttribute(Kind);
  201. }
  202. bool hasAttribute(StringRef Kind) const;
  203. bool hasAttributes() const { return NumAttrs != 0; }
  204. Attribute getAttribute(Attribute::AttrKind Kind) const;
  205. Attribute getAttribute(StringRef Kind) const;
  206. MaybeAlign getAlignment() const;
  207. MaybeAlign getStackAlignment() const;
  208. uint64_t getDereferenceableBytes() const;
  209. uint64_t getDereferenceableOrNullBytes() const;
  210. std::optional<std::pair<unsigned, std::optional<unsigned>>> getAllocSizeArgs()
  211. const;
  212. unsigned getVScaleRangeMin() const;
  213. std::optional<unsigned> getVScaleRangeMax() const;
  214. UWTableKind getUWTableKind() const;
  215. AllocFnKind getAllocKind() const;
  216. MemoryEffects getMemoryEffects() const;
  217. std::string getAsString(bool InAttrGrp) const;
  218. Type *getAttributeType(Attribute::AttrKind Kind) const;
  219. using iterator = const Attribute *;
  220. iterator begin() const { return getTrailingObjects<Attribute>(); }
  221. iterator end() const { return begin() + NumAttrs; }
  222. void Profile(FoldingSetNodeID &ID) const {
  223. Profile(ID, ArrayRef(begin(), end()));
  224. }
  225. static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) {
  226. for (const auto &Attr : AttrList)
  227. Attr.Profile(ID);
  228. }
  229. };
  230. //===----------------------------------------------------------------------===//
  231. /// \class
  232. /// This class represents a set of attributes that apply to the function,
  233. /// return type, and parameters.
  234. class AttributeListImpl final
  235. : public FoldingSetNode,
  236. private TrailingObjects<AttributeListImpl, AttributeSet> {
  237. friend class AttributeList;
  238. friend TrailingObjects;
  239. private:
  240. unsigned NumAttrSets; ///< Number of entries in this set.
  241. /// Available enum function attributes.
  242. AttributeBitSet AvailableFunctionAttrs;
  243. /// Union of enum attributes available at any index.
  244. AttributeBitSet AvailableSomewhereAttrs;
  245. // Helper fn for TrailingObjects class.
  246. size_t numTrailingObjects(OverloadToken<AttributeSet>) { return NumAttrSets; }
  247. public:
  248. AttributeListImpl(ArrayRef<AttributeSet> Sets);
  249. // AttributesSetImpt is uniqued, these should not be available.
  250. AttributeListImpl(const AttributeListImpl &) = delete;
  251. AttributeListImpl &operator=(const AttributeListImpl &) = delete;
  252. /// Return true if the AttributeSet or the FunctionIndex has an
  253. /// enum attribute of the given kind.
  254. bool hasFnAttribute(Attribute::AttrKind Kind) const {
  255. return AvailableFunctionAttrs.hasAttribute(Kind);
  256. }
  257. /// Return true if the specified attribute is set for at least one
  258. /// parameter or for the return value. If Index is not nullptr, the index
  259. /// of a parameter with the specified attribute is provided.
  260. bool hasAttrSomewhere(Attribute::AttrKind Kind,
  261. unsigned *Index = nullptr) const;
  262. using iterator = const AttributeSet *;
  263. iterator begin() const { return getTrailingObjects<AttributeSet>(); }
  264. iterator end() const { return begin() + NumAttrSets; }
  265. void Profile(FoldingSetNodeID &ID) const;
  266. static void Profile(FoldingSetNodeID &ID, ArrayRef<AttributeSet> Nodes);
  267. void dump() const;
  268. };
  269. static_assert(std::is_trivially_destructible<AttributeListImpl>::value,
  270. "AttributeListImpl should be trivially destructible");
  271. } // end namespace llvm
  272. #endif // LLVM_LIB_IR_ATTRIBUTEIMPL_H