LLVM 17.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;
269 std::string getAsString(bool InAttrGrp) const;
271
272 using iterator = const Attribute *;
273
274 iterator begin() const { return getTrailingObjects<Attribute>(); }
275 iterator end() const { return begin() + NumAttrs; }
276
278 Profile(ID, ArrayRef(begin(), end()));
279 }
280
282 for (const auto &Attr : AttrList)
283 Attr.Profile(ID);
284 }
285};
286
287//===----------------------------------------------------------------------===//
288/// \class
289/// This class represents a set of attributes that apply to the function,
290/// return type, and parameters.
292 : public FoldingSetNode,
293 private TrailingObjects<AttributeListImpl, AttributeSet> {
294 friend class AttributeList;
295 friend TrailingObjects;
296
297private:
298 unsigned NumAttrSets; ///< Number of entries in this set.
299 /// Available enum function attributes.
300 AttributeBitSet AvailableFunctionAttrs;
301 /// Union of enum attributes available at any index.
302 AttributeBitSet AvailableSomewhereAttrs;
303
304 // Helper fn for TrailingObjects class.
305 size_t numTrailingObjects(OverloadToken<AttributeSet>) { return NumAttrSets; }
306
307public:
309
310 // AttributesSetImpt is uniqued, these should not be available.
313
314 /// Return true if the AttributeSet or the FunctionIndex has an
315 /// enum attribute of the given kind.
317 return AvailableFunctionAttrs.hasAttribute(Kind);
318 }
319
320 /// Return true if the specified attribute is set for at least one
321 /// parameter or for the return value. If Index is not nullptr, the index
322 /// of a parameter with the specified attribute is provided.
324 unsigned *Index = nullptr) const;
325
326 using iterator = const AttributeSet *;
327
328 iterator begin() const { return getTrailingObjects<AttributeSet>(); }
329 iterator end() const { return begin() + NumAttrSets; }
330
331 void Profile(FoldingSetNodeID &ID) const;
333
334 void dump() const;
335};
336
337static_assert(std::is_trivially_destructible<AttributeListImpl>::value,
338 "AttributeListImpl should be trivially destructible");
339
340} // end namespace llvm
341
342#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:390
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
return ToRemove size() > 0
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:624
void Profile(FoldingSetNodeID &ID) const
Definition: AttributeImpl.h:78
Type * getValueAsType() const
Definition: Attributes.cpp:659
Attribute::AttrKind getKindAsEnum() const
Definition: Attributes.cpp:634
static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values)
bool operator<(const AttributeImpl &AI) const
Used when sorting the attributes.
Definition: Attributes.cpp:664
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:639
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:644
StringRef getKindAsString() const
Definition: Attributes.cpp:649
StringRef getValueAsString() const
Definition: Attributes.cpp:654
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:966
uint64_t getDereferenceableOrNullBytes() const
Definition: Attributes.cpp:984
std::optional< unsigned > getVScaleRangeMax() const
bool hasAttribute(Attribute::AttrKind Kind) const
Type * getAttributeType(Attribute::AttrKind Kind) const
Definition: Attributes.cpp:972
AllocFnKind getAllocKind() const
unsigned getVScaleRangeMin() const
Definition: Attributes.cpp:997
MaybeAlign getAlignment() const
Definition: Attributes.cpp:960
MemoryEffects getMemoryEffects() const
iterator begin() const
UWTableKind getUWTableKind() const
std::optional< std::pair< unsigned, std::optional< unsigned > > > getAllocSizeArgs() const
Definition: Attributes.cpp:991
iterator end() const
uint64_t getDereferenceableBytes() const
Definition: Attributes.cpp:978
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:925
bool hasAttributes() const
Attribute getAttribute(Attribute::AttrKind Kind) const
Definition: Attributes.cpp:950
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
Sentinal 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
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:136
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
Definition: FoldingSet.h:318
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
Summary of how a function affects memory in the program.
Definition: ModRef.h:63
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
AllocFnKind
Definition: Attributes.h:49
UWTableKind
Definition: CodeGen.h:121
OutputIt copy(R &&Range, OutputIt Out)
Definition: STLExtras.h:1837
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition: Alignment.h:117