LLVM 19.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 when sorting the attributes.
90 bool operator<(const AttributeImpl &AI) const;
91
93 if (isEnumAttribute())
95 else if (isIntAttribute())
97 else if (isStringAttribute())
99 else if (isTypeAttribute())
101 else if (isConstantRangeAttribute())
103 else
105 }
106
108 assert(Attribute::isEnumAttrKind(Kind) && "Expected enum attribute");
109 ID.AddInteger(Kind);
110 }
111
113 uint64_t Val) {
114 assert(Attribute::isIntAttrKind(Kind) && "Expected int attribute");
115 ID.AddInteger(Kind);
116 ID.AddInteger(Val);
117 }
118
119 static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values) {
120 ID.AddString(Kind);
121 if (!Values.empty()) ID.AddString(Values);
122 }
123
125 Type *Ty) {
126 ID.AddInteger(Kind);
127 ID.AddPointer(Ty);
128 }
129
131 const ConstantRange &CR) {
132 ID.AddInteger(Kind);
133 CR.getLower().Profile(ID);
134 CR.getUpper().Profile(ID);
135 }
136
139 ID.AddInteger(Kind);
140 ID.AddInteger(Val.size());
141 for (auto &CR : Val) {
142 CR.getLower().Profile(ID);
143 CR.getUpper().Profile(ID);
144 }
145 }
146};
147
148static_assert(std::is_trivially_destructible<AttributeImpl>::value,
149 "AttributeImpl should be trivially destructible");
150
151//===----------------------------------------------------------------------===//
152/// \class
153/// A set of classes that contain the value of the
154/// attribute object. There are three main categories: enum attribute entries,
155/// represented by Attribute::AttrKind; alignment attribute entries; and string
156/// attribute enties, which are for target-dependent attributes.
157
160
161protected:
163 : AttributeImpl(ID), Kind(Kind) {}
164
165public:
167 : AttributeImpl(EnumAttrEntry), Kind(Kind) {
169 "Can't create a None attribute!");
170 }
171
172 Attribute::AttrKind getEnumKind() const { return Kind; }
173};
174
176 uint64_t Val;
177
178public:
180 : EnumAttributeImpl(IntAttrEntry, Kind), Val(Val) {
182 "Wrong kind for int attribute!");
183 }
184
185 uint64_t getValue() const { return Val; }
186};
187
189 : public AttributeImpl,
190 private TrailingObjects<StringAttributeImpl, char> {
191 friend TrailingObjects;
192
193 unsigned KindSize;
194 unsigned ValSize;
195 size_t numTrailingObjects(OverloadToken<char>) const {
196 return KindSize + 1 + ValSize + 1;
197 }
198
199public:
201 : AttributeImpl(StringAttrEntry), KindSize(Kind.size()),
202 ValSize(Val.size()) {
203 char *TrailingString = getTrailingObjects<char>();
204 // Some users rely on zero-termination.
205 llvm::copy(Kind, TrailingString);
206 TrailingString[KindSize] = '\0';
207 llvm::copy(Val, &TrailingString[KindSize + 1]);
208 TrailingString[KindSize + 1 + ValSize] = '\0';
209 }
210
212 return StringRef(getTrailingObjects<char>(), KindSize);
213 }
215 return StringRef(getTrailingObjects<char>() + KindSize + 1, ValSize);
216 }
217
218 static size_t totalSizeToAlloc(StringRef Kind, StringRef Val) {
219 return TrailingObjects::totalSizeToAlloc<char>(Kind.size() + 1 +
220 Val.size() + 1);
221 }
222};
223
225 Type *Ty;
226
227public:
229 : EnumAttributeImpl(TypeAttrEntry, Kind), Ty(Ty) {}
230
231 Type *getTypeValue() const { return Ty; }
232};
233
235 ConstantRange CR;
236
237public:
239 : EnumAttributeImpl(ConstantRangeAttrEntry, Kind), CR(CR) {}
240
241 const ConstantRange &getConstantRangeValue() const { return CR; }
242};
243
245 : public EnumAttributeImpl,
246 private TrailingObjects<ConstantRangeListAttributeImpl, ConstantRange> {
247 friend TrailingObjects;
248
249 unsigned Size;
250 size_t numTrailingObjects(OverloadToken<ConstantRange>) const { return Size; }
251
252public:
256 assert(Size > 0);
257 ConstantRange *TrailingCR = getTrailingObjects<ConstantRange>();
258 std::uninitialized_copy(Val.begin(), Val.end(), TrailingCR);
259 }
260
262 ConstantRange *TrailingCR = getTrailingObjects<ConstantRange>();
263 for (unsigned I = 0; I != Size; ++I)
264 TrailingCR[I].~ConstantRange();
265 }
266
268 return ArrayRef(getTrailingObjects<ConstantRange>(), Size);
269 }
270
272 return TrailingObjects::totalSizeToAlloc<ConstantRange>(Val.size());
273 }
274};
275
277 /// Bitset with a bit for each available attribute Attribute::AttrKind.
278 uint8_t AvailableAttrs[12] = {};
279 static_assert(Attribute::EndAttrKinds <= sizeof(AvailableAttrs) * CHAR_BIT,
280 "Too many attributes");
281
282public:
284 return AvailableAttrs[Kind / 8] & (1 << (Kind % 8));
285 }
286
288 AvailableAttrs[Kind / 8] |= 1 << (Kind % 8);
289 }
290};
291
292//===----------------------------------------------------------------------===//
293/// \class
294/// This class represents a group of attributes that apply to one
295/// element: function, return type, or parameter.
297 : public FoldingSetNode,
298 private TrailingObjects<AttributeSetNode, Attribute> {
299 friend TrailingObjects;
300
301 unsigned NumAttrs; ///< Number of attributes in this node.
302 AttributeBitSet AvailableAttrs; ///< Available enum attributes.
303
305
307
308 static AttributeSetNode *getSorted(LLVMContext &C,
309 ArrayRef<Attribute> SortedAttrs);
310 std::optional<Attribute> findEnumAttribute(Attribute::AttrKind Kind) const;
311
312public:
313 // AttributesSetNode is uniqued, these should not be available.
316
317 void operator delete(void *p) { ::operator delete(p); }
318
319 static AttributeSetNode *get(LLVMContext &C, const AttrBuilder &B);
320
322
323 /// Return the number of attributes this AttributeList contains.
324 unsigned getNumAttributes() const { return NumAttrs; }
325
327 return AvailableAttrs.hasAttribute(Kind);
328 }
329 bool hasAttribute(StringRef Kind) const;
330 bool hasAttributes() const { return NumAttrs != 0; }
331
333 Attribute getAttribute(StringRef Kind) const;
334
335 MaybeAlign getAlignment() const;
339 std::optional<std::pair<unsigned, std::optional<unsigned>>> getAllocSizeArgs()
340 const;
341 unsigned getVScaleRangeMin() const;
342 std::optional<unsigned> getVScaleRangeMax() const;
347 std::string getAsString(bool InAttrGrp) const;
349
350 using iterator = const Attribute *;
351
352 iterator begin() const { return getTrailingObjects<Attribute>(); }
353 iterator end() const { return begin() + NumAttrs; }
354
356 Profile(ID, ArrayRef(begin(), end()));
357 }
358
360 for (const auto &Attr : AttrList)
361 Attr.Profile(ID);
362 }
363};
364
365//===----------------------------------------------------------------------===//
366/// \class
367/// This class represents a set of attributes that apply to the function,
368/// return type, and parameters.
370 : public FoldingSetNode,
371 private TrailingObjects<AttributeListImpl, AttributeSet> {
372 friend class AttributeList;
373 friend TrailingObjects;
374
375private:
376 unsigned NumAttrSets; ///< Number of entries in this set.
377 /// Available enum function attributes.
378 AttributeBitSet AvailableFunctionAttrs;
379 /// Union of enum attributes available at any index.
380 AttributeBitSet AvailableSomewhereAttrs;
381
382 // Helper fn for TrailingObjects class.
383 size_t numTrailingObjects(OverloadToken<AttributeSet>) { return NumAttrSets; }
384
385public:
387
388 // AttributesSetImpt is uniqued, these should not be available.
391
392 /// Return true if the AttributeSet or the FunctionIndex has an
393 /// enum attribute of the given kind.
395 return AvailableFunctionAttrs.hasAttribute(Kind);
396 }
397
398 /// Return true if the specified attribute is set for at least one
399 /// parameter or for the return value. If Index is not nullptr, the index
400 /// of a parameter with the specified attribute is provided.
402 unsigned *Index = nullptr) const;
403
404 using iterator = const AttributeSet *;
405
406 iterator begin() const { return getTrailingObjects<AttributeSet>(); }
407 iterator end() const { return begin() + NumAttrSets; }
408
409 void Profile(FoldingSetNodeID &ID) const;
411
412 void dump() const;
413};
414
415static_assert(std::is_trivially_destructible<AttributeListImpl>::value,
416 "AttributeListImpl should be trivially destructible");
417
418} // end namespace llvm
419
420#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:391
This file defines the DenseMap class.
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:155
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:154
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:165
iterator begin() const
Definition: ArrayRef.h:153
void addAttribute(Attribute::AttrKind Kind)
bool hasAttribute(Attribute::AttrKind Kind) const
bool isConstantRangeAttribute() const
Definition: AttributeImpl.h:66
bool hasAttribute(Attribute::AttrKind A) const
Definition: Attributes.cpp:757
void Profile(FoldingSetNodeID &ID) const
Definition: AttributeImpl.h:92
Type * getValueAsType() const
Definition: Attributes.cpp:793
Attribute::AttrKind getKindAsEnum() const
Definition: Attributes.cpp:767
static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values)
bool operator<(const AttributeImpl &AI) const
Used when sorting the attributes.
Definition: Attributes.cpp:810
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:773
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:778
StringRef getKindAsString() const
Definition: Attributes.cpp:783
StringRef getValueAsString() const
Definition: Attributes.cpp:788
bool isEnumAttribute() const
Definition: AttributeImpl.h:62
ArrayRef< ConstantRange > getValueAsConstantRangeList() const
Definition: Attributes.cpp:804
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:798
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:50
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:134
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:137
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:1680
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:1824
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition: Alignment.h:117