LLVM  13.0.0git
AttributeImpl.h
Go to the documentation of this file.
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 
15 #ifndef LLVM_LIB_IR_ATTRIBUTEIMPL_H
16 #define LLVM_LIB_IR_ATTRIBUTEIMPL_H
17 
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/FoldingSet.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/IR/Attributes.h"
24 #include <cassert>
25 #include <cstddef>
26 #include <cstdint>
27 #include <string>
28 #include <utility>
29 
30 namespace llvm {
31 
32 class LLVMContext;
33 class Type;
34 
35 //===----------------------------------------------------------------------===//
36 /// \class
37 /// This class represents a single, uniqued attribute. That attribute
38 /// could be a single enum, a tuple, or a string.
39 class AttributeImpl : public FoldingSetNode {
40  unsigned char KindID; ///< Holds the AttrEntryKind of the attribute
41 
42 protected:
48  };
49 
50  AttributeImpl(AttrEntryKind KindID) : KindID(KindID) {}
51 
52 public:
53  // AttributesImpl is uniqued, these should not be available.
54  AttributeImpl(const AttributeImpl &) = delete;
55  AttributeImpl &operator=(const AttributeImpl &) = delete;
56 
57  bool isEnumAttribute() const { return KindID == EnumAttrEntry; }
58  bool isIntAttribute() const { return KindID == IntAttrEntry; }
59  bool isStringAttribute() const { return KindID == StringAttrEntry; }
60  bool isTypeAttribute() const { return KindID == TypeAttrEntry; }
61 
62  bool hasAttribute(Attribute::AttrKind A) const;
63  bool hasAttribute(StringRef Kind) const;
64 
66  uint64_t getValueAsInt() const;
67  bool getValueAsBool() const;
68 
69  StringRef getKindAsString() const;
71 
72  Type *getValueAsType() const;
73 
74  /// Used when sorting the attributes.
75  bool operator<(const AttributeImpl &AI) const;
76 
77  void Profile(FoldingSetNodeID &ID) const {
78  if (isEnumAttribute())
79  Profile(ID, getKindAsEnum(), static_cast<uint64_t>(0));
80  else if (isIntAttribute())
82  else if (isStringAttribute())
84  else
86  }
87 
89  uint64_t Val) {
90  ID.AddInteger(Kind);
91  if (Val) ID.AddInteger(Val);
92  }
93 
95  ID.AddString(Kind);
96  if (!Values.empty()) ID.AddString(Values);
97  }
98 
100  Type *Ty) {
101  ID.AddInteger(Kind);
102  ID.AddPointer(Ty);
103  }
104 };
105 
106 static_assert(std::is_trivially_destructible<AttributeImpl>::value,
107  "AttributeImpl should be trivially destructible");
108 
109 //===----------------------------------------------------------------------===//
110 /// \class
111 /// A set of classes that contain the value of the
112 /// attribute object. There are three main categories: enum attribute entries,
113 /// represented by Attribute::AttrKind; alignment attribute entries; and string
114 /// attribute enties, which are for target-dependent attributes.
115 
117  Attribute::AttrKind Kind;
118 
119 protected:
121  : AttributeImpl(ID), Kind(Kind) {}
122 
123 public:
125  : AttributeImpl(EnumAttrEntry), Kind(Kind) {
127  "Can't create a None attribute!");
128  }
129 
130  Attribute::AttrKind getEnumKind() const { return Kind; }
131 };
132 
134  uint64_t Val;
135 
136 public:
138  : EnumAttributeImpl(IntAttrEntry, Kind), Val(Val) {
140  "Wrong kind for int attribute!");
141  }
142 
143  uint64_t getValue() const { return Val; }
144 };
145 
147  : public AttributeImpl,
148  private TrailingObjects<StringAttributeImpl, char> {
149  friend TrailingObjects;
150 
151  unsigned KindSize;
152  unsigned ValSize;
153  size_t numTrailingObjects(OverloadToken<char>) const {
154  return KindSize + 1 + ValSize + 1;
155  }
156 
157 public:
159  : AttributeImpl(StringAttrEntry), KindSize(Kind.size()),
160  ValSize(Val.size()) {
161  char *TrailingString = getTrailingObjects<char>();
162  // Some users rely on zero-termination.
163  llvm::copy(Kind, TrailingString);
164  TrailingString[KindSize] = '\0';
165  llvm::copy(Val, &TrailingString[KindSize + 1]);
166  TrailingString[KindSize + 1 + ValSize] = '\0';
167  }
168 
170  return StringRef(getTrailingObjects<char>(), KindSize);
171  }
173  return StringRef(getTrailingObjects<char>() + KindSize + 1, ValSize);
174  }
175 
176  static size_t totalSizeToAlloc(StringRef Kind, StringRef Val) {
177  return TrailingObjects::totalSizeToAlloc<char>(Kind.size() + 1 +
178  Val.size() + 1);
179  }
180 };
181 
183  Type *Ty;
184 
185 public:
187  : EnumAttributeImpl(TypeAttrEntry, Kind), Ty(Ty) {}
188 
189  Type *getTypeValue() const { return Ty; }
190 };
191 
193  /// Bitset with a bit for each available attribute Attribute::AttrKind.
194  uint8_t AvailableAttrs[12] = {};
195  static_assert(Attribute::EndAttrKinds <= sizeof(AvailableAttrs) * CHAR_BIT,
196  "Too many attributes");
197 
198 public:
200  return AvailableAttrs[Kind / 8] & (1 << (Kind % 8));
201  }
202 
204  AvailableAttrs[Kind / 8] |= 1 << (Kind % 8);
205  }
206 };
207 
208 //===----------------------------------------------------------------------===//
209 /// \class
210 /// This class represents a group of attributes that apply to one
211 /// element: function, return type, or parameter.
212 class AttributeSetNode final
213  : public FoldingSetNode,
214  private TrailingObjects<AttributeSetNode, Attribute> {
215  friend TrailingObjects;
216 
217  unsigned NumAttrs; ///< Number of attributes in this node.
218  AttributeBitSet AvailableAttrs; ///< Available enum attributes.
219 
220  DenseMap<StringRef, Attribute> StringAttrs;
221 
223 
224  static AttributeSetNode *getSorted(LLVMContext &C,
225  ArrayRef<Attribute> SortedAttrs);
226  Optional<Attribute> findEnumAttribute(Attribute::AttrKind Kind) const;
227 
228 public:
229  // AttributesSetNode is uniqued, these should not be available.
230  AttributeSetNode(const AttributeSetNode &) = delete;
231  AttributeSetNode &operator=(const AttributeSetNode &) = delete;
232 
233  void operator delete(void *p) { ::operator delete(p); }
234 
235  static AttributeSetNode *get(LLVMContext &C, const AttrBuilder &B);
236 
238 
239  /// Return the number of attributes this AttributeList contains.
240  unsigned getNumAttributes() const { return NumAttrs; }
241 
243  return AvailableAttrs.hasAttribute(Kind);
244  }
245  bool hasAttribute(StringRef Kind) const;
246  bool hasAttributes() const { return NumAttrs != 0; }
247 
250 
251  MaybeAlign getAlignment() const;
253  uint64_t getDereferenceableBytes() const;
254  uint64_t getDereferenceableOrNullBytes() const;
255  std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
256  std::pair<unsigned, unsigned> getVScaleRangeArgs() const;
257  std::string getAsString(bool InAttrGrp) const;
258  Type *getByValType() const;
259  Type *getStructRetType() const;
260  Type *getByRefType() const;
261  Type *getPreallocatedType() const;
262  Type *getInAllocaType() const;
263 
264  using iterator = const Attribute *;
265 
266  iterator begin() const { return getTrailingObjects<Attribute>(); }
267  iterator end() const { return begin() + NumAttrs; }
268 
269  void Profile(FoldingSetNodeID &ID) const {
270  Profile(ID, makeArrayRef(begin(), end()));
271  }
272 
273  static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) {
274  for (const auto &Attr : AttrList)
275  Attr.Profile(ID);
276  }
277 };
278 
279 //===----------------------------------------------------------------------===//
280 /// \class
281 /// This class represents a set of attributes that apply to the function,
282 /// return type, and parameters.
283 class AttributeListImpl final
284  : public FoldingSetNode,
285  private TrailingObjects<AttributeListImpl, AttributeSet> {
286  friend class AttributeList;
287  friend TrailingObjects;
288 
289 private:
290  unsigned NumAttrSets; ///< Number of entries in this set.
291  /// Available enum function attributes.
292  AttributeBitSet AvailableFunctionAttrs;
293  /// Union of enum attributes available at any index.
294  AttributeBitSet AvailableSomewhereAttrs;
295 
296  // Helper fn for TrailingObjects class.
297  size_t numTrailingObjects(OverloadToken<AttributeSet>) { return NumAttrSets; }
298 
299 public:
301 
302  // AttributesSetImpt is uniqued, these should not be available.
303  AttributeListImpl(const AttributeListImpl &) = delete;
304  AttributeListImpl &operator=(const AttributeListImpl &) = delete;
305 
306  /// Return true if the AttributeSet or the FunctionIndex has an
307  /// enum attribute of the given kind.
309  return AvailableFunctionAttrs.hasAttribute(Kind);
310  }
311 
312  /// Return true if the specified attribute is set for at least one
313  /// parameter or for the return value. If Index is not nullptr, the index
314  /// of a parameter with the specified attribute is provided.
316  unsigned *Index = nullptr) const;
317 
318  using iterator = const AttributeSet *;
319 
320  iterator begin() const { return getTrailingObjects<AttributeSet>(); }
321  iterator end() const { return begin() + NumAttrSets; }
322 
323  void Profile(FoldingSetNodeID &ID) const;
324  static void Profile(FoldingSetNodeID &ID, ArrayRef<AttributeSet> Nodes);
325 
326  void dump() const;
327 };
328 
329 static_assert(std::is_trivially_destructible<AttributeListImpl>::value,
330  "AttributeListImpl should be trivially destructible");
331 
332 } // end namespace llvm
333 
334 #endif // LLVM_LIB_IR_ATTRIBUTEIMPL_H
llvm::Attribute::EndAttrKinds
@ EndAttrKinds
Sentinal value useful for loops.
Definition: Attributes.h:77
llvm::EnumAttributeImpl::EnumAttributeImpl
EnumAttributeImpl(Attribute::AttrKind Kind)
Definition: AttributeImpl.h:124
llvm::AttributeImpl
Definition: AttributeImpl.h:39
TrailingObjects.h
Attrs
Function Attrs
Definition: README_ALTIVEC.txt:215
llvm
Definition: AllocatorList.h:23
llvm::StringRef::empty
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:153
llvm::TypeAttributeImpl::getTypeValue
Type * getTypeValue() const
Definition: AttributeImpl.h:189
llvm::AttributeListImpl::end
iterator end() const
Definition: AttributeImpl.h:321
llvm::Attribute
Definition: Attributes.h:52
StringRef.h
llvm::AttributeSetNode::getNumAttributes
unsigned getNumAttributes() const
Return the number of attributes this AttributeList contains.
Definition: AttributeImpl.h:240
llvm::AttributeImpl::Profile
static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind, uint64_t Val)
Definition: AttributeImpl.h:88
llvm::AttributeImpl::operator=
AttributeImpl & operator=(const AttributeImpl &)=delete
llvm::AttributeSetNode::getVScaleRangeArgs
std::pair< unsigned, unsigned > getVScaleRangeArgs() const
Definition: Attributes.cpp:1100
llvm::AttributeImpl::TypeAttrEntry
@ TypeAttrEntry
Definition: AttributeImpl.h:47
llvm::TypeAttributeImpl::TypeAttributeImpl
TypeAttributeImpl(Attribute::AttrKind Kind, Type *Ty)
Definition: AttributeImpl.h:186
llvm::AttributeSetNode::getStructRetType
Type * getStructRetType() const
Definition: Attributes.cpp:1057
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:46
DenseMap.h
llvm::AttributeList
Definition: Attributes.h:385
llvm::copy
OutputIt copy(R &&Range, OutputIt Out)
Definition: STLExtras.h:1533
llvm::TypeAttributeImpl
Definition: AttributeImpl.h:182
llvm::Optional
Definition: APInt.h:33
llvm::AttributeBitSet::hasAttribute
bool hasAttribute(Attribute::AttrKind Kind) const
Definition: AttributeImpl.h:199
llvm::AttributeListImpl::begin
iterator begin() const
Definition: AttributeImpl.h:320
llvm::AttributeSetNode::getDereferenceableBytes
uint64_t getDereferenceableBytes() const
Definition: Attributes.cpp:1081
llvm::AttributeImpl::AttributeImpl
AttributeImpl(AttrEntryKind KindID)
Definition: AttributeImpl.h:50
llvm::AttributeImpl::Profile
static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind, Type *Ty)
Definition: AttributeImpl.h:99
llvm::AttributeSetNode::operator=
AttributeSetNode & operator=(const AttributeSetNode &)=delete
llvm::AttributeListImpl::hasFnAttribute
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the AttributeSet or the FunctionIndex has an enum attribute of the given kind.
Definition: AttributeImpl.h:308
p
the resulting code requires compare and branches when and if * p
Definition: README.txt:396
llvm::AttributeSetNode::getAlignment
MaybeAlign getAlignment() const
Definition: Attributes.cpp:1039
llvm::AttributeListImpl::operator=
AttributeListImpl & operator=(const AttributeListImpl &)=delete
llvm::AttributeListImpl
Definition: AttributeImpl.h:283
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::AttributeImpl::isEnumAttribute
bool isEnumAttribute() const
Definition: AttributeImpl.h:57
llvm::AttributeImpl::getKindAsString
StringRef getKindAsString() const
Definition: Attributes.cpp:669
llvm::IntAttributeImpl
Definition: AttributeImpl.h:133
llvm::AttributeImpl::hasAttribute
bool hasAttribute(Attribute::AttrKind A) const
Definition: Attributes.cpp:644
llvm::AttributeImpl::getKindAsEnum
Attribute::AttrKind getKindAsEnum() const
Definition: Attributes.cpp:654
llvm::AttributeImpl::isIntAttribute
bool isIntAttribute() const
Definition: AttributeImpl.h:58
llvm::MaybeAlign
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition: Alignment.h:109
B
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
llvm::AttributeImpl::EnumAttrEntry
@ EnumAttrEntry
Definition: AttributeImpl.h:44
llvm::AttributeSetNode::hasAttribute
bool hasAttribute(Attribute::AttrKind Kind) const
Definition: AttributeImpl.h:242
llvm::StringAttributeImpl::getStringKind
StringRef getStringKind() const
Definition: AttributeImpl.h:169
llvm::None
const NoneType None
Definition: None.h:23
llvm::lltok::Kind
Kind
Definition: LLToken.h:18
llvm::AttributeSetNode::begin
iterator begin() const
Definition: AttributeImpl.h:266
Index
uint32_t Index
Definition: ELFObjHandler.cpp:84
llvm::AttributeListImpl::dump
void dump() const
Definition: Attributes.cpp:1174
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
llvm::DenseMap
Definition: DenseMap.h:714
llvm::AttrBuilder
Definition: Attributes.h:804
llvm::Attribute::AttrKind
AttrKind
This enumeration lists the attributes that can be associated with parameters, function results,...
Definition: Attributes.h:71
llvm::AttributeSetNode::Profile
void Profile(FoldingSetNodeID &ID) const
Definition: AttributeImpl.h:269
ArrayRef.h
TemplateParamKind::Type
@ Type
llvm::Attribute::doesAttrKindHaveArgument
static bool doesAttrKindHaveArgument(Attribute::AttrKind AttrKind)
Return true if and only if the attribute has an Argument.
Definition: Attributes.cpp:239
llvm::AttributeImpl::getValueAsString
StringRef getValueAsString() const
Definition: Attributes.cpp:674
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::AttributeBitSet
Definition: AttributeImpl.h:192
llvm::AttributeSetNode::getDereferenceableOrNullBytes
uint64_t getDereferenceableOrNullBytes() const
Definition: Attributes.cpp:1087
llvm::AttributeImpl::isTypeAttribute
bool isTypeAttribute() const
Definition: AttributeImpl.h:60
llvm::AttributeSetNode::getByValType
Type * getByValType() const
Definition: Attributes.cpp:1051
llvm::AttributeSetNode::getStackAlignment
MaybeAlign getStackAlignment() const
Definition: Attributes.cpp:1045
llvm::StringAttributeImpl
Definition: AttributeImpl.h:146
llvm::AttributeImpl::Profile
static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values)
Definition: AttributeImpl.h:94
llvm::size
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition: STLExtras.h:1463
llvm::AttributeImpl::StringAttrEntry
@ StringAttrEntry
Definition: AttributeImpl.h:46
llvm::AttributeImpl::operator<
bool operator<(const AttributeImpl &AI) const
Used when sorting the attributes.
Definition: Attributes.cpp:684
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::FoldingSetBase::Node
Node - This class is used to maintain the singly linked bucket list in a folding set.
Definition: FoldingSet.h:133
llvm::EnumAttributeImpl::getEnumKind
Attribute::AttrKind getEnumKind() const
Definition: AttributeImpl.h:130
llvm::AttributeSetNode::getAttribute
Attribute getAttribute(Attribute::AttrKind Kind) const
Definition: Attributes.cpp:1029
llvm::FoldingSetNodeID
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
Definition: FoldingSet.h:313
llvm::AttributeSetNode
Definition: AttributeImpl.h:212
llvm::AttributeListImpl::Profile
void Profile(FoldingSetNodeID &ID) const
Definition: Attributes.cpp:1145
llvm::AttributeImpl::AttrEntryKind
AttrEntryKind
Definition: AttributeImpl.h:43
llvm::StringAttributeImpl::totalSizeToAlloc
static size_t totalSizeToAlloc(StringRef Kind, StringRef Val)
Definition: AttributeImpl.h:176
llvm::IntAttributeImpl::getValue
uint64_t getValue() const
Definition: AttributeImpl.h:143
FoldingSet.h
Attributes.h
llvm::AttributeImpl::getValueAsBool
bool getValueAsBool() const
Definition: Attributes.cpp:664
llvm::AttributeListImpl::AttributeListImpl
AttributeListImpl(ArrayRef< AttributeSet > Sets)
Definition: Attributes.cpp:1126
llvm::IntAttributeImpl::IntAttributeImpl
IntAttributeImpl(Attribute::AttrKind Kind, uint64_t Val)
Definition: AttributeImpl.h:137
llvm::EnumAttributeImpl
Definition: AttributeImpl.h:116
llvm::AttributeSetNode::getPreallocatedType
Type * getPreallocatedType() const
Definition: Attributes.cpp:1069
llvm::AttributeSetNode::Profile
static void Profile(FoldingSetNodeID &ID, ArrayRef< Attribute > AttrList)
Definition: AttributeImpl.h:273
llvm::StringAttributeImpl::getStringValue
StringRef getStringValue() const
Definition: AttributeImpl.h:172
llvm::TrailingObjects
See the file comment for details on the usage of the TrailingObjects type.
Definition: TrailingObjects.h:212
llvm::AttributeSetNode::hasAttributes
bool hasAttributes() const
Definition: AttributeImpl.h:246
llvm::AttributeSetNode::getInAllocaType
Type * getInAllocaType() const
Definition: Attributes.cpp:1075
llvm::StringAttributeImpl::StringAttributeImpl
StringAttributeImpl(StringRef Kind, StringRef Val=StringRef())
Definition: AttributeImpl.h:158
llvm::AttributeImpl::IntAttrEntry
@ IntAttrEntry
Definition: AttributeImpl.h:45
llvm::AttributeSetNode::end
iterator end() const
Definition: AttributeImpl.h:267
llvm::makeArrayRef
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:476
llvm::AttributeSet
Definition: Attributes.h:254
llvm::AttributeSetNode::getAsString
std::string getAsString(bool InAttrGrp) const
Definition: Attributes.cpp:1106
llvm::AttributeBitSet::addAttribute
void addAttribute(Attribute::AttrKind Kind)
Definition: AttributeImpl.h:203
llvm::AttributeImpl::getValueAsType
Type * getValueAsType() const
Definition: Attributes.cpp:679
llvm::AttributeSetNode::getByRefType
Type * getByRefType() const
Definition: Attributes.cpp:1063
llvm::AttributeImpl::Profile
void Profile(FoldingSetNodeID &ID) const
Definition: AttributeImpl.h:77
llvm::StringRef::size
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:157
llvm::AttributeListImpl::hasAttrSomewhere
bool hasAttrSomewhere(Attribute::AttrKind Kind, unsigned *Index=nullptr) const
Return true if the specified attribute is set for at least one parameter or for the return value.
Definition: Attributes.cpp:1155
llvm::AttributeImpl::isStringAttribute
bool isStringAttribute() const
Definition: AttributeImpl.h:59
llvm::AttributeImpl::getValueAsInt
uint64_t getValueAsInt() const
Definition: Attributes.cpp:659
llvm::AttributeSetNode::get
static AttributeSetNode * get(LLVMContext &C, const AttrBuilder &B)
Definition: Attributes.cpp:944
llvm::EnumAttributeImpl::EnumAttributeImpl
EnumAttributeImpl(AttrEntryKind ID, Attribute::AttrKind Kind)
Definition: AttributeImpl.h:120
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:38
llvm::AttributeSetNode::getAllocSizeArgs
std::pair< unsigned, Optional< unsigned > > getAllocSizeArgs() const
Definition: Attributes.cpp:1094