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"
34 //===----------------------------------------------------------------------===//
36 /// \brief This class represents a single, uniqued attribute. That attribute
37 /// could be a single enum, a tuple, or a string.
38 class AttributeImpl : public FoldingSetNode {
39 unsigned char KindID; ///< Holds the AttrEntryKind of the attribute
48 AttributeImpl(AttrEntryKind KindID) : KindID(KindID) {}
51 // AttributesImpl is uniqued, these should not be available.
52 AttributeImpl(const AttributeImpl &) = delete;
53 AttributeImpl &operator=(const AttributeImpl &) = delete;
55 virtual ~AttributeImpl();
57 bool isEnumAttribute() const { return KindID == EnumAttrEntry; }
58 bool isIntAttribute() const { return KindID == IntAttrEntry; }
59 bool isStringAttribute() const { return KindID == StringAttrEntry; }
61 bool hasAttribute(Attribute::AttrKind A) const;
62 bool hasAttribute(StringRef Kind) const;
64 Attribute::AttrKind getKindAsEnum() const;
65 uint64_t getValueAsInt() const;
67 StringRef getKindAsString() const;
68 StringRef getValueAsString() const;
70 /// \brief Used when sorting the attributes.
71 bool operator<(const AttributeImpl &AI) const;
73 void Profile(FoldingSetNodeID &ID) const {
74 if (isEnumAttribute())
75 Profile(ID, getKindAsEnum(), 0);
76 else if (isIntAttribute())
77 Profile(ID, getKindAsEnum(), getValueAsInt());
79 Profile(ID, getKindAsString(), getValueAsString());
82 static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind,
85 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 {
119 void anchor() override;
122 IntAttributeImpl(Attribute::AttrKind Kind, uint64_t Val)
123 : EnumAttributeImpl(IntAttrEntry, Kind), Val(Val) {
124 assert((Kind == Attribute::Alignment || Kind == Attribute::StackAlignment ||
125 Kind == Attribute::Dereferenceable ||
126 Kind == Attribute::DereferenceableOrNull ||
127 Kind == Attribute::AllocSize) &&
128 "Wrong kind for int attribute!");
131 uint64_t getValue() const { return Val; }
134 class StringAttributeImpl : public AttributeImpl {
135 virtual void anchor();
140 StringAttributeImpl(StringRef Kind, StringRef Val = StringRef())
141 : AttributeImpl(StringAttrEntry), Kind(Kind), Val(Val) {}
143 StringRef getStringKind() const { return Kind; }
144 StringRef getStringValue() const { return Val; }
147 //===----------------------------------------------------------------------===//
149 /// \brief This class represents a group of attributes that apply to one
150 /// element: function, return type, or parameter.
151 class AttributeSetNode final
152 : public FoldingSetNode,
153 private TrailingObjects<AttributeSetNode, Attribute> {
154 friend TrailingObjects;
156 /// Bitset with a bit for each available attribute Attribute::AttrKind.
157 uint64_t AvailableAttrs;
158 unsigned NumAttrs; ///< Number of attributes in this node.
160 AttributeSetNode(ArrayRef<Attribute> Attrs);
163 // AttributesSetNode is uniqued, these should not be available.
164 AttributeSetNode(const AttributeSetNode &) = delete;
165 AttributeSetNode &operator=(const AttributeSetNode &) = delete;
167 void operator delete(void *p) { ::operator delete(p); }
169 static AttributeSetNode *get(LLVMContext &C, const AttrBuilder &B);
171 static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs);
173 /// \brief Return the number of attributes this AttributeList contains.
174 unsigned getNumAttributes() const { return NumAttrs; }
176 bool hasAttribute(Attribute::AttrKind Kind) const {
177 return AvailableAttrs & ((uint64_t)1) << Kind;
179 bool hasAttribute(StringRef Kind) const;
180 bool hasAttributes() const { return NumAttrs != 0; }
182 Attribute getAttribute(Attribute::AttrKind Kind) const;
183 Attribute getAttribute(StringRef Kind) const;
185 unsigned getAlignment() const;
186 unsigned getStackAlignment() const;
187 uint64_t getDereferenceableBytes() const;
188 uint64_t getDereferenceableOrNullBytes() const;
189 std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
190 std::string getAsString(bool InAttrGrp) const;
192 using iterator = const Attribute *;
194 iterator begin() const { return getTrailingObjects<Attribute>(); }
195 iterator end() const { return begin() + NumAttrs; }
197 void Profile(FoldingSetNodeID &ID) const {
198 Profile(ID, makeArrayRef(begin(), end()));
201 static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) {
202 for (const auto &Attr : AttrList)
207 using IndexAttrPair = std::pair<unsigned, AttributeSet>;
209 //===----------------------------------------------------------------------===//
211 /// \brief This class represents a set of attributes that apply to the function,
212 /// return type, and parameters.
213 class AttributeListImpl final
214 : public FoldingSetNode,
215 private TrailingObjects<AttributeListImpl, IndexAttrPair> {
216 friend class AttributeList;
217 friend TrailingObjects;
220 LLVMContext &Context;
221 unsigned NumSlots; ///< Number of entries in this set.
222 /// Bitset with a bit for each available attribute Attribute::AttrKind.
223 uint64_t AvailableFunctionAttrs;
225 // Helper fn for TrailingObjects class.
226 size_t numTrailingObjects(OverloadToken<IndexAttrPair>) { return NumSlots; }
228 /// \brief Return a pointer to the IndexAttrPair for the specified slot.
229 const IndexAttrPair *getSlotPair(unsigned Slot) const {
230 return getTrailingObjects<IndexAttrPair>() + Slot;
234 AttributeListImpl(LLVMContext &C,
235 ArrayRef<std::pair<unsigned, AttributeSet>> Slots);
237 // AttributesSetImpt is uniqued, these should not be available.
238 AttributeListImpl(const AttributeListImpl &) = delete;
239 AttributeListImpl &operator=(const AttributeListImpl &) = delete;
241 void operator delete(void *p) { ::operator delete(p); }
243 /// \brief Get the context that created this AttributeListImpl.
244 LLVMContext &getContext() { return Context; }
246 /// \brief Return the number of slots used in this attribute list. This is
247 /// the number of arguments that have an attribute set on them (including the
248 /// function itself).
249 unsigned getNumSlots() const { return NumSlots; }
251 /// \brief Get the index of the given "slot" in the AttrNodes list. This index
252 /// is the index of the return, parameter, or function object that the
253 /// attributes are applied to, not the index into the AttrNodes list where the
254 /// attributes reside.
255 unsigned getSlotIndex(unsigned Slot) const {
256 return getSlotPair(Slot)->first;
259 /// \brief Retrieve the attribute set node for the given "slot" in the
261 AttributeSet getSlotAttributes(unsigned Slot) const {
262 return getSlotPair(Slot)->second;
265 /// \brief Return true if the AttributeSet or the FunctionIndex has an
266 /// enum attribute of the given kind.
267 bool hasFnAttribute(Attribute::AttrKind Kind) const {
268 return AvailableFunctionAttrs & ((uint64_t)1) << Kind;
271 using iterator = AttributeSet::iterator;
273 iterator begin(unsigned Slot) const {
274 return getSlotAttributes(Slot).begin();
276 iterator end(unsigned Slot) const { return getSlotAttributes(Slot).end(); }
278 void Profile(FoldingSetNodeID &ID) const;
279 static void Profile(FoldingSetNodeID &ID,
280 ArrayRef<std::pair<unsigned, AttributeSet>> Nodes);
285 } // end namespace llvm
287 #endif // LLVM_LIB_IR_ATTRIBUTEIMPL_H