1 //===-- AttributeImpl.h - Attribute Internals -------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief This file defines various helper methods and classes used by
12 /// LLVMContextImpl for creating and managing attributes.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_LIB_IR_ATTRIBUTEIMPL_H
17 #define LLVM_LIB_IR_ATTRIBUTEIMPL_H
19 #include "llvm/ADT/ArrayRef.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 /// \brief 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
50 AttributeImpl(AttrEntryKind KindID) : KindID(KindID) {}
53 // AttributesImpl is uniqued, these should not be available.
54 AttributeImpl(const AttributeImpl &) = delete;
55 AttributeImpl &operator=(const AttributeImpl &) = delete;
57 virtual ~AttributeImpl();
59 bool isEnumAttribute() const { return KindID == EnumAttrEntry; }
60 bool isIntAttribute() const { return KindID == IntAttrEntry; }
61 bool isStringAttribute() const { return KindID == StringAttrEntry; }
63 bool hasAttribute(Attribute::AttrKind A) const;
64 bool hasAttribute(StringRef Kind) const;
66 Attribute::AttrKind getKindAsEnum() const;
67 uint64_t getValueAsInt() const;
69 StringRef getKindAsString() const;
70 StringRef getValueAsString() const;
72 /// \brief Used when sorting the attributes.
73 bool operator<(const AttributeImpl &AI) const;
75 void Profile(FoldingSetNodeID &ID) const {
76 if (isEnumAttribute())
77 Profile(ID, getKindAsEnum(), 0);
78 else if (isIntAttribute())
79 Profile(ID, getKindAsEnum(), getValueAsInt());
81 Profile(ID, getKindAsString(), getValueAsString());
83 static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind,
86 if (Val) ID.AddInteger(Val);
88 static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values) {
90 if (!Values.empty()) ID.AddString(Values);
94 //===----------------------------------------------------------------------===//
96 /// \brief A set of classes that contain the value of the
97 /// attribute object. There are three main categories: enum attribute entries,
98 /// represented by Attribute::AttrKind; alignment attribute entries; and string
99 /// attribute enties, which are for target-dependent attributes.
101 class EnumAttributeImpl : public AttributeImpl {
102 virtual void anchor();
103 Attribute::AttrKind Kind;
106 EnumAttributeImpl(AttrEntryKind ID, Attribute::AttrKind Kind)
107 : AttributeImpl(ID), Kind(Kind) {}
110 EnumAttributeImpl(Attribute::AttrKind Kind)
111 : AttributeImpl(EnumAttrEntry), Kind(Kind) {}
113 Attribute::AttrKind getEnumKind() const { return Kind; }
116 class IntAttributeImpl : public EnumAttributeImpl {
117 void anchor() override;
121 IntAttributeImpl(Attribute::AttrKind Kind, uint64_t Val)
122 : EnumAttributeImpl(IntAttrEntry, Kind), Val(Val) {
123 assert((Kind == Attribute::Alignment || Kind == Attribute::StackAlignment ||
124 Kind == Attribute::Dereferenceable ||
125 Kind == Attribute::DereferenceableOrNull ||
126 Kind == Attribute::AllocSize) &&
127 "Wrong kind for int attribute!");
130 uint64_t getValue() const { return Val; }
133 class StringAttributeImpl : public AttributeImpl {
134 virtual void anchor();
139 StringAttributeImpl(StringRef Kind, StringRef Val = StringRef())
140 : AttributeImpl(StringAttrEntry), Kind(Kind), Val(Val) {}
142 StringRef getStringKind() const { return Kind; }
143 StringRef getStringValue() const { return Val; }
146 //===----------------------------------------------------------------------===//
148 /// \brief This class represents a group of attributes that apply to one
149 /// element: function, return type, or parameter.
150 class AttributeSetNode final
151 : public FoldingSetNode,
152 private TrailingObjects<AttributeSetNode, Attribute> {
153 friend TrailingObjects;
155 /// Bitset with a bit for each available attribute Attribute::AttrKind.
156 uint64_t AvailableAttrs;
157 unsigned NumAttrs; ///< Number of attributes in this node.
159 AttributeSetNode(ArrayRef<Attribute> Attrs);
162 // AttributesSetNode is uniqued, these should not be available.
163 AttributeSetNode(const AttributeSetNode &) = delete;
164 AttributeSetNode &operator=(const AttributeSetNode &) = delete;
166 void operator delete(void *p) { ::operator delete(p); }
168 static AttributeSetNode *get(LLVMContext &C, const AttrBuilder &B);
170 static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs);
172 /// \brief Return the number of attributes this AttributeList contains.
173 unsigned getNumAttributes() const { return NumAttrs; }
175 bool hasAttribute(Attribute::AttrKind Kind) const {
176 return AvailableAttrs & ((uint64_t)1) << Kind;
178 bool hasAttribute(StringRef Kind) const;
179 bool hasAttributes() const { return NumAttrs != 0; }
181 Attribute getAttribute(Attribute::AttrKind Kind) const;
182 Attribute getAttribute(StringRef Kind) const;
184 unsigned getAlignment() const;
185 unsigned getStackAlignment() const;
186 uint64_t getDereferenceableBytes() const;
187 uint64_t getDereferenceableOrNullBytes() const;
188 std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
189 std::string getAsString(bool InAttrGrp) const;
191 typedef const Attribute *iterator;
192 iterator begin() const { return getTrailingObjects<Attribute>(); }
193 iterator end() const { return begin() + NumAttrs; }
195 void Profile(FoldingSetNodeID &ID) const {
196 Profile(ID, makeArrayRef(begin(), end()));
198 static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) {
199 for (const auto &Attr : AttrList)
204 typedef std::pair<unsigned, AttributeSet> IndexAttrPair;
206 //===----------------------------------------------------------------------===//
208 /// \brief This class represents a set of attributes that apply to the function,
209 /// return type, and parameters.
210 class AttributeListImpl final
211 : public FoldingSetNode,
212 private TrailingObjects<AttributeListImpl, IndexAttrPair> {
213 friend class AttributeList;
214 friend TrailingObjects;
217 LLVMContext &Context;
218 unsigned NumSlots; ///< Number of entries in this set.
219 /// Bitset with a bit for each available attribute Attribute::AttrKind.
220 uint64_t AvailableFunctionAttrs;
222 // Helper fn for TrailingObjects class.
223 size_t numTrailingObjects(OverloadToken<IndexAttrPair>) { return NumSlots; }
225 /// \brief Return a pointer to the IndexAttrPair for the specified slot.
226 const IndexAttrPair *getSlotPair(unsigned Slot) const {
227 return getTrailingObjects<IndexAttrPair>() + Slot;
231 AttributeListImpl(LLVMContext &C,
232 ArrayRef<std::pair<unsigned, AttributeSet>> Slots);
234 // AttributesSetImpt is uniqued, these should not be available.
235 AttributeListImpl(const AttributeListImpl &) = delete;
236 AttributeListImpl &operator=(const AttributeListImpl &) = delete;
238 void operator delete(void *p) { ::operator delete(p); }
240 /// \brief Get the context that created this AttributeListImpl.
241 LLVMContext &getContext() { return Context; }
243 /// \brief Return the number of slots used in this attribute list. This is
244 /// the number of arguments that have an attribute set on them (including the
245 /// function itself).
246 unsigned getNumSlots() const { return NumSlots; }
248 /// \brief Get the index of the given "slot" in the AttrNodes list. This index
249 /// is the index of the return, parameter, or function object that the
250 /// attributes are applied to, not the index into the AttrNodes list where the
251 /// attributes reside.
252 unsigned getSlotIndex(unsigned Slot) const {
253 return getSlotPair(Slot)->first;
256 /// \brief Retrieve the attribute set node for the given "slot" in the
258 AttributeSet getSlotAttributes(unsigned Slot) const {
259 return getSlotPair(Slot)->second;
262 /// \brief Return true if the AttributeSet or the FunctionIndex has an
263 /// enum attribute of the given kind.
264 bool hasFnAttribute(Attribute::AttrKind Kind) const {
265 return AvailableFunctionAttrs & ((uint64_t)1) << Kind;
268 typedef AttributeSet::iterator iterator;
269 iterator begin(unsigned Slot) const {
270 return getSlotAttributes(Slot).begin();
272 iterator end(unsigned Slot) const { return getSlotAttributes(Slot).end(); }
274 void Profile(FoldingSetNodeID &ID) const;
275 static void Profile(FoldingSetNodeID &ID,
276 ArrayRef<std::pair<unsigned, AttributeSet>> Nodes);
281 } // end namespace llvm
283 #endif // LLVM_LIB_IR_ATTRIBUTEIMPL_H