LLVM 24.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:
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
95 /// Only the ConstantRange kinds are uniqued by profile; every other kind
96 /// has a pool with a typed key.
103
105 const ConstantRange &CR) {
106 ID.AddInteger(Kind);
107 CR.getLower().Profile(ID);
108 CR.getUpper().Profile(ID);
109 }
110
113 ID.AddInteger(Kind);
114 ID.AddInteger(Val.size());
115 for (auto &CR : Val) {
116 CR.getLower().Profile(ID);
117 CR.getUpper().Profile(ID);
118 }
119 }
120};
121
122static_assert(std::is_trivially_destructible<AttributeImpl>::value,
123 "AttributeImpl should be trivially destructible");
124
125//===----------------------------------------------------------------------===//
126/// \class
127/// A set of classes that contain the value of the
128/// attribute object. There are three main categories: enum attribute entries,
129/// represented by Attribute::AttrKind; alignment attribute entries; and string
130/// attribute enties, which are for target-dependent attributes.
131
134
135protected:
138
139public:
141 : AttributeImpl(EnumAttrEntry), Kind(Kind) {
143 "Can't create a None attribute!");
144 }
145
146 Attribute::AttrKind getEnumKind() const { return Kind; }
147};
148
150 uint64_t Val;
151
152public:
154 : EnumAttributeImpl(IntAttrEntry, Kind), Val(Val) {
156 "Wrong kind for int attribute!");
157 }
158
159 uint64_t getValue() const { return Val; }
160
161 std::pair<unsigned, uint64_t> getKey() const { return {getEnumKind(), Val}; }
162};
163
165 : public AttributeImpl,
166 private TrailingObjects<StringAttributeImpl, char> {
167 friend TrailingObjects;
168
169 unsigned KindSize;
170 unsigned ValSize;
171
172public:
174 : AttributeImpl(StringAttrEntry), KindSize(Kind.size()),
175 ValSize(Val.size()) {
176 char *TrailingString = getTrailingObjects();
177 // Some users rely on zero-termination.
178 llvm::copy(Kind, TrailingString);
179 TrailingString[KindSize] = '\0';
180 llvm::copy(Val, &TrailingString[KindSize + 1]);
181 TrailingString[KindSize + 1 + ValSize] = '\0';
182 }
183
185 return StringRef(getTrailingObjects(), KindSize);
186 }
188 return StringRef(getTrailingObjects() + KindSize + 1, ValSize);
189 }
190
191 std::pair<StringRef, StringRef> getKey() const {
192 return {getStringKind(), getStringValue()};
193 }
194
195 static size_t totalSizeToAlloc(StringRef Kind, StringRef Val) {
196 return TrailingObjects::totalSizeToAlloc<char>(Kind.size() + 1 +
197 Val.size() + 1);
198 }
199};
200
202 Type *Ty;
203
204public:
207
208 Type *getTypeValue() const { return Ty; }
209
210 std::pair<unsigned, Type *> getKey() const { return {getEnumKind(), Ty}; }
211};
212
222
224 : public EnumAttributeImpl,
225 private TrailingObjects<ConstantRangeListAttributeImpl, ConstantRange> {
226 friend TrailingObjects;
227
228 unsigned Size;
229
230public:
237
239 for (ConstantRange &CR : getTrailingObjects(Size))
240 CR.~ConstantRange();
241 }
242
246
250};
251
253 /// Bitset with a bit for each available attribute Attribute::AttrKind.
254 uint8_t AvailableAttrs[16] = {};
255 static_assert(Attribute::EndAttrKinds <= sizeof(AvailableAttrs) * CHAR_BIT,
256 "Too many attributes");
257
258public:
260 return AvailableAttrs[Kind / 8] & (1 << (Kind % 8));
261 }
262
264 AvailableAttrs[Kind / 8] |= 1 << (Kind % 8);
265 }
266};
267
268//===----------------------------------------------------------------------===//
269/// \class
270/// This class represents a group of attributes that apply to one
271/// element: function, return type, or parameter.
272class AttributeSetNode final
273 : public FoldingSetNode,
274 private TrailingObjects<AttributeSetNode, Attribute> {
275 friend TrailingObjects;
276
277 unsigned NumAttrs; ///< Number of attributes in this node.
278 AttributeBitSet AvailableAttrs; ///< Available enum attributes.
279
281
282 AttributeSetNode(ArrayRef<Attribute> Attrs);
283
284 static AttributeSetNode *getSorted(LLVMContext &C,
285 ArrayRef<Attribute> SortedAttrs);
286 std::optional<Attribute> findEnumAttribute(Attribute::AttrKind Kind) const;
287
288public:
289 // AttributesSetNode is uniqued, these should not be available.
290 AttributeSetNode(const AttributeSetNode &) = delete;
291 AttributeSetNode &operator=(const AttributeSetNode &) = delete;
292
293 void operator delete(void *p) { ::operator delete(p); }
294
295 static AttributeSetNode *get(LLVMContext &C, const AttrBuilder &B);
296
298
299 /// Return the number of attributes this AttributeList contains.
300 unsigned getNumAttributes() const { return NumAttrs; }
301
303 return AvailableAttrs.hasAttribute(Kind);
304 }
305 bool hasAttribute(StringRef Kind) const;
306 bool hasAttributes() const { return NumAttrs != 0; }
307
309 Attribute getAttribute(StringRef Kind) const;
310
311 MaybeAlign getAlignment() const;
316 std::optional<std::pair<unsigned, std::optional<unsigned>>> getAllocSizeArgs()
317 const;
318 unsigned getVScaleRangeMin() const;
319 std::optional<unsigned> getVScaleRangeMax() const;
325 std::string getAsString(bool InAttrGrp) const;
327
328 using iterator = const Attribute *;
329
330 iterator begin() const { return getTrailingObjects(); }
331 iterator end() const { return begin() + NumAttrs; }
332
333 ArrayRef<Attribute> getKey() const { return getTrailingObjects(NumAttrs); }
334};
335
336//===----------------------------------------------------------------------===//
337/// \class
338/// This class represents a set of attributes that apply to the function,
339/// return type, and parameters.
341 : public FoldingSetNode,
342 private TrailingObjects<AttributeListImpl, AttributeSet> {
343 friend class AttributeList;
344 friend TrailingObjects;
345
346private:
347 unsigned NumAttrSets; ///< Number of entries in this set.
348 /// Available enum function attributes.
349 AttributeBitSet AvailableFunctionAttrs;
350 /// Union of enum attributes available at any index.
351 AttributeBitSet AvailableSomewhereAttrs;
352
353public:
355
356 // AttributesSetImpt is uniqued, these should not be available.
359
360 /// Return true if the AttributeSet or the FunctionIndex has an
361 /// enum attribute of the given kind.
363 return AvailableFunctionAttrs.hasAttribute(Kind);
364 }
365
366 /// Return true if the specified attribute is set for at least one
367 /// parameter or for the return value. If Index is not nullptr, the index
368 /// of a parameter with the specified attribute is provided.
370 unsigned *Index = nullptr) const;
371
372 using iterator = const AttributeSet *;
373
374 iterator begin() const { return getTrailingObjects(); }
375 iterator end() const { return begin() + NumAttrSets; }
376
378 return getTrailingObjects(NumAttrSets);
379 }
380
381 void dump() const;
382};
383
384static_assert(std::is_trivially_destructible<AttributeListImpl>::value,
385 "AttributeListImpl should be trivially destructible");
386
387} // end namespace llvm
388
389#endif // LLVM_LIB_IR_ATTRIBUTEIMPL_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
unsigned uint64_t
This file contains the simple types necessary to represent the attributes associated with functions a...
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
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
This header defines support for implementing classes that have some trailing object (or arrays of obj...
LLVM_ABI void Profile(FoldingSetNodeID &id) const
Used to insert APInt objects, or objects that contain APInt objects, into FoldingSets.
Definition APInt.cpp:152
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
Get the array size.
Definition ArrayRef.h:141
void addAttribute(Attribute::AttrKind Kind)
bool hasAttribute(Attribute::AttrKind Kind) const
This class represents a single, uniqued attribute.
int cmp(const AttributeImpl &AI, bool KindOnly) const
Used to sort attributes.
bool isConstantRangeAttribute() const
bool hasAttribute(Attribute::AttrKind A) const
void Profile(FoldingSetNodeID &ID) const
Only the ConstantRange kinds are uniqued by profile; every other kind has a pool with a typed key.
Type * getValueAsType() const
Attribute::AttrKind getKindAsEnum() const
bool operator<(const AttributeImpl &AI) const
Used when sorting the attributes.
AttributeImpl & operator=(const AttributeImpl &)=delete
uint64_t getValueAsInt() const
bool isIntAttribute() const
bool isTypeAttribute() const
static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind, ArrayRef< ConstantRange > Val)
AttributeImpl(AttrEntryKind KindID)
AttributeImpl(const AttributeImpl &)=delete
bool getValueAsBool() const
StringRef getKindAsString() const
StringRef getValueAsString() const
bool isEnumAttribute() const
ArrayRef< ConstantRange > getValueAsConstantRangeList() const
bool isConstantRangeListAttribute() const
bool isStringAttribute() const
static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind, const ConstantRange &CR)
const ConstantRange & getValueAsConstantRange() const
const AttributeSet * iterator
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
AttributeListImpl(ArrayRef< AttributeSet > Sets)
ArrayRef< AttributeSet > getKey() const
AttributeListImpl & operator=(const AttributeListImpl &)=delete
AttributeListImpl(const AttributeListImpl &)=delete
This class represents a group of attributes that apply to one element: function, return type,...
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
CaptureInfo getCaptureInfo() 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
ArrayRef< Attribute > getKey() const
DeadOnReturnInfo getDeadOnReturnInfo() const
const Attribute * iterator
uint64_t getDereferenceableBytes() const
unsigned getNumAttributes() const
Return the number of attributes this AttributeList contains.
AttributeSetNode & operator=(const AttributeSetNode &)=delete
AttributeSetNode(const AttributeSetNode &)=delete
std::string getAsString(bool InAttrGrp) const
static AttributeSetNode * get(LLVMContext &C, const AttrBuilder &B)
FPClassTest getNoFPClass() const
Attribute getAttribute(Attribute::AttrKind Kind) const
This class holds the attributes for a particular argument, parameter, function, or return value.
Definition Attributes.h:407
Functions, function parameters, and return types can have attributes to indicate how they should be t...
Definition Attributes.h:106
AttrKind
This enumeration lists the attributes that can be associated with parameters, function results,...
Definition Attributes.h:125
@ None
No attributes have been set.
Definition Attributes.h:127
@ EndAttrKinds
Sentinel value useful for loops.
Definition Attributes.h:130
static bool isIntAttrKind(AttrKind Kind)
Definition Attributes.h:142
Represents which components of the pointer may be captured in which location.
Definition ModRef.h:414
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.
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)
This class is used to gather all the unique data bits of a node.
Definition FoldingSet.h:162
FoldingSetNode()=default
IntAttributeImpl(Attribute::AttrKind Kind, uint64_t Val)
uint64_t getValue() const
std::pair< unsigned, uint64_t > getKey() const
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
StringAttributeImpl(StringRef Kind, StringRef Val=StringRef())
StringRef getStringKind() const
StringRef getStringValue() const
std::pair< StringRef, StringRef > getKey() const
static size_t totalSizeToAlloc(StringRef Kind, StringRef Val)
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
constexpr size_t size() const
Get the string size.
Definition StringRef.h:144
static constexpr std::enable_if_t< std::is_same_v< Foo< TrailingTys... >, Foo< Tys... > >, size_t > totalSizeToAlloc(typename trailing_objects_internal::ExtractSecondType< TrailingTys, size_t >::type... Counts)
Returns the total size of an object if it were allocated with the given trailing object counts.
std::pair< unsigned, Type * > getKey() const
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:46
This is an optimization pass for GlobalISel generic memory operations.
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:1669
AllocFnKind
Definition Attributes.h:54
MemoryEffectsBase< IRMemLocation > MemoryEffects
Summary of how a function affects memory in the program.
Definition ModRef.h:356
auto uninitialized_copy(R &&Src, IterTy Dst)
Definition STLExtras.h:2111
UWTableKind
Definition CodeGen.h:221
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
OutputIt copy(R &&Range, OutputIt Out)
Definition STLExtras.h:1885
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition Alignment.h:106