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. 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. static_assert(std::is_trivially_destructible<AttributeImpl>::value,
  87. "AttributeImpl should be trivially destructible");
  88. //===----------------------------------------------------------------------===//
  89. /// \class
  90. /// A set of classes that contain the value of the
  91. /// attribute object. There are three main categories: enum attribute entries,
  92. /// represented by Attribute::AttrKind; alignment attribute entries; and string
  93. /// attribute enties, which are for target-dependent attributes.
  94. class EnumAttributeImpl : public AttributeImpl {
  95. Attribute::AttrKind Kind;
  96. protected:
  97. EnumAttributeImpl(AttrEntryKind ID, Attribute::AttrKind Kind)
  98. : AttributeImpl(ID), Kind(Kind) {}
  99. public:
  100. EnumAttributeImpl(Attribute::AttrKind Kind)
  101. : AttributeImpl(EnumAttrEntry), Kind(Kind) {
  102. assert(Kind != Attribute::AttrKind::None &&
  103. "Can't create a None attribute!");
  104. }
  105. Attribute::AttrKind getEnumKind() const { return Kind; }
  106. };
  107. class IntAttributeImpl : public EnumAttributeImpl {
  108. uint64_t Val;
  109. public:
  110. IntAttributeImpl(Attribute::AttrKind Kind, uint64_t Val)
  111. : EnumAttributeImpl(IntAttrEntry, Kind), Val(Val) {
  112. assert(Attribute::doesAttrKindHaveArgument(Kind) &&
  113. "Wrong kind for int attribute!");
  114. }
  115. uint64_t getValue() const { return Val; }
  116. };
  117. class StringAttributeImpl final
  118. : public AttributeImpl,
  119. private TrailingObjects<StringAttributeImpl, char> {
  120. friend TrailingObjects;
  121. unsigned KindSize;
  122. unsigned ValSize;
  123. size_t numTrailingObjects(OverloadToken<char>) const {
  124. return KindSize + 1 + ValSize + 1;
  125. }
  126. public:
  127. StringAttributeImpl(StringRef Kind, StringRef Val = StringRef())
  128. : AttributeImpl(StringAttrEntry), KindSize(Kind.size()),
  129. ValSize(Val.size()) {
  130. char *TrailingString = getTrailingObjects<char>();
  131. // Some users rely on zero-termination.
  132. llvm::copy(Kind, TrailingString);
  133. TrailingString[KindSize] = '\0';
  134. llvm::copy(Val, &TrailingString[KindSize + 1]);
  135. TrailingString[KindSize + 1 + ValSize] = '\0';
  136. }
  137. StringRef getStringKind() const {
  138. return StringRef(getTrailingObjects<char>(), KindSize);
  139. }
  140. StringRef getStringValue() const {
  141. return StringRef(getTrailingObjects<char>() + KindSize + 1, ValSize);
  142. }
  143. static size_t totalSizeToAlloc(StringRef Kind, StringRef Val) {
  144. return TrailingObjects::totalSizeToAlloc<char>(Kind.size() + 1 +
  145. Val.size() + 1);
  146. }
  147. };
  148. class TypeAttributeImpl : public EnumAttributeImpl {
  149. Type *Ty;
  150. public:
  151. TypeAttributeImpl(Attribute::AttrKind Kind, Type *Ty)
  152. : EnumAttributeImpl(TypeAttrEntry, Kind), Ty(Ty) {}
  153. Type *getTypeValue() const { return Ty; }
  154. };
  155. class AttributeBitSet {
  156. /// Bitset with a bit for each available attribute Attribute::AttrKind.
  157. uint8_t AvailableAttrs[12] = {};
  158. static_assert(Attribute::EndAttrKinds <= sizeof(AvailableAttrs) * CHAR_BIT,
  159. "Too many attributes");
  160. public:
  161. bool hasAttribute(Attribute::AttrKind Kind) const {
  162. return AvailableAttrs[Kind / 8] & (1 << (Kind % 8));
  163. }
  164. void addAttribute(Attribute::AttrKind Kind) {
  165. AvailableAttrs[Kind / 8] |= 1 << (Kind % 8);
  166. }
  167. };
  168. //===----------------------------------------------------------------------===//
  169. /// \class
  170. /// This class represents a group of attributes that apply to one
  171. /// element: function, return type, or parameter.
  172. class AttributeSetNode final
  173. : public FoldingSetNode,
  174. private TrailingObjects<AttributeSetNode, Attribute> {
  175. friend TrailingObjects;
  176. unsigned NumAttrs; ///< Number of attributes in this node.
  177. AttributeBitSet AvailableAttrs; ///< Available enum attributes.
  178. DenseMap<StringRef, Attribute> StringAttrs;
  179. AttributeSetNode(ArrayRef<Attribute> Attrs);
  180. static AttributeSetNode *getSorted(LLVMContext &C,
  181. ArrayRef<Attribute> SortedAttrs);
  182. Optional<Attribute> findEnumAttribute(Attribute::AttrKind Kind) const;
  183. public:
  184. // AttributesSetNode is uniqued, these should not be available.
  185. AttributeSetNode(const AttributeSetNode &) = delete;
  186. AttributeSetNode &operator=(const AttributeSetNode &) = delete;
  187. void operator delete(void *p) { ::operator delete(p); }
  188. static AttributeSetNode *get(LLVMContext &C, const AttrBuilder &B);
  189. static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs);
  190. /// Return the number of attributes this AttributeList contains.
  191. unsigned getNumAttributes() const { return NumAttrs; }
  192. bool hasAttribute(Attribute::AttrKind Kind) const {
  193. return AvailableAttrs.hasAttribute(Kind);
  194. }
  195. bool hasAttribute(StringRef Kind) const;
  196. bool hasAttributes() const { return NumAttrs != 0; }
  197. Attribute getAttribute(Attribute::AttrKind Kind) const;
  198. Attribute getAttribute(StringRef Kind) const;
  199. MaybeAlign getAlignment() const;
  200. MaybeAlign getStackAlignment() const;
  201. uint64_t getDereferenceableBytes() const;
  202. uint64_t getDereferenceableOrNullBytes() const;
  203. std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
  204. std::string getAsString(bool InAttrGrp) const;
  205. Type *getByValType() const;
  206. Type *getStructRetType() const;
  207. Type *getByRefType() const;
  208. Type *getPreallocatedType() 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