LLVM 18.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 <optional>
28#include <string>
29#include <utility>
30
31namespace llvm {
32
33class LLVMContext;
34class Type;
35
36//===----------------------------------------------------------------------===//
37/// \class
38/// This class represents a single, uniqued attribute. That attribute
39/// could be a single enum, a tuple, or a string.
41 unsigned char KindID; ///< Holds the AttrEntryKind of the attribute
42
43protected:
49 };
50
51 AttributeImpl(AttrEntryKind KindID) : KindID(KindID) {}
52
53public:
54 // AttributesImpl is uniqued, these should not be available.
55 AttributeImpl(const AttributeImpl &) = delete;
57
58 bool isEnumAttribute() const { return KindID == EnumAttrEntry; }
59 bool isIntAttribute() const { return KindID == IntAttrEntry; }
60 bool isStringAttribute() const { return KindID == StringAttrEntry; }
61 bool isTypeAttribute() const { return KindID == TypeAttrEntry; }
62
64 bool hasAttribute(StringRef Kind) const;
65
67 uint64_t getValueAsInt() const;
68 bool getValueAsBool() const;
69
72
73 Type *getValueAsType() const;
74
75 /// Used when sorting the attributes.
76 bool operator<(const AttributeImpl &AI) const;
77
79 if (isEnumAttribute())
81 else if (isIntAttribute())
83 else if (isStringAttribute())
85 else
87 }
88
90 assert(Attribute::isEnumAttrKind(Kind) && "Expected enum attribute");
91 ID.AddInteger(Kind);
92 }
93
95 uint64_t Val) {
96 assert(Attribute::isIntAttrKind(Kind) && "Expected int attribute");
97 ID.AddInteger(Kind);
98 ID.AddInteger(Val);
99 }
100
101 static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values) {
102 ID.AddString(Kind);
103 if (!Values.empty()) ID.AddString(Values);
104 }
105
107 Type *Ty) {
108 ID.AddInteger(Kind);
109 ID.AddPointer(Ty);
110 }
111};
112
113static_assert(std::is_trivially_destructible<AttributeImpl>::value,
114 "AttributeImpl should be trivially destructible");
115
116//===----------------------------------------------------------------------===//
117/// \class
118/// A set of classes that contain the value of the
119/// attribute object. There are three main categories: enum attribute entries,
120/// represented by Attribute::AttrKind; alignment attribute entries; and string
121/// attribute enties, which are for target-dependent attributes.
122
125
126protected:
128 : AttributeImpl(ID), Kind(Kind) {}
129
130public:
132 : AttributeImpl(EnumAttrEntry), Kind(Kind) {
134 "Can't create a None attribute!");
135 }
136
137 Attribute::AttrKind getEnumKind() const { return Kind; }
138};
139
141 uint64_t Val;
142
143public:
145 : EnumAttributeImpl(IntAttrEntry, Kind), Val(Val) {
147 "Wrong kind for int attribute!");
148 }
149
150 uint64_t getValue() const { return Val; }
151};
152
154 : public AttributeImpl,
155 private TrailingObjects<StringAttributeImpl, char> {
156 friend TrailingObjects;
157
158 unsigned KindSize;
159 unsigned ValSize;
160 size_t numTrailingObjects(OverloadToken<char>) const {
161 return KindSize + 1 + ValSize + 1;
162 }
163
164public:
166 : AttributeImpl(StringAttrEntry), KindSize(Kind.size()),
167 ValSize(Val.size()) {
168 char *TrailingString = getTrailingObjects<char>();
169 // Some users rely on zero-termination.
170 llvm::copy(Kind, TrailingString);
171 TrailingString[KindSize] = '\0';
172 llvm::copy(Val, &TrailingString[KindSize + 1]);
173 TrailingString[KindSize + 1 + ValSize] = '\0';
174 }
175
177 return StringRef(getTrailingObjects<char>(), KindSize);
178 }
180 return StringRef(getTrailingObjects<char>() + KindSize + 1, ValSize);
181 }
182
183 static size_t totalSizeToAlloc(StringRef Kind, StringRef Val) {
184 return TrailingObjects::totalSizeToAlloc<char>(Kind.size() + 1 +
185 Val.size() + 1);
186 }
187};
188
190 Type *Ty;
191
192public:
194 : EnumAttributeImpl(TypeAttrEntry, Kind), Ty(Ty) {}
195
196 Type *getTypeValue() const { return Ty; }
197};
198
200 /// Bitset with a bit for each available attribute Attribute::AttrKind.
201 uint8_t AvailableAttrs[12] = {};
202 static_assert(Attribute::EndAttrKinds <= sizeof(AvailableAttrs) * CHAR_BIT,
203 "Too many attributes");
204
205public:
207 return AvailableAttrs[Kind / 8] & (1 << (Kind % 8));
208 }
209
211 AvailableAttrs[Kind / 8] |= 1 << (Kind % 8);
212 }
213};
214
215//===----------------------------------------------------------------------===//
216/// \class
217/// This class represents a group of attributes that apply to one
218/// element: function, return type, or parameter.
220 : public FoldingSetNode,
221 private TrailingObjects<AttributeSetNode, Attribute> {
222 friend TrailingObjects;
223
224 unsigned NumAttrs; ///< Number of attributes in this node.
225 AttributeBitSet AvailableAttrs; ///< Available enum attributes.
226
228
230
231 static AttributeSetNode *getSorted(LLVMContext &C,
232 ArrayRef<Attribute> SortedAttrs);
233 std::optional<Attribute> findEnumAttribute(Attribute::AttrKind Kind) const;
234
235public:
236 // AttributesSetNode is uniqued, these should not be available.
239
240 void operator delete(void *p) { ::operator delete(p); }
241
242 static AttributeSetNode *get(LLVMContext &C, const AttrBuilder &B);
243
245
246 /// Return the number of attributes this AttributeList contains.
247 unsigned getNumAttributes() const { return NumAttrs; }
248
250 return AvailableAttrs.hasAttribute(Kind);
251 }
252 bool hasAttribute(StringRef Kind) const;
253 bool hasAttributes() const { return NumAttrs != 0; }
254
256 Attribute getAttribute(StringRef Kind) const;
257
258 MaybeAlign getAlignment() const;
262 std::optional<std::pair<unsigned, std::optional<unsigned>>> getAllocSizeArgs()
263 const;
264 unsigned getVScaleRangeMin() const;
265 std::optional<unsigned> getVScaleRangeMax() const;
270 std::string getAsString(bool InAttrGrp) const;
272
273 using iterator = const Attribute *;
274
275 iterator begin() const { return getTrailingObjects<Attribute>(); }
276 iterator end() const { return begin() + NumAttrs; }
277
279 Profile(ID, ArrayRef(begin(), end()));
280 }
281
283 for (const auto &Attr : AttrList)
284 Attr.Profile(ID);
285 }
286};
287
288//===----------------------------------------------------------------------===//
289/// \class
290/// This class represents a set of attributes that apply to the function,
291/// return type, and parameters.
293 : public FoldingSetNode,
294 private TrailingObjects<AttributeListImpl, AttributeSet> {
295 friend class AttributeList;
296 friend TrailingObjects;
297
298private:
299 unsigned NumAttrSets; ///< Number of entries in this set.
300 /// Available enum function attributes.
301 AttributeBitSet AvailableFunctionAttrs;
302 /// Union of enum attributes available at any index.
303 AttributeBitSet AvailableSomewhereAttrs;
304
305 // Helper fn for TrailingObjects class.
306 size_t numTrailingObjects(OverloadToken<AttributeSet>) { return NumAttrSets; }
307
308public:
310
311 // AttributesSetImpt is uniqued, these should not be available.
314
315 /// Return true if the AttributeSet or the FunctionIndex has an
316 /// enum attribute of the given kind.
318 return AvailableFunctionAttrs.hasAttribute(Kind);
319 }
320
321 /// Return true if the specified attribute is set for at least one
322 /// parameter or for the return value. If Index is not nullptr, the index
323 /// of a parameter with the specified attribute is provided.
325 unsigned *Index = nullptr) const;
326
327 using iterator = const AttributeSet *;
328
329 iterator begin() const { return getTrailingObjects<AttributeSet>(); }
330 iterator end() const { return begin() + NumAttrSets; }
331
332 void Profile(FoldingSetNodeID &ID) const;
334
335 void dump() const;
336};
337
338static_assert(std::is_trivially_destructible<AttributeListImpl>::value,
339 "AttributeListImpl should be trivially destructible");
340
341} // end namespace llvm
342
343#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.
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...
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
void addAttribute(Attribute::AttrKind Kind)
bool hasAttribute(Attribute::AttrKind Kind) const
bool hasAttribute(Attribute::AttrKind A) const
Definition: Attributes.cpp:643
void Profile(FoldingSetNodeID &ID) const
Definition: AttributeImpl.h:78
Type * getValueAsType() const
Definition: Attributes.cpp:678
Attribute::AttrKind getKindAsEnum() const
Definition: Attributes.cpp:653
static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values)
bool operator<(const AttributeImpl &AI) const
Used when sorting the attributes.
Definition: Attributes.cpp:683
static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind, uint64_t Val)
Definition: AttributeImpl.h:94
AttributeImpl & operator=(const AttributeImpl &)=delete
static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind)
Definition: AttributeImpl.h:89
uint64_t getValueAsInt() const
Definition: Attributes.cpp:658
bool isIntAttribute() const
Definition: AttributeImpl.h:59
bool isTypeAttribute() const
Definition: AttributeImpl.h:61
AttributeImpl(AttrEntryKind KindID)
Definition: AttributeImpl.h:51
AttributeImpl(const AttributeImpl &)=delete
static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind, Type *Ty)
bool getValueAsBool() const
Definition: Attributes.cpp:663
StringRef getKindAsString() const
Definition: Attributes.cpp:668
StringRef getValueAsString() const
Definition: Attributes.cpp:673
bool isEnumAttribute() const
Definition: AttributeImpl.h:58
bool isStringAttribute() const
Definition: AttributeImpl.h:60
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
Definition: Attributes.cpp:989
uint64_t getDereferenceableOrNullBytes() const
std::optional< unsigned > getVScaleRangeMax() const
bool hasAttribute(Attribute::AttrKind Kind) const
Type * getAttributeType(Attribute::AttrKind Kind) const
Definition: Attributes.cpp:995
AllocFnKind getAllocKind() const
unsigned getVScaleRangeMin() const
MaybeAlign getAlignment() const
Definition: Attributes.cpp:983
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)
Definition: Attributes.cpp:948
bool hasAttributes() const
FPClassTest getNoFPClass() const
Attribute getAttribute(Attribute::AttrKind Kind) const
Definition: Attributes.cpp:973
AttrKind
This enumeration lists the attributes that can be associated with parameters, function results,...
Definition: Attributes.h:84
@ None
No attributes have been set.
Definition: Attributes.h:86
@ EndAttrKinds
Sentinal value useful for loops.
Definition: Attributes.h:89
static bool isIntAttrKind(AttrKind Kind)
Definition: Attributes.h:100
static bool isEnumAttrKind(AttrKind Kind)
Definition: Attributes.h:97
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:137
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
Definition: FoldingSet.h:319
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:1684
AllocFnKind
Definition: Attributes.h:47
UWTableKind
Definition: CodeGen.h:120
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
OutputIt copy(R &&Range, OutputIt Out)
Definition: STLExtras.h:1828
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition: Alignment.h:117