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/FoldingSet.h"
20 #include "llvm/ADT/Optional.h"
21 #include "llvm/IR/Attributes.h"
22 #include "AttributeSetNode.h"
23 #include "llvm/Support/DataTypes.h"
32 //===----------------------------------------------------------------------===//
34 /// \brief This class represents a single, uniqued attribute. That attribute
35 /// could be a single enum, a tuple, or a string.
36 class AttributeImpl : public FoldingSetNode {
37 unsigned char KindID; ///< Holds the AttrEntryKind of the attribute
39 // AttributesImpl is uniqued, these should not be publicly available.
40 void operator=(const AttributeImpl &) = delete;
41 AttributeImpl(const AttributeImpl &) = delete;
50 AttributeImpl(AttrEntryKind KindID) : KindID(KindID) {}
53 virtual ~AttributeImpl();
55 bool isEnumAttribute() const { return KindID == EnumAttrEntry; }
56 bool isIntAttribute() const { return KindID == IntAttrEntry; }
57 bool isStringAttribute() const { return KindID == StringAttrEntry; }
59 bool hasAttribute(Attribute::AttrKind A) const;
60 bool hasAttribute(StringRef Kind) const;
62 Attribute::AttrKind getKindAsEnum() const;
63 uint64_t getValueAsInt() const;
65 StringRef getKindAsString() const;
66 StringRef getValueAsString() const;
68 /// \brief Used when sorting the attributes.
69 bool operator<(const AttributeImpl &AI) const;
71 void Profile(FoldingSetNodeID &ID) const {
72 if (isEnumAttribute())
73 Profile(ID, getKindAsEnum(), 0);
74 else if (isIntAttribute())
75 Profile(ID, getKindAsEnum(), getValueAsInt());
77 Profile(ID, getKindAsString(), getValueAsString());
79 static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind,
82 if (Val) ID.AddInteger(Val);
84 static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values) {
86 if (!Values.empty()) ID.AddString(Values);
89 // FIXME: Remove this!
90 static uint64_t getAttrMask(Attribute::AttrKind Val);
93 //===----------------------------------------------------------------------===//
95 /// \brief A set of classes that contain the value of the
96 /// attribute object. There are three main categories: enum attribute entries,
97 /// represented by Attribute::AttrKind; alignment attribute entries; and string
98 /// attribute enties, which are for target-dependent attributes.
100 class EnumAttributeImpl : public AttributeImpl {
101 virtual void anchor();
102 Attribute::AttrKind Kind;
105 EnumAttributeImpl(AttrEntryKind ID, Attribute::AttrKind Kind)
106 : AttributeImpl(ID), Kind(Kind) {}
109 EnumAttributeImpl(Attribute::AttrKind Kind)
110 : AttributeImpl(EnumAttrEntry), Kind(Kind) {}
112 Attribute::AttrKind getEnumKind() const { return Kind; }
115 class IntAttributeImpl : public EnumAttributeImpl {
116 void anchor() override;
120 IntAttributeImpl(Attribute::AttrKind Kind, uint64_t Val)
121 : EnumAttributeImpl(IntAttrEntry, Kind), Val(Val) {
122 assert((Kind == Attribute::Alignment || Kind == Attribute::StackAlignment ||
123 Kind == Attribute::Dereferenceable ||
124 Kind == Attribute::DereferenceableOrNull ||
125 Kind == Attribute::AllocSize) &&
126 "Wrong kind for int attribute!");
129 uint64_t getValue() const { return Val; }
132 class StringAttributeImpl : public AttributeImpl {
133 virtual void anchor();
138 StringAttributeImpl(StringRef Kind, StringRef Val = StringRef())
139 : AttributeImpl(StringAttrEntry), Kind(Kind), Val(Val) {}
141 StringRef getStringKind() const { return Kind; }
142 StringRef getStringValue() const { return Val; }
145 typedef std::pair<unsigned, AttributeSetNode *> IndexAttrPair;
147 //===----------------------------------------------------------------------===//
149 /// \brief This class represents a set of attributes that apply to the function,
150 /// return type, and parameters.
151 class AttributeSetImpl final
152 : public FoldingSetNode,
153 private TrailingObjects<AttributeSetImpl, IndexAttrPair> {
154 friend class AttributeSet;
155 friend TrailingObjects;
158 LLVMContext &Context;
159 unsigned NumSlots; ///< Number of entries in this set.
160 /// Bitset with a bit for each available attribute Attribute::AttrKind.
161 uint64_t AvailableFunctionAttrs;
163 // Helper fn for TrailingObjects class.
164 size_t numTrailingObjects(OverloadToken<IndexAttrPair>) { return NumSlots; }
166 /// \brief Return a pointer to the IndexAttrPair for the specified slot.
167 const IndexAttrPair *getNode(unsigned Slot) const {
168 return getTrailingObjects<IndexAttrPair>() + Slot;
171 // AttributesSet is uniqued, these should not be publicly available.
172 void operator=(const AttributeSetImpl &) = delete;
173 AttributeSetImpl(const AttributeSetImpl &) = delete;
175 AttributeSetImpl(LLVMContext &C,
176 ArrayRef<std::pair<unsigned, AttributeSetNode *> > Slots)
177 : Context(C), NumSlots(Slots.size()), AvailableFunctionAttrs(0) {
178 static_assert(Attribute::EndAttrKinds <=
179 sizeof(AvailableFunctionAttrs) * CHAR_BIT,
180 "Too many attributes");
183 if (Slots.size() >= 2) {
184 for (const std::pair<unsigned, AttributeSetNode *> *i = Slots.begin() + 1,
187 assert((i-1)->first <= i->first && "Attribute set not ordered!");
191 // There's memory after the node where we can store the entries in.
192 std::copy(Slots.begin(), Slots.end(), getTrailingObjects<IndexAttrPair>());
194 // Initialize AvailableFunctionAttrs summary bitset.
196 static_assert(AttributeSet::FunctionIndex == ~0u,
197 "FunctionIndex should be biggest possible index");
198 const std::pair<unsigned, AttributeSetNode *> &Last = Slots.back();
199 if (Last.first == AttributeSet::FunctionIndex) {
200 const AttributeSetNode *Node = Last.second;
201 for (Attribute I : *Node) {
202 if (!I.isStringAttribute())
203 AvailableFunctionAttrs |= ((uint64_t)1) << I.getKindAsEnum();
209 void operator delete(void *p) { ::operator delete(p); }
211 /// \brief Get the context that created this AttributeSetImpl.
212 LLVMContext &getContext() { return Context; }
214 /// \brief Return the number of slots used in this attribute list. This is
215 /// the number of arguments that have an attribute set on them (including the
216 /// function itself).
217 unsigned getNumSlots() const { return NumSlots; }
219 /// \brief Get the index of the given "slot" in the AttrNodes list. This index
220 /// is the index of the return, parameter, or function object that the
221 /// attributes are applied to, not the index into the AttrNodes list where the
222 /// attributes reside.
223 unsigned getSlotIndex(unsigned Slot) const {
224 return getNode(Slot)->first;
227 /// \brief Retrieve the attributes for the given "slot" in the AttrNode list.
228 /// \p Slot is an index into the AttrNodes list, not the index of the return /
229 /// parameter/ function which the attributes apply to.
230 AttributeSet getSlotAttributes(unsigned Slot) const {
231 return AttributeSet::get(Context, *getNode(Slot));
234 /// \brief Retrieve the attribute set node for the given "slot" in the
236 AttributeSetNode *getSlotNode(unsigned Slot) const {
237 return getNode(Slot)->second;
240 /// \brief Return true if the AttributeSetNode for the FunctionIndex has an
241 /// enum attribute of the given kind.
242 bool hasFnAttribute(Attribute::AttrKind Kind) const {
243 return AvailableFunctionAttrs & ((uint64_t)1) << Kind;
246 typedef AttributeSetNode::iterator iterator;
247 iterator begin(unsigned Slot) const { return getSlotNode(Slot)->begin(); }
248 iterator end(unsigned Slot) const { return getSlotNode(Slot)->end(); }
250 void Profile(FoldingSetNodeID &ID) const {
251 Profile(ID, makeArrayRef(getNode(0), getNumSlots()));
253 static void Profile(FoldingSetNodeID &ID,
254 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Nodes) {
255 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
256 ID.AddInteger(Nodes[i].first);
257 ID.AddPointer(Nodes[i].second);
261 // FIXME: This atrocity is temporary.
262 uint64_t Raw(unsigned Index) const;
267 } // end llvm namespace