AttributeImpl.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. //===-- AttributeImpl.h - Attribute Internals -------------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. ///
  10. /// \file
  11. /// \brief This file defines various helper methods and classes used by
  12. /// LLVMContextImpl for creating and managing attributes.
  13. ///
  14. //===----------------------------------------------------------------------===//
  15. #ifndef LLVM_LIB_IR_ATTRIBUTEIMPL_H
  16. #define LLVM_LIB_IR_ATTRIBUTEIMPL_H
  17. #include "llvm/ADT/FoldingSet.h"
  18. #include "llvm/IR/Attributes.h"
  19. #include <string>
  20. namespace llvm {
  21. class Constant;
  22. class LLVMContext;
  23. //===----------------------------------------------------------------------===//
  24. /// \class
  25. /// \brief This class represents a single, uniqued attribute. That attribute
  26. /// could be a single enum, a tuple, or a string.
  27. class AttributeImpl : public FoldingSetNode {
  28. unsigned char KindID; ///< Holds the AttrEntryKind of the attribute
  29. // AttributesImpl is uniqued, these should not be publicly available.
  30. void operator=(const AttributeImpl &) = delete;
  31. AttributeImpl(const AttributeImpl &) = delete;
  32. protected:
  33. enum AttrEntryKind {
  34. EnumAttrEntry,
  35. IntAttrEntry,
  36. StringAttrEntry
  37. };
  38. AttributeImpl(AttrEntryKind KindID) : KindID(KindID) {}
  39. public:
  40. virtual ~AttributeImpl();
  41. bool isEnumAttribute() const { return KindID == EnumAttrEntry; }
  42. bool isIntAttribute() const { return KindID == IntAttrEntry; }
  43. bool isStringAttribute() const { return KindID == StringAttrEntry; }
  44. bool hasAttribute(Attribute::AttrKind A) const;
  45. bool hasAttribute(StringRef Kind) const;
  46. Attribute::AttrKind getKindAsEnum() const;
  47. uint64_t getValueAsInt() const;
  48. StringRef getKindAsString() const;
  49. StringRef getValueAsString() const;
  50. /// \brief Used when sorting the attributes.
  51. bool operator<(const AttributeImpl &AI) const;
  52. void Profile(FoldingSetNodeID &ID) const {
  53. if (isEnumAttribute())
  54. Profile(ID, getKindAsEnum(), 0);
  55. else if (isIntAttribute())
  56. Profile(ID, getKindAsEnum(), getValueAsInt());
  57. else
  58. Profile(ID, getKindAsString(), getValueAsString());
  59. }
  60. static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind,
  61. uint64_t Val) {
  62. ID.AddInteger(Kind);
  63. if (Val) ID.AddInteger(Val);
  64. }
  65. static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values) {
  66. ID.AddString(Kind);
  67. if (!Values.empty()) ID.AddString(Values);
  68. }
  69. // FIXME: Remove this!
  70. static uint64_t getAttrMask(Attribute::AttrKind Val);
  71. };
  72. //===----------------------------------------------------------------------===//
  73. /// \class
  74. /// \brief A set of classes that contain the value of the
  75. /// attribute object. There are three main categories: enum attribute entries,
  76. /// represented by Attribute::AttrKind; alignment attribute entries; and string
  77. /// attribute enties, which are for target-dependent attributes.
  78. class EnumAttributeImpl : public AttributeImpl {
  79. virtual void anchor();
  80. Attribute::AttrKind Kind;
  81. protected:
  82. EnumAttributeImpl(AttrEntryKind ID, Attribute::AttrKind Kind)
  83. : AttributeImpl(ID), Kind(Kind) {}
  84. public:
  85. EnumAttributeImpl(Attribute::AttrKind Kind)
  86. : AttributeImpl(EnumAttrEntry), Kind(Kind) {}
  87. Attribute::AttrKind getEnumKind() const { return Kind; }
  88. };
  89. class IntAttributeImpl : public EnumAttributeImpl {
  90. void anchor() override;
  91. uint64_t Val;
  92. public:
  93. IntAttributeImpl(Attribute::AttrKind Kind, uint64_t Val)
  94. : EnumAttributeImpl(IntAttrEntry, Kind), Val(Val) {
  95. assert((Kind == Attribute::Alignment || Kind == Attribute::StackAlignment ||
  96. Kind == Attribute::Dereferenceable ||
  97. Kind == Attribute::DereferenceableOrNull) &&
  98. "Wrong kind for int attribute!");
  99. }
  100. uint64_t getValue() const { return Val; }
  101. };
  102. class StringAttributeImpl : public AttributeImpl {
  103. virtual void anchor();
  104. std::string Kind;
  105. std::string Val;
  106. public:
  107. StringAttributeImpl(StringRef Kind, StringRef Val = StringRef())
  108. : AttributeImpl(StringAttrEntry), Kind(Kind), Val(Val) {}
  109. StringRef getStringKind() const { return Kind; }
  110. StringRef getStringValue() const { return Val; }
  111. };
  112. //===----------------------------------------------------------------------===//
  113. /// \class
  114. /// \brief This class represents a group of attributes that apply to one
  115. /// element: function, return type, or parameter.
  116. class AttributeSetNode : public FoldingSetNode {
  117. unsigned NumAttrs; ///< Number of attributes in this node.
  118. AttributeSetNode(ArrayRef<Attribute> Attrs) : NumAttrs(Attrs.size()) {
  119. // There's memory after the node where we can store the entries in.
  120. std::copy(Attrs.begin(), Attrs.end(),
  121. reinterpret_cast<Attribute *>(this + 1));
  122. }
  123. // AttributesSetNode is uniqued, these should not be publicly available.
  124. void operator=(const AttributeSetNode &) = delete;
  125. AttributeSetNode(const AttributeSetNode &) = delete;
  126. public:
  127. static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs);
  128. bool hasAttribute(Attribute::AttrKind Kind) const;
  129. bool hasAttribute(StringRef Kind) const;
  130. bool hasAttributes() const { return NumAttrs != 0; }
  131. Attribute getAttribute(Attribute::AttrKind Kind) const;
  132. Attribute getAttribute(StringRef Kind) const;
  133. unsigned getAlignment() const;
  134. unsigned getStackAlignment() const;
  135. uint64_t getDereferenceableBytes() const;
  136. uint64_t getDereferenceableOrNullBytes() const;
  137. std::string getAsString(bool InAttrGrp) const;
  138. typedef const Attribute *iterator;
  139. iterator begin() const { return reinterpret_cast<iterator>(this + 1); }
  140. iterator end() const { return begin() + NumAttrs; }
  141. void Profile(FoldingSetNodeID &ID) const {
  142. Profile(ID, makeArrayRef(begin(), end()));
  143. }
  144. static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) {
  145. for (unsigned I = 0, E = AttrList.size(); I != E; ++I)
  146. AttrList[I].Profile(ID);
  147. }
  148. };
  149. static_assert(
  150. AlignOf<AttributeSetNode>::Alignment >= AlignOf<Attribute>::Alignment,
  151. "Alignment is insufficient for objects appended to AttributeSetNode");
  152. // //
  153. ///////////////////////////////////////////////////////////////////////////////
  154. /// \class
  155. /// \brief This class represents a set of attributes that apply to the function,
  156. /// return type, and parameters.
  157. class AttributeSetImpl : public FoldingSetNode {
  158. friend class AttributeSet;
  159. public:
  160. typedef std::pair<unsigned, AttributeSetNode*> IndexAttrPair;
  161. private:
  162. LLVMContext &Context;
  163. unsigned NumAttrs; ///< Number of entries in this set.
  164. /// \brief Return a pointer to the IndexAttrPair for the specified slot.
  165. const IndexAttrPair *getNode(unsigned Slot) const {
  166. return reinterpret_cast<const IndexAttrPair *>(this + 1) + Slot;
  167. }
  168. // AttributesSet is uniqued, these should not be publicly available.
  169. void operator=(const AttributeSetImpl &) = delete;
  170. AttributeSetImpl(const AttributeSetImpl &) = delete;
  171. public:
  172. AttributeSetImpl(LLVMContext &C,
  173. ArrayRef<std::pair<unsigned, AttributeSetNode *> > Attrs)
  174. : Context(C), NumAttrs(Attrs.size()) {
  175. #ifndef NDEBUG
  176. if (Attrs.size() >= 2) {
  177. for (const std::pair<unsigned, AttributeSetNode *> *i = Attrs.begin() + 1,
  178. *e = Attrs.end();
  179. i != e; ++i) {
  180. assert((i-1)->first <= i->first && "Attribute set not ordered!");
  181. }
  182. }
  183. #endif
  184. // There's memory after the node where we can store the entries in.
  185. std::copy(Attrs.begin(), Attrs.end(),
  186. reinterpret_cast<IndexAttrPair *>(this + 1));
  187. }
  188. /// \brief Get the context that created this AttributeSetImpl.
  189. LLVMContext &getContext() { return Context; }
  190. /// \brief Return the number of attributes this AttributeSet contains.
  191. unsigned getNumAttributes() const { return NumAttrs; }
  192. /// \brief Get the index of the given "slot" in the AttrNodes list. This index
  193. /// is the index of the return, parameter, or function object that the
  194. /// attributes are applied to, not the index into the AttrNodes list where the
  195. /// attributes reside.
  196. unsigned getSlotIndex(unsigned Slot) const {
  197. return getNode(Slot)->first;
  198. }
  199. /// \brief Retrieve the attributes for the given "slot" in the AttrNode list.
  200. /// \p Slot is an index into the AttrNodes list, not the index of the return /
  201. /// parameter/ function which the attributes apply to.
  202. AttributeSet getSlotAttributes(unsigned Slot) const {
  203. return AttributeSet::get(Context, *getNode(Slot));
  204. }
  205. /// \brief Retrieve the attribute set node for the given "slot" in the
  206. /// AttrNode list.
  207. AttributeSetNode *getSlotNode(unsigned Slot) const {
  208. return getNode(Slot)->second;
  209. }
  210. typedef AttributeSetNode::iterator iterator;
  211. iterator begin(unsigned Slot) const { return getSlotNode(Slot)->begin(); }
  212. iterator end(unsigned Slot) const { return getSlotNode(Slot)->end(); }
  213. void Profile(FoldingSetNodeID &ID) const {
  214. Profile(ID, makeArrayRef(getNode(0), getNumAttributes()));
  215. }
  216. static void Profile(FoldingSetNodeID &ID,
  217. ArrayRef<std::pair<unsigned, AttributeSetNode*> > Nodes) {
  218. for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
  219. ID.AddInteger(Nodes[i].first);
  220. ID.AddPointer(Nodes[i].second);
  221. }
  222. }
  223. // FIXME: This atrocity is temporary.
  224. uint64_t Raw(unsigned Index) const;
  225. void dump() const;
  226. };
  227. static_assert(
  228. AlignOf<AttributeSetImpl>::Alignment >=
  229. AlignOf<AttributeSetImpl::IndexAttrPair>::Alignment,
  230. "Alignment is insufficient for objects appended to AttributeSetImpl");
  231. } // end llvm namespace
  232. #endif