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" |
23 | #include "llvm/IR/ConstantRange.h" |
24 | #include "llvm/Support/TrailingObjects.h" |
25 | #include <cassert> |
26 | #include <cstddef> |
27 | #include <cstdint> |
28 | #include <optional> |
29 | #include <string> |
30 | #include <utility> |
31 | |
32 | namespace llvm { |
33 | |
34 | class LLVMContext; |
35 | class 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. |
41 | class AttributeImpl : public FoldingSetNode { |
42 | unsigned char KindID; ///< Holds the AttrEntryKind of the attribute |
43 | |
44 | protected: |
45 | enum AttrEntryKind { |
46 | EnumAttrEntry, |
47 | IntAttrEntry, |
48 | StringAttrEntry, |
49 | TypeAttrEntry, |
50 | ConstantRangeAttrEntry, |
51 | }; |
52 | |
53 | AttributeImpl(AttrEntryKind KindID) : KindID(KindID) {} |
54 | |
55 | public: |
56 | // AttributesImpl is uniqued, these should not be available. |
57 | AttributeImpl(const AttributeImpl &) = delete; |
58 | AttributeImpl &operator=(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; } |
64 | bool isConstantRangeAttribute() const { |
65 | return KindID == ConstantRangeAttrEntry; |
66 | } |
67 | |
68 | bool hasAttribute(Attribute::AttrKind A) const; |
69 | bool hasAttribute(StringRef Kind) const; |
70 | |
71 | Attribute::AttrKind getKindAsEnum() const; |
72 | uint64_t getValueAsInt() const; |
73 | bool getValueAsBool() const; |
74 | |
75 | StringRef getKindAsString() const; |
76 | StringRef getValueAsString() const; |
77 | |
78 | Type *getValueAsType() const; |
79 | |
80 | ConstantRange getValueAsConstantRange() const; |
81 | |
82 | /// Used when sorting the attributes. |
83 | bool operator<(const AttributeImpl &AI) const; |
84 | |
85 | void Profile(FoldingSetNodeID &ID) const { |
86 | if (isEnumAttribute()) |
87 | Profile(ID, Kind: getKindAsEnum()); |
88 | else if (isIntAttribute()) |
89 | Profile(ID, Kind: getKindAsEnum(), Val: getValueAsInt()); |
90 | else if (isStringAttribute()) |
91 | Profile(ID, Kind: getKindAsString(), Values: getValueAsString()); |
92 | else if (isTypeAttribute()) |
93 | Profile(ID, Kind: getKindAsEnum(), Ty: getValueAsType()); |
94 | else |
95 | Profile(ID, Kind: getKindAsEnum(), CR: getValueAsConstantRange()); |
96 | } |
97 | |
98 | static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind) { |
99 | assert(Attribute::isEnumAttrKind(Kind) && "Expected enum attribute" ); |
100 | ID.AddInteger(I: Kind); |
101 | } |
102 | |
103 | static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind, |
104 | uint64_t Val) { |
105 | assert(Attribute::isIntAttrKind(Kind) && "Expected int attribute" ); |
106 | ID.AddInteger(I: Kind); |
107 | ID.AddInteger(I: Val); |
108 | } |
109 | |
110 | static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values) { |
111 | ID.AddString(String: Kind); |
112 | if (!Values.empty()) ID.AddString(String: Values); |
113 | } |
114 | |
115 | static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind, |
116 | Type *Ty) { |
117 | ID.AddInteger(I: Kind); |
118 | ID.AddPointer(Ptr: Ty); |
119 | } |
120 | |
121 | static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind, |
122 | const ConstantRange &CR) { |
123 | ID.AddInteger(I: Kind); |
124 | ID.AddInteger(Int: CR.getLower()); |
125 | ID.AddInteger(Int: CR.getUpper()); |
126 | } |
127 | }; |
128 | |
129 | static_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 | |
139 | class EnumAttributeImpl : public AttributeImpl { |
140 | Attribute::AttrKind Kind; |
141 | |
142 | protected: |
143 | EnumAttributeImpl(AttrEntryKind ID, Attribute::AttrKind Kind) |
144 | : AttributeImpl(ID), Kind(Kind) {} |
145 | |
146 | public: |
147 | EnumAttributeImpl(Attribute::AttrKind Kind) |
148 | : AttributeImpl(EnumAttrEntry), Kind(Kind) { |
149 | assert(Kind != Attribute::AttrKind::None && |
150 | "Can't create a None attribute!" ); |
151 | } |
152 | |
153 | Attribute::AttrKind getEnumKind() const { return Kind; } |
154 | }; |
155 | |
156 | class IntAttributeImpl : public EnumAttributeImpl { |
157 | uint64_t Val; |
158 | |
159 | public: |
160 | IntAttributeImpl(Attribute::AttrKind Kind, uint64_t Val) |
161 | : EnumAttributeImpl(IntAttrEntry, Kind), Val(Val) { |
162 | assert(Attribute::isIntAttrKind(Kind) && |
163 | "Wrong kind for int attribute!" ); |
164 | } |
165 | |
166 | uint64_t getValue() const { return Val; } |
167 | }; |
168 | |
169 | class StringAttributeImpl final |
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 | |
180 | public: |
181 | StringAttributeImpl(StringRef Kind, StringRef Val = StringRef()) |
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(Range&: Kind, Out: TrailingString); |
187 | TrailingString[KindSize] = '\0'; |
188 | llvm::copy(Range&: Val, Out: &TrailingString[KindSize + 1]); |
189 | TrailingString[KindSize + 1 + ValSize] = '\0'; |
190 | } |
191 | |
192 | StringRef getStringKind() const { |
193 | return StringRef(getTrailingObjects<char>(), KindSize); |
194 | } |
195 | StringRef getStringValue() const { |
196 | return StringRef(getTrailingObjects<char>() + KindSize + 1, ValSize); |
197 | } |
198 | |
199 | static size_t totalSizeToAlloc(StringRef Kind, StringRef Val) { |
200 | return TrailingObjects::totalSizeToAlloc<char>(Counts: Kind.size() + 1 + |
201 | Val.size() + 1); |
202 | } |
203 | }; |
204 | |
205 | class TypeAttributeImpl : public EnumAttributeImpl { |
206 | Type *Ty; |
207 | |
208 | public: |
209 | TypeAttributeImpl(Attribute::AttrKind Kind, Type *Ty) |
210 | : EnumAttributeImpl(TypeAttrEntry, Kind), Ty(Ty) {} |
211 | |
212 | Type *getTypeValue() const { return Ty; } |
213 | }; |
214 | |
215 | class ConstantRangeAttributeImpl : public EnumAttributeImpl { |
216 | ConstantRange CR; |
217 | |
218 | public: |
219 | ConstantRangeAttributeImpl(Attribute::AttrKind Kind, const ConstantRange &CR) |
220 | : EnumAttributeImpl(ConstantRangeAttrEntry, Kind), CR(CR) {} |
221 | |
222 | ConstantRange getConstantRangeValue() const { return CR; } |
223 | }; |
224 | |
225 | class AttributeBitSet { |
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 | |
231 | public: |
232 | bool hasAttribute(Attribute::AttrKind Kind) const { |
233 | return AvailableAttrs[Kind / 8] & (1 << (Kind % 8)); |
234 | } |
235 | |
236 | void addAttribute(Attribute::AttrKind Kind) { |
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. |
245 | class AttributeSetNode final |
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 | |
253 | DenseMap<StringRef, Attribute> StringAttrs; |
254 | |
255 | AttributeSetNode(ArrayRef<Attribute> Attrs); |
256 | |
257 | static AttributeSetNode *getSorted(LLVMContext &C, |
258 | ArrayRef<Attribute> SortedAttrs); |
259 | std::optional<Attribute> findEnumAttribute(Attribute::AttrKind Kind) const; |
260 | |
261 | public: |
262 | // AttributesSetNode is uniqued, these should not be available. |
263 | AttributeSetNode(const AttributeSetNode &) = delete; |
264 | AttributeSetNode &operator=(const AttributeSetNode &) = delete; |
265 | |
266 | void operator delete(void *p) { ::operator delete(p); } |
267 | |
268 | static AttributeSetNode *get(LLVMContext &C, const AttrBuilder &B); |
269 | |
270 | static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs); |
271 | |
272 | /// Return the number of attributes this AttributeList contains. |
273 | unsigned getNumAttributes() const { return NumAttrs; } |
274 | |
275 | bool hasAttribute(Attribute::AttrKind Kind) const { |
276 | return AvailableAttrs.hasAttribute(Kind); |
277 | } |
278 | bool hasAttribute(StringRef Kind) const; |
279 | bool hasAttributes() const { return NumAttrs != 0; } |
280 | |
281 | Attribute getAttribute(Attribute::AttrKind Kind) const; |
282 | Attribute getAttribute(StringRef Kind) const; |
283 | |
284 | MaybeAlign getAlignment() const; |
285 | MaybeAlign getStackAlignment() const; |
286 | uint64_t getDereferenceableBytes() const; |
287 | uint64_t getDereferenceableOrNullBytes() const; |
288 | std::optional<std::pair<unsigned, std::optional<unsigned>>> getAllocSizeArgs() |
289 | const; |
290 | unsigned getVScaleRangeMin() const; |
291 | std::optional<unsigned> getVScaleRangeMax() const; |
292 | UWTableKind getUWTableKind() const; |
293 | AllocFnKind getAllocKind() const; |
294 | MemoryEffects getMemoryEffects() const; |
295 | FPClassTest getNoFPClass() const; |
296 | std::string getAsString(bool InAttrGrp) const; |
297 | Type *getAttributeType(Attribute::AttrKind Kind) const; |
298 | |
299 | using iterator = const Attribute *; |
300 | |
301 | iterator begin() const { return getTrailingObjects<Attribute>(); } |
302 | iterator end() const { return begin() + NumAttrs; } |
303 | |
304 | void Profile(FoldingSetNodeID &ID) const { |
305 | Profile(ID, AttrList: ArrayRef(begin(), end())); |
306 | } |
307 | |
308 | static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) { |
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. |
318 | class AttributeListImpl final |
319 | : public FoldingSetNode, |
320 | private TrailingObjects<AttributeListImpl, AttributeSet> { |
321 | friend class AttributeList; |
322 | friend TrailingObjects; |
323 | |
324 | private: |
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 | |
334 | public: |
335 | AttributeListImpl(ArrayRef<AttributeSet> Sets); |
336 | |
337 | // AttributesSetImpt is uniqued, these should not be available. |
338 | AttributeListImpl(const AttributeListImpl &) = delete; |
339 | AttributeListImpl &operator=(const AttributeListImpl &) = delete; |
340 | |
341 | /// Return true if the AttributeSet or the FunctionIndex has an |
342 | /// enum attribute of the given kind. |
343 | bool hasFnAttribute(Attribute::AttrKind Kind) const { |
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. |
350 | bool hasAttrSomewhere(Attribute::AttrKind Kind, |
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; |
359 | static void Profile(FoldingSetNodeID &ID, ArrayRef<AttributeSet> Nodes); |
360 | |
361 | void dump() const; |
362 | }; |
363 | |
364 | static_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 |
370 | |