1 //===- AttributeImpl.h - Attribute Internals --------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
10 /// This file defines various helper methods and classes used by
11 /// LLVMContextImpl for creating and managing attributes.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_LIB_IR_ATTRIBUTEIMPL_H
16 #define LLVM_LIB_IR_ATTRIBUTEIMPL_H
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"
23 #include "llvm/Support/TrailingObjects.h"
36 //===----------------------------------------------------------------------===//
38 /// This class represents a single, uniqued attribute. That attribute
39 /// could be a single enum, a tuple, or a string.
40 class AttributeImpl
: public FoldingSetNode
{
41 unsigned char KindID
; ///< Holds the AttrEntryKind of the attribute
51 AttributeImpl(AttrEntryKind KindID
) : KindID(KindID
) {}
54 // AttributesImpl is uniqued, these should not be available.
55 AttributeImpl(const AttributeImpl
&) = delete;
56 AttributeImpl
&operator=(const AttributeImpl
&) = delete;
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
; }
63 bool hasAttribute(Attribute::AttrKind A
) const;
64 bool hasAttribute(StringRef Kind
) const;
66 Attribute::AttrKind
getKindAsEnum() const;
67 uint64_t getValueAsInt() const;
68 bool getValueAsBool() const;
70 StringRef
getKindAsString() const;
71 StringRef
getValueAsString() const;
73 Type
*getValueAsType() const;
75 /// Used when sorting the attributes.
76 bool operator<(const AttributeImpl
&AI
) const;
78 void Profile(FoldingSetNodeID
&ID
) const {
79 if (isEnumAttribute())
80 Profile(ID
, getKindAsEnum());
81 else if (isIntAttribute())
82 Profile(ID
, getKindAsEnum(), getValueAsInt());
83 else if (isStringAttribute())
84 Profile(ID
, getKindAsString(), getValueAsString());
86 Profile(ID
, getKindAsEnum(), getValueAsType());
89 static void Profile(FoldingSetNodeID
&ID
, Attribute::AttrKind Kind
) {
90 assert(Attribute::isEnumAttrKind(Kind
) && "Expected enum attribute");
94 static void Profile(FoldingSetNodeID
&ID
, Attribute::AttrKind Kind
,
96 assert(Attribute::isIntAttrKind(Kind
) && "Expected int attribute");
101 static void Profile(FoldingSetNodeID
&ID
, StringRef Kind
, StringRef Values
) {
103 if (!Values
.empty()) ID
.AddString(Values
);
106 static void Profile(FoldingSetNodeID
&ID
, Attribute::AttrKind Kind
,
113 static_assert(std::is_trivially_destructible
<AttributeImpl
>::value
,
114 "AttributeImpl should be trivially destructible");
116 //===----------------------------------------------------------------------===//
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.
123 class EnumAttributeImpl
: public AttributeImpl
{
124 Attribute::AttrKind Kind
;
127 EnumAttributeImpl(AttrEntryKind ID
, Attribute::AttrKind Kind
)
128 : AttributeImpl(ID
), Kind(Kind
) {}
131 EnumAttributeImpl(Attribute::AttrKind Kind
)
132 : AttributeImpl(EnumAttrEntry
), Kind(Kind
) {
133 assert(Kind
!= Attribute::AttrKind::None
&&
134 "Can't create a None attribute!");
137 Attribute::AttrKind
getEnumKind() const { return Kind
; }
140 class IntAttributeImpl
: public EnumAttributeImpl
{
144 IntAttributeImpl(Attribute::AttrKind Kind
, uint64_t Val
)
145 : EnumAttributeImpl(IntAttrEntry
, Kind
), Val(Val
) {
146 assert(Attribute::isIntAttrKind(Kind
) &&
147 "Wrong kind for int attribute!");
150 uint64_t getValue() const { return Val
; }
153 class StringAttributeImpl final
154 : public AttributeImpl
,
155 private TrailingObjects
<StringAttributeImpl
, char> {
156 friend TrailingObjects
;
160 size_t numTrailingObjects(OverloadToken
<char>) const {
161 return KindSize
+ 1 + ValSize
+ 1;
165 StringAttributeImpl(StringRef Kind
, StringRef Val
= StringRef())
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';
176 StringRef
getStringKind() const {
177 return StringRef(getTrailingObjects
<char>(), KindSize
);
179 StringRef
getStringValue() const {
180 return StringRef(getTrailingObjects
<char>() + KindSize
+ 1, ValSize
);
183 static size_t totalSizeToAlloc(StringRef Kind
, StringRef Val
) {
184 return TrailingObjects::totalSizeToAlloc
<char>(Kind
.size() + 1 +
189 class TypeAttributeImpl
: public EnumAttributeImpl
{
193 TypeAttributeImpl(Attribute::AttrKind Kind
, Type
*Ty
)
194 : EnumAttributeImpl(TypeAttrEntry
, Kind
), Ty(Ty
) {}
196 Type
*getTypeValue() const { return Ty
; }
199 class AttributeBitSet
{
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");
206 bool hasAttribute(Attribute::AttrKind Kind
) const {
207 return AvailableAttrs
[Kind
/ 8] & (1 << (Kind
% 8));
210 void addAttribute(Attribute::AttrKind Kind
) {
211 AvailableAttrs
[Kind
/ 8] |= 1 << (Kind
% 8);
215 //===----------------------------------------------------------------------===//
217 /// This class represents a group of attributes that apply to one
218 /// element: function, return type, or parameter.
219 class AttributeSetNode final
220 : public FoldingSetNode
,
221 private TrailingObjects
<AttributeSetNode
, Attribute
> {
222 friend TrailingObjects
;
224 unsigned NumAttrs
; ///< Number of attributes in this node.
225 AttributeBitSet AvailableAttrs
; ///< Available enum attributes.
227 DenseMap
<StringRef
, Attribute
> StringAttrs
;
229 AttributeSetNode(ArrayRef
<Attribute
> Attrs
);
231 static AttributeSetNode
*getSorted(LLVMContext
&C
,
232 ArrayRef
<Attribute
> SortedAttrs
);
233 std::optional
<Attribute
> findEnumAttribute(Attribute::AttrKind Kind
) const;
236 // AttributesSetNode is uniqued, these should not be available.
237 AttributeSetNode(const AttributeSetNode
&) = delete;
238 AttributeSetNode
&operator=(const AttributeSetNode
&) = delete;
240 void operator delete(void *p
) { ::operator delete(p
); }
242 static AttributeSetNode
*get(LLVMContext
&C
, const AttrBuilder
&B
);
244 static AttributeSetNode
*get(LLVMContext
&C
, ArrayRef
<Attribute
> Attrs
);
246 /// Return the number of attributes this AttributeList contains.
247 unsigned getNumAttributes() const { return NumAttrs
; }
249 bool hasAttribute(Attribute::AttrKind Kind
) const {
250 return AvailableAttrs
.hasAttribute(Kind
);
252 bool hasAttribute(StringRef Kind
) const;
253 bool hasAttributes() const { return NumAttrs
!= 0; }
255 Attribute
getAttribute(Attribute::AttrKind Kind
) const;
256 Attribute
getAttribute(StringRef Kind
) const;
258 MaybeAlign
getAlignment() const;
259 MaybeAlign
getStackAlignment() const;
260 uint64_t getDereferenceableBytes() const;
261 uint64_t getDereferenceableOrNullBytes() const;
262 std::optional
<std::pair
<unsigned, std::optional
<unsigned>>> getAllocSizeArgs()
264 unsigned getVScaleRangeMin() const;
265 std::optional
<unsigned> getVScaleRangeMax() const;
266 UWTableKind
getUWTableKind() const;
267 AllocFnKind
getAllocKind() const;
268 MemoryEffects
getMemoryEffects() const;
269 FPClassTest
getNoFPClass() const;
270 std::string
getAsString(bool InAttrGrp
) const;
271 Type
*getAttributeType(Attribute::AttrKind Kind
) const;
273 using iterator
= const Attribute
*;
275 iterator
begin() const { return getTrailingObjects
<Attribute
>(); }
276 iterator
end() const { return begin() + NumAttrs
; }
278 void Profile(FoldingSetNodeID
&ID
) const {
279 Profile(ID
, ArrayRef(begin(), end()));
282 static void Profile(FoldingSetNodeID
&ID
, ArrayRef
<Attribute
> AttrList
) {
283 for (const auto &Attr
: AttrList
)
288 //===----------------------------------------------------------------------===//
290 /// This class represents a set of attributes that apply to the function,
291 /// return type, and parameters.
292 class AttributeListImpl final
293 : public FoldingSetNode
,
294 private TrailingObjects
<AttributeListImpl
, AttributeSet
> {
295 friend class AttributeList
;
296 friend TrailingObjects
;
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
;
305 // Helper fn for TrailingObjects class.
306 size_t numTrailingObjects(OverloadToken
<AttributeSet
>) { return NumAttrSets
; }
309 AttributeListImpl(ArrayRef
<AttributeSet
> Sets
);
311 // AttributesSetImpt is uniqued, these should not be available.
312 AttributeListImpl(const AttributeListImpl
&) = delete;
313 AttributeListImpl
&operator=(const AttributeListImpl
&) = delete;
315 /// Return true if the AttributeSet or the FunctionIndex has an
316 /// enum attribute of the given kind.
317 bool hasFnAttribute(Attribute::AttrKind Kind
) const {
318 return AvailableFunctionAttrs
.hasAttribute(Kind
);
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.
324 bool hasAttrSomewhere(Attribute::AttrKind Kind
,
325 unsigned *Index
= nullptr) const;
327 using iterator
= const AttributeSet
*;
329 iterator
begin() const { return getTrailingObjects
<AttributeSet
>(); }
330 iterator
end() const { return begin() + NumAttrSets
; }
332 void Profile(FoldingSetNodeID
&ID
) const;
333 static void Profile(FoldingSetNodeID
&ID
, ArrayRef
<AttributeSet
> Nodes
);
338 static_assert(std::is_trivially_destructible
<AttributeListImpl
>::value
,
339 "AttributeListImpl should be trivially destructible");
341 } // end namespace llvm
343 #endif // LLVM_LIB_IR_ATTRIBUTEIMPL_H