LLVM  14.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;
255  std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
256  unsigned getVScaleRangeMin() const;
258  std::string getAsString(bool InAttrGrp) const;
260 
261  using iterator = const Attribute *;
262 
263  iterator begin() const { return getTrailingObjects<Attribute>(); }
264  iterator end() const { return begin() + NumAttrs; }
265 
266  void Profile(FoldingSetNodeID &ID) const {
267  Profile(ID, makeArrayRef(begin(), end()));
268  }
269 
270  static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) {
271  for (const auto &Attr : AttrList)
272  Attr.Profile(ID);
273  }
274 };
275 
276 //===----------------------------------------------------------------------===//
277 /// \class
278 /// This class represents a set of attributes that apply to the function,
279 /// return type, and parameters.
280 class AttributeListImpl final
281  : public FoldingSetNode,
282  private TrailingObjects<AttributeListImpl, AttributeSet> {
283  friend class AttributeList;
284  friend TrailingObjects;
285 
286 private:
287  unsigned NumAttrSets; ///< Number of entries in this set.
288  /// Available enum function attributes.
289  AttributeBitSet AvailableFunctionAttrs;
290  /// Union of enum attributes available at any index.
291  AttributeBitSet AvailableSomewhereAttrs;
292 
293  // Helper fn for TrailingObjects class.
294  size_t numTrailingObjects(OverloadToken<AttributeSet>) { return NumAttrSets; }
295 
296 public:
298 
299  // AttributesSetImpt is uniqued, these should not be available.
300  AttributeListImpl(const AttributeListImpl &) = delete;
301  AttributeListImpl &operator=(const AttributeListImpl &) = delete;
302 
303  /// Return true if the AttributeSet or the FunctionIndex has an
304  /// enum attribute of the given kind.
306  return AvailableFunctionAttrs.hasAttribute(Kind);
307  }
308 
309  /// Return true if the specified attribute is set for at least one
310  /// parameter or for the return value. If Index is not nullptr, the index
311  /// of a parameter with the specified attribute is provided.
313  unsigned *Index = nullptr) const;
314 
315  using iterator = const AttributeSet *;
316 
317  iterator begin() const { return getTrailingObjects<AttributeSet>(); }
318  iterator end() const { return begin() + NumAttrSets; }
319 
320  void Profile(FoldingSetNodeID &ID) const;
321  static void Profile(FoldingSetNodeID &ID, ArrayRef<AttributeSet> Nodes);
322 
323  void dump() const;
324 };
325 
326 static_assert(std::is_trivially_destructible<AttributeListImpl>::value,
327  "AttributeListImpl should be trivially destructible");
328 
329 } // end namespace llvm
330 
331 #endif // LLVM_LIB_IR_ATTRIBUTEIMPL_H
llvm::Attribute::EndAttrKinds
@ EndAttrKinds
Sentinal value useful for loops.
Definition: Attributes.h:76
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
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
llvm::TypeAttributeImpl::getTypeValue
Type * getTypeValue() const
Definition: AttributeImpl.h:189
llvm::AttributeListImpl::end
iterator end() const
Definition: AttributeImpl.h:318
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::Attribute::isIntAttrKind
static bool isIntAttrKind(AttrKind Kind)
Definition: Attributes.h:87
llvm::AttributeImpl::operator=
AttributeImpl & operator=(const AttributeImpl &)=delete
llvm::AttributeImpl::TypeAttrEntry
@ TypeAttrEntry
Definition: AttributeImpl.h:47
llvm::TypeAttributeImpl::TypeAttributeImpl
TypeAttributeImpl(Attribute::AttrKind Kind, Type *Ty)
Definition: AttributeImpl.h:186
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
DenseMap.h
llvm::AttributeList
Definition: Attributes.h:403
llvm::copy
OutputIt copy(R &&Range, OutputIt Out)
Definition: STLExtras.h:1700
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:317
llvm::AttributeSetNode::getDereferenceableBytes
uint64_t getDereferenceableBytes() const
Definition: Attributes.cpp:870
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:305
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:852
llvm::AttributeListImpl::operator=
AttributeListImpl & operator=(const AttributeListImpl &)=delete
llvm::AttributeListImpl
Definition: AttributeImpl.h:280
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:555
llvm::IntAttributeImpl
Definition: AttributeImpl.h:133
llvm::AttributeImpl::hasAttribute
bool hasAttribute(Attribute::AttrKind A) const
Definition: Attributes.cpp:530
llvm::AttributeImpl::getKindAsEnum
Attribute::AttrKind getKindAsEnum() const
Definition: Attributes.cpp:540
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:263
llvm::StringRef::empty
constexpr LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:152
Index
uint32_t Index
Definition: ELFObjHandler.cpp:83
uint64_t
llvm::AttributeListImpl::dump
void dump() const
Definition: Attributes.cpp:969
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
llvm::DenseMap
Definition: DenseMap.h:714
llvm::AttributeSetNode::getAttributeType
Type * getAttributeType(Attribute::AttrKind Kind) const
Definition: Attributes.cpp:864
llvm::AttrBuilder
Definition: Attributes.h:997
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:266
ArrayRef.h
TemplateParamKind::Type
@ Type
llvm::AttributeImpl::getValueAsString
StringRef getValueAsString() const
Definition: Attributes.cpp:560
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::AttributeBitSet
Definition: AttributeImpl.h:192
llvm::AttributeSetNode::getDereferenceableOrNullBytes
uint64_t getDereferenceableOrNullBytes() const
Definition: Attributes.cpp:876
llvm::AttributeImpl::isTypeAttribute
bool isTypeAttribute() const
Definition: AttributeImpl.h:60
llvm::AttributeSetNode::getStackAlignment
MaybeAlign getStackAlignment() const
Definition: Attributes.cpp:858
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:1630
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:570
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:57
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:842
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:940
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::StringRef::size
constexpr LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:156
llvm::AttributeImpl::getValueAsBool
bool getValueAsBool() const
Definition: Attributes.cpp:550
llvm::AttributeListImpl::AttributeListImpl
AttributeListImpl(ArrayRef< AttributeSet > Sets)
Definition: Attributes.cpp:921
llvm::IntAttributeImpl::IntAttributeImpl
IntAttributeImpl(Attribute::AttrKind Kind, uint64_t Val)
Definition: AttributeImpl.h:137
llvm::EnumAttributeImpl
Definition: AttributeImpl.h:116
llvm::AttributeSetNode::Profile
static void Profile(FoldingSetNodeID &ID, ArrayRef< Attribute > AttrList)
Definition: AttributeImpl.h:270
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::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:264
llvm::makeArrayRef
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:474
llvm::AttributeSet
Definition: Attributes.h:270
llvm::AttributeSetNode::getAsString
std::string getAsString(bool InAttrGrp) const
Definition: Attributes.cpp:901
llvm::AttributeBitSet::addAttribute
void addAttribute(Attribute::AttrKind Kind)
Definition: AttributeImpl.h:203
llvm::AttributeSetNode::getVScaleRangeMax
Optional< unsigned > getVScaleRangeMax() const
Definition: Attributes.cpp:895
llvm::AttributeImpl::getValueAsType
Type * getValueAsType() const
Definition: Attributes.cpp:565
llvm::AttributeImpl::Profile
void Profile(FoldingSetNodeID &ID) const
Definition: AttributeImpl.h:77
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:950
llvm::AttributeImpl::isStringAttribute
bool isStringAttribute() const
Definition: AttributeImpl.h:59
llvm::AttributeImpl::getValueAsInt
uint64_t getValueAsInt() const
Definition: Attributes.cpp:545
llvm::AttributeSetNode::get
static AttributeSetNode * get(LLVMContext &C, const AttrBuilder &B)
Definition: Attributes.cpp:817
llvm::AttributeSetNode::getVScaleRangeMin
unsigned getVScaleRangeMin() const
Definition: Attributes.cpp:889
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:883