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"
25#include <cassert>
26#include <cstddef>
27#include <cstdint>
28#include <optional>
29#include <string>
30#include <utility>
31
32namespace llvm {
33
34class LLVMContext;
35class Type;
36
37//===----------------------------------------------------------------------===//
38/// \class
39/// This class represents a single, uniqued attribute. That attribute
40/// could be a single enum, a tuple, or a string.
42 unsigned char KindID; ///< Holds the AttrEntryKind of the attribute
43
44protected:
51 };
52
53 AttributeImpl(AttrEntryKind KindID) : KindID(KindID) {}
54
55public:
56 // AttributesImpl is uniqued, these should not be available.
57 AttributeImpl(const AttributeImpl &) = delete;
59
60 bool isEnumAttribute() const { return KindID == EnumAttrEntry; }
61 bool isIntAttribute() const { return KindID == IntAttrEntry; }
62 bool isStringAttribute() const { return KindID == StringAttrEntry; }
63 bool isTypeAttribute() const { return KindID == TypeAttrEntry; }
65 return KindID == ConstantRangeAttrEntry;
66 }
67
69 bool hasAttribute(StringRef Kind) const;
70
72 uint64_t getValueAsInt() const;
73 bool getValueAsBool() const;
74
77
78 Type *getValueAsType() const;
79
81
82 /// Used when sorting the attributes.
83 bool operator<(const AttributeImpl &AI) const;
84
86 if (isEnumAttribute())
88 else if (isIntAttribute())
90 else if (isStringAttribute())
92 else if (isTypeAttribute())
94 else
96 }
97
99 assert(Attribute::isEnumAttrKind(Kind) && "Expected enum attribute");
100 ID.AddInteger(Kind);
101 }
102
104 uint64_t Val) {
105 assert(Attribute::isIntAttrKind(Kind) && "Expected int attribute");
106 ID.AddInteger(Kind);
107 ID.AddInteger(Val);
108 }
109
110 static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values) {
111 ID.AddString(Kind);
112 if (!Values.empty()) ID.AddString(Values);
113 }
114
116 Type *Ty) {
117 ID.AddInteger(Kind);
118 ID.AddPointer(Ty);
119 }
120
122 const ConstantRange &CR) {
123 ID.AddInteger(Kind);
124 CR.getLower().Profile(ID);
125 CR.getUpper().Profile(ID);
126 }
127};
128
129static_assert(std::is_trivially_destructible<AttributeImpl>::value,
130 "AttributeImpl should be trivially destructible");
131
132//===----------------------------------------------------------------------===//
133/// \class
134/// A set of classes that contain the value of the
135/// attribute object. There are three main categories: enum attribute entries,
136/// represented by Attribute::AttrKind; alignment attribute entries; and string
137/// attribute enties, which are for target-dependent attributes.
138
141
142protected:
144 : AttributeImpl(ID), Kind(Kind) {}
145
146public:
148 : AttributeImpl(EnumAttrEntry), Kind(Kind) {
150 "Can't create a None attribute!");
151 }
152
153 Attribute::AttrKind getEnumKind() const { return Kind; }
154};
155
157 uint64_t Val;
158
159public:
161 : EnumAttributeImpl(IntAttrEntry, Kind), Val(Val) {
163 "Wrong kind for int attribute!");
164 }
165
166 uint64_t getValue() const { return Val; }
167};
168
170 : public AttributeImpl,
171 private TrailingObjects<StringAttributeImpl, char> {
172 friend TrailingObjects;
173
174 unsigned KindSize;
175 unsigned ValSize;
176 size_t numTrailingObjects(OverloadToken<char>) const {
177 return KindSize + 1 + ValSize + 1;
178 }
179
180public:
182 : AttributeImpl(StringAttrEntry), KindSize(Kind.size()),
183 ValSize(Val.size()) {
184 char *TrailingString = getTrailingObjects<char>();
185 // Some users rely on zero-termination.
186 llvm::copy(Kind, TrailingString);
187 TrailingString[KindSize] = '\0';
188 llvm::copy(Val, &TrailingString[KindSize + 1]);
189 TrailingString[KindSize + 1 + ValSize] = '\0';
190 }
191
193 return StringRef(getTrailingObjects<char>(), KindSize);
194 }
196 return StringRef(getTrailingObjects<char>() + KindSize + 1, ValSize);
197 }
198
199 static size_t totalSizeToAlloc(StringRef Kind, StringRef Val) {
200 return TrailingObjects::totalSizeToAlloc<char>(Kind.size() + 1 +
201 Val.size() + 1);
202 }
203};
204
206 Type *Ty;
207
208public:
210 : EnumAttributeImpl(TypeAttrEntry, Kind), Ty(Ty) {}
211
212 Type *getTypeValue() const { return Ty; }
213};
214
216 ConstantRange CR;
217
218public:
220 : EnumAttributeImpl(ConstantRangeAttrEntry, Kind), CR(CR) {}
221
222 const ConstantRange &getConstantRangeValue() const { return CR; }
223};
224
226 /// Bitset with a bit for each available attribute Attribute::AttrKind.
227 uint8_t AvailableAttrs[12] = {};
228 static_assert(Attribute::EndAttrKinds <= sizeof(AvailableAttrs) * CHAR_BIT,
229 "Too many attributes");
230
231public:
233 return AvailableAttrs[Kind / 8] & (1 << (Kind % 8));
234 }
235
237 AvailableAttrs[Kind / 8] |= 1 << (Kind % 8);
238 }
239};
240
241//===----------------------------------------------------------------------===//
242/// \class
243/// This class represents a group of attributes that apply to one
244/// element: function, return type, or parameter.
246 : public FoldingSetNode,
247 private TrailingObjects<AttributeSetNode, Attribute> {
248 friend TrailingObjects;
249
250 unsigned NumAttrs; ///< Number of attributes in this node.
251 AttributeBitSet AvailableAttrs; ///< Available enum attributes.
252
254
256
257 static AttributeSetNode *getSorted(LLVMContext &C,
258 ArrayRef<Attribute> SortedAttrs);
259 std::optional<Attribute> findEnumAttribute(Attribute::AttrKind Kind) const;
260
261public:
262 // AttributesSetNode is uniqued, these should not be available.
265
266 void operator delete(void *p) { ::operator delete(p); }
267
268 static AttributeSetNode *get(LLVMContext &C, const AttrBuilder &B);
269
271
272 /// Return the number of attributes this AttributeList contains.
273 unsigned getNumAttributes() const { return NumAttrs; }
274
276 return AvailableAttrs.hasAttribute(Kind);
277 }
278 bool hasAttribute(StringRef Kind) const;
279 bool hasAttributes() const { return NumAttrs != 0; }
280
282 Attribute getAttribute(StringRef Kind) const;
283
284 MaybeAlign getAlignment() const;
288 std::optional<std::pair<unsigned, std::optional<unsigned>>> getAllocSizeArgs()
289 const;
290 unsigned getVScaleRangeMin() const;
291 std::optional<unsigned> getVScaleRangeMax() const;
296 std::string getAsString(bool InAttrGrp) const;
298
299 using iterator = const Attribute *;
300
301 iterator begin() const { return getTrailingObjects<Attribute>(); }
302 iterator end() const { return begin() + NumAttrs; }
303
305 Profile(ID, ArrayRef(begin(), end()));
306 }
307
309 for (const auto &Attr : AttrList)
310 Attr.Profile(ID);
311 }
312};
313
314//===----------------------------------------------------------------------===//
315/// \class
316/// This class represents a set of attributes that apply to the function,
317/// return type, and parameters.
319 : public FoldingSetNode,
320 private TrailingObjects<AttributeListImpl, AttributeSet> {
321 friend class AttributeList;
322 friend TrailingObjects;
323
324private:
325 unsigned NumAttrSets; ///< Number of entries in this set.
326 /// Available enum function attributes.
327 AttributeBitSet AvailableFunctionAttrs;
328 /// Union of enum attributes available at any index.
329 AttributeBitSet AvailableSomewhereAttrs;
330
331 // Helper fn for TrailingObjects class.
332 size_t numTrailingObjects(OverloadToken<AttributeSet>) { return NumAttrSets; }
333
334public:
336
337 // AttributesSetImpt is uniqued, these should not be available.
340
341 /// Return true if the AttributeSet or the FunctionIndex has an
342 /// enum attribute of the given kind.
344 return AvailableFunctionAttrs.hasAttribute(Kind);
345 }
346
347 /// Return true if the specified attribute is set for at least one
348 /// parameter or for the return value. If Index is not nullptr, the index
349 /// of a parameter with the specified attribute is provided.
351 unsigned *Index = nullptr) const;
352
353 using iterator = const AttributeSet *;
354
355 iterator begin() const { return getTrailingObjects<AttributeSet>(); }
356 iterator end() const { return begin() + NumAttrSets; }
357
358 void Profile(FoldingSetNodeID &ID) const;
360
361 void dump() const;
362};
363
364static_assert(std::is_trivially_destructible<AttributeListImpl>::value,
365 "AttributeListImpl should be trivially destructible");
366
367} // end namespace llvm
368
369#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...
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
void addAttribute(Attribute::AttrKind Kind)
bool hasAttribute(Attribute::AttrKind Kind) const
bool isConstantRangeAttribute() const
Definition: AttributeImpl.h:64
bool hasAttribute(Attribute::AttrKind A) const
Definition: Attributes.cpp:692
void Profile(FoldingSetNodeID &ID) const
Definition: AttributeImpl.h:85
Type * getValueAsType() const
Definition: Attributes.cpp:728
Attribute::AttrKind getKindAsEnum() const
Definition: Attributes.cpp:702
static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values)
bool operator<(const AttributeImpl &AI) const
Used when sorting the attributes.
Definition: Attributes.cpp:739
static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind, uint64_t Val)
AttributeImpl & operator=(const AttributeImpl &)=delete
static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind)
Definition: AttributeImpl.h:98
uint64_t getValueAsInt() const
Definition: Attributes.cpp:708
bool isIntAttribute() const
Definition: AttributeImpl.h:61
bool isTypeAttribute() const
Definition: AttributeImpl.h:63
AttributeImpl(AttrEntryKind KindID)
Definition: AttributeImpl.h:53
AttributeImpl(const AttributeImpl &)=delete
static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind, Type *Ty)
bool getValueAsBool() const
Definition: Attributes.cpp:713
StringRef getKindAsString() const
Definition: Attributes.cpp:718
StringRef getValueAsString() const
Definition: Attributes.cpp:723
bool isEnumAttribute() const
Definition: AttributeImpl.h:60
bool isStringAttribute() const
Definition: AttributeImpl.h:62
static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind, const ConstantRange &CR)
const ConstantRange & getValueAsConstantRange() const
Definition: Attributes.cpp:733
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:85
@ None
No attributes have been set.
Definition: Attributes.h:87
@ EndAttrKinds
Sentinel value useful for loops.
Definition: Attributes.h:90
static bool isIntAttrKind(AttrKind Kind)
Definition: Attributes.h:101
static bool isEnumAttrKind(AttrKind Kind)
Definition: Attributes.h:98
const ConstantRange & getConstantRangeValue() const
ConstantRangeAttributeImpl(Attribute::AttrKind Kind, const ConstantRange &CR)
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: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:1680
AllocFnKind
Definition: Attributes.h:48
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