AttributeImpl.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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/FoldingSet.h"
  18. #include "llvm/ADT/StringRef.h"
  19. #include "llvm/IR/Attributes.h"
  20. #include "llvm/Support/TrailingObjects.h"
  21. #include <cassert>
  22. #include <cstddef>
  23. #include <cstdint>
  24. #include <string>
  25. #include <utility>
  26. namespace llvm {
  27. class LLVMContext;
  28. class Type;
  29. //===----------------------------------------------------------------------===//
  30. /// \class
  31. /// This class represents a single, uniqued attribute. That attribute
  32. /// could be a single enum, a tuple, or a string.
  33. class AttributeImpl : public FoldingSetNode {
  34. unsigned char KindID; ///< Holds the AttrEntryKind of the attribute
  35. protected:
  36. enum AttrEntryKind {
  37. EnumAttrEntry,
  38. IntAttrEntry,
  39. StringAttrEntry,
  40. TypeAttrEntry,
  41. };
  42. AttributeImpl(AttrEntryKind KindID) : KindID(KindID) {}
  43. public:
  44. // AttributesImpl is uniqued, these should not be available.
  45. AttributeImpl(const AttributeImpl &) = delete;
  46. AttributeImpl &operator=(const AttributeImpl &) = delete;
  47. virtual ~AttributeImpl();
  48. bool isEnumAttribute() const { return KindID == EnumAttrEntry; }
  49. bool isIntAttribute() const { return KindID == IntAttrEntry; }
  50. bool isStringAttribute() const { return KindID == StringAttrEntry; }
  51. bool isTypeAttribute() const { return KindID == TypeAttrEntry; }
  52. bool hasAttribute(Attribute::AttrKind A) const;
  53. bool hasAttribute(StringRef Kind) const;
  54. Attribute::AttrKind getKindAsEnum() const;
  55. uint64_t getValueAsInt() const;
  56. StringRef getKindAsString() const;
  57. StringRef getValueAsString() const;
  58. Type *getValueAsType() const;
  59. /// Used when sorting the attributes.
  60. bool operator<(const AttributeImpl &AI) const;
  61. void Profile(FoldingSetNodeID &ID) const {
  62. if (isEnumAttribute())
  63. Profile(ID, getKindAsEnum(), static_cast<uint64_t>(0));
  64. else if (isIntAttribute())
  65. Profile(ID, getKindAsEnum(), getValueAsInt());
  66. else if (isStringAttribute())
  67. Profile(ID, getKindAsString(), getValueAsString());
  68. else
  69. Profile(ID, getKindAsEnum(), getValueAsType());
  70. }
  71. static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind,
  72. uint64_t Val) {
  73. ID.AddInteger(Kind);
  74. if (Val) ID.AddInteger(Val);
  75. }
  76. static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values) {
  77. ID.AddString(Kind);
  78. if (!Values.empty()) ID.AddString(Values);
  79. }
  80. static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind,
  81. Type *Ty) {
  82. ID.AddInteger(Kind);
  83. ID.AddPointer(Ty);
  84. }
  85. };
  86. //===----------------------------------------------------------------------===//
  87. /// \class
  88. /// A set of classes that contain the value of the
  89. /// attribute object. There are three main categories: enum attribute entries,
  90. /// represented by Attribute::AttrKind; alignment attribute entries; and string
  91. /// attribute enties, which are for target-dependent attributes.
  92. class EnumAttributeImpl : public AttributeImpl {
  93. virtual void anchor();
  94. Attribute::AttrKind Kind;
  95. protected:
  96. EnumAttributeImpl(AttrEntryKind ID, Attribute::AttrKind Kind)
  97. : AttributeImpl(ID), Kind(Kind) {}
  98. public:
  99. EnumAttributeImpl(Attribute::AttrKind Kind)
  100. : AttributeImpl(EnumAttrEntry), Kind(Kind) {}
  101. Attribute::AttrKind getEnumKind() const { return Kind; }
  102. };
  103. class IntAttributeImpl : public EnumAttributeImpl {
  104. uint64_t Val;
  105. void anchor() override;
  106. public:
  107. IntAttributeImpl(Attribute::AttrKind Kind, uint64_t Val)
  108. : EnumAttributeImpl(IntAttrEntry, Kind), Val(Val) {
  109. assert((Kind == Attribute::Alignment || Kind == Attribute::StackAlignment ||
  110. Kind == Attribute::Dereferenceable ||
  111. Kind == Attribute::DereferenceableOrNull ||
  112. Kind == Attribute::AllocSize) &&
  113. "Wrong kind for int attribute!");
  114. }
  115. uint64_t getValue() const { return Val; }
  116. };
  117. class StringAttributeImpl : public AttributeImpl {
  118. virtual void anchor();
  119. std::string Kind;
  120. std::string Val;
  121. public:
  122. StringAttributeImpl(StringRef Kind, StringRef Val = StringRef())
  123. : AttributeImpl(StringAttrEntry), Kind(Kind), Val(Val) {}
  124. StringRef getStringKind() const { return Kind; }
  125. StringRef getStringValue() const { return Val; }
  126. };
  127. class TypeAttributeImpl : public EnumAttributeImpl {
  128. virtual void anchor();
  129. Type *Ty;
  130. public:
  131. TypeAttributeImpl(Attribute::AttrKind Kind, Type *Ty)
  132. : EnumAttributeImpl(TypeAttrEntry, Kind), Ty(Ty) {}
  133. Type *getTypeValue() const { return Ty; }
  134. };
  135. //===----------------------------------------------------------------------===//
  136. /// \class
  137. /// This class represents a group of attributes that apply to one
  138. /// element: function, return type, or parameter.
  139. class AttributeSetNode final
  140. : public FoldingSetNode,
  141. private TrailingObjects<AttributeSetNode, Attribute> {
  142. friend TrailingObjects;
  143. unsigned NumAttrs; ///< Number of attributes in this node.
  144. /// Bitset with a bit for each available attribute Attribute::AttrKind.
  145. uint8_t AvailableAttrs[12] = {};
  146. AttributeSetNode(ArrayRef<Attribute> Attrs);
  147. public:
  148. // AttributesSetNode is uniqued, these should not be available.
  149. AttributeSetNode(const AttributeSetNode &) = delete;
  150. AttributeSetNode &operator=(const AttributeSetNode &) = delete;
  151. void operator delete(void *p) { ::operator delete(p); }
  152. static AttributeSetNode *get(LLVMContext &C, const AttrBuilder &B);
  153. static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs);
  154. /// Return the number of attributes this AttributeList contains.
  155. unsigned getNumAttributes() const { return NumAttrs; }
  156. bool hasAttribute(Attribute::AttrKind Kind) const {
  157. return AvailableAttrs[Kind / 8] & ((uint64_t)1) << (Kind % 8);
  158. }
  159. bool hasAttribute(StringRef Kind) const;
  160. bool hasAttributes() const { return NumAttrs != 0; }
  161. Attribute getAttribute(Attribute::AttrKind Kind) const;
  162. Attribute getAttribute(StringRef Kind) const;
  163. unsigned getAlignment() const;
  164. unsigned getStackAlignment() const;
  165. uint64_t getDereferenceableBytes() const;
  166. uint64_t getDereferenceableOrNullBytes() const;
  167. std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
  168. std::string getAsString(bool InAttrGrp) const;
  169. Type *getByValType() const;
  170. using iterator = const Attribute *;
  171. iterator begin() const { return getTrailingObjects<Attribute>(); }
  172. iterator end() const { return begin() + NumAttrs; }
  173. void Profile(FoldingSetNodeID &ID) const {
  174. Profile(ID, makeArrayRef(begin(), end()));
  175. }
  176. static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) {
  177. for (const auto &Attr : AttrList)
  178. Attr.Profile(ID);
  179. }
  180. };
  181. using IndexAttrPair = std::pair<unsigned, AttributeSet>;
  182. //===----------------------------------------------------------------------===//
  183. /// \class
  184. /// This class represents a set of attributes that apply to the function,
  185. /// return type, and parameters.
  186. class AttributeListImpl final
  187. : public FoldingSetNode,
  188. private TrailingObjects<AttributeListImpl, AttributeSet> {
  189. friend class AttributeList;
  190. friend TrailingObjects;
  191. private:
  192. LLVMContext &Context;
  193. unsigned NumAttrSets; ///< Number of entries in this set.
  194. /// Bitset with a bit for each available attribute Attribute::AttrKind.
  195. uint8_t AvailableFunctionAttrs[12] = {};
  196. // Helper fn for TrailingObjects class.
  197. size_t numTrailingObjects(OverloadToken<AttributeSet>) { return NumAttrSets; }
  198. public:
  199. AttributeListImpl(LLVMContext &C, ArrayRef<AttributeSet> Sets);
  200. // AttributesSetImpt is uniqued, these should not be available.
  201. AttributeListImpl(const AttributeListImpl &) = delete;
  202. AttributeListImpl &operator=(const AttributeListImpl &) = delete;
  203. void operator delete(void *p) { ::operator delete(p); }
  204. /// Get the context that created this AttributeListImpl.
  205. LLVMContext &getContext() { return Context; }
  206. /// Return true if the AttributeSet or the FunctionIndex has an
  207. /// enum attribute of the given kind.
  208. bool hasFnAttribute(Attribute::AttrKind Kind) const {
  209. return AvailableFunctionAttrs[Kind / 8] & ((uint64_t)1) << (Kind % 8);
  210. }
  211. using iterator = const AttributeSet *;
  212. iterator begin() const { return getTrailingObjects<AttributeSet>(); }
  213. iterator end() const { return begin() + NumAttrSets; }
  214. void Profile(FoldingSetNodeID &ID) const;
  215. static void Profile(FoldingSetNodeID &ID, ArrayRef<AttributeSet> Nodes);
  216. void dump() const;
  217. };
  218. } // end namespace llvm
  219. #endif // LLVM_LIB_IR_ATTRIBUTEIMPL_H