AttributeImpl.h 11 KB

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