LLVM 20.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"
26#include <cassert>
27#include <cstddef>
28#include <cstdint>
29#include <optional>
30#include <string>
31#include <utility>
32
33namespace llvm {
34
35class LLVMContext;
36class Type;
37
38//===----------------------------------------------------------------------===//
39/// \class
40/// This class represents a single, uniqued attribute. That attribute
41/// could be a single enum, a tuple, or a string.
43 unsigned char KindID; ///< Holds the AttrEntryKind of the attribute
44
45protected:
53 };
54
55 AttributeImpl(AttrEntryKind KindID) : KindID(KindID) {}
56
57public:
58 // AttributesImpl is uniqued, these should not be available.
59 AttributeImpl(const AttributeImpl &) = delete;
61
62 bool isEnumAttribute() const { return KindID == EnumAttrEntry; }
63 bool isIntAttribute() const { return KindID == IntAttrEntry; }
64 bool isStringAttribute() const { return KindID == StringAttrEntry; }
65 bool isTypeAttribute() const { return KindID == TypeAttrEntry; }
67 return KindID == ConstantRangeAttrEntry;
68 }
70 return KindID == ConstantRangeListAttrEntry;
71 }
72
74 bool hasAttribute(StringRef Kind) const;
75
77 uint64_t getValueAsInt() const;
78 bool getValueAsBool() const;
79
82
83 Type *getValueAsType() const;
84
86
88
89 /// Used to sort attributes. KindOnly controls if the sort includes the
90 /// attributes' values or just the kind.
91 int cmp(const AttributeImpl &AI, bool KindOnly) const;
92 /// Used when sorting the attributes.
93 bool operator<(const AttributeImpl &AI) const;
94
96 if (isEnumAttribute())
98 else if (isIntAttribute())
100 else if (isStringAttribute())
102 else if (isTypeAttribute())
104 else if (isConstantRangeAttribute())
106 else
108 }
109
111 assert(Attribute::isEnumAttrKind(Kind) && "Expected enum attribute");
112 ID.AddInteger(Kind);
113 }
114
116 uint64_t Val) {
117 assert(Attribute::isIntAttrKind(Kind) && "Expected int attribute");
118 ID.AddInteger(Kind);
119 ID.AddInteger(Val);
120 }
121
122 static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values) {
123 ID.AddString(Kind);
124 if (!Values.empty()) ID.AddString(Values);
125 }
126
128 Type *Ty) {
129 ID.AddInteger(Kind);
130 ID.AddPointer(Ty);
131 }
132
134 const ConstantRange &CR) {
135 ID.AddInteger(Kind);
136 CR.getLower().Profile(ID);
137 CR.getUpper().Profile(ID);
138 }
139
142 ID.AddInteger(Kind);
143 ID.AddInteger(Val.size());
144 for (auto &CR : Val) {
145 CR.getLower().Profile(ID);
146 CR.getUpper().Profile(ID);
147 }
148 }
149};
150
151static_assert(std::is_trivially_destructible<AttributeImpl>::value,
152 "AttributeImpl should be trivially destructible");
153
154//===----------------------------------------------------------------------===//
155/// \class
156/// A set of classes that contain the value of the
157/// attribute object. There are three main categories: enum attribute entries,
158/// represented by Attribute::AttrKind; alignment attribute entries; and string
159/// attribute enties, which are for target-dependent attributes.
160
163
164protected:
166 : AttributeImpl(ID), Kind(Kind) {}
167
168public:
170 : AttributeImpl(EnumAttrEntry), Kind(Kind) {
172 "Can't create a None attribute!");
173 }
174
175 Attribute::AttrKind getEnumKind() const { return Kind; }
176};
177
179 uint64_t Val;
180
181public:
183 : EnumAttributeImpl(IntAttrEntry, Kind), Val(Val) {
185 "Wrong kind for int attribute!");
186 }
187
188 uint64_t getValue() const { return Val; }
189};
190
192 : public AttributeImpl,
193 private TrailingObjects<StringAttributeImpl, char> {
194 friend TrailingObjects;
195
196 unsigned KindSize;
197 unsigned ValSize;
198 size_t numTrailingObjects(OverloadToken<char>) const {
199 return KindSize + 1 + ValSize + 1;
200 }
201
202public:
204 : AttributeImpl(StringAttrEntry), KindSize(Kind.size()),
205 ValSize(Val.size()) {
206 char *TrailingString = getTrailingObjects<char>();
207 // Some users rely on zero-termination.
208 llvm::copy(Kind, TrailingString);
209 TrailingString[KindSize] = '\0';
210 llvm::copy(Val, &TrailingString[KindSize + 1]);
211 TrailingString[KindSize + 1 + ValSize] = '\0';
212 }
213
215 return StringRef(getTrailingObjects<char>(), KindSize);
216 }
218 return StringRef(getTrailingObjects<char>() + KindSize + 1, ValSize);
219 }
220
221 static size_t totalSizeToAlloc(StringRef Kind, StringRef Val) {
222 return TrailingObjects::totalSizeToAlloc<char>(Kind.size() + 1 +
223 Val.size() + 1);
224 }
225};
226
228 Type *Ty;
229
230public:
232 : EnumAttributeImpl(TypeAttrEntry, Kind), Ty(Ty) {}
233
234 Type *getTypeValue() const { return Ty; }
235};
236
238 ConstantRange CR;
239
240public:
242 : EnumAttributeImpl(ConstantRangeAttrEntry, Kind), CR(CR) {}
243
244 const ConstantRange &getConstantRangeValue() const { return CR; }
245};
246
248 : public EnumAttributeImpl,
249 private TrailingObjects<ConstantRangeListAttributeImpl, ConstantRange> {
250 friend TrailingObjects;
251
252 unsigned Size;
253 size_t numTrailingObjects(OverloadToken<ConstantRange>) const { return Size; }
254
255public:
259 assert(Size > 0);
260 ConstantRange *TrailingCR = getTrailingObjects<ConstantRange>();
261 std::uninitialized_copy(Val.begin(), Val.end(), TrailingCR);
262 }
263
265 ConstantRange *TrailingCR = getTrailingObjects<ConstantRange>();
266 for (unsigned I = 0; I != Size; ++I)
267 TrailingCR[I].~ConstantRange();
268 }
269
271 return ArrayRef(getTrailingObjects<ConstantRange>(), Size);
272 }
273
275 return TrailingObjects::totalSizeToAlloc<ConstantRange>(Val.size());
276 }
277};
278
280 /// Bitset with a bit for each available attribute Attribute::AttrKind.
281 uint8_t AvailableAttrs[16] = {};
282 static_assert(Attribute::EndAttrKinds <= sizeof(AvailableAttrs) * CHAR_BIT,
283 "Too many attributes");
284
285public:
287 return AvailableAttrs[Kind / 8] & (1 << (Kind % 8));
288 }
289
291 AvailableAttrs[Kind / 8] |= 1 << (Kind % 8);
292 }
293};
294
295//===----------------------------------------------------------------------===//
296/// \class
297/// This class represents a group of attributes that apply to one
298/// element: function, return type, or parameter.
300 : public FoldingSetNode,
301 private TrailingObjects<AttributeSetNode, Attribute> {
302 friend TrailingObjects;
303
304 unsigned NumAttrs; ///< Number of attributes in this node.
305 AttributeBitSet AvailableAttrs; ///< Available enum attributes.
306
308
310
311 static AttributeSetNode *getSorted(LLVMContext &C,
312 ArrayRef<Attribute> SortedAttrs);
313 std::optional<Attribute> findEnumAttribute(Attribute::AttrKind Kind) const;
314
315public:
316 // AttributesSetNode is uniqued, these should not be available.
319
320 void operator delete(void *p) { ::operator delete(p); }
321
322 static AttributeSetNode *get(LLVMContext &C, const AttrBuilder &B);
323
325
326 /// Return the number of attributes this AttributeList contains.
327 unsigned getNumAttributes() const { return NumAttrs; }
328
330 return AvailableAttrs.hasAttribute(Kind);
331 }
332 bool hasAttribute(StringRef Kind) const;
333 bool hasAttributes() const { return NumAttrs != 0; }
334
336 Attribute getAttribute(StringRef Kind) const;
337
338 MaybeAlign getAlignment() const;
342 std::optional<std::pair<unsigned, std::optional<unsigned>>> getAllocSizeArgs()
343 const;
344 unsigned getVScaleRangeMin() const;
345 std::optional<unsigned> getVScaleRangeMax() const;
350 std::string getAsString(bool InAttrGrp) const;
352
353 using iterator = const Attribute *;
354
355 iterator begin() const { return getTrailingObjects<Attribute>(); }
356 iterator end() const { return begin() + NumAttrs; }
357
359 Profile(ID, ArrayRef(begin(), end()));
360 }
361
363 for (const auto &Attr : AttrList)
364 Attr.Profile(ID);
365 }
366};
367
368//===----------------------------------------------------------------------===//
369/// \class
370/// This class represents a set of attributes that apply to the function,
371/// return type, and parameters.
373 : public FoldingSetNode,
374 private TrailingObjects<AttributeListImpl, AttributeSet> {
375 friend class AttributeList;
376 friend TrailingObjects;
377
378private:
379 unsigned NumAttrSets; ///< Number of entries in this set.
380 /// Available enum function attributes.
381 AttributeBitSet AvailableFunctionAttrs;
382 /// Union of enum attributes available at any index.
383 AttributeBitSet AvailableSomewhereAttrs;
384
385 // Helper fn for TrailingObjects class.
386 size_t numTrailingObjects(OverloadToken<AttributeSet>) { return NumAttrSets; }
387
388public:
390
391 // AttributesSetImpt is uniqued, these should not be available.
394
395 /// Return true if the AttributeSet or the FunctionIndex has an
396 /// enum attribute of the given kind.
398 return AvailableFunctionAttrs.hasAttribute(Kind);
399 }
400
401 /// Return true if the specified attribute is set for at least one
402 /// parameter or for the return value. If Index is not nullptr, the index
403 /// of a parameter with the specified attribute is provided.
405 unsigned *Index = nullptr) const;
406
407 using iterator = const AttributeSet *;
408
409 iterator begin() const { return getTrailingObjects<AttributeSet>(); }
410 iterator end() const { return begin() + NumAttrSets; }
411
412 void Profile(FoldingSetNodeID &ID) const;
414
415 void dump() const;
416};
417
418static_assert(std::is_trivially_destructible<AttributeListImpl>::value,
419 "AttributeListImpl should be trivially destructible");
420
421} // end namespace llvm
422
423#endif // LLVM_LIB_IR_ATTRIBUTEIMPL_H
This file contains the simple types necessary to represent the attributes associated with functions a...
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
RelocType Type
Definition: COFFYAML.cpp:410
This file defines the DenseMap class.
uint32_t Index
This file defines a hash set that can be used to remove duplication of nodes in a graph.
#define I(x, y, z)
Definition: MD5.cpp:58
Load MIR Sample Profile
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This header defines support for implementing classes that have some trailing object (or arrays of obj...
void Profile(FoldingSetNodeID &id) const
Used to insert APInt objects, or objects that contain APInt objects, into FoldingSets.
Definition: APInt.cpp:156
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
iterator end() const
Definition: ArrayRef.h:157
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:168
iterator begin() const
Definition: ArrayRef.h:156
void addAttribute(Attribute::AttrKind Kind)
bool hasAttribute(Attribute::AttrKind Kind) const
int cmp(const AttributeImpl &AI, bool KindOnly) const
Used to sort attributes.
Definition: Attributes.cpp:854
bool isConstantRangeAttribute() const
Definition: AttributeImpl.h:66
bool hasAttribute(Attribute::AttrKind A) const
Definition: Attributes.cpp:801
void Profile(FoldingSetNodeID &ID) const
Definition: AttributeImpl.h:95
Type * getValueAsType() const
Definition: Attributes.cpp:837
Attribute::AttrKind getKindAsEnum() const
Definition: Attributes.cpp:811
static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values)
bool operator<(const AttributeImpl &AI) const
Used when sorting the attributes.
Definition: Attributes.cpp:889
static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind, uint64_t Val)
AttributeImpl & operator=(const AttributeImpl &)=delete
static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind)
uint64_t getValueAsInt() const
Definition: Attributes.cpp:817
bool isIntAttribute() const
Definition: AttributeImpl.h:63
bool isTypeAttribute() const
Definition: AttributeImpl.h:65
static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind, ArrayRef< ConstantRange > Val)
AttributeImpl(AttrEntryKind KindID)
Definition: AttributeImpl.h:55
AttributeImpl(const AttributeImpl &)=delete
static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind, Type *Ty)
bool getValueAsBool() const
Definition: Attributes.cpp:822
StringRef getKindAsString() const
Definition: Attributes.cpp:827
StringRef getValueAsString() const
Definition: Attributes.cpp:832
bool isEnumAttribute() const
Definition: AttributeImpl.h:62
ArrayRef< ConstantRange > getValueAsConstantRangeList() const
Definition: Attributes.cpp:848
bool isConstantRangeListAttribute() const
Definition: AttributeImpl.h:69
bool isStringAttribute() const
Definition: AttributeImpl.h:64
static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind, const ConstantRange &CR)
const ConstantRange & getValueAsConstantRange() const
Definition: Attributes.cpp:842
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.
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the AttributeSet or the FunctionIndex has an enum attribute of the given kind.
iterator begin() const
iterator end() const
AttributeListImpl & operator=(const AttributeListImpl &)=delete
AttributeListImpl(const AttributeListImpl &)=delete
MaybeAlign getStackAlignment() const
uint64_t getDereferenceableOrNullBytes() const
std::optional< unsigned > getVScaleRangeMax() const
bool hasAttribute(Attribute::AttrKind Kind) const
Type * getAttributeType(Attribute::AttrKind Kind) const
AllocFnKind getAllocKind() const
unsigned getVScaleRangeMin() const
MaybeAlign getAlignment() const
MemoryEffects getMemoryEffects() const
iterator begin() const
UWTableKind getUWTableKind() const
std::optional< std::pair< unsigned, std::optional< unsigned > > > getAllocSizeArgs() const
iterator end() const
uint64_t getDereferenceableBytes() const
unsigned getNumAttributes() const
Return the number of attributes this AttributeList contains.
AttributeSetNode & operator=(const AttributeSetNode &)=delete
static void Profile(FoldingSetNodeID &ID, ArrayRef< Attribute > AttrList)
void Profile(FoldingSetNodeID &ID) const
AttributeSetNode(const AttributeSetNode &)=delete
std::string getAsString(bool InAttrGrp) const
static AttributeSetNode * get(LLVMContext &C, const AttrBuilder &B)
bool hasAttributes() const
FPClassTest getNoFPClass() const
Attribute getAttribute(Attribute::AttrKind Kind) const
AttrKind
This enumeration lists the attributes that can be associated with parameters, function results,...
Definition: Attributes.h:86
@ None
No attributes have been set.
Definition: Attributes.h:88
@ EndAttrKinds
Sentinel value useful for loops.
Definition: Attributes.h:91
static bool isIntAttrKind(AttrKind Kind)
Definition: Attributes.h:102
static bool isEnumAttrKind(AttrKind Kind)
Definition: Attributes.h:99
const ConstantRange & getConstantRangeValue() const
ConstantRangeAttributeImpl(Attribute::AttrKind Kind, const ConstantRange &CR)
ArrayRef< ConstantRange > getConstantRangeListValue() const
static size_t totalSizeToAlloc(ArrayRef< ConstantRange > Val)
ConstantRangeListAttributeImpl(Attribute::AttrKind Kind, ArrayRef< ConstantRange > Val)
This class represents a range of values.
Definition: ConstantRange.h:47
const APInt & getLower() const
Return the lower value for this range.
const APInt & getUpper() const
Return the upper value for this range.
EnumAttributeImpl(AttrEntryKind ID, Attribute::AttrKind Kind)
Attribute::AttrKind getEnumKind() const
EnumAttributeImpl(Attribute::AttrKind Kind)
Node - This class is used to maintain the singly linked bucket list in a folding set.
Definition: FoldingSet.h:138
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
Definition: FoldingSet.h:327
IntAttributeImpl(Attribute::AttrKind Kind, uint64_t Val)
uint64_t getValue() const
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
StringAttributeImpl(StringRef Kind, StringRef Val=StringRef())
StringRef getStringKind() const
StringRef getStringValue() const
static size_t totalSizeToAlloc(StringRef Kind, StringRef Val)
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:147
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:150
See the file comment for details on the usage of the TrailingObjects type.
Type * getTypeValue() const
TypeAttributeImpl(Attribute::AttrKind Kind, Type *Ty)
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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:1697
AllocFnKind
Definition: Attributes.h:49
UWTableKind
Definition: CodeGen.h:120
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
OutputIt copy(R &&Range, OutputIt Out)
Definition: STLExtras.h:1841
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition: Alignment.h:117