• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/Support/TrailingObjects.h"
24 #include <cassert>
25 #include <cstddef>
26 #include <cstdint>
27 #include <string>
28 #include <utility>
29 
30 namespace llvm {
31 
32 class LLVMContext;
33 class Type;
34 
35 //===----------------------------------------------------------------------===//
36 /// \class
37 /// This class represents a single, uniqued attribute. That attribute
38 /// could be a single enum, a tuple, or a string.
39 class AttributeImpl : public FoldingSetNode {
40   unsigned char KindID; ///< Holds the AttrEntryKind of the attribute
41 
42 protected:
43   enum AttrEntryKind {
44     EnumAttrEntry,
45     IntAttrEntry,
46     StringAttrEntry,
47     TypeAttrEntry,
48   };
49 
AttributeImpl(AttrEntryKind KindID)50   AttributeImpl(AttrEntryKind KindID) : KindID(KindID) {}
51 
52 public:
53   // AttributesImpl is uniqued, these should not be available.
54   AttributeImpl(const AttributeImpl &) = delete;
55   AttributeImpl &operator=(const AttributeImpl &) = delete;
56 
isEnumAttribute()57   bool isEnumAttribute() const { return KindID == EnumAttrEntry; }
isIntAttribute()58   bool isIntAttribute() const { return KindID == IntAttrEntry; }
isStringAttribute()59   bool isStringAttribute() const { return KindID == StringAttrEntry; }
isTypeAttribute()60   bool isTypeAttribute() const { return KindID == TypeAttrEntry; }
61 
62   bool hasAttribute(Attribute::AttrKind A) const;
63   bool hasAttribute(StringRef Kind) const;
64 
65   Attribute::AttrKind getKindAsEnum() const;
66   uint64_t getValueAsInt() const;
67 
68   StringRef getKindAsString() const;
69   StringRef getValueAsString() const;
70 
71   Type *getValueAsType() const;
72 
73   /// Used when sorting the attributes.
74   bool operator<(const AttributeImpl &AI) const;
75 
Profile(FoldingSetNodeID & ID)76   void Profile(FoldingSetNodeID &ID) const {
77     if (isEnumAttribute())
78       Profile(ID, getKindAsEnum(), static_cast<uint64_t>(0));
79     else if (isIntAttribute())
80       Profile(ID, getKindAsEnum(), getValueAsInt());
81     else if (isStringAttribute())
82       Profile(ID, getKindAsString(), getValueAsString());
83     else
84       Profile(ID, getKindAsEnum(), getValueAsType());
85   }
86 
Profile(FoldingSetNodeID & ID,Attribute::AttrKind Kind,uint64_t Val)87   static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind,
88                       uint64_t Val) {
89     ID.AddInteger(Kind);
90     if (Val) ID.AddInteger(Val);
91   }
92 
Profile(FoldingSetNodeID & ID,StringRef Kind,StringRef Values)93   static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values) {
94     ID.AddString(Kind);
95     if (!Values.empty()) ID.AddString(Values);
96   }
97 
Profile(FoldingSetNodeID & ID,Attribute::AttrKind Kind,Type * Ty)98   static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind,
99                       Type *Ty) {
100     ID.AddInteger(Kind);
101     ID.AddPointer(Ty);
102   }
103 };
104 
105 static_assert(std::is_trivially_destructible<AttributeImpl>::value,
106               "AttributeImpl should be trivially destructible");
107 
108 //===----------------------------------------------------------------------===//
109 /// \class
110 /// A set of classes that contain the value of the
111 /// attribute object. There are three main categories: enum attribute entries,
112 /// represented by Attribute::AttrKind; alignment attribute entries; and string
113 /// attribute enties, which are for target-dependent attributes.
114 
115 class EnumAttributeImpl : public AttributeImpl {
116   Attribute::AttrKind Kind;
117 
118 protected:
EnumAttributeImpl(AttrEntryKind ID,Attribute::AttrKind Kind)119   EnumAttributeImpl(AttrEntryKind ID, Attribute::AttrKind Kind)
120       : AttributeImpl(ID), Kind(Kind) {}
121 
122 public:
EnumAttributeImpl(Attribute::AttrKind Kind)123   EnumAttributeImpl(Attribute::AttrKind Kind)
124       : AttributeImpl(EnumAttrEntry), Kind(Kind) {
125     assert(Kind != Attribute::AttrKind::None &&
126            "Can't create a None attribute!");
127   }
128 
getEnumKind()129   Attribute::AttrKind getEnumKind() const { return Kind; }
130 };
131 
132 class IntAttributeImpl : public EnumAttributeImpl {
133   uint64_t Val;
134 
135 public:
IntAttributeImpl(Attribute::AttrKind Kind,uint64_t Val)136   IntAttributeImpl(Attribute::AttrKind Kind, uint64_t Val)
137       : EnumAttributeImpl(IntAttrEntry, Kind), Val(Val) {
138     assert(Attribute::doesAttrKindHaveArgument(Kind) &&
139            "Wrong kind for int attribute!");
140   }
141 
getValue()142   uint64_t getValue() const { return Val; }
143 };
144 
145 class StringAttributeImpl final
146     : public AttributeImpl,
147       private TrailingObjects<StringAttributeImpl, char> {
148   friend TrailingObjects;
149 
150   unsigned KindSize;
151   unsigned ValSize;
numTrailingObjects(OverloadToken<char>)152   size_t numTrailingObjects(OverloadToken<char>) const {
153     return KindSize + 1 + ValSize + 1;
154   }
155 
156 public:
157   StringAttributeImpl(StringRef Kind, StringRef Val = StringRef())
AttributeImpl(StringAttrEntry)158       : AttributeImpl(StringAttrEntry), KindSize(Kind.size()),
159         ValSize(Val.size()) {
160     char *TrailingString = getTrailingObjects<char>();
161     // Some users rely on zero-termination.
162     llvm::copy(Kind, TrailingString);
163     TrailingString[KindSize] = '\0';
164     llvm::copy(Val, &TrailingString[KindSize + 1]);
165     TrailingString[KindSize + 1 + ValSize] = '\0';
166   }
167 
getStringKind()168   StringRef getStringKind() const {
169     return StringRef(getTrailingObjects<char>(), KindSize);
170   }
getStringValue()171   StringRef getStringValue() const {
172     return StringRef(getTrailingObjects<char>() + KindSize + 1, ValSize);
173   }
174 
totalSizeToAlloc(StringRef Kind,StringRef Val)175   static size_t totalSizeToAlloc(StringRef Kind, StringRef Val) {
176     return TrailingObjects::totalSizeToAlloc<char>(Kind.size() + 1 +
177                                                    Val.size() + 1);
178   }
179 };
180 
181 class TypeAttributeImpl : public EnumAttributeImpl {
182   Type *Ty;
183 
184 public:
TypeAttributeImpl(Attribute::AttrKind Kind,Type * Ty)185   TypeAttributeImpl(Attribute::AttrKind Kind, Type *Ty)
186       : EnumAttributeImpl(TypeAttrEntry, Kind), Ty(Ty) {}
187 
getTypeValue()188   Type *getTypeValue() const { return Ty; }
189 };
190 
191 class AttributeBitSet {
192   /// Bitset with a bit for each available attribute Attribute::AttrKind.
193   uint8_t AvailableAttrs[12] = {};
194   static_assert(Attribute::EndAttrKinds <= sizeof(AvailableAttrs) * CHAR_BIT,
195                 "Too many attributes");
196 
197 public:
hasAttribute(Attribute::AttrKind Kind)198   bool hasAttribute(Attribute::AttrKind Kind) const {
199     return AvailableAttrs[Kind / 8] & (1 << (Kind % 8));
200   }
201 
addAttribute(Attribute::AttrKind Kind)202   void addAttribute(Attribute::AttrKind Kind) {
203     AvailableAttrs[Kind / 8] |= 1 << (Kind % 8);
204   }
205 };
206 
207 //===----------------------------------------------------------------------===//
208 /// \class
209 /// This class represents a group of attributes that apply to one
210 /// element: function, return type, or parameter.
211 class AttributeSetNode final
212     : public FoldingSetNode,
213       private TrailingObjects<AttributeSetNode, Attribute> {
214   friend TrailingObjects;
215 
216   unsigned NumAttrs; ///< Number of attributes in this node.
217   AttributeBitSet AvailableAttrs; ///< Available enum attributes.
218 
219   DenseMap<StringRef, Attribute> StringAttrs;
220 
221   AttributeSetNode(ArrayRef<Attribute> Attrs);
222 
223   static AttributeSetNode *getSorted(LLVMContext &C,
224                                      ArrayRef<Attribute> SortedAttrs);
225   Optional<Attribute> findEnumAttribute(Attribute::AttrKind Kind) const;
226 
227 public:
228   // AttributesSetNode is uniqued, these should not be available.
229   AttributeSetNode(const AttributeSetNode &) = delete;
230   AttributeSetNode &operator=(const AttributeSetNode &) = delete;
231 
delete(void * p)232   void operator delete(void *p) { ::operator delete(p); }
233 
234   static AttributeSetNode *get(LLVMContext &C, const AttrBuilder &B);
235 
236   static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs);
237 
238   /// Return the number of attributes this AttributeList contains.
getNumAttributes()239   unsigned getNumAttributes() const { return NumAttrs; }
240 
hasAttribute(Attribute::AttrKind Kind)241   bool hasAttribute(Attribute::AttrKind Kind) const {
242     return AvailableAttrs.hasAttribute(Kind);
243   }
244   bool hasAttribute(StringRef Kind) const;
hasAttributes()245   bool hasAttributes() const { return NumAttrs != 0; }
246 
247   Attribute getAttribute(Attribute::AttrKind Kind) const;
248   Attribute getAttribute(StringRef Kind) const;
249 
250   MaybeAlign getAlignment() const;
251   MaybeAlign getStackAlignment() const;
252   uint64_t getDereferenceableBytes() const;
253   uint64_t getDereferenceableOrNullBytes() const;
254   std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
255   std::string getAsString(bool InAttrGrp) const;
256   Type *getByValType() const;
257   Type *getStructRetType() const;
258   Type *getByRefType() const;
259   Type *getPreallocatedType() const;
260 
261   using iterator = const Attribute *;
262 
begin()263   iterator begin() const { return getTrailingObjects<Attribute>(); }
end()264   iterator end() const { return begin() + NumAttrs; }
265 
Profile(FoldingSetNodeID & ID)266   void Profile(FoldingSetNodeID &ID) const {
267     Profile(ID, makeArrayRef(begin(), end()));
268   }
269 
Profile(FoldingSetNodeID & ID,ArrayRef<Attribute> AttrList)270   static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) {
271     for (const auto &Attr : AttrList)
272       Attr.Profile(ID);
273   }
274 };
275 
276 //===----------------------------------------------------------------------===//
277 /// \class
278 /// This class represents a set of attributes that apply to the function,
279 /// return type, and parameters.
280 class AttributeListImpl final
281     : public FoldingSetNode,
282       private TrailingObjects<AttributeListImpl, AttributeSet> {
283   friend class AttributeList;
284   friend TrailingObjects;
285 
286 private:
287   unsigned NumAttrSets; ///< Number of entries in this set.
288   /// Available enum function attributes.
289   AttributeBitSet AvailableFunctionAttrs;
290   /// Union of enum attributes available at any index.
291   AttributeBitSet AvailableSomewhereAttrs;
292 
293   // Helper fn for TrailingObjects class.
numTrailingObjects(OverloadToken<AttributeSet>)294   size_t numTrailingObjects(OverloadToken<AttributeSet>) { return NumAttrSets; }
295 
296 public:
297   AttributeListImpl(ArrayRef<AttributeSet> Sets);
298 
299   // AttributesSetImpt is uniqued, these should not be available.
300   AttributeListImpl(const AttributeListImpl &) = delete;
301   AttributeListImpl &operator=(const AttributeListImpl &) = delete;
302 
303   /// Return true if the AttributeSet or the FunctionIndex has an
304   /// enum attribute of the given kind.
hasFnAttribute(Attribute::AttrKind Kind)305   bool hasFnAttribute(Attribute::AttrKind Kind) const {
306     return AvailableFunctionAttrs.hasAttribute(Kind);
307   }
308 
309   /// Return true if the specified attribute is set for at least one
310   /// parameter or for the return value. If Index is not nullptr, the index
311   /// of a parameter with the specified attribute is provided.
312   bool hasAttrSomewhere(Attribute::AttrKind Kind,
313                         unsigned *Index = nullptr) const;
314 
315   using iterator = const AttributeSet *;
316 
begin()317   iterator begin() const { return getTrailingObjects<AttributeSet>(); }
end()318   iterator end() const { return begin() + NumAttrSets; }
319 
320   void Profile(FoldingSetNodeID &ID) const;
321   static void Profile(FoldingSetNodeID &ID, ArrayRef<AttributeSet> Nodes);
322 
323   void dump() const;
324 };
325 
326 static_assert(std::is_trivially_destructible<AttributeListImpl>::value,
327               "AttributeListImpl should be trivially destructible");
328 
329 } // end namespace llvm
330 
331 #endif // LLVM_LIB_IR_ATTRIBUTEIMPL_H
332